addon-tools-raub
Advanced tools
Comparing version 1.0.0 to 2.0.1
@@ -20,6 +20,6 @@ 'use strict'; | ||
switch (platform) { | ||
case 'win32' : return process.arch === 'x64' ? 'win64' : 'win32'; | ||
case 'linux' : return process.arch === 'x64' ? 'linux64' : 'linux32'; | ||
case 'win32' : return process.arch === 'x64' ? 'win64' : 'win32'; | ||
case 'linux' : return process.arch === 'x64' ? 'linux64' : 'linux32'; | ||
case 'darwin' : return 'mac64'; | ||
default : throw new Error(`Platform "${platform}" is not supported.`); | ||
default : throw new Error(`Platform "${platform}" is not supported.`); | ||
} | ||
@@ -62,2 +62,3 @@ }; | ||
const includePath = `${nanInclude} ${thisInclude}`; | ||
const binPath = currentDir; | ||
@@ -73,2 +74,3 @@ const mkdirPath = isWindows ? `${rootPath}/_mkdir.bat` : 'mkdir'; | ||
binPath, | ||
rootPath, | ||
@@ -80,2 +82,3 @@ includePath, | ||
bin() { return console.log(binPath); }, | ||
root() { return console.log(rootPath); }, | ||
@@ -82,0 +85,0 @@ include() { console.log(includePath); }, |
{ | ||
"author": "Luis Blanco <luisblanco1337@gmail.com>", | ||
"name": "addon-tools-raub", | ||
"version": "1.0.0", | ||
"version": "2.0.1", | ||
"description": "Helpers for Node.js addons and dependency packages", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
311
README.md
@@ -16,3 +16,3 @@ # Addon Tools | ||
Useful links: [V8 Ref](https://v8docs.nodesource.com/node-0.8/d2/dc3/namespacev8.html), | ||
Useful links: [V8 Ref](https://v8.paulfryzel.com/docs/master/), | ||
[Nan Docs](https://github.com/nodejs/nan#api), | ||
@@ -51,54 +51,90 @@ [GYP Docs](https://gyp.gsrc.io/docs/UserDocumentation.md). | ||
* Crossplatform commands can be put into the variables for later use. | ||
<details> | ||
``` | ||
'variables': { | ||
'rm' : '<!(node -e "require(\'addon-tools-raub\').rm()")', | ||
'cp' : '<!(node -e "require(\'addon-tools-raub\').cp()")', | ||
'mkdir' : '<!(node -e "require(\'addon-tools-raub\').mkdir()")', | ||
}, | ||
``` | ||
<summary>Crossplatform commands</summary> | ||
``` | ||
'variables': { | ||
'rm' : '<!(node -e "require(\'addon-tools-raub\').rm()")', | ||
'cp' : '<!(node -e "require(\'addon-tools-raub\').cp()")', | ||
'mkdir' : '<!(node -e "require(\'addon-tools-raub\').mkdir()")', | ||
}, | ||
``` | ||
On both Windows and Unix those are the console commands for various | ||
file system operations. No need for GYP conditions, yay! | ||
</details> | ||
* Include directories for Addon Tools and Nan (which is preinstalled with Addon Tools) | ||
are accessible as shown below. | ||
``` | ||
'include_dirs': [ | ||
'<!@(node -e "require(\'addon-tools-raub\').include()")', | ||
], | ||
``` | ||
<details> | ||
* Intermediate build-files can be removed in a separate build-step with `<(rm)`. | ||
<summary>Addon binary directory</summary> | ||
``` | ||
'variables': { | ||
'binary' : '<!(node -e "require(\'addon-tools-raub\').bin()")', | ||
}, | ||
``` | ||
In some cases, you'd like to have your addon installed for multiple architectures | ||
simultaneously. For example, when using NVM to fluently switch environments. | ||
Because the target directory is different for each arch, you only have to do | ||
`npm rebuild` after the first switch. | ||
</details> | ||
<details> | ||
<summary>Show Snippet</summary> | ||
<summary>Include directories</summary> | ||
``` | ||
'include_dirs': [ | ||
'<!@(node -e "require(\'addon-tools-raub\').include()")', | ||
], | ||
``` | ||
Those are the directory paths to C++ include files for Addon Tools and Nan | ||
(which is preinstalled with Addon Tools) | ||
</details> | ||
``` | ||
[ 'OS=="linux"', { 'action' : [ | ||
'<(rm)', | ||
'<(module_root_dir)/build/Release/obj.target/addon/cpp/addon.o', | ||
'<(module_root_dir)/build/Release/addon.node' | ||
] } ], | ||
[ 'OS=="mac"', { 'action' : [ | ||
'<(rm)', | ||
'<(module_root_dir)/build/Release/obj.target/addon/cpp/addon.o', | ||
'<(module_root_dir)/build/Release/addon.node' | ||
] } ], | ||
[ 'OS=="win"', { 'action' : [ | ||
'<(rm)', | ||
'<(module_root_dir)/build/Release/addon.*', | ||
'<(module_root_dir)/build/Release/obj/addon/*.*' | ||
] } ], | ||
``` | ||
<details> | ||
<summary>Remove intermediates</summary> | ||
``` | ||
[ 'OS=="linux"', { 'action' : [ | ||
'<(rm)', | ||
'<(module_root_dir)/build/Release/obj.target/addon/cpp/addon.o', | ||
'<(module_root_dir)/build/Release/addon.node' | ||
] } ], | ||
[ 'OS=="mac"', { 'action' : [ | ||
'<(rm)', | ||
'<(module_root_dir)/build/Release/obj.target/addon/cpp/addon.o', | ||
'<(module_root_dir)/build/Release/addon.node' | ||
] } ], | ||
[ 'OS=="win"', { 'action' : [ | ||
'<(rm)', | ||
'<(module_root_dir)/build/Release/addon.*', | ||
'<(module_root_dir)/build/Release/obj/addon/*.*' | ||
] } ], | ||
``` | ||
Build-files can be removed in a separate build-step with `<(rm)`. Those are | ||
usually PDB and OBJ files, which are rather big. However, in case of a hardcore | ||
debug session you might want to comment this out. | ||
</details> | ||
### Binary dependencies | ||
### Binary dependency package | ||
If you design a module with binary dependencies for several platforms, Addon Tools | ||
If you design a module with binary dependencies for several platforms, **Addon Tools** | ||
would encourage you to abide by the following rules: | ||
* Your binary directories are: | ||
* bin-win32 | ||
@@ -109,95 +145,57 @@ * bin-win64 | ||
* bin-mac64 | ||
* The following piece of code in your `index.js` without changes. Method `paths()` | ||
is described [here](#indexjs). | ||
``` | ||
module.exports = require('addon-tools-raub').paths(__dirname); | ||
``` | ||
* Your whole **binding.gyp**: | ||
<details> | ||
<summary>Show binding.gyp</summary> | ||
``` | ||
{ | ||
'variables': { | ||
'rm' : '<!(node -e "require(\'addon-tools-raub\').rm()")', | ||
'rem' : '<!(node -e "require(\'.\').rem()")', | ||
'XALL%': 'false', | ||
}, | ||
'targets': [ | ||
{ | ||
'target_name' : 'remove_extras', | ||
'type' : 'none', | ||
'conditions' : [['XALL=="false"', {'actions': [ | ||
{ | ||
'action_name' : 'Unnecessary binaries removed.', | ||
'inputs' : [], | ||
'outputs' : ['build'], | ||
'action' : ['<(rm)', '-rf', '<@(rem)'], | ||
} | ||
]}]], | ||
} | ||
] | ||
} | ||
``` | ||
Notice the `XALL` variable here. If the package is installed with `npm i`, then | ||
quite expectedly all but the required arch directories are removed. But with | ||
`npm i --XALL` you can keep all the binaries. It might be useful when debugging | ||
multiple archs and switching Node.js versions with | ||
[NVM](https://github.com/creationix/nvm). | ||
</details> | ||
``` | ||
module.exports = require('addon-tools-raub').paths(__dirname); | ||
``` | ||
* Your whole `binding.gyp`: | ||
<details> | ||
<summary>Show Snippet</summary> | ||
``` | ||
{ | ||
'variables': { | ||
'rm' : '<!(node -e "require(\'addon-tools-raub\').rm()")', | ||
'rem' : '<!(node -e "require(\'.\').rem()")', | ||
'XALL%': 'false', | ||
}, | ||
'targets': [ | ||
{ | ||
'target_name' : 'remove_extras', | ||
'type' : 'none', | ||
'conditions' : [['XALL=="false"', {'actions': [ | ||
{ | ||
'action_name' : 'Unnecessary binaries removed.', | ||
'inputs' : [], | ||
'outputs' : ['build'], | ||
'action' : ['<(rm)', '-rf', '<@(rem)'], | ||
} | ||
]}]], | ||
} | ||
] | ||
} | ||
``` | ||
Notice the `XALL` variable here. If the package is installed with `npm i`, then | ||
quite expectedly all but the required arch directories are removed. But with | ||
`npm i --XALL` you can keep all the binaries. It might be useful when debugging | ||
multiple archs and switching Node.js versions with | ||
[NVM](https://github.com/creationix/nvm). | ||
</details> | ||
### Compiled addon | ||
If you always copy your compiled addon to the `binary` directory, it will be easy to | ||
`require()` it without any hesitation. For copying, you can use the following snippet: | ||
It is easy to build a C++ addon with **Addon Tools**. To have a full picture, you | ||
can view the | ||
[official example](https://github.com/node-3d/addon-tools-raub/tree/master/examples/addon). | ||
<details> | ||
The main file for an addon is **binding.gyp**. Here's a snippet with most of the features. | ||
<summary>Show Snippet</summary> | ||
``` | ||
{ | ||
'target_name' : 'make_directory', | ||
'type' : 'none', | ||
'dependencies' : ['MY_ADDON'], | ||
'actions' : [{ | ||
'action_name' : 'Directory created.', | ||
'inputs' : [], | ||
'outputs' : ['build'], | ||
'action': ['<(mkdir)', '-p', 'binary'] | ||
}], | ||
}, | ||
{ | ||
'target_name' : 'copy_binary', | ||
'type' : 'none', | ||
'dependencies' : ['make_directory'], | ||
'actions' : [{ | ||
'action_name' : 'Module copied.', | ||
'inputs' : [], | ||
'outputs' : ['binary'], | ||
'action' : ['<(cp)', 'build/Release/MY_ADDON.node', 'binary/MY_ADDON.node'], | ||
}], | ||
}, | ||
``` | ||
</details> | ||
Here `MY_ADDON` should be replaced by any name you like. Then require like | ||
this: | ||
``` | ||
module.exports = require('./binary/MY_ADDON'); | ||
``` | ||
#### Generic addon snippet | ||
<details> | ||
@@ -217,2 +215,3 @@ | ||
'mkdir' : '<!(node -e "require(\'addon-tools-raub\').mkdir()")', | ||
'binary' : '<!(node -e "require(\'addon-tools-raub\').bin()")', | ||
'EXT_LIB_include' : '<!(node -e "require(\'node-deps-EXT_LIB-raub\').include()")', | ||
@@ -264,3 +263,5 @@ 'EXT_LIB_bin' : '<!(node -e "require(\'node-deps-EXT_LIB-raub\').bin()")', | ||
'AdditionalOptions' : [ | ||
'/O2','/Oy','/GL','/GF','/Gm-','/EHsc', | ||
'/O2','/Oy', # Comment this for debugging | ||
# '/Z7', # Unomment this for debugging | ||
'/GL','/GF','/Gm-','/EHsc', | ||
'/MT','/GS','/Gy','/GR-','/Gd', | ||
@@ -286,3 +287,3 @@ ] | ||
'outputs' : ['build'], | ||
'action': ['<(mkdir)', '-p', 'binary'] | ||
'action': ['<(mkdir)', '-p', '<(binary)'] | ||
}], | ||
@@ -298,3 +299,3 @@ }, | ||
'outputs' : ['binary'], | ||
'action' : ['<(cp)', 'build/Release/MY_ADDON.node', 'binary/MY_ADDON.node'], | ||
'action' : ['<(cp)', 'build/Release/MY_ADDON.node', '<(binary)/MY_ADDON.node'], | ||
}], | ||
@@ -336,2 +337,9 @@ }, | ||
Then require the built module like this: | ||
``` | ||
const { binPath } = require('addon-tools-raub'); | ||
const core = require(`./${binPath}/MY_ADDON`); | ||
``` | ||
</details> | ||
@@ -347,15 +355,15 @@ | ||
so that you can replace: | ||
``` | ||
// #include <v8.h> // already in node.h | ||
// #include <node.h> // already in nan.h | ||
#include <nan.h> | ||
``` | ||
with | ||
``` | ||
#include <addon-tools.hpp> // or event-emitter.hpp | ||
``` | ||
``` | ||
// #include <v8.h> // node.h includes it | ||
// #include <node.h> // nan.h includes it | ||
#include <nan.h> | ||
``` | ||
with | ||
``` | ||
#include <addon-tools.hpp> // or event-emitter.hpp | ||
``` | ||
In gyp, the include directory should be set for your addon to know where to get it. | ||
@@ -507,3 +515,4 @@ As it was mentioned above, this can be done automatically. Also an actual path to the | ||
NOTE: The conversion from `Nan::Utf8String` to `std::string` (via `char *`) is possible with unary `*` operator. | ||
NOTE: The conversion from `Nan::Utf8String` to `std::string` (via `char *`) | ||
is possible with unary `*` operator. | ||
@@ -589,6 +598,5 @@ </details> | ||
* `void *getImageData(value)` - if value is a TypedArray, then the result of | ||
* `void *getData(value)` - if value is a TypedArray, then the result of | ||
`getArrayData(value)` is returned. Otherwise if value has `'data'` property, it's | ||
content is then returned as `node::Buffer`. Returns `NULL` for empty JS values. | ||
For unacceptable values throws TypeError. | ||
content is then returned as `node::Buffer`. Returns `nullptr` in other cases. | ||
@@ -605,6 +613,6 @@ </details> | ||
input `dir`. | ||
* `bin()` - prints platform binary path. | ||
* `bin()` - prints platform binary directory absolute path. | ||
* `rem()` - prints a space-separated list of binary paths to be cleaned on this platform. | ||
* `include()` - prints include directory for this `dir`. | ||
* `binPath` - platform binary path. | ||
* `binPath` - platform binary directory absolute path. | ||
* `remPath` - a space-separated list of binary paths to be cleaned on this platform. | ||
@@ -618,2 +626,4 @@ * `includePath` - include directory for this `dir`. | ||
* `mkdir()` - prints the location of `'_mkdir.bat'` file on Windows and plain `mkdir` on Unix. | ||
* `bin()` - prints platform binary directory name. | ||
* `binPath` - platform binary directory name. | ||
* `rootPath` - where `'addon-tools-raub'` module is situated. | ||
@@ -691,3 +701,3 @@ * `includePath` - both `'addon-tools-raub'` and `'nan'` include paths. | ||
NOTE: This implementation has some minor deviations from the above standard. | ||
> Note: This implementation has some minor deviations from the above standard. | ||
Specifically there is no static `EventEmitter.defaultMaxListeners` property. | ||
@@ -706,9 +716,9 @@ However the dynamic one persists and is infinite (`0`) by default. | ||
* `void emit(const std::string &name, int argc = 0, v8::Local<v8::Value> *argv = NULL)` | ||
* `void emit(const std::string &name, int argc = 0, v8::Local<v8::Value> *argv = NULL)` - | ||
emits an event with the given `name` and, optionally, some additional arguments where | ||
`argc` is the number of arguments and `argv` is a pointer to the arguments array. | ||
* `void on(const std::string &name, v8::Local<v8::Value> that, const std::string &method)` | ||
subscribes `that[method]` to receive `name` events from this emitter, basically | ||
`emitter.on(name, that[method])`. | ||
* `void on(const std::string &name, V8_VAR_FUNC cb)` - | ||
subscribes `cb` to receive `name` events from this emitter, basically | ||
`emitter.on(name, cb)`. | ||
@@ -739,4 +749,5 @@ * `virtual void _destroy()` - destroys the object, i.e. deactivates it and frees | ||
NOTE: Do not forget to call `EventEmitter::init()` once, in the module `init()`. | ||
> Note: Do not forget to call `EventEmitter::init()` once, in the module `init()`. | ||
<details> | ||
@@ -792,1 +803,3 @@ | ||
string. | ||
> Note: Don't do it in GC-accessible code: sometimes it works, sometimes it crashes. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55
789
54707