-
Notifications
You must be signed in to change notification settings - Fork 90
/
CMakeLists.txt
180 lines (155 loc) · 7.2 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
cmake_minimum_required(VERSION 3.22)
# For std::filesystem
# Must be a cache variable and be set before project()
# Reference: https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_DEPLOYMENT_TARGET.html
# Maybe it can be a normal variable if policy CMP0126 is set to NEW?
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "Minimum OS X deployment version")
project(onnx_optimizer C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(cmake/utils.cmake)
# For integration with onnxruntime_webassembly etc.
if (NOT DEFINED ONNX_TARGET_NAME)
set(ONNX_TARGET_NAME onnx)
endif()
option(ONNX_OPT_USE_SYSTEM_PROTOBUF "" OFF)
if(NOT ONNX_OPT_USE_SYSTEM_PROTOBUF)
option(protobuf_BUILD_TESTS "" OFF)
option(protobuf_MSVC_STATIC_RUNTIME "" ${ONNX_USE_MSVC_STATIC_RUNTIME})
add_subdirectory_if_no_target(${PROJECT_SOURCE_DIR}/third_party/protobuf/cmake libprotobuf)
endif()
set(ONNX_ROOT ${PROJECT_SOURCE_DIR}/third_party/onnx)
add_subdirectory_if_no_target(${ONNX_ROOT} ${ONNX_TARGET_NAME})
file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" ONNX_OPTIMIZER_VERSION)
string(STRIP "${ONNX_OPTIMIZER_VERSION}" ONNX_OPTIMIZER_VERSION)
file(GLOB onnx_opt_srcs "onnxoptimizer/*.cc"
"onnxoptimizer/*.h"
"onnxoptimizer/passes/*.cc"
"onnxoptimizer/passes/*.h"
)
list(REMOVE_ITEM onnx_opt_srcs "${PROJECT_SOURCE_DIR}/onnxoptimizer/cpp2py_export.cc")
onnxopt_add_library(onnx_optimizer ${onnx_opt_srcs})
target_link_libraries(onnx_optimizer PUBLIC ${ONNX_TARGET_NAME})
target_include_directories(onnx_optimizer PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
onnxopt_add_executable(onnx_optimizer_exec examples/onnx_optimizer_exec.cpp)
target_link_libraries(onnx_optimizer_exec onnx_optimizer)
file(GLOB onnx_opt_c_api_srcs "onnxoptimizer/c_api/*.cc"
"onnxoptimizer/c_api/*.h"
)
onnxopt_add_library(onnx_optimizer_c_api ${onnx_opt_c_api_srcs})
target_link_libraries(onnx_optimizer_c_api PRIVATE onnx_optimizer)
target_include_directories(onnx_optimizer_c_api PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
if(BUILD_ONNX_PYTHON)
if("${PY_EXT_SUFFIX}" STREQUAL "")
if(MSVC)
set(PY_EXT_SUFFIX ".pyd")
else()
set(PY_EXT_SUFFIX ".so")
endif()
endif()
find_package(Python COMPONENTS Interpreter REQUIRED)
onnxopt_add_library(onnx_opt_cpp2py_export MODULE "onnxoptimizer/cpp2py_export.cc")
set_target_properties(onnx_opt_cpp2py_export PROPERTIES PREFIX "")
set_target_properties(onnx_opt_cpp2py_export
PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
set_target_properties(onnx_opt_cpp2py_export PROPERTIES SUFFIX ${PY_EXT_SUFFIX})
set_target_properties(onnx_opt_cpp2py_export
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
target_include_directories(onnx_opt_cpp2py_export PRIVATE
$<BUILD_INTERFACE:${ONNX_ROOT}>
$<INSTALL_INTERFACE:include>
${Python_INCLUDE_DIR})
# pybind11 is a header only lib
find_package(pybind11 2.2)
if(pybind11_FOUND)
target_include_directories(onnx_opt_cpp2py_export PUBLIC
${pybind11_INCLUDE_DIRS})
else()
if(EXISTS ${ONNX_ROOT}/third_party/pybind11/include/pybind11/pybind11.h)
target_include_directories(onnx_opt_cpp2py_export PUBLIC
${ONNX_ROOT}/third_party/pybind11/include)
else()
message(FATAL_ERROR "cannot find pybind")
endif()
endif()
if(APPLE)
set_target_properties(onnx_opt_cpp2py_export
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
target_link_libraries(onnx_opt_cpp2py_export
PRIVATE -Wl,-force_load,$<TARGET_FILE:onnx_optimizer>)
elseif(MSVC)
# In MSVC, we will add whole archive in default
target_link_libraries(onnx_opt_cpp2py_export
PRIVATE -WHOLEARCHIVE:$<TARGET_FILE:onnx_optimizer>)
elseif(CMAKE_SYSTEM_NAME STREQUAL "AIX")
# whole-archive linker option not available on AIX
target_sources(onnx_opt_cpp2py_export
PRIVATE $<TARGET_OBJECTS:onnx_optimizer>)
else()
# Assume everything else is like gcc
target_link_libraries(onnx_opt_cpp2py_export
PRIVATE "-Wl,--whole-archive" $<TARGET_FILE:onnx_optimizer>
"-Wl,--no-whole-archive")
set_target_properties(onnx_opt_cpp2py_export
PROPERTIES LINK_FLAGS "-Wl,--exclude-libs,ALL")
endif()
target_link_libraries(onnx_opt_cpp2py_export PRIVATE onnx_optimizer)
if(MSVC)
find_package(Python COMPONENTS Interpreter Development REQUIRED)
target_link_libraries(onnx_opt_cpp2py_export PRIVATE ${Python_LIBRARIES})
target_compile_options(onnx_opt_cpp2py_export
PRIVATE /MP
/WX
/wd4800 # disable warning type' : forcing
# value to bool 'true' or 'false'
# (performance warning)
/wd4503 # identifier' : decorated name length
# exceeded, name was truncated
/wd4146 # unary minus operator applied to
# unsigned type, result still
# unsigned from include\google\protob
# uf\wire_format_lite.h
/wd4244 # 'argument': conversion from 'google::
# protobuf::uint64' to 'int', possible
# loss of data
/wd4267 # Conversion from 'size_t' to 'int',
# possible loss of data
/wd4996 # The second parameter is ignored.
${EXTRA_FLAGS})
if(ONNX_USE_PROTOBUF_SHARED_LIBS)
target_compile_options(onnx_opt_cpp2py_export
PRIVATE /wd4251 # 'identifier' : class 'type1' needs to
# have dll-interface to be used by
# clients of class 'type2'
)
endif()
endif()
endif()
include(GNUInstallDirs)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/onnxoptimizer
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN "*.h")
configure_file(
${PROJECT_SOURCE_DIR}/cmake/ONNXOptimizerConfigVersion.cmake.in
${PROJECT_BINARY_DIR}/ONNXOptimizerConfigVersion.cmake
@ONLY)
configure_file(
${PROJECT_SOURCE_DIR}/cmake/ONNXOptimizerConfig.cmake.in
${PROJECT_BINARY_DIR}/ONNXOptimizerConfig.cmake
@ONLY)
install(FILES
${PROJECT_BINARY_DIR}/ONNXOptimizerConfigVersion.cmake
${PROJECT_BINARY_DIR}/ONNXOptimizerConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ONNXOptimizer
COMPONENT dev)
install(EXPORT ONNXOptimizerTargets DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ONNXOptimizer")
install(TARGETS
onnx_optimizer onnx_optimizer_c_api
EXPORT ONNXOptimizerTargets DESTINATION ${CMAKE_INSTALL_LIBDIR})