-
Notifications
You must be signed in to change notification settings - Fork 1.5k
time: use C altzone when available #8357
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
Open
lms0806
wants to merge
2
commits into
RustPython:main
Choose a base branch
from
lms0806:time-use-c-altzone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,72 @@ | ||
| //! Like CPython's `HAVE_ALTZONE`, it detects the presence of `altzone` in `time.h` at build time. | ||
| #![allow( | ||
| clippy::disallowed_methods, | ||
| reason = "build scripts cannot use rustpython-host_env" | ||
| )] | ||
|
|
||
| use std::{env, fs, path::PathBuf}; | ||
|
|
||
| fn main() { | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
| println!("cargo:rustc-check-cfg=cfg(has_altzone)"); | ||
|
|
||
| let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); | ||
| let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); | ||
|
|
||
| if target_env == "msvc" || target_arch == "wasm32" { | ||
| return; | ||
| } | ||
|
|
||
| let host = env::var("HOST").unwrap_or_default(); | ||
| let target = env::var("TARGET").unwrap_or_default(); | ||
| // cc::Build resolves CC_<target>, CC_<target_with_underscores>, TARGET_CC, then CC. | ||
| if host != target && !has_target_c_compiler(&target) { | ||
| return; | ||
| } | ||
|
|
||
| if probe_altzone() { | ||
| println!("cargo:rustc-cfg=has_altzone"); | ||
| } | ||
| } | ||
|
|
||
| /// Whether any compiler env var that `cc::Build` would consult for the target is set. | ||
| fn has_target_c_compiler(target: &str) -> bool { | ||
| let underscored = target.replace(['-', '.'], "_"); | ||
| env::var_os(format!("CC_{target}")).is_some() | ||
| || env::var_os(format!("CC_{underscored}")).is_some() | ||
| || env::var_os("TARGET_CC").is_some() | ||
| || env::var_os("CC").is_some() | ||
| } | ||
|
|
||
| /// Check corresponding to `AC_TRY_COMPILE(... altzone ...)` in CPython's `configure`. | ||
| fn probe_altzone() -> bool { | ||
| let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR")); | ||
| let src = out_dir.join("probe_altzone.c"); | ||
| let obj = out_dir.join("probe_altzone.o"); | ||
|
|
||
| if fs::write( | ||
| &src, | ||
| "#include <time.h>\nint main(void) { return (int)altzone; }\n", | ||
| ) | ||
| .is_err() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| let Ok(compiler) = cc::Build::new().try_get_compiler() else { | ||
| return false; | ||
| }; | ||
|
|
||
| let mut cmd = compiler.to_command(); | ||
| if compiler.is_like_msvc() { | ||
| cmd.arg("/c").arg(&src).arg(format!("/Fo{}", obj.display())); | ||
| } else { | ||
| cmd.arg("-c").arg(&src).arg("-o").arg(&obj); | ||
| } | ||
|
|
||
| match cmd.output() { | ||
| Ok(output) => output.status.success(), | ||
| Err(_) => false, | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.