CMakeLists.txt 22.9 KB
Newer Older
1
# ******************************************************************************
2
# Copyright 2017-2019 Intel Corporation
3
#
Robert Kimball's avatar
Robert Kimball committed
4 5 6
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
7
#
Robert Kimball's avatar
Robert Kimball committed
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
Robert Kimball's avatar
Robert Kimball committed
10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
15
# ******************************************************************************
Robert Kimball's avatar
Robert Kimball committed
16

17
cmake_minimum_required (VERSION 3.4)
Robert Kimball's avatar
Robert Kimball committed
18

19 20 21
# set directory where the custom finders live
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

22 23 24 25
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
    message(FATAL_ERROR "In-source builds are not allowed.")
endif()

26 27 28 29
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
    message(FATAL_ERROR "Ninja generator not supported.")
endif()

30 31 32 33 34 35 36 37
if (CMAKE_BUILD_TYPE)
    set(RELEASE_TYPES Debug Release RelWithDebInfo MinSizeRel)
    list(FIND RELEASE_TYPES ${CMAKE_BUILD_TYPE} INDEX_FOUND)
    if (${INDEX_FOUND} EQUAL -1)
        message(FATAL_ERROR "CMAKE_BUILD_TYPE must be one of Debug, Release, RelWithDebInfo, or MinSizeRel")
    endif()
endif()

38 39
include(git_tags)
include(var_functions)
40

41 42
NGRAPH_GET_VERSION_LABEL()

Sang Ik Lee's avatar
Sang Ik Lee committed
43
string(REGEX MATCH "([0-9?]+)\\.([0-9?]+)\\.([0-9?]+)(-(rc|dev)\\.[0-9?]+)?" NGRAPH_VERSION_SHORT "${NGRAPH_VERSION_LABEL}")
44
string(REGEX REPLACE "-rc." "rc" NGRAPH_WHEEL_VERSION "${NGRAPH_VERSION_SHORT}")
Sang Ik Lee's avatar
Sang Ik Lee committed
45
string(REGEX REPLACE "-dev." "dev" NGRAPH_WHEEL_VERSION "${NGRAPH_WHEEL_VERSION}")
46
string(REGEX MATCH "([0-9?]+)\\.([0-9?]+)" NGRAPH_API_VERSION "${NGRAPH_VERSION_LABEL}")
47 48 49 50 51
string(REGEX MATCH "[^v](.*)" NGRAPH_VERSION "${NGRAPH_VERSION_LABEL}")
string(REPLACE "." ";" NGRAPH_VERSION_PARTS "${NGRAPH_VERSION_SHORT}")
list(GET NGRAPH_VERSION_PARTS 0 NGRAPH_VERSION_MAJOR)
list(GET NGRAPH_VERSION_PARTS 1 NGRAPH_VERSION_MINOR)
list(GET NGRAPH_VERSION_PARTS 2 NGRAPH_VERSION_PATCH)
52 53 54 55
configure_file(VERSION.in VERSION)

message(STATUS "NGRAPH_VERSION ${NGRAPH_VERSION}")
message(STATUS "NGRAPH_VERSION_SHORT ${NGRAPH_VERSION_SHORT}")
56
message(STATUS "NGRAPH_WHEEL_VERSION ${NGRAPH_WHEEL_VERSION}")
57 58
message(STATUS "NGRAPH_API_VERSION ${NGRAPH_API_VERSION}")

59 60 61
set(NGRAPH_INCLUDE_PATH
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)
Sang Ik Lee's avatar
Sang Ik Lee committed
62 63

if (APPLE)
64 65
    # Enable MACOS_RPATH by default.
    cmake_policy(SET CMP0042 NEW)
66 67
    # Enable CMAKE_<LANG>_COMPILER_ID AppleClang
    cmake_policy(SET CMP0025 NEW)
68
endif()
Robert Kimball's avatar
Robert Kimball committed
69

70
project (ngraph)
Robert Kimball's avatar
Robert Kimball committed
71

72 73 74 75
if (UNIX AND NOT APPLE)
    set(LINUX TRUE)
endif()

76 77 78 79
if ("${CMAKE_GENERATOR}" MATCHES "^Visual Studio.*$")
    set(MSVS TRUE)
endif()

80 81
# APPLE: Set CMAKE_OSX_SYSROOT if not set already.
if (APPLE)
82 83 84 85 86 87 88
    execute_process(COMMAND sw_vers -productVersion
        OUTPUT_VARIABLE OSX_FULL_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE)

    string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*$" "\\1"
        OSX_SHORT_VERSION "${OSX_FULL_VERSION}")

89 90 91 92 93 94 95 96 97 98
    message(STATUS "Detected: OSX ${OSX_SHORT_VERSION}")

    if (CMAKE_OSX_SYSROOT)
        message(STATUS "Using CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT}")
    else()
        execute_process(COMMAND xcode-select -p
            OUTPUT_VARIABLE APPLE_DEV_DIR
            OUTPUT_STRIP_TRAILING_WHITESPACE)

        if("${APPLE_DEV_DIR}" STREQUAL "/Library/Developer/CommandLineTools")
99
            # Command line tools only
100
            set(XCODE_ISYSROOT ${APPLE_DEV_DIR}/SDKs/MacOSX.sdk)
101 102 103 104
            message(STATUS "Trying command line tool sdk at ${XCODE_ISYSROOT}.")
            if(NOT EXISTS ${XCODE_ISYSROOT})
                message(FATAL_ERROR "Cannot find macos sdk.")
            endif()
105 106 107 108 109 110 111
        else()
            # Xcode is installed
            set(XCODE_ISYSROOT ${APPLE_DEV_DIR}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${OSX_SHORT_VERSION}.sdk)
            message(STATUS "Trying Xcode sdk at ${XCODE_ISYSROOT}.")
            if(NOT EXISTS ${XCODE_ISYSROOT})
                message(FATAL_ERROR "Cannot find macos sdk.")
            endif()
112 113 114 115 116 117
        endif()
        message(STATUS "Setting CMAKE_OSX_SYSROOT for macos ${OSX_SHORT_VERSION} to ${XCODE_ISYSROOT}")
        set(CMAKE_OSX_SYSROOT ${XCODE_ISYSROOT})
    endif()
endif()

118 119 120 121 122 123 124 125 126 127
set(NGRAPH_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD ${NGRAPH_CXX_STANDARD})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Create compilation database compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

128
# Setup CMAKE_ARGS to be forwarded to External Projects
129 130 131 132 133 134 135 136 137 138
set(NGRAPH_FORWARD_CMAKE_ARGS
    -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    -DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}
    -DCMAKE_CXX_STANDARD_REQUIRED:BOOL=${CMAKE_CXX_STANDARD_REQUIRED}
    -DCMAKE_CXX_EXTENSIONS:BOOL=${CMAKE_CXX_EXTENSIONS}
    -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=${CMAKE_EXPORT_COMPILE_COMMANDS}
    -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=${CMAKE_POSITION_INDEPENDENT_CODE}
    )

139 140
if (CMAKE_OSX_SYSROOT)
    set(NGRAPH_FORWARD_CMAKE_ARGS
141 142 143
            ${NGRAPH_FORWARD_CMAKE_ARGS}
            -DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}
            )
144
endif()
145 146 147

if (NOT MSVS)
    if(NOT CMAKE_BUILD_TYPE)
148
        set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
149
    endif()
150 151 152 153 154

    set(NGRAPH_FORWARD_CMAKE_ARGS
            ${NGRAPH_FORWARD_CMAKE_ARGS}
            -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
            )
155 156 157
endif()
message(STATUS "NGRAPH_FORWARD_CMAKE_ARGS ${NGRAPH_FORWARD_CMAKE_ARGS}")

158
# Prevent Eigen from using any LGPL3 code
159
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_MPL2_ONLY -DTBB_USE_THREADING_TOOLS")
160

161
ngraph_var(NGRAPH_USE_PREBUILT_LLVM DEFAULT "FALSE")
162

163 164 165
option(NGRAPH_UNIT_TEST_ENABLE "Control the building of unit tests" TRUE)
option(NGRAPH_TOOLS_ENABLE "Control the building of tool" TRUE)
option(NGRAPH_CPU_ENABLE "Control the building of the CPU backend" TRUE)
166
option(NGRAPH_MLIR_ENABLE "Control the building of MLIR backend" FALSE)
167
option(NGRAPH_INTELGPU_ENABLE "Control the building of the Intel GPU backend with clDNN" FALSE)
168
option(NGRAPH_GPU_ENABLE "Control the building of the GPU backend" FALSE)
169
option(NGRAPH_INTERPRETER_ENABLE "Control the building of the INTERPRETER backend" TRUE)
Robert Kimball's avatar
Robert Kimball committed
170
option(NGRAPH_NOP_ENABLE "Control the building of the NOP backend" TRUE)
171
option(NGRAPH_GPUH_ENABLE "Control the building of the Hybrid GPU backend" FALSE)
172
option(NGRAPH_GENERIC_CPU_ENABLE "Enable build nGraph for generic CPU backend" FALSE)
173
option(NGRAPH_DEBUG_ENABLE "Enable output for NGRAPH_DEBUG statements" FALSE)
174
option(NGRAPH_DEPRECATED_ENABLE "Enable compiler deprecation pragmas for deprecated APIs (recommended only for development use)" FALSE)
175
option(NGRAPH_ONNX_IMPORT_ENABLE "Enable ONNX importer" FALSE)
176
option(NGRAPH_DEX_ONLY "Build CPU DEX without codegen" FALSE)
177
option(NGRAPH_ENABLE_CPU_CONV_AUTO "Enable mkldnn convolution_auto for CPU" TRUE)
178
option(NGRAPH_CODE_COVERAGE_ENABLE "Enable code coverage data collection" FALSE)
179
option(NGRAPH_LIB_VERSIONING_ENABLE "Enable shared library versioning" FALSE)
180
option(NGRAPH_PYTHON_BUILD_ENABLE "Enable build nGraph python package wheel" FALSE)
181
option(NGRAPH_PLAIDML_ENABLE "Enable the PlaidML backend" ${PLAIDML_FOUND})
182
option(NGRAPH_DISTRIBUTED_ENABLE "Enable distributed training using MLSL/OpenMPI" OFF)
183
option(NGRAPH_FAST_MATH_ENABLE "Enable fast math" ON)
184
option(NGRAPH_JSON_ENABLE "Enable JSON based serialization and tracing features" TRUE)
185
option(NGRAPH_STATIC_LIB_ENABLE "Enable build nGraph static library" FALSE)
Robert Kimball's avatar
Robert Kimball committed
186
option(NGRAPH_INTERPRETER_STATIC_LIB_ENABLE "Enable build INTERPRETER backend static library" FALSE)
187
option(NGRAPH_CPU_STATIC_LIB_ENABLE "Enable build CPU backend static library" FALSE)
188 189 190

if (NGRAPH_CPU_ENABLE
        AND
191
    ((NOT NGRAPH_GPU_ENABLE) AND (NOT NGRAPH_GPUH_ENABLE)
192 193 194 195
        AND (NOT NGRAPH_GENERIC_CPU_ENABLE) AND (NOT NGRAPH_INTELGPU_ENABLE))
   )
    set(NGRAPH_INTEL_CPU_ONLY_ENABLE ON)
endif()
196

nmostafa's avatar
nmostafa committed
197
if (NGRAPH_MLIR_ENABLE AND (NOT NGRAPH_DEX_ONLY))
198
    #disable code-gen due to sym collision with LLVM
nmostafa's avatar
nmostafa committed
199 200
    message(FATAL_ERROR "Cannot build with both MLIR and code-gen ON.\n"
                        "Use -DNGRAPH_DEX_ONLY=ON and try again. \n")
201 202
endif()

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
if (NGRAPH_DISTRIBUTED_ENABLE)
    if ("${NGRAPH_DISTRIBUTED_ENABLE}" STREQUAL "MLSL")
        if (NGRAPH_INTEL_CPU_ONLY_ENABLE)
            set(NGRAPH_DISTRIBUTED_MLSL_ENABLE TRUE)
        else()
            message(FATAL_ERROR
                    "-DNGRAPH_DISTRIBUTED_ENABLE=MLSL to be used, if Intel CPU is the only backend enabled.\n"
                    "Use -DNGRAPH_DISTRIBUTED_ENABLE=OMPI for all other situations.\n")
        endif()
    elseif("${NGRAPH_DISTRIBUTED_ENABLE}" STREQUAL  "OMPI")
        set(NGRAPH_DISTRIBUTED_OMPI_ENABLE TRUE)
    else()
        message(FATAL_ERROR
                    "Invalid arguments passed to NGRAPH_DISTRIBUTED_ENABLE, must select  one of  MLSL, OMPI or OFF.\n"
                    "If using Intel CPU only backend, recommend Intel MLSL by setting -DNGRAPH_DISTRIBUTED_ENABLE=MLSL .\n")
    endif()
219 220
endif()

221 222
if (NGRAPH_GPUH_ENABLE)
    set(NGRAPH_GPU_ENABLE TRUE)
223 224
endif()

225 226 227 228 229
if (NGRAPH_ONNX_IMPORT_ENABLE)
    option(NGRAPH_USE_SYSTEM_PROTOBUF "Use system provided Protobuf shared object" FALSE)
    option(NGRAPH_ONNXIFI_ENABLE "Enable ONNX Interface for Framework Integration" TRUE)
endif()

230 231 232 233
if (NOT NGRAPH_JSON_ENABLE)
    set(NGRAPH_TOOLS_ENABLE FALSE)
endif()

234 235 236 237 238 239 240 241 242 243 244
macro (NORMALIZE_BOOL VAL)
    if (${VAL})
        set(${VAL} ON)
    else()
        set(${VAL} OFF)
    endif()
endmacro()

NORMALIZE_BOOL(NGRAPH_UNIT_TEST_ENABLE)
NORMALIZE_BOOL(NGRAPH_TOOLS_ENABLE)
NORMALIZE_BOOL(NGRAPH_CPU_ENABLE)
245
NORMALIZE_BOOL(NGRAPH_MLIR_ENABLE)
246 247 248 249 250 251 252
NORMALIZE_BOOL(NGRAPH_INTELGPU_ENABLE)
NORMALIZE_BOOL(NGRAPH_GPU_ENABLE)
NORMALIZE_BOOL(NGRAPH_INTERPRETER_ENABLE)
NORMALIZE_BOOL(NGRAPH_NOP_ENABLE)
NORMALIZE_BOOL(NGRAPH_GPUH_ENABLE)
NORMALIZE_BOOL(NGRAPH_GENERIC_CPU_ENABLE)
NORMALIZE_BOOL(NGRAPH_DEBUG_ENABLE)
253
NORMALIZE_BOOL(NGRAPH_DEPRECATED_ENABLE)
254 255
NORMALIZE_BOOL(NGRAPH_ONNX_IMPORT_ENABLE)
NORMALIZE_BOOL(NGRAPH_DEX_ONLY)
256
NORMALIZE_BOOL(NGRAPH_ENABLE_CPU_CONV_AUTO)
257 258 259 260 261
NORMALIZE_BOOL(NGRAPH_CODE_COVERAGE_ENABLE)
NORMALIZE_BOOL(NGRAPH_LIB_VERSIONING_ENABLE)
NORMALIZE_BOOL(NGRAPH_PYTHON_BUILD_ENABLE)
NORMALIZE_BOOL(NGRAPH_USE_PREBUILT_LLVM)
NORMALIZE_BOOL(NGRAPH_PLAIDML_ENABLE)
262
NORMALIZE_BOOL(NGRAPH_JSON_ENABLE)
263
NORMALIZE_BOOL(NGRAPH_STATIC_LIB_ENABLE)
Robert Kimball's avatar
Robert Kimball committed
264
NORMALIZE_BOOL(NGRAPH_INTERPRETER_STATIC_LIB_ENABLE)
265
NORMALIZE_BOOL(NGRAPH_CPU_STATIC_LIB_ENABLE)
Robert Kimball's avatar
Robert Kimball committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

message(STATUS "NGRAPH_UNIT_TEST_ENABLE:              ${NGRAPH_UNIT_TEST_ENABLE}")
message(STATUS "NGRAPH_TOOLS_ENABLE:                  ${NGRAPH_TOOLS_ENABLE}")
message(STATUS "NGRAPH_CPU_ENABLE:                    ${NGRAPH_CPU_ENABLE}")
message(STATUS "NGRAPH_MLIR_ENABLE:                   ${NGRAPH_MLIR_ENABLE}")
message(STATUS "NGRAPH_INTELGPU_ENABLE:               ${NGRAPH_INTELGPU_ENABLE}")
message(STATUS "NGRAPH_GPU_ENABLE:                    ${NGRAPH_GPU_ENABLE}")
message(STATUS "NGRAPH_INTERPRETER_ENABLE:            ${NGRAPH_INTERPRETER_ENABLE}")
message(STATUS "NGRAPH_NOP_ENABLE:                    ${NGRAPH_NOP_ENABLE}")
message(STATUS "NGRAPH_GPUH_ENABLE:                   ${NGRAPH_GPUH_ENABLE}")
message(STATUS "NGRAPH_GENERIC_CPU_ENABLE:            ${NGRAPH_GENERIC_CPU_ENABLE}")
message(STATUS "NGRAPH_DEBUG_ENABLE:                  ${NGRAPH_DEBUG_ENABLE}")
message(STATUS "NGRAPH_DEPRECATED_ENABLE:             ${NGRAPH_DEPRECATED_ENABLE}")
message(STATUS "NGRAPH_ONNX_IMPORT_ENABLE:            ${NGRAPH_ONNX_IMPORT_ENABLE}")
message(STATUS "NGRAPH_DEX_ONLY:                      ${NGRAPH_DEX_ONLY}")
message(STATUS "NGRAPH_ENABLE_CPU_CONV_AUTO:          ${NGRAPH_ENABLE_CPU_CONV_AUTO}")
message(STATUS "NGRAPH_CODE_COVERAGE_ENABLE:          ${NGRAPH_CODE_COVERAGE_ENABLE}")
message(STATUS "NGRAPH_LIB_VERSIONING_ENABLE:         ${NGRAPH_LIB_VERSIONING_ENABLE}")
message(STATUS "NGRAPH_PYTHON_BUILD_ENABLE:           ${NGRAPH_PYTHON_BUILD_ENABLE}")
message(STATUS "NGRAPH_USE_PREBUILT_LLVM:             ${NGRAPH_USE_PREBUILT_LLVM}")
message(STATUS "NGRAPH_PLAIDML_ENABLE:                ${NGRAPH_PLAIDML_ENABLE}")
message(STATUS "NGRAPH_DISTRIBUTED_ENABLE:            ${NGRAPH_DISTRIBUTED_ENABLE}")
message(STATUS "NGRAPH_JSON_ENABLE:                   ${NGRAPH_JSON_ENABLE}")
message(STATUS "NGRAPH_STATIC_LIB_ENABLE:             ${NGRAPH_STATIC_LIB_ENABLE}")
message(STATUS "NGRAPH_INTERPRETER_STATIC_LIB_ENABLE: ${NGRAPH_INTERPRETER_STATIC_LIB_ENABLE}")
291
message(STATUS "NGRAPH_CPU_STATIC_LIB_ENABLE:         ${NGRAPH_CPU_STATIC_LIB_ENABLE}")
292

293 294 295 296
#-----------------------------------------------------------------------------------------------
# Installation logic...
#-----------------------------------------------------------------------------------------------

297 298 299 300 301 302 303
if (LINUX)
    include(GNUInstallDirs)
else()
    set(CMAKE_INSTALL_BINDIR "bin")
    set(CMAKE_INSTALL_INCLUDEDIR "include")
    set(CMAKE_INSTALL_DOCDIR "doc")
    set(CMAKE_INSTALL_LIBDIR "lib")
304 305
endif()

306 307 308
if (DEFINED NGRAPH_INSTALL_PREFIX)
    set(CMAKE_INSTALL_PREFIX ${NGRAPH_INSTALL_PREFIX})
endif()
309
message(STATUS "Installation directory: ${CMAKE_INSTALL_PREFIX}")
310 311

# Destinations
312 313 314 315
set(NGRAPH_INSTALL_LIB "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(NGRAPH_INSTALL_INCLUDE "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
set(NGRAPH_INSTALL_DOC "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DOCDIR}")
set(NGRAPH_INSTALL_BIN "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}")
316
if (LINUX)
Sang Ik Lee's avatar
Sang Ik Lee committed
317
    if (DEFINED NGRAPH_RPATH)
318
        set(CMAKE_BUILD_RPATH "$ORIGIN:${NGRAPH_RPATH}")
Sang Ik Lee's avatar
Sang Ik Lee committed
319 320
        set(CMAKE_INSTALL_RPATH "$ORIGIN:${NGRAPH_RPATH}")
    else()
321
        set(CMAKE_BUILD_RPATH "$ORIGIN")
Sang Ik Lee's avatar
Sang Ik Lee committed
322 323
        set(CMAKE_INSTALL_RPATH "$ORIGIN")
    endif()
324
endif()
325

326 327 328 329 330
#-----------------------------------------------------------------------------------------------
# Compiler-specific logic...
#-----------------------------------------------------------------------------------------------

# Compiler-specific logic...
331
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang$")
332
    message( STATUS "Setting clang flags...")
333 334 335
    include( cmake/clang_4_0_flags.cmake )
endif()

336
if (WIN32)
337
    set(CMAKE_CXX_FLAGS "/W0 /EHsc /MP")
338 339 340 341 342 343 344
else()
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g")
    set(CMAKE_CXX_FLAGS_DEBUG  "-O0 -g")
    # These can be uncommented once we have visibility fully in place
    # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
    # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
endif()
Robert Kimball's avatar
Robert Kimball committed
345

346
include(cmake/sdl.cmake)
347

Avijit's avatar
Avijit committed
348 349 350 351
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    if (DEFINED NGRAPH_USE_CXX_ABI)
        message( STATUS "nGraph using CXX11 ABI: " ${NGRAPH_USE_CXX_ABI} )
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${NGRAPH_USE_CXX_ABI}")
352
    endif()
Avijit's avatar
Avijit committed
353 354
endif()

355
ngraph_var(NGRAPH_WARNINGS_AS_ERRORS DEFAULT "OFF")
356
if (${NGRAPH_WARNINGS_AS_ERRORS})
357 358
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
    message(STATUS "Warnings as errors")
359
endif()
360

361 362 363 364
if (NGRAPH_CODE_COVERAGE_ENABLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
endif()

365
# Enable build target CPU features
366 367 368
if(NOT WIN32)
    set(NGRAPH_TARGET_ARCH native CACHE
        STRING "Target CPU architecture to build for. Defaults to the native CPU architecture")
369

370 371 372 373
    if (NOT "${NGRAPH_TARGET_ARCH}" STREQUAL "native")
        message(WARNING
            "Build target architecture was overridden. The resulting build might not work correctly on the host CPU.")
    endif()
374

375 376
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${NGRAPH_TARGET_ARCH}")
endif()
377

378 379 380 381
if (DEFINED NGRAPH_TUNE_ARCH)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=${NGRAPH_TUNE_ARCH}")
endif()

382 383 384 385
if (NGRAPH_USE_GOLD)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=gold")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=gold")
endif()
386 387 388
if(WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOMINMAX")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS")
389
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
390
endif()
391

392
if (NGRAPH_CPU_ENABLE)
393
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_CPU_ENABLE")
394 395
endif()

396
if (NGRAPH_MLIR_ENABLE)
397
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_MLIR_ENABLE")
398
    set(NGRAPH_MLIR_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/contrib/mlir)
399 400
endif()

401 402 403 404
if (NGRAPH_STATIC_LIB_ENABLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_STATIC_LIB_ENABLE")
endif()

405
if (NGRAPH_PLAIDML_ENABLE)
406 407 408 409 410
    find_package(PlaidML CONFIG)
    if (NOT PLAIDML_FOUND)
        message(FATAL_ERROR "PlaidML not installed")
    endif()

411 412 413
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_PlaidML_ENABLE")
endif()

414 415 416 417 418 419 420 421 422
if (NGRAPH_DISTRIBUTED_ENABLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_DISTRIBUTED_ENABLE")
    if (NGRAPH_DISTRIBUTED_MLSL_ENABLE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_DISTRIBUTED_MLSL_ENABLE")
    elseif (NGRAPH_DISTRIBUTED_OMPI_ENABLE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_DISTRIBUTED_OMPI_ENABLE")
    endif()
endif()

423 424
if (NOT DEFINED NGRAPH_TBB_ENABLE)
    set(NGRAPH_TBB_ENABLE ${NGRAPH_CPU_ENABLE})
Tristan Webb's avatar
Tristan Webb committed
425 426
endif()

427 428 429 430 431 432 433 434 435 436 437 438
# Since UNIX and APPLE support Bash we can use a Bash script to do the clang-format functions
# This is much faster than the cmake method
if (UNIX OR APPLE)
    add_custom_target(style-check COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/maint/check-code-format.sh)
    add_custom_target(style-apply COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/maint/apply-code-format.sh)
    add_custom_target(style COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/maint/apply-code-format.sh)
else()
    add_custom_target(style-check
        COMMAND ${CMAKE_COMMAND}
        -DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
        -P ${CMAKE_MODULE_PATH}style_check.cmake
    )
439

440 441 442 443 444 445 446 447 448 449 450 451
    add_custom_target(style-apply
        COMMAND ${CMAKE_COMMAND}
        -DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
        -P ${CMAKE_MODULE_PATH}style_apply.cmake
    )

    add_custom_target(style
        COMMAND ${CMAKE_COMMAND}
        -DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
        -P ${CMAKE_MODULE_PATH}style_apply.cmake
    )
endif()
452

453 454 455 456 457 458
add_custom_target(fix-mode
    COMMAND ${CMAKE_COMMAND}
    -DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
    -P ${CMAKE_MODULE_PATH}fix_mode.cmake
)

459 460 461 462 463 464 465
#-----------------------------------------------------------------------------------------------
# enable or disable output from NGRAPH_DEBUG statements
#-----------------------------------------------------------------------------------------------
if(NGRAPH_DEBUG_ENABLE)
    add_definitions(-DNGRAPH_DEBUG_ENABLE)
endif()

466 467 468 469 470 471 472
#-----------------------------------------------------------------------------------------------
# enable or disable deprecation warnings for old APIs
#-----------------------------------------------------------------------------------------------
if(NGRAPH_DEPRECATED_ENABLE)
    add_definitions(-DNGRAPH_DEPRECATED_ENABLE)
endif()

473 474 475 476 477 478 479 480 481 482
add_definitions(-DPROJECT_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}")

#-----------------------------------------------------------------------------------------------
# Print Global Options
#-----------------------------------------------------------------------------------------------
message(STATUS "Compile Flags: ${CMAKE_CXX_FLAGS}")
message(STATUS "Shared Link Flags: ${CMAKE_SHARED_LINKER_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}")
message(STATUS "CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}")

483
#-----------------------------------------------------------------------------------------------
484 485 486
# External projects install directory
#-----------------------------------------------------------------------------------------------

Robert Kimball's avatar
Robert Kimball committed
487
set(NGRAPH_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/ngraph)
488 489
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR})
if(WIN32)
490 491
    set(NGRAPH_LIBRARY_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR}/${CMAKE_CFG_INTDIR})
    set(NGRAPH_LIBRARY_INSTALL_SRC_DIRECTORY ${NGRAPH_BUILD_DIR}/\${CMAKE_INSTALL_CONFIG_NAME})
492
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR})
493
    set(NGRAPH_ARCHIVE_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR}/${CMAKE_CFG_INTDIR})
494
    set(NGRAPH_ARCHIVE_INSTALL_SRC_DIRECTORY ${NGRAPH_BUILD_DIR}/\${CMAKE_INSTALL_CONFIG_NAME})
495 496
    set(CMAKE_PDB_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR})
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR})
497 498 499
else()
    set(NGRAPH_LIBRARY_OUTPUT_DIRECTORY ${NGRAPH_BUILD_DIR})
    set(NGRAPH_LIBRARY_INSTALL_SRC_DIRECTORY ${NGRAPH_BUILD_DIR})
500
endif()
Robert Kimball's avatar
Robert Kimball committed
501

502
set(EXTERNAL_INSTALL_DIR ${CMAKE_BINARY_DIR}/external)
503

504 505 506
if(NOT DEFINED EXTERNAL_PROJECTS_ROOT)
    set(EXTERNAL_PROJECTS_ROOT ${CMAKE_CURRENT_BINARY_DIR})
endif()
507 508

if (NGRAPH_ONNX_IMPORT_ENABLE)
509 510 511 512 513 514 515
    if (MSVS)
        # When we build dll libraries. These flags make sure onnx and protobuf build with /MD, not /MT.
        # These two options can't be mixed, because they requires link two imcompatiable runtime.
        set(ONNX_USE_MSVC_STATIC_RUNTIME OFF)
        set(protobuf_WITH_ZLIB  OFF CACHE BOOL "" FORCE)
        set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
    endif(MSVS)
516
    if (NOT NGRAPH_USE_SYSTEM_PROTOBUF)
517
        include(cmake/external_protobuf.cmake)
518 519 520
    else()
        find_package(Protobuf 2.6.1 REQUIRED)
    endif()
521 522 523
    include(cmake/external_onnx.cmake)
    if (TARGET ext_protobuf)
        add_dependencies(ext_onnx ext_protobuf)
524
    endif()
525 526
endif()

527
include(cmake/external_gtest.cmake)
528 529 530
if(NGRAPH_JSON_ENABLE)
    include(cmake/external_json.cmake)
endif()
531 532 533 534 535 536
if(NGRAPH_CPU_ENABLE OR NGRAPH_GENERIC_CPU_ENABLE)
    include(cmake/external_eigen.cmake)
endif()
if(NGRAPH_CPU_ENABLE)
    include(cmake/external_mkldnn.cmake)
endif()
537 538 539
if (NGRAPH_MLIR_ENABLE)
    include(cmake/external_mlir.cmake)
endif()
540 541
if(NGRAPH_INTELGPU_ENABLE)
    include(cmake/external_cldnn.cmake)
542
endif()
543

544 545
if ((NGRAPH_GPU_ENABLE OR NGRAPH_CPU_ENABLE) AND NOT NGRAPH_DEX_ONLY)
    set(NGRAPH_CODEGEN_ENABLE TRUE)
546
else()
547 548 549 550 551 552 553 554 555
    set(NGRAPH_CODEGEN_ENABLE FALSE)
endif()

if(NGRAPH_CODEGEN_ENABLE)
    if (NGRAPH_USE_PREBUILT_LLVM OR DEFINED LLVM_TARBALL_URL)
        include(cmake/external_llvm_prebuilt.cmake)
    elseif (NOT MSVS)
        include(cmake/external_llvm.cmake)
    else()
556
        message(FATAL_ERROR "CODEGEN is not supported on Windows!")
557 558 559 560 561 562 563 564 565
    endif()
endif()

if(NGRAPH_TBB_ENABLE)
    if (WIN32 OR APPLE)
        include(cmake/external_tbb_prebuilt.cmake)
    else()
        include(cmake/external_tbb.cmake)
    endif()
566
endif()
567 568

if (NGRAPH_DISTRIBUTED_MLSL_ENABLE)
569 570 571
    include(cmake/external_mlsl.cmake)
endif()

572 573 574 575 576 577 578
if (NGRAPH_HALIDE)
    message(WARNING "Halide build system integration is currently using an older LLVM release \
                     and is not expected to work across most build environments. Consider \
                     disabling it till this message goes away")
    include(cmake/external_halide.cmake)
endif()

Robert Kimball's avatar
Robert Kimball committed
579
add_subdirectory(src)
580

581
add_subdirectory(test)
582

Avijit's avatar
Avijit committed
583 584 585 586
if (NGRAPH_DOC_BUILD_ENABLE)
    add_subdirectory(doc)
endif()

587 588 589
if (NGRAPH_PYTHON_BUILD_ENABLE)
    add_subdirectory(python)
endif()
590 591 592 593 594 595

install(DIRECTORY
    ${CMAKE_CURRENT_SOURCE_DIR}/licenses
    DESTINATION "${CMAKE_INSTALL_PREFIX}"
)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE DESTINATION ${CMAKE_INSTALL_PREFIX})