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

[Merged by Bors] - Initial work for cross compiling targets #182

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
### Improvements
- Add cross compilation support to nj-cli. ([#182](https://github.com/infinyon/node-bindgen/pull/182)).

## [5.0.0] - 2021-07-15
### Improvements
Expand Down
4 changes: 2 additions & 2 deletions examples/buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test(b: i32) -> Result<ArrayBuffer, NjError> {
};

let json_string = serde_json::to_vec(&my_struct)
.map_err(|err| NjError::Other(format!("serialization error: {}", err.to_string())))?;
.map_err(|err| NjError::Other(format!("serialization error: {}", err)))?;

Ok(ArrayBuffer::new(json_string))
}
Expand Down Expand Up @@ -56,7 +56,7 @@ fn test2(b: i32) -> Result<Record, NjError> {
};

let json_string = serde_json::to_vec(&my_struct)
.map_err(|err| NjError::Other(format!("serialization error: {}", err.to_string())))?;
.map_err(|err| NjError::Other(format!("serialization error: {}", err)))?;

Ok(Record {
buffer: ArrayBuffer::new(json_string),
Expand Down
2 changes: 1 addition & 1 deletion nj-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nj-cli"
version = "0.4.1"
version = "0.4.2"
authors = ["fluvio.io"]
edition = "2018"
description = "build tool for node-bindgen"
Expand Down
40 changes: 32 additions & 8 deletions nj-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct BuildOpt {
#[structopt(long)]
release: bool,

#[structopt(long)]
target: Option<String>,

extras: Vec<String>,
}

Expand Down Expand Up @@ -98,6 +101,9 @@ fn build(opt: BuildOpt) {
if opt.release {
args.push("--release".to_string());
}
if let Some(ref target) = opt.target {
args.push(format!("--target={target}"));
}
args.extend(opt.extras);

let mut build_command = Command::new("cargo")
Expand Down Expand Up @@ -129,18 +135,24 @@ fn build(opt: BuildOpt) {
}
Some(_) => {}
}
let target = if opt.release { "release" } else { "debug" };

copy_lib(opt.output, target);
let target_mode = if opt.release { "release" } else { "debug" };

copy_lib(opt.output, target_mode, opt.target);
}

/// copy library to target directory
fn copy_lib(out: String, target_type: &str) {
fn copy_lib(out: String, target_mode: &str, target_tripple: Option<String>) {
let manifest_path = manifest_path();
let metadata = load_metadata(&manifest_path);
if let Some(package) = find_current_package(&metadata, &manifest_path) {
if let Some(target) = find_cdylib(package) {
let lib_path = lib_path(&metadata.target_directory, target_type, &target.name);
let lib_path = lib_path(
&metadata.target_directory,
target_mode,
&target.name,
target_tripple,
);
let error_msg = format!("copy failed of {:?}", lib_path);
copy_cdylib(&lib_path, &out).expect(&error_msg);
} else {
Expand Down Expand Up @@ -184,7 +196,12 @@ fn manifest_path() -> PathBuf {
current_path.join("Cargo.toml")
}

fn lib_path(target: &Path, build_type: &str, target_name: &str) -> PathBuf {
fn lib_path(
target: &Path,
build_type: &str,
target_name: &str,
target_tripple: Option<String>,
) -> PathBuf {
let file_name = if cfg!(target_os = "windows") {
format!("{}.dll", target_name)
} else if cfg!(target_os = "macos") {
Expand All @@ -194,9 +211,16 @@ fn lib_path(target: &Path, build_type: &str, target_name: &str) -> PathBuf {
} else {
panic!("Unsupported operating system.");
}
.replace("-", "_");

target.join(target).join(build_type).join(file_name)
.replace('-', "_");
if let Some(target_tripple) = target_tripple {
target
.join(target)
.join(target_tripple)
.join(build_type)
.join(file_name)
} else {
target.join(target).join(build_type).join(file_name)
}
}

// where we are outputting
Expand Down
2 changes: 1 addition & 1 deletion nj-core/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::ptr;

use crate::sys::napi_value;
use crate::val::JsEnv;
use crate::val::JsObject;
use crate::NjError;
use crate::napi_call_result;

Expand Down Expand Up @@ -197,6 +196,7 @@ where
#[cfg(feature = "serde_json")]
impl TryIntoJs for serde_json::map::Map<String, serde_json::Value> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
use crate::val::JsObject;
let mut obj = JsObject::new(*js_env, js_env.create_object()?);

let converted_obj = self
Expand Down
1 change: 0 additions & 1 deletion nj-core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ pub trait JSWorker: Sized + Send + 'static {
}

/// call by Node to create promise
#[no_mangle]
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 think rust-lang/rust#28179 is what's going on. cargo check says that this will throw a hard error in the future but it's just a warning for now so I removed it.

extern "C" fn start_promise(env: napi_env, info: napi_callback_info) -> napi_value {
let js_env = JsEnv::new(env);

Expand Down
2 changes: 1 addition & 1 deletion nj-derive/src/ast/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::MyTupleType;
pub struct FunctionArgs<'a> {
pub args: Vec<FunctionArg<'a>>,
pub is_method: bool,
receiver: Option<&'a Receiver>,
_receiver: Option<&'a Receiver>,
}

impl<'a> FunctionArgs<'a> {
Expand Down
2 changes: 1 addition & 1 deletion nj-derive/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl<'a> MyEnum<'a> {
let variants = enum_data
.variants
.iter()
.map(|v| MyVariant::from_ast(v))
.map(MyVariant::from_ast)
.collect::<Result<Vec<MyVariant>>>()?;

Ok(MyEnum { variants })
Expand Down
4 changes: 2 additions & 2 deletions nj-derive/src/generator/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ fn generate_variant_conversion(

fn drop_generic_bounds(params: &[GenericParam]) -> Vec<GenericParam> {
params
.to_owned()
.into_iter()
.iter()
.cloned()
.map(|generic| match generic {
GenericParam::Type(type_param) => GenericParam::Type(TypeParam {
colon_token: None,
Expand Down