forked from holepunchto/cmake-napi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmake-napi.cmake
329 lines (251 loc) · 6.76 KB
/
cmake-napi.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
include_guard()
set(napi_module_dir "${CMAKE_CURRENT_LIST_DIR}")
function(download_node_headers result)
set(one_value_keywords
DESTINATION
VERSION
IMPORT_FILE
NODE_BIN
)
cmake_parse_arguments(
PARSE_ARGV 1 ARGV "" "${one_value_keywords}" ""
)
if(NOT ARGV_DESTINATION)
set(ARGV_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/_napi")
endif()
if(NOT ARGV_VERSION)
set(ARGV_VERSION "18.20.4")
endif()
napi_target(target)
set(version "v${ARGV_VERSION}")
set(archive "${CMAKE_CURRENT_BINARY_DIR}/node-${version}.tar.gz")
file(DOWNLOAD
"https://github.com/nodejs-mobile/nodejs-mobile/releases/download/${version}/nodejs-mobile-${version}-android.zip"
"${archive}"
)
file(ARCHIVE_EXTRACT
INPUT "${archive}"
DESTINATION "${ARGV_DESTINATION}"
)
set(import_file ${ARGV_IMPORT_FILE})
if(import_file)
if(target MATCHES "win32")
napi_arch(arch)
set(lib "${ARGV_DESTINATION}/node-${version}/lib/node.lib")
file(DOWNLOAD
"https://nodejs.org/download/release/${version}/win-${arch}/node.lib"
"${lib}"
)
set(${import_file} "${lib}")
else()
set(${import_file} ${import_file}-NOTFOUND)
endif()
endif()
set(${result} "${ARGV_DESTINATION}/include/node")
set(node_bin_arg ${ARGV_NODE_BIN})
if (node_bin_arg)
node_arch(arch)
set(${node_bin_arg} "${ARGV_DESTINATION}/bin/${arch}/libnode.so")
endif()
return(PROPAGATE ${result} ${node_bin_arg} ${import_file})
endfunction()
function(napi_platform result)
set(platform ${CMAKE_SYSTEM_NAME})
if(NOT platform)
set(platform ${CMAKE_HOST_SYSTEM_NAME})
endif()
string(TOLOWER ${platform} platform)
if(platform MATCHES "darwin|ios|linux|android")
set(${result} ${platform})
elseif(platform MATCHES "windows")
set(${result} "win32")
else()
set(${result} "unknown")
endif()
return(PROPAGATE ${result})
endfunction()
function(napi_arch result)
if(APPLE AND CMAKE_OSX_ARCHITECTURES)
set(arch ${CMAKE_OSX_ARCHITECTURES})
elseif(MSVC AND CMAKE_GENERATOR_PLATFORM)
set(arch ${CMAKE_GENERATOR_PLATFORM})
elseif(ANDROID AND CMAKE_ANDROID_ARCH_ABI)
set(arch ${CMAKE_ANDROID_ARCH_ABI})
else()
set(arch ${CMAKE_SYSTEM_PROCESSOR})
endif()
if(NOT arch)
set(arch ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()
string(TOLOWER ${arch} arch)
if(arch MATCHES "arm64|aarch64")
set(${result} "arm64")
elseif(arch MATCHES "armv7-a|armeabi-v7a")
set(${result} "arm")
elseif(arch MATCHES "x64|x86_64|amd64")
set(${result} "x64")
elseif(arch MATCHES "x86|i386|i486|i586|i686")
set(${result} "ia32")
else()
set(${result} "unknown")
endif()
return(PROPAGATE ${result})
endfunction()
function(node_arch result)
napi_arch(arch)
if(arch STREQUAL "arm")
set(${result} "armeabi-v7a")
elseif(arch STREQUAL "arm64")
set(${result} "arm64-v8a")
elseif(arch STREQUAL "ia32")
set(${result} "x86")
elseif(arch STREQUAL "x64")
set(${result} "x86_64")
else()
set(${result} ${arch})
endif()
return(PROPAGATE ${result})
endfunction()
function(napi_target result)
napi_platform(platform)
napi_arch(arch)
set(${result} ${platform}-${arch})
return(PROPAGATE ${result})
endfunction()
function(napi_module_target directory result)
set(one_value_keywords
NAME
VERSION
HASH
)
cmake_parse_arguments(
PARSE_ARGV 2 ARGV "" "${one_value_keywords}" ""
)
set(package_path package.json)
cmake_path(ABSOLUTE_PATH directory NORMALIZE)
cmake_path(ABSOLUTE_PATH package_path BASE_DIRECTORY "${directory}" NORMALIZE)
file(READ "${package_path}" package)
string(JSON name GET "${package}" "name")
string(REGEX REPLACE "/" "+" name ${name})
string(REGEX REPLACE "^@" "" name ${name})
string(JSON version GET "${package}" "version")
string(SHA256 hash "napi ${package_path}")
string(SUBSTRING "${hash}" 0 8 hash)
set(${result} "${name}-${version}-${hash}")
if(ARGV_NAME)
set(${ARGV_NAME} ${name} PARENT_SCOPE)
endif()
if(ARGV_VERSION)
set(${ARGV_VERSION} ${version} PARENT_SCOPE)
endif()
if(ARGV_HASH)
set(${ARGV_HASH} ${hash} PARENT_SCOPE)
endif()
return(PROPAGATE ${result})
endfunction()
function(add_napi_module result)
download_node_headers(node_headers IMPORT_FILE node_lib NODE_BIN node_bin)
napi_module_target("." target NAME name VERSION version)
string(REGEX MATCH "^[0-9]+" major "${version}")
add_library(${target} OBJECT)
set_target_properties(
${target}
PROPERTIES
C_STANDARD 11
CXX_STANDARD 20
POSITION_INDEPENDENT_CODE ON
)
target_include_directories(
${target}
PRIVATE
${node_headers}
)
set(${result} ${target})
napi_target(host)
if(host MATCHES "ios")
return(PROPAGATE ${result})
endif()
add_library(${target}_module SHARED)
set_target_properties(
${target}_module
PROPERTIES
OUTPUT_NAME ${name}
PREFIX ""
SUFFIX ".node"
IMPORT_PREFIX ""
IMPORT_SUFFIX ".node.lib"
# Don't set a shared library name to allow loading the resulting library as
# a plugin.
NO_SONAME ON
# Set the Mach-O compatibility versions for macOS and iOS. This is mostly
# for debugging purposes.
MACHO_CURRENT_VERSION ${version}
MACHO_COMPATIBILITY_VERSION ${major}
# Automatically export all available symbols on Windows. Without this,
# module authors would have to explicitly export public symbols.
WINDOWS_EXPORT_ALL_SYMBOLS ON
)
target_link_libraries(
${target}_module
PRIVATE
${target}
)
if(host MATCHES "android")
target_link_libraries(
${target}_module
PRIVATE
${node_bin}
)
endif()
if(host MATCHES "win32")
add_executable(${target}_import_library IMPORTED)
set_target_properties(
${target}_import_library
PROPERTIES
ENABLE_EXPORTS ON
IMPORTED_IMPLIB "${node_lib}"
)
if(NOT TARGET napi_delay_load)
add_library(napi_delay_load STATIC)
target_sources(
napi_delay_load
PRIVATE
"${napi_module_dir}/win32/delay-load.c"
)
target_include_directories(
napi_delay_load
PRIVATE
${napi_headers}
)
target_link_libraries(
napi_delay_load
INTERFACE
delayimp
)
target_link_options(
napi_delay_load
INTERFACE
/DELAYLOAD:node.exe
)
endif()
target_link_libraries(
${target}_module
PRIVATE
${target}_import_library
PUBLIC
napi_delay_load
)
else()
target_link_options(
${target}_module
PRIVATE
-Wl,-undefined,dynamic_lookup
)
endif()
install(
FILES $<TARGET_FILE:${target}_module>
DESTINATION ${host}
RENAME ${name}.node
)
return(PROPAGATE ${result})
endfunction()