-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Port argument-non-c-like-enum to Rust #123149
Merged
bors
merged 3 commits into
rust-lang:master
from
jieyouxu:rmake-arguments-non-c-like-enum
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
use std::env; | ||
use std::path::Path; | ||
use std::process::{Command, Output}; | ||
|
||
use crate::{bin_name, cygpath_windows, handle_failed_output, is_msvc, is_windows, tmp_dir, uname}; | ||
|
||
/// Construct a new platform-specific C compiler invocation. | ||
/// | ||
/// WARNING: This means that what flags are accepted by the underlying C compiler is | ||
/// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`. | ||
pub fn cc() -> Cc { | ||
Cc::new() | ||
} | ||
|
||
/// A platform-specific C compiler invocation builder. The specific C compiler used is | ||
/// passed down from compiletest. | ||
#[derive(Debug)] | ||
pub struct Cc { | ||
cmd: Command, | ||
} | ||
|
||
impl Cc { | ||
/// Construct a new platform-specific C compiler invocation. | ||
/// | ||
/// WARNING: This means that what flags are accepted by the underlying C compile is | ||
/// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`. | ||
pub fn new() -> Self { | ||
let compiler = env::var("CC").unwrap(); | ||
|
||
let mut cmd = Command::new(compiler); | ||
|
||
let default_cflags = env::var("CC_DEFAULT_FLAGS").unwrap(); | ||
for flag in default_cflags.split(char::is_whitespace) { | ||
cmd.arg(flag); | ||
} | ||
|
||
Self { cmd } | ||
} | ||
|
||
/// Specify path of the input file. | ||
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { | ||
self.cmd.arg(path.as_ref()); | ||
self | ||
} | ||
|
||
/// Add a *platform-and-compiler-specific* argument. Please consult the docs for the various | ||
/// possible C compilers on the various platforms to check which arguments are legal for | ||
/// which compiler. | ||
pub fn arg(&mut self, flag: &str) -> &mut Self { | ||
self.cmd.arg(flag); | ||
self | ||
} | ||
|
||
/// Add multiple *platform-and-compiler-specific* arguments. Please consult the docs for the | ||
/// various possible C compilers on the various platforms to check which arguments are legal | ||
/// for which compiler. | ||
pub fn args(&mut self, args: &[&str]) -> &mut Self { | ||
self.cmd.args(args); | ||
self | ||
} | ||
|
||
/// Specify `-o` or `-Fe`/`-Fo` depending on platform/compiler. This assumes that the executable | ||
/// is under `$TMPDIR`. | ||
pub fn out_exe(&mut self, name: &str) -> &mut Self { | ||
// Ref: tools.mk (irrelevant lines omitted): | ||
// | ||
// ```makefile | ||
// ifdef IS_MSVC | ||
// OUT_EXE=-Fe:`cygpath -w $(TMPDIR)/$(call BIN,$(1))` \ | ||
// -Fo:`cygpath -w $(TMPDIR)/$(1).obj` | ||
// else | ||
// OUT_EXE=-o $(TMPDIR)/$(1) | ||
// endif | ||
// ``` | ||
|
||
if is_msvc() { | ||
let fe_path = cygpath_windows(tmp_dir().join(bin_name(name))); | ||
let fo_path = cygpath_windows(tmp_dir().join(format!("{name}.obj"))); | ||
self.cmd.arg(format!("-Fe:{fe_path}")); | ||
self.cmd.arg(format!("-Fo:{fo_path}")); | ||
} else { | ||
self.cmd.arg("-o"); | ||
self.cmd.arg(tmp_dir().join(name)); | ||
} | ||
|
||
self | ||
} | ||
|
||
/// Run the constructed C invocation command and assert that it is successfully run. | ||
#[track_caller] | ||
pub fn run(&mut self) -> Output { | ||
let caller_location = std::panic::Location::caller(); | ||
let caller_line_number = caller_location.line(); | ||
|
||
let output = self.cmd.output().unwrap(); | ||
if !output.status.success() { | ||
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number); | ||
} | ||
output | ||
} | ||
|
||
/// Inspect what the underlying [`Command`] is up to the current construction. | ||
pub fn inspect(&mut self, f: impl FnOnce(&Command)) -> &mut Self { | ||
f(&self.cmd); | ||
self | ||
} | ||
} | ||
|
||
/// `EXTRACFLAGS` | ||
pub fn extra_c_flags() -> Vec<&'static str> { | ||
// Adapted from tools.mk (trimmed): | ||
// | ||
// ```makefile | ||
// ifdef IS_WINDOWS | ||
// ifdef IS_MSVC | ||
// EXTRACFLAGS := ws2_32.lib userenv.lib advapi32.lib bcrypt.lib ntdll.lib synchronization.lib | ||
// else | ||
// EXTRACFLAGS := -lws2_32 -luserenv -lbcrypt -lntdll -lsynchronization | ||
// endif | ||
// else | ||
// ifeq ($(UNAME),Darwin) | ||
// EXTRACFLAGS := -lresolv | ||
// else | ||
// ifeq ($(UNAME),FreeBSD) | ||
// EXTRACFLAGS := -lm -lpthread -lgcc_s | ||
// else | ||
// ifeq ($(UNAME),SunOS) | ||
// EXTRACFLAGS := -lm -lpthread -lposix4 -lsocket -lresolv | ||
// else | ||
// ifeq ($(UNAME),OpenBSD) | ||
// EXTRACFLAGS := -lm -lpthread -lc++abi | ||
// else | ||
// EXTRACFLAGS := -lm -lrt -ldl -lpthread | ||
// endif | ||
// endif | ||
// endif | ||
// endif | ||
// endif | ||
// ``` | ||
|
||
if is_windows() { | ||
if is_msvc() { | ||
vec![ | ||
"ws2_32.lib", | ||
"userenv.lib", | ||
"advapi32.lib", | ||
"bcrypt.lib", | ||
"ntdll.lib", | ||
"synchronization.lib", | ||
] | ||
} else { | ||
vec!["-lws2_32", "-luserenv", "-lbcrypt", "-lntdll", "-lsynchronization"] | ||
} | ||
} else { | ||
match uname() { | ||
n if n.contains("Darwin") => vec!["-lresolv"], | ||
n if n.contains("FreeBSD") => vec!["-lm", "-lpthread", "-lgcc_s"], | ||
n if n.contains("SunOS") => { | ||
vec!["-lm", "-lpthread", "-lposix4", "-lsocket", "-lresolv"] | ||
} | ||
n if n.contains("OpenBSD") => vec!["-lm", "-lpthread", "-lc++abi"], | ||
_ => vec!["-lm", "-lrt", "-ldl", "-lpthread"], | ||
} | ||
} | ||
} | ||
|
||
/// `EXTRACXXFLAGS` | ||
pub fn extra_cxx_flags() -> Vec<&'static str> { | ||
// Adapted from tools.mk (trimmed): | ||
// | ||
// ```makefile | ||
// ifdef IS_WINDOWS | ||
// ifdef IS_MSVC | ||
// else | ||
// EXTRACXXFLAGS := -lstdc++ | ||
// endif | ||
// else | ||
// ifeq ($(UNAME),Darwin) | ||
// EXTRACXXFLAGS := -lc++ | ||
// else | ||
// ifeq ($(UNAME),FreeBSD) | ||
// else | ||
// ifeq ($(UNAME),SunOS) | ||
// else | ||
// ifeq ($(UNAME),OpenBSD) | ||
// else | ||
// EXTRACXXFLAGS := -lstdc++ | ||
// endif | ||
// endif | ||
// endif | ||
// endif | ||
// endif | ||
// ``` | ||
if is_windows() { | ||
if is_msvc() { vec![] } else { vec!["-lstdc++"] } | ||
} else { | ||
match uname() { | ||
n if n.contains("Darwin") => vec!["-lc++"], | ||
_ => vec!["-lstdc++"], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This drops the
vec![]
for FreeBSD, SunOS, OpenBSD that the original hadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original tools.mk had
It should've been empty
vec![]
forFreeBSD
,SunOS
andOpenBSD
is what you mean right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch, I'll send a patch for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly that, you got it