Skip to content

Commit

Permalink
cargo: Use CARGO_ENCODED_RUSTFLAGS to support spaces in environment…
Browse files Browse the repository at this point in the history
… paths
  • Loading branch information
MarijnS95 committed Aug 14, 2024
1 parent ffe3e34 commit 8b94872
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions xbuild/src/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use self::config::LocalizedConfig;
use self::manifest::Manifest;
use crate::{CompileTarget, Opt};

const SEP: &str = "\x1f";

pub struct Cargo {
package: String,
features: Vec<String>,
Expand Down Expand Up @@ -236,7 +238,7 @@ pub struct CargoBuild {
triple: Option<&'static str>,
c_flags: String,
cxx_flags: String,
rust_flags: String,
rust_flags: Vec<String>,
}

impl CargoBuild {
Expand Down Expand Up @@ -401,31 +403,29 @@ impl CargoBuild {
}

pub fn add_lib_dir(&mut self, path: &Path) {
self.rust_flags
.push_str(&format!("-Lnative={} ", path.display()));
self.rust_flags.push(format!("-Lnative={}", path.display()));
}

pub fn add_framework_dir(&mut self, path: &Path) {
self.rust_flags
.push_str(&format!("-Lframework={} ", path.display()));
.push(format!("-Lframework={}", path.display()));
}

pub fn link_lib(&mut self, name: &str) {
self.rust_flags.push_str(&format!("-l{} ", name));
self.rust_flags.push(format!("-l{}", name));
}

pub fn link_framework(&mut self, name: &str) {
self.rust_flags.push_str(&format!("-lframework={} ", name));
self.rust_flags.push(format!("-lframework={}", name));
}

pub fn add_target_feature(&mut self, target_feature: &str) {
self.rust_flags
.push_str(&format!("-Ctarget-feature={} ", target_feature));
.push(format!("-Ctarget-feature={}", target_feature));
}

pub fn add_link_arg(&mut self, link_arg: &str) {
self.rust_flags
.push_str(&format!("-Clink-arg={} ", link_arg));
self.rust_flags.push(format!("-Clink-arg={}", link_arg));
}

pub fn add_define(&mut self, name: &str, value: &str) {
Expand Down Expand Up @@ -465,7 +465,10 @@ impl CargoBuild {
}

pub fn exec(mut self) -> Result<()> {
self.cargo_target_env("RUSTFLAGS", &self.rust_flags.clone());
// TODO: There's no target-triple specific ENCODED_RUSTFLAGS: https://github.com/rust-lang/cargo/issues/14398
// self.cargo_target_env("ENCODED_RUSTFLAGS", &self.rust_flags.join(SEP));
self.cmd
.env("CARGO_ENCODED_RUSTFLAGS", self.rust_flags.join(SEP));
self.cc_triple_env("CFLAGS", &self.c_flags.clone());
// These strings already end with a space if they're non-empty:
self.cc_triple_env("CXXFLAGS", &format!("{}{}", self.c_flags, self.cxx_flags));
Expand Down

0 comments on commit 8b94872

Please sign in to comment.