Skip to content

Commit

Permalink
wip: replace tests/targets with hello_c example
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Feb 6, 2024
1 parent 201f85d commit baa0195
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 458 deletions.
2 changes: 0 additions & 2 deletions lib/cMake.js
Expand Up @@ -13,8 +13,6 @@ 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'
Expand Down
3 changes: 0 additions & 3 deletions share/cmake/api.js

This file was deleted.

File renamed without changes.
13 changes: 13 additions & 0 deletions tests/api/hello_c/CMakeLists.txt
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)

project(hello_c)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/share/cmake")
include(CMakeJS)

# cmakejs_setup_node_api_c_library() # This can be used, but is called by cmakejs_create_node_api_addon

cmakejs_create_node_api_addon(
addon
"src/hello/addon.c"
)
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/api/hello_c/index.js
@@ -0,0 +1,5 @@
// This small codeblock in your root-level index.js allows others to consume
// your addon as any other NodeJS module

const addon = require(`./build/lib/addon.node`);
module.exports = addon;
@@ -1,5 +1,5 @@
{
"name": "@vendor/hello",
"name": "@vendor/hello-c",
"version": "1.0.0",
"description": "A test addon made using CMakeJS.cmake",
"main": "index.js",
Expand All @@ -17,7 +17,6 @@
},
"dependencies": {
"cmake-js": "link:../../../",
"node-addon-api": "^7.1.0",
"node-api-headers": "^1.1.0"
}
}
43 changes: 43 additions & 0 deletions tests/api/hello_c/src/hello/addon.c
@@ -0,0 +1,43 @@
/**
* @file addon.cpp
* @brief A quick 'hello world' Napi Addon in C++
*/

// Required header and flag
#if __has_include(<node_api.h>) && BUILDING_NODE_EXTENSION

#include <node_api.h>

napi_value vendor_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 NULL;
return greeting;
}

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, NULL, 0, vendor_addon_hello, NULL, &fn); // Name of function on Javascript side...
if (status != napi_ok) return NULL;

status = napi_set_named_property(env, exports, "hello", fn); // Name of function on C side...
if (status != napi_ok) return NULL;

// The above expose the C function 'addon_hello' as a javascript function named '<name>.hello', etc...
return exports;
}

// Register a new addon with the intializer function defined above
NAPI_MODULE(addon, vendor_addon_init) // (<name> to use, initializer to use)

#else // !__has_include(<napi.h>) || !BUILDING_NODE_EXTENSION
#warning "Warning: Cannot find '<napi.h>' - try running 'npm -g install cmake-js'..."
#endif
22 changes: 0 additions & 22 deletions tests/targets/README.md

This file was deleted.

26 changes: 0 additions & 26 deletions tests/targets/cmake-js/CMakeLists.txt

This file was deleted.

10 changes: 0 additions & 10 deletions tests/targets/cmake-js/index.js

This file was deleted.

23 changes: 0 additions & 23 deletions tests/targets/cmake-js/package.json

This file was deleted.

43 changes: 0 additions & 43 deletions tests/targets/cmake-js/src/hello/addon.cpp

This file was deleted.

3 changes: 0 additions & 3 deletions tests/targets/node-addon-api/.gitignore

This file was deleted.

28 changes: 0 additions & 28 deletions tests/targets/node-addon-api/CMakeLists.txt

This file was deleted.

7 changes: 0 additions & 7 deletions tests/targets/node-addon-api/README.md

This file was deleted.

10 changes: 0 additions & 10 deletions tests/targets/node-addon-api/index.js

This file was deleted.

43 changes: 0 additions & 43 deletions tests/targets/node-addon-api/src/hello/addon.cpp

This file was deleted.

3 changes: 0 additions & 3 deletions tests/targets/node-api/.gitignore

This file was deleted.

25 changes: 0 additions & 25 deletions tests/targets/node-api/CMakeLists.txt

This file was deleted.

7 changes: 0 additions & 7 deletions tests/targets/node-api/README.md

This file was deleted.

10 changes: 0 additions & 10 deletions tests/targets/node-api/index.js

This file was deleted.

22 changes: 0 additions & 22 deletions tests/targets/node-api/package.json

This file was deleted.

0 comments on commit baa0195

Please sign in to comment.