Skip to content

Commit

Permalink
Force strip = false and cleanup env handling
Browse files Browse the repository at this point in the history
Closes #87
  • Loading branch information
intelfx authored May 9, 2024
1 parent fa666f6 commit 122e1cd
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::HashMap;
use std::convert::TryInto;
use std::process::{self, Command};
use std::{fmt, fs, path, str};
use std::ffi::OsStr;

use multimap::MultiMap;

Expand Down Expand Up @@ -386,6 +387,18 @@ fn parse_args(raw_args: Vec<std::ffi::OsString>) -> Result<Args, pico_args::Erro
Ok(args)
}

impl Args {
fn get_profile(&self) -> &str {
if let Some(profile) = &self.profile {
profile
} else if self.release {
"release"
} else {
"dev"
}
}
}

fn wrapper_mode(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let start = std::time::Instant::now();

Expand Down Expand Up @@ -544,14 +557,8 @@ fn process_crate(args: &Args) -> Result<CrateData, Error> {
// Run `cargo build` without json output first, so we could print build errors.
{
let cmd = &mut Command::new("cargo");
cmd.args(&get_cargo_args(args, false));

// When targeting MSVC, symbols data will be stored in PDB files.
// But unlike other targets, the Release build would not have any useful information.
// Therefore we have to force debug info in Release mode for MSVC target.
if args.release && target_triple.contains("msvc") {
cmd.env("CARGO_PROFILE_RELEASE_DEBUG", "true");
}
cmd.args(get_cargo_args(args, false));
cmd.envs(get_cargo_envs(args, &target_triple));

cmd.spawn()
.map_err(|_| Error::CargoBuildFailed)?
Expand All @@ -562,17 +569,11 @@ fn process_crate(args: &Args) -> Result<CrateData, Error> {
// Run `cargo build` with json output and collect it.
// This would not cause a rebuild.
let cmd = &mut Command::new("cargo");
cmd.args(&get_cargo_args(args, true));
cmd.args(get_cargo_args(args, true));
cmd.envs(get_cargo_envs(args, &target_triple));
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::null());

// When targeting MSVC, symbols data will be stored in PDB files.
// But unlike other targets, the Release build would not have any useful information.
// Therefore we have to force debug info in Release mode for MSVC target.
if args.release && target_triple.contains("msvc") {
cmd.env("CARGO_PROFILE_RELEASE_DEBUG", "true");
}

let child = cmd.spawn().map_err(|_| Error::CargoBuildFailed)?;

let output = child
Expand Down Expand Up @@ -680,6 +681,30 @@ fn process_crate(args: &Args) -> Result<CrateData, Error> {
Err(Error::UnsupportedCrateType)
}

fn get_cargo_envs(
args: &Args,
target_triple: &str
) -> Vec<(impl AsRef<OsStr>, impl AsRef<OsStr>)> {
let mut list = Vec::new();

let profile = args.get_profile()
.to_ascii_uppercase()
.replace('-', "_");

// No matter which profile we are building for, never strip the binary
// because we need the symbols.
list.push((format!("CARGO_PROFILE_{}_STRIP", profile), "false"));

// When targeting MSVC, symbols data will be stored in PDB files.
// Because of that, the Release build would not have any useful information
// even if not stripped. Therefore, force the debug info for MSVC target.
if target_triple.contains("msvc") {
list.push((format!("CARGO_PROFILE_{}_DEBUG", profile), "true"));
}

list
}

#[allow(clippy::vec_init_then_push)]
fn get_cargo_args(args: &Args, json_output: bool) -> Vec<String> {
let mut list = Vec::new();
Expand Down

0 comments on commit 122e1cd

Please sign in to comment.