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

Force strip = false and cleanup env handling #106

Merged
merged 3 commits into from
May 9, 2024
Merged
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
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
Loading