Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #42 from neon-bindings/atom-support-npm-arch-flag
Browse files Browse the repository at this point in the history
Windows support
  • Loading branch information
Dave Herman committed Jan 9, 2017
2 parents da56b39 + 9ca4ebb commit df6d5e3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/ops/neon_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ const LIB_SUFFIX = {
'win32': ".dll"
};

function cargo(toolchain, configuration, nodeModuleVersion) {
function explicit_cargo_target() {
if (process.platform === 'win32') {
let arch = process.env.npm_config_arch || process.arch;
if (arch === 'ia32') {
return 'i686-pc-windows-msvc';
} else {
return 'x86_64-pc-windows-msvc';
}
}
}

function cargo(toolchain, configuration, nodeModuleVersion, target) {
let macos = process.platform === 'darwin';

let [command, prefix] = toolchain === 'default'
Expand All @@ -36,20 +47,25 @@ function cargo(toolchain, configuration, nodeModuleVersion) {
configuration === 'release' ? ["--release"] : [],
macos ? ["--", "-C", "link-args=-Wl,-undefined,dynamic_lookup"] : []);

// Save the current Node ABI version as an environment variable.
// Pass the Node modules ABI version to the build as an environment variable.
let env = clone(process.env);
if (nodeModuleVersion) {
env.NEON_NODE_ABI = nodeModuleVersion;
env.NEON_NODE_ABI = nodeModuleVersion || process.versions.modules;

if (target) {
args.push("--target=" + target);
}

console.log(style.info([command].concat(args).join(" ")));

return spawn(command, args, { cwd: 'native', stdio: 'inherit', env: env });
}

async function main(name, configuration) {
async function main(name, configuration, target) {
let pp = process.platform;
let dylib = path.resolve('native', 'target', configuration, LIB_PREFIX[pp] + name + LIB_SUFFIX[pp]);
let output_directory = target ?
path.resolve('native', 'target', target, configuration) :
path.resolve('native', 'target', configuration);
let dylib = path.resolve(output_directory, LIB_PREFIX[pp] + name + LIB_SUFFIX[pp]);
let index = path.resolve('native', 'index.node');

console.log(style.info("generating native" + path.sep + "index.node"));
Expand All @@ -66,13 +82,15 @@ export default async function neon_build(pwd, toolchain, configuration, nodeModu
throw new Error("Cargo.toml does not contain a [lib] section with a 'name' field");
}

let target = explicit_cargo_target();

console.log(style.info("running cargo"));

// 2. Build the binary.
if ((await cargo(toolchain, configuration, nodeModuleVersion)) !== 0) {
if ((await cargo(toolchain, configuration, nodeModuleVersion, target)) !== 0) {
throw new Error("cargo build failed");
}

// 3. Copy the dylib into the main index.node file.
await main(metadata.lib.name, configuration);
await main(metadata.lib.name, configuration, target);
}
2 changes: 2 additions & 0 deletions src/ops/neon_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const NPM_TEMPLATE = compile('package.json.hbs');
const INDEXJS_TEMPLATE = compile('index.js.hbs');
const LIBRS_TEMPLATE = compile('lib.rs.hbs');
const README_TEMPLATE = compile('README.md.hbs');
const BUILDRS_TEMPLATE = compile('build.rs.hbs');

async function guessAuthor() {
let author = {
Expand Down Expand Up @@ -139,6 +140,7 @@ export default async function wizard(pwd, name, toolchain) {
await writeFile(path.resolve(root, 'README.md'), (await README_TEMPLATE)(ctx), { flag: 'wx' });
await writeFile(path.resolve(root, answers.node), (await INDEXJS_TEMPLATE)(ctx), { flag: 'wx' });
await writeFile(path.resolve(src, 'lib.rs'), (await LIBRS_TEMPLATE)(ctx), { flag: 'wx' });
await writeFile(path.resolve(native_, 'build.rs'), (await BUILDRS_TEMPLATE)(ctx), { flag: 'wx' });

let relativeRoot = path.relative(pwd, root);
let relativeNode = path.relative(pwd, path.resolve(root, answers.node));
Expand Down
12 changes: 12 additions & 0 deletions templates/build.rs.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::env;

fn main() {
if cfg!(windows) {
let debug = env::var("DEBUG").ok().map_or(false, |s| s == "true");
let configuration = if debug { "Debug" } else { "Release" };
let node_root_dir = env::var("DEP_NEON_SYS_NODE_ROOT_DIR").unwrap();
let node_lib_file = env::var("DEP_NEON_SYS_NODE_LIB_FILE").unwrap();
println!("cargo:rustc-link-search={}\\{}", node_root_dir, configuration);
println!("cargo:rustc-link-lib={}", node_lib_file);
}
}

0 comments on commit df6d5e3

Please sign in to comment.