forked from onnx/optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
149 lines (132 loc) · 6.05 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
cmake_minimum_required(VERSION 3.1)
project(onnx_optimizer C CXX)
# Set C++11 as standard for the whole project
if(NOT MSVC)
set(CMAKE_CXX_STANDARD 11)
endif(NOT MSVC)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(ONNX_ROOT ${PROJECT_SOURCE_DIR}/third_party/onnx)
add_subdirectory(${ONNX_ROOT})
file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" ONNX_OPTIMIZER_VERSION)
string(STRIP "${ONNX_OPTIMIZER_VERSION}" ONNX_OPTIMIZER_VERSION)
file(GLOB_RECURSE onnx_opt_srcs "onnxoptimizer/*.cc"
"onnxoptimizer/*.h"
)
list(REMOVE_ITEM onnx_opt_srcs "${PROJECT_SOURCE_DIR}/onnxoptimizer/cpp2py_export.cc")
add_library(onnx_optimizer ${onnx_opt_srcs})
target_link_libraries(onnx_optimizer PUBLIC onnx)
target_include_directories(onnx_optimizer PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
add_executable(onnx_optimizer_exec examples/onnx_optimizer_exec.cpp)
target_link_libraries(onnx_optimizer_exec onnx_optimizer)
if(BUILD_ONNX_PYTHON)
if("${PY_EXT_SUFFIX}" STREQUAL "")
if(MSVC)
set(PY_EXT_SUFFIX ".pyd")
else()
set(PY_EXT_SUFFIX ".so")
endif()
endif()
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(PythonInterp ${PY_VERSION} REQUIRED)
find_package(PythonLibs ${PY_VERSION} 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()
add_msvc_runtime_flag(onnx_opt_cpp2py_export)
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
EXPORT ONNXOptimizerTargets DESTINATION ${CMAKE_INSTALL_LIBDIR})