-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
203 lines (176 loc) · 7.14 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Minimum CMake required
cmake_minimum_required(VERSION 3.1)
# Project
project(onnx C CXX)
# Set C++11 as standard for the whole project
set(CMAKE_CXX_STANDARD 11)
set(ONNX_ROOT ${PROJECT_SOURCE_DIR})
if (WIN32)
# parallel build
add_compile_options(/MP)
add_compile_options(/WX)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /WX")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /WX")
endif()
# ---[ pybind11
if (NOT TARGET pybind11)
add_subdirectory(third_party/pybind11)
endif()
# Customized version of find Protobuf. We need to avoid situations mentioned
# in https://github.com/caffe2/caffe2/blob/b7d983f255ef5496474f1ea188edb5e0ac442761/cmake/ProtoBuf.cmake#L82-L92
# The following section is stolen from cmake/ProtoBuf.cmake in Caffe2
find_program(PROTOBUF_PROTOC_EXECUTABLE
NAMES protoc
DOC "The Google Protocol Buffers Compiler")
# Only if protoc was found, seed the include directories and libraries.
# We assume that protoc is installed at PREFIX/bin.
# We use get_filename_component to resolve PREFIX.
if(PROTOBUF_PROTOC_EXECUTABLE)
get_filename_component(
_PROTOBUF_INSTALL_PREFIX
${PROTOBUF_PROTOC_EXECUTABLE}
DIRECTORY)
get_filename_component(
_PROTOBUF_INSTALL_PREFIX
${_PROTOBUF_INSTALL_PREFIX}/..
REALPATH)
find_library(PROTOBUF_LIBRARY
NAMES protobuf
PATHS ${_PROTOBUF_INSTALL_PREFIX}/lib
NO_DEFAULT_PATH)
find_library(PROTOBUF_PROTOC_LIBRARY
NAMES protoc
PATHS ${_PROTOBUF_INSTALL_PREFIX}/lib
NO_DEFAULT_PATH)
find_library(PROTOBUF_LITE_LIBRARY
NAMES protobuf-lite
PATHS ${_PROTOBUF_INSTALL_PREFIX}/lib
NO_DEFAULT_PATH)
find_path(PROTOBUF_INCLUDE_DIR
google/protobuf/service.h
PATHS ${_PROTOBUF_INSTALL_PREFIX}/include
NO_DEFAULT_PATH)
find_package(Protobuf REQUIRED)
endif()
# Build the libraries with -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(ONNX_NAMESPACE)
SET(MY_ONNX_NAMESPACE "-DONNX_NAMESPACE=${ONNX_NAMESPACE}")
else()
SET(ONNX_NAMESPACE "onnx")
SET(MY_ONNX_NAMESPACE "-DONNX_NAMESPACE=onnx")
endif()
add_definitions(${MY_ONNX_NAMESPACE})
find_package(PythonInterp REQUIRED)
# function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR)
# from https://github.com/tensorflow/tensorflow/blob/d2c3b873c6f8ff999a2e4ee707a84ff00d9c15a5/tensorflow/contrib/cmake/tf_core_framework.cmake
# to solve the problem that customized dir can't be specified when calling PROTOBUF_GENERATE_CPP.
function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR)
if(NOT ARGN)
message(SEND_ERROR "Error: RELATIVE_PROTOBUF_GENERATE_CPP() called without any proto files")
return()
endif()
if(MSVC)
set(DLLEXPORT_STR "dllexport_decl=ONNX_API:")
else()
set(DLLEXPORT_STR "")
endif()
set(${SRCS})
set(${HDRS})
set(GEN_PROTO_PY ${ROOT_DIR}/onnx/gen_proto.py)
foreach(INFILE ${ARGN})
set(ABS_FILE ${ROOT_DIR}/${INFILE})
get_filename_component(FILE_DIR ${ABS_FILE} DIRECTORY)
get_filename_component(FILE_WE ${INFILE} NAME_WE)
file(RELATIVE_PATH REL_DIR ${ROOT_DIR} ${FILE_DIR})
set(OUTPUT_PROTO_DIR "${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}")
if(ONNX_NAMESPACE STREQUAL "onnx")
set(OUTPUT_PB_HEADER "${OUTPUT_PROTO_DIR}/${FILE_WE}.pb.h")
set(OUTPUT_PB_SRC "${OUTPUT_PROTO_DIR}/${FILE_WE}.pb.cc")
set(GENERATED_PROTO "${OUTPUT_PROTO_DIR}/${FILE_WE}.proto")
else()
set(OUTPUT_PB_HEADER "${OUTPUT_PROTO_DIR}/${FILE_WE}_${ONNX_NAMESPACE}.pb.h")
set(OUTPUT_PB_SRC "${OUTPUT_PROTO_DIR}/${FILE_WE}_${ONNX_NAMESPACE}.pb.cc")
# We need this dummy header generated by gen_proto.py when ONNX_NAMESPACE
# is not onnx
list(APPEND ${HDRS} "${OUTPUT_PROTO_DIR}/${FILE_WE}.pb.h")
set(GENERATED_PROTO "${OUTPUT_PROTO_DIR}/${FILE_WE}_${ONNX_NAMESPACE}.proto")
endif()
list(APPEND ${SRCS} "${OUTPUT_PB_SRC}")
list(APPEND ${HDRS} "${OUTPUT_PB_HEADER}")
add_custom_command(
OUTPUT "${GENERATED_PROTO}"
COMMAND mkdir
ARGS -p ${OUTPUT_PROTO_DIR}
COMMAND ${PYTHON_EXECUTABLE} ${GEN_PROTO_PY}
ARGS -p ${ONNX_NAMESPACE} -o ${OUTPUT_PROTO_DIR} ${FILE_WE}
DEPENDS ${INFILE}
COMMENT "Running gen_proto.py on ${INFILE}"
VERBATIM )
add_custom_command (
OUTPUT "${OUTPUT_PB_SRC}"
"${OUTPUT_PB_HEADER}"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --cpp_out ${DLLEXPORT_STR}${OUTPUT_PROTO_DIR} ${GENERATED_PROTO} -I ${PROTOBUF_INCLUDE_DIRS} -I ${OUTPUT_PROTO_DIR}
DEPENDS ${GENERATED_PROTO}
COMMENT "Running C++ protocol buffer compiler on ${GENERATED_PROTO}"
VERBATIM )
endforeach()
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()
RELATIVE_PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${ONNX_ROOT}
onnx/onnx.in.proto
)
# Temp solution to remove files generated by calling 'pip install ..."
# Should be removed after changing setup.py to use cmake to build c++ part.
file(REMOVE
"${ONNX_ROOT}/onnx/onnx.pb.h"
"${ONNX_ROOT}/onnx/onnx.pb.cc"
"${ONNX_ROOT}/onnx/onnx-operators.pb.h"
"${ONNX_ROOT}/onnx/onnx-operators.pb.cc"
)
file(GLOB_RECURSE onnx_src
"${ONNX_ROOT}/onnx/*.h"
"${ONNX_ROOT}/onnx/*.cc"
)
list(REMOVE_ITEM onnx_src "${ONNX_ROOT}/onnx/cpp2py_export.cc")
if(MSVC)
set(DLLEXPORT_OPTION "-DONNX_API=__declspec(dllexport)")
else()
set(DLLEXPORT_OPTION "-DONNX_API=")
endif()
add_library(onnx_proto ${PROTO_SRCS} ${PROTO_HDRS})
target_include_directories(onnx_proto PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" "${PROTOBUF_INCLUDE_DIRS}")
target_link_libraries(onnx_proto PUBLIC ${PROTOBUF_LIBRARIES})
if(MSVC)
target_compile_options(onnx_proto PRIVATE /WX- ${DLLEXPORT_OPTION})
endif()
add_library(onnx ${onnx_src})
target_include_directories(onnx PUBLIC ${ONNX_ROOT} "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(onnx PUBLIC onnx_proto)
add_library(onnx_pybind "${ONNX_ROOT}/onnx/cpp2py_export.cc")
target_include_directories(onnx_pybind PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" "${PROTOBUF_INCLUDE_DIRS}" "${PYBIND11_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}")
target_link_libraries(onnx_pybind PUBLIC onnx_proto onnx)
# Export include directories
set(ONNX_INCLUDE_DIRS "${ONNX_ROOT}" "${CMAKE_CURRENT_BINARY_DIR}")
set(ONNX_INCLUDE_DIRS ${ONNX_INCLUDE_DIRS} PARENT_SCOPE)
if (WIN32)
target_compile_options(onnx PRIVATE
/wd4800 # disable warning type' : forcing value to bool 'true' or 'false' (performance warning)
/wd4503 # identifier' : decorated name length exceeded, name was truncated
)
target_compile_options(onnx_pybind PRIVATE
/wd4800 # disable warning type' : forcing value to bool 'true' or 'false' (performance warning)
/wd4503 # identifier' : decorated name length exceeded, name was truncated
)
set(onnx_static_library_flags
-IGNORE:4221 # LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library
)
set_target_properties(onnx PROPERTIES
STATIC_LIBRARY_FLAGS "${onnx_static_library_flags}")
endif()
if(APPLE)
set_target_properties(onnx PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
endif()