Skip to content
Merged
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ glob = "0.3"
itertools = "0.14"
implib = "0.4.0"
object = { version = "0.37.1", default-features = false, features = ["std", "read_core", "pe"] }
cargo-platform = "0.3.1"

[features]
default = []
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ rustflags = "-Cpanic=abort"
# Used to disable the generation of additional import library file in platforms
# that have the concept such as Windows
import_library = false

[package.metadata.capi.library.target.'cfg(target_os = "linux")']
# Add target-specific rustflags, the are folded with the main rustflags above
rustflags = "-Clink-arg=-Wl,--version-script=assets/version.map"
```

### Custom data install
Expand Down
33 changes: 28 additions & 5 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

Expand All @@ -14,6 +15,7 @@ use cargo::util::interning::InternedString;
use cargo::{CliResult, GlobalContext};

use anyhow::Context as _;
use cargo_platform::Platform;
use cargo_util::paths::{copy, create_dir_all, open, read, read_bytes, write};
use implib::def::ModuleDef;
use implib::{Flavor, ImportLibrary, MachineType};
Expand Down Expand Up @@ -648,17 +650,38 @@ fn load_manifest_capi_config(
});
}

fn make_args(args: &str) -> impl Iterator<Item = String> + use<'_> {
args.split(' ')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string)
}

import_library = library
.get("import_library")
.and_then(|v| v.as_bool())
.unwrap_or(true);

if let Some(args) = library.get("rustflags").and_then(|v| v.as_str()) {
rustflags.extend(make_args(args));
}

if let Some(args) = library.get("target").and_then(|v| v.as_table()) {
let args = args
.split(' ')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string);
rustflags.extend(args);
.iter()
.filter_map(|(p, v)| {
if Platform::from_str(p)
.ok()
.is_some_and(|p| p.matches(name, &rustc_target.cfg))
{
v.as_str()
} else {
None
}
})
.flat_map(make_args);

rustflags.extend(args)
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ mod test {
arch: String::from(""),
os: os.to_string(),
env: String::from(""),
cfg: Vec::new(),
target: None,
};
let file_names =
FileNames::from_target(&target, "ferris", Path::new("/foo/bar"), false);
Expand All @@ -261,6 +263,8 @@ mod test {
arch: String::from(""),
os: os.to_string(),
env: String::from(""),
cfg: Vec::new(),
target: None,
};
let file_names =
FileNames::from_target(&target, "ferris", Path::new("/foo/bar"), false);
Expand All @@ -284,6 +288,8 @@ mod test {
arch: String::from(""),
os: String::from("windows"),
env: String::from("msvc"),
cfg: Vec::new(),
target: None,
};
let file_names = FileNames::from_target(&target, "ferris", Path::new("/foo/bar"), false);

Expand All @@ -305,6 +311,8 @@ mod test {
arch: String::from(""),
os: String::from("windows"),
env: String::from("gnu"),
cfg: Vec::new(),
target: None,
};
let file_names = FileNames::from_target(&target, "ferris", Path::new("/foo/bar"), false);

Expand Down
32 changes: 28 additions & 4 deletions src/target.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::env::consts;
use std::path::{Path, PathBuf};

use anyhow::*;
use std::str::FromStr;

use crate::build::CApiConfig;
use anyhow::*;
use cargo::core::compiler::CompileTarget;
use cargo_platform::Cfg;

/// Split a target string to its components
///
Expand All @@ -16,15 +18,18 @@ pub struct Target {
// pub vendor: String,
pub os: String,
pub env: String,
pub target: Option<CompileTarget>,
pub cfg: Vec<Cfg>,
}

impl Target {
pub fn new<T: AsRef<std::ffi::OsStr>>(
pub fn new<T: AsRef<std::ffi::OsStr> + AsRef<str>>(
target: Option<T>,
is_target_overridden: bool,
) -> Result<Self, anyhow::Error> {
) -> Result<Self> {
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
let mut cmd = std::process::Command::new(rustc);
let target = target.as_ref();

cmd.arg("--print").arg("cfg");
if let Some(target) = target {
Expand All @@ -46,18 +51,37 @@ impl Target {

let s = std::str::from_utf8(&out.stdout).unwrap();

let lines = s.lines();

let cfg = lines
.map(|line| Ok(Cfg::from_str(line)?))
.collect::<Result<Vec<_>>>()
.with_context(|| {
format!(
"failed to parse the cfg from `rustc --print=cfg`, got:\n{}",
s
)
})?;

Ok(Target {
arch: match_re(arch_re, s),
// vendor: match_re(vendor_re, s),
os: match_re(os_re, s),
env: match_re(env_re, s),
is_target_overridden,
target: target.map(|t| CompileTarget::new(t.as_ref())).transpose()?,
cfg,
})
} else {
Err(anyhow!("Cannot run {:?}", cmd))
}
}

/// Produce the target name, if known
pub fn name(&self) -> Option<&str> {
self.target.as_ref().map(|t| t.short_name())
}

/// Build a list of linker arguments
pub fn shared_object_link_args(
&self,
Expand Down
Loading