CMakeLists.txt 10.2 KB
Newer Older
1
# ******************************************************************************
2 3
# Copyright 2017-2018 Intel Corporation
#
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.1)
Robert Kimball's avatar
Robert Kimball committed
18

19
include(cmake/Modules/git_tags.cmake)
20

21 22 23 24
NGRAPH_GET_VERSION_LABEL()

string(REGEX MATCH "([0-9]+)\.([0-9]+)\.([0-9]+)" NGRAPH_VERSION_SHORT "${NGRAPH_VERSION_LABEL}")
string(REGEX MATCH "([0-9]+)\.([0-9]+)" NGRAPH_API_VERSION "${NGRAPH_VERSION_LABEL}")
25 26 27 28 29
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)
30 31 32 33 34 35
configure_file(VERSION.in VERSION)

message(STATUS "NGRAPH_VERSION ${NGRAPH_VERSION}")
message(STATUS "NGRAPH_VERSION_SHORT ${NGRAPH_VERSION_SHORT}")
message(STATUS "NGRAPH_API_VERSION ${NGRAPH_API_VERSION}")

36 37 38
set(NGRAPH_INCLUDE_PATH
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)
39 40 41 42
# Suppress an OS X-specific warning.
if (POLICY CMP0042)
    cmake_policy(SET CMP0042 OLD)
endif()
Robert Kimball's avatar
Robert Kimball committed
43

44
project (ngraph)
Robert Kimball's avatar
Robert Kimball committed
45

46 47 48 49
if (UNIX AND NOT APPLE)
    set(LINUX TRUE)
endif()

50 51
SET(GCC_MIN_VERSION 4.8)
SET(CLANG_MIN_VERSION 3.8)
52
SET(APPLE_CLANG_MIN_VERSION 8.1)
53 54 55 56 57 58 59 60 61

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS GCC_MIN_VERSION)
        message(FATAL_ERROR "GCC version must be at least ${GCC_MIN_VERSION}!")
    endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS CLANG_MIN_VERSION)
        message(FATAL_ERROR "Clang version must be at least ${CLANG_MIN_VERSION}!")
    endif()
62 63 64 65
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS APPLE_CLANG_MIN_VERSION)
        message(FATAL_ERROR "Apple Clang version must be at least ${APPLE_CLANG_MIN_VERSION}!")
    endif()
66
else()
67
    message(WARNING "You are using an unsupported compiler. Compilation has only been tested with Clang (${CLANG_MIN_VERSION} and up), Apple Clang (${APPLE_CLANG_MIN_VERSION} and up), and GCC (${GCC_MIN_VERSION} and up).")
68 69
endif()

70 71 72
# Prevent Eigen from using any LGPL3 code
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEIGEN_MPL2_ONLY")

73 74 75 76
if($ENV{NGRAPH_USE_PREBUILT_LLVM})
    set(NGRAPH_USE_PREBUILT_LLVM TRUE)
endif()

77
# These variables are undocumented but useful.
Robert Kimball's avatar
Robert Kimball committed
78 79 80
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)

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

Robert Kimball's avatar
Robert Kimball committed
84 85 86
# set directory where the custom finders live
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

87 88
include(var_functions)

89 90 91
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)
92
option(NGRAPH_INTELGPU_ENABLE "Control the building of the Intel GPU backend with clDNN" FALSE)
93
option(NGRAPH_GPU_ENABLE "Control the building of the GPU backend" FALSE)
94
option(NGRAPH_INTERPRETER_ENABLE "Control the building of the INTERPRETER backend" TRUE)
95
option(NGRAPH_DISTRIBUTED_ENABLE "Add distributed mode to the CPU backend" FALSE)
96
option(NGRAPH_DEBUG_ENABLE "Enable output for NGRAPH_DEBUG statements" FALSE)
97
option(NGRAPH_ONNX_IMPORT_ENABLE "Enable ONNX importer" FALSE)
98
option(NGRAPH_DEX_ONLY "Build CPU DEX without codegen" FALSE)
99
option(NGRAPH_CODE_COVERAGE_ENABLE "Enable code coverage data collection" FALSE)
100

101 102
if (NGRAPH_ONNX_IMPORT_ENABLE)
    option(NGRAPH_USE_SYSTEM_PROTOBUF "Use system provided Protobuf shared object" FALSE)
103
    option(NGRAPH_ONNXIFI_ENABLE "Enable ONNX Interface for Framework Integration" TRUE)
104 105
endif()

106 107 108 109
#-----------------------------------------------------------------------------------------------
# Installation logic...
#-----------------------------------------------------------------------------------------------

110 111
if (LINUX)
    include(GNUInstallDirs)
112
    include(cmake/platform.cmake)
113 114 115 116 117
else()
    set(CMAKE_INSTALL_BINDIR "bin")
    set(CMAKE_INSTALL_INCLUDEDIR "include")
    set(CMAKE_INSTALL_DOCDIR "doc")
    set(CMAKE_INSTALL_LIBDIR "lib")
118 119
endif()

120 121 122
if (DEFINED NGRAPH_INSTALL_PREFIX)
    set(CMAKE_INSTALL_PREFIX ${NGRAPH_INSTALL_PREFIX})
endif()
123
message(STATUS "Installation directory: ${CMAKE_INSTALL_PREFIX}")
124 125

# Destinations
126 127 128 129
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}")
130
set(CMAKE_INSTALL_RPATH "$ORIGIN")
131

132 133 134 135 136
#-----------------------------------------------------------------------------------------------
# Compiler-specific logic...
#-----------------------------------------------------------------------------------------------

# Compiler-specific logic...
137
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang$")
138
    message( STATUS "Setting clang flags...")
139 140 141 142
    include( cmake/clang_4_0_flags.cmake )
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
143

Avijit's avatar
Avijit committed
144 145 146 147 148 149 150
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}")
    endif()    
endif()

151
ngraph_var(NGRAPH_WARNINGS_AS_ERRORS DEFAULT "OFF")
152
if (${NGRAPH_WARNINGS_AS_ERRORS})
153 154
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
    message(STATUS "Warnings as errors")
155
endif()
156 157

SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g")
158
SET(CMAKE_CXX_FLAGS_DEBUG  "-O0 -g")
159

160 161 162 163
if (NGRAPH_CODE_COVERAGE_ENABLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
endif()

164 165 166 167 168 169 170 171
# Enable build target CPU features
set(NGRAPH_TARGET_ARCH native CACHE STRING "Target CPU architecture to build for. Defaults to the native CPU architecture")

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()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${NGRAPH_TARGET_ARCH}")
172

173 174 175 176
if (DEFINED NGRAPH_TUNE_ARCH)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=${NGRAPH_TUNE_ARCH}")
endif()

177 178
# flags required for SDL-3
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -fPIE -D_FORTIFY_SOURCE=2")
179 180 181
if (NOT WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wformat -Wformat-security")
endif()
182 183 184
if (NOT APPLE)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z noexecstack")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z relro -z now")
185
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
186
endif()
187 188 189 190 191 192 193 194
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
    else()
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong")
    endif()
endif()

195 196 197 198
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()
199 200 201 202
if(WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOMINMAX")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS")
endif()
203

204
if (NGRAPH_CPU_ENABLE)
205
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_CPU_ENABLE")	
206 207
endif()

208 209
if (NOT DEFINED NGRAPH_TBB_ENABLE)
    set(NGRAPH_TBB_ENABLE ${NGRAPH_CPU_ENABLE})
Tristan Webb's avatar
Tristan Webb committed
210 211
endif()

212 213 214 215
add_custom_target(style-check
    COMMAND ${PROJECT_SOURCE_DIR}/maint/check-code-format.sh
)

216 217 218 219 220 221 222
#-----------------------------------------------------------------------------------------------
# enable or disable output from NGRAPH_DEBUG statements
#-----------------------------------------------------------------------------------------------
if(NGRAPH_DEBUG_ENABLE)
    add_definitions(-DNGRAPH_DEBUG_ENABLE)
endif()

223
#-----------------------------------------------------------------------------------------------
224 225 226
# External projects install directory
#-----------------------------------------------------------------------------------------------

Robert Kimball's avatar
Robert Kimball committed
227 228
set(NGRAPH_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/ngraph)

229
set(EXTERNAL_INSTALL_DIR ${CMAKE_BINARY_DIR}/external)
230

231 232 233
if(NOT DEFINED EXTERNAL_PROJECTS_ROOT)
    set(EXTERNAL_PROJECTS_ROOT ${CMAKE_CURRENT_BINARY_DIR})
endif()
234 235 236

if (NGRAPH_ONNX_IMPORT_ENABLE)
    if (NOT NGRAPH_USE_SYSTEM_PROTOBUF)
237
        include(cmake/external_protobuf.cmake)
238 239 240
    else()
        find_package(Protobuf 2.6.1 REQUIRED)
    endif()
241 242 243 244 245 246
    if (NGRAPH_ONNXIFI_ENABLE)
        include(cmake/external_onnx.cmake)
        if (TARGET ext_protobuf)
            add_dependencies(ext_onnx ext_protobuf)
        endif()
    endif()
247 248
endif()

249 250 251 252
include(cmake/external_gtest.cmake)
include(cmake/external_json.cmake)
include(cmake/external_eigen.cmake)
include(cmake/external_mkldnn.cmake)
253

254 255 256 257 258
if (NGRAPH_USE_PREBUILT_LLVM OR DEFINED LLVM_TARBALL_URL)
    include(cmake/external_llvm_prebuilt.cmake)
else()
    include(cmake/external_llvm.cmake)
endif()
259

260
include(cmake/external_tbb.cmake)
Robert Kimball's avatar
Robert Kimball committed
261

262 263 264 265 266 267 268
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()

269 270
add_definitions(-DPROJECT_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}")

Robert Kimball's avatar
Robert Kimball committed
271
add_subdirectory(src)
272

273 274 275 276
if (NGRAPH_UNIT_TEST_ENABLE)
    add_subdirectory(test)
    message(STATUS "unit tests enabled")
else()
277
    add_subdirectory(test/util)
278 279
    message(STATUS "unit tests disabled")
endif()
280

Avijit's avatar
Avijit committed
281 282 283 284
if (NGRAPH_DOC_BUILD_ENABLE)
    add_subdirectory(doc)
endif()