diff --git a/tests/es6/buildSystem.js b/tests/es6/buildSystem.js index 1fe4df42..1182943d 100644 --- a/tests/es6/buildSystem.js +++ b/tests/es6/buildSystem.js @@ -48,6 +48,10 @@ describe('BuildSystem', function () { await testCases.buildPrototypeNapi() }) + it('should build prototpye with nodeapi in c', async function () { + await testCases.buildPrototypeNapiC() + }) + it('should run with old GNU compilers', async function () { await testCases.shouldConfigurePreC11Properly() }) diff --git a/tests/es6/prototype-napi-c/CMakeLists.txt b/tests/es6/prototype-napi-c/CMakeLists.txt new file mode 100644 index 00000000..fcb445a9 --- /dev/null +++ b/tests/es6/prototype-napi-c/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.15) +cmake_policy(SET CMP0091 NEW) +cmake_policy(SET CMP0042 NEW) + +project (addon_napi_c LANGUAGES C) + +if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")) + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + +include_directories(${CMAKE_JS_INC}) + +add_library(${PROJECT_NAME} SHARED src/addon.c ${CMAKE_JS_SRC}) + +set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") + +target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) + +if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) + # Generate node.lib + execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) +endif() diff --git a/tests/es6/prototype-napi-c/src/addon.c b/tests/es6/prototype-napi-c/src/addon.c new file mode 100644 index 00000000..1cf4c5b8 --- /dev/null +++ b/tests/es6/prototype-napi-c/src/addon.c @@ -0,0 +1,27 @@ +#include +#include + +static napi_value Method(napi_env env, napi_callback_info info) +{ + napi_status status; + napi_value world; + status = napi_create_string_utf8(env, "world", 5, &world); + assert(status == napi_ok); + return world; +} + +#define DECLARE_NAPI_METHOD(name, func) \ + { \ + name, 0, func, 0, 0, 0, napi_default, 0 \ + } + +static napi_value Init(napi_env env, napi_value exports) +{ + napi_status status; + napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); + status = napi_define_properties(env, exports, 1, &desc); + assert(status == napi_ok); + return exports; +} + +NAPI_MODULE(addon_napi, Init); diff --git a/tests/es6/prototype-napi/CMakeLists.txt b/tests/es6/prototype-napi/CMakeLists.txt index 53320e33..a4e73e3a 100644 --- a/tests/es6/prototype-napi/CMakeLists.txt +++ b/tests/es6/prototype-napi/CMakeLists.txt @@ -10,7 +10,7 @@ endif() include_directories(${CMAKE_JS_INC}) -add_library(${PROJECT_NAME} SHARED src/addon.cpp) +add_library(${PROJECT_NAME} SHARED src/addon.cpp ${CMAKE_JS_SRC}) set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") diff --git a/tests/es6/prototype/CMakeLists.txt b/tests/es6/prototype/CMakeLists.txt index 0a0e9ab3..cd1219f6 100644 --- a/tests/es6/prototype/CMakeLists.txt +++ b/tests/es6/prototype/CMakeLists.txt @@ -10,7 +10,7 @@ endif() include_directories(${CMAKE_JS_INC}) -add_library(addon SHARED src/addon.cpp) +add_library(addon SHARED src/addon.cpp ${CMAKE_JS_SRC}) set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") diff --git a/tests/es6/prototype2/CMakeLists.txt b/tests/es6/prototype2/CMakeLists.txt index aab7498a..03645c63 100644 --- a/tests/es6/prototype2/CMakeLists.txt +++ b/tests/es6/prototype2/CMakeLists.txt @@ -10,7 +10,7 @@ endif() include_directories(${CMAKE_JS_INC}) -add_library(${PROJECT_NAME} SHARED src/addon.cpp) +add_library(${PROJECT_NAME} SHARED src/addon.cpp ${CMAKE_JS_SRC}) set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") diff --git a/tests/es6/testCases.js b/tests/es6/testCases.js index c359cec0..0d93c1db 100644 --- a/tests/es6/testCases.js +++ b/tests/es6/testCases.js @@ -37,6 +37,17 @@ const testCases = { process.chdir(cwd) } }, + buildPrototypeNapiC: async function (options) { + const cwd = process.cwd() + process.chdir(path.resolve(path.join(__dirname, './prototype-napi-c'))) + const buildSystem = new BuildSystem(options) + try { + await buildSystem.rebuild() + assert.ok((await fs.stat(path.join(__dirname, 'prototype-napi-c/build/Release/addon_napi_c.node'))).isFile()) + } finally { + process.chdir(cwd) + } + }, shouldConfigurePreC11Properly: async function (options) { options = { directory: path.resolve(path.join(__dirname, './prototype')),