Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: add tool to check for N-API modules #346

Closed
wants to merge 3 commits into from

Conversation

gabrielschulhof
Copy link
Contributor

Adds tools/check-napi.js which uses nm -a on UNIX and
dumpbin /imports on Windows to check whether a given .node file
is an N-API module or not. Intentionally ignores files named
nothing.node because they are node-addon-api build artefacts.

Sets the target type for nothing (which gets built when a built-in
N-API is found to be present) to 'static_library' so as to avoid the
creation of nothing.node files which incorrectly end up showing up in
the output of check-napi.js as non-N-API modules.

src/node_api.gyp Outdated
@@ -1,7 +1,8 @@
{
'targets': [
{
'target_name': 'nothing'
'target_name': 'nothing',
'type': 'static_library'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part of the intended change? Does not seem related to adding the script to check if modules are N-API or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kind of is. We need this so that node-addon-api might stop creating nothing.node files in other projects. I did change the script to ignore nothing.node files, but that's kind of a kludge. There's nothing stopping module authors from calling their module nothing, and the script would ignore it.

I'll PR it separately.

@mhdawson mhdawson mentioned this pull request Sep 18, 2018
6 tasks
Adds tools/check-napi.js which uses `nm -a` on UNIX and
`dumpbin /imports` on Windows to check whether a given `.node` file
is an N-API module or not. Intentionally ignores files named
`nothing.node` because they are node-addon-api build artefacts.

Sets the target type for `nothing` (which gets built when a built-in
N-API is found to be present) to `'static_library'` so as to avoid the
creation of `nothing.node` files which incorrectly end up showing up in
the output of `check-napi.js` as non-N-API modules.
line = line.match(/([0-9a-f]*)? ([a-zA-Z]) (.*$)/);
line.shift();
if (line[1] === 'U') {
if (line[2].match(/^napi/)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't returning from here and changing the condition at 41 to !soFar be better?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we don't want to get the matches but need to check if the string matches the format, then I believe RegExp#test would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having return true up here and then return soFar at the bottom. I'd rather have one return statement for the whole function.

The reason I don't have !soFar at the top is so if, in the future, we add a check that will disqualify a file from being N-API, we can set soFar to false and have the rest short-circuit.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough 👍

child.stdout.on('data', (chunk) => {
if (isNapi === undefined) {
chunk = leftover + chunk.toString();
const haveLeftover = !!chunk.match(/[\r\n]+$/);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed as it is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the chunk guaranteed to end on a line boundary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, NM. You meant the variable, not the rest of the logic. You're right.

const haveLeftover = !!chunk.match(/[\r\n]+$/);
chunk = chunk.split(/[\r\n]+/);
leftover = chunk.pop();
isNapi = chunk.reduce(reducer, isNapi);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, we can kill the child process if isNapi is true, right?

@gabrielschulhof gabrielschulhof force-pushed the check-napi branch 2 times, most recently from 6b52ecc to 94c44e3 Compare September 19, 2018 13:41
@@ -0,0 +1,96 @@
// Descend into a directory structure and, for each file matching *.node, output
// based on the imports found in the file whether it's an N-API module or not.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Generally strict mode is preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye fixed, and a good thing too, because it had implications for the rest of the code. Thanks!

// Use nm -a to list symbols.
function checkFileUNIX(file) {
checkFile(file, 'nm', ['-a', file], (soFar, line) => {
if (soFar === undefined) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Indentation is not consistent in this and the other two functions as well.

// Descend into a directory structure and pass each file ending in '.node' to
// one of the above checks, depending on the OS.
function recurse(top) {
fs.readdir(top, (error, items) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This error is ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@mhdawson
Copy link
Member

I'll assume the script does what it needs to, but I think we should also add some doc somewhere that explains how it is used.

@gabrielschulhof
Copy link
Contributor Author

@mhdawson I added a document that explains the workings of the script, and I linked it off the main README.md – just like the documentation for the conversion script, after which I modelled it.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

mhdawson pushed a commit that referenced this pull request Sep 21, 2018
Adds tools/check-napi.js which uses `nm -a` on UNIX and
`dumpbin /imports` on Windows to check whether a given `.node` file
is an N-API module or not. Intentionally ignores files named
`nothing.node` because they are node-addon-api build artefacts.

Sets the target type for `nothing` (which gets built when a built-in
N-API is found to be present) to `'static_library'` so as to avoid the
creation of `nothing.node` files which incorrectly end up showing up in
the output of `check-napi.js` as non-N-API modules.

PR-URL: #346
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
@mhdawson
Copy link
Member

Landed as fd3c37b

@mhdawson mhdawson closed this Sep 21, 2018
@gabrielschulhof gabrielschulhof deleted the check-napi branch October 2, 2018 18:03
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
Adds tools/check-napi.js which uses `nm -a` on UNIX and
`dumpbin /imports` on Windows to check whether a given `.node` file
is an N-API module or not. Intentionally ignores files named
`nothing.node` because they are node-addon-api build artefacts.

Sets the target type for `nothing` (which gets built when a built-in
N-API is found to be present) to `'static_library'` so as to avoid the
creation of `nothing.node` files which incorrectly end up showing up in
the output of `check-napi.js` as non-N-API modules.

PR-URL: nodejs/node-addon-api#346
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
Adds tools/check-napi.js which uses `nm -a` on UNIX and
`dumpbin /imports` on Windows to check whether a given `.node` file
is an N-API module or not. Intentionally ignores files named
`nothing.node` because they are node-addon-api build artefacts.

Sets the target type for `nothing` (which gets built when a built-in
N-API is found to be present) to `'static_library'` so as to avoid the
creation of `nothing.node` files which incorrectly end up showing up in
the output of `check-napi.js` as non-N-API modules.

PR-URL: nodejs/node-addon-api#346
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
Adds tools/check-napi.js which uses `nm -a` on UNIX and
`dumpbin /imports` on Windows to check whether a given `.node` file
is an N-API module or not. Intentionally ignores files named
`nothing.node` because they are node-addon-api build artefacts.

Sets the target type for `nothing` (which gets built when a built-in
N-API is found to be present) to `'static_library'` so as to avoid the
creation of `nothing.node` files which incorrectly end up showing up in
the output of `check-napi.js` as non-N-API modules.

PR-URL: nodejs/node-addon-api#346
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
Adds tools/check-napi.js which uses `nm -a` on UNIX and
`dumpbin /imports` on Windows to check whether a given `.node` file
is an N-API module or not. Intentionally ignores files named
`nothing.node` because they are node-addon-api build artefacts.

Sets the target type for `nothing` (which gets built when a built-in
N-API is found to be present) to `'static_library'` so as to avoid the
creation of `nothing.node` files which incorrectly end up showing up in
the output of `check-napi.js` as non-N-API modules.

PR-URL: nodejs/node-addon-api#346
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants