Skip to content

Commit fd0155f

Browse files
committed
Use cmake directly
1 parent bc89a1e commit fd0155f

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

CMakeLists.txt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
cmake_minimum_required(VERSION 3.15...3.31)
22
project(node-api-cts)
33

4+
set(NODE_API_HEADERS_DIR ${PROJECT_SOURCE_DIR}/node_modules/node-api-headers)
5+
6+
if(NOT EXISTS ${NODE_API_HEADERS_DIR})
7+
message(FATAL_ERROR "Expected ${NODE_API_HEADERS_DIR} to exist")
8+
endif()
9+
10+
if(MSVC)
11+
set(NODE_API_LIB ${PROJECT_BINARY_DIR}/node.lib)
12+
set(NODE_API_DEF ${NODE_API_HEADERS_DIR}/def/node_api.def)
13+
execute_process(COMMAND ${CMAKE_AR} /def:${NODE_API_DEF} /out:${NODE_API_LIB} ${CMAKE_STATIC_LINKER_FLAGS})
14+
set(NODE_API_SRC ${PROJECT_SOURCE_DIR}/implementors/node/win_delay_load_hook.cc)
15+
endif()
16+
417
function(add_node_api_cts_addon ADDON_NAME SRC)
5-
add_library(${ADDON_NAME} SHARED ${SRC} ${CMAKE_JS_SRC})
18+
add_library(${ADDON_NAME} SHARED ${SRC} ${NODE_API_SRC})
619
set_target_properties(${ADDON_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
7-
target_include_directories(${ADDON_NAME} PRIVATE ${CMAKE_JS_INC})
8-
target_link_libraries(${ADDON_NAME} PRIVATE ${CMAKE_JS_LIB})
20+
target_include_directories(${ADDON_NAME} PRIVATE ${NODE_API_HEADERS_DIR}/include)
21+
target_link_libraries(${ADDON_NAME} PRIVATE ${NODE_API_LIB})
922
target_compile_features(${ADDON_NAME} PRIVATE cxx_std_17)
1023
target_compile_definitions(${ADDON_NAME} PRIVATE ADDON_NAME=${ADDON_NAME})
1124
set_target_properties(${ADDON_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
25+
# TODO: Set these conditionally?
26+
set_target_properties(${ADDON_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
1227
endfunction()
1328

1429
file(GLOB_RECURSE cmake_dirs RELATIVE ${CMAKE_SOURCE_DIR} tests/*/CMakeLists.txt)
@@ -17,8 +32,3 @@ foreach(cmake_file ${cmake_dirs})
1732
get_filename_component(subdir ${cmake_file} DIRECTORY)
1833
add_subdirectory(${subdir})
1934
endforeach()
20-
21-
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
22-
# Generate node.lib
23-
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
24-
endif()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// This file is copied from
2+
// https://github.com/nodejs/node-gyp/blob/db5385c5467e5bfb914b9954f0313c46f1f4e10d/src/win_delay_load_hook.cc
3+
4+
/*
5+
* When this file is linked to a DLL, it sets up a delay-load hook that
6+
* intervenes when the DLL is trying to load the host executable
7+
* dynamically. Instead of trying to locate the .exe file it'll just
8+
* return a handle to the process image.
9+
*
10+
* This allows compiled addons to work when the host executable is renamed.
11+
*/
12+
13+
#ifdef _MSC_VER
14+
15+
#pragma managed(push, off)
16+
17+
#ifndef WIN32_LEAN_AND_MEAN
18+
#define WIN32_LEAN_AND_MEAN
19+
#endif
20+
21+
#include <windows.h>
22+
23+
#include <delayimp.h>
24+
#include <string.h>
25+
26+
static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
27+
HMODULE m;
28+
if (event != dliNotePreLoadLibrary)
29+
return NULL;
30+
31+
if (_stricmp(info->szDll, HOST_BINARY) != 0)
32+
return NULL;
33+
34+
// try for libnode.dll to compat node.js that using 'vcbuild.bat dll'
35+
m = GetModuleHandle(TEXT("libnode.dll"));
36+
if (m == NULL) m = GetModuleHandle(NULL);
37+
return (FARPROC) m;
38+
}
39+
40+
decltype(__pfnDliNotifyHook2) __pfnDliNotifyHook2 = load_exe_hook;
41+
42+
#pragma managed(pop)
43+
44+
#endif

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
"scripts": {
77
"node:test": "node --test ./implementors/node/run-tests.ts",
88
"lint": "eslint",
9-
"build-addons": "cmake-js build"
9+
"addons:configure": "cmake -G Ninja -S . -B ./build",
10+
"addons:build": "cmake --build ./build",
11+
"addons:clean": "git clean -xf '**/*.node' && rm -rf ./build"
1012
},
1113
"devDependencies": {
1214
"@types/node": "^24.10.1",
13-
"cmake-js": "^7.4.0",
14-
"eslint": "^9.39.1"
15+
"eslint": "^9.39.1",
16+
"node-api-headers": "^1.7.0"
1517
},
1618
"dependencies": {
1719
"amaro": "^1.1.5"

0 commit comments

Comments
 (0)