-
Notifications
You must be signed in to change notification settings - Fork 4
/
vendor.cmake
236 lines (214 loc) · 8.12 KB
/
vendor.cmake
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
include_guard(GLOBAL)
if (EMSCRIPTEN)
if (EMSCRIPTEN_PTHREADS)
set(ARCH wasm-mt)
else ()
set(ARCH wasm)
endif ()
elseif (OHOS)
if (OHOS_ARCH STREQUAL "arm64-v8a")
set(ARCH arm64)
elseif (OHOS_ARCH STREQUAL "x86_64")
set(ARCH x64)
else ()
set(ARCH arm)
endif ()
elseif (ANDROID OR IOS)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
if (SDK_NAME STREQUAL iphonesimulator)
set(ARCH arm64-simulator)
else ()
set(ARCH arm64)
endif ()
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(ARCH x64)
else ()
set(ARCH arm)
endif ()
else ()
if (MSVC)
string(TOLOWER ${MSVC_C_ARCHITECTURE_ID} ARCH)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR $CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
set(ARCH x64)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
set(ARCH arm64)
else ()
set(ARCH x86)
endif ()
endif ()
if (EMSCRIPTEN)
set(WEB TRUE)
set(PLATFORM web)
elseif (OHOS)
set(PLATFORM ohos)
message("Using OHOS NDK: ${OHOS_SDK_NATIVE}")
set(ENV{CMAKE_OHOS_NDK} ${OHOS_SDK_NATIVE})
list(APPEND ENV_LIST CMAKE_OHOS_NDK=${OHOS_SDK_NATIVE})
elseif (ANDROID)
set(PLATFORM android)
message("Using Android NDK: ${CMAKE_ANDROID_NDK}")
set(ENV{CMAKE_ANDROID_NDK} ${CMAKE_ANDROID_NDK})
list(APPEND ENV_LIST CMAKE_ANDROID_NDK=${CMAKE_ANDROID_NDK})
elseif (IOS)
set(PLATFORM ios)
elseif (APPLE)
set(MACOS TRUE)
set(PLATFORM mac)
elseif (WIN32)
set(PLATFORM win)
elseif (CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
set(LINUX TRUE)
set(PLATFORM linux)
endif ()
if (MSVC)
string(REGEX MATCH ".*Microsoft Visual Studio.*VC/Tools/MSVC/" CMAKE_MSVC_PATH ${CMAKE_C_COMPILER})
if (CMAKE_MSVC_PATH)
string(REGEX REPLACE "/Tools/MSVC/" "" CMAKE_MSVC_PATH ${CMAKE_MSVC_PATH})
message("Using MSVC: ${CMAKE_MSVC_PATH}")
set(ENV{CMAKE_MSVC_PATH} ${CMAKE_MSVC_PATH})
list(APPEND ENV_LIST CMAKE_MSVC_PATH=${CMAKE_MSVC_PATH})
endif ()
endif ()
# Sets the default build type to release.
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif ()
if (WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(VENDOR_DEBUG ON)
endif ()
if (VENDOR_DEBUG)
set(LIBRARY_ENTRY debug/${PLATFORM}/${ARCH})
set(INCLUDE_ENTRY debug/${PLATFORM}/include)
set(VENDOR_DEBUG_FLAG -d)
else ()
set(LIBRARY_ENTRY ${PLATFORM}/${ARCH})
set(INCLUDE_ENTRY ${PLATFORM}/include)
endif ()
set(VENDOR_TOOLS_DIR ${CMAKE_CURRENT_LIST_DIR})
set(ENV_CMD ${CMAKE_COMMAND} -E env ${ENV_LIST})
# merge_libraries_into(target [staticLibraries...])
function(merge_libraries_into target)
if (ARGC GREATER 2)
list(JOIN ARGN "\" \"" STATIC_LIBRARIES)
else ()
list(APPEND STATIC_LIBRARIES ${ARGN})
endif ()
separate_arguments(STATIC_LIBRARIES_LIST NATIVE_COMMAND "\"${STATIC_LIBRARIES}\"")
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${ENV_CMD} node ${VENDOR_TOOLS_DIR}/lib-merge -p ${PLATFORM} -a ${ARCH} -v
$<TARGET_FILE:${target}> ${STATIC_LIBRARIES_LIST} -o $<TARGET_FILE:${target}>
VERBATIM USES_TERMINAL)
endfunction()
# add_vendor_target(targetName [STATIC_VENDORS] <vendorNames...> [SHARED_VENDORS] <vendorNames...> [CONFIG_DIR] <configDir>)
function(add_vendor_target targetName)
set(IS_SHARED FALSE)
set(IS_CONFIG_DIR FALSE)
set(CONFIG_DIR ${CMAKE_CURRENT_LIST_DIR})
foreach (arg ${ARGN})
if (arg STREQUAL "STATIC_VENDORS")
set(IS_SHARED FALSE)
continue()
endif ()
if (arg STREQUAL "SHARED_VENDORS")
set(IS_SHARED TRUE)
continue()
endif ()
if (arg STREQUAL "CONFIG_DIR")
set(IS_CONFIG_DIR TRUE)
continue()
endif ()
if (IS_CONFIG_DIR)
set(CONFIG_DIR ${arg})
set(IS_CONFIG_DIR FALSE)
elseif (IS_SHARED)
list(APPEND sharedVendors ${arg})
else ()
list(APPEND staticVendors ${arg})
endif ()
endforeach ()
if (NOT sharedVendors AND NOT staticVendors)
add_custom_target(${targetName} COMMENT "Generating ${targetName}")
return()
endif ()
set(VENDOR_STATIC_LIBRARIES)
set(VENDOR_SHARED_LIBRARIES)
set(VENDOR_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${targetName}.dir)
if (staticVendors)
set(VENDOR_OUTPUT_LIB "${VENDOR_OUTPUT_DIR}/${ARCH}/lib${targetName}${CMAKE_STATIC_LIBRARY_SUFFIX}")
list(APPEND VENDOR_STATIC_LIBRARIES ${VENDOR_OUTPUT_LIB})
endif ()
foreach (sharedVendor ${sharedVendors})
file(GLOB SHARED_LIBS third_party/out/${sharedVendor}/${LIBRARY_ENTRY}/*${CMAKE_SHARED_LIBRARY_SUFFIX})
if (NOT SHARED_LIBS)
# Build shared libraries immediately if they don't exist to gather output files initially.
execute_process(COMMAND ${ENV_CMD} node ${VENDOR_TOOLS_DIR}/vendor-build ${sharedVendor} -p ${PLATFORM} -a ${ARCH} -v ${VENDOR_DEBUG_FLAG}
WORKING_DIRECTORY ${CONFIG_DIR})
endif ()
file(GLOB SHARED_LIBS third_party/out/${sharedVendor}/${LIBRARY_ENTRY}/*${CMAKE_SHARED_LIBRARY_SUFFIX})
list(APPEND VENDOR_SHARED_LIBRARIES ${SHARED_LIBS})
endforeach ()
set(VENDOR_CMD ${ENV_CMD} node ${VENDOR_TOOLS_DIR}/vendor-build -p ${PLATFORM} -a ${ARCH} -v ${VENDOR_DEBUG_FLAG} -o ${VENDOR_OUTPUT_DIR})
add_custom_target(${targetName} COMMAND ${VENDOR_CMD} ${staticVendors} ${sharedVendors} WORKING_DIRECTORY ${CONFIG_DIR}
BYPRODUCTS ${VENDOR_STATIC_LIBRARIES} ${VENDOR_SHARED_LIBRARIES} ${VENDOR_OUTPUT_DIR}/.${ARCH}.md5
VERBATIM USES_TERMINAL COMMENT "Generating ${targetName}")
# set the target properties:
if (VENDOR_STATIC_LIBRARIES)
string(REPLACE ";" " " VENDOR_STATIC_LIBS "${VENDOR_STATIC_LIBRARIES}")
set_target_properties(${targetName} PROPERTIES STATIC_LIBRARIES ${VENDOR_STATIC_LIBS})
endif ()
if (VENDOR_SHARED_LIBRARIES)
string(REPLACE ";" " " VENDOR_SHARED_LIBS "${VENDOR_SHARED_LIBRARIES}")
set_target_properties(${targetName} PROPERTIES SHARED_LIBRARIES ${VENDOR_SHARED_LIBS})
endif ()
endfunction()
# find_vendor_libraries(target [STATIC] <STATIC_LIBRARIES_VAR> [SHARED] <SHARED_LIBRARIES_VAR>)
function(find_vendor_libraries target)
set(STATIC_LIBRARIES_VAR)
set(SHARED_LIBRARIES_VAR)
set(IS_STATIC FALSE)
set(IS_SHARED FALSE)
foreach (arg ${ARGN})
if (arg STREQUAL "STATIC")
set(IS_STATIC TRUE)
continue()
endif ()
if (arg STREQUAL "SHARED")
set(IS_SHARED TRUE)
continue()
endif ()
if (IS_STATIC)
set(STATIC_LIBRARIES_VAR ${arg})
set(IS_STATIC FALSE)
elseif (IS_SHARED)
set(SHARED_LIBRARIES_VAR ${arg})
set(IS_SHARED FALSE)
endif ()
endforeach ()
if (NOT STATIC_LIBRARIES_VAR AND NOT SHARED_LIBRARIES_VAR)
return()
endif ()
get_target_property(VENDOR_STATIC_LIBS ${target} STATIC_LIBRARIES)
string(REPLACE " " ";" VENDOR_STATIC_LIBRARIES "${VENDOR_STATIC_LIBS}")
get_target_property(VENDOR_SHARED_LIBS ${target} SHARED_LIBRARIES)
string(REPLACE " " ";" VENDOR_SHARED_LIBRARIES "${VENDOR_SHARED_LIBS}")
if (STATIC_LIBRARIES_VAR)
if (VENDOR_STATIC_LIBRARIES)
set(${STATIC_LIBRARIES_VAR} ${VENDOR_STATIC_LIBRARIES} PARENT_SCOPE)
else ()
set(${STATIC_LIBRARIES_VAR} PARENT_SCOPE)
endif ()
endif ()
if (SHARED_LIBRARIES_VAR)
if (VENDOR_SHARED_LIBRARIES)
set(${SHARED_LIBRARIES_VAR} ${VENDOR_SHARED_LIBRARIES} PARENT_SCOPE)
else ()
set(${SHARED_LIBRARIES_VAR} PARENT_SCOPE)
endif ()
endif ()
endfunction()
# Synchronizes the third-party dependencies of current platform.
if (WIN32)
execute_process(COMMAND cmd /C depsync ${PLATFORM} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ENCODING NONE)
else ()
execute_process(COMMAND depsync ${PLATFORM} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif ()