-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
executable file
·216 lines (187 loc) · 9.06 KB
/
CMakeLists.txt
File metadata and controls
executable file
·216 lines (187 loc) · 9.06 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
option(ENABLE_OPEN_SRC "Enable graphengine compile in opensource." FALSE)
if (ENABLE_OPEN_SRC)
cmake_minimum_required(VERSION 3.14)
project(TFAdapter)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_FLAGS "-O2 -DNDEBUG -Wno-deprecated-declarations -Wall -fPIC -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -s -pipe ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "-std=c++11 -O2 -DNDEBUG -Wno-deprecated-declarations -Wall -fPIC -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -s -pipe ${CMAKE_CXX_FLAGS}")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
# build external prjects
if(DEFINED ENV{D_PKG_SERVER})
set(TF_PKG_SERVER $ENV{D_PKG_SERVER})
message("Download packages from PKG server")
endif()
include(${CMAKE_CURRENT_LIST_DIR}/cmake/nlohmann_json.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/secure_c.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/tensorflow.cmake)
include_directories(${CMAKE_CURRENT_LIST_DIR})
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc)
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc/external)
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc/soft_dp)
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc/graphengine/inc)
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc/graphengine/inc/external)
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc/metadef/inc)
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc/metadef/inc/external)
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/tools/COMPILE_FLAGS OR NOT EXISTS
${CMAKE_CURRENT_LIST_DIR}/tools/LINK_FLAGS OR NOT EXISTS
${CMAKE_CURRENT_LIST_DIR}/tools/PYTHON_BIN_PATH OR NOT EXISTS
${CMAKE_CURRENT_LIST_DIR}/tools/SWIG_BIN_PATH)
message(FATAL_ERROR "No validate configuration found. Did you forget to configure first?")
endif ()
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/COMPILE_FLAGS" COMPILE_FLAGS)
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/LINK_FLAGS" LINK_FLAGS)
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/PYTHON_BIN_PATH" PYTHON_BIN_PATH)
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/SWIG_BIN_PATH" SWIG_BIN_PATH)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/python DESTINATION ${CMAKE_BINARY_DIR}/dist)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/convert_tf2npu DESTINATION ${CMAKE_BINARY_DIR}/dist/python/npu_bridge)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/swig DESTINATION ${CMAKE_BINARY_DIR}/dist)
execute_process(COMMAND ${SWIG_BIN_PATH} -python -c++ -threads ${CMAKE_BINARY_DIR}/dist/swig/ge_plugin.i)
file(COPY ${CMAKE_BINARY_DIR}/dist/swig/tf_adapter.py DESTINATION ${CMAKE_BINARY_DIR}/dist/python/npu_bridge)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/*.cc)
add_library(_tf_adapter SHARED ${SOURCES} ${CMAKE_BINARY_DIR}/dist/swig/ge_plugin_wrap.cxx)
foreach (COMPILE_FLAG ${COMPILE_FLAGS})
target_compile_options(_tf_adapter PUBLIC "${COMPILE_FLAG}")
endforeach (COMPILE_FLAG)
target_link_libraries(_tf_adapter PUBLIC "dl")
foreach (LINK_FLAG ${LINK_FLAGS})
target_link_libraries(_tf_adapter PUBLIC "${LINK_FLAG}")
endforeach (LINK_FLAG)
target_compile_definitions(_tf_adapter PUBLIC
LOG_CPP
)
set_target_properties(_tf_adapter PROPERTIES PREFIX "")
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/dist/python/npu_bridge)
add_custom_command(TARGET _tf_adapter
POST_BUILD
COMMAND cd ${CMAKE_BINARY_DIR}/dist/python/ && ${PYTHON_BIN_PATH} setup.py bdist_wheel
VERBATIM
)
else()
get_filename_component(TOP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
set(BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_VERBOSE_MAKEFILE ON)
# rewrite __FILE__ to avoid absolute path
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> -D__FILE__='\"$(subst ${CMAKE_CURRENT_SOURCE_DIR}/,,$(abspath $<))\"' -Wno-builtin-macro-redefined <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> -D__FILE__='\"$(subst ${CMAKE_CURRENT_SOURCE_DIR}/,,$(abspath $<))\"' -Wno-builtin-macro-redefined <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
add_custom_command(
OUTPUT ${BASE_DIR}/tensorflow_framework.cc
COMMAND touch ${BASE_DIR}/tensorflow_framework.cc
)
add_library(tensorflow_framework SHARED
${BASE_DIR}/tensorflow_framework.cc
)
#rename libtensorflow_framework.so to libtensorflow_framework.so.1
set_target_properties(tensorflow_framework
PROPERTIES
VERSION 1
)
add_custom_command(
OUTPUT ${BASE_DIR}/pywrap_tensorflow_internal.cc
COMMAND touch ${BASE_DIR}/pywrap_tensorflow_internal.cc
)
add_library(pywrap_tensorflow_internal SHARED
${BASE_DIR}/pywrap_tensorflow_internal.cc
)
#rename libpywrap_tensorflow_internal.so to _pywrap_tensorflow_internal.so
set_target_properties(pywrap_tensorflow_internal
PROPERTIES
PREFIX _
)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/*.cc)
add_library(tf_adapter SHARED
${SOURCES}
${BASE_DIR}/tf_adapter/util/ge_plugin_wrap.cxx
)
target_include_directories(tf_adapter PRIVATE
${BASE_DIR}/
${TOP_DIR}/inc/
${TOP_DIR}/inc/external/
${TOP_DIR}/inc/common/
${TOP_DIR}/inc/soft_dp/
${TOP_DIR}/soft_dp/
${TOP_DIR}/graphengine/inc/
${TOP_DIR}/graphengine/inc/external/
${TOP_DIR}/metadef/inc/
${TOP_DIR}/metadef/inc/external/
${TOP_DIR}/libc_sec/include/
${TOP_DIR}/third_party/json/include/
${TOP_DIR}/third_party/tensorflow/tensorflow-1.15.0/
${TOP_DIR}/third_party/tensorflow/compile_deps/tf-1.15.0/include/
${HI_PYTHON_INC}/
)
target_compile_options(tf_adapter PUBLIC
$<$<COMPILE_LANGUAGE:CXX>:-std=c++11>
-O2
-DNDEBUG
-ftrapv
-Wno-deprecated-declarations
)
target_compile_definitions(tf_adapter PUBLIC
_FORTIFY_SOURCE=2
LOG_CPP
)
target_link_libraries(tf_adapter PUBLIC
$<BUILD_INTERFACE:intf_pub>
-Wl,--no-as-needed
c_sec
ge_runner
datatransfer
fmk_parser
indextransform
tensorflow_framework
pywrap_tensorflow_internal
-Wl,--as-needed
-s
)
# rename libtf_adapter.so to _tf_adapter.so
set_target_properties(tf_adapter
PROPERTIES
PREFIX _
)
###################################################################################################
add_custom_command(
OUTPUT ${BASE_DIR}/tf_adapter/util/ge_plugin_wrap.cxx
COMMAND echo "before generate swig files"
&& swig -c++ -threads -python ${BASE_DIR}/tf_adapter/swig/ge_plugin.i
&& mv ${BASE_DIR}/tf_adapter/swig/ge_plugin_wrap.cxx ${BASE_DIR}/tf_adapter/util/
&& mv ${BASE_DIR}/tf_adapter/swig/tf_adapter.py ${BASE_DIR}/tf_adapter/python/npu_bridge/
&& echo "end generate swig files"
)
###################################################################################################
add_custom_target(
hw_npu_bridge_1.15.0 ALL
DEPENDS tensorflow_framework pywrap_tensorflow_internal tf_adapter SoftDp ${CMAKE_CURRENT_BINARY_DIR}/npu_bridge-1.15.0-py3-none-any.whl
)
# package _tf_adapter.so and libSoftDp.so in npu_bridge-xxx.whl
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/npu_bridge-1.15.0-py3-none-any.whl
COMMAND echo "package whl start"
&& mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/wheel
&& cp -r ${BASE_DIR}/tf_adapter/python/. ${CMAKE_CURRENT_BINARY_DIR}/wheel
&& cp -r ${BASE_DIR}/convert_tf2npu ${CMAKE_CURRENT_BINARY_DIR}/wheel/npu_bridge
&& cp -r ${CMAKE_CURRENT_BINARY_DIR}/_tf_adapter.so ${CMAKE_CURRENT_BINARY_DIR}/wheel/npu_bridge
&& cp -r $<TARGET_FILE:SoftDp> ${CMAKE_CURRENT_BINARY_DIR}/wheel/npu_bridge
# && cp -r ${CMAKE_CURRENT_BINARY_DIR}/../../../../../soft_dp/libSoftDp.so ${CMAKE_CURRENT_BINARY_DIR}/wheel/npu_bridge
&& cd ${CMAKE_CURRENT_BINARY_DIR}/wheel
&& ${HI_PYTHON} setup.py bdist_wheel >/dev/null
&& cp -f dist/npu_bridge-1.15.0-py3-none-any.whl ${CMAKE_CURRENT_BINARY_DIR}/
&& rm -rf ${BASE_DIR}/pywrap_tensorflow_internal.cc
&& rm -rf ${BASE_DIR}/tensorflow_framework.cc
&& rm -rf ${BASE_DIR}/_pywrap_tensorflow_internal.so
&& rm -rf ${BASE_DIR}/libpywrap_tensorflow_internal.so
&& rm -rf ${BASE_DIR}/libtensorflow_framework.so.1
&& rm -rf ${BASE_DIR}/libtensorflow_framework.so
&& echo "package whl end"
)
###################################################################################################
set(INSTALL_BASE_DIR "")
set(INSTALL_LIBRARY_DIR lib)
include(CMakePackageConfigHelpers)
install(TARGETS tf_adapter OPTIONAL
EXPORT tfplugin-targets
LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/npu_bridge-1.15.0-py3-none-any.whl OPTIONAL
DESTINATION ${INSTALL_LIBRARY_DIR}
)
endif()