forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
185 lines (161 loc) · 7.41 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# SPDX-License-Identifier: Apache-2.0
# Match the minimum required version of LLVM and MLIR
cmake_minimum_required(VERSION 3.13.4)
project(onnx-mlir)
option(ONNX_MLIR_BUILD_TESTS "Build ONNX-MLIR test executables. If OFF, just generate build targets." ON)
option(ONNX_MLIR_CCACHE_BUILD "Set to ON for a ccache enabled build." OFF)
option(ONNX_MLIR_ENABLE_WERROR "Enable warnings as errors." OFF)
option(ONNX_MLIR_SUPPRESS_THIRD_PARTY_WARNINGS "Suppress warning in third_party code." ON)
# On systems that still have legacy lib64 directories (e.g., rhel/fedora,
# etc.), by default some components (e.g., cmake) install into lib while
# others (e.g., python) install into lib64.
# This causes trouble when we try to figure out the runtime directory in
# CompilerUtils.cpp::getRuntimeDir(). So we explicitly set CMAKE_INSTALL_LIBDIR
# to install into lib on all systems so we don't have to deal with lib64.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INSTALL_LIBDIR lib)
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Debug")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type (default Debug)" FORCE)
endif()
# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
# Use the same setting as llvm-project
if (POLICY CMP0116)
cmake_policy(SET CMP0116 OLD)
endif()
if (MSVC)
# Disable warnings generated by onnx but treated as errors by onnx
# See: https://github.com/onnx/onnx/issues/3530
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4125")
endif()
# Enable ccache
# This setup is sufficient for Ninja and Makefiles, but not Xcode
if (ONNX_MLIR_CCACHE_BUILD)
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
# RULE_LAUNCH_COMPILE is more powerful than C_COMPILER_LAUNCHER (ccache
# supports additional compilers) but has issues with cmd and MSVC tools
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
else()
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
endif()
else()
message(FATAL_ERROR "Unable to find the program ccache. Set ONNX_MLIR_CCACHE_BUILD to OFF.")
endif()
endif()
# Enable warnings as errors
# Leverage the imported LLVM_ENABLE_WERROR for compiler logic
set(LLVM_ENABLE_WERROR ${ONNX_MLIR_ENABLE_WERROR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/bin)
set(CMAKE_INCLUDE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/include)
set(ONNX_MLIR_SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set(ONNX_MLIR_BIN_ROOT ${CMAKE_CURRENT_BINARY_DIR})
set(ONNX_MLIR_LIBRARY_PATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(ONNX_MLIR_RUNTIME_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(ONNX_MLIR_INCLUDE_PATH ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
set(ONNX_MLIR_VENDOR ${PACKAGE_VENDOR} CACHE STRING
"Vendor-specific text for showing with version information.")
include(CTest)
include(ExternalProject)
include(MLIR.cmake)
# MLIR.cmake calls find_package(MLIR) which sets LLVM_MINIMUM_PYTHON_VERSION
if (WIN32)
# On Windows, only search the environment for Python (instead of also checking the registry).
# This allows us to pick up the correct version of Python instead of always picking the latest
# installed on the machine.
set(Python3_FIND_REGISTRY NEVER)
endif()
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED COMPONENTS Interpreter Development)
# Don't require AWT to allow headless JDK to reduce docker image size
set(JAVA_AWT_INCLUDE_PATH headless)
set(JAVA_AWT_LIBRARY headless)
find_package(Java COMPONENTS Development)
find_package(JNI)
if (Java_Development_FOUND AND JNI_FOUND)
set(ONNX_MLIR_ENABLE_JNI TRUE)
include(UseJava)
else()
set(ONNX_MLIR_ENABLE_JNI FALSE)
message(WARNING "Java Development component or JNI not found, JNI targets will not work")
endif()
# The onnx build on windows requires the provision of either static or shared libs for protobuf
# and to achieve that there are several different flags that need to be set in protobuf, onnx
# as well as cmake (for find_package). At the very minimum ONNX_USE_PROTOBUF_SHARED_LIBS and
# Protobuf_USE_STATIC_LIBS need to be opposites of each other, but onnx does not currently set them
# up that way (onnx doesn't set Protobuf_USE_STATIC_LIBS at all and cmake interprets that as setting
# Protobuf_USE_STATIC_LIBS to OFF). If/when onnx is updated to correctly set up both variables,
# this can be removed (see https://github.com/onnx/onnx/issues/3345).
# We are setting CMP0077 to NEW here, so that onnx does not override our regular variables for
# protobuf when the project is included below.
if (POLICY CMP0077)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
endif (POLICY CMP0077)
if (NOT DEFINED ONNX_USE_PROTOBUF_SHARED_LIBS AND NOT DEFINED Protobuf_USE_STATIC_LIBS)
set(ONNX_USE_PROTOBUF_SHARED_LIBS OFF)
set(Protobuf_USE_STATIC_LIBS ON)
elseif (NOT DEFINED Protobuf_USE_STATIC_LIBS)
if (ONNX_USE_PROTOBUF_SHARED_LIBS)
set(Protobuf_USE_STATIC_LIBS OFF)
else()
set(Protobuf_USE_STATIC_LIBS ON)
endif()
elseif (NOT DEFINED ONNX_USE_PROTOBUF_SHARED_LIBS)
if (Protobuf_USE_STATIC_LIBS)
set(ONNX_USE_PROTOBUF_SHARED_LIBS OFF)
else()
set(ONNX_USE_PROTOBUF_SHARED_LIBS ON)
endif()
elseif ((ONNX_USE_PROTOBUF_SHARED_LIBS AND Protobuf_USE_STATIC_LIBS)
OR (NOT ONNX_USE_PROTOBUF_SHARED_LIBS AND NOT Protobuf_USE_STATIC_LIBS))
message(FATAL_ERROR
"ONNX_USE_PROTOBUF_SHARED_LIBS and Protobuf_USE_STATIC_LIBS must be opposites of each other.")
endif()
# Use the new MSVC preprocessor to improve standard conformance.
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Zc:preprocessor")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:preprocessor")
endif()
# Suppress warnings in third party code.
if (ONNX_MLIR_SUPPRESS_THIRD_PARTY_WARNINGS)
set(CMAKE_C_FLAGS_COPY ${CMAKE_C_FLAGS})
set(CMAKE_CXX_FLAGS_COPY ${CMAKE_CXX_FLAGS})
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")
elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# MSVC complains when overriding existing warning levels flags, remove
# them and add "/W".
STRING(REGEX REPLACE "/W[0-9]" "/W0" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
STRING(REGEX REPLACE "/W[0-9]" "/W0" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
endif()
endif()
# Add third party subdirectories and define options appropriate to run their cmakes.
set(CMAKE_MESSAGE_LOG_LEVEL NOTICE)
set(pybind11_FIND_QUIETLY ON)
set(BENCHMARK_USE_BUNDLED_GTEST OFF)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF)
set(BENCHMARK_ENABLE_TESTING OFF)
add_subdirectory(third_party/onnx)
add_subdirectory(third_party/pybind11)
add_subdirectory(third_party/rapidcheck)
if (NOT TARGET benchmark)
add_subdirectory(third_party/benchmark)
endif()
set(CMAKE_MESSAGE_LOG_LEVEL STATUS)
# Ensure warnings are reported for onnx-mlir code.
if (ONNX_MLIR_SUPPRESS_THIRD_PARTY_WARNINGS)
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_COPY})
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_COPY})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSUPPRESS_THIRD_PARTY_WARNINGS")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSUPPRESS_THIRD_PARTY_WARNINGS")
endif()
add_subdirectory(utils)
add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(docs)
add_subdirectory(test)