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

Respect npm_config_arch for cross-compilation scenarios #567

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/util/versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
target: options.target || '',
platform: options.target_platform || process.platform,
target_platform: options.target_platform || process.platform,
arch: options.target_arch || process.arch,
target_arch: options.target_arch || process.arch,
arch: options.target_arch || process.env.npm_config_arch || process.arch,
target_arch: options.target_arch || process.env.npm_config_arch || process.arch,
libc: options.target_libc || detect_libc.family || 'unknown',
module_main: package_json.main,
toolset: options.toolset || '' // address https://github.com/mapbox/node-pre-gyp/issues/119
Expand Down
67 changes: 67 additions & 0 deletions test/target_arch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

const run = require('./run.util.js');
const test = require('tape');

const app = { 'name': 'app1', 'args': '' };
const target_arch = 'ia32';
const new_env = JSON.parse(JSON.stringify(process.env));
new_env.npm_config_arch = target_arch;

const tests = [
{
args: '--target_arch=' + target_arch,
opts: {}
},
{
args: '',
opts: {
env: new_env
}
}
];

/**
* Context:
*
* node-pre-gyp supports two ways to cross-compile for different architectures, namely:
* 1. Explicitly providing the --target_arch=${ARCH} argument
* 2. Setting npm_config_arch=${ARCH} as an environment variable. This is supported by
* Electron: https://www.electronjs.org/docs/tutorial/using-native-node-modules#using-npm
*
* This test file validates whether both scenarios work, assuming a x64 host that cross-compiles to ia32.
*/

tests.forEach((testInstance) => {
test(app.name + ' builds with custom target_arch ' + target_arch, (t) => {
run('node-pre-gyp', 'rebuild', '--build-from-source --fallback-to-build ' + testInstance.args, app, testInstance.opts, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});

test(app.name + ' produces a package with the correct target arch', (t) => {
run('node-pre-gyp', 'package', testInstance.args, app, testInstance.opts, (err, stdout, stderr) => {
t.ifError(err);
t.notEqual(stdout, '');
// Example: 'Binary staged at "build\stage\node-pre-gyp\app7\v0.1.0\Release\app7-v0.1.0-win32-ia32-napi-v1-node-node-v83.tar.gz"'
// The output should contain the target_arch, in this case ia32
const pattern = 'Binary staged at.*' + target_arch;
t.assert(
stderr.match(new RegExp(pattern, 'g')),
'Generated .tar.gz file contains target_arch ' + target_arch
);
t.end();
});

});

test(app.name, (t) => {
run('node-pre-gyp', 'testpackage', testInstance.args, app, testInstance.opts, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
});