Skip to content

Commit

Permalink
Adds support for rust gcno profiling files
Browse files Browse the repository at this point in the history
  • Loading branch information
montekki authored and sylvestre committed Nov 11, 2021
1 parent 73f26e3 commit 0192fdd
Showing 1 changed file with 95 additions and 2 deletions.
97 changes: 95 additions & 2 deletions src/compiler/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ pub struct ParsedArguments {
crate_types: CrateTypes,
/// If dependency info is being emitted, the name of the dep info file.
dep_info: Option<PathBuf>,
/// If gcno info is being emitted, the name of the gcno file.
gcno: Option<PathBuf>,
/// rustc says that emits .rlib for --emit=metadata
/// https://github.com/rust-lang/rust/issues/54852
emit: HashSet<String>,
Expand Down Expand Up @@ -835,6 +837,36 @@ impl IntoArg for ArgCodegen {
}
}

#[derive(Clone, Debug, PartialEq)]
struct ArgUnstable {
opt: String,
value: Option<String>,
}
impl FromArg for ArgUnstable {
fn process(arg: OsString) -> ArgParseResult<Self> {
let (opt, value) = split_os_string_arg(arg, "=")?;
Ok(ArgUnstable { opt, value })
}
}
impl IntoArg for ArgUnstable {
fn into_arg_os_string(self) -> OsString {
let ArgUnstable { opt, value } = self;
if let Some(value) = value {
make_os_string!(opt, "=", value)
} else {
make_os_string!(opt)
}
}
fn into_arg_string(self, transformer: PathTransformerFn<'_>) -> ArgToStringResult {
let ArgUnstable { opt, value } = self;
Ok(if let Some(value) = value {
format!("{}={}", opt, value.into_arg_string(transformer)?)
} else {
opt
})
}
}

#[derive(Clone, Debug, PartialEq)]
struct ArgExtern {
name: String,
Expand Down Expand Up @@ -930,6 +962,7 @@ ArgData! {
CodeGen(ArgCodegen),
PassThrough(OsString),
Target(ArgTarget),
Unstable(ArgUnstable),
}

use self::ArgData::*;
Expand Down Expand Up @@ -968,7 +1001,7 @@ counted_array!(static ARGS: [ArgInfo<ArgData>; _] = [
take_arg!("-L", ArgLinkPath, CanBeSeparated, LinkPath),
flag!("-V", NotCompilationFlag),
take_arg!("-W", OsString, CanBeSeparated, PassThrough),
take_arg!("-Z", OsString, CanBeSeparated, PassThrough),
take_arg!("-Z", ArgUnstable, CanBeSeparated, Unstable),
take_arg!("-l", ArgLinkLibrary, CanBeSeparated, LinkLibrary),
take_arg!("-o", PathBuf, CanBeSeparated, TooHardPath),
]);
Expand All @@ -991,6 +1024,7 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
let mut static_link_paths: Vec<PathBuf> = vec![];
let mut color_mode = ColorMode::Auto;
let mut has_json = false;
let mut profile = false;

for arg in ArgsIter::new(arguments.iter().cloned(), &ARGS[..]) {
let arg = try_or_cannot_cache!(arg, "argument parse");
Expand Down Expand Up @@ -1057,6 +1091,12 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
(_, _) => (),
}
}
Some(Unstable(ArgUnstable { opt, value })) => match value.as_deref() {
Some("y") | Some("yes") | Some("on") | None if opt == "profile" => {
profile = true;
}
_ => (),
},
Some(Color(value)) => {
// We'll just assume the last specified value wins.
color_mode = match value.as_ref() {
Expand Down Expand Up @@ -1136,14 +1176,27 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
// Figure out the dep-info filename, if emitting dep-info.
let dep_info = if emit.contains("dep-info") {
let mut dep_info = crate_name.clone();
if let Some(extra_filename) = extra_filename {
if let Some(extra_filename) = extra_filename.clone() {
dep_info.push_str(&extra_filename[..]);
}
dep_info.push_str(".d");
Some(dep_info)
} else {
None
};

// Figure out the gcno filename, if producing gcno files with `-Zprofile`.
let gcno = if profile {
let mut gcno = crate_name.clone();
if let Some(extra_filename) = extra_filename {
gcno.push_str(&extra_filename[..]);
}
gcno.push_str(".gcno");
Some(gcno)
} else {
None
};

// Locate all static libs specified on the commandline.
let staticlibs = static_lib_names
.into_iter()
Expand Down Expand Up @@ -1179,6 +1232,7 @@ fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<Pars
staticlibs,
crate_name,
dep_info: dep_info.map(|s| s.into()),
gcno: gcno.map(|s| s.into()),
emit,
color_mode,
has_json,
Expand Down Expand Up @@ -1219,6 +1273,7 @@ where
dep_info,
emit,
has_json,
gcno,
..
},
} = *self;
Expand Down Expand Up @@ -1421,6 +1476,10 @@ where
} else {
None
};
if let Some(gcno) = gcno {
let p = output_dir.join(&gcno);
outputs.insert(gcno.to_string_lossy().into_owned(), p);
}
let mut arguments = arguments;
// Request color output unless json was requested. The client will strip colors if needed.
if !has_json {
Expand Down Expand Up @@ -2923,6 +2982,7 @@ c:/foo/bar.rs:
emit,
color_mode: ColorMode::Auto,
has_json: false,
gcno: None,
},
});
let creator = new_creator();
Expand Down Expand Up @@ -3241,4 +3301,37 @@ c:/foo/bar.rs:
)
);
}

#[test]
fn test_parse_unstable_profile_flag() {
let h = parses!(
"--crate-name",
"foo",
"--crate-type",
"lib",
"./src/lib.rs",
"--emit=dep-info,link",
"--out-dir",
"/out",
"-Zprofile"
);

assert_eq!(h.gcno, Some("foo.gcno".into()));

let h = parses!(
"--crate-name",
"foo",
"--crate-type",
"lib",
"./src/lib.rs",
"--emit=dep-info,link",
"-C",
"extra-filename=-a1b6419f8321841f",
"--out-dir",
"/out",
"-Zprofile"
);

assert_eq!(h.gcno, Some("foo-a1b6419f8321841f.gcno".into()));
}
}

0 comments on commit 0192fdd

Please sign in to comment.