resolve napajs_inc/napajs_lib by build.paths (#57)

* resolve napajs_inc/napajs_lib by build.paths
This commit is contained in:
Allen Wang 2017-08-30 10:43:20 -07:00 коммит произвёл GitHub
Родитель 406a6f3d9d
Коммит bcb87d368a
19 изменённых файлов: 155 добавлений и 212 удалений

Просмотреть файл

@ -5,18 +5,22 @@ The source codes can be organized as the structure below.
+-- napa-module-example +-- napa-module-example
+-- inc +-- inc
| +-- example.h | +-- example.h
+-- src
| +-- example.cpp
| +-- addon.h
| +-- addon.cpp
+-- lib +-- lib
| +-- example.ts | +-- example.ts
| +-- tsconfig.json | +-- tsconfig.json
+-- napa
| +-- addon.cpp
| +-- CMakeLists.txt (cmake specific)
| +-- example-wrap.cpp
| +-- example-wrap.h
+-- src
| +-- CMakeLists.txt (cmake specific)
| +-- example.cpp
+-- test +-- test
| +-- test.ts | +-- test.ts
| +-- tsconfig.json | +-- tsconfig.json
+-- binding.gyp (when build configuration is given in node-gyp) +-- binding.gyp (gyp specific)
+-- CMakeLists.txt (when build configuration is given in cmake-js) +-- CMakeLists.txt (cmake specific)
+-- package.json +-- package.json
+-- README.md +-- README.md
``` ```

Просмотреть файл

@ -1,23 +1,7 @@
{ {
"variables": { "variables": {
"napajs_root": "<!(node -e \" \ "napajs_lib": "<!(node -e \"require('napajs/build').paths.lib\")",
var path = require('path'); \ "napajs_inc": "<!(node -e \"require('napajs/build').paths.inc\")"
process.stdout.write(path.resolve(path.dirname(require.resolve('napajs')), '..')); \
\")",
'conditions': [
['OS=="win"', {
"library_prefix": "",
"library_suffix": ".lib"
}],
['OS=="linux"', {
"library_prefix": "lib",
"library_suffix": ".dylib"
}],
['OS=="max"', {
"library_prefix": "lib",
"library_suffix": ".so"
}]
]
}, },
"targets": [ "targets": [
{ {
@ -25,27 +9,24 @@
"type": "<(library)", "type": "<(library)",
"product_extension": "", "product_extension": "",
"product_dir": "<(PRODUCT_DIR)/../../bin", "product_dir": "<(PRODUCT_DIR)/../../bin",
"sources": [ "src/addon.cpp"], "sources": [ "napa/addon.cpp"],
"defines": [ "defines": [
"NAPA_BINDING_EXPORTS",
"BUILDING_NODE_EXTENSION" "BUILDING_NODE_EXTENSION"
], ],
"include_dirs": ["<(napajs_root)/inc"] "include_dirs": ["<(napajs_inc)"]
}, },
{ {
"target_name": "addon.napa", "target_name": "addon.napa",
"type": "<(library)", "type": "<(library)",
"product_extension": "", "product_extension": "",
"product_dir": "<(PRODUCT_DIR)/../../bin", "product_dir": "<(PRODUCT_DIR)/../../bin",
"sources": [ "src/addon.cpp" ], "sources": [ "napa/addon.cpp" ],
"defines": [ "defines": [
"NAPA_EXPORTS",
"NAPA_BINDING_EXPORTS",
"BUILDING_NAPA_EXTENSION" "BUILDING_NAPA_EXTENSION"
], ],
"include_dirs": ["<(napajs_root)/inc"], "include_dirs": ["<(napajs_inc)"],
"link_settings": { "link_settings": {
"libraries": ["<(napajs_root)/bin/<(library_prefix)napa<(library_suffix)"] "libraries": ["<(napajs_lib)"]
} }
} }
] ]

Просмотреть файл

@ -15,7 +15,7 @@
"markdown-table": "1.1.0" "markdown-table": "1.1.0"
}, },
"dependencies": { "dependencies": {
"napajs": ">= 0.1.0" "napajs": ">= 0.1.2"
}, },
"scripts": { "scripts": {
"install": "node-gyp configure && node-gyp build", "install": "node-gyp configure && node-gyp build",

Просмотреть файл

@ -2,9 +2,7 @@
// Licensed under the MIT license. // Licensed under the MIT license.
let assert = require('assert'); let assert = require('assert');
import * as path from "path"; let asyncNumber = require('..');
let asyncNumberDir: string = path.resolve(__dirname, '..');
let asyncNumber = require(asyncNumberDir);
let napa = require('napajs'); let napa = require('napajs');
let zone = napa.zone.create('zone'); let zone = napa.zone.create('zone');
@ -46,9 +44,9 @@ describe('Test suite for async-number', function() {
}); });
it('change number asynchronously on separate thread in napa zone', () => { it('change number asynchronously on separate thread in napa zone', () => {
return zone.execute((asyncNumberDir: string) => { return zone.execute(() => {
let assert = require('assert'); let assert = require('assert');
let asyncNumber = require(asyncNumberDir); let asyncNumber = require('..');
let now = asyncNumber.now(); let now = asyncNumber.now();
assert.equal(now, 0); assert.equal(now, 0);
@ -61,13 +59,13 @@ describe('Test suite for async-number', function() {
}); });
asyncNumber.increaseSync(3, (value: number) => {} ); asyncNumber.increaseSync(3, (value: number) => {} );
}, [asyncNumberDir]); });
}); });
it('change number synchronously on current thread in napa zone', () => { it('change number synchronously on current thread in napa zone', () => {
zone.execute((asyncNumberDir: string) => { zone.execute(() => {
let assert = require('assert'); let assert = require('assert');
let asyncNumber = require(asyncNumberDir); let asyncNumber = require('..');
let now = asyncNumber.now(); let now = asyncNumber.now();
assert.equal(now, 0); assert.equal(now, 0);
@ -84,6 +82,6 @@ describe('Test suite for async-number', function() {
assert.equal(now, 3); assert.equal(now, 3);
asyncNumber.increaseSync(3, (value: number) => {} ); asyncNumber.increaseSync(3, (value: number) => {} );
return 1; return 1;
}, [asyncNumberDir]); });
}); });
}) })

Просмотреть файл

@ -1,23 +1,7 @@
{ {
"variables": { "variables": {
"napajs_root": "<!(node -e \" \ "napajs_lib": "<!(node -e \"require('napajs/build').paths.lib\")",
var path = require('path'); \ "napajs_inc": "<!(node -e \"require('napajs/build').paths.inc\")"
process.stdout.write(path.resolve(path.dirname(require.resolve('napajs')), '..')); \
\")",
'conditions': [
['OS=="win"', {
"library_prefix": "",
"library_suffix": ".lib"
}],
['OS=="linux"', {
"library_prefix": "lib",
"library_suffix": ".dylib"
}],
['OS=="max"', {
"library_prefix": "lib",
"library_suffix": ".so"
}]
]
}, },
"targets": [ "targets": [
{ {
@ -25,27 +9,24 @@
"type": "<(library)", "type": "<(library)",
"product_extension": "", "product_extension": "",
"product_dir": "<(PRODUCT_DIR)/../../bin", "product_dir": "<(PRODUCT_DIR)/../../bin",
"sources": [ "src/addon.cpp"], "sources": [ "napa/addon.cpp"],
"defines": [ "defines": [
"NAPA_BINDING_EXPORTS",
"BUILDING_NODE_EXTENSION" "BUILDING_NODE_EXTENSION"
], ],
"include_dirs": ["<(napajs_root)/inc"] "include_dirs": ["<(napajs_inc)"]
}, },
{ {
"target_name": "addon.napa", "target_name": "addon.napa",
"type": "<(library)", "type": "<(library)",
"product_extension": "", "product_extension": "",
"product_dir": "<(PRODUCT_DIR)/../../bin", "product_dir": "<(PRODUCT_DIR)/../../bin",
"sources": [ "src/addon.cpp" ], "sources": [ "napa/addon.cpp" ],
"defines": [ "defines": [
"NAPA_EXPORTS",
"NAPA_BINDING_EXPORTS",
"BUILDING_NAPA_EXTENSION" "BUILDING_NAPA_EXTENSION"
], ],
"include_dirs": ["<(napajs_root)/inc"], "include_dirs": ["<(napajs_inc)"],
"link_settings": { "link_settings": {
"libraries": ["<(napajs_root)/bin/<(library_prefix)napa<(library_suffix)"] "libraries": ["<(napajs_lib)"]
} }
} }
] ]

Просмотреть файл

@ -15,7 +15,7 @@
"markdown-table": "1.1.0" "markdown-table": "1.1.0"
}, },
"dependencies": { "dependencies": {
"napajs": ">= 0.1.0" "napajs": ">= 0.1.2"
}, },
"scripts": { "scripts": {
"install": "node-gyp configure && node-gyp build", "install": "node-gyp configure && node-gyp build",

Просмотреть файл

@ -2,9 +2,7 @@
// Licensed under the MIT license. // Licensed under the MIT license.
let assert = require('assert'); let assert = require('assert');
import * as path from "path"; let helloWorld = require('..');
let helloWorldDir: string = path.resolve(__dirname, '..');
let helloWorld = require(helloWorldDir);
let napa = require('napajs'); let napa = require('napajs');
let zone = napa.zone.create('zone'); let zone = napa.zone.create('zone');
@ -17,11 +15,11 @@ describe('Test suite for hello-word', function() {
}); });
it('prints the string "world" in napa zone', function() { it('prints the string "world" in napa zone', function() {
return zone.execute((helloWorldDir: string) => { return zone.execute(() => {
let helloWorld = require(helloWorldDir); let helloWorld = require('..');
let result: string = helloWorld.hello(); let result: string = helloWorld.hello();
return result; return result;
}, [helloWorldDir]).then((result : any) => { }).then((result : any) => {
assert.equal(result.value, 'world'); assert.equal(result.value, 'world');
}); });
}); });

Просмотреть файл

@ -19,89 +19,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN) set(CMAKE_VISIBILITY_INLINES_HIDDEN)
# Prefix an suffix of Library name for different OS. # Build plus-number library
if (WIN32) add_subdirectory(src)
set(LIBRARY_PREFIX "")
set(LIBRARY_SUFFIX ".lib")
elseif (APPLE)
set(LIBRARY_PREFIX "lib")
set(LIBRARY_SUFFIX ".dylib")
else()
set(LIBRARY_PREFIX "lib")
set(LIBRARY_SUFFIX ".so")
endif()
execute_process(COMMAND node -e # Build napa addon
"var path = require('path'); process.stdout.write(path.resolve(path.dirname(require.resolve('napajs')), '..'));" add_subdirectory(napa)
RESULT_VARIABLE ERR
OUTPUT_VARIABLE NAPAJS_ROOT)
if(ERR)
message(FATAL_ERROR "Failed to get NAPAJS_ROOT")
endif(ERR)
#######################################################################################
# Build plus-number library.
set(LIBRARY_TARGET_NAME "plus-number")
# The generated library
add_library(${LIBRARY_TARGET_NAME} SHARED "src/plus-number.cpp")
# Include directories
target_include_directories(${LIBRARY_TARGET_NAME} PRIVATE inc)
# Compiler definitions
target_compile_definitions(${LIBRARY_TARGET_NAME} PRIVATE NAPA_EXAMPLE_API)
#######################################################################################
# Build napa addon.
set(NAPA_ADDON_TARGET_NAME "${PROJECT_NAME}.napa")
# The generated library
add_library(${NAPA_ADDON_TARGET_NAME} SHARED "src/addon.cpp")
set_target_properties(${NAPA_ADDON_TARGET_NAME} PROPERTIES PREFIX "" SUFFIX "")
# Include directories
target_include_directories(${NAPA_ADDON_TARGET_NAME} PRIVATE
inc
${CMAKE_JS_INC}
${NAPAJS_ROOT}/inc)
# Compiler definitions
target_compile_definitions(${NAPA_ADDON_TARGET_NAME} PRIVATE
NAPA_EXPORTS
NAPA_BINDING_EXPORTS
BUILDING_NAPA_EXTENSION)
# Link libraries
target_link_libraries(${NAPA_ADDON_TARGET_NAME} PRIVATE
plus-number
${CMAKE_JS_LIB}
${NAPAJS_ROOT}/bin/${LIBRARY_PREFIX}napa${LIBRARY_SUFFIX})
#######################################################################################
if (CMAKE_JS_VERSION)
# Build napa addon for node.
set(NODE_ADDON_TARGET_NAME "${PROJECT_NAME}.node")
# The generated library
add_library(${NODE_ADDON_TARGET_NAME} SHARED "src/addon.cpp")
set_target_properties(${NODE_ADDON_TARGET_NAME} PROPERTIES PREFIX "" SUFFIX "")
# Include directories
target_include_directories(${NODE_ADDON_TARGET_NAME} PRIVATE
inc
${CMAKE_JS_INC}
${NAPAJS_ROOT}/inc)
# Compiler definitions
target_compile_definitions(${NODE_ADDON_TARGET_NAME} PRIVATE
NAPA_BINDING_EXPORTS
BUILDING_NODE_EXTENSION)
# Link libraries
target_link_libraries(${NODE_ADDON_TARGET_NAME} PRIVATE
plus-number
${CMAKE_JS_LIB})
endif()

Просмотреть файл

@ -38,7 +38,7 @@ namespace demo {
## Wrapper class ## Wrapper class
*addon.h* declares the wrapper class inherited from *NAPA_OBJECTWRAP* as follows, *plus-number-wrap.h* declares the wrapper class inherited from *NAPA_OBJECTWRAP* as follows,
```h ```h
#include <napa/module.h> #include <napa/module.h>
@ -85,11 +85,10 @@ namespace demo {
} // namespace napa } // namespace napa
``` ```
*addon.cpp* implements each functions as follows, *plus-number-wrap.cpp* implements each functions as follows,
```cpp ```cpp
#include "addon.h" #include "plus-number-wrap.h"
using namespace napa::demo; using namespace napa::demo;
using namespace v8; using namespace v8;
@ -125,7 +124,7 @@ void PlusNumberWrap::NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args
const int argc = 1; const int argc = 1;
Local<Value> argv[argc] = { args[0] }; Local<Value> argv[argc] = { args[0] };
auto constructor = NAPA_GET_PERSISTENT_CONSTRUCTOR(_exportName); auto constructor = NAPA_GET_PERSISTENT_CONSTRUCTOR(_exportName, PlusNumberWrap);
auto context = isolate->GetCurrentContext(); auto context = isolate->GetCurrentContext();
auto instance = constructor->NewInstance(context, argc, argv).ToLocalChecked(); auto instance = constructor->NewInstance(context, argc, argv).ToLocalChecked();
@ -171,6 +170,15 @@ void PlusNumberWrap::Add(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(Number::New(isolate, value)); args.GetReturnValue().Set(Number::New(isolate, value));
} }
```
*addon.cpp* implements the addon as below,
```cpp
#include "plus-number-wrap.h"
using namespace napa::demo;
using namespace v8;
void CreatePlusNumber(const FunctionCallbackInfo<Value>& args) { void CreatePlusNumber(const FunctionCallbackInfo<Value>& args) {
PlusNumberWrap::NewInstance(args); PlusNumberWrap::NewInstance(args);

Просмотреть файл

@ -0,0 +1,58 @@
execute_process(COMMAND node -e "require('napajs/build').paths.inc" RESULT_VARIABLE ERR OUTPUT_VARIABLE NAPAJS_INC)
if(ERR)
message(FATAL_ERROR "Failed to resolve napa include directory")
endif(ERR)
execute_process(COMMAND node -e "require('napajs/build').paths.lib" RESULT_VARIABLE ERR OUTPUT_VARIABLE NAPAJS_LIB)
if(ERR)
message(FATAL_ERROR "Failed to resolve napa library path")
endif(ERR)
#######################################################################################
# Build napa addon.
set(NAPA_ADDON_TARGET_NAME "${PROJECT_NAME}.napa")
# The generated library
add_library(${NAPA_ADDON_TARGET_NAME} SHARED "addon.cpp" "plus-number-wrap.cpp")
set_target_properties(${NAPA_ADDON_TARGET_NAME} PROPERTIES PREFIX "" SUFFIX "")
# Include directories
target_include_directories(${NAPA_ADDON_TARGET_NAME} PRIVATE
../inc
${CMAKE_JS_INC}
${NAPAJS_INC})
# Compiler definitions
target_compile_definitions(${NAPA_ADDON_TARGET_NAME} PRIVATE
BUILDING_NAPA_EXTENSION)
# Link libraries
target_link_libraries(${NAPA_ADDON_TARGET_NAME} PRIVATE
plus-number
${CMAKE_JS_LIB}
${NAPAJS_LIB})
#######################################################################################
# Build napa addon for node.
set(NODE_ADDON_TARGET_NAME "${PROJECT_NAME}.node")
# The generated library
add_library(${NODE_ADDON_TARGET_NAME} SHARED "addon.cpp" "plus-number-wrap.cpp")
set_target_properties(${NODE_ADDON_TARGET_NAME} PROPERTIES PREFIX "" SUFFIX "")
# Include directories
target_include_directories(${NODE_ADDON_TARGET_NAME} PRIVATE
../inc
${CMAKE_JS_INC}
${NAPAJS_INC})
# Compiler definitions
target_compile_definitions(${NODE_ADDON_TARGET_NAME} PRIVATE
BUILDING_NODE_EXTENSION)
# Link libraries
target_link_libraries(${NODE_ADDON_TARGET_NAME} PRIVATE
plus-number
${CMAKE_JS_LIB})

Просмотреть файл

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "plus-number-wrap.h"
using namespace napa::demo;
using namespace v8;
void CreatePlusNumber(const FunctionCallbackInfo<Value>& args) {
PlusNumberWrap::NewInstance(args);
}
void InitAll(Local<Object> exports) {
PlusNumberWrap::Init();
NAPA_SET_METHOD(exports, "createPlusNumber", CreatePlusNumber);
}
NAPA_MODULE(addon, InitAll);

Просмотреть файл

@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. // Licensed under the MIT license.
#include "addon.h" #include "plus-number-wrap.h"
using namespace napa::demo; using namespace napa::demo;
using namespace v8; using namespace v8;
@ -83,15 +83,3 @@ void PlusNumberWrap::Add(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(Number::New(isolate, value)); args.GetReturnValue().Set(Number::New(isolate, value));
} }
void CreatePlusNumber(const FunctionCallbackInfo<Value>& args) {
PlusNumberWrap::NewInstance(args);
}
void InitAll(Local<Object> exports) {
PlusNumberWrap::Init();
NAPA_SET_METHOD(exports, "createPlusNumber", CreatePlusNumber);
}
NAPA_MODULE(addon, InitAll);

Просмотреть файл

@ -14,7 +14,7 @@
"markdown-table": "1.1.0" "markdown-table": "1.1.0"
}, },
"dependencies": { "dependencies": {
"napajs": ">= 0.1.0" "napajs": ">= 0.1.2"
}, },
"scripts": { "scripts": {
"install": "cmake-js compile", "install": "cmake-js compile",

Просмотреть файл

@ -0,0 +1,11 @@
# Build plus-number library.
set(TARGET_NAME "plus-number")
# The generated library
add_library(${TARGET_NAME} SHARED "plus-number.cpp")
# Include directories
target_include_directories(${TARGET_NAME} PRIVATE ../inc)
# Compiler definitions
target_compile_definitions(${TARGET_NAME} PRIVATE NAPA_EXAMPLE_API)

Просмотреть файл

@ -2,9 +2,7 @@
// Licensed under the MIT license. // Licensed under the MIT license.
let assert = require('assert'); let assert = require('assert');
import * as path from "path"; let plusNumber = require('..');
let plusNumberDir: string = path.resolve(__dirname, '..');
let plusNumber = require(plusNumberDir);
let napa = require('napajs'); let napa = require('napajs');
let zone = napa.zone.create('zone'); let zone = napa.zone.create('zone');
@ -28,24 +26,24 @@ describe('Test suite for plus-number', function() {
}); });
it('adds a given value in napa zone', function() { it('adds a given value in napa zone', function() {
return zone.execute((plusNumberDir: string) => { return zone.execute(() => {
let plusNumber = require(plusNumberDir); let plusNumber = require('..');
let po = plusNumber.createPlusNumber(3); let po = plusNumber.createPlusNumber(3);
let result: number = po.add(4); let result: number = po.add(4);
return result; return result;
}, [plusNumberDir]) })
.then((result: any) => { .then((result: any) => {
assert.equal(result.value, 7); assert.equal(result.value, 7);
}); });
}); });
it('adds a given value in node zone', function() { it('adds a given value in node zone', function() {
return napa.zone.node.execute((plusNumberDir: string) => { return napa.zone.node.execute(() => {
let plusNumber = require(plusNumberDir); let plusNumber = require('..');
let po = plusNumber.createPlusNumber(3); let po = plusNumber.createPlusNumber(3);
let result: number = po.add(4); let result: number = po.add(4);
return result; return result;
}, [plusNumberDir]) })
.then((result: any) => { .then((result: any) => {
assert.equal(result.value, 7); assert.equal(result.value, 7);
}); });

Просмотреть файл

@ -19,32 +19,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN) set(CMAKE_VISIBILITY_INLINES_HIDDEN)
# Prefix an suffix of Library name for different OS.
if (WIN32)
set(LIBRARY_PREFIX "")
set(LIBRARY_SUFFIX ".lib")
elseif (APPLE)
set(LIBRARY_PREFIX "lib")
set(LIBRARY_SUFFIX ".dylib")
else()
set(LIBRARY_PREFIX "lib")
set(LIBRARY_SUFFIX ".so")
endif()
execute_process(COMMAND node -e
"var path = require('path'); process.stdout.write(path.resolve(path.dirname(require.resolve('napajs')), '..'));"
RESULT_VARIABLE ERR
OUTPUT_VARIABLE NAPAJS_ROOT)
if(ERR)
message(FATAL_ERROR "Failed to get NAPAJS_ROOT")
endif(ERR)
file(GLOB LIBRARY-FILES "../bin/*plus-number.*") file(GLOB LIBRARY-FILES "../bin/*plus-number.*")
file(COPY ${LIBRARY-FILES} DESTINATION ${PROJECT_SOURCE_DIR}/bin/) file(COPY ${LIBRARY-FILES} DESTINATION ${PROJECT_SOURCE_DIR}/bin/)
# Files to compile # Files to compile
file(GLOB SOURCE_FILES file(GLOB SOURCE_FILES "main.cpp")
"main.cpp")
# The executable name # The executable name
set(TARGET_NAME "library-test") set(TARGET_NAME "library-test")
@ -55,6 +34,7 @@ add_executable(${TARGET_NAME} ${SOURCE_FILES})
# Include directories # Include directories
target_include_directories(${TARGET_NAME} PRIVATE ../inc) target_include_directories(${TARGET_NAME} PRIVATE ../inc)
find_library(PLUS_NUMBER_LIBRARY NAMES plus-number PATHS ${PROJECT_SOURCE_DIR}/bin)
# Link libraries # Link libraries
target_link_libraries(${TARGET_NAME} PRIVATE target_link_libraries(${TARGET_NAME} PRIVATE ${PLUS_NUMBER_LIBRARY})
${PROJECT_SOURCE_DIR}/bin/${LIBRARY_PREFIX}plus-number${LIBRARY_SUFFIX})