From 43ab009a46a1ab75a7e7fadce99805920f1045ed Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Thu, 1 Feb 2024 23:20:57 +0100 Subject: [PATCH 01/92] remove proof of concept for clarity --- package.json | 26 +------------------------- vcpkg.json | 21 --------------------- 2 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 vcpkg.json diff --git a/package.json b/package.json index b11e3114..58023657 100644 --- a/package.json +++ b/package.json @@ -66,31 +66,7 @@ }, "scripts": { "test": "mocha tests", - "lint": "eslint lib bin/cmake-js tests", - - "postinstall": "cmake -S . -B ./build -DBUILD_TESTS:BOOL=TRUE && cmake --build ./build && cmake --install ./build --prefix ./install && ctest --test-dir ./build --rerun-failed --output-on-failure --verbose && cpack -B ./install --config ./build/CPackConfig.cmake && cpack -B ./install --config ./build/CPackSourceConfig.cmake", - - "configure": "cmake -S . -B ./build -DBUILD_TESTS:BOOL=TRUE", - "reconfigure": "cmake --fresh -S . -B ./build -DBUILD_TESTS:BOOL=TRUE", - "build": "cmake --build ./build", - "rebuild": "cmake --fresh -S . -B ./build && cmake --build ./build", - - - "cmake:configure": "cmake -S . -B ./build -DBUILD_TESTS:BOOL=TRUE", - "cmake:reconfigure": "cmake --fresh -S . -B ./build -DBUILD_TESTS:BOOL=TRUE", - "cmake:build": "cmake --build ./build", - "cmake:rebuild": "cmake --fresh -S . -B ./build && cmake --build ./build", - "cmake:install": "cmake --install ./build --prefix ./install", - "cmake:help": "cmake --help", - - "ctest": "ctest --test-dir ./build --rerun-failed --output-on-failure --verbose", - "ctest:help": "ctest --help", - - "cpack:all": "cpack -B ./build --config ./build/CPackConfig.cmake", - "cpack:zip": "cpack -B ./build --config ./build/CPackConfig.cmake --generator ZIP", - "cpack:tar": "cpack -B ./build --config ./build/CPackConfig.cmake --generator TAR", - "cpack:source": "cpack -B ./build --config ./build/CPackSourceConfig.cmake", - "cpack:help": "cpack --help" + "lint": "eslint lib bin/cmake-js tests" }, "files": [ "lib", diff --git a/vcpkg.json b/vcpkg.json deleted file mode 100644 index 8ced79dc..00000000 --- a/vcpkg.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "cmake-js", - "version": "7.3.3", - "homepage": "https://github.com/cmake-js/cmake-js", - "description": "CMake.js - a Node.js native addon build tool.", - "license": "MIT", - "builtin-baseline": "288e8bebf4ca67c1f8ebd49366b03650cfd9eb7d", - "features": { - "node-dev": { "description": "NodeJS development headers", "dependencies": ["vcpkg-tool-nodejs"]}, - "node-api": { "description": "Node API (C) headers", "dependencies": ["node-api-headers"]}, - "node-addon-api": { "description": "Node Addon API (C++) headers", "dependencies": ["node-addon-api"]}, - "cmake-js": { "description": "CMake.js - a Node.js native addon build tool.", "dependencies": ["vcpkg-tool-nodejs", "node-api-headers", "node-addon-api"]} - }, - "dependencies": [ - { "name": "vcpkg-cmake", "host": true }, - { "name": "vcpkg-cmake-config", "host": true }, - { "name": "vcpkg-tool-nodejs", "host": true }, - { "name": "node-api-headers", "host": false, "version>=": "1.1.0" }, - { "name": "node-addon-api", "host": false, "version>=": "6.1.0" } - ] -} From dd30c384dc4f508503f6f46952349e2733546a43 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Thu, 1 Feb 2024 23:29:19 +0100 Subject: [PATCH 02/92] cd into this dir and do npm/yarn install then run start --- tests/api/hello/.gitingore | 3 ++ tests/api/hello/CMakeLists.txt | 12 ++++++++ tests/api/hello/index.js | 5 +++ tests/api/hello/package.json | 22 +++++++++++++ tests/api/hello/src/hello/addon.cpp | 48 +++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 tests/api/hello/.gitingore create mode 100644 tests/api/hello/CMakeLists.txt create mode 100644 tests/api/hello/index.js create mode 100644 tests/api/hello/package.json create mode 100644 tests/api/hello/src/hello/addon.cpp diff --git a/tests/api/hello/.gitingore b/tests/api/hello/.gitingore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/api/hello/.gitingore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/api/hello/CMakeLists.txt b/tests/api/hello/CMakeLists.txt new file mode 100644 index 00000000..cf6086b8 --- /dev/null +++ b/tests/api/hello/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.15) + +list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") + +project(hello) + +include(CMakeJS) + +cmakejs_create_napi_addon( + addon + src/hello/addon.cpp +) diff --git a/tests/api/hello/index.js b/tests/api/hello/index.js new file mode 100644 index 00000000..0fa61d24 --- /dev/null +++ b/tests/api/hello/index.js @@ -0,0 +1,5 @@ +const demo = require('./build/lib/addon.node'); + +console.log(demo.hello()) + +module.exports = demo; diff --git a/tests/api/hello/package.json b/tests/api/hello/package.json new file mode 100644 index 00000000..c7a2aba3 --- /dev/null +++ b/tests/api/hello/package.json @@ -0,0 +1,22 @@ +{ + "name": "@vendor/hello", + "version": "1.0.0", + "description": "A test addon made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node ./index.js", + "postinstall": "cmake-js install", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" + }, + "dependencies": { + "cmake-js": "../../../", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + } +} diff --git a/tests/api/hello/src/hello/addon.cpp b/tests/api/hello/src/hello/addon.cpp new file mode 100644 index 00000000..806362d5 --- /dev/null +++ b/tests/api/hello/src/hello/addon.cpp @@ -0,0 +1,48 @@ +/** + * @file addon.cpp + * @brief A quick 'hello world' Napi Addon in C++ +*/ + +// Required header and C++ flag +#if __has_include() && BUILDING_NODE_EXTENSION + +#include + +#ifndef STRINGIFY +# define STRINGIFY_HELPER(n) #n +# define STRINGIFY(n) STRINGIFY_HELPER(n) +#endif + +Napi::Value Hello(const Napi::CallbackInfo& info) { + return Napi::String::New(info.Env(), STRINGIFY(CMAKEJS_ADDON_NAME)".node is online!"); +} + +Napi::Value Version(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), NAPI_VERSION); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + + // Export a chosen C++ function under a given Javascript key + exports.Set( + Napi::String::New(env, "hello"), // Name of function on Javascript side... + Napi::Function::New(env, Hello) // Name of function on C++ side... + ); + + exports.Set( + Napi::String::New(env, "version"), + Napi::Function::New(env, Version) + ); + + // The above will expose the C++ function 'Hello' as a javascript function + // named 'hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NODE_API_MODULE(CMAKEJS_ADDON_NAME, Init) // (name to use, initializer to use) + + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif From 995e72bf8a1f96f11e327f311e92305b4babd5a6 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Thu, 1 Feb 2024 23:52:33 +0100 Subject: [PATCH 03/92] typo! --- tests/api/hello/{.gitingore => .gitignore} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/api/hello/{.gitingore => .gitignore} (100%) diff --git a/tests/api/hello/.gitingore b/tests/api/hello/.gitignore similarity index 100% rename from tests/api/hello/.gitingore rename to tests/api/hello/.gitignore From 538aa421fce30f7c90b92e4b2f28908f649549b0 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 00:21:42 +0100 Subject: [PATCH 04/92] Added consumer of test addon --- tests/api/hello_consumer/.gitignore | 3 +++ tests/api/hello_consumer/index.js | 3 +++ tests/api/hello_consumer/package.json | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 tests/api/hello_consumer/.gitignore create mode 100644 tests/api/hello_consumer/index.js create mode 100644 tests/api/hello_consumer/package.json diff --git a/tests/api/hello_consumer/.gitignore b/tests/api/hello_consumer/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/api/hello_consumer/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/api/hello_consumer/index.js b/tests/api/hello_consumer/index.js new file mode 100644 index 00000000..0c2a50dc --- /dev/null +++ b/tests/api/hello_consumer/index.js @@ -0,0 +1,3 @@ +const addon = require('@vendor/hello'); + +console.log(addon.hello()) diff --git a/tests/api/hello_consumer/package.json b/tests/api/hello_consumer/package.json new file mode 100644 index 00000000..a5e009f4 --- /dev/null +++ b/tests/api/hello_consumer/package.json @@ -0,0 +1,13 @@ +{ + "name": "@vendor/hello_consumer", + "version": "1.0.0", + "description": "A test addon consumer made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node ./index.js" + }, + "dependencies": { + "@vendor/hello": "../hello/" + } +} From 677623a508aecf7f0dc5a593876ff1f6e2db1ced Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 02:14:51 +0100 Subject: [PATCH 05/92] Test consumer working, without depending on cmake-js :) --- CMakeLists.txt | 6 +++--- lib/cMake.js | 5 +++++ share/cmake/api.js | 3 +++ tests/api/hello/CMakeLists.txt | 5 ++++- tests/api/hello/package.json | 3 ++- 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 share/cmake/api.js diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bca7f82..b1806bff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,8 +133,8 @@ console.log(`Napi Version: ${demo.version()}`); set(CPACK_PACKAGE_VERSION_TWEAK ${PROJECT_VERSION_VERSION_TWEAK}) set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) - set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF) - set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY OFF) + set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON) + set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") set(CPACK_SOURCE_IGNORE_FILES _CPack_Packages @@ -156,7 +156,7 @@ console.log(`Napi Version: ${demo.version()}`); #/lib /usr /out - /build + #/build /Release /Debug /MinSizeRel diff --git a/lib/cMake.js b/lib/cMake.js index 6ec0a79a..ce1921b4 100644 --- a/lib/cMake.js +++ b/lib/cMake.js @@ -13,6 +13,8 @@ const npmConfigData = require('rc')('npm') const Toolset = require('./toolset') const headers = require('node-api-headers') +const path_to_api = require('../share/cmake/api') + class CMake { get path() { return this.options.cmakePath || 'cmake' @@ -194,6 +196,9 @@ class CMake { D.push({ CMAKE_MAKE_PROGRAM: this.toolset.makePath }) } + // CMakeJS.cmake api + D.push({ CMAKE_MODULE_PATH: path_to_api }) + // Load NPM config for (const [key, value] of Object.entries(npmConfigData)) { if (key.startsWith('cmake_')) { diff --git a/share/cmake/api.js b/share/cmake/api.js new file mode 100644 index 00000000..bf2f2fee --- /dev/null +++ b/share/cmake/api.js @@ -0,0 +1,3 @@ +// The CLI needs a portable path to the API +const path_to_api = __dirname; +module.exports = path_to_api; diff --git a/tests/api/hello/CMakeLists.txt b/tests/api/hello/CMakeLists.txt index cf6086b8..c00bee4b 100644 --- a/tests/api/hello/CMakeLists.txt +++ b/tests/api/hello/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.15) -list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +# If not using cmake-js CLI, fetch the API manually +if (NOT DEFINED CMAKE_JS_VERSION) + list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif () project(hello) diff --git a/tests/api/hello/package.json b/tests/api/hello/package.json index c7a2aba3..05a9531a 100644 --- a/tests/api/hello/package.json +++ b/tests/api/hello/package.json @@ -6,7 +6,8 @@ "license": "MIT", "scripts": { "start": "node ./index.js", - "postinstall": "cmake-js install", + "install": "cmake-js install", + "postinstall": "cmake-js compile", "configure": "cmake-js configure", "reconfigure": "cmake-js reconfigure", "build": "cmake-js build", From aa14223c684691839a965e7e76237b20d8d08d4c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 02:20:47 +0100 Subject: [PATCH 06/92] Happy consumer, doesn't even need cmake-js :) --- tests/api/hello/index.js | 9 ++++----- tests/api/hello_consumer/index.js | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/api/hello/index.js b/tests/api/hello/index.js index 0fa61d24..322ab6f9 100644 --- a/tests/api/hello/index.js +++ b/tests/api/hello/index.js @@ -1,5 +1,4 @@ -const demo = require('./build/lib/addon.node'); - -console.log(demo.hello()) - -module.exports = demo; +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const hello = require('./build/lib/addon.node'); +module.exports = hello; diff --git a/tests/api/hello_consumer/index.js b/tests/api/hello_consumer/index.js index 0c2a50dc..37e8c2b5 100644 --- a/tests/api/hello_consumer/index.js +++ b/tests/api/hello_consumer/index.js @@ -1,3 +1,8 @@ +// I am a third-party, or maybe fourth or fifth... +// I've never heard of 'cmake-js' before +// I just have NodeJS and CMake installed +// and everything "just works" for me! + const addon = require('@vendor/hello'); console.log(addon.hello()) From 93a5868d3dec8d6dbaeb21895560a56c833f4838 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 03:24:56 +0100 Subject: [PATCH 07/92] optional targets via CLI ;) --- lib/cMake.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/cMake.js b/lib/cMake.js index ce1921b4..73277dbd 100644 --- a/lib/cMake.js +++ b/lib/cMake.js @@ -196,7 +196,20 @@ class CMake { D.push({ CMAKE_MAKE_PROGRAM: this.toolset.makePath }) } - // CMakeJS.cmake api + // Provides a CLI switch for our targets (default = all 'ON', full API available). + // In this list, each target depends on the previous one; the next target requires + // that the previous one to be 'ON', because of the dependency chain. + // Most users probably won't interact with this, but some might only want + // for example the 'libnode_dev' stuff and nothing else... if they are brave :) + + // TODO: needs some cascading logic, ideally... + // but CMake should 'safely fail' if wrong settings anyway + D.push({ CMAKEJS_NODE_DEV: 'ON' }) // cmake-js::node-dev + D.push({ CMAKEJS_NODE_API: 'ON' }) // cmake-js::node-api + D.push({ CMAKEJS_NODE_ADDON_API: 'ON' }) // cmake-js::node-aaddon-pi + D.push({ CMAKEJS_CMAKEJS: 'ON' }) // cmake-js::cmake-js (required for 'cmakejs_create_napi_addon()') + + // CMakeJS.cmake api (could use an arg to toggle on/off of you want) D.push({ CMAKE_MODULE_PATH: path_to_api }) // Load NPM config From 2aae9c6e93d89044b365b64435f413f213a2660c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 03:55:14 +0100 Subject: [PATCH 08/92] better exe lookup? --- share/cmake/CMakeJS.cmake | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 6bde57a5..7b407605 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -99,25 +99,22 @@ if (NOT DEFINED CMAKE_JS_VERSION) DOC "cmake-js system executable binary" REQUIRED ) - if (NOT CMAKE_JS_EXECUTABLE) - message(FATAL_ERROR "cmake-js system installation not found! Please run 'npm -g install cmake-js@latest' and try again.") - return() + if(NOT CMAKE_JS_EXECUTABLE) + find_program(CMAKE_JS_EXECUTABLE + NAMES "cmake-js" "cmake-js.exe" + PATHS "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/bin" + DOC "cmake-js project-local npm package binary" + REQUIRED + ) + if (NOT CMAKE_JS_EXECUTABLE) + message(FATAL_ERROR "cmake-js not found! Please run 'npm install' and try again.") + return() + endif() endif() _cmakejs_normalize_path(CMAKE_JS_EXECUTABLE) string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_EXECUTABLE "${CMAKE_JS_EXECUTABLE}") - find_program(CMAKE_JS_NPM_PACKAGE - NAMES "cmake-js" "cmake-js.exe" - PATHS "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/bin" - DOC "cmake-js project-local npm package binary" - REQUIRED - ) - if (NOT CMAKE_JS_NPM_PACKAGE) - message(FATAL_ERROR "cmake-js project-local npm package not found! Please run 'npm install' and try again.") - return() - endif() - # Execute the CLI commands, and write their outputs into the cached vars # where the remaining build processes expect them to be... execute_process( From 6658e8965674cc4969d27cbf6b1a57cf6d913da3 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 04:24:30 +0100 Subject: [PATCH 09/92] Enhanced clarity of intent --- CMakeLists.txt | 8 ++++++++ share/cmake/CMakeJS.cmake | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1806bff..c2d4e3c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,8 @@ include ("${CMAKE_CURRENT_LIST_DIR}/share/cmake/CMakeJS.cmake") # We instead offer a simple test/demonstration of our API, and reserve the # CMakeJS vendor namespace in C++/CMake land. +# PROOF OF CONCEPT: + # If this is not a subproject... if (CMakeJS_IS_TOP_LEVEL # (and we're not building into the source tree) @@ -121,6 +123,12 @@ console.log(`Napi Version: ${demo.version()}`); endif() # offer a basic/messy CPack + # TODO: I don't think this is fully working because there is no built + # addon inside the binary tar outputs. + # It's distracting from the simplicity of the demo, and only here for + # more proof of concept. Remove it if you wish, it isn't part of the + # proposed API itself at all (same with CTest above). + # set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... set(CPACK_PACKAGE_VENDOR "cmake-js") set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 7b407605..7534162d 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -248,7 +248,7 @@ Provides :: NODE_API_HEADERS_DIR, where to find node_api.h, etc. - NODE_API_INC_FILES, the headers required to use Node Addon API. + NODE_API_INC_FILES, the headers required to use Node API. ]=============================================================================]# function(cmakejs_acquire_napi_c_files) @@ -275,7 +275,7 @@ function(cmakejs_acquire_napi_c_files) unset(NODE_API_INC_FILES) file(GLOB NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE FILEPATH "Node API Header files." FORCE) - source_group("Node Addon API (C)" FILES "${NODE_API_INC_FILES}") + source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") if(VERBOSE) message(STATUS "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") @@ -326,7 +326,7 @@ endfunction() #[=============================================================================[ Silently create an interface library (no output) with all Addon API dependencies -resolved, for Addon targets to link with. +resolved, for each feature that we offer; this is for Addon targets to link with. (This should contain most of cmake-js globally-required configuration) @@ -498,6 +498,38 @@ if(CMAKEJS_CMAKEJS) add_library(${name} SHARED) add_library(${name_alt}::${name} ALIAS ${name}) + # TODO: If we instead set up a var like 'CMAKEJS_LINK_LEVEL', + # it can carry an integer number corresponding to which + # dependency level the builder wants. The value of this + # integer can be determined somehow from the result of the + # 'CMakeDependentOption's at the top of this file. + # + # i.e. (psudeo code); + # + # if options = 0; set (CMAKEJS_LINK_LEVEL "cmake-js::node-dev") + # if options = 1; set (CMAKEJS_LINK_LEVEL "cmake-js::node-api") + # if options = 2; set (CMAKEJS_LINK_LEVEL "cmake-js::node-addon-api") + # if options = 3; set (CMAKEJS_LINK_LEVEL "cmake-js::cmake-js") + # + # target_link_libraries(${name} PRIVATE ${CMAKEJS_LINK_LEVEL}) + # + # Why? + # + # Because currently, our 'create_napi_addon()' depends on cmake-js::cmake-js, + # Which is why I had to wrap our nice custom functions inside of + # this 'CMAKEJS_CMAKEJS=TRUE' block, for now. + # + # If do we make our functions available at all times, we must also + # validate that all the possible configurations work (or fail safely, + # and with a prompt.) + # + # Testing (and supporting) the above could be exponentially complex. + # I think most people won't use the target toggles anyway, + # and those that do, won't have access to any broken/untested + # variations of our functions. + # + # Just my suggestion; do as you will :) + target_link_libraries(${name} PRIVATE cmake-js::cmake-js) set_property( From ccefa1a07388a84614f3b40aabcd66b712cdf87e Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 04:54:03 +0100 Subject: [PATCH 10/92] Enhanced clarity of intent --- share/cmake/CMakeJS.cmake | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 7534162d..57f00075 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -705,6 +705,33 @@ endforeach() #[=============================================================================[ Collect targets and allow CMake to provide them + +Builders working with CMake at any level know how fussy CMake is about stuff +like filepaths, and how to resolve your project's dependencies. Enough people +went "agh if CMake is gonna be so fussy about my project's filepaths, why can't +it just look after that stuff by itself? Why have I got to do this?" and CMake +went "ok then, do these new 'export()' and 'install()' functions and I'll sort it +all out myself, for you. I'll also sort it out for your users, and their users too!" + +DISCLAIMER: the names 'export()' and 'install()' are just old CMake parlance - +very misleading, at first - try to not think about 'installing' in the traditional +system-level sense, nobody does that until much later downstream from here... + +Earlier, we scooped up all the different header files, logically arranged them into +seperate 'targets' (with a little bit of inter-dependency management), and copied +them into the binary dir. In doing so, we effectively 'chopped off' their +absolute paths; they now 'exist' (temporarily) on a path that *we have not +defined yet*, which is CMAKE_BINARY_DIR. + +In using the BUILD_ and INSTALL_ interfaces, we told CMake how to relocate those +files as it pleases. CMake will move them around as it pleases, but no matter +where those files end up, they will *always* be at 'CMAKE_BINARY_DIR/include/dir', +as far as CMake cares; it will put those files anywhere it needs to, at any time, +*but* we (and our consumers' CMake) can depend on *always* finding them at +'CMAKE_BINARY_DIR/include/dir', no matter what anybody sets their CMAKE_BINARY_DIR +to be. + +It's not quite over yet, but the idea should be becoming clear now... ]=============================================================================]# export ( From 3fc2f144bcbc8f12ab1ebdec834ea25c6cd03c9b Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 05:22:42 +0100 Subject: [PATCH 11/92] suggestion on new CLI option --- share/cmake/CMakeJS.cmake | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 57f00075..59b87b94 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -519,9 +519,18 @@ if(CMAKEJS_CMAKEJS) # Which is why I had to wrap our nice custom functions inside of # this 'CMAKEJS_CMAKEJS=TRUE' block, for now. # - # If do we make our functions available at all times, we must also - # validate that all the possible configurations work (or fail safely, - # and with a prompt.) + # cmake-js cli users could then be offered a new flag for setting a + # preferred dependency level for their project, controlling the + # values on the JS side before being passed to the command line + # (default to 3 if not set): + # + # $ cmake-js configure --link-level=2 + # + # The above would provide dependency resolution up to cmake-js::node-adon-api level. + # + # If do we make our functions available at all times this way, + # we must also validate that all the possible configurations work + # (or fail safely, and with a prompt.) # # Testing (and supporting) the above could be exponentially complex. # I think most people won't use the target toggles anyway, From 701829f155534b6763d83f04fe96ba4b26bb482a Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 05:48:55 +0100 Subject: [PATCH 12/92] suggestion --- share/cmake/CMakeJS.cmake | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 59b87b94..c26f217f 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -145,6 +145,16 @@ if (NOT DEFINED CMAKE_JS_VERSION) string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_LIB "${CMAKE_JS_LIB}") # relocate... + file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") + # TODO: this next one is not really done correctly, because; + # - we absolutely should be recreating the whole filetree (including the named dirs and which headers go where) + # - it is possible to copy whole directories (perhaps one by one?) with cmake functions. + # - But, that way, we're not passing the globbed files into 'source_group', a function which gives excellent IDE support (and nothing else). + # - A long-winded notion involves setting up 'cmake-js::node-dev::v8' and other targets (cmake-js::node-dev could be made to depend on them) + # I avoided doing any of these things so far because this hasn't been critical, + # but I believe that consumers of cmake-js::node-dev (alone) might have intellisense + # issues currently, since the filesystem doesn't strictly resemble what it's supposed to. + # And overall it seems to be working enough for now :) file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/**/*.h") file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") unset(_CMAKE_JS_INC_FILES) From 66e3155ae68360da9bb672edf46c19464cb4791e Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 06:36:43 +0100 Subject: [PATCH 13/92] Fixed node-dev deps headers --- share/cmake/CMakeJS.cmake | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index c26f217f..91868823 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -144,21 +144,22 @@ if (NOT DEFINED CMAKE_JS_VERSION) string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_SRC "${CMAKE_JS_SRC}") string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_LIB "${CMAKE_JS_LIB}") - # relocate... + # relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") - # TODO: this next one is not really done correctly, because; - # - we absolutely should be recreating the whole filetree (including the named dirs and which headers go where) - # - it is possible to copy whole directories (perhaps one by one?) with cmake functions. - # - But, that way, we're not passing the globbed files into 'source_group', a function which gives excellent IDE support (and nothing else). - # - A long-winded notion involves setting up 'cmake-js::node-dev::v8' and other targets (cmake-js::node-dev could be made to depend on them) - # I avoided doing any of these things so far because this hasn't been critical, - # but I believe that consumers of cmake-js::node-dev (alone) might have intellisense - # issues currently, since the filesystem doesn't strictly resemble what it's supposed to. - # And overall it seems to be working enough for now :) - file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/**/*.h") file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") unset(_CMAKE_JS_INC_FILES) + set(_NODE_DEV_DEPS "") + list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) + foreach(_DEP IN LISTS _NODE_DEV_DEPS) + if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") + file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/*.h") + file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") + unset(_CMAKE_JS_INC_FILES) + endif() + endforeach() + unset(_NODE_DEV_DEPS) + # target include directories (as if 'node-dev' were an isolated CMake project...) set(CMAKE_JS_INC $ @@ -185,7 +186,8 @@ else () endif () set(CMAKE_JS_INC_FILES "") # prevent repetitive globbing on each run -file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**.h") +file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") +file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") source_group("cmake-js v${_version} Node ${NODE_VERSION}" FILES "${CMAKE_JS_INC_FILES}") # Log the vars to the console for sanity... From 10354e686ccec06b58f695b559cb6a50ed1b67e9 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 06:56:13 +0100 Subject: [PATCH 14/92] node deps now always in circuit (this needs close consideration...) --- share/cmake/CMakeJS.cmake | 69 +++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 91868823..e1e67728 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -144,38 +144,6 @@ if (NOT DEFINED CMAKE_JS_VERSION) string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_SRC "${CMAKE_JS_SRC}") string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_LIB "${CMAKE_JS_LIB}") - # relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) - file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") - file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") - unset(_CMAKE_JS_INC_FILES) - - set(_NODE_DEV_DEPS "") - list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) - foreach(_DEP IN LISTS _NODE_DEV_DEPS) - if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") - file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/*.h") - file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") - unset(_CMAKE_JS_INC_FILES) - endif() - endforeach() - unset(_NODE_DEV_DEPS) - - # target include directories (as if 'node-dev' were an isolated CMake project...) - set(CMAKE_JS_INC - $ - $ - ) - - set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE STRING "cmake-js include directory." FORCE) - set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE STRING "cmake-js source file." FORCE) - set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE STRING "cmake-js lib file." FORCE) - - # TODO: At this point, some warnings may occur re: the below (still investigating); - # Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS. - #set (NAPI_CPP_EXCEPTIONS TRUE CACHE STRING "Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS") - add_definitions(-DNAPI_CPP_EXCEPTIONS) # Also needs /EHsc - # add_definitions(-DNAPI_DISABLE_CPP_EXCEPTIONS) - else () # ... we already are calling via npm/cmake-js, so we should already have all the vars we need! @@ -185,6 +153,43 @@ else () endif () +# 'always-on' codeblock; we provide the following blob, no matter the config. + +# relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) +file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") +file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") +unset(_CMAKE_JS_INC_FILES) + +set(_NODE_DEV_DEPS "") +list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) +foreach(_DEP IN LISTS _NODE_DEV_DEPS) + if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") + file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/*.h") + file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") + unset(_CMAKE_JS_INC_FILES) + endif() +endforeach() +unset(_NODE_DEV_DEPS) + +# relocate... (this is crucial for 'install()' to work on user's addons) +unset(CMAKE_JS_INC) + +# target include directories (as if 'node-dev' were an isolated CMake project...) +set(CMAKE_JS_INC + $ + $ +) + +set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE STRING "cmake-js include directory." FORCE) +set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE STRING "cmake-js source file." FORCE) +set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE STRING "cmake-js lib file." FORCE) + +# TODO: At this point, some warnings may occur re: the below (still investigating); +# Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS. +#set (NAPI_CPP_EXCEPTIONS TRUE CACHE STRING "Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS") +add_definitions(-DNAPI_CPP_EXCEPTIONS) # Also needs /EHsc +# add_definitions(-DNAPI_DISABLE_CPP_EXCEPTIONS) + set(CMAKE_JS_INC_FILES "") # prevent repetitive globbing on each run file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") From 68096676369c6183f7f29add08c0d6ae3ad19bdf Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 07:48:20 +0100 Subject: [PATCH 15/92] install our CMake package - fixed --- share/cmake/CMakeJS.cmake | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index e1e67728..f58ca955 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -790,19 +790,6 @@ write_basic_package_version_file ( COMPATIBILITY AnyNewerVersion ) -# copy headers (and definitions?) to build dir for distribution -if(CMAKEJS_NODE_DEV) - install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "include/node") -endif() - -if(CMAKEJS_NODE_API) - install(FILES ${NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") -endif() - -if(CMAKEJS_NODE_ADDON_API) - install(FILES ${NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") -endif() - # This whole block that follows, and the last changes I made to this file (re: 'file/directory reolcation') # is all predicated on the idea that our consumers want to control certain vars themselves: # @@ -831,6 +818,23 @@ endif() include(GNUInstallDirs) +# copy headers (and definitions?) to build dir for distribution +if(CMAKEJS_NODE_DEV) + install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") +endif() + +if(CMAKEJS_NODE_API) + install(FILES ${NODE_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-api-headers") +endif() + +if(CMAKEJS_NODE_ADDON_API) + install(FILES ${NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-addon-api") +endif() + +if(CMAKEJS_CMAKEJS) + install(FILES ${_CMAKEJS_SCRIPT} DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CMakeJS") +endif() + # configure a 'CMakeJSTargets' export file for install install(TARGETS ${CMAKEJS_TARGETS} EXPORT CMakeJSTargets From 8efd99982b5b1984d622e4c29632f07d83050dbe Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 07:59:53 +0100 Subject: [PATCH 16/92] as above --- share/cmake/CMakeJS.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index f58ca955..6be22686 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -789,6 +789,8 @@ write_basic_package_version_file ( VERSION ${_version} COMPATIBILITY AnyNewerVersion ) +# pass our module along +file(COPY "${_CMAKEJS_SCRIPT}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/share/cmake") # This whole block that follows, and the last changes I made to this file (re: 'file/directory reolcation') # is all predicated on the idea that our consumers want to control certain vars themselves: From bafefd97881b7e33d5ec8cfffffaf0988750af0d Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 11:43:36 +0100 Subject: [PATCH 17/92] Improved file relocatability --- share/cmake/CMakeJS.cmake | 160 ++++++++++++++++++++++++++++++-------- 1 file changed, 126 insertions(+), 34 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 6be22686..20830328 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -172,7 +172,7 @@ endforeach() unset(_NODE_DEV_DEPS) # relocate... (this is crucial for 'install()' to work on user's addons) -unset(CMAKE_JS_INC) +set(CMAKE_JS_INC "") # target include directories (as if 'node-dev' were an isolated CMake project...) set(CMAKE_JS_INC @@ -180,19 +180,14 @@ set(CMAKE_JS_INC $ ) -set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE STRING "cmake-js include directory." FORCE) -set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE STRING "cmake-js source file." FORCE) -set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE STRING "cmake-js lib file." FORCE) - -# TODO: At this point, some warnings may occur re: the below (still investigating); -# Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS. -#set (NAPI_CPP_EXCEPTIONS TRUE CACHE STRING "Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS") -add_definitions(-DNAPI_CPP_EXCEPTIONS) # Also needs /EHsc -# add_definitions(-DNAPI_DISABLE_CPP_EXCEPTIONS) +set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE PATH "cmake-js include directory." FORCE) +set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE FILEPATH "cmake-js source file." FORCE) +set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE FILEPATH "cmake-js lib file." FORCE) set(CMAKE_JS_INC_FILES "") # prevent repetitive globbing on each run file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") +set(CMAKE_JS_INC_FILES "${CMAKE_JS_INC_FILES}" CACHE STRING "" FORCE) source_group("cmake-js v${_version} Node ${NODE_VERSION}" FILES "${CMAKE_JS_INC_FILES}") # Log the vars to the console for sanity... @@ -278,6 +273,7 @@ function(cmakejs_acquire_napi_c_files) string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") # relocate... + set(_NODE_API_INC_FILES "") file(GLOB _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") unset(_NODE_API_INC_FILES) @@ -287,9 +283,9 @@ function(cmakejs_acquire_napi_c_files) $ $ ) - set(NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}" CACHE PATH "Node API Headers directory." FORCE) + set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} CACHE PATH "Node API Headers directory." FORCE) - unset(NODE_API_INC_FILES) + set(NODE_API_INC_FILES "") file(GLOB NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE FILEPATH "Node API Header files." FORCE) source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") @@ -319,8 +315,10 @@ function(cmakejs_acquire_napi_cpp_files) string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}") # relocate... + set(_NODE_ADDON_API_INC_FILES "") file(GLOB _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") + message(STATUS "_NODE_ADDON_API_INC_FILES = ${_NODE_ADDON_API_INC_FILES}") unset(_NODE_ADDON_API_INC_FILES) # target include directories (as if 'node-addon-api' were an isolated CMake project...) @@ -328,11 +326,12 @@ function(cmakejs_acquire_napi_cpp_files) $ $ ) - set(NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}" CACHE PATH "Node Addon API Headers directory." FORCE) + set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} PARENT_SCOPE) - unset(NODE_ADDON_API_INC_FILES) + set(NODE_ADDON_API_INC_FILES "") file(GLOB NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE FILEPATH "Node Addon API Header files." FORCE) + set(NODE_ADDON_API_INC_FILES ${NODE_ADDON_API_INC_FILES} PARENT_SCOPE) + # set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE STRING "Node Addon API Header files." FORCE) source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") if(VERBOSE) @@ -372,8 +371,37 @@ if(CMAKEJS_NODE_DEV) target_sources (node-dev INTERFACE "${CMAKE_JS_SRC}") target_sources (node-dev INTERFACE "${CMAKE_JS_INC_FILES}") target_link_libraries (node-dev INTERFACE "${CMAKE_JS_LIB}") - list(APPEND CMAKEJS_TARGETS node-dev) set_target_properties (node-dev PROPERTIES VERSION ${NODE_VERSION}) + + set(NODE_DEV_FILES "") + list(APPEND NODE_DEV_FILES + # NodeJS core + "node_buffer.h" + "node_object_wrap.h" + "node_version.h" + "node.h" + # uv + "uv.h" + # v8 + # zlib + "zconf.h" + "zlib.h" + ) + + foreach(FILE IN LISTS NODE_DEV_FILES) + target_sources(node-dev INTERFACE + FILE_SET node_dev_INTERFACE_HEADERS + TYPE HEADERS + BASE_DIRS + $ + $ + FILES + $ + $ + ) + endforeach() + + list(APPEND CMAKEJS_TARGETS node-dev) endif() if(CMAKEJS_NODE_API) @@ -381,23 +409,44 @@ if(CMAKEJS_NODE_API) # Acquire if needed... if(NOT DEFINED NODE_API_HEADERS_DIR) cmakejs_acquire_napi_c_files() + set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} CACHE PATH "Node API Headers directory." FORCE) message(DEBUG "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") if(NOT DEFINED NODE_API_INC_FILES) file(GLOB NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE FILEPATH "Node API Header files." FORCE) - source_group("Node Addon API (C)" FILES "${NODE_API_INC_FILES}") + source_group("Node Addon API (C)" FILES ${NODE_API_INC_FILES}) endif() + set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE STRING "Node API Header files." FORCE) endif() # Node API (C) - requires NodeJS system installation headers # cmake-js::node-api add_library (node-api INTERFACE) add_library (cmake-js::node-api ALIAS node-api) - target_include_directories (node-api INTERFACE "${NODE_API_HEADERS_DIR}") - target_sources (node-api INTERFACE "${NODE_API_INC_FILES}") + target_include_directories (node-api INTERFACE ${NODE_API_HEADERS_DIR}) target_link_libraries (node-api INTERFACE cmake-js::node-dev) set_target_properties (node-api PROPERTIES VERSION 6.1.0) - list(APPEND CMAKEJS_TARGETS node-api) + + set(NODE_API_FILES "") + list(APPEND NODE_API_FILES + "node_api.h" + "node_api_types.h" + "js_native_api.h" + "js_native_api_types.h" + ) + + foreach(FILE IN LISTS NODE_API_FILES) + target_sources(node-api INTERFACE + FILE_SET node_api_INTERFACE_HEADERS + TYPE HEADERS + BASE_DIRS + $ + $ + FILES + $ + $ + ) + endforeach() + list(APPEND CMAKEJS_TARGETS node-api) endif() if(CMAKEJS_NODE_ADDON_API) @@ -405,22 +454,43 @@ if(CMAKEJS_NODE_ADDON_API) # Acquire if needed... if(NOT DEFINED NODE_ADDON_API_DIR) cmakejs_acquire_napi_cpp_files() + set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} CACHE PATH "Node Addon API Headers directory." FORCE) message(DEBUG "NODE_ADDON_API_DIR: ${NODE_ADDON_API_DIR}") if(NOT DEFINED NODE_ADDON_API_INC_FILES) file(GLOB NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE FILEPATH "Node Addon API Header files." FORCE) source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") endif() + set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE STRING "Node Addon API Header files." FORCE) endif() # Node Addon API (C++) - requires Node API (C) # cmake-js::node-addon-api add_library (node-addon-api INTERFACE) add_library (cmake-js::node-addon-api ALIAS node-addon-api) - target_include_directories (node-addon-api INTERFACE "${NODE_ADDON_API_DIR}") - target_sources (node-addon-api INTERFACE "${NODE_ADDON_API_INC_FILES}") + target_include_directories (node-addon-api INTERFACE ${NODE_ADDON_API_DIR}) target_link_libraries (node-addon-api INTERFACE cmake-js::node-api) set_target_properties (node-addon-api PROPERTIES VERSION 1.1.0) + + set(NODE_ADDON_API_FILES "") + list(APPEND NODE_ADDON_API_FILES + "napi-inl.deprecated.h" + "napi-inl.h" + "napi.h" + ) + + foreach(FILE IN LISTS NODE_ADDON_API_FILES) + target_sources(node-addon-api INTERFACE + FILE_SET node_addon_api_INTERFACE_HEADERS + TYPE HEADERS + BASE_DIRS + $ + $ + FILES + $ + $ + ) + endforeach() + list(APPEND CMAKEJS_TARGETS node-addon-api) endif() @@ -434,7 +504,7 @@ if(CMAKEJS_CMAKEJS) target_compile_features (cmake-js INTERFACE cxx_nullptr) # Signal a basic C++11 feature to require C++11. set_target_properties (cmake-js PROPERTIES VERSION 7.3.3) set_target_properties (cmake-js PROPERTIES SOVERSION 7) - set_target_properties (cmake-js PROPERTIES COMPATIBLE_INTERFACE_STRING cmake-js_MAJOR_VERSION) + set_target_properties (cmake-js PROPERTIES COMPATIBLE_INTERFACE_STRING CMakeJS_MAJOR_VERSION) # Generate definitions if(MSVC) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." FORCE) @@ -823,29 +893,51 @@ include(GNUInstallDirs) # copy headers (and definitions?) to build dir for distribution if(CMAKEJS_NODE_DEV) install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") + install(TARGETS node-dev + EXPORT CMakeJSTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + FILE_SET node_dev_INTERFACE_HEADERS + ) endif() if(CMAKEJS_NODE_API) install(FILES ${NODE_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-api-headers") + install(TARGETS node-api + EXPORT CMakeJSTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + FILE_SET node_api_INTERFACE_HEADERS + ) endif() if(CMAKEJS_NODE_ADDON_API) install(FILES ${NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-addon-api") + install(TARGETS node-addon-api + EXPORT CMakeJSTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + FILE_SET node_addon_api_INTERFACE_HEADERS + ) endif() if(CMAKEJS_CMAKEJS) install(FILES ${_CMAKEJS_SCRIPT} DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CMakeJS") + install(TARGETS cmake-js + EXPORT CMakeJSTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + ) endif() -# configure a 'CMakeJSTargets' export file for install -install(TARGETS ${CMAKEJS_TARGETS} - EXPORT CMakeJSTargets - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" -) - # install config files install(FILES "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/CMakeJSConfig.cmake" From 93d07f2a1fc36284b8cff8d6f1a66549d45349f7 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Fri, 2 Feb 2024 11:44:18 +0100 Subject: [PATCH 18/92] basic exceptions handling --- share/cmake/CMakeJS.cmake | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 20830328..829f0828 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -535,7 +535,7 @@ if(CMAKEJS_CMAKEJS) endif() set(options) - set(args ALIAS NAMESPACE NAPI_VERSION) + set(args ALIAS NAMESPACE NAPI_VERSION EXCEPTIONS) set(list_args) cmake_parse_arguments(ARG "${options}" "${args}" "${list_args}" "${ARGN}") @@ -576,6 +576,22 @@ if(CMAKEJS_CMAKEJS) set(name_alt "${ARG_NAMESPACE}") endif() + if(NOT ARG_EXCEPTIONS) + set(ARG_EXCEPTIONS "MAYBE") # YES, NO, or MAYBE... + endif() + + if((NOT DEFINED NAPI_CPP_EXCEPTIONS) OR (NOT DEFINED NAPI_DISABLE_CPP_EXCEPTIONS) OR (NOT DEFINED NAPI_CPP_EXCEPTIONS_MAYBE)) + + if(ARG_EXCEPTIONS STREQUAL "YES") + set(_NAPI_GLOBAL_EXCEPTIONS_POLICY "NAPI_CPP_EXCEPTIONS") + elseif(ARG_EXCEPTIONS STREQUAL "NO") + set(_NAPI_GLOBAL_EXCEPTIONS_POLICY "NAPI_DISABLE_CPP_EXCEPTIONS") + else() + set(_NAPI_GLOBAL_EXCEPTIONS_POLICY "NAPI_CPP_EXCEPTIONS_MAYBE") + endif() + + endif() + if(VERBOSE) message(STATUS "Configuring Napi Addon: ${name}") endif() @@ -664,8 +680,12 @@ if(CMAKEJS_CMAKEJS) PUBLIC # These definitions are shared with anything that links to this addon "NAPI_VERSION=${ARG_NAPI_VERSION}" "BUILDING_NODE_EXTENSION" + "${_NAPI_GLOBAL_EXCEPTIONS_POLICY}" ) + # Global exceptions policy + unset(_NAPI_GLOBAL_EXCEPTIONS_POLICY) + endfunction() #[=============================================================================[ From 0193e9b697d23ab2ffeaad59fb2ab1338b22950f Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 2 Feb 2024 22:10:07 +0100 Subject: [PATCH 19/92] Provide our undefined vars in the shared CMake config file --- share/cmake/CMakeJS.cmake | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 6be22686..05d44b07 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -768,12 +768,30 @@ export ( include (CMakePackageConfigHelpers) -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeJSConfig.cmake.in" [==[ +file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeJSConfig.cmake.in" [==[ @PACKAGE_INIT@ -include(${CMAKE_CURRENT_LIST_DIR}/CMakeJSTargets.cmake) +include (${CMAKE_CURRENT_LIST_DIR}/CMakeJSTargets.cmake) + +check_required_components (cmake-js) + +set (CMAKE_JS_SRC "@CMAKE_JS_SRC@") +set (CMAKE_JS_INC "@CMAKE_JS_INC@") +set (CMAKE_JS_LIB "@CMAKE_JS_LIB@") +set (CMAKE_JS_VERSION "@CMAKE_JS_VERSION@") +set (CMAKE_JS_EXECUTABLE "@CMAKE_JS_EXECUTABLE@") +set (CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") + +if (CMAKEJS_NODE_API) + set (NODE_API_HEADERS_DIR "@NODE_API_HEADERS_DIR@") + set (NODE_API_INC_FILES "@NODE_API_INC_FILES@") +endif() + +if (CMAKE_JS_NODE_ADDON_API) + set (NODE_ADDON_API_DIR "@NODE_ADDON_API_DIR@") + set (NODE_ADDON_API_INC_FILES "@NODE_ADDON_API_INC_FILES@") +endif () -check_required_components(cmake-js) ]==]) # create cmake config file From eae781abf9ecb31c3002e600ec7e1f79b27c9adf Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 01:05:58 +0100 Subject: [PATCH 20/92] Safety incase file not found --- share/cmake/CMakeJS.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 829f0828..86128dc2 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -389,6 +389,7 @@ if(CMAKEJS_NODE_DEV) ) foreach(FILE IN LISTS NODE_DEV_FILES) + if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node/${FILE}") target_sources(node-dev INTERFACE FILE_SET node_dev_INTERFACE_HEADERS TYPE HEADERS @@ -399,6 +400,7 @@ if(CMAKEJS_NODE_DEV) $ $ ) + endif() endforeach() list(APPEND CMAKEJS_TARGETS node-dev) From fae3c18a076f0a42f942fa05c95ee8bee7976d06 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 01:18:23 +0100 Subject: [PATCH 21/92] Completed v8 header fileset for node-dev --- share/cmake/CMakeJS.cmake | 51 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 86128dc2..dedc0512 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -383,6 +383,54 @@ if(CMAKEJS_NODE_DEV) # uv "uv.h" # v8 + "v8-array-buffer.h" + "v8-callbacks.h" + "v8-container.h" + "v8-context.h" + "v8-data.h" + "v8-date.h" + "v8-debug.h" + "v8-embedder-heap.h" + "v8-embedder-state-scope.h" + "v8-exception.h" + "v8-extension.h" + "v8-forward.h" + "v8-function-callback.h" + "v8-function.h" + "v8-initialization.h" + "v8-internal.h" + "v8-isolate.h" + "v8-json.h" + "v8-local-handle.h" + "v8-locker.h" + "v8-maybe.h" + "v8-memory-span.h" + "v8-message.h" + "v8-microtask-queue.h" + "v8-microtask.h" + "v8-object.h" + "v8-persistent-handle.h" + "v8-platform.h" + "v8-primitive-object.h" + "v8-primitive.h" + "v8-profiler.h" + "v8-promise.h" + "v8-proxy.h" + "v8-regexp.h" + "v8-script.h" + "v8-snapshot.h" + "v8-statistics.h" + "v8-template.h" + "v8-traced-handle.h" + "v8-typed-array.h" + "v8-unwinder.h" + "v8-value-serializer.h" + "v8-value.h" + "v8-version.h" + "v8-wasm.h" + "v8-weak-callback.h" + "v8.h" + "v8-config.h" # zlib "zconf.h" "zlib.h" @@ -390,7 +438,8 @@ if(CMAKEJS_NODE_DEV) foreach(FILE IN LISTS NODE_DEV_FILES) if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node/${FILE}") - target_sources(node-dev INTERFACE + message(STATUS "Found NodeJS developer header: ${FILE}") + target_sources(node-dev INTERFACE FILE_SET node_dev_INTERFACE_HEADERS TYPE HEADERS BASE_DIRS From f39c38acf6c6bea42ca96291608e40a1f34c7e0b Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 02:20:59 +0100 Subject: [PATCH 22/92] Revised my suggestion for new CLI arg --- lib/cMake.js | 16 +++-- share/cmake/CMakeJS.cmake | 121 ++++++++++++++++++++++---------------- 2 files changed, 83 insertions(+), 54 deletions(-) diff --git a/lib/cMake.js b/lib/cMake.js index 73277dbd..e1e0f4be 100644 --- a/lib/cMake.js +++ b/lib/cMake.js @@ -204,10 +204,18 @@ class CMake { // TODO: needs some cascading logic, ideally... // but CMake should 'safely fail' if wrong settings anyway - D.push({ CMAKEJS_NODE_DEV: 'ON' }) // cmake-js::node-dev - D.push({ CMAKEJS_NODE_API: 'ON' }) // cmake-js::node-api - D.push({ CMAKEJS_NODE_ADDON_API: 'ON' }) // cmake-js::node-aaddon-pi - D.push({ CMAKEJS_CMAKEJS: 'ON' }) // cmake-js::cmake-js (required for 'cmakejs_create_napi_addon()') + // We should not be exposing all four options at a time, + // because that could lead tom coneone illogically de-activating + // say, node-api, while trying to build with node-addon-api (which isn't + // how the addon API works of course). + // Thus, I propose offering a new CLI arg for something like '--link-level=2', + // which would pick the third entry of our (zero-indexed) list below. + // Default value would be 3, providing cmake-js::cmake-js (our full API) + // as a silent default. + D.push({ CMAKEJS_USING_NODE_DEV: 'ON' }) // cmake-js::node-dev + D.push({ CMAKEJS_USING_NODE_API: 'ON' }) // cmake-js::node-api + D.push({ CMAKEJS_USING_NODE_ADDON_API: 'ON' }) // cmake-js::node-addon-pi + D.push({ CMAKEJS_USING_CMAKEJS: 'ON' }) // cmake-js::cmake-js (required for 'cmakejs_create_napi_addon()') // CMakeJS.cmake api (could use an arg to toggle on/off of you want) D.push({ CMAKE_MODULE_PATH: path_to_api }) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index ecf5e26a..6dd605d6 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -41,10 +41,10 @@ VCPKG_FEATURE_FLAGS or by passing for example '-DCMAKE_NODE_API:BOOL=FALSE' set (CMAKEJS_TARGETS "") include(CMakeDependentOption) -option (CMAKEJS_NODE_DEV "Supply cmake-js::node-dev target for linkage" ON) -cmake_dependent_option(CMAKEJS_NODE_API "Supply cmake-js::node-api target for linkage" ON CMAKEJS_NODE_DEV OFF) -cmake_dependent_option(CMAKEJS_NODE_ADDON_API "Supply cmake-js::node-addon-api target for linkage" ON CMAKEJS_NODE_API OFF) -cmake_dependent_option(CMAKEJS_CMAKEJS "Supply cmake-js::cmake-js target for linkage" ON CMAKEJS_NODE_ADDON_API OFF) +option (CMAKEJS_USING_NODE_DEV "Supply cmake-js::node-dev target for linkage" ON) +cmake_dependent_option(CMAKEJS_USING_NODE_API "Supply cmake-js::node-api target for linkage" ON CMAKEJS_USING_NODE_DEV OFF) +cmake_dependent_option(CMAKEJS_USING_NODE_ADDON_API "Supply cmake-js::node-addon-api target for linkage" ON CMAKEJS_USING_NODE_API OFF) +cmake_dependent_option(CMAKEJS_USING_CMAKEJS "Supply cmake-js::cmake-js target for linkage" ON CMAKEJS_USING_NODE_ADDON_API OFF) #[=============================================================================[ Internal helper (borrowed from CMakeRC). @@ -156,7 +156,7 @@ endif () # 'always-on' codeblock; we provide the following blob, no matter the config. # relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) -file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") +file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") unset(_CMAKE_JS_INC_FILES) @@ -164,7 +164,7 @@ set(_NODE_DEV_DEPS "") list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) foreach(_DEP IN LISTS _NODE_DEV_DEPS) if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") - file(GLOB _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/*.h") + file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/*.h") file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") unset(_CMAKE_JS_INC_FILES) endif() @@ -185,8 +185,8 @@ set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE FILEPATH "cmake-js source file." set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE FILEPATH "cmake-js lib file." FORCE) set(CMAKE_JS_INC_FILES "") # prevent repetitive globbing on each run -file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") -file(GLOB CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") +file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") +file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") set(CMAKE_JS_INC_FILES "${CMAKE_JS_INC_FILES}" CACHE STRING "" FORCE) source_group("cmake-js v${_version} Node ${NODE_VERSION}" FILES "${CMAKE_JS_INC_FILES}") @@ -274,7 +274,7 @@ function(cmakejs_acquire_napi_c_files) # relocate... set(_NODE_API_INC_FILES "") - file(GLOB _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") unset(_NODE_API_INC_FILES) @@ -286,7 +286,7 @@ function(cmakejs_acquire_napi_c_files) set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} CACHE PATH "Node API Headers directory." FORCE) set(NODE_API_INC_FILES "") - file(GLOB NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE FILEPATH "Node API Header files." FORCE) source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") @@ -316,9 +316,8 @@ function(cmakejs_acquire_napi_cpp_files) # relocate... set(_NODE_ADDON_API_INC_FILES "") - file(GLOB _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") - message(STATUS "_NODE_ADDON_API_INC_FILES = ${_NODE_ADDON_API_INC_FILES}") unset(_NODE_ADDON_API_INC_FILES) # target include directories (as if 'node-addon-api' were an isolated CMake project...) @@ -329,7 +328,7 @@ function(cmakejs_acquire_napi_cpp_files) set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} PARENT_SCOPE) set(NODE_ADDON_API_INC_FILES "") - file(GLOB NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + file(GLOB_RECURSE NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") set(NODE_ADDON_API_INC_FILES ${NODE_ADDON_API_INC_FILES} PARENT_SCOPE) # set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE STRING "Node Addon API Header files." FORCE) source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") @@ -354,7 +353,7 @@ cmake-js::node-addon-api cmake-js::cmake-js ]=============================================================================]# -if(CMAKEJS_NODE_DEV) +if(CMAKEJS_USING_NODE_DEV) # acquire if needed... if(NOT DEFINED NODE_EXECUTABLE) @@ -380,9 +379,15 @@ if(CMAKEJS_NODE_DEV) "node_object_wrap.h" "node_version.h" "node.h" + # NodeJS addon + "node_api.h" + "node_api_types.h" + "js_native_api.h" + "js_native_api_types.h" # uv "uv.h" # v8 + "v8config.h" "v8-array-buffer.h" "v8-callbacks.h" "v8-container.h" @@ -424,10 +429,15 @@ if(CMAKEJS_NODE_DEV) "v8-traced-handle.h" "v8-typed-array.h" "v8-unwinder.h" + "v8-util.h" + "v8-value-serializer-version.h" "v8-value-serializer.h" "v8-value.h" "v8-version.h" + "v8-version-string.h" "v8-wasm.h" + "v8-wasm-trap-handler-posix.h" + "v8-wasm-trap-handler-win.h" "v8-weak-callback.h" "v8.h" "v8-config.h" @@ -438,7 +448,7 @@ if(CMAKEJS_NODE_DEV) foreach(FILE IN LISTS NODE_DEV_FILES) if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node/${FILE}") - message(STATUS "Found NodeJS developer header: ${FILE}") + message(DEBUG "Found NodeJS developer header: ${FILE}") target_sources(node-dev INTERFACE FILE_SET node_dev_INTERFACE_HEADERS TYPE HEADERS @@ -455,7 +465,7 @@ if(CMAKEJS_NODE_DEV) list(APPEND CMAKEJS_TARGETS node-dev) endif() -if(CMAKEJS_NODE_API) +if(CMAKEJS_USING_NODE_API) # Acquire if needed... if(NOT DEFINED NODE_API_HEADERS_DIR) @@ -463,7 +473,7 @@ if(CMAKEJS_NODE_API) set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} CACHE PATH "Node API Headers directory." FORCE) message(DEBUG "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") if(NOT DEFINED NODE_API_INC_FILES) - file(GLOB NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") source_group("Node Addon API (C)" FILES ${NODE_API_INC_FILES}) endif() set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE STRING "Node API Header files." FORCE) @@ -486,21 +496,24 @@ if(CMAKEJS_NODE_API) ) foreach(FILE IN LISTS NODE_API_FILES) - target_sources(node-api INTERFACE - FILE_SET node_api_INTERFACE_HEADERS - TYPE HEADERS - BASE_DIRS - $ - $ - FILES - $ - $ - ) + if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers/${FILE}") + message(DEBUG "Found Napi API C header: ${FILE}") + target_sources(node-api INTERFACE + FILE_SET node_api_INTERFACE_HEADERS + TYPE HEADERS + BASE_DIRS + $ + $ + FILES + $ + $ + ) + endif() endforeach() list(APPEND CMAKEJS_TARGETS node-api) endif() -if(CMAKEJS_NODE_ADDON_API) +if(CMAKEJS_USING_NODE_ADDON_API) # Acquire if needed... if(NOT DEFINED NODE_ADDON_API_DIR) @@ -508,7 +521,7 @@ if(CMAKEJS_NODE_ADDON_API) set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} CACHE PATH "Node Addon API Headers directory." FORCE) message(DEBUG "NODE_ADDON_API_DIR: ${NODE_ADDON_API_DIR}") if(NOT DEFINED NODE_ADDON_API_INC_FILES) - file(GLOB NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + file(GLOB_RECURSE NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") endif() set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE STRING "Node Addon API Header files." FORCE) @@ -530,22 +543,25 @@ if(CMAKEJS_NODE_ADDON_API) ) foreach(FILE IN LISTS NODE_ADDON_API_FILES) - target_sources(node-addon-api INTERFACE - FILE_SET node_addon_api_INTERFACE_HEADERS - TYPE HEADERS - BASE_DIRS - $ - $ - FILES - $ - $ - ) + if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api/${FILE}") + message(DEBUG "Found Napi Addon API C++ header: ${FILE}") + target_sources(node-addon-api INTERFACE + FILE_SET node_addon_api_INTERFACE_HEADERS + TYPE HEADERS + BASE_DIRS + $ + $ + FILES + $ + $ + ) + endif() endforeach() list(APPEND CMAKEJS_TARGETS node-addon-api) endif() -if(CMAKEJS_CMAKEJS) +if(CMAKEJS_USING_CMAKEJS) # CMakeJS API - requires Node Addon API (C++), resolves the full Napi Addon dependency chain # cmake-js::cmake-js add_library (cmake-js INTERFACE) @@ -671,7 +687,7 @@ if(CMAKEJS_CMAKEJS) # # Because currently, our 'create_napi_addon()' depends on cmake-js::cmake-js, # Which is why I had to wrap our nice custom functions inside of - # this 'CMAKEJS_CMAKEJS=TRUE' block, for now. + # this 'CMAKEJS_USING_CMAKEJS=TRUE' block, for now. # # cmake-js cli users could then be offered a new flag for setting a # preferred dependency level for their project, controlling the @@ -712,6 +728,7 @@ if(CMAKEJS_CMAKEJS) RUNTIME_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/bin" # # Conventional C++-style debug settings might be useful to have... + # Getting Javascript bindings to grep different paths is tricky, though! # LIBRARY_OUTPUT_NAME_DEBUG "d${name}" # ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKEJS_BINARY_DIR}/lib/Debug" # LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKEJS_BINARY_DIR}/lib/Debug" @@ -916,21 +933,25 @@ include (${CMAKE_CURRENT_LIST_DIR}/CMakeJSTargets.cmake) check_required_components (cmake-js) +# Not sure if this is needed... set (CMAKE_JS_SRC "@CMAKE_JS_SRC@") set (CMAKE_JS_INC "@CMAKE_JS_INC@") set (CMAKE_JS_LIB "@CMAKE_JS_LIB@") set (CMAKE_JS_VERSION "@CMAKE_JS_VERSION@") set (CMAKE_JS_EXECUTABLE "@CMAKE_JS_EXECUTABLE@") -set (CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") +set (CMAKE_JS_INC_FILES "") +list (APPEND CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") if (CMAKEJS_NODE_API) - set (NODE_API_HEADERS_DIR "@NODE_API_HEADERS_DIR@") - set (NODE_API_INC_FILES "@NODE_API_INC_FILES@") + set (NODE_API_HEADERS_DIR "@NODE_API_HEADERS_DIR@") + set (NODE_API_INC_FILES "") + list (APPEND NODE_API_INC_FILES "@NODE_API_INC_FILES@") endif() if (CMAKE_JS_NODE_ADDON_API) - set (NODE_ADDON_API_DIR "@NODE_ADDON_API_DIR@") - set (NODE_ADDON_API_INC_FILES "@NODE_ADDON_API_INC_FILES@") + set (NODE_ADDON_API_DIR "@NODE_ADDON_API_DIR@") + set (NODE_ADDON_API_INC_FILES "") + list (APPEND NODE_ADDON_API_INC_FILES "@NODE_ADDON_API_INC_FILES@") endif () ]==]) @@ -980,7 +1001,7 @@ file(COPY "${_CMAKEJS_SCRIPT}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/share/cm include(GNUInstallDirs) # copy headers (and definitions?) to build dir for distribution -if(CMAKEJS_NODE_DEV) +if(CMAKEJS_USING_NODE_DEV) install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") install(TARGETS node-dev EXPORT CMakeJSTargets @@ -992,7 +1013,7 @@ if(CMAKEJS_NODE_DEV) ) endif() -if(CMAKEJS_NODE_API) +if(CMAKEJS_USING_NODE_API) install(FILES ${NODE_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-api-headers") install(TARGETS node-api EXPORT CMakeJSTargets @@ -1004,7 +1025,7 @@ if(CMAKEJS_NODE_API) ) endif() -if(CMAKEJS_NODE_ADDON_API) +if(CMAKEJS_USING_NODE_ADDON_API) install(FILES ${NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-addon-api") install(TARGETS node-addon-api EXPORT CMakeJSTargets @@ -1016,7 +1037,7 @@ if(CMAKEJS_NODE_ADDON_API) ) endif() -if(CMAKEJS_CMAKEJS) +if(CMAKEJS_USING_CMAKEJS) install(FILES ${_CMAKEJS_SCRIPT} DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CMakeJS") install(TARGETS cmake-js EXPORT CMakeJSTargets From 118766a7fe163bd6b9191b6aefa8ea783198c87c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 02:32:52 +0100 Subject: [PATCH 23/92] Each target is now installing nicely --- share/cmake/CMakeJS.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 6dd605d6..12ffc18f 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -505,8 +505,8 @@ if(CMAKEJS_USING_NODE_API) $ $ FILES - $ - $ + $ + $ ) endif() endforeach() From 00fe1f93bd6f2a7343b0833f5136dd446594191c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 04:34:49 +0100 Subject: [PATCH 24/92] More TODO's regarding NAPI_EXCEPTIONS... ideas needed! --- lib/cMake.js | 2 +- share/cmake/CMakeJS.cmake | 75 ++++++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/lib/cMake.js b/lib/cMake.js index e1e0f4be..6e0a6b1a 100644 --- a/lib/cMake.js +++ b/lib/cMake.js @@ -205,7 +205,7 @@ class CMake { // TODO: needs some cascading logic, ideally... // but CMake should 'safely fail' if wrong settings anyway // We should not be exposing all four options at a time, - // because that could lead tom coneone illogically de-activating + // because that could lead to someone illogically de-activating // say, node-api, while trying to build with node-addon-api (which isn't // how the addon API works of course). // Thus, I propose offering a new CLI arg for something like '--link-level=2', diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 12ffc18f..d3098698 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -582,17 +582,18 @@ if(CMAKEJS_USING_CMAKEJS) list(APPEND CMAKEJS_TARGETS cmake-js) - # Node that the below function definitions are contained inside 'if(CMAKEJS_CMAKEJS)' (our main helper library).... +# Note that the below function definitions are contained inside +# 'if(CMAKEJS_CMAKEJS)' (because they require our main helper library).... - #[=============================================================================[ - Exposes a user-side helper function for creating a dynamic '*.node' library, - linked to the Addon API interface. +#[=============================================================================[ +Exposes a user-side helper function for creating a dynamic '*.node' library, +linked to the Addon API interface. - cmakejs_create_napi_addon( []) - cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_VERSION ] []) +cmakejs_create_napi_addon( []) +cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_VERSION ] []) - (This should wrap the CMakeLists.txt-side requirements for building a Napi Addon) - ]=============================================================================]# +(This should wrap the CMakeLists.txt-side requirements for building a Napi Addon) +]=============================================================================]# function(cmakejs_create_napi_addon name) # Avoid duplicate target names @@ -643,11 +644,34 @@ if(CMAKEJS_USING_CMAKEJS) set(name_alt "${ARG_NAMESPACE}") endif() + # TODO: How the exceptions are set in fallback cases can be very tricky + # to ascertain. There are numerous different '-D' flags for different + # compilers and platforms for either enabling or disabling exceptions; + # It is also not a good idea to use mixed exceptions policies, or + # link different libraries together with different exceptions policies; + # The user could call this nice new EXCEPTIONS arg in our function, which + # sets a PUBLIC definition (meaning, it propagates to anything that might + # be linked with it); our arg accepts YES, NO, or MAYBE as per . + # Default is MAYBE (as in, no opinion of our own...) + # But, this is not taking into account the users that would rather set + # '-D_UNWIND', '-DCPP_EXCEPTIONS', or some other flag specific to their + # system. If they did, and we are not honouring it, then we are risking + # breaking their global exceptions policy... + # I suggest taking a look at the header file that CMakeRC generates + # to understand how to grep a variety of different possiple exceptions flags + # all into a custom one which handles all cases. The Napi way of having + # three seperate args, that can each be defined against logic, is unfortunate + # and we don't want to break compatibility of existing users' projects. + # I have made one attempt at this in the past which I will revisit + # shortly... but definitely a case of, all ideas welcome! if(NOT ARG_EXCEPTIONS) set(ARG_EXCEPTIONS "MAYBE") # YES, NO, or MAYBE... endif() - if((NOT DEFINED NAPI_CPP_EXCEPTIONS) OR (NOT DEFINED NAPI_DISABLE_CPP_EXCEPTIONS) OR (NOT DEFINED NAPI_CPP_EXCEPTIONS_MAYBE)) + if((NOT DEFINED NAPI_CPP_EXCEPTIONS) OR + (NOT DEFINED NAPI_DISABLE_CPP_EXCEPTIONS) OR + (NOT DEFINED NAPI_CPP_EXCEPTIONS_MAYBE) + ) if(ARG_EXCEPTIONS STREQUAL "YES") set(_NAPI_GLOBAL_EXCEPTIONS_POLICY "NAPI_CPP_EXCEPTIONS") @@ -713,7 +737,7 @@ if(CMAKEJS_USING_CMAKEJS) set_property( TARGET ${name} - PROPERTY "${name}_IS_NAPI_ADDON_LIBRARY" TRUE + PROPERTY "${name}_IS_NAPI_ADDON_LIBRARY" TRUE # Custom property ) set_target_properties(${name} @@ -756,14 +780,14 @@ if(CMAKEJS_USING_CMAKEJS) endfunction() - #[=============================================================================[ - Add source files to an existing Napi Addon target. +#[=============================================================================[ +Add source files to an existing Napi Addon target. - cmakejs_napi_addon_add_sources( [items1...]) - cmakejs_napi_addon_add_sources( [BASE_DIRS ] [items1...]) - cmakejs_napi_addon_add_sources( [ [items1...] [ [items2...] ...]]) - cmakejs_napi_addon_add_sources( [ [BASE_DIRS [...]] [items1...]...) - ]=============================================================================]# +cmakejs_napi_addon_add_sources( [items1...]) +cmakejs_napi_addon_add_sources( [BASE_DIRS ] [items1...]) +cmakejs_napi_addon_add_sources( [ [items1...] [ [items2...] ...]]) +cmakejs_napi_addon_add_sources( [ [BASE_DIRS [...]] [items1...]...) +]=============================================================================]# function(cmakejs_napi_addon_add_sources name) # Check that this is a Node Addon target @@ -832,12 +856,12 @@ if(CMAKEJS_USING_CMAKEJS) endfunction() - #[=============================================================================[ - Add pre-processor definitions to an existing Napi Addon target. +#[=============================================================================[ +Add pre-processor definitions to an existing Napi Addon target. - cmakejs_napi_addon_add_definitions( [items1...]) - cmakejs_napi_addon_add_definitions( [items1...] [ [items2...] ...]) - ]=============================================================================]# +cmakejs_napi_addon_add_definitions( [items1...]) +cmakejs_napi_addon_add_definitions( [items1...] [ [items2...] ...]) +]=============================================================================]# function(cmakejs_napi_addon_add_definitions name) # Check that this is a Node Addon target @@ -878,7 +902,8 @@ if(CMAKEJS_USING_CMAKEJS) endif() # CMAKEJS_CMAKEJS -# This should enable each target to behave well with intellisense (in case they weren't already) +# This should enable each target to behave well with intellisense +# (in case they weren't already) foreach(TARGET IN LISTS CMAKEJS_TARGETS) target_include_directories(${TARGET} INTERFACE @@ -935,7 +960,7 @@ check_required_components (cmake-js) # Not sure if this is needed... set (CMAKE_JS_SRC "@CMAKE_JS_SRC@") -set (CMAKE_JS_INC "@CMAKE_JS_INC@") +set (CMAKE_JS_INC @CMAKE_JS_INC@) set (CMAKE_JS_LIB "@CMAKE_JS_LIB@") set (CMAKE_JS_VERSION "@CMAKE_JS_VERSION@") set (CMAKE_JS_EXECUTABLE "@CMAKE_JS_EXECUTABLE@") @@ -943,7 +968,7 @@ set (CMAKE_JS_INC_FILES "") list (APPEND CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") if (CMAKEJS_NODE_API) - set (NODE_API_HEADERS_DIR "@NODE_API_HEADERS_DIR@") + set (NODE_API_HEADERS_DIR @NODE_API_HEADERS_DIR@) set (NODE_API_INC_FILES "") list (APPEND NODE_API_INC_FILES "@NODE_API_INC_FILES@") endif() From 395b4847485c4c43f0441d271c6e524eb6f980f4 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 05:04:01 +0100 Subject: [PATCH 25/92] more TODO --- share/cmake/CMakeJS.cmake | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index d3098698..d1520360 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -624,7 +624,7 @@ cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_V endif() endif() - # Needs more validation... + # TODO: This needs more/better validation... if(DEFINED ARG_NAPI_VERSION AND (ARG_NAPI_VERSION LESS_EQUAL 0)) message(SEND_ERROR "NAPI_VERSION for ${name} is not a valid Integer number (${ARG_NAPI_VERSION})") return() @@ -809,9 +809,6 @@ cmakejs_napi_addon_add_sources( [ [BASE_DIRS [ Date: Sat, 3 Feb 2024 05:04:30 +0100 Subject: [PATCH 26/92] added multi-platform bindings to hello addon --- tests/api/hello/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/api/hello/index.js b/tests/api/hello/index.js index 322ab6f9..1ec69311 100644 --- a/tests/api/hello/index.js +++ b/tests/api/hello/index.js @@ -1,4 +1,10 @@ // This small codeblock in your root-level index.js allows others to consume // your addon as any other NodeJS module -const hello = require('./build/lib/addon.node'); +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const hello = require(`..${buildDir}addon.node`); module.exports = hello; From 1b3985940560d3fa193e4a3a8a2fa8d34e6d4eb8 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 05:33:53 +0100 Subject: [PATCH 27/92] Tell the user how it works --- share/cmake/CMakeJS.cmake | 98 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 6 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index d1520360..de13e40c 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -1093,6 +1093,78 @@ foreach(TARGET IN LISTS CMAKEJS_TARGETS) endforeach() if(NOT CMakeJS_IS_TOP_LEVEL) + + # cmake-js --link-level=0 + if(CMAKEJS_USING_NODE_DEV AND (NOT CMAKEJS_USING_NODE_API)) + message(STATUS [==[ +-- +-- To build with the Node.js developer API, +-- +-- Add this to your CMakeLists.txt: +-- + +include(CMakeJS) + +add_library(my_library SHARED) +target_sources(my_library PRIVATE src//my_library.cc) +target_link_libraries(my_library PRIVATE cmake-js::node-dev) + +-- +-- You can include '' in 'my_library.cc' and start building +-- with the Node API, including v8, uv, and all its' dependencies. +-- +]==]) + endif() # CMAKEJS_USING_NODE_API + + # cmake-js --link-level=1 + if(CMAKEJS_USING_NODE_API AND (NOT CMAKEJS_USING_NODE_ADDON_API)) + message(STATUS [==[ +-- +-- To build a Node.js addon in C, +-- +-- Add this to your CMakeLists.txt: +-- + +include(CMakeJS) + +add_library(my_addon SHARED) +target_sources(my_addon PRIVATE src//my_addon.c) +target_link_libraries(my_addon PRIVATE cmake-js::node-api) +set_target_properties(my_addon PROPERTIES PREFIX "" SUFFIX ".node") + +-- +-- You can include '' in 'my_addon.c' and start building +-- with the Node Addon API in C. +-- +]==]) + endif() # CMAKEJS_USING_NODE_API + + # cmake-js --link-level=2 + if(CMAKEJS_USING_NODE_ADDON_API AND (NOT CMAKEJS_USING_CMAKEJS)) + message(STATUS [==[ +-- +-- To build a Node.js addon in C++, +-- +-- Add this to your CMakeLists.txt: +-- + +include(CMakeJS) + +add_library(my_addon SHARED) +target_sources(my_addon PRIVATE src//my_addon.cpp) +target_link_libraries(my_addon PRIVATE cmake-js::node-addon-api) +set_target_properties(my_addon PROPERTIES PREFIX "" SUFFIX ".node") +add_target_definitions(my_addon PRIVATE BUILDING_NODE_EXTENSION) + +-- +-- You can include '' in 'my_addon.cpp' and start building +-- with the Node Addon API in C++. +-- +]==]) + endif() # CMAKEJS_USING_NODE_ADDON_API + + # cmake-js --link-level=3 (default) + if(CMAKEJS_USING_CMAKEJS) message(STATUS [==[ -- -- To build a Node.js addon, @@ -1100,6 +1172,8 @@ if(NOT CMakeJS_IS_TOP_LEVEL) -- Add this to your CMakeLists.txt: -- +include(CMakeJS) + cmakejs_create_napi_addon ( # CMAKEJS_ADDON_NAME my_addon @@ -1109,16 +1183,18 @@ cmakejs_create_napi_addon ( NAMESPACE ) --- You will be able to load your addon in JavaScript code: --- - -const my_addon = require("./build/lib/my_addon.node"); +]==]) -console.log(`Napi Status: ${my_addon.hello()}`); -console.log(`Napi Version: ${my_addon.version()}`); + # cmake-js --link-level=4 (experimental) + if(CMAKEJS_USING_NODE_SEA_CONFIG) + # https://nodejs.org/api/single-executable-applications.html + endif() +# Global message (our CLI applies in all scenarios) +message(STATUS [==[ -- You may use either the regular CMake interface, or the cmake-js CLI, to build your addon! -- +-- Add this to your package.json: { "name": "@/my-addon", @@ -1135,6 +1211,15 @@ console.log(`Napi Version: ${my_addon.version()}`); // ... } +-- You will be able to load your built addon in JavaScript code: +-- + +const my_addon = require("./build/lib/my_addon.node"); + +console.log(`Napi Status: ${my_addon.hello()}`); +console.log(`Napi Version: ${my_addon.version()}`); + + -- Make sure to register a module in your C/C++ code like official example does: -- https://github.com/nodejs/node-addon-examples/blob/main/src/1-getting-started/1_hello_world/node-addon-api/hello.cc -- @@ -1145,6 +1230,7 @@ console.log(`Napi Version: ${my_addon.version()}`); -- https://github.com/nodejs/node-addon-examples -- -- ]==]) + endif() # CMAKEJS_USING_CMAKEJS endif() unset(_version) From 7bf682278aefac4f0422e9d22db213f9c93bf34e Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 05:49:56 +0100 Subject: [PATCH 28/92] Relocatable include dir --- share/cmake/CMakeJS.cmake | 59 ++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index de13e40c..ba83fa2f 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -171,16 +171,16 @@ foreach(_DEP IN LISTS _NODE_DEV_DEPS) endforeach() unset(_NODE_DEV_DEPS) -# relocate... (this is crucial for 'install()' to work on user's addons) -set(CMAKE_JS_INC "") +# # relocate... (this is crucial to get for 'install()' to work on user's addons) +# set(CMAKE_JS_INC "") -# target include directories (as if 'node-dev' were an isolated CMake project...) -set(CMAKE_JS_INC - $ - $ -) +# # target include directories (as if 'node-dev' were an isolated CMake project...) +# set(CMAKE_JS_INC +# $ +# $ +# ) -set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE PATH "cmake-js include directory." FORCE) +# set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE PATH "cmake-js include directory." FORCE) set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE FILEPATH "cmake-js source file." FORCE) set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE FILEPATH "cmake-js lib file." FORCE) @@ -955,26 +955,26 @@ include (${CMAKE_CURRENT_LIST_DIR}/CMakeJSTargets.cmake) check_required_components (cmake-js) -# Not sure if this is needed... -set (CMAKE_JS_SRC "@CMAKE_JS_SRC@") -set (CMAKE_JS_INC @CMAKE_JS_INC@) -set (CMAKE_JS_LIB "@CMAKE_JS_LIB@") -set (CMAKE_JS_VERSION "@CMAKE_JS_VERSION@") -set (CMAKE_JS_EXECUTABLE "@CMAKE_JS_EXECUTABLE@") -set (CMAKE_JS_INC_FILES "") -list (APPEND CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") - -if (CMAKEJS_NODE_API) - set (NODE_API_HEADERS_DIR @NODE_API_HEADERS_DIR@) - set (NODE_API_INC_FILES "") - list (APPEND NODE_API_INC_FILES "@NODE_API_INC_FILES@") -endif() - -if (CMAKE_JS_NODE_ADDON_API) - set (NODE_ADDON_API_DIR "@NODE_ADDON_API_DIR@") - set (NODE_ADDON_API_INC_FILES "") - list (APPEND NODE_ADDON_API_INC_FILES "@NODE_ADDON_API_INC_FILES@") -endif () +# # Not sure if this is needed... +# set (CMAKE_JS_SRC "@CMAKE_JS_SRC@") +# set (CMAKE_JS_INC @CMAKE_JS_INC@) +# set (CMAKE_JS_LIB "@CMAKE_JS_LIB@") +# set (CMAKE_JS_VERSION "@CMAKE_JS_VERSION@") +# set (CMAKE_JS_EXECUTABLE "@CMAKE_JS_EXECUTABLE@") +# set (CMAKE_JS_INC_FILES "") +# list (APPEND CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") + +# if (CMAKEJS_NODE_API) +# set (NODE_API_HEADERS_DIR @NODE_API_HEADERS_DIR@) +# set (NODE_API_INC_FILES "") +# list (APPEND NODE_API_INC_FILES "@NODE_API_INC_FILES@") +# endif() + +# if (CMAKE_JS_NODE_ADDON_API) +# set (NODE_ADDON_API_DIR "@NODE_ADDON_API_DIR@") +# set (NODE_ADDON_API_INC_FILES "") +# list (APPEND NODE_ADDON_API_INC_FILES "@NODE_ADDON_API_INC_FILES@") +# endif () ]==]) @@ -1021,7 +1021,8 @@ file(COPY "${_CMAKEJS_SCRIPT}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/share/cm # 'install' target. include(GNUInstallDirs) - +unset(CMAKEJS_INC_DIR) +set(CMAKEJS_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING "Installation directory for include files, a relative path that will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.") # copy headers (and definitions?) to build dir for distribution if(CMAKEJS_USING_NODE_DEV) install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") From 287811f4d08403826b3eb33e244b71992459d907 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 05:56:13 +0100 Subject: [PATCH 29/92] Downstream fixes --- share/cmake/CMakeJS.cmake | 2 +- tests/api/hello/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index ba83fa2f..67383e0a 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -171,7 +171,7 @@ foreach(_DEP IN LISTS _NODE_DEV_DEPS) endforeach() unset(_NODE_DEV_DEPS) -# # relocate... (this is crucial to get for 'install()' to work on user's addons) +# # relocate... (this is crucial to get right for 'install()' to work on user's addons) # set(CMAKE_JS_INC "") # # target include directories (as if 'node-dev' were an isolated CMake project...) diff --git a/tests/api/hello/index.js b/tests/api/hello/index.js index 1ec69311..4e5b1cdb 100644 --- a/tests/api/hello/index.js +++ b/tests/api/hello/index.js @@ -6,5 +6,5 @@ var buildDir = "/build/lib/"; if(platform === "win32") buildDir = "\\build\\bin\\Release\\"; -const hello = require(`..${buildDir}addon.node`); +const hello = require(`.${buildDir}addon.node`); module.exports = hello; From e0ee5b8c0c98a83a9a810c1decd972b37a3be10d Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 06:25:25 +0100 Subject: [PATCH 30/92] Strings fix - so files that dont exist wont show up as dirs in sources list --- share/cmake/CMakeJS.cmake | 44 ++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 67383e0a..d8e4d6de 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -14,6 +14,8 @@ set(_version 7.3.3) cmake_minimum_required(VERSION 3.15) cmake_policy(VERSION 3.15) include(CMakeParseArguments) +include(GNUInstallDirs) +include(CMakeDependentOption) if(COMMAND cmakejs_napi_addon_add_sources) if(NOT DEFINED _CMAKEJS_VERSION OR NOT (_version STREQUAL _CMAKEJS_VERSION)) @@ -39,13 +41,26 @@ Setup optional targets dependency chain, e.g., for end-user specification with VCPKG_FEATURE_FLAGS or by passing for example '-DCMAKE_NODE_API:BOOL=FALSE' ]=============================================================================]# -set (CMAKEJS_TARGETS "") -include(CMakeDependentOption) +set (CMAKEJS_TARGETS "") # This list will auto-populate from --link-level option (CMAKEJS_USING_NODE_DEV "Supply cmake-js::node-dev target for linkage" ON) cmake_dependent_option(CMAKEJS_USING_NODE_API "Supply cmake-js::node-api target for linkage" ON CMAKEJS_USING_NODE_DEV OFF) cmake_dependent_option(CMAKEJS_USING_NODE_ADDON_API "Supply cmake-js::node-addon-api target for linkage" ON CMAKEJS_USING_NODE_API OFF) cmake_dependent_option(CMAKEJS_USING_CMAKEJS "Supply cmake-js::cmake-js target for linkage" ON CMAKEJS_USING_NODE_ADDON_API OFF) +# TODO: re; the above. +# I propose instead of exposing all four "CMAKEJS_USING_*" options at once against and +# allowing illogical combinations of dependencies, we instead setup a new CLI arg +# from the Javascript side, '--link-level'. + +# 'cmake-js configure --link-level=0' equals 'CMAKEJS_USING_NODE_DEV=ON' +# 'cmake-js configure --link-level=1' equals 'CMAKEJS_USING_NODE_API=ON' +# 'cmake-js configure --link-level=2' equals 'CMAKEJS_USING_NODE_ADDON_API=ON' +# 'cmake-js configure --link-level=3' equals 'CMAKEJS_USING_CMAKEJS=ON' + +# I already created the '-DCMAKEJS_USING_*' entries in the JS CLI, but currently +# without any of the logic proposed above. + + #[=============================================================================[ Internal helper (borrowed from CMakeRC). ]=============================================================================]# @@ -171,7 +186,7 @@ foreach(_DEP IN LISTS _NODE_DEV_DEPS) endforeach() unset(_NODE_DEV_DEPS) -# # relocate... (this is crucial to get right for 'install()' to work on user's addons) +# relocate... (this is crucial to get right for 'install()' to work on user's addons) # set(CMAKE_JS_INC "") # # target include directories (as if 'node-dev' were an isolated CMake project...) @@ -180,9 +195,9 @@ unset(_NODE_DEV_DEPS) # $ # ) -# set(CMAKE_JS_INC "${CMAKE_JS_INC}" CACHE PATH "cmake-js include directory." FORCE) -set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE FILEPATH "cmake-js source file." FORCE) -set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE FILEPATH "cmake-js lib file." FORCE) +# set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) +set(CMAKE_JS_SRC ${CMAKE_JS_SRC} CACHE FILEPATH "cmake-js source file." FORCE) +set(CMAKE_JS_LIB ${CMAKE_JS_LIB} CACHE FILEPATH "cmake-js lib file." FORCE) set(CMAKE_JS_INC_FILES "") # prevent repetitive globbing on each run file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") @@ -366,10 +381,10 @@ if(CMAKEJS_USING_NODE_DEV) # cmake-js::node-dev add_library (node-dev INTERFACE) add_library (cmake-js::node-dev ALIAS node-dev) - target_include_directories (node-dev INTERFACE "${CMAKE_JS_INC}") - target_sources (node-dev INTERFACE "${CMAKE_JS_SRC}") - target_sources (node-dev INTERFACE "${CMAKE_JS_INC_FILES}") - target_link_libraries (node-dev INTERFACE "${CMAKE_JS_LIB}") + target_include_directories (node-dev INTERFACE ${CMAKE_JS_INC}) + target_sources (node-dev INTERFACE ${CMAKE_JS_SRC}) + # target_sources (node-dev INTERFACE "${CMAKE_JS_INC_FILES}") + target_link_libraries (node-dev INTERFACE ${CMAKE_JS_LIB}) set_target_properties (node-dev PROPERTIES VERSION ${NODE_VERSION}) set(NODE_DEV_FILES "") @@ -485,7 +500,8 @@ if(CMAKEJS_USING_NODE_API) add_library (cmake-js::node-api ALIAS node-api) target_include_directories (node-api INTERFACE ${NODE_API_HEADERS_DIR}) target_link_libraries (node-api INTERFACE cmake-js::node-dev) - set_target_properties (node-api PROPERTIES VERSION 6.1.0) + set_target_properties (node-api PROPERTIES VERSION 6.1.0) + set_target_properties (node-api PROPERTIES SOVERSION 6) set(NODE_API_FILES "") list(APPEND NODE_API_FILES @@ -533,7 +549,8 @@ if(CMAKEJS_USING_NODE_ADDON_API) add_library (cmake-js::node-addon-api ALIAS node-addon-api) target_include_directories (node-addon-api INTERFACE ${NODE_ADDON_API_DIR}) target_link_libraries (node-addon-api INTERFACE cmake-js::node-api) - set_target_properties (node-addon-api PROPERTIES VERSION 1.1.0) + set_target_properties (node-addon-api PROPERTIES VERSION 1.1.0) + set_target_properties (node-addon-api PROPERTIES SOVERSION 1) set(NODE_ADDON_API_FILES "") list(APPEND NODE_ADDON_API_FILES @@ -1020,9 +1037,8 @@ file(COPY "${_CMAKEJS_SCRIPT}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/share/cm # To do this, they set '-DCMAKE_INSTALL_PREFIX=./install', configure, then build the # 'install' target. -include(GNUInstallDirs) unset(CMAKEJS_INC_DIR) -set(CMAKEJS_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING "Installation directory for include files, a relative path that will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.") +set(CMAKEJS_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for include files, a relative path that will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.") # copy headers (and definitions?) to build dir for distribution if(CMAKEJS_USING_NODE_DEV) install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") From a6becb18e2110c27354d382edfb6321481e28967 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 06:40:29 +0100 Subject: [PATCH 31/92] Finally got rid of this wierd 'directory-as-source-file' issue - careful with strings in CMake! --- share/cmake/CMakeJS.cmake | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index d8e4d6de..9a9e77c1 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -382,9 +382,8 @@ if(CMAKEJS_USING_NODE_DEV) add_library (node-dev INTERFACE) add_library (cmake-js::node-dev ALIAS node-dev) target_include_directories (node-dev INTERFACE ${CMAKE_JS_INC}) - target_sources (node-dev INTERFACE ${CMAKE_JS_SRC}) - # target_sources (node-dev INTERFACE "${CMAKE_JS_INC_FILES}") - target_link_libraries (node-dev INTERFACE ${CMAKE_JS_LIB}) + target_sources (node-dev INTERFACE ${CMAKE_JS_SRC}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) + target_link_libraries (node-dev INTERFACE ${CMAKE_JS_LIB}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) set_target_properties (node-dev PROPERTIES VERSION ${NODE_VERSION}) set(NODE_DEV_FILES "") From 6a68eef143b0acb27c2baa0c60f78b563560b264 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:01:34 +0100 Subject: [PATCH 32/92] added 'hello_with_types' example cmake-js project --- tests/api/hello_consumer/index.js | 3 + tests/api/hello_with_types/.gitignore | 3 + tests/api/hello_with_types/CMakeLists.txt | 15 +++++ tests/api/hello_with_types/index.js | 4 ++ .../api/hello_with_types/lib/addon.node.d.ts | 6 ++ tests/api/hello_with_types/lib/addon.node.js | 11 ++++ tests/api/hello_with_types/lib/addon.node.ts | 17 +++++ tests/api/hello_with_types/package.json | 23 +++++++ .../api/hello_with_types/src/hello/addon.cpp | 62 +++++++++++++++++++ 9 files changed, 144 insertions(+) create mode 100644 tests/api/hello_with_types/.gitignore create mode 100644 tests/api/hello_with_types/CMakeLists.txt create mode 100644 tests/api/hello_with_types/index.js create mode 100644 tests/api/hello_with_types/lib/addon.node.d.ts create mode 100644 tests/api/hello_with_types/lib/addon.node.js create mode 100644 tests/api/hello_with_types/lib/addon.node.ts create mode 100644 tests/api/hello_with_types/package.json create mode 100644 tests/api/hello_with_types/src/hello/addon.cpp diff --git a/tests/api/hello_consumer/index.js b/tests/api/hello_consumer/index.js index 37e8c2b5..4437af53 100644 --- a/tests/api/hello_consumer/index.js +++ b/tests/api/hello_consumer/index.js @@ -6,3 +6,6 @@ const addon = require('@vendor/hello'); console.log(addon.hello()) + +// If I swap this package for '@vendor/hello_with_types', +// my intellisense engine explains the functions to me :) diff --git a/tests/api/hello_with_types/.gitignore b/tests/api/hello_with_types/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/api/hello_with_types/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/api/hello_with_types/CMakeLists.txt b/tests/api/hello_with_types/CMakeLists.txt new file mode 100644 index 00000000..c00bee4b --- /dev/null +++ b/tests/api/hello_with_types/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.15) + +# If not using cmake-js CLI, fetch the API manually +if (NOT DEFINED CMAKE_JS_VERSION) + list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif () + +project(hello) + +include(CMakeJS) + +cmakejs_create_napi_addon( + addon + src/hello/addon.cpp +) diff --git a/tests/api/hello_with_types/index.js b/tests/api/hello_with_types/index.js new file mode 100644 index 00000000..71b5dd00 --- /dev/null +++ b/tests/api/hello_with_types/index.js @@ -0,0 +1,4 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const hello = require(`./lib/addon.node`); +module.exports = hello; diff --git a/tests/api/hello_with_types/lib/addon.node.d.ts b/tests/api/hello_with_types/lib/addon.node.d.ts new file mode 100644 index 00000000..89e5844c --- /dev/null +++ b/tests/api/hello_with_types/lib/addon.node.d.ts @@ -0,0 +1,6 @@ +declare interface addon { + hello(): string; + version(): string; +} +declare const addon: addon; +export = addon; diff --git a/tests/api/hello_with_types/lib/addon.node.js b/tests/api/hello_with_types/lib/addon.node.js new file mode 100644 index 00000000..55c8ada8 --- /dev/null +++ b/tests/api/hello_with_types/lib/addon.node.js @@ -0,0 +1,11 @@ +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const addon + = require(`..${buildDir}addon +.node`); +module.exports = addon +; diff --git a/tests/api/hello_with_types/lib/addon.node.ts b/tests/api/hello_with_types/lib/addon.node.ts new file mode 100644 index 00000000..a7eef18a --- /dev/null +++ b/tests/api/hello_with_types/lib/addon.node.ts @@ -0,0 +1,17 @@ +/** + * The 'addon' C++ addon interface. + */ +interface addon { + /** + * Returns a string, confirming the cmake-js addon is online. + * @returns string + */ + hello(): string; + /** + * Returns a number, confirming the Napi Addon API version number. + * @returns number + */ + version(): number; +} +const addon: addon = require('../build/lib/addon.node'); +export = addon; diff --git a/tests/api/hello_with_types/package.json b/tests/api/hello_with_types/package.json new file mode 100644 index 00000000..05a9531a --- /dev/null +++ b/tests/api/hello_with_types/package.json @@ -0,0 +1,23 @@ +{ + "name": "@vendor/hello", + "version": "1.0.0", + "description": "A test addon made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node ./index.js", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" + }, + "dependencies": { + "cmake-js": "../../../", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + } +} diff --git a/tests/api/hello_with_types/src/hello/addon.cpp b/tests/api/hello_with_types/src/hello/addon.cpp new file mode 100644 index 00000000..bd3e3e1b --- /dev/null +++ b/tests/api/hello_with_types/src/hello/addon.cpp @@ -0,0 +1,62 @@ +/** + * @file addon.cpp + * @brief A quick 'hello world' Napi Addon in C++ +*/ + +// Required header and C++ flag +#if __has_include() && BUILDING_NODE_EXTENSION + +#include + +#ifndef STRINGIFY +# define STRINGIFY_HELPER(n) #n +# define STRINGIFY(n) STRINGIFY_HELPER(n) +#endif + +namespace Napi +{ +namespace NAPI_CPP_CUSTOM_NAMESPACE +{ + +Napi::Value Hello(const Napi::CallbackInfo& info) { + return Napi::String::New(info.Env(), STRINGIFY(CMAKEJS_ADDON_NAME)".node is online!"); +} + +Napi::Value Version(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), NAPI_VERSION); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + + // Export a chosen C++ function under a given Javascript key + exports.Set( + Napi::String::New(env, "hello"), // Name of function on Javascript side... + Napi::Function::New(env, Hello) // Name of function on C++ side... + ); + + exports.Set( + Napi::String::New(env, "version"), + Napi::Function::New(env, Version) + ); + + // The above will expose the C++ function 'Hello' as a javascript function + // named 'hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NODE_API_MODULE(CMAKEJS_ADDON_NAME, Init) // (name to use, initializer to use) + +} // namespace NAPI_CPP_CUSTOM_NAMESPACE +} // namespace Napi + +// Export your custom namespace to outside of the Napi namespace, providing an +// alias to the Napi Addon API; e.g., '::::Object()', along with the +// functions defined above, such as '::::Hello()'. +namespace NAPI_CPP_CUSTOM_NAMESPACE::CMAKEJS_ADDON_NAME { + using namespace Napi::NAPI_CPP_CUSTOM_NAMESPACE; +} + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif From b509ad8c009b6bd838645bb32124687774ebbba9 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:02:11 +0100 Subject: [PATCH 33/92] comment on my choice of usage of 'NODE_*' namespace --- share/cmake/CMakeJS.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 9a9e77c1..eeebb4f1 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -482,7 +482,7 @@ endif() if(CMAKEJS_USING_NODE_API) # Acquire if needed... - if(NOT DEFINED NODE_API_HEADERS_DIR) + if(NOT DEFINED NODE_API_HEADERS_DIR) # Why the NODE_API_* namespace? Because 'node-api-headers' from vcpkg also provides this exact var, so we can help our users from vcpkg-land avoid picking up headers they already have ; but, we still need to process those headers into our target(s) for them! cmakejs_acquire_napi_c_files() set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} CACHE PATH "Node API Headers directory." FORCE) message(DEBUG "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") @@ -531,7 +531,7 @@ endif() if(CMAKEJS_USING_NODE_ADDON_API) # Acquire if needed... - if(NOT DEFINED NODE_ADDON_API_DIR) + if(NOT DEFINED NODE_ADDON_API_DIR) # Why the NODE_ADDON_API_* namespace? Because 'node-addon-api' from vcpkg also provides this exact var, so we can help our users from vcpkg-land avoid picking up headers they already have ; but, we still need to process those headers into our target(s) for them! cmakejs_acquire_napi_cpp_files() set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} CACHE PATH "Node Addon API Headers directory." FORCE) message(DEBUG "NODE_ADDON_API_DIR: ${NODE_ADDON_API_DIR}") From 4f9cf133e5081a3ca7eb69ae809b75dbaf5e3b3e Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:04:02 +0100 Subject: [PATCH 34/92] custom namespace demo --- tests/api/hello_with_types/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/api/hello_with_types/CMakeLists.txt b/tests/api/hello_with_types/CMakeLists.txt index c00bee4b..57ef4d68 100644 --- a/tests/api/hello_with_types/CMakeLists.txt +++ b/tests/api/hello_with_types/CMakeLists.txt @@ -12,4 +12,5 @@ include(CMakeJS) cmakejs_create_napi_addon( addon src/hello/addon.cpp + NAMESPACE vendor ) From c7bc40e3506fd427dcf985e02b63be9b2dba2852 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:12:15 +0100 Subject: [PATCH 35/92] fixed 'hello_with_types' --- tests/api/hello_with_types/index.js | 4 ++-- tests/api/hello_with_types/package.json | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/api/hello_with_types/index.js b/tests/api/hello_with_types/index.js index 71b5dd00..a4f57180 100644 --- a/tests/api/hello_with_types/index.js +++ b/tests/api/hello_with_types/index.js @@ -1,4 +1,4 @@ // This small codeblock in your root-level index.js allows others to consume // your addon as any other NodeJS module -const hello = require(`./lib/addon.node`); -module.exports = hello; +const hello_with_types = require(`./lib/addon.node`); +module.exports = hello_with_types; diff --git a/tests/api/hello_with_types/package.json b/tests/api/hello_with_types/package.json index 05a9531a..b5eec3be 100644 --- a/tests/api/hello_with_types/package.json +++ b/tests/api/hello_with_types/package.json @@ -1,5 +1,5 @@ { - "name": "@vendor/hello", + "name": "@vendor/hello_with_types", "version": "1.0.0", "description": "A test addon made using CMakeJS.cmake", "main": "index.js", @@ -19,5 +19,9 @@ "cmake-js": "../../../", "node-addon-api": "^7.1.0", "node-api-headers": "^1.1.0" - } + }, + "devDependencies": { + "@types/node": "^20.11.14", + "typescript": "^5.3.3" + } } From b83cf0698c0d2a4e41686c8897ff3dcdabdf479f Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:13:11 +0100 Subject: [PATCH 36/92] typo --- tests/api/hello_with_types/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api/hello_with_types/package.json b/tests/api/hello_with_types/package.json index b5eec3be..b48c5c73 100644 --- a/tests/api/hello_with_types/package.json +++ b/tests/api/hello_with_types/package.json @@ -1,7 +1,7 @@ { "name": "@vendor/hello_with_types", "version": "1.0.0", - "description": "A test addon made using CMakeJS.cmake", + "description": "A test addon with Typescript support made using CMakeJS.cmake", "main": "index.js", "license": "MIT", "scripts": { From d214b338c721b5188a9b3e4b9e05b7b3b55fbdcb Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:14:44 +0100 Subject: [PATCH 37/92] linted --- tests/api/hello/package.json | 14 +++++++------- tests/api/hello_with_types/package.json | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/api/hello/package.json b/tests/api/hello/package.json index 05a9531a..9e6b420b 100644 --- a/tests/api/hello/package.json +++ b/tests/api/hello/package.json @@ -5,15 +5,15 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node ./index.js", - "install": "cmake-js install", + "start": "node ./index.js", + "install": "cmake-js install", "postinstall": "cmake-js compile", - "configure": "cmake-js configure", + "configure": "cmake-js configure", "reconfigure": "cmake-js reconfigure", - "build": "cmake-js build", - "rebuild": "cmake-js rebuild", - "clean": "cmake-js clean", - "wipe": "cmake-js clean && rm -rvf ./node_modules" + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { "cmake-js": "../../../", diff --git a/tests/api/hello_with_types/package.json b/tests/api/hello_with_types/package.json index b48c5c73..2e1d46be 100644 --- a/tests/api/hello_with_types/package.json +++ b/tests/api/hello_with_types/package.json @@ -5,15 +5,15 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node ./index.js", - "install": "cmake-js install", + "start": "node ./index.js", + "install": "cmake-js install", "postinstall": "cmake-js compile", - "configure": "cmake-js configure", + "configure": "cmake-js configure", "reconfigure": "cmake-js reconfigure", - "build": "cmake-js build", - "rebuild": "cmake-js rebuild", - "clean": "cmake-js clean", - "wipe": "cmake-js clean && rm -rvf ./node_modules" + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { "cmake-js": "../../../", From 66c69e08cea0638e0b22b14e869337924cee5f53 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 07:35:00 +0100 Subject: [PATCH 38/92] comment on types usage --- tests/api/hello_consumer/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/api/hello_consumer/index.js b/tests/api/hello_consumer/index.js index 4437af53..b2f4d881 100644 --- a/tests/api/hello_consumer/index.js +++ b/tests/api/hello_consumer/index.js @@ -7,5 +7,8 @@ const addon = require('@vendor/hello'); console.log(addon.hello()) -// If I swap this package for '@vendor/hello_with_types', -// my intellisense engine explains the functions to me :) +// If I swap my package.json dependency for +// '@vendor/hello_with_types' and do +// 'require('@vendor/hello_with_types')' here instead, +// then my intellisense engine explains the +// functions to me :) From 28a4879de91c45130bbe85c6934cbe2dcaa08216 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 08:07:26 +0100 Subject: [PATCH 39/92] alias commands for demo and a comment --- CMakeLists.txt | 4 ++-- package.json | 9 ++++++++- share/cmake/CMakeJS.cmake | 9 +++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2d4e3c1..e7319992 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,9 +117,9 @@ console.log(`Napi Version: ${demo.version()}`); ) if(BUILD_TESTS) - # include(CTest) # If you want to see all the test dashboard targets, uncomment this include() enable_testing() include("${CMAKE_CURRENT_LIST_DIR}/tests/demo/tests.cmake") + include(CTest) # If you want to see all the test dashboard targets, uncomment this include() endif() # offer a basic/messy CPack @@ -143,7 +143,7 @@ console.log(`Napi Version: ${demo.version()}`); set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON) set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) - set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") + set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") # Check out CPack's 'NSIS' installer for Win32, and the others! set(CPACK_SOURCE_IGNORE_FILES _CPack_Packages /*.zip diff --git a/package.json b/package.json index 58023657..20f03cbd 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,14 @@ }, "scripts": { "test": "mocha tests", - "lint": "eslint lib bin/cmake-js tests" + "lint": "eslint lib bin/cmake-js tests", + + "demo:configure": "cmake --fresh -S . -B ./build -DBUILD_TESTS:BOOL=TRUE -DCMAKE_INSTALL_PREFIX:PATH=./install -G Ninja", + "demo:build": "cmake --build ./build", + "demo:install": "cmake --install ./build", + "demo:ctest": "ctest --test-dir ./build", + "demo:cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./install", + "demo:cdist": "cpack --config ./build/CPackConfig.cmake -B ./install" }, "files": [ "lib", diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index eeebb4f1..78d1838e 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -368,7 +368,7 @@ cmake-js::node-addon-api cmake-js::cmake-js ]=============================================================================]# -if(CMAKEJS_USING_NODE_DEV) +if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or higher # acquire if needed... if(NOT DEFINED NODE_EXECUTABLE) @@ -479,7 +479,7 @@ if(CMAKEJS_USING_NODE_DEV) list(APPEND CMAKEJS_TARGETS node-dev) endif() -if(CMAKEJS_USING_NODE_API) +if(CMAKEJS_USING_NODE_API) # user did 'cmake-js configure --link-level=1' or higher # Acquire if needed... if(NOT DEFINED NODE_API_HEADERS_DIR) # Why the NODE_API_* namespace? Because 'node-api-headers' from vcpkg also provides this exact var, so we can help our users from vcpkg-land avoid picking up headers they already have ; but, we still need to process those headers into our target(s) for them! @@ -528,7 +528,7 @@ if(CMAKEJS_USING_NODE_API) list(APPEND CMAKEJS_TARGETS node-api) endif() -if(CMAKEJS_USING_NODE_ADDON_API) +if(CMAKEJS_USING_NODE_ADDON_API) # user did 'cmake-js configure --link-level=2' or higher # Acquire if needed... if(NOT DEFINED NODE_ADDON_API_DIR) # Why the NODE_ADDON_API_* namespace? Because 'node-addon-api' from vcpkg also provides this exact var, so we can help our users from vcpkg-land avoid picking up headers they already have ; but, we still need to process those headers into our target(s) for them! @@ -577,7 +577,8 @@ if(CMAKEJS_USING_NODE_ADDON_API) list(APPEND CMAKEJS_TARGETS node-addon-api) endif() -if(CMAKEJS_USING_CMAKEJS) +if(CMAKEJS_USING_CMAKEJS) # user did 'cmake-js configure --link-level=3', or did not specify a link level (fallback case) + # CMakeJS API - requires Node Addon API (C++), resolves the full Napi Addon dependency chain # cmake-js::cmake-js add_library (cmake-js INTERFACE) From f286fbe9a6ab099574a60449d5576349eaa95274 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 08:53:20 +0100 Subject: [PATCH 40/92] Added alias commands for packing both API's (Javascript and CMake) for distro --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 20f03cbd..5f61ed5e 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,10 @@ "demo:install": "cmake --install ./build", "demo:ctest": "ctest --test-dir ./build", "demo:cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./install", - "demo:cdist": "cpack --config ./build/CPackConfig.cmake -B ./install" + "demo:cdist": "cpack --config ./build/CPackConfig.cmake -B ./install", + + "api:pack-js": "cpack --config ./build/CPackSourceConfig.cmake -B ./dist", + "api:pack-cmake": "cpack --config ./build/CPackConfig.cmake -B ./dist" }, "files": [ "lib", From 5a7d19708bb7e80c608ada87e4e4ea39a6324e73 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 08:59:02 +0100 Subject: [PATCH 41/92] Added 'dist' to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 60687d5a..b9eaf4d5 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ node_modules # Build build +dist install package-lock.json yarn.lock From f51693b166049d5aef2e0850459505dbba6d42f7 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 09:00:18 +0100 Subject: [PATCH 42/92] Added 'dist' to CPack ignore --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7319992..05268e24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,7 @@ console.log(`Napi Version: ${demo.version()}`); /.cache /.config /.local + /dist /doc /docs #/bin From 517f27b368e62d7b11e67e72fae5762e1753277a Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 10:21:26 +0100 Subject: [PATCH 43/92] Fixed libnode-dev header lookup and install. We can now distro from cmake-js to CMake to npm! :) --- CMakeLists.txt | 8 +- share/cmake/CMakeJS.cmake | 268 ++++++++++++++++++++++---------------- 2 files changed, 162 insertions(+), 114 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05268e24..6de846b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,9 +117,9 @@ console.log(`Napi Version: ${demo.version()}`); ) if(BUILD_TESTS) + # include(CTest) # If you want to see all the test dashboard targets, uncomment this enable_testing() include("${CMAKE_CURRENT_LIST_DIR}/tests/demo/tests.cmake") - include(CTest) # If you want to see all the test dashboard targets, uncomment this include() endif() # offer a basic/messy CPack @@ -131,9 +131,9 @@ console.log(`Napi Version: ${demo.version()}`); # set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... set(CPACK_PACKAGE_VENDOR "cmake-js") - set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") - set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") # Compiled binary distribution - set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source") # No system spec as this is un-compiled source file distribution + set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") + set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") # Multi-platform CMake API distribution + set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source") # No system spec as this is un-compiled source file distribution (i.e., the Javascript API) set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_DESCRIPTION}) set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_VERSION_MINOR}) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 78d1838e..7684f261 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -171,31 +171,36 @@ endif () # 'always-on' codeblock; we provide the following blob, no matter the config. # relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) -file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") -file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") -unset(_CMAKE_JS_INC_FILES) - -set(_NODE_DEV_DEPS "") -list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) -foreach(_DEP IN LISTS _NODE_DEV_DEPS) - if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") - file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/*.h") - file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") - unset(_CMAKE_JS_INC_FILES) - endif() -endforeach() -unset(_NODE_DEV_DEPS) + +# This is too specific... see comments around the node-dev target fileset +# file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") +# file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") +# unset(_CMAKE_JS_INC_FILES) + +# set(_NODE_DEV_DEPS "") +# list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) +# foreach(_DEP IN LISTS _NODE_DEV_DEPS) +# if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") +# file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/**/*.h") +# file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") +# unset(_CMAKE_JS_INC_FILES) +# endif() +# endforeach() +# unset(_NODE_DEV_DEPS) + +# do this instead! +install(DIRECTORY "${CMAKE_JS_INC}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node" FILES_MATCHING PATTERN "*.h") # relocate... (this is crucial to get right for 'install()' to work on user's addons) -# set(CMAKE_JS_INC "") +set(CMAKE_JS_INC "") -# # target include directories (as if 'node-dev' were an isolated CMake project...) -# set(CMAKE_JS_INC -# $ -# $ -# ) +# target include directories (as if 'node-dev' were an isolated CMake project...) +set(CMAKE_JS_INC + $ + $ +) -# set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) +set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) set(CMAKE_JS_SRC ${CMAKE_JS_SRC} CACHE FILEPATH "cmake-js source file." FORCE) set(CMAKE_JS_LIB ${CMAKE_JS_LIB} CACHE FILEPATH "cmake-js lib file." FORCE) @@ -386,95 +391,138 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig target_link_libraries (node-dev INTERFACE ${CMAKE_JS_LIB}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) set_target_properties (node-dev PROPERTIES VERSION ${NODE_VERSION}) - set(NODE_DEV_FILES "") - list(APPEND NODE_DEV_FILES - # NodeJS core - "node_buffer.h" - "node_object_wrap.h" - "node_version.h" - "node.h" - # NodeJS addon - "node_api.h" - "node_api_types.h" - "js_native_api.h" - "js_native_api_types.h" - # uv - "uv.h" - # v8 - "v8config.h" - "v8-array-buffer.h" - "v8-callbacks.h" - "v8-container.h" - "v8-context.h" - "v8-data.h" - "v8-date.h" - "v8-debug.h" - "v8-embedder-heap.h" - "v8-embedder-state-scope.h" - "v8-exception.h" - "v8-extension.h" - "v8-forward.h" - "v8-function-callback.h" - "v8-function.h" - "v8-initialization.h" - "v8-internal.h" - "v8-isolate.h" - "v8-json.h" - "v8-local-handle.h" - "v8-locker.h" - "v8-maybe.h" - "v8-memory-span.h" - "v8-message.h" - "v8-microtask-queue.h" - "v8-microtask.h" - "v8-object.h" - "v8-persistent-handle.h" - "v8-platform.h" - "v8-primitive-object.h" - "v8-primitive.h" - "v8-profiler.h" - "v8-promise.h" - "v8-proxy.h" - "v8-regexp.h" - "v8-script.h" - "v8-snapshot.h" - "v8-statistics.h" - "v8-template.h" - "v8-traced-handle.h" - "v8-typed-array.h" - "v8-unwinder.h" - "v8-util.h" - "v8-value-serializer-version.h" - "v8-value-serializer.h" - "v8-value.h" - "v8-version.h" - "v8-version-string.h" - "v8-wasm.h" - "v8-wasm-trap-handler-posix.h" - "v8-wasm-trap-handler-win.h" - "v8-weak-callback.h" - "v8.h" - "v8-config.h" - # zlib - "zconf.h" - "zlib.h" - ) - - foreach(FILE IN LISTS NODE_DEV_FILES) - if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node/${FILE}") - message(DEBUG "Found NodeJS developer header: ${FILE}") - target_sources(node-dev INTERFACE - FILE_SET node_dev_INTERFACE_HEADERS - TYPE HEADERS - BASE_DIRS - $ - $ - FILES - $ - $ - ) - endif() - endforeach() + # TODO: this list would be un-manageable for all iterations of NodeJS dev files + # across versions; even minor versions seem to have lots of different files! + # Fortunately for us, HEADERS should never really go to 'target_sources', + # because HEADERS are *not supposed to be compiled*, they are only used + # for lookup by the compiler. They are just symbolic, nothing more. + # + # The two 'correct' mechanisms for adding headers to a target in CMake are: + # + # Modern CMake: you can put them in 'target_sources()' - one by one, never + # by globbing expressions! - if you create a FILE_SET of TYPE HEADERS + # and follow the strict naming conventions and BASE_DIRS thing. + # + # Classic CMake: You don't actually pass any header files to your target + # explicitly; instead, you just put their path on 'target_include_directories()', + # and your compiler/CMake/IDE/intellisense will loop up the file relative to + # that path. So you can then '#include ' + # + # This manual listing of files maybe wouldnt be so bad if we just globbed + # everything recursively and dumped them all into one massive dir. But (aHa!), + # they have all been authored to '#include ', and for + # very good reason - these libnode-dev headers contain familiar libs such as + # openssl, but these ones are doctored by team NodeJS according to their custom + # needs, and that's why NodeJS ships these third-party copies under their own + # include line. + # + # We can glob the files; we can even have CMake just pick up the base dir and + # throw that around; but, we'd have to generate the entire FILESET below with + # a fully-maintained filetree and include line, specific to whichever version + # of NodeJS that cmake.js has picked up for us. + # + # So instead of taking on unthinkable complexity of maintaining this, we can + # just pass the relocatable '*_INCLUDE_DIR' from under these copied files that + # CMake is already passing around, and just pass that to our target's + # 'target_include_directories()' and let it do the 'Classical lookup-only header' + # approach. We lose nothing, really, since none of these files are supposed to be + # compiled in. CMake only ever just needed to know where the base dir is that + # it can instruct the compiler and linker do their symbol lookups from, and + # the dir we're passing in has been made CMake-relocatable thanks to it's + # BUILD_ and INSTALL_ interfaces and generator expressions. That is really + # the definition of an INTERFACE library in CMake parlance anyway - a CMake + # - formatted header-only library :) + + + # set(NODE_DEV_FILES "") + # list(APPEND NODE_DEV_FILES + # # NodeJS core + # "node_buffer.h" + # "node_object_wrap.h" + # "node_version.h" + # "node.h" + # # NodeJS addon + # "node_api.h" + # "node_api_types.h" + # "js_native_api.h" + # "js_native_api_types.h" + # # uv + # "uv.h" + # # v8 + # "v8config.h" + # "v8-array-buffer.h" + # "v8-callbacks.h" + # "v8-container.h" + # "v8-context.h" + # "v8-data.h" + # "v8-date.h" + # "v8-debug.h" + # "v8-embedder-heap.h" + # "v8-embedder-state-scope.h" + # "v8-exception.h" + # "v8-extension.h" + # "v8-forward.h" + # "v8-function-callback.h" + # "v8-function.h" + # "v8-initialization.h" + # "v8-internal.h" + # "v8-isolate.h" + # "v8-json.h" + # "v8-local-handle.h" + # "v8-locker.h" + # "v8-maybe.h" + # "v8-memory-span.h" + # "v8-message.h" + # "v8-microtask-queue.h" + # "v8-microtask.h" + # "v8-object.h" + # "v8-persistent-handle.h" + # "v8-platform.h" + # "v8-primitive-object.h" + # "v8-primitive.h" + # "v8-profiler.h" + # "v8-promise.h" + # "v8-proxy.h" + # "v8-regexp.h" + # "v8-script.h" + # "v8-snapshot.h" + # "v8-statistics.h" + # "v8-template.h" + # "v8-traced-handle.h" + # "v8-typed-array.h" + # "v8-unwinder.h" + # "v8-util.h" + # "v8-value-serializer-version.h" + # "v8-value-serializer.h" + # "v8-value.h" + # "v8-version.h" + # "v8-version-string.h" + # "v8-wasm.h" + # "v8-wasm-trap-handler-posix.h" + # "v8-wasm-trap-handler-win.h" + # "v8-weak-callback.h" + # "v8.h" + # "v8-config.h" + # # zlib + # "zconf.h" + # "zlib.h" + # ) + + # foreach(FILE IN LISTS NODE_DEV_FILES) + # if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/node/${FILE}") + # message(DEBUG "Found NodeJS developer header: ${FILE}") + # target_sources(node-dev INTERFACE + # FILE_SET node_dev_INTERFACE_HEADERS + # TYPE HEADERS + # BASE_DIRS + # $ + # $ + # FILES + # $ + # $ + # ) + # endif() + # endforeach() list(APPEND CMAKEJS_TARGETS node-dev) endif() From fede10ee100c177004a2f49dc063a999ab3d1a63 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sat, 3 Feb 2024 16:59:06 +0100 Subject: [PATCH 44/92] globbing headers is a nad idea... trying better strategy --- share/cmake/CMakeJS.cmake | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 7684f261..cf069e93 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -188,22 +188,23 @@ endif () # endforeach() # unset(_NODE_DEV_DEPS) -# do this instead! -install(DIRECTORY "${CMAKE_JS_INC}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node" FILES_MATCHING PATTERN "*.h") +file(COPY "${CMAKE_JS_INC}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") -# relocate... (this is crucial to get right for 'install()' to work on user's addons) +# # relocate... (this is crucial to get right for 'install()' to work on user's addons) set(CMAKE_JS_INC "") -# target include directories (as if 'node-dev' were an isolated CMake project...) -set(CMAKE_JS_INC - $ - $ -) +# # target include directories (as if 'node-dev' were an isolated CMake project...) +# set(CMAKE_JS_INC +# $ +# $ +# ) -set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) +# set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) +set(CMAKE_JS_INC "${CMAKE_CURRENT_BINARY_DIR}/include/node") set(CMAKE_JS_SRC ${CMAKE_JS_SRC} CACHE FILEPATH "cmake-js source file." FORCE) set(CMAKE_JS_LIB ${CMAKE_JS_LIB} CACHE FILEPATH "cmake-js lib file." FORCE) +# this block is just for IDE support (Visual Studio will group these files) set(CMAKE_JS_INC_FILES "") # prevent repetitive globbing on each run file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") @@ -288,7 +289,7 @@ function(cmakejs_acquire_napi_c_files) COMMAND "${NODE_EXECUTABLE}" -p "require('node-api-headers').include_dir" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE NODE_API_HEADERS_DIR - # COMMAND_ERROR_IS_FATAL ANY + # COMMAND_ERROR_IS_FATAL ANY - crashes on ARM64 builds? ) string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") @@ -298,16 +299,18 @@ function(cmakejs_acquire_napi_c_files) file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") unset(_NODE_API_INC_FILES) + unset(NODE_API_HEADERS_DIR CACHE) # target include directories (as if 'node-api-headers' were an isolated CMake project...) set(NODE_API_HEADERS_DIR $ $ ) - set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} CACHE PATH "Node API Headers directory." FORCE) + set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} PARENT_SCOPE) + # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! set(NODE_API_INC_FILES "") file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE FILEPATH "Node API Header files." FORCE) + set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" PARENT_SCOPE) source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") if(VERBOSE) @@ -330,7 +333,7 @@ function(cmakejs_acquire_napi_cpp_files) COMMAND "${NODE_EXECUTABLE}" -p "require('node-addon-api').include" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE NODE_ADDON_API_DIR - # COMMAND_ERROR_IS_FATAL ANY + # COMMAND_ERROR_IS_FATAL ANY -these vars seem to error on ARM64 builds ...? ) string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}") @@ -340,6 +343,7 @@ function(cmakejs_acquire_napi_cpp_files) file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") unset(_NODE_ADDON_API_INC_FILES) + unset(NODE_ADDON_API_DIR CACHE) # target include directories (as if 'node-addon-api' were an isolated CMake project...) set(NODE_ADDON_API_DIR $ @@ -347,10 +351,10 @@ function(cmakejs_acquire_napi_cpp_files) ) set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} PARENT_SCOPE) + # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! set(NODE_ADDON_API_INC_FILES "") file(GLOB_RECURSE NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") set(NODE_ADDON_API_INC_FILES ${NODE_ADDON_API_INC_FILES} PARENT_SCOPE) - # set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE STRING "Node Addon API Header files." FORCE) source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") if(VERBOSE) @@ -536,7 +540,7 @@ if(CMAKEJS_USING_NODE_API) # user did 'cmake-js configure --link-level=1' or hig message(DEBUG "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") if(NOT DEFINED NODE_API_INC_FILES) file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - source_group("Node Addon API (C)" FILES ${NODE_API_INC_FILES}) + source_group("Node Addon API (C)" FILES ${NODE_API_INC_FILES}) # IDE only, don't pass this to target_sources()! endif() set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE STRING "Node API Header files." FORCE) endif() From f689132ab7152a46519018e4e6b4f836f2c7830a Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 02:34:51 +0100 Subject: [PATCH 45/92] I think it's done. Will await conf that this works. --- share/cmake/CMakeJS.cmake | 83 ++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index cf069e93..4f7c7317 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -294,10 +294,11 @@ function(cmakejs_acquire_napi_c_files) string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") # relocate... - set(_NODE_API_INC_FILES "") - file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") - unset(_NODE_API_INC_FILES) + # set(_NODE_API_INC_FILES "") + # file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + # file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") + # unset(_NODE_API_INC_FILES) + file(COPY "${NODE_API_HEADERS_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") unset(NODE_API_HEADERS_DIR CACHE) # target include directories (as if 'node-api-headers' were an isolated CMake project...) @@ -338,10 +339,11 @@ function(cmakejs_acquire_napi_cpp_files) string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}") # relocate... - set(_NODE_ADDON_API_INC_FILES "") - file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") - unset(_NODE_ADDON_API_INC_FILES) + # set(_NODE_ADDON_API_INC_FILES "") + # file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + # file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") + # unset(_NODE_ADDON_API_INC_FILES) + file(COPY "${NODE_ADDON_API_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") unset(NODE_ADDON_API_DIR CACHE) # target include directories (as if 'node-addon-api' were an isolated CMake project...) @@ -390,10 +392,14 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig # cmake-js::node-dev add_library (node-dev INTERFACE) add_library (cmake-js::node-dev ALIAS node-dev) - target_include_directories (node-dev INTERFACE ${CMAKE_JS_INC}) + # target_include_directories (node-dev INTERFACE ${CMAKE_JS_INC}) target_sources (node-dev INTERFACE ${CMAKE_JS_SRC}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) target_link_libraries (node-dev INTERFACE ${CMAKE_JS_LIB}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) set_target_properties (node-dev PROPERTIES VERSION ${NODE_VERSION}) + target_include_directories (node-dev INTERFACE + $ + $ + ) # TODO: this list would be un-manageable for all iterations of NodeJS dev files # across versions; even minor versions seem to have lots of different files! @@ -527,6 +533,33 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig # ) # endif() # endforeach() + set(_CMAKE_JS_INC_FILES "") + # file(GLOB_RECURSE _CMAKE_JS_INC_FILES CONFIGURE_DEPENDS "${CMAKE_JS_INC}" "*.h") + + # Not quite working, but not breaking anything... + foreach(input IN ITEMS "${CMAKE_JS_INC}") + + _cmakejs_normalize_path(input) + get_filename_component(file_abs_path "${input}" ABSOLUTE) + get_filename_component(file_name "${input}" NAME) + file(RELATIVE_PATH file_rel_path "${CMAKE_CURRENT_BINARY_DIR}" "${file_abs_path}/${file_name}") + + message(STATUS "Found NodeJS development header: ${file_name}") + message(STATUS "file_abs_path: ${file_abs_path}") + message(STATUS "file_rel_path: ${file_rel_path}") + target_sources(node-dev INTERFACE + FILE_SET node_dev_INTERFACE_HEADERS + TYPE HEADERS + BASE_DIRS + $ + $ + FILES + $ + $ + ) + + endforeach() + unset(_CMAKE_JS_INC_FILES) list(APPEND CMAKEJS_TARGETS node-dev) endif() @@ -1090,7 +1123,12 @@ file(COPY "${_CMAKEJS_SCRIPT}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/share/cm # 'install' target. unset(CMAKEJS_INC_DIR) -set(CMAKEJS_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for include files, a relative path that will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path.") +set(CMAKEJS_INC_DIR + $ + $ + CACHE PATH "Installation directory for include files, a relative path that will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path." + FORCE +) # copy headers (and definitions?) to build dir for distribution if(CMAKEJS_USING_NODE_DEV) install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") @@ -1309,3 +1347,28 @@ unset(_version) # unset (CMAKE_JS_LIB) # unset (CMAKE_JS_VERSION) # unset (CMAKE_JS_EXECUTABLE) + +# function(source_maker) + +# foreach(input IN CMAKE_JS_INC) + +# _cmakejs_normalize_path(input) +# get_filename_component(file_abs_path "${input}" ABSOLUTE) +# get_filename_component(file_name "${input}" NAME) +# file(RELATIVE_PATH file_rel_path "${CMAKE_CURRENT_BINARY_DIR}/include" "${file_abs_path}") + +# message(DEBUG "Found NodeJS development header: ${input}") +# target_sources(node-dev INTERFACE +# FILE_SET node_dev_INTERFACE_HEADERS +# TYPE HEADERS +# BASE_DIRS +# $ +# $ +# FILES +# $ +# $ +# ) + +# endforeach() + +# endfunction() From 9c9ea22a315ca462431a4f3a13da756299639eea Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 03:31:18 +0100 Subject: [PATCH 46/92] clarity --- share/cmake/CMakeJS.cmake | 44 ++++++++++++--------------------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 4f7c7317..5eb12fc1 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -48,7 +48,7 @@ cmake_dependent_option(CMAKEJS_USING_NODE_ADDON_API "Supply cmake-js::node-add cmake_dependent_option(CMAKEJS_USING_CMAKEJS "Supply cmake-js::cmake-js target for linkage" ON CMAKEJS_USING_NODE_ADDON_API OFF) # TODO: re; the above. -# I propose instead of exposing all four "CMAKEJS_USING_*" options at once against and +# I propose instead of exposing all four "CMAKEJS_USING_*" options at once and # allowing illogical combinations of dependencies, we instead setup a new CLI arg # from the Javascript side, '--link-level'. @@ -210,6 +210,7 @@ file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/*.h") file(GLOB_RECURSE CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/node/**/*.h") set(CMAKE_JS_INC_FILES "${CMAKE_JS_INC_FILES}" CACHE STRING "" FORCE) source_group("cmake-js v${_version} Node ${NODE_VERSION}" FILES "${CMAKE_JS_INC_FILES}") +# 'source_group' is the IDE support. Not related to 'target_sources' - two different things! # Log the vars to the console for sanity... if(VERBOSE) @@ -289,7 +290,7 @@ function(cmakejs_acquire_napi_c_files) COMMAND "${NODE_EXECUTABLE}" -p "require('node-api-headers').include_dir" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE NODE_API_HEADERS_DIR - # COMMAND_ERROR_IS_FATAL ANY - crashes on ARM64 builds? + # COMMAND_ERROR_IS_FATAL ANY - crashes on ARM64 builds? unfortunate! ) string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") @@ -313,6 +314,11 @@ function(cmakejs_acquire_napi_c_files) file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" PARENT_SCOPE) source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") + # end IDE support codeblock + # it's not a 'source_group' for targets! + # VS users will see the above globbed headers as a filegroup named "Node API (C)" + # and that is literally all that this 'source_group' function does. + # it is not the same as 'target_sources' - so, globbing was ok here! if(VERBOSE) message(STATUS "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") @@ -343,6 +349,8 @@ function(cmakejs_acquire_napi_cpp_files) # file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") # file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") # unset(_NODE_ADDON_API_INC_FILES) + + # much easier this way than the above... file(COPY "${NODE_ADDON_API_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") unset(NODE_ADDON_API_DIR CACHE) @@ -392,7 +400,6 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig # cmake-js::node-dev add_library (node-dev INTERFACE) add_library (cmake-js::node-dev ALIAS node-dev) - # target_include_directories (node-dev INTERFACE ${CMAKE_JS_INC}) target_sources (node-dev INTERFACE ${CMAKE_JS_SRC}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) target_link_libraries (node-dev INTERFACE ${CMAKE_JS_LIB}) # tip: don't enclose this in strings! (or it won't be null if the file is nonexistent) set_target_properties (node-dev PROPERTIES VERSION ${NODE_VERSION}) @@ -544,9 +551,9 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig get_filename_component(file_name "${input}" NAME) file(RELATIVE_PATH file_rel_path "${CMAKE_CURRENT_BINARY_DIR}" "${file_abs_path}/${file_name}") - message(STATUS "Found NodeJS development header: ${file_name}") - message(STATUS "file_abs_path: ${file_abs_path}") - message(STATUS "file_rel_path: ${file_rel_path}") + message(DEBUG "Found NodeJS development header: ${file_name}") + message(DEBUG "file_abs_path: ${file_abs_path}") + message(DEBUG "file_rel_path: ${file_rel_path}") target_sources(node-dev INTERFACE FILE_SET node_dev_INTERFACE_HEADERS TYPE HEADERS @@ -1347,28 +1354,3 @@ unset(_version) # unset (CMAKE_JS_LIB) # unset (CMAKE_JS_VERSION) # unset (CMAKE_JS_EXECUTABLE) - -# function(source_maker) - -# foreach(input IN CMAKE_JS_INC) - -# _cmakejs_normalize_path(input) -# get_filename_component(file_abs_path "${input}" ABSOLUTE) -# get_filename_component(file_name "${input}" NAME) -# file(RELATIVE_PATH file_rel_path "${CMAKE_CURRENT_BINARY_DIR}/include" "${file_abs_path}") - -# message(DEBUG "Found NodeJS development header: ${input}") -# target_sources(node-dev INTERFACE -# FILE_SET node_dev_INTERFACE_HEADERS -# TYPE HEADERS -# BASE_DIRS -# $ -# $ -# FILES -# $ -# $ -# ) - -# endforeach() - -# endfunction() From 98bbe53a4adda067755649598ba93ecd8f53f995 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 04:18:17 +0100 Subject: [PATCH 47/92] Last comments/todos/clarity. No code changes. --- share/cmake/CMakeJS.cmake | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 5eb12fc1..8bbe5a5c 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -200,7 +200,7 @@ set(CMAKE_JS_INC "") # ) # set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) -set(CMAKE_JS_INC "${CMAKE_CURRENT_BINARY_DIR}/include/node") +set(CMAKE_JS_INC "${CMAKE_CURRENT_BINARY_DIR}/include/node") # dont CACHE FORCE this yet, it will change again soon... set(CMAKE_JS_SRC ${CMAKE_JS_SRC} CACHE FILEPATH "cmake-js source file." FORCE) set(CMAKE_JS_LIB ${CMAKE_JS_LIB} CACHE FILEPATH "cmake-js lib file." FORCE) @@ -307,7 +307,7 @@ function(cmakejs_acquire_napi_c_files) $ $ ) - set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} PARENT_SCOPE) + set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} PARENT_SCOPE) # dont wrap this one in quotes; it breaks! # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! set(NODE_API_INC_FILES "") @@ -585,7 +585,7 @@ if(CMAKEJS_USING_NODE_API) # user did 'cmake-js configure --link-level=1' or hig set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" CACHE STRING "Node API Header files." FORCE) endif() - # Node API (C) - requires NodeJS system installation headers + # Node API (C) - requires NodeJS developer headers target, cmake-js::node-dev # cmake-js::node-api add_library (node-api INTERFACE) add_library (cmake-js::node-api ALIAS node-api) @@ -629,12 +629,12 @@ if(CMAKEJS_USING_NODE_ADDON_API) # user did 'cmake-js configure --link-level=2' message(DEBUG "NODE_ADDON_API_DIR: ${NODE_ADDON_API_DIR}") if(NOT DEFINED NODE_ADDON_API_INC_FILES) file(GLOB_RECURSE NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") + source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") # just for IDE support; another misleading function name! endif() set(NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_INC_FILES}" CACHE STRING "Node Addon API Header files." FORCE) endif() - # Node Addon API (C++) - requires Node API (C) + # Node Addon API (C++) - requires Node API (C) target, cmake-js::node-api # cmake-js::node-addon-api add_library (node-addon-api INTERFACE) add_library (cmake-js::node-addon-api ALIAS node-addon-api) @@ -671,7 +671,7 @@ endif() if(CMAKEJS_USING_CMAKEJS) # user did 'cmake-js configure --link-level=3', or did not specify a link level (fallback case) - # CMakeJS API - requires Node Addon API (C++), resolves the full Napi Addon dependency chain + # CMakeJS API - requires Node Addon API (C++) target, cmake-js::node-api; resolves the full Napi Addon dependency chain # cmake-js::cmake-js add_library (cmake-js INTERFACE) add_library (cmake-js::cmake-js ALIAS cmake-js) @@ -680,7 +680,7 @@ if(CMAKEJS_USING_CMAKEJS) # user did 'cmake-js configure --link-level=3', or did target_compile_features (cmake-js INTERFACE cxx_nullptr) # Signal a basic C++11 feature to require C++11. set_target_properties (cmake-js PROPERTIES VERSION 7.3.3) set_target_properties (cmake-js PROPERTIES SOVERSION 7) - set_target_properties (cmake-js PROPERTIES COMPATIBLE_INTERFACE_STRING CMakeJS_MAJOR_VERSION) + set_target_properties (cmake-js PROPERTIES COMPATIBLE_INTERFACE_STRING CMakeJS_MAJOR_VERSION) # This is a hook for dependency version management, not fully implemented here, just a start... # Generate definitions if(MSVC) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." FORCE) @@ -689,6 +689,23 @@ if(CMAKEJS_USING_CMAKEJS) # user did 'cmake-js configure --link-level=3', or did endif() endif() + # TODO: DELAY_HOOK thing - here? idea of this target is, 'automatically give me everything, all set up'. + # I propose that the remainder of the details of Node Addon CMake config which I've not explored yet + # should be well-considered as to which target we supply a pre-config for. Wherever you define this stuff, + # it will probably propogate down to this target level anyway, due to this one carrying a link line + # to all the other targets via it's logical dependency chain. I just think that if you're not using our + # nice 'create()' function, you're an intermediate/advanced CMake dev who would rather spec that + # stuff themselves. An additional cosideration is making sure we only apply stuff to our targets, + # and never just define things globally. Meaning, always use 'target_add_definitions()' and never ever + # use regular 'add_definitions()' when taking care of this. Why? Because that global function + # will propogate globally, downstream, and who knows where else (see also: : never do 'using namespace ' + # in the global namespace scope in a public C++ header file... you don't know where it will propogate, + # what it might break, or even where the issue stems from when it does arise). + # Keep in mind that PUBLIC defs on are propogated to any other targets that link with it. + # PRIVATE does not propogate. Sometimes, INTERFACE libs insist you use INTERFACE specifiers. + # so whatever is decided, it must be very well considered i.e., not arbitrary, and if CMake + # insists something, don't fight it. CMake doggedly refuses to be beaten at it's own games :) + list(APPEND CMAKEJS_TARGETS cmake-js) # Note that the below function definitions are contained inside From d1a98583f3733620f4547950ca61a97d372fbfc2 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 06:30:41 +0100 Subject: [PATCH 48/92] relocator tweak - satisfied with the installation routine now. --- share/cmake/CMakeJS.cmake | 47 ++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 8bbe5a5c..10dfeccf 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -188,7 +188,7 @@ endif () # endforeach() # unset(_NODE_DEV_DEPS) -file(COPY "${CMAKE_JS_INC}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") +file(COPY ${CMAKE_JS_INC} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") # # relocate... (this is crucial to get right for 'install()' to work on user's addons) set(CMAKE_JS_INC "") @@ -295,11 +295,10 @@ function(cmakejs_acquire_napi_c_files) string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") # relocate... - # set(_NODE_API_INC_FILES "") - # file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - # file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") - # unset(_NODE_API_INC_FILES) - file(COPY "${NODE_API_HEADERS_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") + set(_NODE_API_INC_FILES "") + file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") + unset(_NODE_API_INC_FILES) unset(NODE_API_HEADERS_DIR CACHE) # target include directories (as if 'node-api-headers' were an isolated CMake project...) @@ -345,13 +344,10 @@ function(cmakejs_acquire_napi_cpp_files) string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}") # relocate... - # set(_NODE_ADDON_API_INC_FILES "") - # file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - # file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") - # unset(_NODE_ADDON_API_INC_FILES) - - # much easier this way than the above... - file(COPY "${NODE_ADDON_API_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") + set(_NODE_ADDON_API_INC_FILES "") + file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") + unset(_NODE_ADDON_API_INC_FILES) unset(NODE_ADDON_API_DIR CACHE) # target include directories (as if 'node-addon-api' were an isolated CMake project...) @@ -541,7 +537,6 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig # endif() # endforeach() set(_CMAKE_JS_INC_FILES "") - # file(GLOB_RECURSE _CMAKE_JS_INC_FILES CONFIGURE_DEPENDS "${CMAKE_JS_INC}" "*.h") # Not quite working, but not breaking anything... foreach(input IN ITEMS "${CMAKE_JS_INC}") @@ -549,7 +544,7 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig _cmakejs_normalize_path(input) get_filename_component(file_abs_path "${input}" ABSOLUTE) get_filename_component(file_name "${input}" NAME) - file(RELATIVE_PATH file_rel_path "${CMAKE_CURRENT_BINARY_DIR}" "${file_abs_path}/${file_name}") + file(RELATIVE_PATH file_rel_path "${CMAKE_CURRENT_BINARY_DIR}" "${file_abs_path}") # /${file_name} message(DEBUG "Found NodeJS development header: ${file_name}") message(DEBUG "file_abs_path: ${file_abs_path}") @@ -558,11 +553,11 @@ if(CMAKEJS_USING_NODE_DEV) # user did 'cmake-js configure --link-level=0' or hig FILE_SET node_dev_INTERFACE_HEADERS TYPE HEADERS BASE_DIRS - $ - $ + $ # /node + $ # /node FILES - $ - $ + $ # /${file_name} + $ ) endforeach() @@ -589,7 +584,7 @@ if(CMAKEJS_USING_NODE_API) # user did 'cmake-js configure --link-level=1' or hig # cmake-js::node-api add_library (node-api INTERFACE) add_library (cmake-js::node-api ALIAS node-api) - target_include_directories (node-api INTERFACE ${NODE_API_HEADERS_DIR}) + target_include_directories (node-api INTERFACE ${NODE_API_HEADERS_DIR}) # no string enclosure here! target_link_libraries (node-api INTERFACE cmake-js::node-dev) set_target_properties (node-api PROPERTIES VERSION 6.1.0) set_target_properties (node-api PROPERTIES SOVERSION 6) @@ -1155,7 +1150,7 @@ set(CMAKEJS_INC_DIR ) # copy headers (and definitions?) to build dir for distribution if(CMAKEJS_USING_NODE_DEV) - install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node") + install(FILES ${CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node") install(TARGETS node-dev EXPORT CMakeJSTargets LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" @@ -1167,25 +1162,27 @@ if(CMAKEJS_USING_NODE_DEV) endif() if(CMAKEJS_USING_NODE_API) - install(FILES ${NODE_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-api-headers") + install(FILES ${NODE_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node-api-headers") install(TARGETS node-api EXPORT CMakeJSTargets LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node-api-headers" # Having trouble setting this correctly + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node-api-headers" # this issue stems from their package.json, not our code... but guess who needs to fix it now :) FILE_SET node_api_INTERFACE_HEADERS ) endif() if(CMAKEJS_USING_NODE_ADDON_API) - install(FILES ${NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDE_DIR}/node-addon-api") + install(FILES ${NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node-addon-api") install(TARGETS node-addon-api EXPORT CMakeJSTargets LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node-addon-api" + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/node-addon-api" FILE_SET node_addon_api_INTERFACE_HEADERS ) endif() From d0b98a8700aea118d595cb7733fb74aa73fc8bc8 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 07:25:22 +0100 Subject: [PATCH 49/92] better demo install routine --- CMakeLists.txt | 139 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6de846b3..8137b727 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,74 @@ console.log(`Napi Version: ${demo.version()}`); -- -- ]==]) + + # offer a basic installer for our demo. + + # NOTE: this is part of the DEMO, I am NOT suggesting + # cmake-js should pack and ship any addons of it's own! + # this entire CMakeLists.txt is just purely about proof + # of concept. + + # the following demonstrates how users can + # easily ship either their source that automatically + # carry all dependencies (required headers), + # which is useful for e.g., offline machines, + # they can build-at-home or in a VM or wokflow run + # targeting a specific OS/platform/arch which + # they don't have access to, and can retrieve + # the built binaries in a tar file, then + # send their pre-built, pre-packaged addon(s) + # down to their remote servers, which don't + # have C++ compilers or CMake on them. + + include(GNUInstallDirs) # dont be alarmed by this - it wont install to your system! + + export ( + TARGETS demo + FILE share/cmake/DemoTargets.cmake + NAMESPACE CMakeJS:: + ) + + # install 'CMakeJSTargets' export file + install( + EXPORT DemoTargets + FILE DemoTargets.cmake + NAMESPACE CMakeJS:: + DESTINATION lib/cmake/CMakeJS + ) + + install(TARGETS demo + EXPORT DemoTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + ) + + + file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/DemoConfig.cmake.in" [==[ +@PACKAGE_INIT@ + +include (${CMAKE_CURRENT_LIST_DIR}/DemoTargets.cmake) + +check_required_components (cmake-js) + +]==]) + + # create cmake config file + configure_package_config_file ( + "${CMAKE_CURRENT_BINARY_DIR}/DemoConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/DemoConfig.cmake" + INSTALL_DESTINATION + "${CMAKE_INSTALL_LIBDIR}/cmake/Demo" + ) + # generate the version file for the cmake config file + write_basic_package_version_file ( + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/DemoConfigVersion.cmake" + VERSION 7.3.3 + COMPATIBILITY AnyNewerVersion + ) + # copy the demo types file(COPY "${CMAKE_CURRENT_LIST_DIR}/lib/demo.node.js" @@ -109,13 +177,76 @@ console.log(`Napi Version: ${demo.version()}`); "${PROJECT_BINARY_DIR}/lib" ) - # copy the CMake API - file(COPY - "${CMAKE_CURRENT_LIST_DIR}/share/cmake/CMakeJS.cmake" + install(FILES + "${PROJECT_BINARY_DIR}/lib/demo.node.js" + "${PROJECT_BINARY_DIR}/lib/demo.node.ts" + "${PROJECT_BINARY_DIR}/lib/demo.node.d.ts" DESTINATION - "${PROJECT_BINARY_DIR}/share" + "${CMAKE_INSTALL_LIBDIR}" ) + # Still seems like a lot of code for builders, so why + # did cmake-js make the effort of defining export() + # and install() at API level? + # Because if I want to actually achieve any of those + # remote scenarios highlighted above, I'd have to do + # all this, for my addon, AND for all the cmake-js + # targets. Since the kind cmake-js devs already + # did their job fully at API level, whoever builds + # an addon with it will find that... their entire + # addon project's dependancies are already fully + # relocatable. + # Considering those can be quite a lot of files + # (libnode-dev...), not to mention they can be + # tricky to locate - and to point your IDE at correctly, + # cmake-js users looking to implement CMake's export() + # and install() stuff on their addons - for whatever + # reasons they have (I mentioned a few) - will + # likely be delighted that so much heavy work has + # been done for them, by a slick combination of + # it's two API's: + # + # - cmake-js CLI controls their project with + # it's super handy alias-like commands + # + # - whatever they do and wherever they point + # their CMake outputs and inputs at, CMake itself + # will simply go "cmake-js::node-api? I got that + # right here." and just "sort it all out" for the user + # + # - one means of sorting it out" is, cmake-js CLI + # will kick in during config, determine if we need + # headers (and which, according to --link-level) + # and baiscally be a Node Addon API package manager + # for us, resolving our deps and handing them over + # to CMake to manage them + # + # - the CMake API will make building an addon a + # very straightforward affair, should they choose + # to either use our addon creation functions, or + # just link to a cmake-js:: target to get everything + # "sorted out" (the previous steps automate this) + # + # - if they want to pack and ship their addon as + # source, as a prebuilt binary, or some custom + # combo, our API is flexible enough to not burden + # them with manually resolving all those previous + # steps before they can take care of their own targets + # + # The beautiful part in the whole process is combining + # the npm-like workflow with the power of a whole C/C++ + # compiler/linker toolchain. Developers from JS and + # developers from C++ should bot feel equally at home, + # whether they want to run CMake native commands or use + # the efficient CLI (and the obvious answer is...! :) ) + # + # The entire thing "just works", as long as they have + # cmake-js on their package.json. + # + # Thanks for reading! + # Nathan + + if(BUILD_TESTS) # include(CTest) # If you want to see all the test dashboard targets, uncomment this enable_testing() From 18ddde8c4da112393153135b8c51b5fb807a68d8 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 07:30:15 +0100 Subject: [PATCH 50/92] typo --- CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8137b727..6cc83af8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,9 +256,8 @@ check_required_components (cmake-js) # offer a basic/messy CPack # TODO: I don't think this is fully working because there is no built # addon inside the binary tar outputs. - # It's distracting from the simplicity of the demo, and only here for - # more proof of concept. Remove it if you wish, it isn't part of the - # proposed API itself at all (same with CTest above). + # If it's distracting from the simplicity of the demo, remove it if you wish; + # it isn't part of the proposed API-to-be-shipped itself at all. # set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... set(CPACK_PACKAGE_VENDOR "cmake-js") From 79d67fa5556bd5c506ace7d25bd340bb2f3b90c0 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 07:31:03 +0100 Subject: [PATCH 51/92] typo. --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cc83af8..66d7a2a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,8 +254,6 @@ check_required_components (cmake-js) endif() # offer a basic/messy CPack - # TODO: I don't think this is fully working because there is no built - # addon inside the binary tar outputs. # If it's distracting from the simplicity of the demo, remove it if you wish; # it isn't part of the proposed API-to-be-shipped itself at all. From 09c3cc95d800a9d35b411866a9238f0d510f02ff Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 07:38:34 +0100 Subject: [PATCH 52/92] typo. --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 66d7a2a4..e3298ae7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -236,12 +236,13 @@ check_required_components (cmake-js) # The beautiful part in the whole process is combining # the npm-like workflow with the power of a whole C/C++ # compiler/linker toolchain. Developers from JS and - # developers from C++ should bot feel equally at home, + # developers from C++ should both feel equally at home, # whether they want to run CMake native commands or use # the efficient CLI (and the obvious answer is...! :) ) # # The entire thing "just works", as long as they have - # cmake-js on their package.json. + # cmake-js on their package.json and configure their builds + # with it. # # Thanks for reading! # Nathan From 7513586429cd8171097b44400d12257082a8790e Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 08:19:09 +0100 Subject: [PATCH 53/92] improved clarity of intent. --- CMakeLists.txt | 82 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e3298ae7..28fa77f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,15 +4,35 @@ MIT License "CMake.js - a Node.js native addon build tool" https://github.com/cmake-js/cmake-js/blob/cmakejs_cmake_api/README.md + + + # THIS FILE IS INTENDED FOR CMAKEJS DEVELOPERS AND CONTRIBUTORS. + # + # IF YOU ARE NOT EITHER OF THE ABOVE, THEN YOU MAY SAFELY CONSIDER + # THIS FILE TO BE NON-EXISTENT. + # + # IF YOU ARE LOOKING TO BUILD YOUR OWN ADDON WITH OUR CMAKE API, PLEASE + # INSTEAD START A NEW NODEJS/CMAKE PROJECT AND ADD OUR JAVASCRIPT PACKAGE + # TO YOUR DEPENDENCIES. THEN, CONFIGURE YOUR PROJECT USING OUR CLI. + # YOU WILL BE READY TO INCLUDE NODE/NAPI HEADERS YOU REQUIRE + # AND BEGIN DEVELOPING! + # + # THE BELOW IS OUR INTERNAL PROJECT WE USE FOR TESTING OUR API ONLY + # + # THIS IS NOT INTENDED TO BE AN EXAMPLE OF HOW OUR API WORKS. + # + # PLEASE DO NOT ATTEMPT TO CREATE A NEW ADDON INSIDE THIS REPO :) + # + # PLEASE DO NOT ATTEMPT TO REPLICATE THIS CODE IN YOUR OWN ADDON :) + # + # IF YOU WOULD LIKE TO SEE AN EXAMPLE OF OUR API IN ACTION, + # PLEASE GO TO '/tests/api/hello' AND '/tests/api/hello_consumer' + # AND RUN THE COMMANDS YOU FIND IN THEIR package.json FILES. ]=============================================================================]# cmake_minimum_required (VERSION 3.15) cmake_policy (VERSION 3.15) -if (NOT DEFINED CMAKE_BUILD_TYPE) - set (CMAKE_BUILD_TYPE "Debug") -endif () - project (CMakeJS VERSION 7.3.3 DESCRIPTION "CMake.js - a Node.js native addon build tool" @@ -20,6 +40,49 @@ project (CMakeJS LANGUAGES C CXX ) +# PROOF OF CONCEPT: +# +# @Julusian +# +# CMakeJS's own targets are abstract 'INTERFACE' targets which only exist +# within CMake's build cache - so, there is nothing to compile, here. +# +# We instead offer a simple test/demonstration of our API, and reserve the +# CMakeJS vendor namespace in C++/CMake land. +# +# Everything below this point is *purely* for demonstration (and some testing) +# purposes. It makes sense that we should have a root-level CMakeLists.txt so +# so that we can make our own 'project()', but we didn't want to do this inside +# the API file because then our users would inheritently always be building +# *sub-projects* below us, which is immediately breaking expected behaviour +# before they can even begin. So... +# +# We establish a root-level CMakeLists with our own 'project()', and consume our +# own API. +# +# BUT, this file *only exists when you are sitting at this project directory*. +# Our API consumers are not *ever* being directed to this file; our package.json +# dependees are never calling these commands nor interfacing with this file. +# +# So, everything contained within this file is purely intended for running +# internal checks on our API before we ship updates to our users. This helps +# simply helps us to confirm that all is working as expected. I.e., it is +# *purely for demonstrative purposes!* +# +# THIS PROJECT IS INTENDED FOR CMAKEJS DEVELOPERS AND CONTRIBUTORS. +# +# YOU ARE WELCOME TO RUN THE SCRIPTS AND SEE HOW EVERYTHING WORKS. + +# PLEASE DO NOT ATTEMPT TO CREATE A NEW ADDON LIKE WE DID HERE :) +# SEE OUR DOCS AND OUR SAMPLE PROJECTS INSTEAD: +# +# - https://github.com/cmake-js/cmake-js/blob/cmakejs_cmake_api/README.md +# +# - '/tests/api/hello' +# - '/tests/api/hello_consumer' +# +# (RUN THE COMMANDS YOU FIND IN THEIR package.json FILES. + # (no building into our source tree is allowed) set (CMakeJS_IS_MASTER_PROJECT FALSE) if(NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)) @@ -28,13 +91,6 @@ endif() include ("${CMAKE_CURRENT_LIST_DIR}/share/cmake/CMakeJS.cmake") -# CMakeJS's own targets are abstract 'INTERFACE' targets which only exist -# within CMake's build cache - so, there is nothing to compile, here. - -# We instead offer a simple test/demonstration of our API, and reserve the -# CMakeJS vendor namespace in C++/CMake land. - -# PROOF OF CONCEPT: # If this is not a subproject... if (CMakeJS_IS_TOP_LEVEL @@ -188,14 +244,16 @@ check_required_components (cmake-js) # Still seems like a lot of code for builders, so why # did cmake-js make the effort of defining export() # and install() at API level? + # # Because if I want to actually achieve any of those # remote scenarios highlighted above, I'd have to do - # all this, for my addon, AND for all the cmake-js + # all this, for my addon(s!), AND for all the cmake-js # targets. Since the kind cmake-js devs already # did their job fully at API level, whoever builds # an addon with it will find that... their entire # addon project's dependancies are already fully # relocatable. + # # Considering those can be quite a lot of files # (libnode-dev...), not to mention they can be # tricky to locate - and to point your IDE at correctly, From 198d7427fed6c284730a8544cf90eb1d56d459f7 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 09:04:15 +0100 Subject: [PATCH 54/92] pruned dead codeblobs --- share/cmake/CMakeJS.cmake | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 10dfeccf..8ecbfb20 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -171,35 +171,10 @@ endif () # 'always-on' codeblock; we provide the following blob, no matter the config. # relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) - -# This is too specific... see comments around the node-dev target fileset -# file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/*.h") -# file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node") -# unset(_CMAKE_JS_INC_FILES) - -# set(_NODE_DEV_DEPS "") -# list(APPEND _NODE_DEV_DEPS cppgc openssl uv libplatform) -# foreach(_DEP IN LISTS _NODE_DEV_DEPS) -# if(IS_DIRECTORY "${CMAKE_JS_INC}/${_DEP}") -# file(GLOB_RECURSE _CMAKE_JS_INC_FILES "${CMAKE_JS_INC}/${_DEP}/**/*.h") -# file(COPY ${_CMAKE_JS_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node/${_DEP}") -# unset(_CMAKE_JS_INC_FILES) -# endif() -# endforeach() -# unset(_NODE_DEV_DEPS) - file(COPY ${CMAKE_JS_INC} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") # # relocate... (this is crucial to get right for 'install()' to work on user's addons) set(CMAKE_JS_INC "") - -# # target include directories (as if 'node-dev' were an isolated CMake project...) -# set(CMAKE_JS_INC -# $ -# $ -# ) - -# set(CMAKE_JS_INC ${CMAKE_JS_INC} CACHE PATH "cmake-js include directory." FORCE) set(CMAKE_JS_INC "${CMAKE_CURRENT_BINARY_DIR}/include/node") # dont CACHE FORCE this yet, it will change again soon... set(CMAKE_JS_SRC ${CMAKE_JS_SRC} CACHE FILEPATH "cmake-js source file." FORCE) set(CMAKE_JS_LIB ${CMAKE_JS_LIB} CACHE FILEPATH "cmake-js lib file." FORCE) From 955faeba7f17814b0fe0c1740134083b0024c740 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 10:06:21 +0100 Subject: [PATCH 55/92] Example project demonstrating CPack and CTest --- tests/api/hello/CMakeLists.txt | 7 +- tests/api/hello/README.md | 7 + tests/api/hello_consumer/README.md | 13 ++ .../hello_with_testing_and_packing/.gitignore | 3 + .../CMakeLists.txt | 152 ++++++++++++++++++ .../hello_with_testing_and_packing/README.md | 7 + .../hello_with_testing_and_packing/index.js | 4 + .../lib/addon.node.d.ts | 6 + .../lib/addon.node.js | 11 ++ .../lib/addon.node.ts | 17 ++ .../package.json | 31 ++++ .../src/hello/addon.cpp | 62 +++++++ .../tests/hello/hello.js | 30 ++++ .../tests/hello/tests.cmake | 22 +++ .../tests/hello/version.js | 30 ++++ tests/api/hello_with_types/CMakeLists.txt | 5 - tests/api/hello_with_types/README.md | 7 + 17 files changed, 403 insertions(+), 11 deletions(-) create mode 100644 tests/api/hello/README.md create mode 100644 tests/api/hello_consumer/README.md create mode 100644 tests/api/hello_with_testing_and_packing/.gitignore create mode 100644 tests/api/hello_with_testing_and_packing/CMakeLists.txt create mode 100644 tests/api/hello_with_testing_and_packing/README.md create mode 100644 tests/api/hello_with_testing_and_packing/index.js create mode 100644 tests/api/hello_with_testing_and_packing/lib/addon.node.d.ts create mode 100644 tests/api/hello_with_testing_and_packing/lib/addon.node.js create mode 100644 tests/api/hello_with_testing_and_packing/lib/addon.node.ts create mode 100644 tests/api/hello_with_testing_and_packing/package.json create mode 100644 tests/api/hello_with_testing_and_packing/src/hello/addon.cpp create mode 100644 tests/api/hello_with_testing_and_packing/tests/hello/hello.js create mode 100644 tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake create mode 100644 tests/api/hello_with_testing_and_packing/tests/hello/version.js create mode 100644 tests/api/hello_with_types/README.md diff --git a/tests/api/hello/CMakeLists.txt b/tests/api/hello/CMakeLists.txt index c00bee4b..65bc8354 100644 --- a/tests/api/hello/CMakeLists.txt +++ b/tests/api/hello/CMakeLists.txt @@ -1,15 +1,10 @@ cmake_minimum_required(VERSION 3.15) -# If not using cmake-js CLI, fetch the API manually -if (NOT DEFINED CMAKE_JS_VERSION) - list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") -endif () - project(hello) include(CMakeJS) cmakejs_create_napi_addon( addon - src/hello/addon.cpp + "src/hello/addon.cpp" ) diff --git a/tests/api/hello/README.md b/tests/api/hello/README.md new file mode 100644 index 00000000..e5f46ff0 --- /dev/null +++ b/tests/api/hello/README.md @@ -0,0 +1,7 @@ +## I am an addon made with Napi Addon API in C++. + +You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C++ functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/api/hello_consumer/README.md b/tests/api/hello_consumer/README.md new file mode 100644 index 00000000..c34d5970 --- /dev/null +++ b/tests/api/hello_consumer/README.md @@ -0,0 +1,13 @@ +## I am an addon consumer, apparently. + +I'm not even sure what an addon or a CMake is. I know I have CMake, NodeJS, and a C++ compiler installed on this system (and this Ninja thing?), but honestly, I have never once had to touch any of that stuff. + +I just import addons from my neighbours and they all seem to "just work" for me. I am a happy consumer! :) + +Usually, I import these addons from online package repositories in ZIP or TAR format and they all work perfectly. The only reason I am not doing that today is ,because of something about a ```CMakeJS.cmake``` script not being on the 'master' branch yet! so I have to fetch the addon from next door until then, but I'm told that everything will work exactly the same for me, when that does happen. + +Something seems to be getting built in my node_modules dir whenever I ```npm/yarn install```.... + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/api/hello_with_testing_and_packing/.gitignore b/tests/api/hello_with_testing_and_packing/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/api/hello_with_testing_and_packing/CMakeLists.txt b/tests/api/hello_with_testing_and_packing/CMakeLists.txt new file mode 100644 index 00000000..68367063 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/CMakeLists.txt @@ -0,0 +1,152 @@ +cmake_minimum_required(VERSION 3.15) + +project(vendor) + +# (must be top-level project to run tests and packing, like any other CMake project!) +set (vendor_IS_MASTER_PROJECT FALSE) +if(NOT (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)) + set (vendor_IS_MASTER_PROJECT TRUE) +endif() + +include(CMakeJS) + +# If this is not a subproject... +if (vendor_IS_TOP_LEVEL + # (and we're not building into the source tree) + AND vendor_IS_MASTER_PROJECT +) + +cmakejs_create_napi_addon ( + # CMAKEJS_ADDON_NAME + addon + # SOURCES + src/hello/addon.cpp + # NAPI_CPP_CUSTOM_NAMESPACE + NAMESPACE vendor +) + +include(GNUInstallDirs) # dont be alarmed by this - it wont install to your system! + +# CPack prep stuff... +export ( + TARGETS addon + FILE share/cmake/AddonTargets.cmake + NAMESPACE vendor:: +) + +install( + EXPORT AddonTargets + FILE addonTargets.cmake + NAMESPACE vendor:: + DESTINATION lib/cmake/vendor +) + +install(TARGETS addon + EXPORT AddonTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) + + +file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" [==[ +@PACKAGE_INIT@ + +include (${CMAKE_CURRENT_LIST_DIR}/AddonTargets.cmake) + +check_required_components (vendor) + +]==]) + +# create cmake config file +configure_package_config_file ( + "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfig.cmake" + INSTALL_DESTINATION + "${CMAKE_INSTALL_LIBDIR}/cmake/Addon" +) +# generate the version file for the cmake config file +write_basic_package_version_file ( + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfigVersion.cmake" + VERSION 1.0.0 + COMPATIBILITY AnyNewerVersion +) + +# copy the addon types +file(COPY + "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.js" + "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.ts" + "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.d.ts" + DESTINATION + "${PROJECT_BINARY_DIR}/lib" +) + +install(FILES + "${PROJECT_BINARY_DIR}/lib/addon.node.js" + "${PROJECT_BINARY_DIR}/lib/addon.node.ts" + "${PROJECT_BINARY_DIR}/lib/addon.node.d.ts" + DESTINATION + "${CMAKE_INSTALL_LIBDIR}" +) + +if(BUILD_TESTS) + # include(CTest) # If you want to see all the test dashboard targets, uncomment this + enable_testing() + include("${CMAKE_CURRENT_LIST_DIR}/tests/addon/tests.cmake") +endif() + +# set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... +set(CPACK_PACKAGE_VENDOR "vendor") +set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") +set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") # Multi-platform CMake API distribution +set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source") # No system spec as this is un-compiled source file distribution (i.e., the Javascript API) +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_DESCRIPTION}) +set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) +set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_VERSION_MINOR}) +set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_VERSION_PATCH}) +set(CPACK_PACKAGE_VERSION_TWEAK ${PROJECT_VERSION_VERSION_TWEAK}) +#set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) +set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) +set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON) +set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) +set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") # Check out CPack's 'NSIS' installer for Win32, and the others! +set(CPACK_SOURCE_IGNORE_FILES + _CPack_Packages + /*.zip + /*.tar + /*.tar.* + /.env* + /.git/* + /.cmake + /.github + /.vs + /.vscode + /.cache + /.config + /.local + /dist + /doc + /docs + #/bin + #/lib + /usr + /out + #/build + /Release + /Debug + /MinSizeRel + /RelWithDebInfo + /downloads + /installed + /install + /node_modules + /vcpkg + /.*build.* + /package-lock.json + /yarn.lock + /\\\\.DS_Store +) +include(CPack) + +endif () diff --git a/tests/api/hello_with_testing_and_packing/README.md b/tests/api/hello_with_testing_and_packing/README.md new file mode 100644 index 00000000..ecf38815 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/README.md @@ -0,0 +1,7 @@ +## I am an installable addon. + +You can build me, run some tests, and then package me up into an archive file for easy distribution, in various formats. + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/api/hello_with_testing_and_packing/index.js b/tests/api/hello_with_testing_and_packing/index.js new file mode 100644 index 00000000..25433f74 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/index.js @@ -0,0 +1,4 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const hello_with_testing_and_packing = require(`./lib/addon.node`); +module.exports = hello_with_testing_and_packing; diff --git a/tests/api/hello_with_testing_and_packing/lib/addon.node.d.ts b/tests/api/hello_with_testing_and_packing/lib/addon.node.d.ts new file mode 100644 index 00000000..89e5844c --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/lib/addon.node.d.ts @@ -0,0 +1,6 @@ +declare interface addon { + hello(): string; + version(): string; +} +declare const addon: addon; +export = addon; diff --git a/tests/api/hello_with_testing_and_packing/lib/addon.node.js b/tests/api/hello_with_testing_and_packing/lib/addon.node.js new file mode 100644 index 00000000..55c8ada8 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/lib/addon.node.js @@ -0,0 +1,11 @@ +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const addon + = require(`..${buildDir}addon +.node`); +module.exports = addon +; diff --git a/tests/api/hello_with_testing_and_packing/lib/addon.node.ts b/tests/api/hello_with_testing_and_packing/lib/addon.node.ts new file mode 100644 index 00000000..a7eef18a --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/lib/addon.node.ts @@ -0,0 +1,17 @@ +/** + * The 'addon' C++ addon interface. + */ +interface addon { + /** + * Returns a string, confirming the cmake-js addon is online. + * @returns string + */ + hello(): string; + /** + * Returns a number, confirming the Napi Addon API version number. + * @returns number + */ + version(): number; +} +const addon: addon = require('../build/lib/addon.node'); +export = addon; diff --git a/tests/api/hello_with_testing_and_packing/package.json b/tests/api/hello_with_testing_and_packing/package.json new file mode 100644 index 00000000..7a20edb8 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/package.json @@ -0,0 +1,31 @@ +{ + "name": "@vendor/hello_with_types", + "version": "1.0.0", + "description": "A test addon with CTest, CPack, and Typescript support made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node ./index.js", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure -CD BUILD_TESTS=ON -CD CMAKE_INSTALL_PREFIX=./install -G Ninja", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules", + + "ctest": "ctest --test-dir ./build", + "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./install", + "cdist": "cpack --config ./build/CPackConfig.cmake -B ./install" + }, + "dependencies": { + "cmake-js": "../../../", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + }, + "devDependencies": { + "@types/node": "^20.11.14", + "typescript": "^5.3.3" + } +} diff --git a/tests/api/hello_with_testing_and_packing/src/hello/addon.cpp b/tests/api/hello_with_testing_and_packing/src/hello/addon.cpp new file mode 100644 index 00000000..bd3e3e1b --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/src/hello/addon.cpp @@ -0,0 +1,62 @@ +/** + * @file addon.cpp + * @brief A quick 'hello world' Napi Addon in C++ +*/ + +// Required header and C++ flag +#if __has_include() && BUILDING_NODE_EXTENSION + +#include + +#ifndef STRINGIFY +# define STRINGIFY_HELPER(n) #n +# define STRINGIFY(n) STRINGIFY_HELPER(n) +#endif + +namespace Napi +{ +namespace NAPI_CPP_CUSTOM_NAMESPACE +{ + +Napi::Value Hello(const Napi::CallbackInfo& info) { + return Napi::String::New(info.Env(), STRINGIFY(CMAKEJS_ADDON_NAME)".node is online!"); +} + +Napi::Value Version(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), NAPI_VERSION); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + + // Export a chosen C++ function under a given Javascript key + exports.Set( + Napi::String::New(env, "hello"), // Name of function on Javascript side... + Napi::Function::New(env, Hello) // Name of function on C++ side... + ); + + exports.Set( + Napi::String::New(env, "version"), + Napi::Function::New(env, Version) + ); + + // The above will expose the C++ function 'Hello' as a javascript function + // named 'hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NODE_API_MODULE(CMAKEJS_ADDON_NAME, Init) // (name to use, initializer to use) + +} // namespace NAPI_CPP_CUSTOM_NAMESPACE +} // namespace Napi + +// Export your custom namespace to outside of the Napi namespace, providing an +// alias to the Napi Addon API; e.g., '::::Object()', along with the +// functions defined above, such as '::::Hello()'. +namespace NAPI_CPP_CUSTOM_NAMESPACE::CMAKEJS_ADDON_NAME { + using namespace Napi::NAPI_CPP_CUSTOM_NAMESPACE; +} + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif diff --git a/tests/api/hello_with_testing_and_packing/tests/hello/hello.js b/tests/api/hello_with_testing_and_packing/tests/hello/hello.js new file mode 100644 index 00000000..570fc28c --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/tests/hello/hello.js @@ -0,0 +1,30 @@ +function test_hello() { + + let status = false; + + try { + + const demo = require("../../lib/demo.node"); + + console.log(demo.hello()); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_hello = test_hello(); + +if((!res_test_hello)) +{ + console.log("'test_hello()' failed."); + return false; +} + +console.log("'test_hello()' passed."); +return true; diff --git a/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake b/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake new file mode 100644 index 00000000..adbb0287 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake @@ -0,0 +1,22 @@ +# Make a list of tests to do (should match the test's filename) +list(APPEND TESTS + hello + version +) + +# define a function to simplify adding tests +function(do_test arg) + add_test( + NAME test_${arg} + COMMAND "${NODE_EXECUTABLE}" "./tests/demo/${arg}.js" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + ) + set_tests_properties(test_${arg} + PROPERTIES PASS_REGULAR_EXPRESSION " passed." + ) +endfunction(do_test) + +# run the tests +foreach(TEST IN LISTS TESTS) + do_test("${TEST}") +endforeach() diff --git a/tests/api/hello_with_testing_and_packing/tests/hello/version.js b/tests/api/hello_with_testing_and_packing/tests/hello/version.js new file mode 100644 index 00000000..682c8dd7 --- /dev/null +++ b/tests/api/hello_with_testing_and_packing/tests/hello/version.js @@ -0,0 +1,30 @@ +function test_version() { + + let status = false; + + try { + + const demo = require("../../lib/demo.node"); + + console.log(`Napi Version: ${demo.version()}`); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_version = test_version(); + +if((!res_test_version)) +{ + console.log("'test_version()' failed."); + return false; +} + +console.log("'test_version()' passed."); +return true; diff --git a/tests/api/hello_with_types/CMakeLists.txt b/tests/api/hello_with_types/CMakeLists.txt index 57ef4d68..11b7170f 100644 --- a/tests/api/hello_with_types/CMakeLists.txt +++ b/tests/api/hello_with_types/CMakeLists.txt @@ -1,10 +1,5 @@ cmake_minimum_required(VERSION 3.15) -# If not using cmake-js CLI, fetch the API manually -if (NOT DEFINED CMAKE_JS_VERSION) - list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") -endif () - project(hello) include(CMakeJS) diff --git a/tests/api/hello_with_types/README.md b/tests/api/hello_with_types/README.md new file mode 100644 index 00000000..7533ccdc --- /dev/null +++ b/tests/api/hello_with_types/README.md @@ -0,0 +1,7 @@ +## I am an addon with Typescript support. + +You can build me, export me as a Javscript module, and share your comments with importing consumers' users' intellisense engines; write descriptions of the cool functions and features that you build, so that your users will have an even better experience using your addon. + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI From 9a535b1b8bb90b5e646021d91e196743b2e55cfb Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Sun, 4 Feb 2024 10:30:39 +0100 Subject: [PATCH 56/92] Fix - example project demonstrating CPack and CTest --- tests/api/hello_with_testing_and_packing/CMakeLists.txt | 2 +- tests/api/hello_with_testing_and_packing/lib/addon.node.js | 7 ++----- tests/api/hello_with_testing_and_packing/package.json | 4 ++-- .../hello_with_testing_and_packing/tests/hello/hello.js | 4 ++-- .../hello_with_testing_and_packing/tests/hello/tests.cmake | 2 +- .../hello_with_testing_and_packing/tests/hello/version.js | 4 ++-- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/api/hello_with_testing_and_packing/CMakeLists.txt b/tests/api/hello_with_testing_and_packing/CMakeLists.txt index 68367063..c06f5fae 100644 --- a/tests/api/hello_with_testing_and_packing/CMakeLists.txt +++ b/tests/api/hello_with_testing_and_packing/CMakeLists.txt @@ -93,7 +93,7 @@ install(FILES if(BUILD_TESTS) # include(CTest) # If you want to see all the test dashboard targets, uncomment this enable_testing() - include("${CMAKE_CURRENT_LIST_DIR}/tests/addon/tests.cmake") + include("${CMAKE_CURRENT_LIST_DIR}/tests/hello/tests.cmake") endif() # set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... diff --git a/tests/api/hello_with_testing_and_packing/lib/addon.node.js b/tests/api/hello_with_testing_and_packing/lib/addon.node.js index 55c8ada8..572207ec 100644 --- a/tests/api/hello_with_testing_and_packing/lib/addon.node.js +++ b/tests/api/hello_with_testing_and_packing/lib/addon.node.js @@ -4,8 +4,5 @@ var buildDir = "/build/lib/"; if(platform === "win32") buildDir = "\\build\\bin\\Release\\"; -const addon - = require(`..${buildDir}addon -.node`); -module.exports = addon -; +const addon = require(`..${buildDir}addon.node`); +module.exports = addon; diff --git a/tests/api/hello_with_testing_and_packing/package.json b/tests/api/hello_with_testing_and_packing/package.json index 7a20edb8..30ca6991 100644 --- a/tests/api/hello_with_testing_and_packing/package.json +++ b/tests/api/hello_with_testing_and_packing/package.json @@ -8,14 +8,14 @@ "start": "node ./index.js", "install": "cmake-js install", "postinstall": "cmake-js compile", - "configure": "cmake-js configure -CD BUILD_TESTS=ON -CD CMAKE_INSTALL_PREFIX=./install -G Ninja", + "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./install", "reconfigure": "cmake-js reconfigure", "build": "cmake-js build", "rebuild": "cmake-js rebuild", "clean": "cmake-js clean", "wipe": "cmake-js clean && rm -rvf ./node_modules", - "ctest": "ctest --test-dir ./build", + "ctest": "ctest --test-dir ./build --rerun-failed --output-on-failure --verbose", "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./install", "cdist": "cpack --config ./build/CPackConfig.cmake -B ./install" }, diff --git a/tests/api/hello_with_testing_and_packing/tests/hello/hello.js b/tests/api/hello_with_testing_and_packing/tests/hello/hello.js index 570fc28c..624cd825 100644 --- a/tests/api/hello_with_testing_and_packing/tests/hello/hello.js +++ b/tests/api/hello_with_testing_and_packing/tests/hello/hello.js @@ -4,9 +4,9 @@ function test_hello() { try { - const demo = require("../../lib/demo.node"); + const addon = require("../../lib/addon.node"); - console.log(demo.hello()); + console.log(addon.hello()); status = true; diff --git a/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake b/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake index adbb0287..56f0435f 100644 --- a/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake +++ b/tests/api/hello_with_testing_and_packing/tests/hello/tests.cmake @@ -8,7 +8,7 @@ list(APPEND TESTS function(do_test arg) add_test( NAME test_${arg} - COMMAND "${NODE_EXECUTABLE}" "./tests/demo/${arg}.js" + COMMAND "${NODE_EXECUTABLE}" "./tests/hello/${arg}.js" WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" ) set_tests_properties(test_${arg} diff --git a/tests/api/hello_with_testing_and_packing/tests/hello/version.js b/tests/api/hello_with_testing_and_packing/tests/hello/version.js index 682c8dd7..643baad2 100644 --- a/tests/api/hello_with_testing_and_packing/tests/hello/version.js +++ b/tests/api/hello_with_testing_and_packing/tests/hello/version.js @@ -4,9 +4,9 @@ function test_version() { try { - const demo = require("../../lib/demo.node"); + const addon = require("../../lib/addon.node"); - console.log(`Napi Version: ${demo.version()}`); + console.log(`Napi Version: ${addon.version()}`); status = true; From 141bd0baad17c13a950c86f05412901f714660fb Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 02:16:08 +0100 Subject: [PATCH 57/92] typo --- tests/api/hello_with_testing_and_packing/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api/hello_with_testing_and_packing/CMakeLists.txt b/tests/api/hello_with_testing_and_packing/CMakeLists.txt index c06f5fae..98cae7dc 100644 --- a/tests/api/hello_with_testing_and_packing/CMakeLists.txt +++ b/tests/api/hello_with_testing_and_packing/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15) -project(vendor) +project(vendor VERSION 1.0.0.0) # (must be top-level project to run tests and packing, like any other CMake project!) set (vendor_IS_MASTER_PROJECT FALSE) From 7309407f12e89c8d47a0a5fc11ac72553b5042ae Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 02:32:53 +0100 Subject: [PATCH 58/92] Simplified steps in CPack example project --- tests/api/hello_with_testing_and_packing/package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/api/hello_with_testing_and_packing/package.json b/tests/api/hello_with_testing_and_packing/package.json index 30ca6991..35b3670a 100644 --- a/tests/api/hello_with_testing_and_packing/package.json +++ b/tests/api/hello_with_testing_and_packing/package.json @@ -7,8 +7,8 @@ "scripts": { "start": "node ./index.js", "install": "cmake-js install", - "postinstall": "cmake-js compile", - "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./install", + "postinstall": "cmake-js compile --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", "reconfigure": "cmake-js reconfigure", "build": "cmake-js build", "rebuild": "cmake-js rebuild", @@ -16,8 +16,8 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules", "ctest": "ctest --test-dir ./build --rerun-failed --output-on-failure --verbose", - "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./install", - "cdist": "cpack --config ./build/CPackConfig.cmake -B ./install" + "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./dist", + "cdist": "cpack --config ./build/CPackConfig.cmake -B ./dist" }, "dependencies": { "cmake-js": "../../../", From 738e427bd34922e818f61a310dda133ce541328f Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:32:33 +0100 Subject: [PATCH 59/92] typo --- share/cmake/CMakeJS.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 8ecbfb20..bc67259a 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -668,10 +668,10 @@ if(CMAKEJS_USING_CMAKEJS) # user did 'cmake-js configure --link-level=3', or did # stuff themselves. An additional cosideration is making sure we only apply stuff to our targets, # and never just define things globally. Meaning, always use 'target_add_definitions()' and never ever # use regular 'add_definitions()' when taking care of this. Why? Because that global function - # will propogate globally, downstream, and who knows where else (see also: : never do 'using namespace ' + # will propogate globally, downstream, and who knows where else (see also: never do 'using namespace ' # in the global namespace scope in a public C++ header file... you don't know where it will propogate, # what it might break, or even where the issue stems from when it does arise). - # Keep in mind that PUBLIC defs on are propogated to any other targets that link with it. + # Keep in mind that PUBLIC defs on a target are propogated to any other targets that link with it. # PRIVATE does not propogate. Sometimes, INTERFACE libs insist you use INTERFACE specifiers. # so whatever is decided, it must be very well considered i.e., not arbitrary, and if CMake # insists something, don't fight it. CMake doggedly refuses to be beaten at it's own games :) From 21d5621f7a60ba452e3c7f2e73e43d205179a5be Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:32:58 +0100 Subject: [PATCH 60/92] Added a 'start' command to our user example --- tests/api/hello/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api/hello/package.json b/tests/api/hello/package.json index 9e6b420b..7b152f0d 100644 --- a/tests/api/hello/package.json +++ b/tests/api/hello/package.json @@ -5,7 +5,7 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node ./index.js", + "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", "install": "cmake-js install", "postinstall": "cmake-js compile", "configure": "cmake-js configure", From 7583b885faef93ba619be1c927e10f15a49bd932 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:34:06 +0100 Subject: [PATCH 61/92] Added example project using cmake-js::cmake-js without helper function --- tests/targets/cmake-js/.gitignore | 3 ++ tests/targets/cmake-js/CMakeLists.txt | 21 +++++++++++ tests/targets/cmake-js/README.md | 7 ++++ tests/targets/cmake-js/index.js | 10 +++++ tests/targets/cmake-js/package.json | 23 ++++++++++++ tests/targets/cmake-js/src/hello/addon.cpp | 43 ++++++++++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 tests/targets/cmake-js/.gitignore create mode 100644 tests/targets/cmake-js/CMakeLists.txt create mode 100644 tests/targets/cmake-js/README.md create mode 100644 tests/targets/cmake-js/index.js create mode 100644 tests/targets/cmake-js/package.json create mode 100644 tests/targets/cmake-js/src/hello/addon.cpp diff --git a/tests/targets/cmake-js/.gitignore b/tests/targets/cmake-js/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/targets/cmake-js/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/targets/cmake-js/CMakeLists.txt b/tests/targets/cmake-js/CMakeLists.txt new file mode 100644 index 00000000..a6e5fadb --- /dev/null +++ b/tests/targets/cmake-js/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.15) + +project(hello) + +include(CMakeJS) + +add_library(addon SHARED "src/hello/addon.cpp") +target_link_libraries(addon PRIVATE cmake-js::cmake-js) # relocatable deps resolved! +set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") +set_target_properties(addon PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") +target_compile_definitions(addon PUBLIC BUILDING_NODE_EXTENSION) +target_compile_definitions(addon PUBLIC NAPI_VERSION=8) +target_compile_definitions(addon PUBLIC NAPI_CPP_EXCEPTIONS_MAYBE) + +# For Visual Studio generators +if(MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." FORCE) + if(CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) + execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) + endif() +endif() diff --git a/tests/targets/cmake-js/README.md b/tests/targets/cmake-js/README.md new file mode 100644 index 00000000..e5f46ff0 --- /dev/null +++ b/tests/targets/cmake-js/README.md @@ -0,0 +1,7 @@ +## I am an addon made with Napi Addon API in C++. + +You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C++ functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/targets/cmake-js/index.js b/tests/targets/cmake-js/index.js new file mode 100644 index 00000000..4e5b1cdb --- /dev/null +++ b/tests/targets/cmake-js/index.js @@ -0,0 +1,10 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const hello = require(`.${buildDir}addon.node`); +module.exports = hello; diff --git a/tests/targets/cmake-js/package.json b/tests/targets/cmake-js/package.json new file mode 100644 index 00000000..7b152f0d --- /dev/null +++ b/tests/targets/cmake-js/package.json @@ -0,0 +1,23 @@ +{ + "name": "@vendor/hello", + "version": "1.0.0", + "description": "A test addon made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" + }, + "dependencies": { + "cmake-js": "../../../", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + } +} diff --git a/tests/targets/cmake-js/src/hello/addon.cpp b/tests/targets/cmake-js/src/hello/addon.cpp new file mode 100644 index 00000000..57c7c072 --- /dev/null +++ b/tests/targets/cmake-js/src/hello/addon.cpp @@ -0,0 +1,43 @@ +/** + * @file addon.cpp + * @brief A quick 'hello world' Napi Addon in C++ +*/ + +// Required header and C++ flag +#if __has_include() && BUILDING_NODE_EXTENSION + +#include + +Napi::Value Hello(const Napi::CallbackInfo& info) { + return Napi::String::New(info.Env(), "addon.node is online!"); +} + +Napi::Value Version(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), NAPI_VERSION); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + + // Export a chosen C++ function under a given Javascript key + exports.Set( + Napi::String::New(env, "hello"), // Name of function on Javascript side... + Napi::Function::New(env, Hello) // Name of function on C++ side... + ); + + exports.Set( + Napi::String::New(env, "version"), + Napi::Function::New(env, Version) + ); + + // The above will expose the C++ function 'Hello' as a javascript function + // named 'hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NODE_API_MODULE(addon, Init) // (name to use, initializer to use) + + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif From 256c34a9244eb70eb654dfba63990b4916375fd0 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:34:31 +0100 Subject: [PATCH 62/92] Added example project using cmake-js::node-addon-api --- tests/targets/node-addon-api/.gitignore | 3 ++ tests/targets/node-addon-api/CMakeLists.txt | 23 ++++++++++ tests/targets/node-addon-api/README.md | 7 +++ tests/targets/node-addon-api/index.js | 10 +++++ tests/targets/node-addon-api/package.json | 23 ++++++++++ .../node-addon-api/src/hello/addon.cpp | 43 +++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 tests/targets/node-addon-api/.gitignore create mode 100644 tests/targets/node-addon-api/CMakeLists.txt create mode 100644 tests/targets/node-addon-api/README.md create mode 100644 tests/targets/node-addon-api/index.js create mode 100644 tests/targets/node-addon-api/package.json create mode 100644 tests/targets/node-addon-api/src/hello/addon.cpp diff --git a/tests/targets/node-addon-api/.gitignore b/tests/targets/node-addon-api/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/targets/node-addon-api/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/targets/node-addon-api/CMakeLists.txt b/tests/targets/node-addon-api/CMakeLists.txt new file mode 100644 index 00000000..5d488faf --- /dev/null +++ b/tests/targets/node-addon-api/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.15) + +project(hello) + +set(CMAKEJS_USING_CMAKEJS FALSE) # this should have been done with '--link-level=2' on the CLI, testing this way for now + +include(CMakeJS) + +add_library(addon SHARED "src/hello/addon.cpp") +target_link_libraries(addon PRIVATE cmake-js::node-addon-api) # relocatable deps resolved! +set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") +set_target_properties(addon PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") +target_compile_definitions(addon PUBLIC BUILDING_NODE_EXTENSION) +target_compile_definitions(addon PUBLIC NAPI_VERSION=8) +target_compile_definitions(addon PUBLIC NAPI_CPP_EXCEPTIONS_MAYBE) + +# For Visual Studio generators +if(MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." FORCE) + if(CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) + execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) + endif() +endif() diff --git a/tests/targets/node-addon-api/README.md b/tests/targets/node-addon-api/README.md new file mode 100644 index 00000000..e5f46ff0 --- /dev/null +++ b/tests/targets/node-addon-api/README.md @@ -0,0 +1,7 @@ +## I am an addon made with Napi Addon API in C++. + +You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C++ functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/targets/node-addon-api/index.js b/tests/targets/node-addon-api/index.js new file mode 100644 index 00000000..4e5b1cdb --- /dev/null +++ b/tests/targets/node-addon-api/index.js @@ -0,0 +1,10 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const hello = require(`.${buildDir}addon.node`); +module.exports = hello; diff --git a/tests/targets/node-addon-api/package.json b/tests/targets/node-addon-api/package.json new file mode 100644 index 00000000..7b152f0d --- /dev/null +++ b/tests/targets/node-addon-api/package.json @@ -0,0 +1,23 @@ +{ + "name": "@vendor/hello", + "version": "1.0.0", + "description": "A test addon made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" + }, + "dependencies": { + "cmake-js": "../../../", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + } +} diff --git a/tests/targets/node-addon-api/src/hello/addon.cpp b/tests/targets/node-addon-api/src/hello/addon.cpp new file mode 100644 index 00000000..57c7c072 --- /dev/null +++ b/tests/targets/node-addon-api/src/hello/addon.cpp @@ -0,0 +1,43 @@ +/** + * @file addon.cpp + * @brief A quick 'hello world' Napi Addon in C++ +*/ + +// Required header and C++ flag +#if __has_include() && BUILDING_NODE_EXTENSION + +#include + +Napi::Value Hello(const Napi::CallbackInfo& info) { + return Napi::String::New(info.Env(), "addon.node is online!"); +} + +Napi::Value Version(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), NAPI_VERSION); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + + // Export a chosen C++ function under a given Javascript key + exports.Set( + Napi::String::New(env, "hello"), // Name of function on Javascript side... + Napi::Function::New(env, Hello) // Name of function on C++ side... + ); + + exports.Set( + Napi::String::New(env, "version"), + Napi::Function::New(env, Version) + ); + + // The above will expose the C++ function 'Hello' as a javascript function + // named 'hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NODE_API_MODULE(addon, Init) // (name to use, initializer to use) + + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif From 8b42aeba4d21ddd431724741cb7889cd99f29d3c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:34:50 +0100 Subject: [PATCH 63/92] Added example project using cmake-js::node-dev --- tests/targets/node-dev/.gitignore | 3 ++ tests/targets/node-dev/CMakeLists.txt | 20 ++++++++++ tests/targets/node-dev/README.md | 7 ++++ tests/targets/node-dev/index.js | 10 +++++ tests/targets/node-dev/package.json | 23 +++++++++++ tests/targets/node-dev/src/hello/addon.cc | 47 +++++++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 tests/targets/node-dev/.gitignore create mode 100644 tests/targets/node-dev/CMakeLists.txt create mode 100644 tests/targets/node-dev/README.md create mode 100644 tests/targets/node-dev/index.js create mode 100644 tests/targets/node-dev/package.json create mode 100644 tests/targets/node-dev/src/hello/addon.cc diff --git a/tests/targets/node-dev/.gitignore b/tests/targets/node-dev/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/targets/node-dev/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/targets/node-dev/CMakeLists.txt b/tests/targets/node-dev/CMakeLists.txt new file mode 100644 index 00000000..bfc0e642 --- /dev/null +++ b/tests/targets/node-dev/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.15) + +project(hello) + +set(CMAKEJS_USING_NODE_API FALSE) # this should have been done with '--link-level=0' on the CLI, testing this way for now + +include(CMakeJS) + +add_library(addon SHARED "src/hello/addon.cc") +target_link_libraries(addon PRIVATE cmake-js::node-dev) # relocatable deps resolved! +set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") +set_target_properties(addon PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") + +# For Visual Studio generators +if(MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." FORCE) + if(CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) + execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) + endif() +endif() diff --git a/tests/targets/node-dev/README.md b/tests/targets/node-dev/README.md new file mode 100644 index 00000000..e5f46ff0 --- /dev/null +++ b/tests/targets/node-dev/README.md @@ -0,0 +1,7 @@ +## I am an addon made with Napi Addon API in C++. + +You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C++ functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/targets/node-dev/index.js b/tests/targets/node-dev/index.js new file mode 100644 index 00000000..4e5b1cdb --- /dev/null +++ b/tests/targets/node-dev/index.js @@ -0,0 +1,10 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const hello = require(`.${buildDir}addon.node`); +module.exports = hello; diff --git a/tests/targets/node-dev/package.json b/tests/targets/node-dev/package.json new file mode 100644 index 00000000..7b152f0d --- /dev/null +++ b/tests/targets/node-dev/package.json @@ -0,0 +1,23 @@ +{ + "name": "@vendor/hello", + "version": "1.0.0", + "description": "A test addon made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" + }, + "dependencies": { + "cmake-js": "../../../", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + } +} diff --git a/tests/targets/node-dev/src/hello/addon.cc b/tests/targets/node-dev/src/hello/addon.cc new file mode 100644 index 00000000..b9a3b72e --- /dev/null +++ b/tests/targets/node-dev/src/hello/addon.cc @@ -0,0 +1,47 @@ +/** + * @file addon.cc + * @brief A quick 'hello world' Node Addon in C++ +*/ + +// Required header +#if __has_include() + +#include + +namespace vendor { + +void Hello(const v8::FunctionCallbackInfo& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "addon.node is online!").ToLocalChecked()); +} + +// void Version(const v8::FunctionCallbackInfo& args) +// { +// v8::Isolate* isolate = args.GetIsolate(); +// args.GetReturnValue().Set(NODE_VERSION); +// } + +// Expose the C++ function 'Hello' as a javascript function named 'hello', etc... +void Initialize(v8::Local exports) +{ + // Export a chosen C++ function under a given Javascript key + NODE_SET_METHOD(exports, + "hello", // Name of function on Javascript side... + Hello // Name of function on C++ side... + ); + + // NODE_SET_METHOD(exports, + // "version", + // Version + // ); +} + +// Register a new addon with the intializer function defined above +NODE_MODULE(addon, Initialize) // (name to use, initializer to use) + +} // namespace vendor + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif From e33032fd2e5e3773b62f821d4b480a7115077ea0 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:36:20 +0100 Subject: [PATCH 64/92] Added a hint to node-dev example project --- tests/targets/node-dev/src/hello/addon.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/targets/node-dev/src/hello/addon.cc b/tests/targets/node-dev/src/hello/addon.cc index b9a3b72e..e31f1c73 100644 --- a/tests/targets/node-dev/src/hello/addon.cc +++ b/tests/targets/node-dev/src/hello/addon.cc @@ -19,7 +19,7 @@ void Hello(const v8::FunctionCallbackInfo& args) // void Version(const v8::FunctionCallbackInfo& args) // { // v8::Isolate* isolate = args.GetIsolate(); -// args.GetReturnValue().Set(NODE_VERSION); +// args.GetReturnValue().Set(NODE_VERSION); // should be a v8::Number... // } // Expose the C++ function 'Hello' as a javascript function named 'hello', etc... From 596d1cfa60fe828605b69d6e5d30c326cbfbc94c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:37:00 +0100 Subject: [PATCH 65/92] typo --- tests/targets/node-dev/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/targets/node-dev/README.md b/tests/targets/node-dev/README.md index e5f46ff0..dbc35886 100644 --- a/tests/targets/node-dev/README.md +++ b/tests/targets/node-dev/README.md @@ -1,4 +1,4 @@ -## I am an addon made with Napi Addon API in C++. +## I am an addon made with Node API in C++. You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C++ functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! From 3285d7344447d1decfe652f69b4d0babfa4efa6b Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:56:48 +0100 Subject: [PATCH 66/92] Added example project using cmake-js::node-api (incomplete...) --- tests/targets/node-api/.gitignore | 3 +++ tests/targets/node-api/CMakeLists.txt | 20 ++++++++++++++++++++ tests/targets/node-api/README.md | 14 ++++++++++++++ tests/targets/node-api/index.js | 10 ++++++++++ tests/targets/node-api/package.json | 22 ++++++++++++++++++++++ tests/targets/node-api/src/hello/addon.c | 23 +++++++++++++++++++++++ tests/targets/node-dev/package.json | 4 +--- 7 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 tests/targets/node-api/.gitignore create mode 100644 tests/targets/node-api/CMakeLists.txt create mode 100644 tests/targets/node-api/README.md create mode 100644 tests/targets/node-api/index.js create mode 100644 tests/targets/node-api/package.json create mode 100644 tests/targets/node-api/src/hello/addon.c diff --git a/tests/targets/node-api/.gitignore b/tests/targets/node-api/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/targets/node-api/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/targets/node-api/CMakeLists.txt b/tests/targets/node-api/CMakeLists.txt new file mode 100644 index 00000000..c02dac31 --- /dev/null +++ b/tests/targets/node-api/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.15) + +project(hello) + +set(CMAKEJS_USING_NODE_ADDON_API FALSE) # this should have been done with '--link-level=1' on the CLI, testing this way for now + +include(CMakeJS) + +add_library(addon SHARED "src/hello/addon.c") +target_link_libraries(addon PRIVATE cmake-js::node-api) # relocatable deps resolved! +set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") +set_target_properties(addon PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") + +# For Visual Studio generators +if(MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." FORCE) + if(CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET) + execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}) + endif() +endif() diff --git a/tests/targets/node-api/README.md b/tests/targets/node-api/README.md new file mode 100644 index 00000000..3d8443fb --- /dev/null +++ b/tests/targets/node-api/README.md @@ -0,0 +1,14 @@ +# DISCLAIMER + +I don't actually know how to build one of these, so for now, +I'm just trying to compile a var or two to test the linked-in +target against. Once someone figures out how to build a real addon, +in C against node_api.h, please replace addon.c and remove this disclaimer! + +## I am an addon made with Napi Addon API in C. + +You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! + +Please see my package.json to understand how I work. + +Powered by cmake-js CLI diff --git a/tests/targets/node-api/index.js b/tests/targets/node-api/index.js new file mode 100644 index 00000000..4e5b1cdb --- /dev/null +++ b/tests/targets/node-api/index.js @@ -0,0 +1,10 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const hello = require(`.${buildDir}addon.node`); +module.exports = hello; diff --git a/tests/targets/node-api/package.json b/tests/targets/node-api/package.json new file mode 100644 index 00000000..ad064835 --- /dev/null +++ b/tests/targets/node-api/package.json @@ -0,0 +1,22 @@ +{ + "name": "@vendor/hello", + "version": "1.0.0", + "description": "A test addon made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules" + }, + "dependencies": { + "cmake-js": "../../../", + "node-api-headers": "^1.1.0" + } +} diff --git a/tests/targets/node-api/src/hello/addon.c b/tests/targets/node-api/src/hello/addon.c new file mode 100644 index 00000000..9e5c3169 --- /dev/null +++ b/tests/targets/node-api/src/hello/addon.c @@ -0,0 +1,23 @@ +/** + * @file addon.cc + * @brief A quick 'hello world' Node Addon in C +*/ + +// Required header +#if __has_include() + +#include + +// I don't actually know how to build one of these +// in C, so just trying to compile these vars for now! + +// If our target works, the header will be found and this will +// compile. The addon probably won't do anything, until +// someone figures out how these work! + +napi_env env; +napi_status status; + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif diff --git a/tests/targets/node-dev/package.json b/tests/targets/node-dev/package.json index 7b152f0d..0e5a880a 100644 --- a/tests/targets/node-dev/package.json +++ b/tests/targets/node-dev/package.json @@ -16,8 +16,6 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../", - "node-addon-api": "^7.1.0", - "node-api-headers": "^1.1.0" + "cmake-js": "../../../" } } From 04fec039ce14a78f8102ba67083129d04926d706 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 04:57:48 +0100 Subject: [PATCH 67/92] typo --- tests/targets/node-api/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/targets/node-api/README.md b/tests/targets/node-api/README.md index 3d8443fb..048a7e80 100644 --- a/tests/targets/node-api/README.md +++ b/tests/targets/node-api/README.md @@ -1,6 +1,6 @@ # DISCLAIMER -I don't actually know how to build one of these, so for now, +It is not so clear how to code one of these, so for now, I'm just trying to compile a var or two to test the linked-in target against. Once someone figures out how to build a real addon, in C against node_api.h, please replace addon.c and remove this disclaimer! From 436584439631a259ba636a296eed321424aee3b8 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 06:00:37 +0100 Subject: [PATCH 68/92] Added example project using cmake-js::node-api (completed) --- share/cmake/CMakeJS.cmake | 2 +- .../package.json | 6 +- tests/targets/README.md | 20 +++++++ tests/targets/node-api/CMakeLists.txt | 2 +- tests/targets/node-api/README.md | 7 --- tests/targets/node-api/src/hello/addon.c | 23 -------- tests/targets/node-api/src/hello/addon.cc | 55 +++++++++++++++++++ tests/targets/node-dev/src/hello/addon.cc | 2 +- 8 files changed, 81 insertions(+), 36 deletions(-) create mode 100644 tests/targets/README.md delete mode 100644 tests/targets/node-api/src/hello/addon.c create mode 100644 tests/targets/node-api/src/hello/addon.cc diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index bc67259a..d92e863f 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -1231,7 +1231,7 @@ target_link_libraries(my_library PRIVATE cmake-js::node-dev) include(CMakeJS) add_library(my_addon SHARED) -target_sources(my_addon PRIVATE src//my_addon.c) +target_sources(my_addon PRIVATE src//my_addon.cc) target_link_libraries(my_addon PRIVATE cmake-js::node-api) set_target_properties(my_addon PROPERTIES PREFIX "" SUFFIX ".node") diff --git a/tests/api/hello_with_testing_and_packing/package.json b/tests/api/hello_with_testing_and_packing/package.json index 35b3670a..fbe4ca8f 100644 --- a/tests/api/hello_with_testing_and_packing/package.json +++ b/tests/api/hello_with_testing_and_packing/package.json @@ -7,9 +7,9 @@ "scripts": { "start": "node ./index.js", "install": "cmake-js install", - "postinstall": "cmake-js compile --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", - "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", - "reconfigure": "cmake-js reconfigure", + "postinstall": "cmake-js compile --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "reconfigure": "cmake-js reconfigure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", "build": "cmake-js build", "rebuild": "cmake-js rebuild", "clean": "cmake-js clean", diff --git a/tests/targets/README.md b/tests/targets/README.md new file mode 100644 index 00000000..78314b03 --- /dev/null +++ b/tests/targets/README.md @@ -0,0 +1,20 @@ +# CMakeJS.cmake targets + +The four directories here correspond to the four CMake targets we currently export: + +| dir | target | provides | link level | +| -------------- | ------------------------ | ------------------------------- | ----------- | +| node-dev | cmake-js::node-dev | \ | 0 | +| node-api | cmake-js::node-api | \ | 1 | +| node-addon-api | cmake-js::node-addon-api | \ | 2 | +| cmake-js | cmake-js::cmake-js | cmakejs_create_napi_addon() | 3 (default) | + +Each target in the above order *depends on* the one before it; they have also all been made relocatable. + +The first three targets, cmake-js::node-*, each carries it's own set of headers, as indicated by their given names, and further clarified in the table above. + +Our proposed ```--link-level``` CLI switch can control how far down the list they want to go. User who specify a ```--link-level=1``` on their cmake-js CLI command(s) will handily find that they are automatically linked with and provided the header sets provided by cmake-js::node-api (the C addon headers), *as well as* cmake-js::node-dev (which the C headers depend on). What they won't recieve or be linked with is any of the header set from cmake-js::node-addon-api (the C++ Addon wrappers). This is [desireable behaviour](). + +cmake-js::cmake-js is unique in that it doesn't carry any headers of it's own, but since it *does* carry all the other targets by being last in the consumer chain, users linking to this target will recieve the full set of developer headers, *and* will be able to use our very nice ```cmakejs_create_napi_addon()``` function in their CMakeLists.txt, which vastly reduces the amount of intermediate/advanced CMake config they might otherwise be faced with. + +As a cherry on the cake, users will be able to call CPack on their addons, and find that their CMake source and binary dirs are bundled up along with the header sets that they requested via ```--link-level``` (and nothing more). diff --git a/tests/targets/node-api/CMakeLists.txt b/tests/targets/node-api/CMakeLists.txt index c02dac31..0ebb57c2 100644 --- a/tests/targets/node-api/CMakeLists.txt +++ b/tests/targets/node-api/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKEJS_USING_NODE_ADDON_API FALSE) # this should have been done with '--lin include(CMakeJS) -add_library(addon SHARED "src/hello/addon.c") +add_library(addon SHARED "src/hello/addon.cc") target_link_libraries(addon PRIVATE cmake-js::node-api) # relocatable deps resolved! set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node") set_target_properties(addon PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") diff --git a/tests/targets/node-api/README.md b/tests/targets/node-api/README.md index 048a7e80..9928f18d 100644 --- a/tests/targets/node-api/README.md +++ b/tests/targets/node-api/README.md @@ -1,10 +1,3 @@ -# DISCLAIMER - -It is not so clear how to code one of these, so for now, -I'm just trying to compile a var or two to test the linked-in -target against. Once someone figures out how to build a real addon, -in C against node_api.h, please replace addon.c and remove this disclaimer! - ## I am an addon made with Napi Addon API in C. You can build me, export me as a Javscript module, extend my functionality with your own code ideas, connecting Javascript and C functionality together in one binding package. Any importing consumers can get me from '@vendor/hello' in their package.json deps... see 'hello_consumer'! diff --git a/tests/targets/node-api/src/hello/addon.c b/tests/targets/node-api/src/hello/addon.c deleted file mode 100644 index 9e5c3169..00000000 --- a/tests/targets/node-api/src/hello/addon.c +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file addon.cc - * @brief A quick 'hello world' Node Addon in C -*/ - -// Required header -#if __has_include() - -#include - -// I don't actually know how to build one of these -// in C, so just trying to compile these vars for now! - -// If our target works, the header will be found and this will -// compile. The addon probably won't do anything, until -// someone figures out how these work! - -napi_env env; -napi_status status; - -#else // !__has_include() || !BUILDING_NODE_EXTENSION - #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." -#endif diff --git a/tests/targets/node-api/src/hello/addon.cc b/tests/targets/node-api/src/hello/addon.cc new file mode 100644 index 00000000..a5af8e28 --- /dev/null +++ b/tests/targets/node-api/src/hello/addon.cc @@ -0,0 +1,55 @@ +/** + * @file addon.cc + * @brief A quick 'hello world' Node Addon in C + * + * Please note that this example is from the NodeJS Addon + * official docs, and uses 'nullptr', which does not exist + * in 'pure' C. If you name your addon source file with an + * extension of just '.c', the compiler/generator will assume + * you are building in 'pure' C and this useage of 'nullptr' + * will cause the build to fail. + * + * To have a more 'C-like' experience building addons in C, + * we recommend using the extension '.cc' for your sources, + * because this extension does not differentiate between + * being a C file or a C++ file, unlike both '.c' and the + * various '.cpp/cxx' file extensions. +*/ + +// Required header +#if __has_include() + +#include + +napi_value addon_hello(napi_env env, napi_callback_info args) +{ + napi_value greeting; + napi_status status; + + status = napi_create_string_utf8(env, "addon.node is online!", NAPI_AUTO_LENGTH, &greeting); + if (status != napi_ok) return nullptr; + return greeting; +} + +napi_value addon_init(napi_env env, napi_value exports) +{ + napi_status status; + napi_value fn; + + // Export a chosen C function under a given Javascript key + status = napi_create_function(env, nullptr, 0, addon_hello, nullptr, &fn); // Name of function on Javascript side... + if (status != napi_ok) return nullptr; + + status = napi_set_named_property(env, exports, "hello", fn); // Name of function on C side... + if (status != napi_ok) return nullptr; + + // The above expose the C function 'addon_hello' as a javascript function named '.hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NAPI_MODULE(addon, addon_init) // ( to use, initializer to use) + +#else // !__has_include() + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif diff --git a/tests/targets/node-dev/src/hello/addon.cc b/tests/targets/node-dev/src/hello/addon.cc index e31f1c73..ad8203e9 100644 --- a/tests/targets/node-dev/src/hello/addon.cc +++ b/tests/targets/node-dev/src/hello/addon.cc @@ -42,6 +42,6 @@ NODE_MODULE(addon, Initialize) // (name to use, initializer to use) } // namespace vendor -#else // !__has_include() || !BUILDING_NODE_EXTENSION +#else // !__has_include() #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." #endif From 9fb75f629de5a13f21870479d06939e89a22a5f0 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 06:02:47 +0100 Subject: [PATCH 69/92] Improved example project using cmake-js::node-api (hint: namespaces in C) --- tests/targets/node-api/src/hello/addon.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/targets/node-api/src/hello/addon.cc b/tests/targets/node-api/src/hello/addon.cc index a5af8e28..6ed16d48 100644 --- a/tests/targets/node-api/src/hello/addon.cc +++ b/tests/targets/node-api/src/hello/addon.cc @@ -21,7 +21,7 @@ #include -napi_value addon_hello(napi_env env, napi_callback_info args) +napi_value vendor_addon_hello(napi_env env, napi_callback_info args) { napi_value greeting; napi_status status; @@ -31,13 +31,14 @@ napi_value addon_hello(napi_env env, napi_callback_info args) return greeting; } -napi_value addon_init(napi_env env, napi_value exports) +napi_value vendor_addon_init(napi_env env, napi_value exports) { napi_status status; napi_value fn; // Export a chosen C function under a given Javascript key - status = napi_create_function(env, nullptr, 0, addon_hello, nullptr, &fn); // Name of function on Javascript side... + + status = napi_create_function(env, nullptr, 0, vendor_addon_hello, nullptr, &fn); // Name of function on Javascript side... if (status != napi_ok) return nullptr; status = napi_set_named_property(env, exports, "hello", fn); // Name of function on C side... @@ -48,7 +49,7 @@ napi_value addon_init(napi_env env, napi_value exports) } // Register a new addon with the intializer function defined above -NAPI_MODULE(addon, addon_init) // ( to use, initializer to use) +NAPI_MODULE(addon, vendor_addon_init) // ( to use, initializer to use) #else // !__has_include() #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." From 4b728bc2416399f2fcb55f31662c129a1d5b5fbc Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 06:03:59 +0100 Subject: [PATCH 70/92] typo --- share/cmake/CMakeJS.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index d92e863f..d0d150ee 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -1236,7 +1236,7 @@ target_link_libraries(my_addon PRIVATE cmake-js::node-api) set_target_properties(my_addon PROPERTIES PREFIX "" SUFFIX ".node") -- --- You can include '' in 'my_addon.c' and start building +-- You can include '' in 'my_addon.cc' and start building -- with the Node Addon API in C. -- ]==]) From 70a25b0453c4783a768defe9f4b16dae9b72f7d2 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 06:37:19 +0100 Subject: [PATCH 71/92] Added some documentation --- tests/api/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/api/README.md diff --git a/tests/api/README.md b/tests/api/README.md new file mode 100644 index 00000000..56992809 --- /dev/null +++ b/tests/api/README.md @@ -0,0 +1,11 @@ +# CMakeJS.cmake API examples + +The directories contained here demonstrate some of the various functionality that cmake-js provides. + +Everything is powered by a tidy combination of cmake-js' Javascript CLI and it's CMake API. + +```hello_consumer``` demonstrates how cmake-js - powered addons behave when they filter out into the wider world; the ```consumer``` in question should have NodeJS, CMake, and a C/C++ compiler toolchain installed, but they are not burdened with actually operating any of those tools in order to use somebody's addon like a regular NodeJS module. + +The remaining ```hello_*``` projects each represent various functionalities that our CLI/API combo provides Addon buiders with - see their package.json commands and README files for a better understanding of each demonstration and their purposes. + +An especially interesting example is ```hello_with_testing_and_packing```, which demonstrates not just the cmake-js CLI/API, but also leverages some of the deeper functionality we have provided our CMake targets with, such as providing Addon builders with fully transportable dependencies :) From 4ba786b7024b77b39be8e244a4192efcdf9c92c6 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 06:39:38 +0100 Subject: [PATCH 72/92] Added some documentation --- tests/targets/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/targets/README.md b/tests/targets/README.md index 78314b03..f42b871f 100644 --- a/tests/targets/README.md +++ b/tests/targets/README.md @@ -17,4 +17,6 @@ Our proposed ```--link-level``` CLI switch can control how far down the list the cmake-js::cmake-js is unique in that it doesn't carry any headers of it's own, but since it *does* carry all the other targets by being last in the consumer chain, users linking to this target will recieve the full set of developer headers, *and* will be able to use our very nice ```cmakejs_create_napi_addon()``` function in their CMakeLists.txt, which vastly reduces the amount of intermediate/advanced CMake config they might otherwise be faced with. +Everything is powered by a tidy combination of cmake-js' Javascript CLI and it's CMake API. + As a cherry on the cake, users will be able to call CPack on their addons, and find that their CMake source and binary dirs are bundled up along with the header sets that they requested via ```--link-level``` (and nothing more). From 7b3ed50cc1f042ed44b710629e7821ae0dcfbb3d Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 07:25:30 +0100 Subject: [PATCH 73/92] Made the examples more independent of eachother --- tests/api/hello/package.json | 2 +- tests/api/hello_with_testing_and_packing/package.json | 2 +- tests/api/hello_with_types/package.json | 2 +- tests/targets/cmake-js/CMakeLists.txt | 5 +++++ tests/targets/cmake-js/package.json | 4 ++-- tests/targets/node-addon-api/CMakeLists.txt | 5 +++++ tests/targets/node-addon-api/package.json | 2 +- tests/targets/node-api/CMakeLists.txt | 5 +++++ tests/targets/node-api/package.json | 4 ++-- tests/targets/node-dev/CMakeLists.txt | 5 +++++ tests/targets/node-dev/package.json | 4 ++-- 11 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/api/hello/package.json b/tests/api/hello/package.json index 7b152f0d..399be3ff 100644 --- a/tests/api/hello/package.json +++ b/tests/api/hello/package.json @@ -16,7 +16,7 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../", + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", "node-addon-api": "^7.1.0", "node-api-headers": "^1.1.0" } diff --git a/tests/api/hello_with_testing_and_packing/package.json b/tests/api/hello_with_testing_and_packing/package.json index fbe4ca8f..a7122a25 100644 --- a/tests/api/hello_with_testing_and_packing/package.json +++ b/tests/api/hello_with_testing_and_packing/package.json @@ -20,7 +20,7 @@ "cdist": "cpack --config ./build/CPackConfig.cmake -B ./dist" }, "dependencies": { - "cmake-js": "../../../", + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", "node-addon-api": "^7.1.0", "node-api-headers": "^1.1.0" }, diff --git a/tests/api/hello_with_types/package.json b/tests/api/hello_with_types/package.json index 2e1d46be..a5800061 100644 --- a/tests/api/hello_with_types/package.json +++ b/tests/api/hello_with_types/package.json @@ -16,7 +16,7 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../", + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", "node-addon-api": "^7.1.0", "node-api-headers": "^1.1.0" }, diff --git a/tests/targets/cmake-js/CMakeLists.txt b/tests/targets/cmake-js/CMakeLists.txt index a6e5fadb..6c747613 100644 --- a/tests/targets/cmake-js/CMakeLists.txt +++ b/tests/targets/cmake-js/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.15) project(hello) +# For CMake IDE tools support (do yarn/install first and keep your node_modules folder around) +if(NOT DEFINED CMAKE_JS_VERSION) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif() + include(CMakeJS) add_library(addon SHARED "src/hello/addon.cpp") diff --git a/tests/targets/cmake-js/package.json b/tests/targets/cmake-js/package.json index 7b152f0d..c676b053 100644 --- a/tests/targets/cmake-js/package.json +++ b/tests/targets/cmake-js/package.json @@ -5,7 +5,7 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "start": "node -p \"const addon = require('./index'); console.log(addon.hello());\"", "install": "cmake-js install", "postinstall": "cmake-js compile", "configure": "cmake-js configure", @@ -16,7 +16,7 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../", + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", "node-addon-api": "^7.1.0", "node-api-headers": "^1.1.0" } diff --git a/tests/targets/node-addon-api/CMakeLists.txt b/tests/targets/node-addon-api/CMakeLists.txt index 5d488faf..17402452 100644 --- a/tests/targets/node-addon-api/CMakeLists.txt +++ b/tests/targets/node-addon-api/CMakeLists.txt @@ -4,6 +4,11 @@ project(hello) set(CMAKEJS_USING_CMAKEJS FALSE) # this should have been done with '--link-level=2' on the CLI, testing this way for now +# For CMake IDE tools support (do yarn/install first and keep your node_modules folder around) +if(NOT DEFINED CMAKE_JS_VERSION) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif() + include(CMakeJS) add_library(addon SHARED "src/hello/addon.cpp") diff --git a/tests/targets/node-addon-api/package.json b/tests/targets/node-addon-api/package.json index 7b152f0d..399be3ff 100644 --- a/tests/targets/node-addon-api/package.json +++ b/tests/targets/node-addon-api/package.json @@ -16,7 +16,7 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../", + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", "node-addon-api": "^7.1.0", "node-api-headers": "^1.1.0" } diff --git a/tests/targets/node-api/CMakeLists.txt b/tests/targets/node-api/CMakeLists.txt index 0ebb57c2..92659acd 100644 --- a/tests/targets/node-api/CMakeLists.txt +++ b/tests/targets/node-api/CMakeLists.txt @@ -4,6 +4,11 @@ project(hello) set(CMAKEJS_USING_NODE_ADDON_API FALSE) # this should have been done with '--link-level=1' on the CLI, testing this way for now +# For CMake IDE tools support (do yarn/install first and keep your node_modules folder around) +if(NOT DEFINED CMAKE_JS_VERSION) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif() + include(CMakeJS) add_library(addon SHARED "src/hello/addon.cc") diff --git a/tests/targets/node-api/package.json b/tests/targets/node-api/package.json index ad064835..02606770 100644 --- a/tests/targets/node-api/package.json +++ b/tests/targets/node-api/package.json @@ -5,7 +5,7 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "start": "node -p \"const addon = require('./index'); console.log(addon.hello());\"", "install": "cmake-js install", "postinstall": "cmake-js compile", "configure": "cmake-js configure", @@ -16,7 +16,7 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../", + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", "node-api-headers": "^1.1.0" } } diff --git a/tests/targets/node-dev/CMakeLists.txt b/tests/targets/node-dev/CMakeLists.txt index bfc0e642..ccf87e11 100644 --- a/tests/targets/node-dev/CMakeLists.txt +++ b/tests/targets/node-dev/CMakeLists.txt @@ -4,6 +4,11 @@ project(hello) set(CMAKEJS_USING_NODE_API FALSE) # this should have been done with '--link-level=0' on the CLI, testing this way for now +# For CMake IDE tools support (do yarn/install first and keep your node_modules folder around) +if(NOT DEFINED CMAKE_JS_VERSION) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif() + include(CMakeJS) add_library(addon SHARED "src/hello/addon.cc") diff --git a/tests/targets/node-dev/package.json b/tests/targets/node-dev/package.json index 0e5a880a..2248627e 100644 --- a/tests/targets/node-dev/package.json +++ b/tests/targets/node-dev/package.json @@ -5,7 +5,7 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node -p \"const addon = require('./build/lib/addon.node'); console.log(addon.hello());\"", + "start": "node -p \"const addon = require('./index'); console.log(addon.hello());\"", "install": "cmake-js install", "postinstall": "cmake-js compile", "configure": "cmake-js configure", @@ -16,6 +16,6 @@ "wipe": "cmake-js clean && rm -rvf ./node_modules" }, "dependencies": { - "cmake-js": "../../../" + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api" } } From 60d8fc2e7f693cea45f1951b4cae716f6afa7162 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 08:02:53 +0100 Subject: [PATCH 74/92] improved down stream config retrival --- share/cmake/CMakeJS.cmake | 63 +++-- .../CMakeLists.txt | 258 +++++++++--------- .../package.json | 27 +- 3 files changed, 185 insertions(+), 163 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index d0d150ee..c8b36440 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -1051,27 +1051,50 @@ include (${CMAKE_CURRENT_LIST_DIR}/CMakeJSTargets.cmake) check_required_components (cmake-js) -# # Not sure if this is needed... -# set (CMAKE_JS_SRC "@CMAKE_JS_SRC@") -# set (CMAKE_JS_INC @CMAKE_JS_INC@) -# set (CMAKE_JS_LIB "@CMAKE_JS_LIB@") -# set (CMAKE_JS_VERSION "@CMAKE_JS_VERSION@") -# set (CMAKE_JS_EXECUTABLE "@CMAKE_JS_EXECUTABLE@") -# set (CMAKE_JS_INC_FILES "") -# list (APPEND CMAKE_JS_INC_FILES "@CMAKE_JS_INC_FILES@") - -# if (CMAKEJS_NODE_API) -# set (NODE_API_HEADERS_DIR @NODE_API_HEADERS_DIR@) -# set (NODE_API_INC_FILES "") -# list (APPEND NODE_API_INC_FILES "@NODE_API_INC_FILES@") -# endif() - -# if (CMAKE_JS_NODE_ADDON_API) -# set (NODE_ADDON_API_DIR "@NODE_ADDON_API_DIR@") -# set (NODE_ADDON_API_INC_FILES "") -# list (APPEND NODE_ADDON_API_INC_FILES "@NODE_ADDON_API_INC_FILES@") -# endif () +# This codeblock make CMakeJS.cmake transportable, by +# also ensuring that anybody who picks up our CMake package +# will also have the CLI (which is a requirement of our CMake API) +# Resolve NodeJS development headers +# TODO: This code block is quite problematic, since: +# 1 - it might trigger a build run, depending on how the builder has set up their package.json scripts... +# 2 - it also currently assumes a preference for yarn over npm (and the others)... +# 3 - finally, because of how cmake-js works, it might create Ninja-build artefacts, +# even when the CMake user specifies a different generator to CMake manually... +# We could use 'add_custom_target()' with a user-side ARG for which package manager to use... +if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/node_modules") + # re: package manager to use, we could check IF_EXISTS for a 'yarn.lock' + # in the CMAKE_CURRENT_SOURCE_DIR, else use npm? + execute_process( + COMMAND yarn install + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE NODE_MODULES_DIR + ) + if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/node_modules") + message(FATAL_ERROR "Something went wrong - NodeJS modules installation failed!") + return() + endif() +endif() + +# this would be just 'yarn/npm add cmake-js' if this API were on our 'master' branch +if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js") + execute_process( + COMMAND yarn "add https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE CMAKE_JS_EXECUTABLE + ) + if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js") + message(FATAL_ERROR "Something went wrong - cmake-js installation failed!") + return() + endif() +endif() + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/share/cmake") + +message(STATUS "-- Appended cmake-js CMake API to your module path.") +message(STATUS "-- You may 'include(CMakeJS)' in your CMake project to use our API and/or relocatable targets.") +message(STATUS "-- Read more about our 'CMakeJS.cmake' API here:") +message(STATUS "-- https://github.com/cmake-js/cmake-js/blob/cmakejs_cmake_api/README.md") ]==]) # create cmake config file diff --git a/tests/api/hello_with_testing_and_packing/CMakeLists.txt b/tests/api/hello_with_testing_and_packing/CMakeLists.txt index 98cae7dc..672674a6 100644 --- a/tests/api/hello_with_testing_and_packing/CMakeLists.txt +++ b/tests/api/hello_with_testing_and_packing/CMakeLists.txt @@ -10,12 +10,6 @@ endif() include(CMakeJS) -# If this is not a subproject... -if (vendor_IS_TOP_LEVEL - # (and we're not building into the source tree) - AND vendor_IS_MASTER_PROJECT -) - cmakejs_create_napi_addon ( # CMAKEJS_ADDON_NAME addon @@ -25,128 +19,134 @@ cmakejs_create_napi_addon ( NAMESPACE vendor ) -include(GNUInstallDirs) # dont be alarmed by this - it wont install to your system! - -# CPack prep stuff... -export ( - TARGETS addon - FILE share/cmake/AddonTargets.cmake - NAMESPACE vendor:: -) - -install( - EXPORT AddonTargets - FILE addonTargets.cmake - NAMESPACE vendor:: - DESTINATION lib/cmake/vendor -) - -install(TARGETS addon - EXPORT AddonTargets - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" -) - - -file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" [==[ -@PACKAGE_INIT@ - -include (${CMAKE_CURRENT_LIST_DIR}/AddonTargets.cmake) - -check_required_components (vendor) - -]==]) - -# create cmake config file -configure_package_config_file ( - "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfig.cmake" - INSTALL_DESTINATION - "${CMAKE_INSTALL_LIBDIR}/cmake/Addon" -) -# generate the version file for the cmake config file -write_basic_package_version_file ( - "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfigVersion.cmake" - VERSION 1.0.0 - COMPATIBILITY AnyNewerVersion -) - -# copy the addon types -file(COPY - "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.js" - "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.ts" - "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.d.ts" - DESTINATION - "${PROJECT_BINARY_DIR}/lib" -) - -install(FILES - "${PROJECT_BINARY_DIR}/lib/addon.node.js" - "${PROJECT_BINARY_DIR}/lib/addon.node.ts" - "${PROJECT_BINARY_DIR}/lib/addon.node.d.ts" - DESTINATION - "${CMAKE_INSTALL_LIBDIR}" -) - -if(BUILD_TESTS) - # include(CTest) # If you want to see all the test dashboard targets, uncomment this - enable_testing() - include("${CMAKE_CURRENT_LIST_DIR}/tests/hello/tests.cmake") -endif() - -# set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... -set(CPACK_PACKAGE_VENDOR "vendor") -set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") -set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") # Multi-platform CMake API distribution -set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source") # No system spec as this is un-compiled source file distribution (i.e., the Javascript API) -set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_DESCRIPTION}) -set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) -set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_VERSION_MINOR}) -set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_VERSION_PATCH}) -set(CPACK_PACKAGE_VERSION_TWEAK ${PROJECT_VERSION_VERSION_TWEAK}) -#set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) -set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) -set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON) -set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) -set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") # Check out CPack's 'NSIS' installer for Win32, and the others! -set(CPACK_SOURCE_IGNORE_FILES - _CPack_Packages - /*.zip - /*.tar - /*.tar.* - /.env* - /.git/* - /.cmake - /.github - /.vs - /.vscode - /.cache - /.config - /.local - /dist - /doc - /docs - #/bin - #/lib - /usr - /out - #/build - /Release - /Debug - /MinSizeRel - /RelWithDebInfo - /downloads - /installed - /install - /node_modules - /vcpkg - /.*build.* - /package-lock.json - /yarn.lock - /\\\\.DS_Store +# If this is not a subproject... +if (vendor_IS_TOP_LEVEL + # (and we're not building into the source tree) + AND vendor_IS_MASTER_PROJECT ) -include(CPack) -endif () + include(GNUInstallDirs) # dont be alarmed by this - it wont install to your system! + + # CPack prep stuff... + export ( + TARGETS addon + FILE share/cmake/AddonTargets.cmake + NAMESPACE vendor:: + ) + + install( + EXPORT AddonTargets + FILE addonTargets.cmake + NAMESPACE vendor:: + DESTINATION lib/cmake/vendor + ) + + install(TARGETS addon + EXPORT AddonTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + ) + + + file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" [==[ + @PACKAGE_INIT@ + + include (${CMAKE_CURRENT_LIST_DIR}/AddonTargets.cmake) + + check_required_components (vendor) + + ]==]) + + # create cmake config file + configure_package_config_file ( + "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfig.cmake" + INSTALL_DESTINATION + "${CMAKE_INSTALL_LIBDIR}/cmake/Addon" + ) + # generate the version file for the cmake config file + write_basic_package_version_file ( + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfigVersion.cmake" + VERSION 1.0.0 + COMPATIBILITY AnyNewerVersion + ) + + # copy the addon types + file(COPY + "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.js" + "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.ts" + "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.d.ts" + DESTINATION + "${PROJECT_BINARY_DIR}/lib" + ) + + install(FILES + "${PROJECT_BINARY_DIR}/lib/addon.node.js" + "${PROJECT_BINARY_DIR}/lib/addon.node.ts" + "${PROJECT_BINARY_DIR}/lib/addon.node.d.ts" + DESTINATION + "${CMAKE_INSTALL_LIBDIR}" + ) + + if(BUILD_TESTS) + # include(CTest) # If you want to see all the test dashboard targets, uncomment this + enable_testing() + include("${CMAKE_CURRENT_LIST_DIR}/tests/hello/tests.cmake") + endif() + + # set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... + set(CPACK_PACKAGE_VENDOR "vendor") + set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") + set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") # Multi-platform CMake API distribution + set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source") # No system spec as this is un-compiled source file distribution (i.e., the Javascript API) + set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_DESCRIPTION}) + set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) + set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_VERSION_MINOR}) + set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_VERSION_PATCH}) + set(CPACK_PACKAGE_VERSION_TWEAK ${PROJECT_VERSION_VERSION_TWEAK}) + #set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) + set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) + set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON) + set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) + set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") # Check out CPack's 'NSIS' installer for Win32, and the others! + set(CPACK_SOURCE_IGNORE_FILES + _CPack_Packages + /*.zip + /*.tar + /*.tar.* + /.env* + /.git/* + /.cmake + /.github + /.vs + /.vscode + /.cache + /.config + /.local + /dist + /doc + /docs + #/bin + #/lib + /usr + /out + #/build + /Release + /Debug + /MinSizeRel + /RelWithDebInfo + /downloads + /installed + /install + /node_modules + /vcpkg + /.*build.* + /package-lock.json + /yarn.lock + /\\\\.DS_Store + ) + include(CPack) + +endif () # *_IS_TOP_LEVEL_PROJECT diff --git a/tests/api/hello_with_testing_and_packing/package.json b/tests/api/hello_with_testing_and_packing/package.json index a7122a25..c62723be 100644 --- a/tests/api/hello_with_testing_and_packing/package.json +++ b/tests/api/hello_with_testing_and_packing/package.json @@ -5,19 +5,18 @@ "main": "index.js", "license": "MIT", "scripts": { - "start": "node ./index.js", - "install": "cmake-js install", + "start": "node ./index.js", + "install": "cmake-js install", "postinstall": "cmake-js compile --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", - "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "configure": "cmake-js configure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", "reconfigure": "cmake-js reconfigure --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", - "build": "cmake-js build", - "rebuild": "cmake-js rebuild", - "clean": "cmake-js clean", - "wipe": "cmake-js clean && rm -rvf ./node_modules", - - "ctest": "ctest --test-dir ./build --rerun-failed --output-on-failure --verbose", - "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./dist", - "cdist": "cpack --config ./build/CPackConfig.cmake -B ./dist" + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules", + "ctest": "ctest --test-dir ./build --rerun-failed --output-on-failure --verbose", + "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./dist", + "cdist": "cpack --config ./build/CPackConfig.cmake -B ./dist" }, "dependencies": { "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", @@ -25,7 +24,7 @@ "node-api-headers": "^1.1.0" }, "devDependencies": { - "@types/node": "^20.11.14", - "typescript": "^5.3.3" - } + "@types/node": "^20.11.14", + "typescript": "^5.3.3" + } } From f5ad29e2ab192e05e79bd948110f01a2dd778a44 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 08:04:26 +0100 Subject: [PATCH 75/92] Tell downstream Cmake package users what to do --- .github/workflows/cmake.yaml | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 .github/workflows/cmake.yaml diff --git a/.github/workflows/cmake.yaml b/.github/workflows/cmake.yaml new file mode 100644 index 00000000..6f9eaab4 --- /dev/null +++ b/.github/workflows/cmake.yaml @@ -0,0 +1,97 @@ +name: Test + +on: + push: + pull_request: + +jobs: + test: + name: Run tests for ${{ matrix.os }}-${{ matrix.arch }}-${{ matrix.libc }} + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + include: + # windows + - os: windows-2019 + arch: x64 + - os: windows-2022 + arch: x64 + - os: windows-2019 + arch: x86 + # macos + - os: macos-12 + arch: x64 + # - os: macos-11 + # arch: arm64 + - os: macos-11 + arch: x64 + # linux + - os: ubuntu-22.04 + arch: x64 + - os: ubuntu-20.04 + arch: x64 + # linux-libc + - os: ubuntu-latest + arch: arm64 + docker-arch: linux/arm64 + docker-image: node:14-bullseye + - os: ubuntu-latest + arch: arm + docker-arch: linux/arm/v7 + docker-image: node:14-bullseye + # linux-musl + - os: ubuntu-latest + arch: x64 + docker-arch: linux/amd64 + docker-image: node:14-alpine + libc: musl + + steps: + - uses: actions/checkout@v3 + + - name: Use Node.js 14.x + if: ${{ !matrix.docker-arch }} + uses: actions/setup-node@v3 + with: + architecture: ${{ matrix.arch }} + node-version: 14.x + + - name: run tests + if: ${{ !matrix.docker-arch }} + shell: bash + run: | + npm install + npm test + env: + CI: true + npm_config_build_from_source: true + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + if: matrix.docker-arch + - name: run tests (in docker) + uses: addnab/docker-run-action@v3 + if: matrix.docker-arch + with: + image: ${{ matrix.docker-image }} + # shell: bash + options: --platform=${{ matrix.docker-arch }} -v ${{ github.workspace }}:/work -e CI=1 + run: | + if command -v apt-get &> /dev/null + then + apt-get update + apt-get install -y cmake + elif command -v apk &> /dev/null + then + apk update + apk add cmake make g++ gcc + fi + + cd /work + + cd /tests/api/hello_consumer + + npm install + npm run start From 1664ccb12b31d4d9c5b1083e9f9644cac9100a99 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 08:04:43 +0100 Subject: [PATCH 76/92] Tell downstream Cmake package users what to do --- share/cmake/CMakeJS.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index c8b36440..127b4991 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -1091,6 +1091,7 @@ endif() list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/share/cmake") +# Tell the user what to do message(STATUS "-- Appended cmake-js CMake API to your module path.") message(STATUS "-- You may 'include(CMakeJS)' in your CMake project to use our API and/or relocatable targets.") message(STATUS "-- Read more about our 'CMakeJS.cmake' API here:") From 02ca512009843115f8bd67a17abfdfb84ebdad50 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 08:05:10 +0100 Subject: [PATCH 77/92] removed a test --- .github/workflows/cmake.yaml | 97 ------------------------------------ 1 file changed, 97 deletions(-) delete mode 100644 .github/workflows/cmake.yaml diff --git a/.github/workflows/cmake.yaml b/.github/workflows/cmake.yaml deleted file mode 100644 index 6f9eaab4..00000000 --- a/.github/workflows/cmake.yaml +++ /dev/null @@ -1,97 +0,0 @@ -name: Test - -on: - push: - pull_request: - -jobs: - test: - name: Run tests for ${{ matrix.os }}-${{ matrix.arch }}-${{ matrix.libc }} - runs-on: ${{ matrix.os }} - - strategy: - fail-fast: false - matrix: - include: - # windows - - os: windows-2019 - arch: x64 - - os: windows-2022 - arch: x64 - - os: windows-2019 - arch: x86 - # macos - - os: macos-12 - arch: x64 - # - os: macos-11 - # arch: arm64 - - os: macos-11 - arch: x64 - # linux - - os: ubuntu-22.04 - arch: x64 - - os: ubuntu-20.04 - arch: x64 - # linux-libc - - os: ubuntu-latest - arch: arm64 - docker-arch: linux/arm64 - docker-image: node:14-bullseye - - os: ubuntu-latest - arch: arm - docker-arch: linux/arm/v7 - docker-image: node:14-bullseye - # linux-musl - - os: ubuntu-latest - arch: x64 - docker-arch: linux/amd64 - docker-image: node:14-alpine - libc: musl - - steps: - - uses: actions/checkout@v3 - - - name: Use Node.js 14.x - if: ${{ !matrix.docker-arch }} - uses: actions/setup-node@v3 - with: - architecture: ${{ matrix.arch }} - node-version: 14.x - - - name: run tests - if: ${{ !matrix.docker-arch }} - shell: bash - run: | - npm install - npm test - env: - CI: true - npm_config_build_from_source: true - - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - if: matrix.docker-arch - - name: run tests (in docker) - uses: addnab/docker-run-action@v3 - if: matrix.docker-arch - with: - image: ${{ matrix.docker-image }} - # shell: bash - options: --platform=${{ matrix.docker-arch }} -v ${{ github.workspace }}:/work -e CI=1 - run: | - if command -v apt-get &> /dev/null - then - apt-get update - apt-get install -y cmake - elif command -v apk &> /dev/null - then - apk update - apk add cmake make g++ gcc - fi - - cd /work - - cd /tests/api/hello_consumer - - npm install - npm run start From a2451a9a2ae6d0e4987c779ee81918d6a9d3db07 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 08:46:04 +0100 Subject: [PATCH 78/92] Refactord to provide more functions --- share/cmake/CMakeJS.cmake | 400 +++++++++++++++++++++----------------- 1 file changed, 224 insertions(+), 176 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 127b4991..cab786a9 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -74,6 +74,215 @@ function(_cmakejs_normalize_path var) set("${var}" "${path}" PARENT_SCOPE) endfunction() +#[=============================================================================[ +Provides CMAKE_JS_EXECUTABLE +]=============================================================================]# +function(cmakejs_find_cmakejs_executable) + # Check for cmake-js installations + find_program(CMAKE_JS_EXECUTABLE + NAMES "cmake-js" "cmake-js.exe" + PATHS "$ENV{PATH}" "$ENV{ProgramFiles}/cmake-js" + DOC "cmake-js system executable binary" + REQUIRED + ) + if(NOT CMAKE_JS_EXECUTABLE) + find_program(CMAKE_JS_EXECUTABLE + NAMES "cmake-js" "cmake-js.exe" + PATHS "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/bin" + DOC "cmake-js project-local npm package binary" + REQUIRED + ) + if (NOT CMAKE_JS_EXECUTABLE) + message(FATAL_ERROR "cmake-js not found! Please run 'npm install' and try again.") + return() + endif() + endif() + _cmakejs_normalize_path(CMAKE_JS_EXECUTABLE) + string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_EXECUTABLE "${CMAKE_JS_EXECUTABLE}") + set(CMAKE_JS_EXECUTABLE ${CMAKE_JS_EXECUTABLE} PARENT_SCOPE) # vars defined in functions only apply to their own scope, so this is needed! +endfunction() + +#[=============================================================================[ +Provides CMAKE_JS_INC +Requires CMAKE_JS_EXECUTABLE +(might accept a '--log-level' arg in future for more verbose output) +]=============================================================================]# +function(cmakejs_print_cmakejs_include) + execute_process( + COMMAND "${CMAKE_JS_EXECUTABLE}" "print-cmakejs-include" "--log-level error" "--generator ${CMAKE_GENERATOR}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE CMAKE_JS_INC + ) + # Strip the var of any unusual chars that might break the paths... + _cmakejs_normalize_path(CMAKE_JS_INC) + string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_INC "${CMAKE_JS_INC}") + set(CMAKE_JS_INC "${CMAKE_JS_INC}" PARENT_SCOPE) +endfunction() + +#[=============================================================================[ +Provides CMAKE_JS_SRC +Requires CMAKE_JS_EXECUTABLE +(might accept a '--log-level' arg in future for more verbose output) +]=============================================================================]# +function(cmakejs_print_cmakejs_src) + execute_process( + COMMAND "${CMAKE_JS_EXECUTABLE}" "print-cmakejs-src" "--log-level error" "--generator ${CMAKE_GENERATOR}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE CMAKE_JS_SRC + ) + # Strip the var of any unusual chars that might break the paths... + _cmakejs_normalize_path(CMAKE_JS_SRC) + string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_SRC "${CMAKE_JS_SRC}") + set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" PARENT_SCOPE) +endfunction() + +#[=============================================================================[ +Provides CMAKE_JS_LIB +Requires CMAKE_JS_EXECUTABLE +(might accept a '--log-level' arg in future for more verbose output) +]=============================================================================]# +function(cmakejs_print_cmakejs_lib) + execute_process( + COMMAND "${CMAKE_JS_EXECUTABLE}" "print-cmakejs-lib" "--log-level error" "--generator ${CMAKE_GENERATOR}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE CMAKE_JS_LIB + ) + _cmakejs_normalize_path(CMAKE_JS_LIB) + string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_LIB "${CMAKE_JS_LIB}") + set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" PARENT_SCOPE) +endfunction() + +#[=============================================================================[ +Get the in-use NodeJS binary for executing NodeJS commands in CMake scripts. + +Provides + +:: + + NODE_EXECUTABLE, the NodeJS runtime binary being used + NODE_VERSION, the version of the NodeJS runtime binary being used + +]=============================================================================]# +function(cmakejs_acquire_node_executable) + find_program(NODE_EXECUTABLE + NAMES "node" "node.exe" + PATHS "$ENV{PATH}" "$ENV{ProgramFiles}/nodejs" + DOC "NodeJs executable binary" + REQUIRED + ) + if (NOT NODE_EXECUTABLE) + message(FATAL_ERROR "NodeJS installation not found! Please check your paths and try again.") + return() + endif() + + execute_process( + COMMAND "${NODE_EXECUTABLE}" "--version" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE NODE_VERSION + ) + string(REGEX REPLACE "[\r\n\"]" "" NODE_VERSION "${NODE_VERSION}") + set(NODE_VERSION "${NODE_VERSION}" CACHE STRING "" FORCE) + + if(VERBOSE) + message(STATUS "NODE_EXECUTABLE: ${NODE_EXECUTABLE}") + message(STATUS "NODE_VERSION: ${NODE_VERSION}") + endif() +endfunction() + +#[=============================================================================[ +Get NodeJS C Addon development files. + +Provides +:: + + NODE_API_HEADERS_DIR, where to find node_api.h, etc. + NODE_API_INC_FILES, the headers required to use Node API. + +]=============================================================================]# +function(cmakejs_acquire_napi_c_files) + execute_process( + COMMAND "${NODE_EXECUTABLE}" -p "require('node-api-headers').include_dir" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE NODE_API_HEADERS_DIR + # COMMAND_ERROR_IS_FATAL ANY - crashes on ARM64 builds? unfortunate! + ) + string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") + + # relocate... + set(_NODE_API_INC_FILES "") + file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") + unset(_NODE_API_INC_FILES) + + unset(NODE_API_HEADERS_DIR CACHE) + # target include directories (as if 'node-api-headers' were an isolated CMake project...) + set(NODE_API_HEADERS_DIR + $ + $ + ) + set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} PARENT_SCOPE) # dont wrap this one in quotes; it breaks! + + # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! + set(NODE_API_INC_FILES "") + file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") + set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" PARENT_SCOPE) + source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") + # end IDE support codeblock + # it's not a 'source_group' for targets! + # VS users will see the above globbed headers as a filegroup named "Node API (C)" + # and that is literally all that this 'source_group' function does. + # it is not the same as 'target_sources' - so, globbing was ok here! + + if(VERBOSE) + message(STATUS "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") + endif() +endfunction() + +#[=============================================================================[ +Get NodeJS C++ Addon development files. + +Provides +:: + + NODE_ADDON_API_DIR, where to find napi.h, etc. + NODE_ADDON_API_INC_FILES, the headers required to use Node Addon API. + +]=============================================================================]# +function(cmakejs_acquire_napi_cpp_files) + execute_process( + COMMAND "${NODE_EXECUTABLE}" -p "require('node-addon-api').include" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE NODE_ADDON_API_DIR + # COMMAND_ERROR_IS_FATAL ANY -these vars seem to error on ARM64 builds ...? + ) + string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}") + + # relocate... + set(_NODE_ADDON_API_INC_FILES "") + file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") + unset(_NODE_ADDON_API_INC_FILES) + + unset(NODE_ADDON_API_DIR CACHE) + # target include directories (as if 'node-addon-api' were an isolated CMake project...) + set(NODE_ADDON_API_DIR + $ + $ + ) + set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} PARENT_SCOPE) + + # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! + set(NODE_ADDON_API_INC_FILES "") + file(GLOB_RECURSE NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") + set(NODE_ADDON_API_INC_FILES ${NODE_ADDON_API_INC_FILES} PARENT_SCOPE) + source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") + + if(VERBOSE) + message(STATUS "NODE_ADDON_API_DIR: ${NODE_ADDON_API_DIR}") + endif() +endfunction() + + #[=======================================================================[ FindCMakeJs.cmake -------- @@ -93,6 +302,13 @@ This module defines CMAKE_JS_EXECUTABLE, the cmake-js binary (global) CMAKE_JS_NPM_PACKAGE, the cmake-js binary (local) + Targets: + + cmake-js::node-dev + cmake-js::node-api + cmake-js::node-addon-api + cmake-js::cmake-js + ]=======================================================================]# # CMAKE_JS_VERSION is defined on all platforms when calling from cmake-js. @@ -107,57 +323,20 @@ if (NOT DEFINED CMAKE_JS_VERSION) message(STATUS "CMake Calling...") endif() - # Check for cmake-js installations - find_program(CMAKE_JS_EXECUTABLE - NAMES "cmake-js" "cmake-js.exe" - PATHS "$ENV{PATH}" "$ENV{ProgramFiles}/cmake-js" - DOC "cmake-js system executable binary" - REQUIRED - ) - if(NOT CMAKE_JS_EXECUTABLE) - find_program(CMAKE_JS_EXECUTABLE - NAMES "cmake-js" "cmake-js.exe" - PATHS "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/bin" - DOC "cmake-js project-local npm package binary" - REQUIRED - ) - if (NOT CMAKE_JS_EXECUTABLE) - message(FATAL_ERROR "cmake-js not found! Please run 'npm install' and try again.") - return() - endif() - endif() - - _cmakejs_normalize_path(CMAKE_JS_EXECUTABLE) - string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_EXECUTABLE "${CMAKE_JS_EXECUTABLE}") + cmakejs_find_cmakejs_executable() + set(CMAKE_JS_EXECUTABLE ${CMAKE_JS_EXECUTABLE} CACHE FILEPATH "cmake-js CLI executable.") # Execute the CLI commands, and write their outputs into the cached vars # where the remaining build processes expect them to be... - execute_process( - COMMAND "${CMAKE_JS_EXECUTABLE}" "print-cmakejs-include" "--log-level error" "--generator ${CMAKE_GENERATOR}" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE CMAKE_JS_INC - ) - execute_process( - COMMAND "${CMAKE_JS_EXECUTABLE}" "print-cmakejs-src" "--log-level error" "--generator ${CMAKE_GENERATOR}" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE CMAKE_JS_SRC - ) - - execute_process( - COMMAND "${CMAKE_JS_EXECUTABLE}" "print-cmakejs-lib" "--log-level error" "--generator ${CMAKE_GENERATOR}" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE CMAKE_JS_LIB - ) + cmakejs_print_cmakejs_include() + set(CMAKE_JS_INC "${CMAKE_JS_INC}") # Don't CACHE FORCE this yet! it will change later... - # Strip the vars of any unusual chars that might break the paths... - _cmakejs_normalize_path(CMAKE_JS_INC) - _cmakejs_normalize_path(CMAKE_JS_SRC) - _cmakejs_normalize_path(CMAKE_JS_LIB) + cmakejs_print_cmakejs_include() # we can CACHE FORCE the others now, as they won't change... + set(CMAKE_JS_SRC "${CMAKE_JS_SRC}" CACHE FILEPATH "cmake-js source file(s) to be compiled in.") - string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_INC "${CMAKE_JS_INC}") - string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_SRC "${CMAKE_JS_SRC}") - string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_LIB "${CMAKE_JS_LIB}") + cmakejs_print_cmakejs_lib() + set(CMAKE_JS_LIB "${CMAKE_JS_LIB}" CACHE FILEPATH "cmake-js libraries(s) to be linked in.") else () @@ -194,43 +373,6 @@ if(VERBOSE) message(DEBUG "CMAKE_JS_LIB = ${CMAKE_JS_LIB}") endif() -#[=============================================================================[ -Get the in-use NodeJS binary for executing NodeJS commands in CMake scripts. - -Provides - -:: - - NODE_EXECUTABLE, the NodeJS runtime binary being used - NODE_VERSION, the version of the NodeJS runtime binary being used - -]=============================================================================]# -function(cmakejs_acquire_node_executable) - find_program(NODE_EXECUTABLE - NAMES "node" "node.exe" - PATHS "$ENV{PATH}" "$ENV{ProgramFiles}/nodejs" - DOC "NodeJs executable binary" - REQUIRED - ) - if (NOT NODE_EXECUTABLE) - message(FATAL_ERROR "NodeJS installation not found! Please check your paths and try again.") - return() - endif() - - execute_process( - COMMAND "${NODE_EXECUTABLE}" "--version" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - OUTPUT_VARIABLE NODE_VERSION - ) - string(REGEX REPLACE "[\r\n\"]" "" NODE_VERSION "${NODE_VERSION}") - set(NODE_VERSION "${NODE_VERSION}" CACHE STRING "" FORCE) - - if(VERBOSE) - message(STATUS "NODE_EXECUTABLE: ${NODE_EXECUTABLE}") - message(STATUS "NODE_VERSION: ${NODE_VERSION}") - endif() -endfunction() - # Resolve NodeJS development headers # TODO: This code block is quite problematic, since: # 1 - it might trigger a build run, depending on how the builder has set up their package.json scripts... @@ -250,100 +392,6 @@ if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/node_modules") endif() endif() -#[=============================================================================[ -Get NodeJS C Addon development files. - -Provides -:: - - NODE_API_HEADERS_DIR, where to find node_api.h, etc. - NODE_API_INC_FILES, the headers required to use Node API. - -]=============================================================================]# -function(cmakejs_acquire_napi_c_files) - execute_process( - COMMAND "${NODE_EXECUTABLE}" -p "require('node-api-headers').include_dir" - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" - OUTPUT_VARIABLE NODE_API_HEADERS_DIR - # COMMAND_ERROR_IS_FATAL ANY - crashes on ARM64 builds? unfortunate! - ) - string(REGEX REPLACE "[\r\n\"]" "" NODE_API_HEADERS_DIR "${NODE_API_HEADERS_DIR}") - - # relocate... - set(_NODE_API_INC_FILES "") - file(GLOB_RECURSE _NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - file(COPY ${_NODE_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-api-headers") - unset(_NODE_API_INC_FILES) - - unset(NODE_API_HEADERS_DIR CACHE) - # target include directories (as if 'node-api-headers' were an isolated CMake project...) - set(NODE_API_HEADERS_DIR - $ - $ - ) - set(NODE_API_HEADERS_DIR ${NODE_API_HEADERS_DIR} PARENT_SCOPE) # dont wrap this one in quotes; it breaks! - - # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! - set(NODE_API_INC_FILES "") - file(GLOB_RECURSE NODE_API_INC_FILES "${NODE_API_HEADERS_DIR}/*.h") - set(NODE_API_INC_FILES "${NODE_API_INC_FILES}" PARENT_SCOPE) - source_group("Node API (C)" FILES "${NODE_API_INC_FILES}") - # end IDE support codeblock - # it's not a 'source_group' for targets! - # VS users will see the above globbed headers as a filegroup named "Node API (C)" - # and that is literally all that this 'source_group' function does. - # it is not the same as 'target_sources' - so, globbing was ok here! - - if(VERBOSE) - message(STATUS "NODE_API_HEADERS_DIR: ${NODE_API_HEADERS_DIR}") - endif() -endfunction() - -#[=============================================================================[ -Get NodeJS C++ Addon development files. - -Provides -:: - - NODE_ADDON_API_DIR, where to find napi.h, etc. - NODE_ADDON_API_INC_FILES, the headers required to use Node Addon API. - -]=============================================================================]# -function(cmakejs_acquire_napi_cpp_files) - execute_process( - COMMAND "${NODE_EXECUTABLE}" -p "require('node-addon-api').include" - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" - OUTPUT_VARIABLE NODE_ADDON_API_DIR - # COMMAND_ERROR_IS_FATAL ANY -these vars seem to error on ARM64 builds ...? - ) - string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR "${NODE_ADDON_API_DIR}") - - # relocate... - set(_NODE_ADDON_API_INC_FILES "") - file(GLOB_RECURSE _NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - file(COPY ${_NODE_ADDON_API_INC_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/node-addon-api") - unset(_NODE_ADDON_API_INC_FILES) - - unset(NODE_ADDON_API_DIR CACHE) - # target include directories (as if 'node-addon-api' were an isolated CMake project...) - set(NODE_ADDON_API_DIR - $ - $ - ) - set(NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR} PARENT_SCOPE) - - # this is just for IDE support only. Never pass globbed headers to 'target_sources()'! - set(NODE_ADDON_API_INC_FILES "") - file(GLOB_RECURSE NODE_ADDON_API_INC_FILES "${NODE_ADDON_API_DIR}/*.h") - set(NODE_ADDON_API_INC_FILES ${NODE_ADDON_API_INC_FILES} PARENT_SCOPE) - source_group("Node Addon API (C++)" FILES "${NODE_ADDON_API_INC_FILES}") - - if(VERBOSE) - message(STATUS "NODE_ADDON_API_DIR: ${NODE_ADDON_API_DIR}") - endif() -endfunction() - - #[=============================================================================[ Silently create an interface library (no output) with all Addon API dependencies resolved, for each feature that we offer; this is for Addon targets to link with. From 2a9f9a9d1a4879a52890cdda476cddf6c3b8b975 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 08:56:11 +0100 Subject: [PATCH 79/92] typo --- share/cmake/CMakeJS.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index cab786a9..d5a14f32 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -311,6 +311,8 @@ This module defines ]=======================================================================]# +# -- BEGIN 'always-on' codeblob; we provide the following blob, no matter the config. + # CMAKE_JS_VERSION is defined on all platforms when calling from cmake-js. # By checking whether this var is pre-defined, we can determine if we are # running from an npm script (via cmake-js), or from CMake directly... @@ -347,8 +349,6 @@ else () endif () -# 'always-on' codeblock; we provide the following blob, no matter the config. - # relocate... (only runs if no node_modules to fallback on.. i.e., on a fresh git clone. Expected behaviour..?) file(COPY ${CMAKE_JS_INC} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") @@ -392,6 +392,8 @@ if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/node_modules") endif() endif() +# -- END 'always-on' codeblob. + #[=============================================================================[ Silently create an interface library (no output) with all Addon API dependencies resolved, for each feature that we offer; this is for Addon targets to link with. From f4fc8bb52657e139f2a0475e2d9d093bfffa5769 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 09:31:57 +0100 Subject: [PATCH 80/92] Added a 'cmakejs_create_addon_bindings()' generator function for users --- CMakeLists.txt | 3 +++ share/cmake/CMakeJS.cmake | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28fa77f9..6e86fae5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,9 @@ if (CMakeJS_IS_TOP_LEVEL NAMESPACE CMakeJS ) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib") # usually set fur users by cmake-js cli... this is just for test + cmakejs_create_addon_bindings(demo) + # ...we're building the CMakeJS demo project message (STATUS "cmake-js::demo \n") diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index d5a14f32..51b73d86 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -282,6 +282,35 @@ function(cmakejs_acquire_napi_cpp_files) endif() endfunction() +#[=============================================================================[ +(experimental) +]=============================================================================]# +function(cmakejs_create_addon_bindings addon_target) + + # Figure out the path from the build dir to wherever the built addon went + file(RELATIVE_PATH _bindings_rel_path "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") + + # Use the addon name and relative path to create a 'configured' string (vars surrounded with @'s get evaluated) + string(CONFIGURE [[ +const @addon_target@ = require(`./@_bindings_rel_path@/@addon_target@.node`); +module.exports = @addon_target@; +]] + _bindings + @ONLY + ) + + # write the configured string to a file in the binary dir, providing a + # consistent binding point for every addon built! :) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${addon_target}.js" "${_bindings}") + + # Now, built addons can be found easier in Javascript: + # const my_addon = require('./build/') + + # If your CMake is not going into './build' then obvuously it should be + # changed; but, we should *never* write CMake-configured bindings file + # into anybody's source tree, as we might corrupt their work! ALWAYS + # put this kind of stuff into the binary dir! +endfunction() #[=======================================================================[ FindCMakeJs.cmake From 4f56fd0b79092741a74912583cf677aa216cd71a Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 09:35:24 +0100 Subject: [PATCH 81/92] Added 'cmakejs_create_addon_bindings()' generator function to 'create_addon()' --- share/cmake/CMakeJS.cmake | 3 +++ 1 file changed, 3 insertions(+) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 51b73d86..7a604050 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -950,6 +950,9 @@ cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_V "${_NAPI_GLOBAL_EXCEPTIONS_POLICY}" ) + # (experimental) :) + cmakejs_create_addon_bindings(${name}) + # Global exceptions policy unset(_NAPI_GLOBAL_EXCEPTIONS_POLICY) From 572348ea95819b4e6a93ff77c4afe787300823e7 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 10:17:50 +0100 Subject: [PATCH 82/92] Removed target output dir contraints by using 'cmakejs_create_addon_bindings()' --- CMakeLists.txt | 11 ++++------ share/cmake/CMakeJS.cmake | 21 +++++++++++++++---- .../CMakeLists.txt | 8 +++---- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e86fae5..9b4cead1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,9 +107,6 @@ if (CMakeJS_IS_TOP_LEVEL NAMESPACE CMakeJS ) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib") # usually set fur users by cmake-js cli... this is just for test - cmakejs_create_addon_bindings(demo) - # ...we're building the CMakeJS demo project message (STATUS "cmake-js::demo \n") @@ -233,13 +230,13 @@ check_required_components (cmake-js) "${CMAKE_CURRENT_LIST_DIR}/lib/demo.node.ts" "${CMAKE_CURRENT_LIST_DIR}/lib/demo.node.d.ts" DESTINATION - "${PROJECT_BINARY_DIR}/lib" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" ) install(FILES - "${PROJECT_BINARY_DIR}/lib/demo.node.js" - "${PROJECT_BINARY_DIR}/lib/demo.node.ts" - "${PROJECT_BINARY_DIR}/lib/demo.node.d.ts" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/demo.node.js" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/demo.node.ts" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/demo.node.d.ts" DESTINATION "${CMAKE_INSTALL_LIBDIR}" ) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 7a604050..11b8baf1 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -283,10 +283,22 @@ function(cmakejs_acquire_napi_cpp_files) endfunction() #[=============================================================================[ +Generate a Javascript bindings file to your built addon, at the root of your +build directory, providing a more predictable file to acquire your built addon +from instead of having to work out where your built addon went from the Javascript +side. + (experimental) ]=============================================================================]# function(cmakejs_create_addon_bindings addon_target) + # Check that this is a Node Addon target + get_target_property(is_addon_lib ${name} ${name}_IS_NAPI_ADDON_LIBRARY) + if(NOT TARGET ${name} OR NOT is_addon_lib) + message(SEND_ERROR "'cmakejs_create_addon_bindings()' called on '${name}' which is not an existing napi addon library") + return() + endif() + # Figure out the path from the build dir to wherever the built addon went file(RELATIVE_PATH _bindings_rel_path "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") @@ -301,7 +313,7 @@ module.exports = @addon_target@; # write the configured string to a file in the binary dir, providing a # consistent binding point for every addon built! :) - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${addon_target}.js" "${_bindings}") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${addon_target}.node.js" "${_bindings}") # Now, built addons can be found easier in Javascript: # const my_addon = require('./build/') @@ -310,6 +322,7 @@ module.exports = @addon_target@; # changed; but, we should *never* write CMake-configured bindings file # into anybody's source tree, as we might corrupt their work! ALWAYS # put this kind of stuff into the binary dir! + message(STATUS "-- Created Javascript bindings: ${addon_target}.node.js") endfunction() #[=======================================================================[ @@ -922,9 +935,9 @@ cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_V PREFIX "" SUFFIX ".node" - ARCHIVE_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" - LIBRARY_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" - RUNTIME_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/bin" + # ARCHIVE_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Actually we dont need to enforce an opinion here! + # LIBRARY_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Instead, we call 'cmakejs_create_addon_bindings()' + # RUNTIME_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/bin" # on this target, and the user can just 'require()' that file! # # Conventional C++-style debug settings might be useful to have... # Getting Javascript bindings to grep different paths is tricky, though! diff --git a/tests/api/hello_with_testing_and_packing/CMakeLists.txt b/tests/api/hello_with_testing_and_packing/CMakeLists.txt index 672674a6..a6783b3a 100644 --- a/tests/api/hello_with_testing_and_packing/CMakeLists.txt +++ b/tests/api/hello_with_testing_and_packing/CMakeLists.txt @@ -79,13 +79,13 @@ if (vendor_IS_TOP_LEVEL "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.ts" "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.d.ts" DESTINATION - "${PROJECT_BINARY_DIR}/lib" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" ) install(FILES - "${PROJECT_BINARY_DIR}/lib/addon.node.js" - "${PROJECT_BINARY_DIR}/lib/addon.node.ts" - "${PROJECT_BINARY_DIR}/lib/addon.node.d.ts" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/addon.node.js" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/addon.node.ts" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/addon.node.d.ts" DESTINATION "${CMAKE_INSTALL_LIBDIR}" ) From 9f9c00243113465169b5b703a3ec90cd4ca3506c Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 10:31:12 +0100 Subject: [PATCH 83/92] rollback the bindings generation slightly (mark as experimental) --- CMakeLists.txt | 8 ++++---- share/cmake/CMakeJS.cmake | 6 +++--- tests/api/hello/index.js | 9 ++------- tests/api/hello_with_testing_and_packing/CMakeLists.txt | 8 ++++---- tests/api/hello_with_types/lib/addon.node.js | 7 ++----- 5 files changed, 15 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b4cead1..28fa77f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,13 +230,13 @@ check_required_components (cmake-js) "${CMAKE_CURRENT_LIST_DIR}/lib/demo.node.ts" "${CMAKE_CURRENT_LIST_DIR}/lib/demo.node.d.ts" DESTINATION - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" + "${PROJECT_BINARY_DIR}/lib" ) install(FILES - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/demo.node.js" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/demo.node.ts" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/demo.node.d.ts" + "${PROJECT_BINARY_DIR}/lib/demo.node.js" + "${PROJECT_BINARY_DIR}/lib/demo.node.ts" + "${PROJECT_BINARY_DIR}/lib/demo.node.d.ts" DESTINATION "${CMAKE_INSTALL_LIBDIR}" ) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index 11b8baf1..b0deb7ea 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -935,9 +935,9 @@ cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_V PREFIX "" SUFFIX ".node" - # ARCHIVE_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Actually we dont need to enforce an opinion here! - # LIBRARY_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Instead, we call 'cmakejs_create_addon_bindings()' - # RUNTIME_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/bin" # on this target, and the user can just 'require()' that file! + ARCHIVE_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Actually we might not need to enforce an opinion here! + LIBRARY_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Instead, we call 'cmakejs_create_addon_bindings()' + RUNTIME_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/bin" # on this target, and the user can just 'require()' that file! # # Conventional C++-style debug settings might be useful to have... # Getting Javascript bindings to grep different paths is tricky, though! diff --git a/tests/api/hello/index.js b/tests/api/hello/index.js index 4e5b1cdb..a4971b05 100644 --- a/tests/api/hello/index.js +++ b/tests/api/hello/index.js @@ -1,10 +1,5 @@ // This small codeblock in your root-level index.js allows others to consume // your addon as any other NodeJS module -const platform = process.platform; -var buildDir = "/build/lib/"; -if(platform === "win32") - buildDir = "\\build\\bin\\Release\\"; - -const hello = require(`.${buildDir}addon.node`); -module.exports = hello; +const addon = require(`./build/lib/addon.node`); +module.exports = addon; diff --git a/tests/api/hello_with_testing_and_packing/CMakeLists.txt b/tests/api/hello_with_testing_and_packing/CMakeLists.txt index a6783b3a..672674a6 100644 --- a/tests/api/hello_with_testing_and_packing/CMakeLists.txt +++ b/tests/api/hello_with_testing_and_packing/CMakeLists.txt @@ -79,13 +79,13 @@ if (vendor_IS_TOP_LEVEL "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.ts" "${CMAKE_CURRENT_LIST_DIR}/lib/addon.node.d.ts" DESTINATION - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" + "${PROJECT_BINARY_DIR}/lib" ) install(FILES - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/addon.node.js" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/addon.node.ts" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/addon.node.d.ts" + "${PROJECT_BINARY_DIR}/lib/addon.node.js" + "${PROJECT_BINARY_DIR}/lib/addon.node.ts" + "${PROJECT_BINARY_DIR}/lib/addon.node.d.ts" DESTINATION "${CMAKE_INSTALL_LIBDIR}" ) diff --git a/tests/api/hello_with_types/lib/addon.node.js b/tests/api/hello_with_types/lib/addon.node.js index 55c8ada8..572207ec 100644 --- a/tests/api/hello_with_types/lib/addon.node.js +++ b/tests/api/hello_with_types/lib/addon.node.js @@ -4,8 +4,5 @@ var buildDir = "/build/lib/"; if(platform === "win32") buildDir = "\\build\\bin\\Release\\"; -const addon - = require(`..${buildDir}addon -.node`); -module.exports = addon -; +const addon = require(`..${buildDir}addon.node`); +module.exports = addon; From a8a0f718ceb85034146ace2ba4cbb8433785c519 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 12:59:45 +0100 Subject: [PATCH 84/92] Added 'hello_with_curl' example project for sending HTTP POST requests --- tests/api/hello_with_curl/.gitignore | 3 + tests/api/hello_with_curl/CMakeLists.txt | 50 ++++++ tests/api/hello_with_curl/README.md | 7 + .../api/hello_with_curl/include/hello/addon.h | 14 ++ tests/api/hello_with_curl/index.js | 4 + tests/api/hello_with_curl/lib/addon.node.d.ts | 8 + tests/api/hello_with_curl/lib/addon.node.js | 8 + tests/api/hello_with_curl/lib/addon.node.ts | 30 ++++ tests/api/hello_with_curl/package.json | 29 ++++ tests/api/hello_with_curl/src/hello/addon.cpp | 150 ++++++++++++++++++ tests/api/hello_with_curl/src/hello/post.cpp | 60 +++++++ tests/api/hello_with_curl/tsconfig.json | 20 +++ 12 files changed, 383 insertions(+) create mode 100644 tests/api/hello_with_curl/.gitignore create mode 100644 tests/api/hello_with_curl/CMakeLists.txt create mode 100644 tests/api/hello_with_curl/README.md create mode 100644 tests/api/hello_with_curl/include/hello/addon.h create mode 100644 tests/api/hello_with_curl/index.js create mode 100644 tests/api/hello_with_curl/lib/addon.node.d.ts create mode 100644 tests/api/hello_with_curl/lib/addon.node.js create mode 100644 tests/api/hello_with_curl/lib/addon.node.ts create mode 100644 tests/api/hello_with_curl/package.json create mode 100644 tests/api/hello_with_curl/src/hello/addon.cpp create mode 100644 tests/api/hello_with_curl/src/hello/post.cpp create mode 100644 tests/api/hello_with_curl/tsconfig.json diff --git a/tests/api/hello_with_curl/.gitignore b/tests/api/hello_with_curl/.gitignore new file mode 100644 index 00000000..4c561b01 --- /dev/null +++ b/tests/api/hello_with_curl/.gitignore @@ -0,0 +1,3 @@ +node_modules +build +install diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt new file mode 100644 index 00000000..398f345f --- /dev/null +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.15) + +project(hello) + +find_package(CURL REQUIRED) + +include(CMakeJS) + +cmakejs_create_napi_addon( + addon + "src/hello/addon.cpp" + "src/hello/post.cpp" + NAMESPACE vendor + EXCEPTIONS YES +) + +target_link_libraries(addon PRIVATE CURL::libcurl) +target_include_directories(addon PRIVATE ${CURL_INCLUDE_DIR}) + +target_include_directories(addon PUBLIC + $ + $ +) +configure_file( + "include/hello/addon.h" # from SOURCE_DIR... + "include/hello/addon.h" # ..to BINARY_DIR +) + +include(GNUInstallDirs) +# CPack prep stuff... +export ( + TARGETS addon + FILE share/cmake/AddonTargets.cmake + NAMESPACE hello:: +) + +install(TARGETS addon + EXPORT AddonTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) + +install( + EXPORT AddonTargets + FILE addonTargets.cmake + NAMESPACE vendor:: + DESTINATION lib/cmake/hello +) diff --git a/tests/api/hello_with_curl/README.md b/tests/api/hello_with_curl/README.md new file mode 100644 index 00000000..d6b88e4e --- /dev/null +++ b/tests/api/hello_with_curl/README.md @@ -0,0 +1,7 @@ +## I am an addon with cURL API support. + +You can build me, export me as a Javscript module, and share your comments with importing consumers' users' intellisense engines; write descriptions of the cool functions and features that you build, so that your users will have an even better experience using your addon. + +Please see my package.json to understand how to make HTTP POST requests with me. + +Powered by cmake-js CLI diff --git a/tests/api/hello_with_curl/include/hello/addon.h b/tests/api/hello_with_curl/include/hello/addon.h new file mode 100644 index 00000000..a4a72783 --- /dev/null +++ b/tests/api/hello_with_curl/include/hello/addon.h @@ -0,0 +1,14 @@ +/** + * +*/ + +#ifndef HELLO_ADDON_H_INCLUDED +#define HELLO_ADDON_H_INCLUDED + +#include + +#include + +int hello_addon_post(const char *url, const char *data); + +#endif // HELLO_ADDON_H_INCLUDED diff --git a/tests/api/hello_with_curl/index.js b/tests/api/hello_with_curl/index.js new file mode 100644 index 00000000..2388022f --- /dev/null +++ b/tests/api/hello_with_curl/index.js @@ -0,0 +1,4 @@ +// This small codeblock in your root-level index.js allows others to consume +// your addon as any other NodeJS module +const hello_with_curl = require(`./lib/addon.node`); +module.exports = hello_with_curl; diff --git a/tests/api/hello_with_curl/lib/addon.node.d.ts b/tests/api/hello_with_curl/lib/addon.node.d.ts new file mode 100644 index 00000000..fa0cd7e9 --- /dev/null +++ b/tests/api/hello_with_curl/lib/addon.node.d.ts @@ -0,0 +1,8 @@ +declare interface addon { + hello(): string; + napi_version(): number; + curl_version(): number; + post(): string; +} +declare const addon: addon; +export = addon; diff --git a/tests/api/hello_with_curl/lib/addon.node.js b/tests/api/hello_with_curl/lib/addon.node.js new file mode 100644 index 00000000..572207ec --- /dev/null +++ b/tests/api/hello_with_curl/lib/addon.node.js @@ -0,0 +1,8 @@ +const platform = process.platform; +var buildDir = "/build/lib/"; + +if(platform === "win32") + buildDir = "\\build\\bin\\Release\\"; + +const addon = require(`..${buildDir}addon.node`); +module.exports = addon; diff --git a/tests/api/hello_with_curl/lib/addon.node.ts b/tests/api/hello_with_curl/lib/addon.node.ts new file mode 100644 index 00000000..75e5dfa6 --- /dev/null +++ b/tests/api/hello_with_curl/lib/addon.node.ts @@ -0,0 +1,30 @@ +/** + * The 'addon' C++ addon interface. + */ +interface addon { + /** + * Returns a string, confirming the cmake-js addon is online. + * @returns string + */ + hello(): string; + /** + * Returns a number, confirming the Napi Addon API version number. + * @returns number + */ + napi_version(): number; + /** + * Returns a number, confirming the Curl API version number. + * @returns number + */ + curl_version(): number; + /** + * Sends an HTTP POST request using the CURL API. + * The request is sent to 'url'. Both the url and the + * data to be posted must be Javascript strings (for now). + * Returns a number, confirming the POST request status. + * @returns number + */ + post(url: string, data: string): string; +} +const addon: addon = require('../build/lib/addon.node'); +export = addon; diff --git a/tests/api/hello_with_curl/package.json b/tests/api/hello_with_curl/package.json new file mode 100644 index 00000000..1ca08a3f --- /dev/null +++ b/tests/api/hello_with_curl/package.json @@ -0,0 +1,29 @@ +{ + "name": "@vendor/hello_with_types", + "version": "1.0.0", + "description": "A test addon with Typescript support made using CMakeJS.cmake", + "main": "index.js", + "license": "MIT", + "scripts": { + "start": "node ./index.js", + "install": "cmake-js install", + "postinstall": "cmake-js compile", + "configure": "cmake-js configure", + "reconfigure": "cmake-js reconfigure", + "build": "cmake-js build", + "rebuild": "cmake-js rebuild", + "clean": "cmake-js clean", + "wipe": "cmake-js clean && rm -rvf ./node_modules", + + "curl:post": "node -p \"const addon = require('./index'); addon.post('https://httpbin.org/anything' ,'name=nathanjhood&project=cmake-js');;\"" + }, + "dependencies": { + "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", + "node-addon-api": "^7.1.0", + "node-api-headers": "^1.1.0" + }, + "devDependencies": { + "@types/node": "^20.11.14", + "typescript": "^5.3.3" + } +} diff --git a/tests/api/hello_with_curl/src/hello/addon.cpp b/tests/api/hello_with_curl/src/hello/addon.cpp new file mode 100644 index 00000000..887d1c4e --- /dev/null +++ b/tests/api/hello_with_curl/src/hello/addon.cpp @@ -0,0 +1,150 @@ +/** + * @file addon.cpp + * @brief A quick 'HTTP POST' Napi Addon in C++ with CURL +*/ + +#include + +// Required header and C++ flag +#if __has_include() && BUILDING_NODE_EXTENSION && NAPI_CPP_EXCEPTIONS + +#include + +#include "hello/addon.h" + +#ifndef STRINGIFY +# define STRINGIFY_HELPER(n) #n +# define STRINGIFY(n) STRINGIFY_HELPER(n) +#endif + +namespace Napi +{ +namespace NAPI_CPP_CUSTOM_NAMESPACE +{ + +Napi::Value Hello(const Napi::CallbackInfo& info) { + return Napi::String::New(info.Env(), STRINGIFY(CMAKEJS_ADDON_NAME)".node is online!"); +} + +Napi::Value NapiVersion(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), NAPI_VERSION); +} + +Napi::Value CurlVersion(const Napi::CallbackInfo& info) { + return Napi::Number::New(info.Env(), CURLVERSION_NOW); +} + +Napi::Value Post(const Napi::CallbackInfo& args) { + + const Napi::Env env = args.Env(); + + // Arguments required: at least one, and no more than two + if (args.Length() != 2) + { + // NAPI_CPP_EXCEPTIONS = YES + Napi::TypeError::New(env, "Wrong number of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); + return env.Null(); + } + + // Param 1 must be a url string + if (!args[0].IsString()) + { + // NAPI_CPP_EXCEPTIONS = YES + Napi::TypeError::New(env, "Wrong number of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); + return env.Null(); + } + + // Param 2 must be a data string + if (!args[1].IsString()) + { + // NAPI_CPP_EXCEPTIONS = YES + Napi::TypeError::New(env, "Wrong number of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); + return env.Null(); + } + + /* First set the URL that is about to receive our POST. This URL can just as well be an https:// URL if that is what should receive the data. */ + std::string url = args[0].ToString().Utf8Value(); + + /* Now specify the POST data */ + std::string data = args[1].ToString().Utf8Value(); + + int status; + + try { + + // Try to post the data to the url + status = hello_addon_post(url.data(), data.data()); + + } catch (const std::exception &x) { + + // If there was an error... + std::string message(x.what()); + message += '\n'; + message += STRINGIFY(CMAKEJS_ADDON_NAME)".node: could not post the following request:\n"; + message += "url: "; + message += args[0].As(); + message += '\n'; + message += "data: "; + message += args[1].As(); + message += '\n'; + // Throw a javascript-side exception + Napi::TypeError::New(env, message).ThrowAsJavaScriptException(); + + // Clear the old string + url.clear(); + + message.clear(); + + // Return null + return env.Null(); + } + + // If the request did not cause an exception, + // return the status and exit. + return Napi::Number::New(env, status); +} + +Napi::Object Init(Napi::Env env, Napi::Object exports) { + + // Export a chosen C++ function under a given Javascript key + exports.Set( + Napi::String::New(env, "hello"), // Name of function on Javascript side... + Napi::Function::New(env, Hello) // Name of function on C++ side... + ); + + exports.Set( + Napi::String::New(env, "napi_version"), + Napi::Function::New(env, NapiVersion) + ); + + exports.Set( + Napi::String::New(env, "curl_version"), + Napi::Function::New(env, CurlVersion) + ); + + exports.Set( + Napi::String::New(env, "post"), + Napi::Function::New(env, Post) + ); + + // The above will expose the C++ function 'Hello' as a javascript function + // named 'hello', etc... + return exports; +} + +// Register a new addon with the intializer function defined above +NODE_API_MODULE(CMAKEJS_ADDON_NAME, Init) // (name to use, initializer to use) + +} // namespace NAPI_CPP_CUSTOM_NAMESPACE +} // namespace Napi + +// Export your custom namespace to outside of the Napi namespace, providing an +// alias to the Napi Addon API; e.g., '::::Object()', along with the +// functions defined above, such as '::::Hello()'. +namespace NAPI_CPP_CUSTOM_NAMESPACE::CMAKEJS_ADDON_NAME { + using namespace Napi::NAPI_CPP_CUSTOM_NAMESPACE; +} + +#else // !__has_include() || !BUILDING_NODE_EXTENSION + #warning "Warning: Cannot find '' - try running 'npm -g install cmake-js'..." +#endif diff --git a/tests/api/hello_with_curl/src/hello/post.cpp b/tests/api/hello_with_curl/src/hello/post.cpp new file mode 100644 index 00000000..b5fbb107 --- /dev/null +++ b/tests/api/hello_with_curl/src/hello/post.cpp @@ -0,0 +1,60 @@ +/** + * @file post.cpp + * @brief A quick 'HTTP POST' Napi Addon in C++ with CURL +*/ + +#include + +#if __has_include() + +#include + +#include "hello/addon.h" + +/** + * @brief Adapted from https://curl.se/libcurl/c/http-post.html + * + * @param url The HTTP URL to send the POST request to. + * @param data The data body to send in the POST request. + * @return int + */ +int hello_addon_post(const char* url, const char* data) +{ + CURL *curl; + CURLcode res; + + std::cout << "\n"; + std::cout << "Using cURL v" << CURLVERSION_NOW << std::endl; + std::cout << "\n"; + + std::cout << "\n"; + std::cout << "url: " << url << std::endl; + std::cout << "data: " << data << std::endl; + std::cout << "\n"; + + /* In windows, this will init the winsock stuff */ + curl_global_init(CURL_GLOBAL_ALL); + + /* get a curl handle */ + curl = curl_easy_init(); + if(curl) { + /* First set the URL that is about to receive our POST. This URL can just as well be an https:// URL if that is what should receive the data. */ + curl_easy_setopt(curl, CURLOPT_URL, url); + /* Now specify the POST data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + + /* always cleanup */ + curl_easy_cleanup(curl); + } + curl_global_cleanup(); + return 0; +} + +#else // !__has_include() + #warning "Warning: Cannot find '' - try installing CURL from your command line'..." +#endif diff --git a/tests/api/hello_with_curl/tsconfig.json b/tests/api/hello_with_curl/tsconfig.json new file mode 100644 index 00000000..7ee6464f --- /dev/null +++ b/tests/api/hello_with_curl/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "declaration": true, + "lib": [ "esnext" ], + "module": "commonjs", + "outDir": "./build", + "rootDir": ".", + "strict": true, + "target": "es6", + "types": [ + "node" + ], + "typeRoots": [ + "node_modules/@types" + ] + }, + "exclude": [ + "node_modules" + ] +} From da8526a02e2c429517c5847924dcf3a14aa7b4b8 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 13:44:29 +0100 Subject: [PATCH 85/92] Enhanced 'hello_with_curl' with a GET request example. --- tests/api/hello_with_curl/CMakeLists.txt | 26 +++++-- tests/api/hello_with_curl/README.md | 2 + .../api/hello_with_curl/include/hello/addon.h | 16 ++++ tests/api/hello_with_curl/lib/addon.node.d.ts | 3 +- tests/api/hello_with_curl/lib/addon.node.ts | 8 ++ tests/api/hello_with_curl/package.json | 3 +- tests/api/hello_with_curl/src/hello/addon.cpp | 75 ++++++++++++++++--- tests/api/hello_with_curl/src/hello/get.cpp | 55 ++++++++++++++ tests/api/hello_with_curl/src/hello/post.cpp | 9 +-- 9 files changed, 173 insertions(+), 24 deletions(-) create mode 100644 tests/api/hello_with_curl/src/hello/get.cpp diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt index 398f345f..59140799 100644 --- a/tests/api/hello_with_curl/CMakeLists.txt +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -4,27 +4,43 @@ project(hello) find_package(CURL REQUIRED) +if(NOT DEFINED CMAKE_JS_VERSION) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") +endif() + include(CMakeJS) cmakejs_create_napi_addon( addon "src/hello/addon.cpp" + "src/hello/get.cpp" "src/hello/post.cpp" NAMESPACE vendor EXCEPTIONS YES ) +# Mode project headers... +configure_file( + "include/hello/addon.h" # from SOURCE_DIR... + "include/hello/addon.h" # ..to BINARY_DIR +) + +if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/curl") + # Set up cURL for transport + file(COPY ${CURL_INCLUDE_DIR} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/curl") + set (CURL_INCLUDE_DIR + $ + $ + CACHE PATH "Path to curl.h" FORCE + ) +endif() target_link_libraries(addon PRIVATE CURL::libcurl) target_include_directories(addon PRIVATE ${CURL_INCLUDE_DIR}) - target_include_directories(addon PUBLIC $ $ ) -configure_file( - "include/hello/addon.h" # from SOURCE_DIR... - "include/hello/addon.h" # ..to BINARY_DIR -) + include(GNUInstallDirs) # CPack prep stuff... diff --git a/tests/api/hello_with_curl/README.md b/tests/api/hello_with_curl/README.md index d6b88e4e..eac612ae 100644 --- a/tests/api/hello_with_curl/README.md +++ b/tests/api/hello_with_curl/README.md @@ -4,4 +4,6 @@ You can build me, export me as a Javscript module, and share your comments with Please see my package.json to understand how to make HTTP POST requests with me. +More cURL API examples can be found at [https://curl.se/libcurl/c/example.html](https://curl.se/libcurl/c/example.html). + Powered by cmake-js CLI diff --git a/tests/api/hello_with_curl/include/hello/addon.h b/tests/api/hello_with_curl/include/hello/addon.h index a4a72783..f67a5cdb 100644 --- a/tests/api/hello_with_curl/include/hello/addon.h +++ b/tests/api/hello_with_curl/include/hello/addon.h @@ -9,6 +9,22 @@ #include +/** + * @brief Adapted from https://curl.se/libcurl/c/simple.html + * + * @param url The HTTP URL to send the POST request to. + * @param follow Whether to follow redirected URL requests. + * @return int + */ +int hello_addon_get(const char* url, const bool& follow); + +/** + * @brief Adapted from https://curl.se/libcurl/c/http-post.html + * + * @param url The HTTP URL to send the POST request to. + * @param data The data body to send in the POST request. + * @return int + */ int hello_addon_post(const char *url, const char *data); #endif // HELLO_ADDON_H_INCLUDED diff --git a/tests/api/hello_with_curl/lib/addon.node.d.ts b/tests/api/hello_with_curl/lib/addon.node.d.ts index fa0cd7e9..fbdecbbf 100644 --- a/tests/api/hello_with_curl/lib/addon.node.d.ts +++ b/tests/api/hello_with_curl/lib/addon.node.d.ts @@ -2,7 +2,8 @@ declare interface addon { hello(): string; napi_version(): number; curl_version(): number; - post(): string; + get(url: string, follow: boolean): string; + post(url: string, data: string): string; } declare const addon: addon; export = addon; diff --git a/tests/api/hello_with_curl/lib/addon.node.ts b/tests/api/hello_with_curl/lib/addon.node.ts index 75e5dfa6..de4100d6 100644 --- a/tests/api/hello_with_curl/lib/addon.node.ts +++ b/tests/api/hello_with_curl/lib/addon.node.ts @@ -17,6 +17,14 @@ interface addon { * @returns number */ curl_version(): number; + /** + * Sends an HTTP POST request using the CURL API. + * The request is sent to 'url'. Both the url and the + * data to be posted must be Javascript strings (for now). + * Returns a number, confirming the POST request status. + * @returns number + */ + get(url: string, follow: boolean): string; /** * Sends an HTTP POST request using the CURL API. * The request is sent to 'url'. Both the url and the diff --git a/tests/api/hello_with_curl/package.json b/tests/api/hello_with_curl/package.json index 1ca08a3f..55509def 100644 --- a/tests/api/hello_with_curl/package.json +++ b/tests/api/hello_with_curl/package.json @@ -15,7 +15,8 @@ "clean": "cmake-js clean", "wipe": "cmake-js clean && rm -rvf ./node_modules", - "curl:post": "node -p \"const addon = require('./index'); addon.post('https://httpbin.org/anything' ,'name=nathanjhood&project=cmake-js');;\"" + "curl:get": "node -p \"const addon = require('./index'); addon.get( 'https://httpbin.org/anything', /** follow redirects? */ true);\"", + "curl:post": "node -p \"const addon = require('./index'); addon.post('https://httpbin.org/anything', 'name=nathanjhood&project=cmake-js');\"" }, "dependencies": { "cmake-js": "https://github.com/nathanjhood/cmake-js#cmakejs_cmake_api", diff --git a/tests/api/hello_with_curl/src/hello/addon.cpp b/tests/api/hello_with_curl/src/hello/addon.cpp index 887d1c4e..e87d2ff1 100644 --- a/tests/api/hello_with_curl/src/hello/addon.cpp +++ b/tests/api/hello_with_curl/src/hello/addon.cpp @@ -34,34 +34,86 @@ Napi::Value CurlVersion(const Napi::CallbackInfo& info) { return Napi::Number::New(info.Env(), CURLVERSION_NOW); } -Napi::Value Post(const Napi::CallbackInfo& args) { +Napi::Value Get(const Napi::CallbackInfo& args) { const Napi::Env env = args.Env(); // Arguments required: at least one, and no more than two - if (args.Length() != 2) - { + if (args.Length() != 2) { // NAPI_CPP_EXCEPTIONS = YES Napi::TypeError::New(env, "Wrong number of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); return env.Null(); } // Param 1 must be a url string - if (!args[0].IsString()) - { + // Param 2 must be a redirect boolean + if (!args[0].IsString() || !args[1].IsBoolean()) { // NAPI_CPP_EXCEPTIONS = YES - Napi::TypeError::New(env, "Wrong number of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); + Napi::TypeError::New(env, "Wrong type of arguments! Please supply a url string, and a boolean whether to follow redirected url's, in that order").ThrowAsJavaScriptException(); return env.Null(); } - // Param 2 must be a data string - if (!args[1].IsString()) - { + /* First set the URL that is about to receive our POST. This URL can just as well be an https:// URL if that is what should receive the data. */ + std::string url = args[0].ToString().Utf8Value(); + + /* Now specify the redirect follow mode */ + bool follow = args[1].ToBoolean().Value(); + + int status; + + try { + + // Try to get the data from the url + status = hello_addon_get(url.data(), follow); + + } catch (const std::exception &x) { + + // If there was an error... + std::string message(x.what()); + message += '\n'; + message += STRINGIFY(CMAKEJS_ADDON_NAME)".node: could not get the following request:\n"; + message += "url: "; + message += args[0].As(); + message += '\n'; + message += "follow redirects: "; + message += args[1].As(); + message += '\n'; + // Throw a javascript-side exception + Napi::TypeError::New(env, message).ThrowAsJavaScriptException(); + + // Clear the old string + url.clear(); + + message.clear(); + + // Return null + return env.Null(); + } + + // If the request did not cause an exception, + // return the status and exit. + return Napi::Number::New(env, status); +} + +Napi::Value Post(const Napi::CallbackInfo& args) { + + const Napi::Env env = args.Env(); + + // Arguments required: at least one, and no more than two + if (args.Length() != 2) { // NAPI_CPP_EXCEPTIONS = YES Napi::TypeError::New(env, "Wrong number of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); return env.Null(); } + // Param 1 must be a url string + // Param 2 must be a data string + if (!args[0].IsString() || !args[1].IsString()) { + // NAPI_CPP_EXCEPTIONS = YES + Napi::TypeError::New(env, "Wrong type of arguments! Please supply a url string, and the string data to POST, in that order").ThrowAsJavaScriptException(); + return env.Null(); + } + /* First set the URL that is about to receive our POST. This URL can just as well be an https:// URL if that is what should receive the data. */ std::string url = args[0].ToString().Utf8Value(); @@ -122,6 +174,11 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) { Napi::Function::New(env, CurlVersion) ); + exports.Set( + Napi::String::New(env, "get"), + Napi::Function::New(env, Get) + ); + exports.Set( Napi::String::New(env, "post"), Napi::Function::New(env, Post) diff --git a/tests/api/hello_with_curl/src/hello/get.cpp b/tests/api/hello_with_curl/src/hello/get.cpp new file mode 100644 index 00000000..b85e001f --- /dev/null +++ b/tests/api/hello_with_curl/src/hello/get.cpp @@ -0,0 +1,55 @@ +/** + * @file post.cpp + * @brief A quick 'HTTP POST' Napi Addon in C++ with CURL +*/ + +#include + +#if __has_include() + +#include + +#include "hello/addon.h" + +/** + * @brief Adapted from https://curl.se/libcurl/c/simple.html + * + * @param url The HTTP URL to send the POST request to. + * @param follow Whether to follow redirected URL requests. + * @return int + */ +int hello_addon_get(const char* url, const bool& follow) +{ + CURL *curl; + CURLcode res; + + std::cout << "\n"; + std::cout << "Using cURL v" << CURLVERSION_NOW << std::endl; + std::cout << "\n"; + + std::cout << "\n"; + std::cout << "url: " << url << std::endl; + std::cout << "follow redirects: " << follow << std::endl; + std::cout << "\n"; + + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_URL, url); + /* example.com is redirected, so we tell libcurl to follow redirection */ + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // 'follow' + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + + /* always cleanup */ + curl_easy_cleanup(curl); + } + return 0; +} + +#else // !__has_include() + #warning "Warning: Cannot find '' - try installing cURL from your command line'..." +#endif diff --git a/tests/api/hello_with_curl/src/hello/post.cpp b/tests/api/hello_with_curl/src/hello/post.cpp index b5fbb107..5b906ea2 100644 --- a/tests/api/hello_with_curl/src/hello/post.cpp +++ b/tests/api/hello_with_curl/src/hello/post.cpp @@ -11,13 +11,6 @@ #include "hello/addon.h" -/** - * @brief Adapted from https://curl.se/libcurl/c/http-post.html - * - * @param url The HTTP URL to send the POST request to. - * @param data The data body to send in the POST request. - * @return int - */ int hello_addon_post(const char* url, const char* data) { CURL *curl; @@ -56,5 +49,5 @@ int hello_addon_post(const char* url, const char* data) } #else // !__has_include() - #warning "Warning: Cannot find '' - try installing CURL from your command line'..." + #warning "Warning: Cannot find '' - try installing cURL from your command line'..." #endif From 14744a41f36f50d9e6f288b54ada4650f463c4d8 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 14:15:33 +0100 Subject: [PATCH 86/92] rolled back on bindings generation - slightly buggy - not required annyay --- share/cmake/CMakeJS.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/cmake/CMakeJS.cmake b/share/cmake/CMakeJS.cmake index b0deb7ea..c5da4dfe 100644 --- a/share/cmake/CMakeJS.cmake +++ b/share/cmake/CMakeJS.cmake @@ -964,7 +964,7 @@ cmakejs_create_napi_addon( [ALIAS ] [NAMESPACE ] [NAPI_V ) # (experimental) :) - cmakejs_create_addon_bindings(${name}) + # cmakejs_create_addon_bindings(${name}) # Global exceptions policy unset(_NAPI_GLOBAL_EXCEPTIONS_POLICY) From 5abe82fe1c1998ce696abc1ab45bf6aac3e4a771 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 15:17:46 +0100 Subject: [PATCH 87/92] Made 'hello_with_curl' compatible with CTest and Cack --- tests/api/hello_with_curl/CMakeLists.txt | 173 ++++++++++++++---- tests/api/hello_with_curl/lib/addon.node.ts | 4 +- tests/api/hello_with_curl/package.json | 14 +- .../tests/hello/curl_version.js | 30 +++ tests/api/hello_with_curl/tests/hello/get.js | 30 +++ .../api/hello_with_curl/tests/hello/hello.js | 30 +++ .../tests/hello/napi_version.js | 30 +++ tests/api/hello_with_curl/tests/hello/post.js | 30 +++ .../hello_with_curl/tests/hello/tests.cmake | 25 +++ 9 files changed, 322 insertions(+), 44 deletions(-) create mode 100644 tests/api/hello_with_curl/tests/hello/curl_version.js create mode 100644 tests/api/hello_with_curl/tests/hello/get.js create mode 100644 tests/api/hello_with_curl/tests/hello/hello.js create mode 100644 tests/api/hello_with_curl/tests/hello/napi_version.js create mode 100644 tests/api/hello_with_curl/tests/hello/post.js create mode 100644 tests/api/hello_with_curl/tests/hello/tests.cmake diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt index 59140799..e301163f 100644 --- a/tests/api/hello_with_curl/CMakeLists.txt +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -1,13 +1,29 @@ cmake_minimum_required(VERSION 3.15) -project(hello) - -find_package(CURL REQUIRED) +project(hello VERSION 1.0.0.0) if(NOT DEFINED CMAKE_JS_VERSION) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/node_modules/cmake-js/share/cmake") endif() +# Get CURL::libcurl +find_package(CURL REQUIRED) + +if(hello_IS_INSTALLABLE) + # cURL relocate... + if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/curl") + # Set up cURL for transport + file(COPY "${CURL_INCLUDE_DIR}/curl" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include") + endif() + set (CURL_INCLUDE_DIR + $ + $ + CACHE PATH "Path to curl.h" FORCE + ) + install(FILES ${CURL_INCLUDE_DIR} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +endif() + +# Get cmake-js::cmake-js via 'cmakejs_create_napi_addon()' include(CMakeJS) cmakejs_create_napi_addon( @@ -18,49 +34,132 @@ cmakejs_create_napi_addon( NAMESPACE vendor EXCEPTIONS YES ) -# Mode project headers... +# Move project headers... configure_file( "include/hello/addon.h" # from SOURCE_DIR... "include/hello/addon.h" # ..to BINARY_DIR ) - -if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/curl") - # Set up cURL for transport - file(COPY ${CURL_INCLUDE_DIR} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/curl") - set (CURL_INCLUDE_DIR - $ - $ - CACHE PATH "Path to curl.h" FORCE - ) -endif() - -target_link_libraries(addon PRIVATE CURL::libcurl) -target_include_directories(addon PRIVATE ${CURL_INCLUDE_DIR}) target_include_directories(addon PUBLIC $ $ ) +# Link Addon to cURl +target_link_libraries(addon PRIVATE CURL::libcurl) +target_include_directories(addon PRIVATE ${CURL_INCLUDE_DIR}) -include(GNUInstallDirs) -# CPack prep stuff... -export ( - TARGETS addon - FILE share/cmake/AddonTargets.cmake - NAMESPACE hello:: -) -install(TARGETS addon - EXPORT AddonTargets - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" -) +# TESTING +if(NOT DEFINED BUILD_TESTS AND hello_IS_TOP_LEVEL) + set(BUILD_TESTS ON) +endif() +if(BUILD_TESTS AND hello_IS_TOP_LEVEL) + # include(CTest) # uncomment this if you want to see the Test Dashboard targets (lots...) + enable_testing() + include("${CMAKE_CURRENT_LIST_DIR}/tests/hello/tests.cmake") +endif() -install( - EXPORT AddonTargets - FILE addonTargets.cmake - NAMESPACE vendor:: - DESTINATION lib/cmake/hello -) +# PACKING +if(hello_IS_INSTALLABLE AND hello_IS_TOP_LEVEL) + include(GNUInstallDirs) + # CPack prep stuff... + include (CMakePackageConfigHelpers) + + file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" [==[ +@PACKAGE_INIT@ + +include (${CMAKE_CURRENT_LIST_DIR}/AddonTargets.cmake) + +check_required_components (cmake-js) + +]==]) + + # create cmake config file + configure_package_config_file ( + "${CMAKE_CURRENT_BINARY_DIR}/AddonConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfig.cmake" + INSTALL_DESTINATION + "${CMAKE_INSTALL_LIBDIR}/cmake/Addon" + ) + # generate the version file for the cmake config file + write_basic_package_version_file ( + "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/AddonConfigVersion.cmake" + VERSION hello_VERSION + COMPATIBILITY AnyNewerVersion + ) + # pass our build script along + file (WRITE "${CMAKE_CURRENT_BINARY_DIR}/share/cmake/Addon.cmake" "${CMAKE_CURRENT_LIST_FILE}") + export ( + TARGETS addon + FILE share/cmake/AddonTargets.cmake + NAMESPACE hello:: + ) + + install(TARGETS addon + EXPORT AddonTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + + install( + EXPORT AddonTargets + FILE addonTargets.cmake + NAMESPACE vendor:: + DESTINATION lib/cmake/hello + ) + + # set(CPACK_PACKAGE_CHECKSUM "${PROJECT_VERSION_TWEAK}") # git rev-parse is a good idea for this... + set(CPACK_PACKAGE_VENDOR "hello") + set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") + set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-node_${NODE_VERSION}") + set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-Source") + set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_DESCRIPTION}) + set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) + set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_VERSION_MINOR}) + set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_VERSION_PATCH}) + set(CPACK_PACKAGE_VERSION_TWEAK ${PROJECT_VERSION_VERSION_TWEAK}) + #set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/LICENSE) + set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md) + set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON) + set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) + set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") # Check out CPack's 'NSIS' installer for Win32, and the others! + set(CPACK_SOURCE_IGNORE_FILES + _CPack_Packages + /*.zip + /*.tar + /*.tar.* + /.env* + /.git/* + /.cmake + /.github + /.vs + /.vscode + /.cache + /.config + /.local + /dist + /doc + /docs + #/bin + #/lib + /usr + /out + #/build + /Release + /Debug + /MinSizeRel + /RelWithDebInfo + /downloads + /installed + /install + /node_modules + /vcpkg + /.*build.* + /package-lock.json + /yarn.lock + /\\\\.DS_Store + ) + include(CPack) +endif() diff --git a/tests/api/hello_with_curl/lib/addon.node.ts b/tests/api/hello_with_curl/lib/addon.node.ts index de4100d6..1df4fdf2 100644 --- a/tests/api/hello_with_curl/lib/addon.node.ts +++ b/tests/api/hello_with_curl/lib/addon.node.ts @@ -24,7 +24,7 @@ interface addon { * Returns a number, confirming the POST request status. * @returns number */ - get(url: string, follow: boolean): string; + get(url: string, follow: boolean): number; /** * Sends an HTTP POST request using the CURL API. * The request is sent to 'url'. Both the url and the @@ -32,7 +32,7 @@ interface addon { * Returns a number, confirming the POST request status. * @returns number */ - post(url: string, data: string): string; + post(url: string, data: string): number; } const addon: addon = require('../build/lib/addon.node'); export = addon; diff --git a/tests/api/hello_with_curl/package.json b/tests/api/hello_with_curl/package.json index 55509def..9b86fc61 100644 --- a/tests/api/hello_with_curl/package.json +++ b/tests/api/hello_with_curl/package.json @@ -6,15 +6,19 @@ "license": "MIT", "scripts": { "start": "node ./index.js", - "install": "cmake-js install", - "postinstall": "cmake-js compile", - "configure": "cmake-js configure", - "reconfigure": "cmake-js reconfigure", + "install": "cmake-js install --CDhello_IS_INSTALLABLE:BOOL=TRUE --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "postinstall": "cmake-js compile --CDhello_IS_INSTALLABLE:BOOL=TRUE --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "configure": "cmake-js configure --CDhello_IS_INSTALLABLE:BOOL=TRUE --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", + "reconfigure": "cmake-js reconfigure --CDhello_IS_INSTALLABLE:BOOL=TRUE --CDBUILD_TESTS:BOOL=TRUE --CDCMAKE_INSTALL_PREFIX:PATH=./dist", "build": "cmake-js build", "rebuild": "cmake-js rebuild", - "clean": "cmake-js clean", + "clean": "cmake-js clean && rm -rvf ./dist", "wipe": "cmake-js clean && rm -rvf ./node_modules", + "ctest": "ctest --test-dir ./build --rerun-failed --output-on-failure --verbose", + "cpack": "cpack --config ./build/CPackSourceConfig.cmake -B ./dist", + "cdist": "cpack --config ./build/CPackConfig.cmake -B ./dist", + "curl:get": "node -p \"const addon = require('./index'); addon.get( 'https://httpbin.org/anything', /** follow redirects? */ true);\"", "curl:post": "node -p \"const addon = require('./index'); addon.post('https://httpbin.org/anything', 'name=nathanjhood&project=cmake-js');\"" }, diff --git a/tests/api/hello_with_curl/tests/hello/curl_version.js b/tests/api/hello_with_curl/tests/hello/curl_version.js new file mode 100644 index 00000000..b527d0e7 --- /dev/null +++ b/tests/api/hello_with_curl/tests/hello/curl_version.js @@ -0,0 +1,30 @@ +function test_curl_version() { + + let status = false; + + try { + + const addon = require("../../lib/addon.node"); + + console.log(`Napi Version: ${addon.napi_version()}`); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_curl_version = test_curl_version(); + +if((!res_test_curl_version)) +{ + console.log("'test_curl_version()' failed."); + return false; +} + +console.log("'test_curl_version()' passed."); +return true; diff --git a/tests/api/hello_with_curl/tests/hello/get.js b/tests/api/hello_with_curl/tests/hello/get.js new file mode 100644 index 00000000..16ba47fd --- /dev/null +++ b/tests/api/hello_with_curl/tests/hello/get.js @@ -0,0 +1,30 @@ +function test_get() { + + let status = false; + + try { + + const addon = require("../../lib/addon.node"); + + addon.get('https://httpbin.org/anything', /** follow redirects? */ true); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_get = test_get(); + +if((!res_test_get)) +{ + console.log("'test_get()' failed."); + return false; +} + +console.log("'test_get()' passed."); +return true; diff --git a/tests/api/hello_with_curl/tests/hello/hello.js b/tests/api/hello_with_curl/tests/hello/hello.js new file mode 100644 index 00000000..624cd825 --- /dev/null +++ b/tests/api/hello_with_curl/tests/hello/hello.js @@ -0,0 +1,30 @@ +function test_hello() { + + let status = false; + + try { + + const addon = require("../../lib/addon.node"); + + console.log(addon.hello()); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_hello = test_hello(); + +if((!res_test_hello)) +{ + console.log("'test_hello()' failed."); + return false; +} + +console.log("'test_hello()' passed."); +return true; diff --git a/tests/api/hello_with_curl/tests/hello/napi_version.js b/tests/api/hello_with_curl/tests/hello/napi_version.js new file mode 100644 index 00000000..81b97082 --- /dev/null +++ b/tests/api/hello_with_curl/tests/hello/napi_version.js @@ -0,0 +1,30 @@ +function test_napi_version() { + + let status = false; + + try { + + const addon = require("../../lib/addon.node"); + + console.log(`Napi Version: ${addon.napi_version()}`); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_napi_version = test_napi_version(); + +if((!res_test_napi_version)) +{ + console.log("'test_napi_version()' failed."); + return false; +} + +console.log("'test_napi_version()' passed."); +return true; diff --git a/tests/api/hello_with_curl/tests/hello/post.js b/tests/api/hello_with_curl/tests/hello/post.js new file mode 100644 index 00000000..1bb2a868 --- /dev/null +++ b/tests/api/hello_with_curl/tests/hello/post.js @@ -0,0 +1,30 @@ +function test_post() { + + let status = false; + + try { + + const addon = require("../../lib/addon.node"); + + addon.post('https://httpbin.org/anything', 'name=nathanjhood&project=cmake-js'); + + status = true; + + } catch(e) { + + console.log(`${e}`); + } + + return status; +}; + +const res_test_post = test_post(); + +if((!res_test_post)) +{ + console.log("'test_post()' failed."); + return false; +} + +console.log("'test_post()' passed."); +return true; diff --git a/tests/api/hello_with_curl/tests/hello/tests.cmake b/tests/api/hello_with_curl/tests/hello/tests.cmake new file mode 100644 index 00000000..6e8fd5c2 --- /dev/null +++ b/tests/api/hello_with_curl/tests/hello/tests.cmake @@ -0,0 +1,25 @@ +# Make a list of tests to do (should match the test's filename) +list(APPEND TESTS + hello + napi_version + curl_version + get + post +) + +# define a function to simplify adding tests +function(do_test arg) + add_test( + NAME test_${arg} + COMMAND "${NODE_EXECUTABLE}" "./tests/hello/${arg}.js" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + ) + set_tests_properties(test_${arg} + PROPERTIES PASS_REGULAR_EXPRESSION " passed." + ) +endfunction(do_test) + +# run the tests +foreach(TEST IN LISTS TESTS) + do_test("${TEST}") +endforeach() From 12278075e4fea09d9e148e3a255aa062f79f66e7 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 15:27:00 +0100 Subject: [PATCH 88/92] Made 'hello_with_curl' more compatible with CTest and CPack --- tests/api/hello_with_curl/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt index e301163f..bee05c17 100644 --- a/tests/api/hello_with_curl/CMakeLists.txt +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -10,6 +10,7 @@ endif() find_package(CURL REQUIRED) if(hello_IS_INSTALLABLE) + include(GNUInstallDirs) # cURL relocate... if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/include/curl") # Set up cURL for transport @@ -48,7 +49,6 @@ target_include_directories(addon PUBLIC target_link_libraries(addon PRIVATE CURL::libcurl) target_include_directories(addon PRIVATE ${CURL_INCLUDE_DIR}) - # TESTING if(NOT DEFINED BUILD_TESTS AND hello_IS_TOP_LEVEL) set(BUILD_TESTS ON) @@ -61,7 +61,6 @@ endif() # PACKING if(hello_IS_INSTALLABLE AND hello_IS_TOP_LEVEL) - include(GNUInstallDirs) # CPack prep stuff... include (CMakePackageConfigHelpers) From 0920f3bdce61c54a58766e580b3b3193a7a00b3f Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 16:25:48 +0100 Subject: [PATCH 89/92] Added exceptions policy usage to 'hello_with_curl' --- tests/api/hello_with_curl/CMakeLists.txt | 10 ++++----- .../api/hello_with_curl/include/hello/addon.h | 22 ++++++------------- tests/api/hello_with_curl/src/hello/addon.cpp | 12 ++++++++-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt index bee05c17..2fd050c1 100644 --- a/tests/api/hello_with_curl/CMakeLists.txt +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -125,11 +125,11 @@ check_required_components (cmake-js) set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY ON) set(CPACK_SOURCE_GENERATOR "TGZ;ZIP") # Check out CPack's 'NSIS' installer for Win32, and the others! set(CPACK_SOURCE_IGNORE_FILES - _CPack_Packages - /*.zip - /*.tar - /*.tar.* - /.env* + _CPack_Packages + /*.zip + /*.tar + /*.tar.* + /.env* /.git/* /.cmake /.github diff --git a/tests/api/hello_with_curl/include/hello/addon.h b/tests/api/hello_with_curl/include/hello/addon.h index f67a5cdb..5f8a6bfe 100644 --- a/tests/api/hello_with_curl/include/hello/addon.h +++ b/tests/api/hello_with_curl/include/hello/addon.h @@ -9,22 +9,14 @@ #include -/** - * @brief Adapted from https://curl.se/libcurl/c/simple.html - * - * @param url The HTTP URL to send the POST request to. - * @param follow Whether to follow redirected URL requests. - * @return int - */ +#define HELLO_ADDON_VERSION @PROJECT_VERSION@ // CMake will evaluate this when 'configure_file()' runs! + +/** Adapted from https://curl.se/libcurl/c/simple.html */ int hello_addon_get(const char* url, const bool& follow); -/** - * @brief Adapted from https://curl.se/libcurl/c/http-post.html - * - * @param url The HTTP URL to send the POST request to. - * @param data The data body to send in the POST request. - * @return int - */ -int hello_addon_post(const char *url, const char *data); +/** Adapted from https://curl.se/libcurl/c/http-post.html */ +int hello_addon_post(const char* url, const char* data); + +#define EX @NAPI_CPP_EXCEPTIONS@ #endif // HELLO_ADDON_H_INCLUDED diff --git a/tests/api/hello_with_curl/src/hello/addon.cpp b/tests/api/hello_with_curl/src/hello/addon.cpp index e87d2ff1..8c09469a 100644 --- a/tests/api/hello_with_curl/src/hello/addon.cpp +++ b/tests/api/hello_with_curl/src/hello/addon.cpp @@ -19,11 +19,13 @@ namespace Napi { +#ifdef NAPI_CPP_CUSTOM_NAMESPACE namespace NAPI_CPP_CUSTOM_NAMESPACE { +#endif Napi::Value Hello(const Napi::CallbackInfo& info) { - return Napi::String::New(info.Env(), STRINGIFY(CMAKEJS_ADDON_NAME)".node is online!"); + return Napi::String::New(info.Env(), STRINGIFY(CMAKEJS_ADDON_NAME)".node v." STRINGIFY(@PROJECT_VERSION@)" is online!"); } Napi::Value NapiVersion(const Napi::CallbackInfo& info) { @@ -66,6 +68,8 @@ Napi::Value Get(const Napi::CallbackInfo& args) { // Try to get the data from the url status = hello_addon_get(url.data(), follow); + NAPI_THROW_IF_FAILED_VOID(env, status) // This behaviour changes depending on EXCEPTIONS policy (can be YES, NO, or MAYBE) + } catch (const std::exception &x) { // If there was an error... @@ -127,6 +131,8 @@ Napi::Value Post(const Napi::CallbackInfo& args) { // Try to post the data to the url status = hello_addon_post(url.data(), data.data()); + NAPI_THROW_IF_FAILED_VOID(env, status) // This behaviour changes depending on EXCEPTIONS policy (can be YES, NO, or MAYBE) + } catch (const std::exception &x) { // If there was an error... @@ -192,7 +198,9 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) { // Register a new addon with the intializer function defined above NODE_API_MODULE(CMAKEJS_ADDON_NAME, Init) // (name to use, initializer to use) -} // namespace NAPI_CPP_CUSTOM_NAMESPACE +#ifdef NAPI_CPP_CUSTOM_NAMESPACE +} // namespace NAPI_CPP_CUSTOM_NAMESPACE +#endif } // namespace Napi // Export your custom namespace to outside of the Napi namespace, providing an From 68494cbc523c502498eb2b06fb6d979f67e168f5 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 16:56:23 +0100 Subject: [PATCH 90/92] Made 'hello_with_curl' more compatible with exceptions --- tests/api/hello_with_curl/CMakeLists.txt | 4 ++++ tests/api/hello_with_curl/src/hello/addon.cpp | 6 ++++-- tests/api/hello_with_curl/src/hello/get.cpp | 2 +- tests/api/hello_with_curl/src/hello/post.cpp | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt index 2fd050c1..ad1c1470 100644 --- a/tests/api/hello_with_curl/CMakeLists.txt +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -40,6 +40,10 @@ configure_file( "include/hello/addon.h" # from SOURCE_DIR... "include/hello/addon.h" # ..to BINARY_DIR ) +configure_file( + "include/hello/urlc.h" + "include/hello/urlc.h" +) target_include_directories(addon PUBLIC $ $ diff --git a/tests/api/hello_with_curl/src/hello/addon.cpp b/tests/api/hello_with_curl/src/hello/addon.cpp index 8c09469a..971dec02 100644 --- a/tests/api/hello_with_curl/src/hello/addon.cpp +++ b/tests/api/hello_with_curl/src/hello/addon.cpp @@ -68,7 +68,7 @@ Napi::Value Get(const Napi::CallbackInfo& args) { // Try to get the data from the url status = hello_addon_get(url.data(), follow); - NAPI_THROW_IF_FAILED_VOID(env, status) // This behaviour changes depending on EXCEPTIONS policy (can be YES, NO, or MAYBE) + NAPI_THROW_IF_FAILED(env, status) // This behaviour changes depending on EXCEPTIONS policy (can be YES, NO, or MAYBE) } catch (const std::exception &x) { @@ -82,6 +82,8 @@ Napi::Value Get(const Napi::CallbackInfo& args) { message += "follow redirects: "; message += args[1].As(); message += '\n'; + + std::cerr << message << std::endl; // Throw a javascript-side exception Napi::TypeError::New(env, message).ThrowAsJavaScriptException(); @@ -131,7 +133,7 @@ Napi::Value Post(const Napi::CallbackInfo& args) { // Try to post the data to the url status = hello_addon_post(url.data(), data.data()); - NAPI_THROW_IF_FAILED_VOID(env, status) // This behaviour changes depending on EXCEPTIONS policy (can be YES, NO, or MAYBE) + NAPI_THROW_IF_FAILED(env, status) // This behaviour changes depending on EXCEPTIONS policy (can be YES, NO, or MAYBE) } catch (const std::exception &x) { diff --git a/tests/api/hello_with_curl/src/hello/get.cpp b/tests/api/hello_with_curl/src/hello/get.cpp index b85e001f..639084a9 100644 --- a/tests/api/hello_with_curl/src/hello/get.cpp +++ b/tests/api/hello_with_curl/src/hello/get.cpp @@ -47,7 +47,7 @@ int hello_addon_get(const char* url, const bool& follow) /* always cleanup */ curl_easy_cleanup(curl); } - return 0; + return EXIT_SUCCESS; } #else // !__has_include() diff --git a/tests/api/hello_with_curl/src/hello/post.cpp b/tests/api/hello_with_curl/src/hello/post.cpp index 5b906ea2..79cedf5d 100644 --- a/tests/api/hello_with_curl/src/hello/post.cpp +++ b/tests/api/hello_with_curl/src/hello/post.cpp @@ -45,7 +45,7 @@ int hello_addon_post(const char* url, const char* data) curl_easy_cleanup(curl); } curl_global_cleanup(); - return 0; + return EXIT_SUCCESS; } #else // !__has_include() From e4e24a1788a118792eba1c2095740a720afc1e52 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 16:56:56 +0100 Subject: [PATCH 91/92] typo --- tests/api/hello_with_curl/include/hello/addon.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/api/hello_with_curl/include/hello/addon.h b/tests/api/hello_with_curl/include/hello/addon.h index 5f8a6bfe..0ca19d0b 100644 --- a/tests/api/hello_with_curl/include/hello/addon.h +++ b/tests/api/hello_with_curl/include/hello/addon.h @@ -17,6 +17,4 @@ int hello_addon_get(const char* url, const bool& follow); /** Adapted from https://curl.se/libcurl/c/http-post.html */ int hello_addon_post(const char* url, const char* data); - -#define EX @NAPI_CPP_EXCEPTIONS@ #endif // HELLO_ADDON_H_INCLUDED From f427db8f706da4f02a4aacc1e46763306d9d3a51 Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Mon, 5 Feb 2024 17:03:33 +0100 Subject: [PATCH 92/92] typo --- tests/api/hello_with_curl/CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/api/hello_with_curl/CMakeLists.txt b/tests/api/hello_with_curl/CMakeLists.txt index ad1c1470..2fd050c1 100644 --- a/tests/api/hello_with_curl/CMakeLists.txt +++ b/tests/api/hello_with_curl/CMakeLists.txt @@ -40,10 +40,6 @@ configure_file( "include/hello/addon.h" # from SOURCE_DIR... "include/hello/addon.h" # ..to BINARY_DIR ) -configure_file( - "include/hello/urlc.h" - "include/hello/urlc.h" -) target_include_directories(addon PUBLIC $ $