Skip to content

Commit 87096f4

Browse files
mafintoshstaltz
authored andcommitted
fork node-gyp-build at 4.6.0
0 parents  commit 87096f4

File tree

12 files changed

+621
-0
lines changed

12 files changed

+621
-0
lines changed

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
strategy:
6+
matrix:
7+
os: [ubuntu-latest, macos-latest, windows-latest]
8+
node: [8, 10, 12, 14, 16]
9+
runs-on: ${{ matrix.os }}
10+
name: ${{ matrix.os }} / Node ${{ matrix.node }}
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
- name: Use node ${{ matrix.node }}
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: ${{ matrix.node }}
18+
- name: Install
19+
run: npm install
20+
- name: Test
21+
run: npm test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test.js
2+
.github

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Mathias Buus
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# node-gyp-build
2+
3+
> Build tool and bindings loader for [`node-gyp`][node-gyp] that supports prebuilds.
4+
5+
```
6+
npm install node-gyp-build
7+
```
8+
9+
[![Test](https://github.com/prebuild/node-gyp-build/actions/workflows/test.yml/badge.svg)](https://github.com/prebuild/node-gyp-build/actions/workflows/test.yml)
10+
11+
Use together with [`prebuildify`][prebuildify] to easily support prebuilds for your native modules.
12+
13+
## Usage
14+
15+
> **Note.** Prebuild names have changed in [`prebuildify@3`][prebuildify] and `node-gyp-build@4`. Please see the documentation below.
16+
17+
`node-gyp-build` works similar to [`node-gyp build`][node-gyp] except that it will check if a build or prebuild is present before rebuilding your project.
18+
19+
It's main intended use is as an npm install script and bindings loader for native modules that bundle prebuilds using [`prebuildify`][prebuildify].
20+
21+
First add `node-gyp-build` as an install script to your native project
22+
23+
``` js
24+
{
25+
...
26+
"scripts": {
27+
"install": "node-gyp-build"
28+
}
29+
}
30+
```
31+
32+
Then in your `index.js`, instead of using the [`bindings`](https://www.npmjs.com/package/bindings) module use `node-gyp-build` to load your binding.
33+
34+
``` js
35+
var binding = require('node-gyp-build')(__dirname)
36+
```
37+
38+
If you do these two things and bundle prebuilds with [`prebuildify`][prebuildify] your native module will work for most platforms
39+
without having to compile on install time AND will work in both node and electron without the need to recompile between usage.
40+
41+
Users can override `node-gyp-build` and force compiling by doing `npm install --build-from-source`.
42+
43+
Prebuilds will be attempted loaded from `MODULE_PATH/prebuilds/...` and then next `EXEC_PATH/prebuilds/...` (the latter allowing use with `zeit/pkg`)
44+
45+
## Supported prebuild names
46+
47+
If so desired you can bundle more specific flavors, for example `musl` builds to support Alpine, or targeting a numbered ARM architecture version.
48+
49+
These prebuilds can be bundled in addition to generic prebuilds; `node-gyp-build` will try to find the most specific flavor first. Prebuild filenames are composed of _tags_. The runtime tag takes precedence, as does an `abi` tag over `napi`. For more details on tags, please see [`prebuildify`][prebuildify].
50+
51+
Values for the `libc` and `armv` tags are auto-detected but can be overridden through the `LIBC` and `ARM_VERSION` environment variables, respectively.
52+
53+
## License
54+
55+
MIT
56+
57+
[prebuildify]: https://github.com/prebuild/prebuildify
58+
[node-gyp]: https://www.npmjs.com/package/node-gyp

bin.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env node
2+
3+
var proc = require('child_process')
4+
var os = require('os')
5+
var path = require('path')
6+
7+
if (!buildFromSource()) {
8+
proc.exec('node-gyp-build-test', function (err, stdout, stderr) {
9+
if (err) {
10+
if (verbose()) console.error(stderr)
11+
preinstall()
12+
}
13+
})
14+
} else {
15+
preinstall()
16+
}
17+
18+
function build () {
19+
var args = [os.platform() === 'win32' ? 'node-gyp.cmd' : 'node-gyp', 'rebuild']
20+
21+
try {
22+
args = [
23+
process.execPath,
24+
path.join(require.resolve('node-gyp/package.json'), '..', require('node-gyp/package.json').bin['node-gyp']),
25+
'rebuild'
26+
]
27+
} catch (_) {}
28+
29+
proc.spawn(args[0], args.slice(1), { stdio: 'inherit' }).on('exit', function (code) {
30+
if (code || !process.argv[3]) process.exit(code)
31+
exec(process.argv[3]).on('exit', function (code) {
32+
process.exit(code)
33+
})
34+
})
35+
}
36+
37+
function preinstall () {
38+
if (!process.argv[2]) return build()
39+
exec(process.argv[2]).on('exit', function (code) {
40+
if (code) process.exit(code)
41+
build()
42+
})
43+
}
44+
45+
function exec (cmd) {
46+
if (process.platform !== 'win32') {
47+
var shell = os.platform() === 'android' ? 'sh' : '/bin/sh'
48+
return proc.spawn(shell, ['-c', '--', cmd], {
49+
stdio: 'inherit'
50+
})
51+
}
52+
53+
return proc.spawn(process.env.comspec || 'cmd.exe', ['/s', '/c', '"' + cmd + '"'], {
54+
windowsVerbatimArguments: true,
55+
stdio: 'inherit'
56+
})
57+
}
58+
59+
function buildFromSource () {
60+
return hasFlag('--build-from-source') || process.env.npm_config_build_from_source === 'true'
61+
}
62+
63+
function verbose () {
64+
return hasFlag('--verbose') || process.env.npm_config_loglevel === 'verbose'
65+
}
66+
67+
// TODO (next major): remove in favor of env.npm_config_* which works since npm
68+
// 0.1.8 while npm_config_argv will stop working in npm 7. See npm/rfcs#90
69+
function hasFlag (flag) {
70+
if (!process.env.npm_config_argv) return false
71+
72+
try {
73+
return JSON.parse(process.env.npm_config_argv).original.indexOf(flag) !== -1
74+
} catch (_) {
75+
return false
76+
}
77+
}

build-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
process.env.NODE_ENV = 'test'
4+
5+
var path = require('path')
6+
var test = null
7+
8+
try {
9+
var pkg = require(path.join(process.cwd(), 'package.json'))
10+
if (pkg.name && process.env[pkg.name.toUpperCase().replace(/-/g, '_')]) {
11+
process.exit(0)
12+
}
13+
test = pkg.prebuild.test
14+
} catch (err) {
15+
// do nothing
16+
}
17+
18+
if (test) require(path.join(process.cwd(), test))
19+
else require('./')()

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (typeof process.addon === 'function') { // if the platform supports native resolving prefer that
2+
module.exports = process.addon.bind(process)
3+
} else { // else use the runtime version here
4+
module.exports = require('./node-gyp-build.js')
5+
}

0 commit comments

Comments
 (0)