Skip to content

Commit da31b2a

Browse files
committed
Improve FindNodeJS.cmake for NodeJS 14 (built as dll).
1 parent 024c860 commit da31b2a

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

cmake/FindNodeJS.cmake

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,31 @@ if(NOT NodeJS_LIBRARY)
390390
set(NodeJS_BUILD_TYPE release)
391391
endif()
392392

393-
execute_process(COMMAND vcbuild.bat dll ${NodeJS_BUILD_TYPE} ${NodeJS_COMPILE_ARCH} ${NodeJS_MSVC_VER} WORKING_DIRECTORY "${NodeJS_OUTPUT_PATH}")
393+
# Building NodeJS 14 as library in Windows is broken (so we need to patch it)
394+
if(WIN32 AND NodeJS_VERSION_MAJOR GREATER_EQUAL 14)
395+
set(Python_ADDITIONAL_VERSIONS 3)
396+
find_package(PythonInterp REQUIRED)
397+
398+
execute_process(
399+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/NodeJSGYPPatch.py ${NodeJS_OUTPUT_PATH}/node.gyp
400+
WORKING_DIRECTORY "${NodeJS_OUTPUT_PATH}"
401+
RESULT_VARIABLE NodeJS_PATCH_SCRIPT
402+
)
403+
404+
if(NOT NodeJS_PATCH_SCRIPT EQUAL 0)
405+
message(FATAL_ERROR "FindNodeJS.cmake failed to patch node.gyp project")
406+
endif()
407+
endif()
408+
409+
execute_process(
410+
COMMAND vcbuild.bat dll ${NodeJS_BUILD_TYPE} ${NodeJS_COMPILE_ARCH} ${NodeJS_MSVC_VER}
411+
WORKING_DIRECTORY "${NodeJS_OUTPUT_PATH}"
412+
RESULT_VARIABLE NodeJS_BUILD_SCRIPT
413+
)
394414

395-
# TODO: Implement msi build
396-
# execute_process(COMMAND vcbuild.bat dll ${NodeJS_BUILD_TYPE} ${NodeJS_COMPILE_ARCH} ${NodeJS_MSVC_VER} msi WORKING_DIRECTORY "${NodeJS_OUTPUT_PATH}")
415+
if(NOT NodeJS_BUILD_SCRIPT EQUAL 0)
416+
message(FATAL_ERROR "FindNodeJS.cmake failed to build node library")
417+
endif()
397418

398419
# Copy library to MetaCall output path
399420
file(COPY ${NodeJS_OUTPUT_PATH}/${CMAKE_BUILD_TYPE}/node.dll DESTINATION ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/node.dll)

cmake/NodeJSGYPPatch.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from sys import argv
2+
from os.path import isfile
3+
from shutil import copy2
4+
5+
# Validate arguments
6+
if len(argv) != 2:
7+
print('Invalid number of arguments, you should pass the location of \'node.gyp\'.')
8+
print('Usage: python3 NodeJSGYPPatch.py <path_to_node_gyp>')
9+
exit(1)
10+
11+
# Validate the project file
12+
if not isfile(argv[1]) or not argv[1].endswith('node.gyp'):
13+
print('The file \'' + argv[1] + '\' does not exist or is not a valid gyp file, it must be named as \'node.gyp\'.')
14+
exit(2)
15+
16+
# Read the file
17+
f = open(argv[1])
18+
nodegyp = eval(f.read())
19+
f.close()
20+
21+
# Validate that the target is present and it is correct
22+
target = next((x for x in nodegyp['targets'] if 'target_name' in x and x['target_name'] == '<(node_lib_target_name)'), None)
23+
24+
if target is None:
25+
print('Invalid node.gyp configuration, the target \'node_lib_target_name\' is not present.')
26+
exit(3)
27+
28+
condition = next((x for x in target['conditions'] if x[0] == 'OS=="win"'), None)
29+
30+
if condition is None or (condition is not None and len(condition) != 2):
31+
print('Invalid node.gyp configuration, the condition \'OS=="win"\' is not present in target \'node_lib_target_name\'.')
32+
exit(4)
33+
34+
if not 'libraries' in condition[1]:
35+
print('Invalid node.gyp configuration, \'libraries\' field is not present in the condition \'OS=="win"\' of the target target \'node_lib_target_name\'.')
36+
exit(5)
37+
38+
# Get the libraries
39+
libraries = condition[1]['libraries']
40+
41+
# Check if the library to patch is present
42+
if not 'Winmm' in libraries:
43+
# Copy file as backup
44+
copy2(argv[1], argv[1] + '.backup')
45+
46+
# Apply the patch to the libraries
47+
libraries.append('Winmm')
48+
49+
# Overwrite the node.gyp project
50+
f = open(argv[1], 'w')
51+
f.write(repr(nodegyp))
52+
f.close()
53+
54+
print('Build project node.gyp patched correctly')

0 commit comments

Comments
 (0)