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

Get neon building on windows #122

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ homepage = "http://neon.rustbridge.io"
repository = "https://github.com/rustbridge/neon"
license = "MIT/Apache-2.0"
exclude = ["neon.jpg"]
build = "build.rs"

# This name is arbitrary, but allows us to re-export the location of node.lib
# to dependent packages so they can link on Windows.
links = "neon-sys"

[dependencies]
cslice = { version = "=0.1.1", path = "crates/cslice" }
Expand Down
35 changes: 35 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
os: Visual Studio 2015

platform:
- x64
- x86

environment:
NODEJS_VERSION: "6"
matrix:
- NODE_ARCHITECTURE: x64
RUST_TOOLCHAIN: stable-x86_64-pc-windows-msvc

- NODE_ARCHITECTURE: x86
RUST_TOOLCHAIN: stable-i686-pc-windows-msvc

install:
- ps: Install-Product node $env:NODEJS_VERSION $env:NODE_ARCHITECTURE
- node -e "console.log(process.arch, process.versions)"

- curl -sSf -o rustup-init.exe https://win.rustup.rs
- rustup-init.exe -y --default-toolchain %RUST_TOOLCHAIN%
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
- rustup show
- rustc -V
- cargo -V

build: false

test_script:
- cd tests
- npm test

cache:
- target
- C:\Users\appveyor\.cargo\registry
8 changes: 8 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::env;

fn main() {
if cfg!(windows) {
println!("cargo:node_root_dir={}", env::var("DEP_NEON_NODE_ROOT_DIR").unwrap());
println!("cargo:node_lib_file={}", env::var("DEP_NEON_NODE_LIB_FILE").unwrap());
}
}
22 changes: 17 additions & 5 deletions crates/neon-sys/binding.gyp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
{
"targets": [{
"target_name": "neon",
"sources": [ "src/neon.cc" ],
"include_dirs": [ "<!(node -e \"require('nan')\")" ]
}]
"targets": [{
"target_name": "neon",
"sources": [ "src/neon.cc" ],
"include_dirs": [ "<!(node -e \"require('nan')\")" ],
'configurations': {
'Release': {
'msvs_settings': {
'VCCLCompilerTool': {
'WholeProgramOptimization': 'false'
},
'VCLinkerTool': {
'LinkTimeCodeGeneration': 0
}
}
}
}
}]
}
103 changes: 74 additions & 29 deletions crates/neon-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,91 @@
extern crate gcc;

use std::process::Command;
use std::process::{Command, Stdio};
use std::env;

fn debug() -> bool {
match env::var("DEBUG") {
Ok(s) => s == "true",
Err(_) => false
}
}

fn mode() -> &'static str {
if debug() { "Debug" } else { "Release" }
}
fn main() {
// 1. Build the object file from source using node-gyp.
build_object_file();

fn object_path(libname: &str) -> String {
format!("build/{}/obj.target/{}/src/{}.o", mode(), libname, libname)
// 2. Link the library from the object file using gcc.
link_library();
}

fn build_object_file() {
let npm_command = if cfg!(unix) { "npm" } else { "npm.cmd" };
let node_gyp_command = if cfg!(unix) { "node-gyp" } else { "node-gyp.cmd" };

if cfg!(windows) {
// Downcase all the npm environment variables to ensure they are read by node-gyp.
for (key, value) in env::vars() {
if key.starts_with("NPM_CONFIG") {
env::remove_var(&key);
env::set_var(key.to_lowercase(), value);
}
}
}

// Ensure that all package.json dependencies and dev dependencies are installed.
Command::new("npm").arg("install").status().ok()
.expect(r#"failed to run "npm install" for neon-sys"#);
Command::new(npm_command).args(&["install", "--silent"]).status().ok().expect("Failed to run \"npm install\" for neon-sys!");

// Run `node-gyp configure` in verbose mode to read node_root_dir on Windows.
let mut configure_args = vec!["configure", "--verbose"];
if debug() {
configure_args.push("--debug");
}

let output = Command::new(node_gyp_command)
.args(&configure_args)
.output()
.expect("Failed to run \"node-gyp configure\" for neon-sys!");

// Run the package.json `configure` script, which invokes `node-gyp configure` from the local node_modules.
Command::new("npm").arg("run").arg(if debug() { "configure-debug" } else { "configure-release" }).status().ok().unwrap();
if cfg!(windows) {
let node_gyp_output = String::from_utf8_lossy(&output.stderr);
let node_root_dir_flag_pattern = "'-Dnode_root_dir=";
let node_root_dir_start_index = node_gyp_output
.find(node_root_dir_flag_pattern)
.map(|i| i + node_root_dir_flag_pattern.len())
.expect("Couldn't find node_root_dir in node-gyp output.");
let node_root_dir_end_index = node_gyp_output[node_root_dir_start_index..].find("'").unwrap() + node_root_dir_start_index;
println!("cargo:node_root_dir={}", &node_gyp_output[node_root_dir_start_index..node_root_dir_end_index]);
let node_lib_file_flag_pattern = "'-Dnode_lib_file=";
let node_lib_file_start_index = node_gyp_output
.find(node_lib_file_flag_pattern)
.map(|i| i + node_lib_file_flag_pattern.len())
.expect("Couldn't find node_lib_file in node-gyp output.");
let node_lib_file_end_index = node_gyp_output[node_lib_file_start_index..].find(".lib").unwrap() + node_lib_file_start_index;
println!("cargo:node_lib_file={}", &node_gyp_output[node_lib_file_start_index..node_lib_file_end_index]);
}

// Run `node-gyp build` (appending -d in debug mode).
let mut build_args = vec!["build"];
if debug() {
build_args.push("--debug");
}

// Run the package.json `build` script, which invokes `node-gyp build` from the local node_modules.
Command::new("npm").arg("run").arg(if debug() { "build-debug" } else { "build-release" }).status().ok().unwrap();
Command::new(node_gyp_command)
.stderr(Stdio::null()) // Prevent cargo build from hanging on Windows.
.args(&build_args)
.status()
.ok()
.expect("Failed to run \"node-gyp build\" for neon-sys!");
}

// Link the built object file into a static library.
fn link_library() {
// Link the built object file into a static library.
gcc::Config::new()
.object(object_path("neon"))
.compile("libneon.a");
}
let configuration = if debug() { "Debug" } else { "Release" };
let object_path = if cfg!(unix) {
format!("build/{}/obj.target/neon/src/neon.o", configuration)
} else {
format!("build\\{}\\obj\\neon\\neon.obj", configuration)
};

fn main() {
// 1. Build the object file from source using node-gyp.
build_object_file();
gcc::Config::new().object(object_path).compile("libneon.a");
}

// 2. Link the library from the object file using gcc.
link_library();
fn debug() -> bool {
match env::var("DEBUG") {
Ok(s) => s == "true",
Err(_) => false
}
}
8 changes: 2 additions & 6 deletions crates/neon-sys/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"main": "index.js",
"scripts": {
"configure-release": "node-gyp configure",
"build-release": "node-gyp build",
"configure-debug": "node-gyp configure -d",
"build-debug": "node-gyp build -d"
"preinstall": "echo 'Skipping node-gyp installation as part of npm install.'"
},
"devDependencies": {
"nan": "^2.3.2",
"node-gyp": "^3.3.1"
"nan": "^2.3.2"
},
"dependencies": {
"bindings": "1.2.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::{Local, Isolate};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Array_New"]
pub fn new(out: &mut Local, isolate: *mut Isolate, length: u32);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cslice::CMutSlice;

// Suppress a spurious rustc warning about the use of CMutSlice.
#[allow(improper_ctypes)]
extern "system" {
extern "C" {

#[link_name = "NeonSys_Buffer_New"]
pub fn new(out: &mut Local, size: u32) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::{FunctionCallbackInfo, Isolate, Local};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Call_SetReturn"]
pub fn set_return(info: &FunctionCallbackInfo, value: Local);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/class.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::raw::c_void;
use raw::{Isolate, Local};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Class_GetClassMap"]
pub fn get_class_map(isolate: *mut Isolate) -> *mut c_void;
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::Local;

extern "system" {
extern "C" {

#[link_name = "NeonSys_Convert_ToObject"]
pub fn to_object(out: &mut Local, value: &Local) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::Local;

extern "system" {
extern "C" {

#[link_name = "NeonSys_Error_Throw"]
pub fn throw(val: Local);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/fun.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::raw::c_void;
use raw::{FunctionCallbackInfo, Local};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Fun_New"]
pub fn new(out: &mut Local, isolate: *mut c_void, callback: *mut c_void, kernel: *mut c_void) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/mem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::Local;

extern "system" {
extern "C" {

#[link_name = "NeonSys_Mem_SameHandle"]
pub fn same_handle(h1: Local, h2: Local) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::raw::c_void;
use raw::Local;

extern "system" {
extern "C" {

#[link_name = "NeonSys_Module_ExecKernel"]
pub fn exec_kernel(kernel: *mut c_void, callback: extern fn(*mut c_void, *mut c_void, *mut c_void), exports: Local, scope: *mut c_void);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/neon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ extern "C" void NeonSys_Scope_Enter(v8::HandleScope *scope, v8::Isolate *isolate
}

extern "C" void NeonSys_Scope_Exit(v8::HandleScope *scope) {
scope->~HandleScope();
scope->HandleScope::~HandleScope();
}

extern "C" size_t NeonSys_Scope_Sizeof() {
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::{Isolate, Local};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Object_New"]
pub fn new(out: &mut Local);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::{Local, Isolate};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Primitive_Undefined"]
pub fn undefined(out: &mut Local);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::raw::c_void;
use raw::{HandleScope, EscapableHandleScope, Local};

extern "system" {
extern "C" {

#[link_name = "NeonSys_Scope_Escape"]
pub fn escape(out: &mut Local, scope: *mut EscapableHandleScope, value: Local);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raw::{Local, Isolate};

extern "system" {
extern "C" {

#[link_name = "NeonSys_String_New"]
pub fn new(out: &mut Local, isolate: *mut Isolate, data: *const u8, len: i32) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Tag {
Other
}

extern "system" {
extern "C" {

#[link_name = "NeonSys_Tag_Of"]
pub fn of(val: Local) -> Tag;
Expand Down
1 change: 1 addition & 0 deletions tests/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "tests"
version = "0.1.0"
authors = ["The Neon Community"]
license = "MIT/Apache-2.0"
build = "build.rs"

[lib]
name = "tests"
Expand Down
12 changes: 12 additions & 0 deletions tests/native/build.rs
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);
}
}