-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
add cfg(target_object_format = "...")
#155126
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
Merged
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 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
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
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
34 changes: 34 additions & 0 deletions
34
src/doc/unstable-book/src/language-features/cfg-target-object-format.md
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,34 @@ | ||
| # `cfg_target_object_format` | ||
|
|
||
| The tracking issue for this feature is: [#152586] | ||
|
|
||
| [#152586]: https://github.com/rust-lang/rust/issues/152586 | ||
|
|
||
| ------------------------ | ||
|
|
||
| The `cfg_target_object_format` feature makes it possible to execute different code | ||
| depending on the current target's object file format. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```rust | ||
| #![feature(cfg_target_object_format)] | ||
|
|
||
| #[cfg(target_object_format = "elf")] | ||
| fn a() { | ||
| // ... | ||
| } | ||
|
|
||
| #[cfg(target_object_format = "mach-o")] | ||
| fn a() { | ||
| // ... | ||
| } | ||
|
|
||
| fn b() { | ||
| if cfg!(target_object_format = "wasm") { | ||
| // ... | ||
| } else { | ||
| // ... | ||
| } | ||
| } | ||
| ``` |
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
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,105 @@ | ||
| //@ add-minicore | ||
| //@ check-pass | ||
| //@ ignore-backends: gcc | ||
| // | ||
| //@ revisions: linux_gnu linux_musl linux_ohos linux_powerpc | ||
| //@[linux_gnu] compile-flags: --target aarch64-unknown-linux-gnu | ||
| //@[linux_gnu] needs-llvm-components: aarch64 | ||
| //@[linux_musl] compile-flags: --target aarch64-unknown-linux-musl | ||
| //@[linux_musl] needs-llvm-components: aarch64 | ||
| //@[linux_ohos] compile-flags: --target aarch64-unknown-linux-ohos | ||
| //@[linux_ohos] needs-llvm-components: aarch64 | ||
| //@[linux_powerpc] compile-flags: --target powerpc-unknown-linux-gnu | ||
| //@[linux_powerpc] needs-llvm-components: powerpc | ||
| // | ||
| //@ revisions: darwin ios | ||
| //@[darwin] compile-flags: --target aarch64-apple-darwin | ||
| //@[darwin] needs-llvm-components: aarch64 | ||
| //@[ios] compile-flags: --target aarch64-apple-ios | ||
| //@[ios] needs-llvm-components: aarch64 | ||
| // | ||
| //@ revisions: win_msvc win_gnu | ||
| //@[win_msvc] compile-flags: --target aarch64-pc-windows-msvc | ||
| //@[win_msvc] needs-llvm-components: aarch64 | ||
| //@[win_gnu] compile-flags: --target x86_64-pc-windows-gnu | ||
| //@[win_gnu] needs-llvm-components: x86 | ||
| // | ||
| //@ revisions: wasm32 wasm64 | ||
| //@[wasm32] compile-flags: --target wasm32-unknown-unknown | ||
| //@[wasm32] needs-llvm-components: webassembly | ||
| //@[wasm64] compile-flags: --target wasm64-unknown-unknown | ||
| //@[wasm64] needs-llvm-components: webassembly | ||
| // | ||
| //@ revisions: aix | ||
| //@[aix] compile-flags: --target powerpc64-ibm-aix | ||
| //@[aix] needs-llvm-components: powerpc | ||
| // | ||
| //@ revisions: hermit sgx uefi | ||
| //@[hermit] compile-flags: --target x86_64-unknown-hermit | ||
| //@[hermit] needs-llvm-components: x86 | ||
| //@[sgx] compile-flags: --target x86_64-fortanix-unknown-sgx | ||
| //@[sgx] needs-llvm-components: x86 | ||
| //@[uefi] compile-flags: --target x86_64-unknown-uefi | ||
| //@[uefi] needs-llvm-components: x86 | ||
| // | ||
| //@ revisions: bpfeb bpfel | ||
| //@[bpfeb] compile-flags: --target bpfeb-unknown-none | ||
| //@[bpfeb] needs-llvm-components: bpf | ||
| //@[bpfel] compile-flags: --target bpfel-unknown-none | ||
| //@[bpfel] needs-llvm-components: bpf | ||
| // | ||
| //@ revisions: avr | ||
| //@[avr] compile-flags: --target avr-none -Ctarget-cpu=atmega328 | ||
| //@[avr] needs-llvm-components: avr | ||
| // | ||
| //@ revisions: msp430 | ||
| //@[msp430] compile-flags: --target msp430-none-elf | ||
| //@[msp430] needs-llvm-components: msp430 | ||
| // | ||
| //@ revisions: thumb | ||
| //@[thumb] compile-flags: --target thumbv7m-none-eabi | ||
| //@[thumb] needs-llvm-components: arm | ||
| #![crate_type = "lib"] | ||
| #![feature(no_core, lang_items, cfg_target_object_format)] | ||
| #![no_core] | ||
|
|
||
| extern crate minicore; | ||
| use minicore::*; | ||
|
|
||
| macro_rules! assert_cfg { | ||
| ($rhs:ident = $rhs_val:literal) => { | ||
| #[cfg(not($rhs = $rhs_val))] | ||
| compile_error!(concat!("expected `", stringify!($rhs), " = ", $rhs_val, "`",)); | ||
| }; | ||
| } | ||
|
|
||
| const _: () = { | ||
| cfg_select!( | ||
| target_os = "linux" => assert_cfg!(target_object_format = "elf"), | ||
| target_os = "aix" => assert_cfg!(target_object_format = "xcoff"), | ||
| target_os = "uefi" => assert_cfg!(target_object_format = "coff"), | ||
| target_os = "windows" => assert_cfg!(target_object_format = "coff"), | ||
| target_os = "hermit" => assert_cfg!(target_object_format = "elf"), | ||
|
|
||
| target_arch = "bpf" => assert_cfg!(target_object_format = "elf"), | ||
| target_arch = "avr" => assert_cfg!(target_object_format = "elf"), | ||
| target_arch = "msp430" => assert_cfg!(target_object_format = "elf"), | ||
|
|
||
| target_abi = "eabi" => assert_cfg!(target_object_format = "elf"), | ||
| target_vendor = "apple" => assert_cfg!(target_object_format = "mach-o"), | ||
| target_family = "wasm" => assert_cfg!(target_object_format = "wasm"), | ||
|
|
||
| windows => assert_cfg!(target_object_format = "coff"), | ||
|
|
||
| _ => {} | ||
| ); | ||
| }; | ||
|
|
||
| const _: () = { | ||
| cfg_select!( | ||
| target_object_format = "mach-o" => assert_cfg!(target_vendor = "apple"), | ||
| target_object_format = "wasm" => assert_cfg!(target_family = "wasm"), | ||
| target_object_format = "xcoff" => assert_cfg!(target_os = "aix"), | ||
| _ => {} | ||
| ); | ||
| }; |
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
8 changes: 8 additions & 0 deletions
8
tests/ui/cfg/disallowed-cli-cfgs.target_object_format_.stderr
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,8 @@ | ||
| error: unexpected `--cfg target_object_format="elf"` flag | ||
| | | ||
| = note: config `target_object_format` is only supposed to be controlled by `--target` | ||
| = note: manually setting a built-in cfg can and does create incoherent behaviors | ||
| = note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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.
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.