Skip to content

Commit

Permalink
When forcing debug = true for MSVC, do not hardcode "release"
Browse files Browse the repository at this point in the history
  • Loading branch information
intelfx committed May 7, 2024
1 parent ddf5373 commit b8973c3
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 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 @@ -545,13 +558,7 @@ fn process_crate(args: &Args) -> Result<CrateData, Error> {
{
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.envs(get_cargo_envs(args, &target_triple));

cmd.spawn()
.map_err(|_| Error::CargoBuildFailed)?
Expand All @@ -563,16 +570,10 @@ fn process_crate(args: &Args) -> Result<CrateData, Error> {
// This would not cause a rebuild.
let cmd = &mut Command::new("cargo");
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,23 @@ 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('-', "_");

// When targeting MSVC, symbols data will be stored in PDB files,
// so always generate debug info
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 b8973c3

Please sign in to comment.