diff --git a/Cargo.lock b/Cargo.lock index b7d9343e230340..d9e5ff69f772bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -717,12 +717,13 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.89" +version = "1.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0ba8f7aaa012f30d5b2861462f6708eccd49c3c39863fe083a308035f63d723" +checksum = "755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2" dependencies = [ "jobserver", "libc", + "shlex", ] [[package]] diff --git a/supply-chain/audits.toml b/supply-chain/audits.toml index e55fd8f317209f..9d36fa38c29bc3 100644 --- a/supply-chain/audits.toml +++ b/supply-chain/audits.toml @@ -1097,6 +1097,16 @@ who = "Erich Gubler " criteria = "safe-to-deploy" delta = "1.0.89 -> 1.2.10" +[[audits.cc]] +who = "Erich Gubler " +criteria = "safe-to-deploy" +delta = "1.2.10 -> 1.2.11" + +[[audits.cc]] +who = "Erich Gubler " +criteria = "safe-to-deploy" +delta = "1.2.11 -> 1.2.12" + [[audits.cfg_aliases]] who = "Alex Franchuk " criteria = "safe-to-deploy" diff --git a/third_party/rust/cc/.cargo-checksum.json b/third_party/rust/cc/.cargo-checksum.json index 7391fbe71f0de1..407d8ce3703963 100644 --- a/third_party/rust/cc/.cargo-checksum.json +++ b/third_party/rust/cc/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.lock":"cfcc30c65888ca9642ffc2d3c4294f416aa1789f1ce40362841e4eeb298a628b","Cargo.toml":"1288f536f4ddf6bcdc664a91a070aad2ebd7c6edc32ce24e8d6bc04c2cd64d49","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"378f5840b258e2779c39418f3f2d7b2ba96f1c7917dd6be0713f88305dbda397","README.md":"f1ddbede208a5b78333a25dac0a7598e678e9b601a7d99a791069bddaf180dfe","src/command_helpers.rs":"3ef95bdcd79a43406fdab275d8a8f45ba787876399b54df34068955ec0109e69","src/lib.rs":"91efa8f9242266752658edd66ee607ce30635f4c30710508a99eb62e7b3c54da","src/parallel/async_executor.rs":"4ce24435fff6b6555b43fee042c16bd65d4150d0346567f246b9190d85b45983","src/parallel/job_token.rs":"0676c3177b5be9d7ede483bf4bd45c5ca0f5511073e4d1c9f181a0bc83db05dc","src/parallel/mod.rs":"aaffed5ad3dc0d28641533ab0d6f522bf34a059d4b1a239dc4d217cb5d58e232","src/parallel/stderr.rs":"a2d18ba3f2e04deb9047ece9ab7ca5452d9a76b515afbe20a76307e31597f34b","src/tool.rs":"172cfcbecd7c6a363ea841a48a10a75b0a01e83b83c0691107c601598b68dedf","src/windows/com.rs":"be1564756c9f3ef1398eafeed7b54ba610caba28e8f6258d28a997737ebf9535","src/windows/find_tools.rs":"9234fe7ab27b0259c6fa9fb47826e7d1a3d1d2c7c4042ef7153ab90ccb9a3412","src/windows/mod.rs":"42f1ad7fee35a17686b003e6aa520d3d1940d47d2f531d626e9ae0c48ba49005","src/windows/registry.rs":"c521b72c825e8095843e73482ffa810ed066ad8bb9f86e6db0c5c143c171aba1","src/windows/setup_config.rs":"754439cbab492afd44c9755abcbec1a41c9b2c358131cee2df13c0e996dbbec8","src/windows/vs_instances.rs":"76e3cee74b5fd38ddaf533bba11fe401667c50dda5f9d064099840893eaa7587","src/windows/winapi.rs":"250d51c1826d1a2329e9889dd9f058cfce253dbf2a678b076147c6cdb5db046c","src/windows/windows_sys.rs":"f6b90b87f23e446284bde86749b53858c0d37b8a43515ed8d0e90b1ac8cf7771"},"package":"a0ba8f7aaa012f30d5b2861462f6708eccd49c3c39863fe083a308035f63d723"} \ No newline at end of file +{"files":{"CHANGELOG.md":"e66396b8cd8631cd459417aaa88e3fd8323ccf06ba0a7c6c2659ede4438ec6e6","Cargo.lock":"7fe14a44fd079561e01d523ebd182929e0c7108d8cd7d30c777aaa1369ec7c99","Cargo.toml":"f100a0af14af5584342423999efe5d3015ad9fc2dbaf6a32845eced57f015b86","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"378f5840b258e2779c39418f3f2d7b2ba96f1c7917dd6be0713f88305dbda397","README.md":"f1ddbede208a5b78333a25dac0a7598e678e9b601a7d99a791069bddaf180dfe","clippy.toml":"aa7850db4350883c8f373bd0d6b4d19bf3b75f13c1c238e24368c109cb52fb1d","src/command_helpers.rs":"511429ff0130a7c5a5c1db29939a78af4f7fb84e6e4293e721c6deceffd28245","src/detect_compiler_family.c":"97ca4b021495611e828becea6187add37414186a16dfedd26c2947cbce6e8b2f","src/flags.rs":"52b1860a0836cfaf3acd57e5f1118430f2269630b54e6ce03de05d46518ce7d2","src/lib.rs":"8479d1b0791b8e284f57afc6043d79a933b6b8132b132e71d58c7ac7db011157","src/parallel/async_executor.rs":"4ce24435fff6b6555b43fee042c16bd65d4150d0346567f246b9190d85b45983","src/parallel/job_token.rs":"018a01cb00182270bbcb68e31e7a7c5c621a95f086e4c68cfa2bf557ac24e5f2","src/parallel/mod.rs":"bd9c1334d17d138c281961c690b8d8118a2d6295a7d6cd7296826255436fa063","src/parallel/stderr.rs":"74384d41198740a6fce0877f144262db09fb091225fa8fbfa771314bb11487c6","src/target.rs":"a85757d86dc4db6c0ebd2f7ce57fd3d062c3a98eaa97409f8be9609bd34bc56a","src/target/apple.rs":"8fb110d87c3c12f62d0b1cf61ff5656b96d651501bc7dc8b021057b675486398","src/target/generated.rs":"db71ef59fcaa55aca99e6df3f228ce05448c059a9c3c960fe41ffca765c7c1c5","src/target/llvm.rs":"e7f41b5f30394718c40f0822e9f87f9076c2b957e78a51857cb04f13c7cb4296","src/target/parser.rs":"c8bfc198abdd944c4292ecbe8bd7bc9d3dba16601325d776be836df852bf7bb3","src/tempfile.rs":"ebafb5b0e5d08b0706916ed911d4245240e60c3e2d0c9a1630c520842988a2b3","src/tool.rs":"bb0909bb7d1514b212b7bb030936cd228a4102680d7d4a0f4281597bb348d0c8","src/utilities.rs":"52b30b24a1c31cdefb105309ee5220cfc9fca76eaf4e6d6509c3e19f431448fe","src/windows/com.rs":"a2800ddb81215fff2bf618336f5c4ff8e8bdb746dd18b795873c7304b3f2a5e3","src/windows/find_tools.rs":"90c831db5f502bfbdaa6d6d55cbdc7097aa819b0c3b41fdb007d18bdedc7a24e","src/windows/mod.rs":"34cfa201cfbcac7ccaa3ea5295d3e4200439af3cc5c6433baf81502596040a89","src/windows/registry.rs":"c521b72c825e8095843e73482ffa810ed066ad8bb9f86e6db0c5c143c171aba1","src/windows/setup_config.rs":"754439cbab492afd44c9755abcbec1a41c9b2c358131cee2df13c0e996dbbec8","src/windows/vs_instances.rs":"946527cf8fd32c3472f6a2884dcdec290763101097334c7478f9c24c3950db6b","src/windows/winapi.rs":"250d51c1826d1a2329e9889dd9f058cfce253dbf2a678b076147c6cdb5db046c","src/windows/windows_sys.rs":"324a391634b956c0ce67b611998b677caf5a1a6e25e6ed749a7d8f7a185f14c9","src/windows/windows_targets.rs":"5b4648ebc22b028caca9f4b4bf8881fe2d094b7bec217264ba2e6e2c49d1ccee"},"package":"755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2"} \ No newline at end of file diff --git a/third_party/rust/cc/CHANGELOG.md b/third_party/rust/cc/CHANGELOG.md new file mode 100644 index 00000000000000..59dddf660fe260 --- /dev/null +++ b/third_party/rust/cc/CHANGELOG.md @@ -0,0 +1,396 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.2.12](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.11...cc-v1.2.12) - 2025-02-04 + +### Other + +- Split impl Build ([#1382](https://github.com/rust-lang/cc-rs/pull/1382)) +- Don't specify both `-target` and `-mtargetos=` on Apple targets ([#1384](https://github.com/rust-lang/cc-rs/pull/1384)) + +## [1.2.11](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.10...cc-v1.2.11) - 2025-01-31 + +### Other + +- Fix more flag inheritance ([#1380](https://github.com/rust-lang/cc-rs/pull/1380)) +- Include wrapper args. in `stdout` family heuristics to restore classifying `clang --driver-mode=cl` as `Msvc { clang_cl: true }` ([#1378](https://github.com/rust-lang/cc-rs/pull/1378)) +- Constrain `-Clto` and `-Cembed-bitcode` flag inheritance to be `clang`-only ([#1379](https://github.com/rust-lang/cc-rs/pull/1379)) +- Pass deployment target with `-m*-version-min=` ([#1339](https://github.com/rust-lang/cc-rs/pull/1339)) +- Regenerate target info ([#1376](https://github.com/rust-lang/cc-rs/pull/1376)) + +## [1.2.10](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.9...cc-v1.2.10) - 2025-01-17 + +### Other + +- Fix CC_FORCE_DISABLE=0 evaluating to true ([#1371](https://github.com/rust-lang/cc-rs/pull/1371)) +- Regenerate target info ([#1369](https://github.com/rust-lang/cc-rs/pull/1369)) +- Make hidden lifetimes explicit. ([#1366](https://github.com/rust-lang/cc-rs/pull/1366)) + +## [1.2.9](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.8...cc-v1.2.9) - 2025-01-12 + +### Other + +- Don't pass inherited PGO flags to GNU compilers (#1363) +- Adjusted zig cc judgment and avoided zigbuild errors([#1360](https://github.com/rust-lang/cc-rs/pull/1360)) ([#1361](https://github.com/rust-lang/cc-rs/pull/1361)) +- Fix compilation on macOS using clang and fix compilation using zig-cc ([#1364](https://github.com/rust-lang/cc-rs/pull/1364)) + +## [1.2.8](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.7...cc-v1.2.8) - 2025-01-11 + +### Other + +- Add `is_like_clang_cl()` getter (#1357) +- Fix clippy error in lib.rs ([#1356](https://github.com/rust-lang/cc-rs/pull/1356)) +- Regenerate target info ([#1352](https://github.com/rust-lang/cc-rs/pull/1352)) +- Fix compiler family detection issue with clang-cl on macOS ([#1328](https://github.com/rust-lang/cc-rs/pull/1328)) +- Update `windows-bindgen` dependency ([#1347](https://github.com/rust-lang/cc-rs/pull/1347)) +- Fix clippy warnings ([#1346](https://github.com/rust-lang/cc-rs/pull/1346)) + +## [1.2.7](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.6...cc-v1.2.7) - 2025-01-03 + +### Other + +- Regenerate target info ([#1342](https://github.com/rust-lang/cc-rs/pull/1342)) +- Document new supported architecture names in windows::find +- Make is_flag_supported_inner take an &Tool ([#1337](https://github.com/rust-lang/cc-rs/pull/1337)) +- Fix is_flag_supported on msvc ([#1336](https://github.com/rust-lang/cc-rs/pull/1336)) +- Allow using Visual Studio target names in `find_tool` ([#1335](https://github.com/rust-lang/cc-rs/pull/1335)) + +## [1.2.6](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.5...cc-v1.2.6) - 2024-12-27 + +### Other + +- Don't inherit the `/Oy` flag for 64-bit targets ([#1330](https://github.com/rust-lang/cc-rs/pull/1330)) + +## [1.2.5](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.4...cc-v1.2.5) - 2024-12-19 + +### Other + +- Check linking when testing if compiler flags are supported ([#1322](https://github.com/rust-lang/cc-rs/pull/1322)) + +## [1.2.4](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.3...cc-v1.2.4) - 2024-12-13 + +### Other + +- Add support for C/C++ compiler for Neutrino QNX: `qcc` ([#1319](https://github.com/rust-lang/cc-rs/pull/1319)) +- use -maix64 instead of -m64 ([#1307](https://github.com/rust-lang/cc-rs/pull/1307)) + +## [1.2.3](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.2...cc-v1.2.3) - 2024-12-06 + +### Other + +- Improve detection of environment when compiling from msbuild or msvc ([#1310](https://github.com/rust-lang/cc-rs/pull/1310)) +- Better error message when failing on unknown targets ([#1313](https://github.com/rust-lang/cc-rs/pull/1313)) +- Optimize RustcCodegenFlags ([#1305](https://github.com/rust-lang/cc-rs/pull/1305)) + +## [1.2.2](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.1...cc-v1.2.2) - 2024-11-29 + +### Other + +- Inherit flags from rustc ([#1279](https://github.com/rust-lang/cc-rs/pull/1279)) +- Add support for using sccache wrapper with cuda/nvcc ([#1304](https://github.com/rust-lang/cc-rs/pull/1304)) +- Fix msvc stdout not shown on error ([#1303](https://github.com/rust-lang/cc-rs/pull/1303)) +- Regenerate target info ([#1301](https://github.com/rust-lang/cc-rs/pull/1301)) +- Fix compilation of C++ code for armv7-unknown-linux-gnueabihf ([#1298](https://github.com/rust-lang/cc-rs/pull/1298)) +- Fetch target info from Cargo even if `Build::target` is manually set ([#1299](https://github.com/rust-lang/cc-rs/pull/1299)) +- Fix two files with different extensions having the same object name ([#1295](https://github.com/rust-lang/cc-rs/pull/1295)) +- Allow disabling cc's ability to compile via env var CC_FORCE_DISABLE ([#1292](https://github.com/rust-lang/cc-rs/pull/1292)) +- Regenerate target info ([#1293](https://github.com/rust-lang/cc-rs/pull/1293)) + +## [1.2.1](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.0...cc-v1.2.1) - 2024-11-14 + +### Other + +- When invoking `cl -?`, set stdin to null ([#1288](https://github.com/rust-lang/cc-rs/pull/1288)) + +## [1.2.0](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.37...cc-v1.2.0) - 2024-11-11 + +### Added + +- add i686-pc-windows-gnullvm prefix detection ([#1283](https://github.com/rust-lang/cc-rs/pull/1283)) + +### Other + +- Allow only specifying the architecture ([#1285](https://github.com/rust-lang/cc-rs/pull/1285)) +- Fix WASM vs. WASI options ([#1284](https://github.com/rust-lang/cc-rs/pull/1284)) + +## [1.1.37](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.36...cc-v1.1.37) - 2024-11-08 + +### Other + +- Use relative directory for obj files hash ([#1270](https://github.com/rust-lang/cc-rs/pull/1270)) +- Regenerate target info ([#1280](https://github.com/rust-lang/cc-rs/pull/1280)) + +## [1.1.36](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.35...cc-v1.1.36) - 2024-11-05 + +### Other + +- Fix CUDA build with clang++. ([#1273](https://github.com/rust-lang/cc-rs/pull/1273)) + +## [1.1.35](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.34...cc-v1.1.35) - 2024-11-04 + +### Other + +- Remove support for FRC ([#1268](https://github.com/rust-lang/cc-rs/pull/1268)) +- Do not add -fPIC by default on UEFI targets ([#1263](https://github.com/rust-lang/cc-rs/pull/1263)) +- Use -windows-gnu for all UEFI targets ([#1264](https://github.com/rust-lang/cc-rs/pull/1264)) + +## [1.1.34](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.33...cc-v1.1.34) - 2024-11-02 + +### Other + +- Remove redundant flags ([#1256](https://github.com/rust-lang/cc-rs/pull/1256)) + +## [1.1.33](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.32...cc-v1.1.33) - 2024-11-02 + +### Other + +- Reduce size of `cc::Build` and size of generated targets ([#1257](https://github.com/rust-lang/cc-rs/pull/1257)) + +## [1.1.32](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.31...cc-v1.1.32) - 2024-11-02 + +### Other + +- Use `rustc`'s knowledge of LLVM/Clang target triples ([#1252](https://github.com/rust-lang/cc-rs/pull/1252)) +- Use Cargo's target information when possible ([#1225](https://github.com/rust-lang/cc-rs/pull/1225)) + +## [1.1.31](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.30...cc-v1.1.31) - 2024-10-19 + +### Other + +- Add comment explaining why cc does not rebuild on env PATH change ([#1247](https://github.com/rust-lang/cc-rs/pull/1247)) + +## [1.1.30](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.29...cc-v1.1.30) - 2024-10-11 + +### Other + +- Don't pass -fPIC by default on wasm ([#1245](https://github.com/rust-lang/cc-rs/pull/1245)) + +## [1.1.29](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.28...cc-v1.1.29) - 2024-10-11 + +### Other + +- Regenerate target info ([#1243](https://github.com/rust-lang/cc-rs/pull/1243)) + +## [1.1.28](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.27...cc-v1.1.28) - 2024-10-06 + +### Other + +- Environment variables: For one accepting boolean, treat "0", "false" and empty env as false ([#1238](https://github.com/rust-lang/cc-rs/pull/1238)) + +## [1.1.27](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.26...cc-v1.1.27) - 2024-10-06 + +### Other + +- Revert "Use debug version of MSVC runtime library on debug ([#1231](https://github.com/rust-lang/cc-rs/pull/1231))" ([#1237](https://github.com/rust-lang/cc-rs/pull/1237)) +- Disable `CC_ENABLE_DEBUG_OUTPUT` if it is set to "0" ([#1234](https://github.com/rust-lang/cc-rs/pull/1234)) + +## [1.1.26](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.25...cc-v1.1.26) - 2024-10-06 + +### Other + +- Use debug version of MSVC runtime library on debug ([#1231](https://github.com/rust-lang/cc-rs/pull/1231)) + +## [1.1.25](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.24...cc-v1.1.25) - 2024-10-05 + +### Other + +- Remove incorrect "lib" prefixes in CXXSTDLIB doc comments ([#1228](https://github.com/rust-lang/cc-rs/pull/1228)) + +## [1.1.24](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.23...cc-v1.1.24) - 2024-10-01 + +### Other + +- Fix wasm32-wasip1-threads: shared-memory disallowed due to not compiled with 'atomics' or 'bulk-memory' features ([#1221](https://github.com/rust-lang/cc-rs/pull/1221)) +- Reduce the need for the host target triple ([#1224](https://github.com/rust-lang/cc-rs/pull/1224)) +- Add auto cancellation for CI jobs ([#1222](https://github.com/rust-lang/cc-rs/pull/1222)) + +## [1.1.23](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.22...cc-v1.1.23) - 2024-09-30 + +### Other + +- Update doc for detecting changes/upgrades of compilers ([#1218](https://github.com/rust-lang/cc-rs/pull/1218)) + +## [1.1.22](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.21...cc-v1.1.22) - 2024-09-27 + +### Other + +- Don't rerun if PATH changes ([#1215](https://github.com/rust-lang/cc-rs/pull/1215)) + +## [1.1.21](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.20...cc-v1.1.21) - 2024-09-18 + +### Other + +- disable pic for targets that end in `-none` ([#1212](https://github.com/rust-lang/cc-rs/pull/1212)) + +## [1.1.20](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.19...cc-v1.1.20) - 2024-09-17 + +### Other + +- Add buildcache as known Rust and C/C++ compiler wrapper ([#1209](https://github.com/rust-lang/cc-rs/pull/1209)) + +## [1.1.19](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.18...cc-v1.1.19) - 2024-09-15 + +### Other + +- Add support arm64e-apple-darwin ([#1207](https://github.com/rust-lang/cc-rs/pull/1207)) + +## [1.1.18](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.17...cc-v1.1.18) - 2024-09-07 + +### Other +- Fixed unsoundness in `StderrForwarder::forward_available` ([#1203](https://github.com/rust-lang/cc-rs/pull/1203)) + +## [1.1.17](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.16...cc-v1.1.17) - 2024-09-06 + +### Fixed +- fix finding toolchains when invoked by msbuild ([#1201](https://github.com/rust-lang/cc-rs/pull/1201)) + +## [1.1.16](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.15...cc-v1.1.16) - 2024-09-04 + +### Other +- Treat VxWorks wr-cc as a Gnu compiler ([#1198](https://github.com/rust-lang/cc-rs/pull/1198)) + +## [1.1.15](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.14...cc-v1.1.15) - 2024-08-26 + +### Other +- Add -mfloat-abi=hard as a default argument when using any arm/thumb-none-eabihf target ([#1194](https://github.com/rust-lang/cc-rs/pull/1194)) + +## [1.1.14](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.13...cc-v1.1.14) - 2024-08-23 + +### Other +- allow finding tools from path if VisualStudioDir is set + +## [1.1.13](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.12...cc-v1.1.13) - 2024-08-16 + +### Other +- Fix detect family: should detect emscripten as clang, closes [#1185](https://github.com/rust-lang/cc-rs/pull/1185) ([#1186](https://github.com/rust-lang/cc-rs/pull/1186)) + +## [1.1.12](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.11...cc-v1.1.12) - 2024-08-15 + +### Other +- improve docs ([#1183](https://github.com/rust-lang/cc-rs/pull/1183)) + +## [1.1.11](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.10...cc-v1.1.11) - 2024-08-14 + +### Other +- Add support for parsing shell encoded `*FLAGS` ([#1181](https://github.com/rust-lang/cc-rs/pull/1181)) +- Replace vector of tuples with BTreeMap which already is sorted and free of duplicates ([#1177](https://github.com/rust-lang/cc-rs/pull/1177)) + +## [1.1.10](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.9...cc-v1.1.10) - 2024-08-11 + +### Other +- Remap Windows targets triples to their LLVM counterparts ([#1176](https://github.com/rust-lang/cc-rs/pull/1176)) + +## [1.1.9](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.8...cc-v1.1.9) - 2024-08-11 + +### Other +- Add custom CC wrapper to the wrapper whitelist ([#1175](https://github.com/rust-lang/cc-rs/pull/1175)) + +## [1.1.8](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.7...cc-v1.1.8) - 2024-08-06 + +### Other +- Fix broken link in docs.rs ([#1173](https://github.com/rust-lang/cc-rs/pull/1173)) + +## [1.1.7](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.6...cc-v1.1.7) - 2024-07-29 + +### Other +- add `.objects` ([#1166](https://github.com/rust-lang/cc-rs/pull/1166)) + +## [1.1.6](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.5...cc-v1.1.6) - 2024-07-19 + +### Other +- Clippy fixes ([#1163](https://github.com/rust-lang/cc-rs/pull/1163)) + +## [1.1.5](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.4...cc-v1.1.5) - 2024-07-15 + +### Other +- Fix cyclic compilation: Use vendored once_cell ([#1154](https://github.com/rust-lang/cc-rs/pull/1154)) + +## [1.1.4](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.3...cc-v1.1.4) - 2024-07-14 + +### Other +- Support compiling on wasm targets (Supersede [#1068](https://github.com/rust-lang/cc-rs/pull/1068)) ([#1160](https://github.com/rust-lang/cc-rs/pull/1160)) + +## [1.1.3](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.2...cc-v1.1.3) - 2024-07-14 + +### Other +- Reduce msrv to 1.63 ([#1158](https://github.com/rust-lang/cc-rs/pull/1158)) +- Revert "Use raw-dylib for windows-sys ([#1137](https://github.com/rust-lang/cc-rs/pull/1137))" ([#1157](https://github.com/rust-lang/cc-rs/pull/1157)) +- Fix typos ([#1152](https://github.com/rust-lang/cc-rs/pull/1152)) +- Fix `doc_lazy_continuation` lints ([#1153](https://github.com/rust-lang/cc-rs/pull/1153)) + +## [1.1.2](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.1...cc-v1.1.2) - 2024-07-12 + +### Other +- Add empty `jobserver` feature. ([#1150](https://github.com/rust-lang/cc-rs/pull/1150)) + +## [1.1.1](https://github.com/rust-lang/cc-rs/compare/cc-v1.1.0...cc-v1.1.1) - 2024-07-12 + +### Other +- Fix is_flag_supported not respecting emit_rerun_if_env_changed ([#1147](https://github.com/rust-lang/cc-rs/pull/1147)) ([#1148](https://github.com/rust-lang/cc-rs/pull/1148)) + +## [1.1.0](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.106...cc-v1.1.0) - 2024-07-08 + +### Added +- add cargo_output to eliminate last vestiges of stdout pollution ([#1141](https://github.com/rust-lang/cc-rs/pull/1141)) + +## [1.0.106](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.105...cc-v1.0.106) - 2024-07-08 + +### Other +- Drop support for Visual Studio 12 (2013) ([#1046](https://github.com/rust-lang/cc-rs/pull/1046)) +- Use raw-dylib for windows-sys ([#1137](https://github.com/rust-lang/cc-rs/pull/1137)) +- Bump msrv to 1.67 ([#1143](https://github.com/rust-lang/cc-rs/pull/1143)) +- Bump msrv to 1.65 ([#1140](https://github.com/rust-lang/cc-rs/pull/1140)) +- Fix clippy warnings ([#1138](https://github.com/rust-lang/cc-rs/pull/1138)) + +## [1.0.105](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.104...cc-v1.0.105) - 2024-07-07 + +### Other +- Regenerate windows sys bindings ([#1132](https://github.com/rust-lang/cc-rs/pull/1132)) +- Fix generate-windows-sys-bindings ([#1133](https://github.com/rust-lang/cc-rs/pull/1133)) +- Fix gen-windows-sys-binding ([#1130](https://github.com/rust-lang/cc-rs/pull/1130)) +- Fix gen-windows-sys-binding ([#1127](https://github.com/rust-lang/cc-rs/pull/1127)) +- Update windows-bindgen requirement from 0.57 to 0.58 ([#1123](https://github.com/rust-lang/cc-rs/pull/1123)) + +## [1.0.104](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.103...cc-v1.0.104) - 2024-07-01 + +### Other +- Fixed link break about compile-time-requirements ([#1118](https://github.com/rust-lang/cc-rs/pull/1118)) + +## [1.0.103](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.102...cc-v1.0.103) - 2024-06-30 + +### Other +- Fix compilation for wasm: env WASI_SYSROOT should be optional ([#1114](https://github.com/rust-lang/cc-rs/pull/1114)) + +## [1.0.102](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.101...cc-v1.0.102) - 2024-06-29 + +### Other +- Fix invalid wasi targets compatibility ([#1105](https://github.com/rust-lang/cc-rs/pull/1105)) +- Speedup regenerate-target-info and regenerate-windows-sys ([#1110](https://github.com/rust-lang/cc-rs/pull/1110)) + +## [1.0.101](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.100...cc-v1.0.101) - 2024-06-25 + +### Other +- Use `Build::getenv` instead of `env::var*` in anywhere that makes sense ([#1103](https://github.com/rust-lang/cc-rs/pull/1103)) + +## [1.0.100](https://github.com/rust-lang/cc-rs/compare/cc-v1.0.99...cc-v1.0.100) - 2024-06-23 + +### Other +- Update publish.yml to use release-plz ([#1101](https://github.com/rust-lang/cc-rs/pull/1101)) +- Accept `OsStr` instead of `str` for flags ([#1100](https://github.com/rust-lang/cc-rs/pull/1100)) +- Use `dep:` syntax to avoid implicit features. ([#1099](https://github.com/rust-lang/cc-rs/pull/1099)) +- Minor clippy fixes. ([#1098](https://github.com/rust-lang/cc-rs/pull/1098)) +- Fix WASI compilation for C++ ([#1083](https://github.com/rust-lang/cc-rs/pull/1083)) +- Regenerate windows sys bindings ([#1096](https://github.com/rust-lang/cc-rs/pull/1096)) +- Rename regenerate-windows-sys to regenerate-windows-sys.yml ([#1095](https://github.com/rust-lang/cc-rs/pull/1095)) +- Create regenerate-windows-sys.yml ([#1094](https://github.com/rust-lang/cc-rs/pull/1094)) +- Update windows-bindgen requirement from 0.56 to 0.57 ([#1091](https://github.com/rust-lang/cc-rs/pull/1091)) +- Eagerly close tempfile to fix [#1082](https://github.com/rust-lang/cc-rs/pull/1082) ([#1087](https://github.com/rust-lang/cc-rs/pull/1087)) +- Output msvc.exe in the output directory ([#1090](https://github.com/rust-lang/cc-rs/pull/1090)) +- Fix clippy warnings on Windows ([#1088](https://github.com/rust-lang/cc-rs/pull/1088)) +- Don't try to free DLL on drop ([#1089](https://github.com/rust-lang/cc-rs/pull/1089)) +- Fix panic safety issue in StderrForwarder ([#1079](https://github.com/rust-lang/cc-rs/pull/1079)) diff --git a/third_party/rust/cc/Cargo.lock b/third_party/rust/cc/Cargo.lock index 4e5d1d06807ff7..56f6bda4e7d758 100644 --- a/third_party/rust/cc/Cargo.lock +++ b/third_party/rust/cc/Cargo.lock @@ -4,16 +4,17 @@ version = 3 [[package]] name = "bitflags" -version = "2.4.2" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" [[package]] name = "cc" -version = "1.0.89" +version = "1.2.12" dependencies = [ "jobserver", "libc", + "shlex", "tempfile", ] @@ -25,9 +26,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", "windows-sys", @@ -35,36 +36,54 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.1" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] [[package]] name = "jobserver" -version = "0.1.28" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab46a6e9526ddef3ae7f787c06f0f2600639ba80ea3eade3d8e670a2230f51d6" +checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" dependencies = [ "libc", ] [[package]] name = "libc" -version = "0.2.153" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "linux-raw-sys" -version = "0.4.13" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "once_cell" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "rustix" -version = "0.38.31" +version = "0.38.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" dependencies = [ "bitflags", "errno", @@ -73,36 +92,54 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "tempfile" -version = "3.10.1" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" dependencies = [ "cfg-if", "fastrand", + "getrandom", + "once_cell", "rustix", "windows-sys", ] +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "windows-sys" -version = "0.52.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", + "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", @@ -111,42 +148,57 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] diff --git a/third_party/rust/cc/Cargo.toml b/third_party/rust/cc/Cargo.toml index 5d974e076f943d..25e8d9ddcaa7ba 100644 --- a/third_party/rust/cc/Cargo.toml +++ b/third_party/rust/cc/Cargo.toml @@ -11,15 +11,21 @@ [package] edition = "2018" -rust-version = "1.53" +rust-version = "1.63" name = "cc" -version = "1.0.89" +version = "1.2.12" authors = ["Alex Crichton "] +build = false exclude = [ "/.github", "tests", "src/bin", ] +autolib = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = """ A build-time dependency for Cargo build scripts to assist in invoking the native C compiler to compile native C code into a static archive to be linked into Rust @@ -33,18 +39,26 @@ categories = ["development-tools::build-utils"] license = "MIT OR Apache-2.0" repository = "https://github.com/rust-lang/cc-rs" +[lib] +name = "cc" +path = "src/lib.rs" + [dependencies.jobserver] -version = "0.1.20" +version = "0.1.30" optional = true default-features = false +[dependencies.shlex] +version = "1.3.0" + [dev-dependencies.tempfile] version = "3" [features] +jobserver = [] parallel = [ - "libc", - "jobserver", + "dep:libc", + "dep:jobserver", ] [target."cfg(unix)".dependencies.libc] diff --git a/third_party/rust/cc/clippy.toml b/third_party/rust/cc/clippy.toml new file mode 100644 index 00000000000000..39e6cc64d88cb9 --- /dev/null +++ b/third_party/rust/cc/clippy.toml @@ -0,0 +1,5 @@ +disallowed-methods = [ + { path = "std::env::var_os", reason = "Please use Build::getenv" }, + { path = "std::env::var", reason = "Please use Build::getenv" }, +] +doc-valid-idents = ["AppleClang", "OpenBSD", ".."] diff --git a/third_party/rust/cc/src/command_helpers.rs b/third_party/rust/cc/src/command_helpers.rs index 919d276c84ec53..62e36629938699 100644 --- a/third_party/rust/cc/src/command_helpers.rs +++ b/third_party/rust/cc/src/command_helpers.rs @@ -1,6 +1,7 @@ //! Miscellaneous helpers for running commands use std::{ + borrow::Cow, collections::hash_map, ffi::OsString, fmt::Display, @@ -22,15 +23,32 @@ pub(crate) struct CargoOutput { pub(crate) metadata: bool, pub(crate) warnings: bool, pub(crate) debug: bool, + pub(crate) output: OutputKind, checked_dbg_var: Arc, } +/// Different strategies for handling compiler output (to stdout) +#[derive(Clone, Debug)] +pub(crate) enum OutputKind { + /// Forward the output to this process' stdout ([`Stdio::inherit()`]) + Forward, + /// Discard the output ([`Stdio::null()`]) + Discard, + /// Capture the result (`[Stdio::piped()`]) + Capture, +} + impl CargoOutput { pub(crate) fn new() -> Self { + #[allow(clippy::disallowed_methods)] Self { metadata: true, warnings: true, - debug: std::env::var_os("CC_ENABLE_DEBUG_OUTPUT").is_some(), + output: OutputKind::Forward, + debug: match std::env::var_os("CC_ENABLE_DEBUG_OUTPUT") { + Some(v) => v != "0" && v != "false" && v != "", + None => false, + }, checked_dbg_var: Arc::new(AtomicBool::new(false)), } } @@ -64,6 +82,14 @@ impl CargoOutput { Stdio::null() } } + + fn stdio_for_output(&self) -> Stdio { + match self.output { + OutputKind::Capture => Stdio::piped(), + OutputKind::Forward => Stdio::inherit(), + OutputKind::Discard => Stdio::null(), + } + } } pub(crate) struct StderrForwarder { @@ -72,6 +98,8 @@ pub(crate) struct StderrForwarder { is_non_blocking: bool, #[cfg(feature = "parallel")] bytes_available_failed: bool, + /// number of bytes buffered in inner + bytes_buffered: usize, } const MIN_BUFFER_CAPACITY: usize = 100; @@ -83,6 +111,7 @@ impl StderrForwarder { .stderr .take() .map(|stderr| (stderr, Vec::with_capacity(MIN_BUFFER_CAPACITY))), + bytes_buffered: 0, #[cfg(feature = "parallel")] is_non_blocking: false, #[cfg(feature = "parallel")] @@ -90,12 +119,9 @@ impl StderrForwarder { } } - #[allow(clippy::uninit_vec)] fn forward_available(&mut self) -> bool { if let Some((stderr, buffer)) = self.inner.as_mut() { loop { - let old_data_end = buffer.len(); - // For non-blocking we check to see if there is data available, so we should try to // read at least that much. For blocking, always read at least the minimum amount. #[cfg(not(feature = "parallel"))] @@ -104,7 +130,7 @@ impl StderrForwarder { let to_reserve = if self.is_non_blocking && !self.bytes_available_failed { match crate::parallel::stderr::bytes_available(stderr) { #[cfg(windows)] - Ok(0) => return false, + Ok(0) => break false, #[cfg(unix)] Ok(0) => { // On Unix, depending on the implementation, we may sometimes get 0 in a @@ -120,7 +146,7 @@ impl StderrForwarder { write_warning(&buffer[..]); } self.inner = None; - return true; + break true; } #[cfg(unix)] Err(_) => { @@ -130,48 +156,54 @@ impl StderrForwarder { self.bytes_available_failed = true; MIN_BUFFER_CAPACITY } + #[cfg(target_family = "wasm")] + Err(_) => panic!("bytes_available should always succeed on wasm"), Ok(bytes_available) => MIN_BUFFER_CAPACITY.max(bytes_available), } } else { MIN_BUFFER_CAPACITY }; - buffer.reserve(to_reserve); - - // SAFETY: 1) the length is set to the capacity, so we are never using memory beyond - // the underlying buffer and 2) we always call `truncate` below to set the len back - // to the initialized data. - unsafe { - buffer.set_len(buffer.capacity()); + if self.bytes_buffered + to_reserve > buffer.len() { + buffer.resize(self.bytes_buffered + to_reserve, 0); } - match stderr.read(&mut buffer[old_data_end..]) { + + match stderr.read(&mut buffer[self.bytes_buffered..]) { Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { // No data currently, yield back. - buffer.truncate(old_data_end); - return false; + break false; } Err(err) if err.kind() == std::io::ErrorKind::Interrupted => { // Interrupted, try again. - buffer.truncate(old_data_end); - } - Ok(0) | Err(_) => { - // End of stream: flush remaining data and bail. - if old_data_end > 0 { - write_warning(&buffer[..old_data_end]); - } - self.inner = None; - return true; + continue; } - Ok(bytes_read) => { - buffer.truncate(old_data_end + bytes_read); + Ok(bytes_read) if bytes_read != 0 => { + self.bytes_buffered += bytes_read; let mut consumed = 0; - for line in buffer.split_inclusive(|&b| b == b'\n') { + for line in buffer[..self.bytes_buffered].split_inclusive(|&b| b == b'\n') { // Only forward complete lines, leave the rest in the buffer. if let Some((b'\n', line)) = line.split_last() { consumed += line.len() + 1; write_warning(line); } } - buffer.drain(..consumed); + if consumed > 0 && consumed < self.bytes_buffered { + // Remove the consumed bytes from buffer + buffer.copy_within(consumed.., 0); + } + self.bytes_buffered -= consumed; + } + res => { + // End of stream: flush remaining data and bail. + if self.bytes_buffered > 0 { + write_warning(&buffer[..self.bytes_buffered]); + } + if let Err(err) = res { + write_warning( + format!("Failed to read from child stderr: {err}").as_bytes(), + ); + } + self.inner.take(); + break true; } } } @@ -215,7 +247,7 @@ fn write_warning(line: &[u8]) { fn wait_on_child( cmd: &Command, - program: &str, + program: &Path, child: &mut Child, cargo_output: &CargoOutput, ) -> Result<(), Error> { @@ -227,8 +259,10 @@ fn wait_on_child( return Err(Error::new( ErrorKind::ToolExecError, format!( - "Failed to wait on spawned child process, command {:?} with args {:?}: {}.", - cmd, program, e + "Failed to wait on spawned child process, command {:?} with args {}: {}.", + cmd, + program.display(), + e ), )); } @@ -242,8 +276,10 @@ fn wait_on_child( Err(Error::new( ErrorKind::ToolExecError, format!( - "Command {:?} with args {:?} did not execute successfully (status code {}).", - cmd, program, status + "Command {:?} with args {} did not execute successfully (status code {}).", + cmd, + program.display(), + status ), )) } @@ -276,7 +312,24 @@ pub(crate) fn objects_from_files(files: &[Arc], dst: &Path) -> Result], dst: &Path) -> Result, cargo_output: &CargoOutput, ) -> Result<(), Error> { + let program = program.as_ref(); + let mut child = spawn(cmd, program, cargo_output)?; wait_on_child(cmd, program, &mut child, cargo_output) } pub(crate) fn run_output( cmd: &mut Command, - program: &str, + program: impl AsRef, cargo_output: &CargoOutput, ) -> Result, Error> { - cmd.stdout(Stdio::piped()); + let program = program.as_ref(); - let mut child = spawn(cmd, program, cargo_output)?; + // We specifically need the output to be captured, so override default + let mut captured_cargo_output = cargo_output.clone(); + captured_cargo_output.output = OutputKind::Capture; + let mut child = spawn(cmd, program, &captured_cargo_output)?; let mut stdout = vec![]; child @@ -323,6 +381,7 @@ pub(crate) fn run_output( .read_to_end(&mut stdout) .unwrap(); + // Don't care about this output, use the normal settings wait_on_child(cmd, program, &mut child, cargo_output)?; Ok(stdout) @@ -330,7 +389,7 @@ pub(crate) fn run_output( pub(crate) fn spawn( cmd: &mut Command, - program: &str, + program: &Path, cargo_output: &CargoOutput, ) -> Result { struct ResetStderr<'cmd>(&'cmd mut Command); @@ -346,42 +405,55 @@ pub(crate) fn spawn( cargo_output.print_debug(&format_args!("running: {:?}", cmd)); let cmd = ResetStderr(cmd); - let child = cmd.0.stderr(cargo_output.stdio_for_warnings()).spawn(); + let child = cmd + .0 + .stderr(cargo_output.stdio_for_warnings()) + .stdout(cargo_output.stdio_for_output()) + .spawn(); match child { Ok(child) => Ok(child), Err(ref e) if e.kind() == io::ErrorKind::NotFound => { let extra = if cfg!(windows) { - " (see https://github.com/rust-lang/cc-rs#compile-time-requirements \ + " (see https://docs.rs/cc/latest/cc/#compile-time-requirements \ for help)" } else { "" }; Err(Error::new( ErrorKind::ToolNotFound, - format!("Failed to find tool. Is `{}` installed?{}", program, extra), + format!( + "Failed to find tool. Is `{}` installed?{}", + program.display(), + extra + ), )) } Err(e) => Err(Error::new( ErrorKind::ToolExecError, format!( - "Command {:?} with args {:?} failed to start: {:?}", - cmd.0, program, e + "Command {:?} with args {} failed to start: {:?}", + cmd.0, + program.display(), + e ), )), } } -pub(crate) fn command_add_output_file( - cmd: &mut Command, - dst: &Path, - cuda: bool, - msvc: bool, - clang: bool, - gnu: bool, - is_asm: bool, - is_arm: bool, -) { - if msvc && !clang && !gnu && !cuda && !(is_asm && is_arm) { +pub(crate) struct CmdAddOutputFileArgs { + pub(crate) cuda: bool, + pub(crate) is_assembler_msvc: bool, + pub(crate) msvc: bool, + pub(crate) clang: bool, + pub(crate) gnu: bool, + pub(crate) is_asm: bool, + pub(crate) is_arm: bool, +} + +pub(crate) fn command_add_output_file(cmd: &mut Command, dst: &Path, args: CmdAddOutputFileArgs) { + if args.is_assembler_msvc + || !(!args.msvc || args.clang || args.gnu || args.cuda || (args.is_asm && args.is_arm)) + { let mut s = OsString::from("-Fo"); s.push(dst); cmd.arg(s); @@ -393,7 +465,7 @@ pub(crate) fn command_add_output_file( #[cfg(feature = "parallel")] pub(crate) fn try_wait_on_child( cmd: &Command, - program: &str, + program: &Path, child: &mut Child, stdout: &mut dyn io::Write, stderr_forwarder: &mut StderrForwarder, @@ -412,8 +484,10 @@ pub(crate) fn try_wait_on_child( Err(Error::new( ErrorKind::ToolExecError, format!( - "Command {:?} with args {:?} did not execute successfully (status code {}).", - cmd, program, status + "Command {:?} with args {} did not execute successfully (status code {}).", + cmd, + program.display(), + status ), )) } @@ -424,8 +498,10 @@ pub(crate) fn try_wait_on_child( Err(Error::new( ErrorKind::ToolExecError, format!( - "Failed to wait on spawned child process, command {:?} with args {:?}: {}.", - cmd, program, e + "Failed to wait on spawned child process, command {:?} with args {}: {}.", + cmd, + program.display(), + e ), )) } diff --git a/third_party/rust/cc/src/detect_compiler_family.c b/third_party/rust/cc/src/detect_compiler_family.c new file mode 100644 index 00000000000000..601cee6c4878cc --- /dev/null +++ b/third_party/rust/cc/src/detect_compiler_family.c @@ -0,0 +1,15 @@ +#ifdef __clang__ +#pragma message "clang" +#endif + +#ifdef __GNUC__ +#pragma message "gcc" +#endif + +#ifdef __EMSCRIPTEN__ +#pragma message "emscripten" +#endif + +#ifdef __VXWORKS__ +#pragma message "VxWorks" +#endif diff --git a/third_party/rust/cc/src/flags.rs b/third_party/rust/cc/src/flags.rs new file mode 100644 index 00000000000000..996c647d745047 --- /dev/null +++ b/third_party/rust/cc/src/flags.rs @@ -0,0 +1,502 @@ +use crate::target::TargetInfo; +use crate::{Build, Error, ErrorKind, Tool, ToolFamily}; +use std::borrow::Cow; +use std::ffi::OsString; + +#[derive(Debug, PartialEq, Default)] +pub(crate) struct RustcCodegenFlags<'a> { + branch_protection: Option<&'a str>, + code_model: Option<&'a str>, + no_vectorize_loops: bool, + no_vectorize_slp: bool, + profile_generate: Option<&'a str>, + profile_use: Option<&'a str>, + control_flow_guard: Option<&'a str>, + lto: Option<&'a str>, + relocation_model: Option<&'a str>, + embed_bitcode: Option, + force_frame_pointers: Option, + no_redzone: Option, + soft_float: Option, +} + +impl<'this> RustcCodegenFlags<'this> { + // Parse flags obtained from CARGO_ENCODED_RUSTFLAGS + pub(crate) fn parse(rustflags_env: &'this str) -> Result { + fn is_flag_prefix(flag: &str) -> bool { + [ + "-Z", + "-C", + "--codegen", + "-L", + "-l", + "-o", + "-W", + "--warn", + "-A", + "--allow", + "-D", + "--deny", + "-F", + "--forbid", + ] + .contains(&flag) + } + + fn handle_flag_prefix<'a>(prev: &'a str, curr: &'a str) -> (&'a str, &'a str) { + match prev { + "--codegen" | "-C" => ("-C", curr), + // Handle flags passed like --codegen=code-model=small + _ if curr.starts_with("--codegen=") => ("-C", &curr[10..]), + "-Z" => ("-Z", curr), + "-L" | "-l" | "-o" => (prev, curr), + // Handle lint flags + "-W" | "--warn" => ("-W", curr), + "-A" | "--allow" => ("-A", curr), + "-D" | "--deny" => ("-D", curr), + "-F" | "--forbid" => ("-F", curr), + _ => ("", curr), + } + } + + let mut codegen_flags = Self::default(); + + let mut prev_prefix = None; + for curr in rustflags_env.split("\u{1f}") { + let prev = prev_prefix.take().unwrap_or(""); + if prev.is_empty() && is_flag_prefix(curr) { + prev_prefix = Some(curr); + continue; + } + + let (prefix, rustc_flag) = handle_flag_prefix(prev, curr); + codegen_flags.set_rustc_flag(prefix, rustc_flag)?; + } + + Ok(codegen_flags) + } + + fn set_rustc_flag(&mut self, prefix: &str, flag: &'this str) -> Result<(), Error> { + // Convert a textual representation of a bool-like rustc flag argument into an actual bool + fn arg_to_bool(arg: impl AsRef) -> Option { + match arg.as_ref() { + "y" | "yes" | "on" | "true" => Some(true), + "n" | "no" | "off" | "false" => Some(false), + _ => None, + } + } + + let (flag, value) = if let Some((flag, value)) = flag.split_once('=') { + (flag, Some(value)) + } else { + (flag, None) + }; + let flag = if prefix.is_empty() { + Cow::Borrowed(flag) + } else { + Cow::Owned(format!("{prefix}{flag}")) + }; + + fn flag_ok_or<'flag>( + flag: Option<&'flag str>, + msg: &'static str, + ) -> Result<&'flag str, Error> { + flag.ok_or(Error::new(ErrorKind::InvalidFlag, msg)) + } + + match flag.as_ref() { + // https://doc.rust-lang.org/rustc/codegen-options/index.html#code-model + "-Ccode-model" => { + self.code_model = Some(flag_ok_or(value, "-Ccode-model must have a value")?); + } + // https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-loops + "-Cno-vectorize-loops" => self.no_vectorize_loops = true, + // https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-slp + "-Cno-vectorize-slp" => self.no_vectorize_slp = true, + // https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-generate + "-Cprofile-generate" => { + self.profile_generate = + Some(flag_ok_or(value, "-Cprofile-generate must have a value")?); + } + // https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-use + "-Cprofile-use" => { + self.profile_use = Some(flag_ok_or(value, "-Cprofile-use must have a value")?); + } + // https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard + "-Ccontrol-flow-guard" => self.control_flow_guard = value.or(Some("true")), + // https://doc.rust-lang.org/rustc/codegen-options/index.html#lto + "-Clto" => self.lto = value.or(Some("true")), + // https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model + "-Crelocation-model" => { + self.relocation_model = + Some(flag_ok_or(value, "-Crelocation-model must have a value")?); + } + // https://doc.rust-lang.org/rustc/codegen-options/index.html#embed-bitcode + "-Cembed-bitcode" => self.embed_bitcode = value.map_or(Some(true), arg_to_bool), + // https://doc.rust-lang.org/rustc/codegen-options/index.html#force-frame-pointers + "-Cforce-frame-pointers" => { + self.force_frame_pointers = value.map_or(Some(true), arg_to_bool) + } + // https://doc.rust-lang.org/rustc/codegen-options/index.html#no-redzone + "-Cno-redzone" => self.no_redzone = value.map_or(Some(true), arg_to_bool), + // https://doc.rust-lang.org/rustc/codegen-options/index.html#soft-float + // Note: This flag is now deprecated in rustc. + "-Csoft-float" => self.soft_float = value.map_or(Some(true), arg_to_bool), + // https://doc.rust-lang.org/beta/unstable-book/compiler-flags/branch-protection.html + // FIXME: Drop the -Z variant and update the doc link once the option is stabilised + "-Zbranch-protection" | "-Cbranch-protection" => { + self.branch_protection = + Some(flag_ok_or(value, "-Zbranch-protection must have a value")?); + } + _ => {} + } + Ok(()) + } + + // Rust and clang/cc don't agree on what equivalent flags should look like. + pub(crate) fn cc_flags(&self, build: &Build, tool: &mut Tool, target: &TargetInfo<'_>) { + let family = tool.family; + // Push `flag` to `flags` if it is supported by the currently used CC + let mut push_if_supported = |flag: OsString| { + if build + .is_flag_supported_inner(&flag, tool, target) + .unwrap_or(false) + { + tool.args.push(flag); + } else { + build.cargo_output.print_warning(&format!( + "Inherited flag {:?} is not supported by the currently used CC", + flag + )); + } + }; + + let clang_or_gnu = + matches!(family, ToolFamily::Clang { .. }) || matches!(family, ToolFamily::Gnu { .. }); + + // Flags shared between clang and gnu + if clang_or_gnu { + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mbranch-protection + // https://gcc.gnu.org/onlinedocs/gcc/AArch64-Options.html#index-mbranch-protection (Aarch64) + // https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html#index-mbranch-protection-1 (ARM) + // https://developer.arm.com/documentation/101754/0619/armclang-Reference/armclang-Command-line-Options/-mbranch-protection + if let Some(value) = self.branch_protection { + push_if_supported( + format!("-mbranch-protection={}", value.replace(",", "+")).into(), + ); + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mcmodel + // https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html (several archs, search for `-mcmodel=`). + // FIXME(madsmtm): Parse the model, to make sure we pass the correct value (depending on arch). + if let Some(value) = self.code_model { + push_if_supported(format!("-mcmodel={value}").into()); + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-vectorize + // https://gcc.gnu.org/onlinedocs/gnat_ugn/Vectorization-of-loops.html + if self.no_vectorize_loops { + push_if_supported("-fno-vectorize".into()); + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-slp-vectorize + // https://gcc.gnu.org/onlinedocs/gnat_ugn/Vectorization-of-loops.html + if self.no_vectorize_slp { + push_if_supported("-fno-slp-vectorize".into()); + } + if let Some(value) = self.relocation_model { + let cc_flag = match value { + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIC + // https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fPIC + "pic" => Some("-fPIC"), + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIE + // https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fPIE + "pie" => Some("-fPIE"), + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mdynamic-no-pic + // https://gcc.gnu.org/onlinedocs/gcc/RS_002f6000-and-PowerPC-Options.html#index-mdynamic-no-pic + "dynamic-no-pic" => Some("-mdynamic-no-pic"), + _ => None, + }; + if let Some(cc_flag) = cc_flag { + push_if_supported(cc_flag.into()); + } + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-omit-frame-pointer + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fomit-frame-pointer + // https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fomit-frame-pointer + if let Some(value) = self.force_frame_pointers { + let cc_flag = if value { + "-fno-omit-frame-pointer" + } else { + "-fomit-frame-pointer" + }; + push_if_supported(cc_flag.into()); + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-red-zone + // https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#index-mno-red-zone + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mred-zone + // https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#index-mred-zone + if let Some(value) = self.no_redzone { + let cc_flag = if value { "-mno-red-zone" } else { "-mred-zone" }; + push_if_supported(cc_flag.into()); + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-msoft-float + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mhard-float + // https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html (several archs, search for `-msoft-float`). + // https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html (several archs, search for `-mhard-float`). + if let Some(value) = self.soft_float { + let cc_flag = if value { + "-msoft-float" + } else { + // Do not use -mno-soft-float, that's basically just an alias for -mno-implicit-float. + "-mhard-float" + }; + push_if_supported(cc_flag.into()); + } + } + + // Compiler-exclusive flags + match family { + ToolFamily::Clang { .. } => { + // GNU and Clang compilers both support the same PGO flags, but they use different libraries and + // different formats for the profile files which are not compatible. + // clang and rustc both internally use llvm, so we want to inherit the PGO flags only for clang. + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-generate + if let Some(value) = self.profile_generate { + push_if_supported(format!("-fprofile-generate={value}").into()); + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-use + if let Some(value) = self.profile_use { + push_if_supported(format!("-fprofile-use={value}").into()); + } + + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fembed-bitcode + if let Some(value) = self.embed_bitcode { + let cc_val = if value { "all" } else { "off" }; + push_if_supported(format!("-fembed-bitcode={cc_val}").into()); + } + + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto + if let Some(value) = self.lto { + let cc_val = match value { + "y" | "yes" | "on" | "true" | "fat" => Some("full"), + "thin" => Some("thin"), + _ => None, + }; + if let Some(cc_val) = cc_val { + push_if_supported(format!("-flto={cc_val}").into()); + } + } + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard + if let Some(value) = self.control_flow_guard { + let cc_val = match value { + "y" | "yes" | "on" | "true" | "checks" => Some("cf"), + "nochecks" => Some("cf-nochecks"), + "n" | "no" | "off" | "false" => Some("none"), + _ => None, + }; + if let Some(cc_val) = cc_val { + push_if_supported(format!("-mguard={cc_val}").into()); + } + } + } + ToolFamily::Gnu { .. } => {} + ToolFamily::Msvc { .. } => { + // https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard + if let Some(value) = self.control_flow_guard { + let cc_val = match value { + "y" | "yes" | "on" | "true" | "checks" => Some("cf"), + "n" | "no" | "off" | "false" => Some("cf-"), + _ => None, + }; + if let Some(cc_val) = cc_val { + push_if_supported(format!("/guard:{cc_val}").into()); + } + } + // https://learn.microsoft.com/en-us/cpp/build/reference/oy-frame-pointer-omission + if let Some(value) = self.force_frame_pointers { + // Flag is unsupported on 64-bit arches + if !target.arch.contains("64") { + let cc_flag = if value { "/Oy-" } else { "/Oy" }; + push_if_supported(cc_flag.into()); + } + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[track_caller] + fn check(env: &str, expected: &RustcCodegenFlags) { + let actual = RustcCodegenFlags::parse(env).unwrap(); + assert_eq!(actual, *expected); + } + + #[test] + fn codegen_type() { + let expected = RustcCodegenFlags { + code_model: Some("tiny"), + ..RustcCodegenFlags::default() + }; + check("-Ccode-model=tiny", &expected); + check("-C\u{1f}code-model=tiny", &expected); + check("--codegen\u{1f}code-model=tiny", &expected); + check("--codegen=code-model=tiny", &expected); + } + + #[test] + fn precedence() { + check( + "-ccode-model=tiny\u{1f}-Ccode-model=small", + &RustcCodegenFlags { + code_model: Some("small"), + ..RustcCodegenFlags::default() + }, + ); + } + + #[test] + fn two_valid_prefixes() { + let expected = RustcCodegenFlags::default(); + check("-L\u{1f}-Clto", &expected); + } + + #[test] + fn three_valid_prefixes() { + let expected = RustcCodegenFlags { + lto: Some("true"), + ..RustcCodegenFlags::default() + }; + check("-L\u{1f}-L\u{1f}-Clto", &expected); + } + + #[test] + fn all_rustc_flags() { + // Throw all possible flags at the parser to catch false positives + let flags = [ + // Set all the flags we recognise first + "-Ccode-model=tiny", + "-Ccontrol-flow-guard=yes", + "-Cembed-bitcode=no", + "-Cforce-frame-pointers=yes", + "-Clto=false", + "-Clink-dead-code=yes", + "-Cno-redzone=yes", + "-Cno-vectorize-loops", + "-Cno-vectorize-slp", + "-Cprofile-generate=fooprofile", + "-Cprofile-use=fooprofile", + "-Crelocation-model=pic", + "-Csoft-float=yes", + "-Zbranch-protection=bti,pac-ret,leaf", + // Set flags we don't recognise but rustc supports next + // rustc flags + "--cfg", + "a", + "--check-cfg 'cfg(verbose)", + "-L", + "/usr/lib/foo", + "-l", + "static:+whole-archive=mylib", + "--crate-type=dylib", + "--crate-name=foo", + "--edition=2021", + "--emit=asm", + "--print=crate-name", + "-g", + "-O", + "-o", + "foooutput", + "--out-dir", + "foooutdir", + "--target", + "aarch64-unknown-linux-gnu", + "-W", + "missing-docs", + "-D", + "unused-variables", + "--force-warn", + "dead-code", + "-A", + "unused", + "-F", + "unused", + "--cap-lints", + "warn", + "--version", + "--verbose", + "-v", + "--extern", + "foocrate", + "--sysroot", + "fooroot", + "--error-format", + "human", + "--color", + "auto", + "--diagnostic-width", + "80", + "--remap-path-prefix", + "foo=bar", + "--json=artifact", + // Codegen flags + "-Car", + "-Ccodegen-units=1", + "-Ccollapse-macro-debuginfo=yes", + "-Cdebug-assertions=yes", + "-Cdebuginfo=1", + "-Cdefault-linker-libraries=yes", + "-Cdlltool=foo", + "-Cextra-filename=foo", + "-Cforce-unwind-tables=yes", + "-Cincremental=foodir", + "-Cinline-threshold=6", + "-Cinstrument-coverage", + "-Clink-arg=-foo", + "-Clink-args=-foo", + "-Clink-self-contained=yes", + "-Clinker=lld", + "-Clinker-flavor=ld.lld", + "-Clinker-plugin-lto=yes", + "-Cllvm-args=foo", + "-Cmetadata=foo", + "-Cno-prepopulate-passes", + "-Cno-stack-check", + "-Copt-level=3", + "-Coverflow-checks=yes", + "-Cpanic=abort", + "-Cpasses=foopass", + "-Cprefer-dynamic=yes", + "-Crelro-level=partial", + "-Cremark=all", + "-Crpath=yes", + "-Csave-temps=yes", + "-Csplit-debuginfo=packed", + "-Cstrip=symbols", + "-Csymbol-mangling-version=v0", + "-Ctarget-cpu=native", + "-Ctarget-feature=+sve", + // Unstable options + "-Ztune-cpu=machine", + ]; + check( + &flags.join("\u{1f}"), + &RustcCodegenFlags { + code_model: Some("tiny"), + control_flow_guard: Some("yes"), + embed_bitcode: Some(false), + force_frame_pointers: Some(true), + lto: Some("false"), + no_redzone: Some(true), + no_vectorize_loops: true, + no_vectorize_slp: true, + profile_generate: Some("fooprofile"), + profile_use: Some("fooprofile"), + relocation_model: Some("pic"), + soft_float: Some(true), + branch_protection: Some("bti,pac-ret,leaf"), + }, + ); + } +} diff --git a/third_party/rust/cc/src/lib.rs b/third_party/rust/cc/src/lib.rs index 1b193dbd0a3bc9..0631584e9552ed 100644 --- a/third_party/rust/cc/src/lib.rs +++ b/third_party/rust/cc/src/lib.rs @@ -19,13 +19,10 @@ //! //! ```rust,no_run //! // build.rs -//! -//! fn main() { -//! cc::Build::new() -//! .file("foo.c") -//! .file("bar.c") -//! .compile("foo"); -//! } +//! cc::Build::new() +//! .file("foo.c") +//! .file("bar.c") +//! .compile("foo"); //! ``` //! //! And that's it! Running `cargo build` should take care of the rest and your Rust @@ -71,23 +68,34 @@ //! number of different environment variables. //! //! * `CFLAGS` - a series of space separated flags passed to compilers. Note that -//! individual flags cannot currently contain spaces, so doing -//! something like: `-L=foo\ bar` is not possible. +//! individual flags cannot currently contain spaces, so doing +//! something like: `-L=foo\ bar` is not possible. //! * `CC` - the actual C compiler used. Note that this is used as an exact -//! executable name, so (for example) no extra flags can be passed inside -//! this variable, and the builder must ensure that there aren't any -//! trailing spaces. This compiler must understand the `-c` flag. For -//! certain `TARGET`s, it also is assumed to know about other flags (most -//! common is `-fPIC`). +//! executable name, so (for example) no extra flags can be passed inside +//! this variable, and the builder must ensure that there aren't any +//! trailing spaces. This compiler must understand the `-c` flag. For +//! certain `TARGET`s, it also is assumed to know about other flags (most +//! common is `-fPIC`). //! * `AR` - the `ar` (archiver) executable to use to build the static library. //! * `CRATE_CC_NO_DEFAULTS` - the default compiler flags may cause conflicts in -//! some cross compiling scenarios. Setting this variable -//! will disable the generation of default compiler -//! flags. +//! some cross compiling scenarios. Setting this variable +//! will disable the generation of default compiler +//! flags. //! * `CC_ENABLE_DEBUG_OUTPUT` - if set, compiler command invocations and exit codes will -//! be logged to stdout. This is useful for debugging build script issues, but can be -//! overly verbose for normal use. +//! be logged to stdout. This is useful for debugging build script issues, but can be +//! overly verbose for normal use. +//! * `CC_SHELL_ESCAPED_FLAGS` - if set, `*FLAGS` will be parsed as if they were shell +//! arguments (similar to `make` and `cmake`) rather than splitting them on each space. +//! For example, with `CFLAGS='a "b c"'`, the compiler will be invoked with 2 arguments - +//! `a` and `b c` - rather than 3: `a`, `"b` and `c"`. //! * `CXX...` - see [C++ Support](#c-support). +//! * `CC_FORCE_DISABLE` - If set, `cc` will never run any [`Command`]s, and methods that +//! would return an [`Error`]. This is intended for use by third-party build systems +//! which want to be absolutely sure that they are in control of building all +//! dependencies. Note that operations that return [`Tool`]s such as +//! [`Build::get_compiler`] may produce less accurate results as in some cases `cc` runs +//! commands in order to locate compilers. Additionally, this does nothing to prevent +//! users from running [`Tool::to_command`] and executing the [`Command`] themselves.//! //! //! Furthermore, projects using this crate may specify custom environment variables //! to be inspected, for example via the `Build::try_flags_from_environment` @@ -136,22 +144,21 @@ //! required varies per platform, but there are three broad categories: //! //! * Unix platforms require `cc` to be the C compiler. This can be found by -//! installing cc/clang on Linux distributions and Xcode on macOS, for example. +//! installing cc/clang on Linux distributions and Xcode on macOS, for example. //! * Windows platforms targeting MSVC (e.g. your target triple ends in `-msvc`) -//! require Visual Studio to be installed. `cc-rs` attempts to locate it, and -//! if it fails, `cl.exe` is expected to be available in `PATH`. This can be -//! set up by running the appropriate developer tools shell. +//! require Visual Studio to be installed. `cc-rs` attempts to locate it, and +//! if it fails, `cl.exe` is expected to be available in `PATH`. This can be +//! set up by running the appropriate developer tools shell. //! * Windows platforms targeting MinGW (e.g. your target triple ends in `-gnu`) -//! require `cc` to be available in `PATH`. We recommend the -//! [MinGW-w64](https://www.mingw-w64.org/) distribution, which is using the -//! [Win-builds](http://win-builds.org/) installation system. -//! You may also acquire it via -//! [MSYS2](https://www.msys2.org/), as explained [here][msys2-help]. Make sure -//! to install the appropriate architecture corresponding to your installation of -//! rustc. GCC from older [MinGW](http://www.mingw.org/) project is compatible -//! only with 32-bit rust compiler. +//! require `cc` to be available in `PATH`. We recommend the +//! [MinGW-w64](https://www.mingw-w64.org/) distribution. +//! You may also acquire it via +//! [MSYS2](https://www.msys2.org/), as explained [here][msys2-help]. Make sure +//! to install the appropriate architecture corresponding to your installation of +//! rustc. GCC from older [MinGW](http://www.mingw.org/) project is compatible +//! only with 32-bit rust compiler. //! -//! [msys2-help]: https://github.com/rust-lang/rust#building-on-windows +//! [msys2-help]: https://github.com/rust-lang/rust/blob/master/INSTALL.md#building-on-windows //! //! # C++ support //! @@ -159,12 +166,10 @@ //! `Build`: //! //! ```rust,no_run -//! fn main() { -//! cc::Build::new() -//! .cpp(true) // Switch to C++ library compilation. -//! .file("foo.cpp") -//! .compile("foo"); -//! } +//! cc::Build::new() +//! .cpp(true) // Switch to C++ library compilation. +//! .file("foo.cpp") +//! .compile("foo"); //! ``` //! //! For C++ libraries, the `CXX` and `CXXFLAGS` environment variables are used instead of `CC` and `CFLAGS`. @@ -173,13 +178,11 @@ //! //! 1. by using the `cpp_link_stdlib` method on `Build`: //! ```rust,no_run -//! fn main() { -//! cc::Build::new() -//! .cpp(true) -//! .file("foo.cpp") -//! .cpp_link_stdlib("stdc++") // use libstdc++ -//! .compile("foo"); -//! } +//! cc::Build::new() +//! .cpp(true) +//! .file("foo.cpp") +//! .cpp_link_stdlib("stdc++") // use libstdc++ +//! .compile("foo"); //! ``` //! 2. by setting the `CXXSTDLIB` environment variable. //! @@ -193,49 +196,55 @@ //! on `Build`: //! //! ```rust,no_run -//! fn main() { -//! cc::Build::new() -//! // Switch to CUDA C++ library compilation using NVCC. -//! .cuda(true) -//! .cudart("static") -//! // Generate code for Maxwell (GTX 970, 980, 980 Ti, Titan X). -//! .flag("-gencode").flag("arch=compute_52,code=sm_52") -//! // Generate code for Maxwell (Jetson TX1). -//! .flag("-gencode").flag("arch=compute_53,code=sm_53") -//! // Generate code for Pascal (GTX 1070, 1080, 1080 Ti, Titan Xp). -//! .flag("-gencode").flag("arch=compute_61,code=sm_61") -//! // Generate code for Pascal (Tesla P100). -//! .flag("-gencode").flag("arch=compute_60,code=sm_60") -//! // Generate code for Pascal (Jetson TX2). -//! .flag("-gencode").flag("arch=compute_62,code=sm_62") -//! // Generate code in parallel -//! .flag("-t0") -//! .file("bar.cu") -//! .compile("bar"); -//! } +//! cc::Build::new() +//! // Switch to CUDA C++ library compilation using NVCC. +//! .cuda(true) +//! .cudart("static") +//! // Generate code for Maxwell (GTX 970, 980, 980 Ti, Titan X). +//! .flag("-gencode").flag("arch=compute_52,code=sm_52") +//! // Generate code for Maxwell (Jetson TX1). +//! .flag("-gencode").flag("arch=compute_53,code=sm_53") +//! // Generate code for Pascal (GTX 1070, 1080, 1080 Ti, Titan Xp). +//! .flag("-gencode").flag("arch=compute_61,code=sm_61") +//! // Generate code for Pascal (Tesla P100). +//! .flag("-gencode").flag("arch=compute_60,code=sm_60") +//! // Generate code for Pascal (Jetson TX2). +//! .flag("-gencode").flag("arch=compute_62,code=sm_62") +//! // Generate code in parallel +//! .flag("-t0") +//! .file("bar.cu") +//! .compile("bar"); //! ``` #![doc(html_root_url = "https://docs.rs/cc/1.0")] -#![cfg_attr(test, deny(warnings))] -#![allow(deprecated)] +#![deny(warnings)] #![deny(missing_docs)] +#![deny(clippy::disallowed_methods)] +#![warn(clippy::doc_markdown)] use std::borrow::Cow; use std::collections::HashMap; use std::env; use std::ffi::{OsStr, OsString}; -use std::fmt::{self, Display, Formatter}; +use std::fmt::{self, Display}; use std::fs; use std::io::{self, Write}; use std::path::{Component, Path, PathBuf}; #[cfg(feature = "parallel")] use std::process::Child; use std::process::Command; -use std::sync::{Arc, Mutex}; +use std::sync::{ + atomic::{AtomicU8, Ordering::Relaxed}, + Arc, RwLock, +}; + +use shlex::Shlex; #[cfg(feature = "parallel")] mod parallel; +mod target; mod windows; +use self::target::TargetInfo; // Regardless of whether this should be in this crate's public API, // it has been since 2015, so don't break it. pub use windows::find_tools as windows_registry; @@ -245,7 +254,33 @@ use command_helpers::*; mod tool; pub use tool::Tool; -use tool::ToolFamily; +use tool::{CompilerFamilyLookupCache, ToolFamily}; + +mod tempfile; + +mod utilities; +use utilities::*; + +mod flags; +use flags::*; + +#[derive(Debug, Eq, PartialEq, Hash)] +struct CompilerFlag { + compiler: Box, + flag: Box, +} + +type Env = Option>; + +#[derive(Debug, Default)] +struct BuildCache { + env_cache: RwLock, Env>>, + apple_sdk_root_cache: RwLock, Arc>>, + apple_versions_cache: RwLock, Arc>>, + cached_compiler_family: RwLock, + known_flag_support_status_cache: RwLock>, + target_info_parser: target::TargetInfoParser, +} /// A builder for compilation of a native library. /// @@ -257,11 +292,10 @@ pub struct Build { include_directories: Vec>, definitions: Vec<(Arc, Option>)>, objects: Vec>, - flags: Vec>, - flags_supported: Vec>, - known_flag_support_status: Arc>>, - ar_flags: Vec>, - asm_flags: Vec>, + flags: Vec>, + flags_supported: Vec>, + ar_flags: Vec>, + asm_flags: Vec>, no_default_flags: bool, files: Vec>, cpp: bool, @@ -269,8 +303,12 @@ pub struct Build { cpp_set_stdlib: Option>, cuda: bool, cudart: Option>, + ccbin: bool, std: Option>, target: Option>, + /// The host compiler. + /// + /// Try to not access this directly, and instead prefer `cfg!(...)`. host: Option>, out_dir: Option>, opt_level: Option>, @@ -281,7 +319,7 @@ pub struct Build { archiver: Option>, ranlib: Option>, cargo_output: CargoOutput, - link_lib_modifiers: Vec>, + link_lib_modifiers: Vec>, pic: Option, use_plt: Option, static_crt: Option, @@ -290,11 +328,10 @@ pub struct Build { warnings_into_errors: bool, warnings: Option, extra_warnings: Option, - env_cache: Arc>>>>, - apple_sdk_root_cache: Arc>>, - apple_versions_cache: Arc>>, emit_rerun_if_env_changed: bool, - cached_compiler_family: Arc, ToolFamily>>>, + shell_escaped_flags: Option, + build_cache: Arc, + inherit_rustflags: bool, } /// Represents the types of errors that may occur while using cc-rs. @@ -302,8 +339,6 @@ pub struct Build { enum ErrorKind { /// Error occurred while performing I/O. IOError, - /// Invalid architecture supplied. - ArchitectureInvalid, /// Environment variable not found, with the var in question as extra info. EnvVarNotFound, /// Error occurred while using external tools (ie: invocation of compiler). @@ -312,9 +347,19 @@ enum ErrorKind { ToolNotFound, /// One of the function arguments failed validation. InvalidArgument, + /// No known macro is defined for the compiler when discovering tool family. + ToolFamilyMacroNotFound, + /// Invalid target. + InvalidTarget, + /// Unknown target. + UnknownTarget, + /// Invalid rustc flag. + InvalidFlag, #[cfg(feature = "parallel")] /// jobserver helpthread failure JobserverHelpThreadError, + /// `cc` has been disabled by an environment variable. + Disabled, } /// Represents an internal error that occurred, with an explanation. @@ -365,6 +410,7 @@ impl Object { } } +/// Configure the builder. impl Build { /// Construct a new instance of a blank set of configuration. /// @@ -378,7 +424,6 @@ impl Build { objects: Vec::new(), flags: Vec::new(), flags_supported: Vec::new(), - known_flag_support_status: Arc::new(Mutex::new(HashMap::new())), ar_flags: Vec::new(), asm_flags: Vec::new(), no_default_flags: false, @@ -390,6 +435,7 @@ impl Build { cpp_set_stdlib: None, cuda: false, cudart: None, + ccbin: true, std: None, target: None, host: None, @@ -409,11 +455,10 @@ impl Build { warnings: None, extra_warnings: None, warnings_into_errors: false, - env_cache: Arc::new(Mutex::new(HashMap::new())), - apple_sdk_root_cache: Arc::new(Mutex::new(HashMap::new())), - apple_versions_cache: Arc::new(Mutex::new(HashMap::new())), emit_rerun_if_env_changed: true, - cached_compiler_family: Arc::default(), + shell_escaped_flags: None, + build_cache: Arc::default(), + inherit_rustflags: true, } } @@ -489,6 +534,18 @@ impl Build { self } + /// Add arbitrary object files to link in + pub fn objects

(&mut self, objs: P) -> &mut Build + where + P: IntoIterator, + P::Item: AsRef, + { + for obj in objs { + self.object(obj); + } + self + } + /// Add an arbitrary flag to the invocation of the compiler /// /// # Example @@ -499,8 +556,8 @@ impl Build { /// .flag("-ffunction-sections") /// .compile("foo"); /// ``` - pub fn flag(&mut self, flag: &str) -> &mut Build { - self.flags.push(flag.into()); + pub fn flag(&mut self, flag: impl AsRef) -> &mut Build { + self.flags.push(flag.as_ref().into()); self } @@ -516,7 +573,6 @@ impl Build { /// .flag("unwanted_flag") /// .remove_flag("unwanted_flag"); /// ``` - pub fn remove_flag(&mut self, flag: &str) -> &mut Build { self.flags.retain(|other_flag| &**other_flag != flag); self @@ -533,8 +589,8 @@ impl Build { /// .ar_flag("/NODEFAULTLIB:libc.dll") /// .compile("foo"); /// ``` - pub fn ar_flag(&mut self, flag: &str) -> &mut Build { - self.ar_flags.push(flag.into()); + pub fn ar_flag(&mut self, flag: impl AsRef) -> &mut Build { + self.ar_flags.push(flag.as_ref().into()); self } @@ -552,104 +608,11 @@ impl Build { /// .file("src/bar.c") // The asm flag will not be applied here /// .compile("foo"); /// ``` - pub fn asm_flag(&mut self, flag: &str) -> &mut Build { - self.asm_flags.push(flag.into()); + pub fn asm_flag(&mut self, flag: impl AsRef) -> &mut Build { + self.asm_flags.push(flag.as_ref().into()); self } - fn ensure_check_file(&self) -> Result { - let out_dir = self.get_out_dir()?; - let src = if self.cuda { - assert!(self.cpp); - out_dir.join("flag_check.cu") - } else if self.cpp { - out_dir.join("flag_check.cpp") - } else { - out_dir.join("flag_check.c") - }; - - if !src.exists() { - let mut f = fs::File::create(&src)?; - write!(f, "int main(void) {{ return 0; }}")?; - } - - Ok(src) - } - - /// Run the compiler to test if it accepts the given flag. - /// - /// For a convenience method for setting flags conditionally, - /// see `flag_if_supported()`. - /// - /// It may return error if it's unable to run the compiler with a test file - /// (e.g. the compiler is missing or a write to the `out_dir` failed). - /// - /// Note: Once computed, the result of this call is stored in the - /// `known_flag_support` field. If `is_flag_supported(flag)` - /// is called again, the result will be read from the hash table. - pub fn is_flag_supported(&self, flag: &str) -> Result { - let mut known_status = self.known_flag_support_status.lock().unwrap(); - if let Some(is_supported) = known_status.get(flag).cloned() { - return Ok(is_supported); - } - - let out_dir = self.get_out_dir()?; - let src = self.ensure_check_file()?; - let obj = out_dir.join("flag_check"); - let target = self.get_target()?; - let host = self.get_host()?; - let mut cfg = Build::new(); - cfg.flag(flag) - .cargo_metadata(self.cargo_output.metadata) - .target(&target) - .opt_level(0) - .host(&host) - .debug(false) - .cpp(self.cpp) - .cuda(self.cuda); - if let Some(ref c) = self.compiler { - cfg.compiler(c.clone()); - } - let mut compiler = cfg.try_get_compiler()?; - - // Clang uses stderr for verbose output, which yields a false positive - // result if the CFLAGS/CXXFLAGS include -v to aid in debugging. - if compiler.family.verbose_stderr() { - compiler.remove_arg("-v".into()); - } - if compiler.family == ToolFamily::Clang { - // Avoid reporting that the arg is unsupported just because the - // compiler complains that it wasn't used. - compiler.push_cc_arg("-Wno-unused-command-line-argument".into()); - } - - let mut cmd = compiler.to_command(); - let is_arm = target.contains("aarch64") || target.contains("arm"); - let clang = compiler.family == ToolFamily::Clang; - let gnu = compiler.family == ToolFamily::Gnu; - command_add_output_file( - &mut cmd, - &obj, - self.cuda, - target.contains("msvc"), - clang, - gnu, - false, - is_arm, - ); - - // Checking for compiler flags does not require linking - cmd.arg("-c"); - - cmd.arg(&src); - - let output = cmd.output()?; - let is_supported = output.status.success() && output.stderr.is_empty(); - - known_status.insert(flag.to_owned(), is_supported); - Ok(is_supported) - } - /// Add an arbitrary flag to the invocation of the compiler if it supports it /// /// # Example @@ -661,8 +624,8 @@ impl Build { /// .flag_if_supported("-Wunreachable-code") // only supported by clang /// .compile("foo"); /// ``` - pub fn flag_if_supported(&mut self, flag: &str) -> &mut Build { - self.flags_supported.push(flag.into()); + pub fn flag_if_supported(&mut self, flag: impl AsRef) -> &mut Build { + self.flags_supported.push(flag.as_ref().into()); self } @@ -691,7 +654,11 @@ impl Build { /// pub fn try_flags_from_environment(&mut self, environ_key: &str) -> Result<&mut Build, Error> { let flags = self.envflags(environ_key)?; - self.flags.extend(flags.into_iter().map(Into::into)); + self.flags.extend( + flags + .into_iter() + .map(|flag| Arc::from(OsString::from(flag).as_os_str())), + ); Ok(self) } @@ -774,8 +741,8 @@ impl Build { /// The name of the C++ standard library to link is decided by: /// 1. If [`cpp_link_stdlib`](Build::cpp_link_stdlib) is set, use its value. /// 2. Else if the `CXXSTDLIB` environment variable is set, use its value. - /// 3. Else the default is `libc++` for OS X and BSDs, `libc++_shared` for Android, - /// `None` for MSVC and `libstdc++` for anything else. + /// 3. Else the default is `c++` for OS X and BSDs, `c++_shared` for Android, + /// `None` for MSVC and `stdc++` for anything else. pub fn cpp(&mut self, cpp: bool) -> &mut Build { self.cpp = cpp; self @@ -813,6 +780,18 @@ impl Build { self } + /// Set CUDA host compiler. + /// + /// By default, a `-ccbin` flag will be passed to NVCC to specify the + /// underlying host compiler. The value of `-ccbin` is the same as the + /// chosen C++ compiler. This is not always desired, because NVCC might + /// not support that compiler. In this case, you can remove the `-ccbin` + /// flag so that NVCC will choose the host compiler by itself. + pub fn ccbin(&mut self, ccbin: bool) -> &mut Build { + self.ccbin = ccbin; + self + } + /// Specify the C or C++ language standard version. /// /// These values are common to modern versions of GCC, Clang and MSVC: @@ -942,7 +921,7 @@ impl Build { &mut self, cpp_link_stdlib: V, ) -> &mut Build { - self.cpp_link_stdlib = Some(cpp_link_stdlib.into().map(|s| s.into())); + self.cpp_link_stdlib = Some(cpp_link_stdlib.into().map(Arc::from)); self } @@ -983,16 +962,22 @@ impl Build { &mut self, cpp_set_stdlib: V, ) -> &mut Build { - let cpp_set_stdlib = cpp_set_stdlib.into(); - self.cpp_set_stdlib = cpp_set_stdlib.map(|s| s.into()); - self.cpp_link_stdlib(cpp_set_stdlib); + let cpp_set_stdlib = cpp_set_stdlib.into().map(Arc::from); + self.cpp_set_stdlib.clone_from(&cpp_set_stdlib); + self.cpp_link_stdlib = Some(cpp_set_stdlib); self } - /// Configures the target this configuration will be compiling for. + /// Configures the `rustc` target this configuration will be compiling + /// for. /// - /// This option is automatically scraped from the `TARGET` environment - /// variable by build scripts, so it's not required to call this function. + /// This will fail if using a target not in a pre-compiled list taken from + /// `rustc +nightly --print target-list`. The list will be updated + /// periodically. + /// + /// You should avoid setting this in build scripts, target information + /// will instead be retrieved from the environment variables `TARGET` and + /// `CARGO_CFG_TARGET_*` that Cargo sets. /// /// # Example /// @@ -1140,13 +1125,28 @@ impl Build { self } + /// Define whether compiler output (to stdout) should be emitted. Defaults to `true` + /// (forward compiler stdout to this process' stdout) + /// + /// Some compilers emit errors to stdout, so if you *really* need stdout to be clean + /// you should also set this to `false`. + pub fn cargo_output(&mut self, cargo_output: bool) -> &mut Build { + self.cargo_output.output = if cargo_output { + OutputKind::Forward + } else { + OutputKind::Discard + }; + self + } + /// Adds a native library modifier that will be added to the /// `rustc-link-lib=static:MODIFIERS=LIBRARY_NAME` metadata line /// emitted for cargo if `cargo_metadata` is enabled. /// See /// for the list of modifiers accepted by rustc. - pub fn link_lib_modifier(&mut self, link_lib_modifier: &str) -> &mut Build { - self.link_lib_modifiers.push(link_lib_modifier.into()); + pub fn link_lib_modifier(&mut self, link_lib_modifier: impl AsRef) -> &mut Build { + self.link_lib_modifiers + .push(link_lib_modifier.as_ref().into()); self } @@ -1177,6 +1177,17 @@ impl Build { /// Define whether metadata should be emitted for cargo to detect environment /// changes that should trigger a rebuild. /// + /// NOTE that cc does not emit metadata to detect changes for `PATH`, since it could + /// be changed every comilation yet does not affect the result of compilation + /// (i.e. rust-analyzer adds temporary directory to `PATH`). + /// + /// cc in general, has no way detecting changes to compiler, as there are so many ways to + /// change it and sidestep the detection, for example the compiler might be wrapped in a script + /// so detecting change of the file, or using checksum won't work. + /// + /// We recommend users to decide for themselves, if they want rebuild if the compiler has been upgraded + /// or changed, and how to detect that. + /// /// This has no effect if the `cargo_metadata` option is `false`. /// /// This option defaults to `true`. @@ -1193,6 +1204,24 @@ impl Build { self } + /// Configure whether *FLAGS variables are parsed using `shlex`, similarly to `make` and + /// `cmake`. + /// + /// This option defaults to `false`. + pub fn shell_escaped_flags(&mut self, shell_escaped_flags: bool) -> &mut Build { + self.shell_escaped_flags = Some(shell_escaped_flags); + self + } + + /// Configure whether cc should automatically inherit compatible flags passed to rustc + /// from `CARGO_ENCODED_RUSTFLAGS`. + /// + /// This option defaults to `true`. + pub fn inherit_rustflags(&mut self, inherit_rustflags: bool) -> &mut Build { + self.inherit_rustflags = inherit_rustflags; + self + } + #[doc(hidden)] pub fn __set_env(&mut self, a: A, b: B) -> &mut Build where @@ -1202,10 +1231,152 @@ impl Build { self.env.push((a.as_ref().into(), b.as_ref().into())); self } +} + +/// Invoke or fetch the compiler or archiver. +impl Build { + /// Run the compiler to test if it accepts the given flag. + /// + /// For a convenience method for setting flags conditionally, + /// see `flag_if_supported()`. + /// + /// It may return error if it's unable to run the compiler with a test file + /// (e.g. the compiler is missing or a write to the `out_dir` failed). + /// + /// Note: Once computed, the result of this call is stored in the + /// `known_flag_support` field. If `is_flag_supported(flag)` + /// is called again, the result will be read from the hash table. + pub fn is_flag_supported(&self, flag: impl AsRef) -> Result { + self.is_flag_supported_inner( + flag.as_ref(), + &self.get_base_compiler()?, + &self.get_target()?, + ) + } + + fn ensure_check_file(&self) -> Result { + let out_dir = self.get_out_dir()?; + let src = if self.cuda { + assert!(self.cpp); + out_dir.join("flag_check.cu") + } else if self.cpp { + out_dir.join("flag_check.cpp") + } else { + out_dir.join("flag_check.c") + }; + + if !src.exists() { + let mut f = fs::File::create(&src)?; + write!(f, "int main(void) {{ return 0; }}")?; + } + + Ok(src) + } + + fn is_flag_supported_inner( + &self, + flag: &OsStr, + tool: &Tool, + target: &TargetInfo<'_>, + ) -> Result { + let compiler_flag = CompilerFlag { + compiler: tool.path().into(), + flag: flag.into(), + }; + + if let Some(is_supported) = self + .build_cache + .known_flag_support_status_cache + .read() + .unwrap() + .get(&compiler_flag) + .cloned() + { + return Ok(is_supported); + } + + let out_dir = self.get_out_dir()?; + let src = self.ensure_check_file()?; + let obj = out_dir.join("flag_check"); + + let mut compiler = { + let mut cfg = Build::new(); + cfg.flag(flag) + .compiler(tool.path()) + .cargo_metadata(self.cargo_output.metadata) + .opt_level(0) + .debug(false) + .cpp(self.cpp) + .cuda(self.cuda) + .inherit_rustflags(false) + .emit_rerun_if_env_changed(self.emit_rerun_if_env_changed); + if let Some(target) = &self.target { + cfg.target(target); + } + if let Some(host) = &self.host { + cfg.host(host); + } + cfg.try_get_compiler()? + }; + + // Clang uses stderr for verbose output, which yields a false positive + // result if the CFLAGS/CXXFLAGS include -v to aid in debugging. + if compiler.family.verbose_stderr() { + compiler.remove_arg("-v".into()); + } + if compiler.is_like_clang() { + // Avoid reporting that the arg is unsupported just because the + // compiler complains that it wasn't used. + compiler.push_cc_arg("-Wno-unused-command-line-argument".into()); + } + + let mut cmd = compiler.to_command(); + let is_arm = matches!(target.arch, "aarch64" | "arm"); + let clang = compiler.is_like_clang(); + let gnu = compiler.family == ToolFamily::Gnu; + command_add_output_file( + &mut cmd, + &obj, + CmdAddOutputFileArgs { + cuda: self.cuda, + is_assembler_msvc: false, + msvc: compiler.is_like_msvc(), + clang, + gnu, + is_asm: false, + is_arm, + }, + ); + + if compiler.supports_path_delimiter() { + cmd.arg("--"); + } + + cmd.arg(&src); + + // On MSVC skip the CRT by setting the entry point to `main`. + // This way we don't need to add the default library paths. + if compiler.is_like_msvc() { + // Flags from _LINK_ are appended to the linker arguments. + cmd.env("_LINK_", "-entry:main"); + } + + let output = cmd.output()?; + let is_supported = output.status.success() && output.stderr.is_empty(); + + self.build_cache + .known_flag_support_status_cache + .write() + .unwrap() + .insert(compiler_flag, is_supported); + + Ok(is_supported) + } /// Run the compiler, generating the file `output` /// - /// This will return a result instead of panicking; see compile() for the complete description. + /// This will return a result instead of panicking; see [`Self::compile()`] for + /// the complete description. pub fn try_compile(&self, output: &str) -> Result<(), Error> { let mut output_components = Path::new(output).components(); match (output_components.next(), output_components.next()) { @@ -1234,7 +1405,8 @@ impl Build { self.compile_objects(&objects)?; self.assemble(lib_name, &dst.join(gnu_lib_name), &objects)?; - if self.get_target()?.contains("msvc") { + let target = self.get_target()?; + if target.env == "msvc" { let compiler = self.get_base_compiler()?; let atlmfc_lib = compiler .env() @@ -1259,10 +1431,13 @@ impl Build { self.cargo_output .print_metadata(&format_args!("cargo:rustc-link-lib=static={}", lib_name)); } else { - let m = self.link_lib_modifiers.join(","); self.cargo_output.print_metadata(&format_args!( "cargo:rustc-link-lib=static:{}={}", - m, lib_name + JoinOsStrs { + slice: &self.link_lib_modifiers, + delimiter: ',' + }, + lib_name )); } self.cargo_output.print_metadata(&format_args!( @@ -1274,16 +1449,26 @@ impl Build { if self.cpp { if let Some(stdlib) = self.get_cpp_link_stdlib()? { self.cargo_output - .print_metadata(&format_args!("cargo:rustc-link-lib={}", stdlib)); + .print_metadata(&format_args!("cargo:rustc-link-lib={}", stdlib.display())); + } + // Link c++ lib from WASI sysroot + if target.os == "wasi" { + if let Ok(wasi_sysroot) = self.wasi_sysroot() { + self.cargo_output.print_metadata(&format_args!( + "cargo:rustc-flags=-L {}/lib/{} -lstatic=c++ -lstatic=c++abi", + Path::new(&wasi_sysroot).display(), + self.get_raw_target()? + )); + } } } let cudart = match &self.cudart { - Some(opt) => &*opt, // {none|shared|static} + Some(opt) => opt, // {none|shared|static} None => "none", }; if cudart != "none" { - if let Some(nvcc) = which(&self.get_compiler().path, None) { + if let Some(nvcc) = self.which(&self.get_compiler().path, None) { // Try to figure out the -L search path. If it fails, // it's on user to specify one by passing it through // RUSTFLAGS environment variable. @@ -1291,15 +1476,14 @@ impl Build { let mut libdir = nvcc; libdir.pop(); // remove 'nvcc' libdir.push(".."); - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); if cfg!(target_os = "linux") { libdir.push("targets"); - libdir.push(target_arch.to_owned() + "-linux"); + libdir.push(format!("{}-linux", target.arch)); libdir.push("lib"); libtst = true; } else if cfg!(target_env = "msvc") { libdir.push("lib"); - match target_arch.as_str() { + match target.arch { "x86_64" => { libdir.push("x64"); libtst = true; @@ -1406,6 +1590,8 @@ impl Build { use parallel::async_executor::{block_on, YieldOnce}; + check_disabled()?; + if objs.len() <= 1 { for obj in objs { let (mut cmd, name) = self.create_compile_object_cmd(obj)?; @@ -1416,7 +1602,7 @@ impl Build { } // Limit our parallelism globally with a jobserver. - let tokens = parallel::job_token::ActiveJobTokenServer::new()?; + let mut tokens = parallel::job_token::ActiveJobTokenServer::new(); // When compiling objects in parallel we do a few dirty tricks to speed // things up: @@ -1439,7 +1625,7 @@ impl Build { let pendings = Cell::new(Vec::<( Command, - String, + Cow<'static, Path>, KillOnDrop, parallel::job_token::JobToken, )>::new()); @@ -1463,38 +1649,35 @@ impl Build { cell_update(&pendings, |mut pendings| { // Try waiting on them. - parallel::retain_unordered_mut( - &mut pendings, - |(cmd, program, child, _token)| { - match try_wait_on_child( - cmd, - program, - &mut child.0, - &mut stdout, - &mut child.1, - ) { - Ok(Some(())) => { - // Task done, remove the entry - has_made_progress.set(true); - false - } - Ok(None) => true, // Task still not finished, keep the entry - Err(err) => { - // Task fail, remove the entry. - // Since we can only return one error, log the error to make - // sure users always see all the compilation failures. - has_made_progress.set(true); - - if self.cargo_output.warnings { - let _ = writeln!(stdout, "cargo:warning={}", err); - } - error = Some(err); - - false + pendings.retain_mut(|(cmd, program, child, _token)| { + match try_wait_on_child( + cmd, + program, + &mut child.0, + &mut stdout, + &mut child.1, + ) { + Ok(Some(())) => { + // Task done, remove the entry + has_made_progress.set(true); + false + } + Ok(None) => true, // Task still not finished, keep the entry + Err(err) => { + // Task fail, remove the entry. + // Since we can only return one error, log the error to make + // sure users always see all the compilation failures. + has_made_progress.set(true); + + if self.cargo_output.warnings { + let _ = writeln!(stdout, "cargo:warning={}", err); } + error = Some(err); + + false } - }, - ); + } + }); pendings_is_empty = pendings.is_empty(); pendings }); @@ -1555,6 +1738,8 @@ impl Build { #[cfg(not(feature = "parallel"))] fn compile_objects(&self, objs: &[Object]) -> Result<(), Error> { + check_disabled()?; + for obj in objs { let (mut cmd, name) = self.create_compile_object_cmd(obj)?; run(&mut cmd, &name, &self.cargo_output)?; @@ -1563,18 +1748,22 @@ impl Build { Ok(()) } - fn create_compile_object_cmd(&self, obj: &Object) -> Result<(Command, String), Error> { + fn create_compile_object_cmd( + &self, + obj: &Object, + ) -> Result<(Command, Cow<'static, Path>), Error> { let asm_ext = AsmFileExt::from_path(&obj.src); let is_asm = asm_ext.is_some(); let target = self.get_target()?; - let msvc = target.contains("msvc"); + let msvc = target.env == "msvc"; let compiler = self.try_get_compiler()?; - let clang = compiler.family == ToolFamily::Clang; + let clang = compiler.is_like_clang(); let gnu = compiler.family == ToolFamily::Gnu; let is_assembler_msvc = msvc && asm_ext == Some(AsmFileExt::DotAsm); let (mut cmd, name) = if is_assembler_msvc { - self.msvc_macro_assembler()? + let (cmd, name) = self.msvc_macro_assembler()?; + (cmd, Cow::Borrowed(Path::new(name))) } else { let mut cmd = compiler.to_command(); for (a, b) in self.env.iter() { @@ -1585,14 +1774,24 @@ impl Build { compiler .path .file_name() - .ok_or_else(|| Error::new(ErrorKind::IOError, "Failed to get compiler path."))? - .to_string_lossy() - .into_owned(), + .ok_or_else(|| Error::new(ErrorKind::IOError, "Failed to get compiler path.")) + .map(PathBuf::from) + .map(Cow::Owned)?, ) }; - let is_arm = target.contains("aarch64") || target.contains("arm"); + let is_arm = matches!(target.arch, "aarch64" | "arm"); command_add_output_file( - &mut cmd, &obj.dst, self.cuda, msvc, clang, gnu, is_asm, is_arm, + &mut cmd, + &obj.dst, + CmdAddOutputFileArgs { + cuda: self.cuda, + is_assembler_msvc, + msvc: compiler.is_like_msvc(), + clang, + gnu, + is_asm, + is_arm, + }, ); // armasm and armasm64 don't requrie -c option if !is_assembler_msvc || !is_arm { @@ -1604,7 +1803,8 @@ impl Build { if is_asm { cmd.args(self.asm_flags.iter().map(std::ops::Deref::deref)); } - if compiler.family == (ToolFamily::Msvc { clang_cl: true }) && !is_assembler_msvc { + + if compiler.supports_path_delimiter() && !is_assembler_msvc { // #513: For `clang-cl`, separate flags/options from the input file. // When cross-compiling macOS -> Windows, this avoids interpreting // common `/Users/...` paths as the `/U` flag and triggering @@ -1612,6 +1812,7 @@ impl Build { cmd.arg("--"); } cmd.arg(&obj.src); + if cfg!(target_os = "macos") { self.fix_env_for_apple_os(&mut cmd)?; } @@ -1619,7 +1820,8 @@ impl Build { Ok((cmd, name)) } - /// This will return a result instead of panicking; see expand() for the complete description. + /// This will return a result instead of panicking; see [`Self::expand()`] for + /// the complete description. pub fn try_expand(&self) -> Result, Error> { let compiler = self.try_get_compiler()?; let mut cmd = compiler.to_command(); @@ -1653,11 +1855,9 @@ impl Build { let name = compiler .path .file_name() - .ok_or_else(|| Error::new(ErrorKind::IOError, "Failed to get compiler path."))? - .to_string_lossy() - .into_owned(); + .ok_or_else(|| Error::new(ErrorKind::IOError, "Failed to get compiler path."))?; - Ok(run_output(&mut cmd, &name, &self.cargo_output)?) + run_output(&mut cmd, name, &self.cargo_output) } /// Run the compiler, returning the macro-expanded version of the input files. @@ -1715,33 +1915,31 @@ impl Build { let mut cmd = self.get_base_compiler()?; // Disable default flag generation via `no_default_flags` or environment variable - let no_defaults = self.no_default_flags || self.getenv("CRATE_CC_NO_DEFAULTS").is_some(); + let no_defaults = self.no_default_flags || self.getenv_boolean("CRATE_CC_NO_DEFAULTS"); if !no_defaults { self.add_default_flags(&mut cmd, &target, &opt_level)?; - } else { - println!("Info: default compiler flags are disabled"); } if let Some(ref std) = self.std { let separator = match cmd.family { ToolFamily::Msvc { .. } => ':', - ToolFamily::Gnu | ToolFamily::Clang => '=', + ToolFamily::Gnu | ToolFamily::Clang { .. } => '=', }; cmd.push_cc_arg(format!("-std{}{}", separator, std).into()); } + for directory in self.include_directories.iter() { + cmd.args.push("-I".into()); + cmd.args.push(directory.as_os_str().into()); + } + if let Ok(flags) = self.envflags(if self.cpp { "CXXFLAGS" } else { "CFLAGS" }) { for arg in flags { cmd.push_cc_arg(arg.into()); } } - for directory in self.include_directories.iter() { - cmd.args.push("-I".into()); - cmd.args.push(directory.as_os_str().into()); - } - // If warnings and/or extra_warnings haven't been explicitly set, // then we set them only if the environment doesn't already have // CFLAGS/CXXFLAGS, since those variables presumably already contain @@ -1762,8 +1960,16 @@ impl Build { cmd.args.push((**flag).into()); } + // Add cc flags inherited from matching rustc flags + if self.inherit_rustflags { + self.add_inherited_rustflags(&mut cmd, &target)?; + } + for flag in self.flags_supported.iter() { - if self.is_flag_supported(flag).unwrap_or(false) { + if self + .is_flag_supported_inner(flag, &cmd, &target) + .unwrap_or(false) + { cmd.push_cc_arg((**flag).into()); } } @@ -1781,15 +1987,29 @@ impl Build { cmd.push_cc_arg(warnings_to_errors_flag); } + // Copied from + // + // Disables non-English messages from localized linkers. + // Such messages may cause issues with text encoding on Windows + // and prevent inspection of msvc output in case of errors, which we occasionally do. + // This should be acceptable because other messages from rustc are in English anyway, + // and may also be desirable to improve searchability of the compiler diagnostics. + if matches!(cmd.family, ToolFamily::Msvc { clang_cl: false }) { + cmd.env.push(("VSLANG".into(), "1033".into())); + } else { + cmd.env.push(("LC_ALL".into(), "C".into())); + } + Ok(cmd) } fn add_default_flags( &self, cmd: &mut Tool, - target: &str, + target: &TargetInfo<'_>, opt_level: &str, ) -> Result<(), Error> { + let raw_target = self.get_raw_target()?; // Non-target flags // If the flag is not conditioned on target variable, it belongs here :) match cmd.family { @@ -1802,7 +2022,7 @@ impl Build { None => { let features = self.getenv("CARGO_CFG_TARGET_FEATURE"); let features = features.as_deref().unwrap_or_default(); - if features.contains("crt-static") { + if features.to_string_lossy().contains("crt-static") { "-MT" } else { "-MD" @@ -1811,7 +2031,7 @@ impl Build { }; cmd.push_cc_arg(crt_flag.into()); - match &opt_level[..] { + match opt_level { // Msvc uses /O1 to enable all optimizations that minimize code size. "z" | "s" | "1" => cmd.push_opt_unless_duplicate("-O1".into()), // -O3 is a valid value for gcc and clang compilers, but not msvc. Cap to /O2. @@ -1819,23 +2039,16 @@ impl Build { _ => {} } } - ToolFamily::Gnu | ToolFamily::Clang => { + ToolFamily::Gnu | ToolFamily::Clang { .. } => { // arm-linux-androideabi-gcc 4.8 shipped with Android NDK does // not support '-Oz' - if opt_level == "z" && cmd.family != ToolFamily::Clang { + if opt_level == "z" && !cmd.is_like_clang() { cmd.push_opt_unless_duplicate("-Os".into()); } else { cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into()); } - if cmd.family == ToolFamily::Clang && target.contains("windows") { - // Disambiguate mingw and msvc on Windows. Problem is that - // depending on the origin clang can default to a mismatchig - // run-time. - cmd.push_cc_arg(format!("--target={}", target).into()); - } - - if cmd.family == ToolFamily::Clang && target.contains("android") { + if cmd.is_like_clang() && target.os == "android" { // For compatibility with code that doesn't use pre-defined `__ANDROID__` macro. // If compiler used via ndk-build or cmake (officially supported build methods) // this macros is defined. @@ -1844,26 +2057,77 @@ impl Build { cmd.push_opt_unless_duplicate("-DANDROID".into()); } - if !target.contains("apple-ios") - && !target.contains("apple-watchos") - && !target.contains("apple-tvos") + if target.os != "ios" + && target.os != "watchos" + && target.os != "tvos" + && target.os != "visionos" { cmd.push_cc_arg("-ffunction-sections".into()); cmd.push_cc_arg("-fdata-sections".into()); } // Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet + // + // `rustc` also defaults to disable PIC on WASM: + // if self.pic.unwrap_or( - !target.contains("windows") - && !target.contains("-none-") - && !target.contains("uefi"), + target.os != "windows" + && target.os != "none" + && target.os != "uefi" + && target.arch != "wasm32" + && target.arch != "wasm64", ) { cmd.push_cc_arg("-fPIC".into()); // PLT only applies if code is compiled with PIC support, // and only for ELF targets. - if target.contains("linux") && !self.use_plt.unwrap_or(true) { + if (target.os == "linux" || target.os == "android") + && !self.use_plt.unwrap_or(true) + { cmd.push_cc_arg("-fno-plt".into()); } } + if target.arch == "wasm32" || target.arch == "wasm64" { + // WASI does not support exceptions yet. + // https://github.com/WebAssembly/exception-handling + // + // `rustc` also defaults to (currently) disable exceptions + // on all WASM targets: + // + cmd.push_cc_arg("-fno-exceptions".into()); + } + + if target.os == "wasi" { + // Link clang sysroot + if let Ok(wasi_sysroot) = self.wasi_sysroot() { + cmd.push_cc_arg( + format!("--sysroot={}", Path::new(&wasi_sysroot).display()).into(), + ); + } + + // FIXME(madsmtm): Read from `target_features` instead? + if raw_target.contains("threads") { + cmd.push_cc_arg("-pthread".into()); + } + } + + if target.os == "nto" { + // Select the target with `-V`, see qcc documentation: + // QNX 7.1: https://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.utilities/topic/q/qcc.html + // QNX 8.0: https://www.qnx.com/developers/docs/8.0/com.qnx.doc.neutrino.utilities/topic/q/qcc.html + // This assumes qcc/q++ as compiler, which is currently the only supported compiler for QNX. + // See for details: https://github.com/rust-lang/cc-rs/pull/1319 + let arg = match target.arch { + "i586" => "-Vgcc_ntox86_cxx", + "aarch64" => "-Vgcc_ntoaarch64le_cxx", + "x86_64" => "-Vgcc_ntox86_64_cxx", + _ => { + return Err(Error::new( + ErrorKind::InvalidTarget, + format!("Unknown architecture for Neutrino QNX: {}", target.arch), + )) + } + }; + cmd.push_cc_arg(arg.into()); + } } } @@ -1882,65 +2146,29 @@ impl Build { } if !cmd.is_like_msvc() { - if target.contains("i686") || target.contains("i586") { + if target.arch == "x86" { cmd.args.push("-m32".into()); - } else if target == "x86_64-unknown-linux-gnux32" { + } else if target.abi == "x32" { cmd.args.push("-mx32".into()); - } else if target.contains("x86_64") || target.contains("powerpc64") { + } else if target.os == "aix" { + if cmd.family == ToolFamily::Gnu { + cmd.args.push("-maix64".into()); + } else { + cmd.args.push("-m64".into()); + } + } else if target.arch == "x86_64" || target.arch == "powerpc64" { cmd.args.push("-m64".into()); } } // Target flags - if target.contains("-apple-") { - self.apple_flags(cmd, target)?; - } else { - self.target_flags(cmd, target); - } - - if self.static_flag.unwrap_or(false) { - cmd.args.push("-static".into()); - } - if self.shared_flag.unwrap_or(false) { - cmd.args.push("-shared".into()); - } - - if self.cpp { - match (self.cpp_set_stdlib.as_ref(), cmd.family) { - (None, _) => {} - (Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => { - cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into()); - } - _ => { - self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family)); - } - } - } - - Ok(()) - } - - fn target_flags(&self, cmd: &mut Tool, target: &str) { match cmd.family { - ToolFamily::Clang => { - if !(target.contains("android") && cmd.has_internal_target_arg) { - if target.starts_with("riscv64gc-") { - cmd.args.push( - format!("--target={}", target.replace("riscv64gc", "riscv64")).into(), - ); - } else if target.starts_with("riscv32gc-") { - cmd.args.push( - format!("--target={}", target.replace("riscv32gc", "riscv32")).into(), - ); - } else if target.contains("uefi") { - if target.contains("x86_64") { - cmd.args.push("--target=x86_64-unknown-windows-gnu".into()); - } else if target.contains("i686") { - cmd.args.push("--target=i686-unknown-windows-gnu".into()) - } else if target.contains("aarch64") { - cmd.args.push("--target=aarch64-unknown-windows-gnu".into()) - } - } else if target.ends_with("-freebsd") { + ToolFamily::Clang { .. } => { + if !(cmd.has_internal_target_arg + || (target.os == "android" + && android_clang_compiler_uses_target_arg_internally(&cmd.path))) + { + if target.os == "freebsd" { // FreeBSD only supports C++11 and above when compiling against libc++ // (available from FreeBSD 10 onwards). Under FreeBSD, clang uses libc++ by // default on FreeBSD 10 and newer unless `--target` is manually passed to @@ -1962,10 +2190,24 @@ impl Build { if self.cpp && self.cpp_set_stdlib.is_none() { cmd.push_cc_arg("-stdlib=libc++".into()); } + } - cmd.push_cc_arg(format!("--target={}", target).into()); - } else { - cmd.push_cc_arg(format!("--target={}", target).into()); + // Pass `--target` with the LLVM target to configure Clang for cross-compiling. + // + // NOTE: In the past, we passed this, along with the deployment version in here + // on Apple targets, but versioned targets were found to have poor compatibility + // with older versions of Clang, especially around comes to configuration files. + // + // Instead, we specify `-arch` along with `-mmacosx-version-min=`, `-mtargetos=` + // and similar flags in `.apple_flags()`. + // + // Note that Clang errors when both `-mtargetos=` and `-target` are specified, + // so we omit this entirely on Apple targets (it's redundant when specifying + // both the `-arch` and the deployment target / OS flag) (in theory we _could_ + // specify this on some of the Apple targets that use the older + // `-m*-version-min=`, but for consistency we omit it entirely). + if target.vendor != "apple" { + cmd.push_cc_arg(format!("--target={}", target.llvm_target).into()); } } } @@ -1976,22 +2218,19 @@ impl Build { cmd.push_cc_arg("-Brepro".into()); if clang_cl { - if target.contains("x86_64") { + if target.arch == "x86_64" { cmd.push_cc_arg("-m64".into()); - } else if target.contains("86") { + } else if target.arch == "x86" { cmd.push_cc_arg("-m32".into()); cmd.push_cc_arg("-arch:IA32".into()); } else { - cmd.push_cc_arg(format!("--target={}", target).into()); - } - } else { - if target.contains("i586") { - cmd.push_cc_arg("-arch:IA32".into()); - } else if target.contains("arm64ec") { - cmd.push_cc_arg("-arm64EC".into()); + cmd.push_cc_arg(format!("--target={}", target.llvm_target).into()); } + } else if target.full_arch == "i586" { + cmd.push_cc_arg("-arch:IA32".into()); + } else if target.full_arch == "arm64ec" { + cmd.push_cc_arg("-arm64EC".into()); } - // There is a check in corecrt.h that will generate a // compilation error if // _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE is @@ -2001,41 +2240,43 @@ impl Build { // The check will be going away in future versions of // the SDK, but for all released versions of the // Windows SDK it is required. - if target.contains("arm") || target.contains("thumb") { + if target.arch == "arm" { cmd.args .push("-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1".into()); } } ToolFamily::Gnu => { - if target.contains("-kmc-solid_") { + if target.vendor == "kmc" { cmd.args.push("-finput-charset=utf-8".into()); } if self.static_flag.is_none() { let features = self.getenv("CARGO_CFG_TARGET_FEATURE"); let features = features.as_deref().unwrap_or_default(); - if features.contains("crt-static") { + if features.to_string_lossy().contains("crt-static") { cmd.args.push("-static".into()); } } // armv7 targets get to use armv7 instructions - if (target.starts_with("armv7") || target.starts_with("thumbv7")) - && (target.contains("-linux-") || target.contains("-kmc-solid_")) + if (target.full_arch.starts_with("armv7") + || target.full_arch.starts_with("thumbv7")) + && (target.os == "linux" || target.vendor == "kmc") { cmd.args.push("-march=armv7-a".into()); - if target.ends_with("eabihf") { + if target.abi == "eabihf" { // lowest common denominator FPU cmd.args.push("-mfpu=vfpv3-d16".into()); + cmd.args.push("-mfloat-abi=hard".into()); } } // (x86 Android doesn't say "eabi") - if target.contains("-androideabi") && target.contains("v7") { - // -march=armv7-a handled above + if target.os == "android" && target.full_arch.contains("v7") { + cmd.args.push("-march=armv7-a".into()); cmd.args.push("-mthumb".into()); - if !target.contains("neon") { + if !target.full_arch.contains("neon") { // On android we can guarantee some extra float instructions // (specified in the android spec online) // NEON guarantees even more; see below. @@ -2044,49 +2285,40 @@ impl Build { cmd.args.push("-mfloat-abi=softfp".into()); } - if target.contains("neon") { + if target.full_arch.contains("neon") { cmd.args.push("-mfpu=neon-vfpv4".into()); } - if target.starts_with("armv4t-unknown-linux-") { + if target.full_arch == "armv4t" && target.os == "linux" { cmd.args.push("-march=armv4t".into()); cmd.args.push("-marm".into()); cmd.args.push("-mfloat-abi=soft".into()); } - if target.starts_with("armv5te-unknown-linux-") { + if target.full_arch == "armv5te" && target.os == "linux" { cmd.args.push("-march=armv5te".into()); cmd.args.push("-marm".into()); cmd.args.push("-mfloat-abi=soft".into()); } // For us arm == armv6 by default - if target.starts_with("arm-unknown-linux-") { + if target.full_arch == "arm" && target.os == "linux" { cmd.args.push("-march=armv6".into()); cmd.args.push("-marm".into()); - if target.ends_with("hf") { + if target.abi == "eabihf" { cmd.args.push("-mfpu=vfp".into()); } else { cmd.args.push("-mfloat-abi=soft".into()); } } - // We can guarantee some settings for FRC - if target.starts_with("arm-frc-") { - cmd.args.push("-march=armv7-a".into()); - cmd.args.push("-mcpu=cortex-a9".into()); - cmd.args.push("-mfpu=vfpv3".into()); - cmd.args.push("-mfloat-abi=softfp".into()); - cmd.args.push("-marm".into()); - } - // Turn codegen down on i586 to avoid some instructions. - if target.starts_with("i586-unknown-linux-") { + if target.full_arch == "i586" && target.os == "linux" { cmd.args.push("-march=pentium".into()); } // Set codegen level for i686 correctly - if target.starts_with("i686-unknown-linux-") { + if target.full_arch == "i686" && target.os == "linux" { cmd.args.push("-march=i686".into()); } @@ -2095,42 +2327,42 @@ impl Build { // linker that we're generating 32-bit executables as well. This'll // typically only be used for build scripts which transitively use // these flags that try to compile executables. - if target == "i686-unknown-linux-musl" || target == "i586-unknown-linux-musl" { + if target.arch == "x86" && target.env == "musl" { cmd.args.push("-Wl,-melf_i386".into()); } - if target.starts_with("thumb") { + if target.arch == "arm" && target.os == "none" && target.abi == "eabihf" { + cmd.args.push("-mfloat-abi=hard".into()) + } + if target.full_arch.starts_with("thumb") { cmd.args.push("-mthumb".into()); - - if target.ends_with("eabihf") { - cmd.args.push("-mfloat-abi=hard".into()) - } } - if target.starts_with("thumbv6m") { + if target.full_arch.starts_with("thumbv6m") { cmd.args.push("-march=armv6s-m".into()); } - if target.starts_with("thumbv7em") { + if target.full_arch.starts_with("thumbv7em") { cmd.args.push("-march=armv7e-m".into()); - if target.ends_with("eabihf") { + if target.abi == "eabihf" { cmd.args.push("-mfpu=fpv4-sp-d16".into()) } } - if target.starts_with("thumbv7m") { + if target.full_arch.starts_with("thumbv7m") { cmd.args.push("-march=armv7-m".into()); } - if target.starts_with("thumbv8m.base") { + if target.full_arch.starts_with("thumbv8m.base") { cmd.args.push("-march=armv8-m.base".into()); } - if target.starts_with("thumbv8m.main") { + if target.full_arch.starts_with("thumbv8m.main") { cmd.args.push("-march=armv8-m.main".into()); - if target.ends_with("eabihf") { + if target.abi == "eabihf" { cmd.args.push("-mfpu=fpv5-sp-d16".into()) } } - if target.starts_with("armebv7r") | target.starts_with("armv7r") { - if target.starts_with("armeb") { + if target.full_arch.starts_with("armebv7r") | target.full_arch.starts_with("armv7r") + { + if target.full_arch.starts_with("armeb") { cmd.args.push("-mbig-endian".into()); } else { cmd.args.push("-mlittle-endian".into()); @@ -2142,87 +2374,114 @@ impl Build { // R Profile cmd.args.push("-march=armv7-r".into()); - if target.ends_with("eabihf") { - // Calling convention - cmd.args.push("-mfloat-abi=hard".into()); - + if target.abi == "eabihf" { // lowest common denominator FPU // (see Cortex-R4 technical reference manual) cmd.args.push("-mfpu=vfpv3-d16".into()) - } else { - // Calling convention - cmd.args.push("-mfloat-abi=soft".into()); } } - if target.starts_with("armv7a") { + if target.full_arch.starts_with("armv7a") { cmd.args.push("-march=armv7-a".into()); - if target.ends_with("eabihf") { + if target.abi == "eabihf" { // lowest common denominator FPU cmd.args.push("-mfpu=vfpv3-d16".into()); } } - if target.starts_with("riscv32") || target.starts_with("riscv64") { + if target.arch == "riscv32" || target.arch == "riscv64" { // get the 32i/32imac/32imc/64gc/64imac/... part - let mut parts = target.split('-'); - if let Some(arch) = parts.next() { - let arch = &arch[5..]; - if arch.starts_with("64") { - if target.contains("linux") - | target.contains("freebsd") - | target.contains("netbsd") - | target.contains("linux") - { - cmd.args.push(("-march=rv64gc").into()); - cmd.args.push("-mabi=lp64d".into()); - } else { - cmd.args.push(("-march=rv".to_owned() + arch).into()); - cmd.args.push("-mabi=lp64".into()); - } - } else if arch.starts_with("32") { - if target.contains("linux") { - cmd.args.push(("-march=rv32gc").into()); - cmd.args.push("-mabi=ilp32d".into()); - } else { - cmd.args.push(("-march=rv".to_owned() + arch).into()); - cmd.args.push("-mabi=ilp32".into()); - } + let arch = &target.full_arch[5..]; + if arch.starts_with("64") { + if matches!(target.os, "linux" | "freebsd" | "netbsd") { + cmd.args.push(("-march=rv64gc").into()); + cmd.args.push("-mabi=lp64d".into()); + } else { + cmd.args.push(("-march=rv".to_owned() + arch).into()); + cmd.args.push("-mabi=lp64".into()); + } + } else if arch.starts_with("32") { + if target.os == "linux" { + cmd.args.push(("-march=rv32gc").into()); + cmd.args.push("-mabi=ilp32d".into()); } else { - cmd.args.push("-mcmodel=medany".into()); + cmd.args.push(("-march=rv".to_owned() + arch).into()); + cmd.args.push("-mabi=ilp32".into()); } + } else { + cmd.args.push("-mcmodel=medany".into()); } } } } + + if target.vendor == "apple" { + self.apple_flags(cmd)?; + } + + if self.static_flag.unwrap_or(false) { + cmd.args.push("-static".into()); + } + if self.shared_flag.unwrap_or(false) { + cmd.args.push("-shared".into()); + } + + if self.cpp { + match (self.cpp_set_stdlib.as_ref(), cmd.family) { + (None, _) => {} + (Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang { .. }) => { + cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into()); + } + _ => { + self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family)); + } + } + } + + Ok(()) + } + + fn add_inherited_rustflags( + &self, + cmd: &mut Tool, + target: &TargetInfo<'_>, + ) -> Result<(), Error> { + let env_os = match self.getenv("CARGO_ENCODED_RUSTFLAGS") { + Some(env) => env, + // No encoded RUSTFLAGS -> nothing to do + None => return Ok(()), + }; + + let env = env_os.to_string_lossy(); + let codegen_flags = RustcCodegenFlags::parse(&env)?; + codegen_flags.cc_flags(self, cmd, target); + Ok(()) } fn has_flags(&self) -> bool { let flags_env_var_name = if self.cpp { "CXXFLAGS" } else { "CFLAGS" }; let flags_env_var_value = self.getenv_with_target_prefixes(flags_env_var_name); - if let Ok(_) = flags_env_var_value { - true - } else { - false - } + flags_env_var_value.is_ok() } - fn msvc_macro_assembler(&self) -> Result<(Command, String), Error> { + fn msvc_macro_assembler(&self) -> Result<(Command, &'static str), Error> { let target = self.get_target()?; - let tool = if target.contains("x86_64") { + let tool = if target.arch == "x86_64" { "ml64.exe" - } else if target.contains("arm") { + } else if target.arch == "arm" { "armasm.exe" - } else if target.contains("aarch64") { + } else if target.arch == "aarch64" { "armasm64.exe" } else { "ml.exe" }; - let mut cmd = windows_registry::find(&target, tool).unwrap_or_else(|| self.cmd(tool)); + let mut cmd = self + .windows_registry_find(&target, tool) + .unwrap_or_else(|| self.cmd(tool)); cmd.arg("-nologo"); // undocumented, yet working with armasm[64] for directory in self.include_directories.iter() { cmd.arg("-I").arg(&**directory); } - if target.contains("aarch64") || target.contains("arm") { + if target.arch == "aarch64" || target.arch == "arm" { if self.get_debug() { cmd.arg("-g"); } @@ -2231,14 +2490,14 @@ impl Build { cmd.arg("-PreDefine"); if let Some(ref value) = *value { if let Ok(i) = value.parse::() { - cmd.arg(&format!("{} SETA {}", key, i)); + cmd.arg(format!("{} SETA {}", key, i)); } else if value.starts_with('"') && value.ends_with('"') { - cmd.arg(&format!("{} SETS {}", key, value)); + cmd.arg(format!("{} SETS {}", key, value)); } else { - cmd.arg(&format!("{} SETS \"{}\"", key, value)); + cmd.arg(format!("{} SETS \"{}\"", key, value)); } } else { - cmd.arg(&format!("{} SETL {}", key, "{TRUE}")); + cmd.arg(format!("{} SETL {}", key, "{TRUE}")); } } } else { @@ -2248,18 +2507,18 @@ impl Build { for (key, value) in self.definitions.iter() { if let Some(ref value) = *value { - cmd.arg(&format!("-D{}={}", key, value)); + cmd.arg(format!("-D{}={}", key, value)); } else { - cmd.arg(&format!("-D{}", key)); + cmd.arg(format!("-D{}", key)); } } } - if target.contains("i686") || target.contains("i586") { + if target.arch == "x86" { cmd.arg("-safeseh"); } - Ok((cmd, tool.to_string())) + Ok((cmd, tool)) } fn assemble(&self, lib_name: &str, dst: &Path, objs: &[Object]) -> Result<(), Error> { @@ -2292,7 +2551,7 @@ impl Build { } let target = self.get_target()?; - if target.contains("msvc") { + if target.env == "msvc" { // The Rust compiler will look for libfoo.a and foo.lib, but the // MSVC linker will also be passed foo.lib, so be sure that both // exist for now. @@ -2329,8 +2588,8 @@ impl Build { fn assemble_progressive(&self, dst: &Path, objs: &[&Path]) -> Result<(), Error> { let target = self.get_target()?; - if target.contains("msvc") { - let (mut cmd, program, any_flags) = self.get_ar()?; + let (mut cmd, program, any_flags) = self.get_ar()?; + if target.env == "msvc" && !program.to_string_lossy().contains("llvm-ar") { // NOTE: -out: here is an I/O flag, and so must be included even if $ARFLAGS/ar_flag is // in use. -nologo on the other hand is just a regular flag, and one that we'll skip if // the caller has explicitly dictated the flags they want. See @@ -2349,8 +2608,6 @@ impl Build { cmd.args(objs); run(&mut cmd, &program, &self.cargo_output)?; } else { - let (mut ar, cmd, _any_flags) = self.get_ar()?; - // Set an environment variable to tell the OSX archiver to ensure // that all dates listed in the archive are zero, improving // determinism of builds. AFAIK there's not really official @@ -2373,234 +2630,82 @@ impl Build { // // In any case if this doesn't end up getting read, it shouldn't // cause that many issues! - ar.env("ZERO_AR_DATE", "1"); + cmd.env("ZERO_AR_DATE", "1"); // NOTE: We add cq here regardless of whether $ARFLAGS/ar_flag have been used because // it dictates the _mode_ ar runs in, which the setter of $ARFLAGS/ar_flag can't // dictate. See https://github.com/rust-lang/cc-rs/pull/763 for further discussion. - run(ar.arg("cq").arg(dst).args(objs), &cmd, &self.cargo_output)?; + run( + cmd.arg("cq").arg(dst).args(objs), + &program, + &self.cargo_output, + )?; } Ok(()) } - fn apple_flags(&self, cmd: &mut Tool, target: &str) -> Result<(), Error> { - let os = if target.contains("-darwin") { - AppleOs::MacOs - } else if target.contains("-watchos") { - AppleOs::WatchOs - } else if target.contains("-tvos") { - AppleOs::TvOs - } else { - AppleOs::Ios - }; - let is_mac = match os { - AppleOs::MacOs => true, - _ => false, - }; - - let arch_str = target.split('-').nth(0).ok_or_else(|| { - Error::new( - ErrorKind::ArchitectureInvalid, - format!("Unknown architecture for {:?} target.", os), - ) - })?; - - let is_catalyst = match target.split('-').nth(3) { - Some(v) => v == "macabi", - None => false, - }; - - let is_arm_sim = match target.split('-').nth(3) { - Some(v) => v == "sim", - None => false, - }; - - let arch = if is_mac { - match arch_str { - "i686" => AppleArchSpec::Device("-m32"), - "x86_64" | "x86_64h" | "aarch64" => AppleArchSpec::Device("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - "Unknown architecture for macOS target.", - )); - } - } - } else if is_catalyst { - match arch_str { - "arm64e" => AppleArchSpec::Catalyst("arm64e"), - "arm64" | "aarch64" => AppleArchSpec::Catalyst("arm64"), - "x86_64" | "x86_64h" => AppleArchSpec::Catalyst("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - "Unknown architecture for iOS target.", - )); - } - } - } else if is_arm_sim { - match arch_str { - "arm64" | "aarch64" => AppleArchSpec::Simulator("arm64"), - "x86_64" | "x86_64h" => AppleArchSpec::Simulator("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - "Unknown architecture for simulator target.", - )); - } - } - } else { - match arch_str { - "arm" | "armv7" | "thumbv7" => AppleArchSpec::Device("armv7"), - "armv7k" => AppleArchSpec::Device("armv7k"), - "armv7s" | "thumbv7s" => AppleArchSpec::Device("armv7s"), - "arm64e" => AppleArchSpec::Device("arm64e"), - "arm64" | "aarch64" => AppleArchSpec::Device("arm64"), - "arm64_32" => AppleArchSpec::Device("arm64_32"), - "i386" | "i686" => AppleArchSpec::Simulator("-m32"), - "x86_64" | "x86_64h" => AppleArchSpec::Simulator("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - format!("Unknown architecture for {:?} target.", os), - )); - } - } - }; - - let sdk_details = apple_os_sdk_parts(os, &arch); - let min_version = self.apple_deployment_version(os, Some(arch_str), &sdk_details.sdk); + fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> { + let target = self.get_target()?; - match arch { - AppleArchSpec::Device(_) if is_mac => { - cmd.args - .push(format!("-mmacosx-version-min={}", min_version).into()); - } - AppleArchSpec::Device(arch) => { - cmd.args.push("-arch".into()); - cmd.args.push(arch.into()); - cmd.args.push( - format!("-m{}os-version-min={}", sdk_details.sdk_prefix, min_version).into(), - ); - } - AppleArchSpec::Simulator(arch) => { - if arch.starts_with('-') { - // -m32 or -m64 - cmd.args.push(arch.into()); - } else { - cmd.args.push("-arch".into()); - cmd.args.push(arch.into()); - } - cmd.args.push( - format!( - "-m{}simulator-version-min={}", - sdk_details.sim_prefix, min_version - ) - .into(), - ); - } - AppleArchSpec::Catalyst(_) => {} - }; + // Add `-arch` on all compilers. This is a Darwin/Apple-specific flag + // that works both on GCC and Clang. + // https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html#:~:text=arch + // https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-arch + let arch = map_darwin_target_from_rust_to_compiler_architecture(&target); + cmd.args.push("-arch".into()); + cmd.args.push(arch.into()); - // AppleClang sometimes requires sysroot even for darwin - if cmd.is_xctoolchain_clang() || !target.ends_with("-darwin") { + // Pass the deployment target via `-mmacosx-version-min=`, `-mtargetos=` and similar. + // + // It is also necessary on GCC, as it forces a compilation error if the compiler is not + // configured for Darwin: https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html + let min_version = self.apple_deployment_target(&target); + cmd.args + .push(target.apple_version_flag(&min_version).into()); + + // AppleClang sometimes requires sysroot even on macOS + if cmd.is_xctoolchain_clang() || target.os != "macos" { self.cargo_output.print_metadata(&format_args!( "Detecting {:?} SDK path for {}", - os, sdk_details.sdk + target.os, + target.apple_sdk_name(), )); - let sdk_path = self.apple_sdk_root(&sdk_details.sdk)?; + let sdk_path = self.apple_sdk_root(&target)?; cmd.args.push("-isysroot".into()); - cmd.args.push(sdk_path); - } - - match cmd.family { - ToolFamily::Gnu => { - if target.contains("darwin") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args.push("-arch".into()); - cmd.args.push(arch.into()); - } - } - } - ToolFamily::Clang => { - if target.contains("darwin") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args - .push(format!("--target={}-apple-darwin", arch).into()); - } - } else if target.contains("macabi") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args - .push(format!("--target={}-apple-ios-macabi", arch).into()); - } - } else if target.contains("ios-sim") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args.push( - format!("--target={}-apple-ios{}-simulator", arch, min_version).into(), - ); - } - } else if target.contains("watchos-sim") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args.push( - format!("--target={}-apple-watchos{}-simulator", arch, min_version) - .into(), - ); - } - } else if target.contains("tvos-sim") || target.contains("x86_64-apple-tvos") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) + cmd.args.push(OsStr::new(&sdk_path).to_owned()); + + if target.abi == "macabi" { + // Mac Catalyst uses the macOS SDK, but to compile against and + // link to iOS-specific frameworks, we should have the support + // library stubs in the include and library search path. + let ios_support = Path::new(&sdk_path).join("System/iOSSupport"); + + cmd.args.extend([ + // Header search path + OsString::from("-isystem"), + ios_support.join("usr/include").into(), + // Framework header search path + OsString::from("-iframework"), + ios_support.join("System/Library/Frameworks").into(), + // Library search path { - cmd.args.push( - format!("--target={}-apple-tvos{}-simulator", arch, min_version).into(), - ); - } - } else if target.contains("aarch64-apple-tvos") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) + let mut s = OsString::from("-L"); + s.push(ios_support.join("usr/lib")); + s + }, + // Framework linker search path { - cmd.args - .push(format!("--target={}-apple-tvos{}", arch, min_version).into()); - } - } + // Technically, we _could_ avoid emitting `-F`, as + // `-iframework` implies it, but let's keep it in for + // clarity. + let mut s = OsString::from("-F"); + s.push(ios_support.join("System/Library/Frameworks")); + s + }, + ]); } - _ => unreachable!("unexpected compiler for apple architectures"), - } - - if let AppleArchSpec::Catalyst(_) = arch { - // Mac Catalyst uses the macOS SDK, but to compile against and - // link to iOS-specific frameworks, we should have the support - // library stubs in the include and library search path. - let sdk_path = self.apple_sdk_root(&sdk_details.sdk)?; - let ios_support = PathBuf::from(sdk_path).join("/System/iOSSupport"); - - cmd.args.extend([ - // Header search path - OsString::from("-isystem"), - ios_support.join("/usr/include").into(), - // Framework header search path - OsString::from("-iframework"), - ios_support.join("/System/Library/Frameworks").into(), - // Library search path - { - let mut s = OsString::from("-L"); - s.push(&ios_support.join("/usr/lib")); - s - }, - // Framework linker search path - { - // Technically, we _could_ avoid emitting `-F`, as - // `-iframework` implies it, but let's keep it in for - // clarity. - let mut s = OsString::from("-F"); - s.push(&ios_support.join("/System/Library/Frameworks")); - s - }, - ]); } Ok(()) @@ -2615,16 +2720,19 @@ impl Build { } fn get_base_compiler(&self) -> Result { + let out_dir = self.get_out_dir().ok(); + let out_dir = out_dir.as_deref(); + if let Some(c) = &self.compiler { return Ok(Tool::new( (**c).to_owned(), - &self.cached_compiler_family, + &self.build_cache.cached_compiler_family, &self.cargo_output, + out_dir, )); } - let host = self.get_host()?; let target = self.get_target()?; - let target = &*target; + let raw_target = self.get_raw_target()?; let (env, msvc, gnu, traditional, clang) = if self.cpp { ("CXX", "cl.exe", "g++", "c++", "clang++") } else { @@ -2635,35 +2743,30 @@ impl Build { // is not flag-compatible with "gcc". This history casts a long shadow, // and many modern illumos distributions today ship GCC as "gcc" without // also making it available as "cc". - let default = if host.contains("solaris") || host.contains("illumos") { + let default = if cfg!(target_os = "solaris") || cfg!(target_os = "illumos") { gnu } else { traditional }; - let cl_exe = windows_registry::find_tool(target, "cl.exe"); + let cl_exe = self.windows_registry_find_tool(&target, "cl.exe"); let tool_opt: Option = self .env_tool(env) .map(|(tool, wrapper, args)| { - // find the driver mode, if any - const DRIVER_MODE: &str = "--driver-mode="; - let driver_mode = args - .iter() - .find(|a| a.starts_with(DRIVER_MODE)) - .map(|a| &a[DRIVER_MODE.len()..]); // Chop off leading/trailing whitespace to work around // semi-buggy build scripts which are shared in // makefiles/configure scripts (where spaces are far more // lenient) - let mut t = Tool::with_clang_driver( + let mut t = Tool::with_args( tool, - driver_mode, - &self.cached_compiler_family, + args.clone(), + &self.build_cache.cached_compiler_family, &self.cargo_output, + out_dir, ); if let Some(cc_wrapper) = wrapper { - t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper)); + t.cc_wrapper_path = Some(Path::new(&cc_wrapper).to_owned()); } for arg in args { t.cc_wrapper_args.push(arg.into()); @@ -2671,14 +2774,13 @@ impl Build { t }) .or_else(|| { - if target.contains("emscripten") { + if target.os == "emscripten" { let tool = if self.cpp { "em++" } else { "emcc" }; // Windows uses bat file so we have to be a bit more specific if cfg!(windows) { - let mut t = Tool::new( + let mut t = Tool::with_family( PathBuf::from("cmd"), - &self.cached_compiler_family, - &self.cargo_output, + ToolFamily::Clang { zig_cc: false }, ); t.args.push("/c".into()); t.args.push(format!("{}.bat", tool).into()); @@ -2686,8 +2788,9 @@ impl Build { } else { Some(Tool::new( PathBuf::from(tool), - &self.cached_compiler_family, + &self.build_cache.cached_compiler_family, &self.cargo_output, + out_dir, )) } } else { @@ -2699,42 +2802,52 @@ impl Build { let tool = match tool_opt { Some(t) => t, None => { - let compiler = if host.contains("windows") && target.contains("windows") { - if target.contains("msvc") { + let compiler = if cfg!(windows) && target.os == "windows" { + if target.env == "msvc" { msvc.to_string() } else { - let cc = if target.contains("llvm") { clang } else { gnu }; + let cc = if target.abi == "llvm" { clang } else { gnu }; format!("{}.exe", cc) } - } else if target.contains("apple-ios") - | target.contains("apple-watchos") - | target.contains("apple-tvos") + } else if target.os == "ios" + || target.os == "watchos" + || target.os == "tvos" + || target.os == "visionos" { clang.to_string() - } else if target.contains("android") { - autodetect_android_compiler(target, &host, gnu, clang) - } else if target.contains("cloudabi") { - format!("{}-{}", target, traditional) - } else if target == "wasm32-wasi" - || target == "wasm32-unknown-wasi" - || target == "wasm32-unknown-unknown" - { - "clang".to_string() - } else if target.contains("vxworks") { + } else if target.os == "android" { + autodetect_android_compiler(&raw_target, gnu, clang) + } else if target.os == "cloudabi" { + format!( + "{}-{}-{}-{}", + target.full_arch, target.vendor, target.os, traditional + ) + } else if target.arch == "wasm32" || target.arch == "wasm64" { + // Compiling WASM is not currently supported by GCC, so + // let's default to Clang. + clang.to_string() + } else if target.os == "vxworks" { if self.cpp { "wr-c++".to_string() } else { "wr-cc".to_string() } - } else if target.starts_with("armv7a-kmc-solid_") { + } else if target.arch == "arm" && target.vendor == "kmc" { format!("arm-kmc-eabi-{}", gnu) - } else if target.starts_with("aarch64-kmc-solid_") { + } else if target.arch == "aarch64" && target.vendor == "kmc" { format!("aarch64-kmc-elf-{}", gnu) - } else if &*self.get_host()? != target { - let prefix = self.prefix_for_target(target); + } else if target.os == "nto" { + // See for details: https://github.com/rust-lang/cc-rs/pull/1319 + if self.cpp { + "q++".to_string() + } else { + "qcc".to_string() + } + } else if self.get_is_cross_compile()? { + let prefix = self.prefix_for_target(&raw_target); match prefix { Some(prefix) => { - let cc = if target.contains("llvm") { clang } else { gnu }; + let cc = if target.abi == "llvm" { clang } else { gnu }; format!("{}-{}", prefix, cc) } None => default.to_string(), @@ -2745,11 +2858,12 @@ impl Build { let mut t = Tool::new( PathBuf::from(compiler), - &self.cached_compiler_family, + &self.build_cache.cached_compiler_family, &self.cargo_output, + out_dir, ); - if let Some(cc_wrapper) = Self::rustc_wrapper_fallback() { - t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper)); + if let Some(cc_wrapper) = self.rustc_wrapper_fallback() { + t.cc_wrapper_path = Some(Path::new(&cc_wrapper).to_owned()); } t } @@ -2766,14 +2880,20 @@ impl Build { }; let mut nvcc_tool = Tool::with_features( nvcc, - None, + vec![], self.cuda, - &self.cached_compiler_family, + &self.build_cache.cached_compiler_family, &self.cargo_output, + out_dir, ); - nvcc_tool - .args - .push(format!("-ccbin={}", tool.path.display()).into()); + if self.ccbin { + nvcc_tool + .args + .push(format!("-ccbin={}", tool.path.display()).into()); + } + if let Some(cc_wrapper) = self.rustc_wrapper_fallback() { + nvcc_tool.cc_wrapper_path = Some(Path::new(&cc_wrapper).to_owned()); + } nvcc_tool.family = tool.family; nvcc_tool } else { @@ -2791,8 +2911,7 @@ impl Build { // on Windows is restricted to around 8k characters instead of around 32k characters. // To remove this limit, we call the main clang binary directly and construct the // `--target=` ourselves. - if host.contains("windows") && android_clang_compiler_uses_target_arg_internally(&tool.path) - { + if cfg!(windows) && android_clang_compiler_uses_target_arg_internally(&tool.path) { if let Some(path) = tool.path.file_name() { let file_name = path.to_str().unwrap().to_owned(); let (target, clang) = file_name.split_at(file_name.rfind('-').unwrap()); @@ -2827,8 +2946,8 @@ impl Build { // of the box" experience. if let Some(cl_exe) = cl_exe { if tool.family == (ToolFamily::Msvc { clang_cl: true }) - && tool.env.len() == 0 - && target.contains("msvc") + && tool.env.is_empty() + && target.env == "msvc" { for (k, v) in cl_exe.env.iter() { tool.env.push((k.to_owned(), v.to_owned())); @@ -2836,7 +2955,7 @@ impl Build { } } - if target.contains("msvc") && tool.family == ToolFamily::Gnu { + if target.env == "msvc" && tool.family == ToolFamily::Gnu { self.cargo_output .print_warning(&"GNU compiler is not supported for this target"); } @@ -2845,37 +2964,40 @@ impl Build { } /// Returns a fallback `cc_compiler_wrapper` by introspecting `RUSTC_WRAPPER` - fn rustc_wrapper_fallback() -> Option { + fn rustc_wrapper_fallback(&self) -> Option> { // No explicit CC wrapper was detected, but check if RUSTC_WRAPPER // is defined and is a build accelerator that is compatible with // C/C++ compilers (e.g. sccache) - const VALID_WRAPPERS: &[&'static str] = &["sccache", "cachepot"]; + const VALID_WRAPPERS: &[&str] = &["sccache", "cachepot", "buildcache"]; - let rustc_wrapper = std::env::var_os("RUSTC_WRAPPER")?; + let rustc_wrapper = self.getenv("RUSTC_WRAPPER")?; let wrapper_path = Path::new(&rustc_wrapper); let wrapper_stem = wrapper_path.file_stem()?; if VALID_WRAPPERS.contains(&wrapper_stem.to_str()?) { - Some(rustc_wrapper.to_str()?.to_owned()) + Some(rustc_wrapper) } else { None } } /// Returns compiler path, optional modifier name from whitelist, and arguments vec - fn env_tool(&self, name: &str) -> Option<(PathBuf, Option, Vec)> { - let tool = match self.getenv_with_target_prefixes(name) { - Ok(tool) => tool, - Err(_) => return None, - }; + fn env_tool(&self, name: &str) -> Option<(PathBuf, Option>, Vec)> { + let tool = self.getenv_with_target_prefixes(name).ok()?; + let tool = tool.to_string_lossy(); + let tool = tool.trim(); + + if tool.is_empty() { + return None; + } // If this is an exact path on the filesystem we don't want to do any // interpretation at all, just pass it on through. This'll hopefully get // us to support spaces-in-paths. - if Path::new(&*tool).exists() { + if Path::new(tool).exists() { return Some(( - PathBuf::from(&*tool), - Self::rustc_wrapper_fallback(), + PathBuf::from(tool), + self.rustc_wrapper_fallback(), Vec::new(), )); } @@ -2900,24 +3022,28 @@ impl Build { // // It's true that everything here is a bit of a pain, but apparently if // you're not literally make or bash then you get a lot of bug reports. - let known_wrappers = ["ccache", "distcc", "sccache", "icecc", "cachepot"]; + let mut known_wrappers = vec![ + "ccache", + "distcc", + "sccache", + "icecc", + "cachepot", + "buildcache", + ]; + let custom_wrapper = self.getenv("CC_KNOWN_WRAPPER_CUSTOM"); + if custom_wrapper.is_some() { + known_wrappers.push(custom_wrapper.as_deref().unwrap().to_str().unwrap()); + } let mut parts = tool.split_whitespace(); - let maybe_wrapper = match parts.next() { - Some(s) => s, - None => return None, - }; + let maybe_wrapper = parts.next()?; - let file_stem = Path::new(maybe_wrapper) - .file_stem() - .unwrap() - .to_str() - .unwrap(); + let file_stem = Path::new(maybe_wrapper).file_stem()?.to_str()?; if known_wrappers.contains(&file_stem) { if let Some(compiler) = parts.next() { return Some(( compiler.into(), - Some(maybe_wrapper.to_string()), + Some(Arc::::from(OsStr::new(&maybe_wrapper))), parts.map(|s| s.to_string()).collect(), )); } @@ -2925,7 +3051,7 @@ impl Build { Some(( maybe_wrapper.into(), - Self::rustc_wrapper_fallback(), + self.rustc_wrapper_fallback(), parts.map(|s| s.to_string()).collect(), )) } @@ -2933,40 +3059,41 @@ impl Build { /// Returns the C++ standard library: /// 1. If [`cpp_link_stdlib`](cc::Build::cpp_link_stdlib) is set, uses its value. /// 2. Else if the `CXXSTDLIB` environment variable is set, uses its value. - /// 3. Else the default is `libc++` for OS X and BSDs, `libc++_shared` for Android, - /// `None` for MSVC and `libstdc++` for anything else. - fn get_cpp_link_stdlib(&self) -> Result, Error> { + /// 3. Else the default is `c++` for OS X and BSDs, `c++_shared` for Android, + /// `None` for MSVC and `stdc++` for anything else. + fn get_cpp_link_stdlib(&self) -> Result>, Error> { match &self.cpp_link_stdlib { - Some(s) => Ok(s.as_ref().map(|s| (*s).to_string())), + Some(s) => Ok(s.as_deref().map(Path::new).map(Cow::Borrowed)), None => { if let Ok(stdlib) = self.getenv_with_target_prefixes("CXXSTDLIB") { if stdlib.is_empty() { Ok(None) } else { - Ok(Some(stdlib.to_string())) + Ok(Some(Cow::Owned(Path::new(&stdlib).to_owned()))) } } else { let target = self.get_target()?; - if target.contains("msvc") { + if target.env == "msvc" { Ok(None) - } else if target.contains("apple") - | target.contains("freebsd") - | target.contains("openbsd") - | target.contains("aix") - | target.contains("linux-ohos") + } else if target.vendor == "apple" + || target.os == "freebsd" + || target.os == "openbsd" + || target.os == "aix" + || (target.os == "linux" && target.env == "ohos") + || target.os == "wasi" { - Ok(Some("c++".to_string())) - } else if target.contains("android") { - Ok(Some("c++_shared".to_string())) + Ok(Some(Cow::Borrowed(Path::new("c++")))) + } else if target.os == "android" { + Ok(Some(Cow::Borrowed(Path::new("c++_shared")))) } else { - Ok(Some("stdc++".to_string())) + Ok(Some(Cow::Borrowed(Path::new("stdc++")))) } } } } } - fn get_ar(&self) -> Result<(Command, String, bool), Error> { + fn get_ar(&self) -> Result<(Command, PathBuf, bool), Error> { self.try_get_archiver_and_flags() } @@ -2999,11 +3126,11 @@ impl Build { Ok(self.try_get_archiver_and_flags()?.0) } - fn try_get_archiver_and_flags(&self) -> Result<(Command, String, bool), Error> { + fn try_get_archiver_and_flags(&self) -> Result<(Command, PathBuf, bool), Error> { let (mut cmd, name) = self.get_base_archiver()?; let mut any_flags = false; if let Ok(flags) = self.envflags("ARFLAGS") { - any_flags = any_flags | !flags.is_empty(); + any_flags |= !flags.is_empty(); cmd.args(flags); } for flag in &self.ar_flags { @@ -3013,9 +3140,10 @@ impl Build { Ok((cmd, name, any_flags)) } - fn get_base_archiver(&self) -> Result<(Command, String), Error> { + fn get_base_archiver(&self) -> Result<(Command, PathBuf), Error> { if let Some(ref a) = self.archiver { - return Ok((self.cmd(&**a), a.to_string_lossy().into_owned())); + let archiver = &**a; + return Ok((self.cmd(archiver), archiver.into())); } self.get_base_archiver_variant("AR", "ar") @@ -3062,39 +3190,48 @@ impl Build { Ok(self.get_base_archiver_variant("RANLIB", "ranlib")?.0) } - fn get_base_archiver_variant(&self, env: &str, tool: &str) -> Result<(Command, String), Error> { + fn get_base_archiver_variant( + &self, + env: &str, + tool: &str, + ) -> Result<(Command, PathBuf), Error> { let target = self.get_target()?; - let mut name = String::new(); + let mut name = PathBuf::new(); let tool_opt: Option = self .env_tool(env) .map(|(tool, _wrapper, args)| { + name.clone_from(&tool); let mut cmd = self.cmd(tool); cmd.args(args); cmd }) .or_else(|| { - if target.contains("emscripten") { + if target.os == "emscripten" { // Windows use bat files so we have to be a bit more specific if cfg!(windows) { let mut cmd = self.cmd("cmd"); - name = format!("em{}.bat", tool); + name = format!("em{}.bat", tool).into(); cmd.arg("/c").arg(&name); Some(cmd) } else { - name = format!("em{}", tool); + name = format!("em{}", tool).into(); Some(self.cmd(&name)) } - } else if target.starts_with("wasm32") { + } else if target.arch == "wasm32" || target.arch == "wasm64" { // Formally speaking one should be able to use this approach, // parsing -print-search-dirs output, to cover all clang targets, // including Android SDKs and other cross-compilation scenarios... // And even extend it to gcc targets by searching for "ar" instead // of "llvm-ar"... let compiler = self.get_base_compiler().ok()?; - if compiler.family == ToolFamily::Clang { - name = format!("llvm-{}", tool); - search_programs(&mut self.cmd(&compiler.path), &name, &self.cargo_output) - .map(|name| self.cmd(name)) + if compiler.is_like_clang() { + name = format!("llvm-{}", tool).into(); + self.search_programs( + &mut self.cmd(&compiler.path), + &name, + &self.cargo_output, + ) + .map(|name| self.cmd(name)) } else { None } @@ -3107,14 +3244,18 @@ impl Build { let tool = match tool_opt { Some(t) => t, None => { - if target.contains("android") { - name = format!("llvm-{}", tool); + if target.os == "android" { + name = format!("llvm-{}", tool).into(); match Command::new(&name).arg("--version").status() { Ok(status) if status.success() => (), - _ => name = format!("{}-{}", target.replace("armv7", "arm"), tool), + _ => { + // FIXME: Use parsed target. + let raw_target = self.get_raw_target()?; + name = format!("{}-{}", raw_target.replace("armv7", "arm"), tool).into() + } } self.cmd(&name) - } else if target.contains("msvc") { + } else if target.env == "msvc" { // NOTE: There isn't really a ranlib on msvc, so arguably we should return // `None` somehow here. But in general, callers will already have to be aware // of not running ranlib on Windows anyway, so it feels okay to return lib.exe @@ -3128,38 +3269,38 @@ impl Build { // next to 'clang-cl' and use 'search_programs()' to locate // 'llvm-lib'. This is because 'clang-cl' doesn't support // the -print-search-dirs option. - if let Some(mut cmd) = which(&compiler.path, None) { + if let Some(mut cmd) = self.which(&compiler.path, None) { cmd.pop(); cmd.push("llvm-lib.exe"); - if let Some(llvm_lib) = which(&cmd, None) { - lib = llvm_lib.to_str().unwrap().to_owned(); + if let Some(llvm_lib) = self.which(&cmd, None) { + llvm_lib.to_str().unwrap().clone_into(&mut lib); } } } if lib.is_empty() { - name = String::from("lib.exe"); - let mut cmd = match windows_registry::find(&target, "lib.exe") { + name = PathBuf::from("lib.exe"); + let mut cmd = match self.windows_registry_find(&target, "lib.exe") { Some(t) => t, None => self.cmd("lib.exe"), }; - if target.contains("arm64ec") { + if target.full_arch == "arm64ec" { cmd.arg("/machine:arm64ec"); } cmd } else { - name = lib; + name = lib.into(); self.cmd(&name) } - } else if target.contains("illumos") { + } else if target.os == "illumos" { // The default 'ar' on illumos uses a non-standard flags, // but the OS comes bundled with a GNU-compatible variant. // // Use the GNU-variant to match other Unix systems. - name = format!("g{}", tool); + name = format!("g{}", tool).into(); self.cmd(&name) - } else if self.get_host()? != target { - match self.prefix_for_target(&target) { + } else if self.get_is_cross_compile()? { + match self.prefix_for_target(&self.get_raw_target()?) { Some(p) => { // GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both. // Prefer -ar if it exists, as builds of `-gcc-ar` have been observed to be @@ -3176,16 +3317,16 @@ impl Build { break; } } - name = chosen; + name = chosen.into(); self.cmd(&name) } None => { - name = default; + name = default.into(); self.cmd(&name) } } } else { - name = default; + name = default.into(); self.cmd(&name) } } @@ -3194,135 +3335,145 @@ impl Build { Ok((tool, name)) } - fn prefix_for_target(&self, target: &str) -> Option { - // Put aside RUSTC_LINKER's prefix to be used as second choice, after CROSS_COMPILE - let linker_prefix = self - .getenv("RUSTC_LINKER") - .and_then(|var| var.strip_suffix("-gcc").map(str::to_string)); + // FIXME: Use parsed target instead of raw target. + fn prefix_for_target(&self, target: &str) -> Option> { // CROSS_COMPILE is of the form: "arm-linux-gnueabi-" - let cc_env = self.getenv("CROSS_COMPILE"); - let cross_compile = cc_env.as_ref().map(|s| s.trim_end_matches('-').to_owned()); - cross_compile.or(linker_prefix).or(match &target[..] { - // Note: there is no `aarch64-pc-windows-gnu` target, only `-gnullvm` - "aarch64-pc-windows-gnullvm" => Some("aarch64-w64-mingw32"), - "aarch64-uwp-windows-gnu" => Some("aarch64-w64-mingw32"), - "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu"), - "aarch64-unknown-linux-musl" => Some("aarch64-linux-musl"), - "aarch64-unknown-netbsd" => Some("aarch64--netbsd"), - "arm-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv4t-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv5te-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv5te-unknown-linux-musleabi" => Some("arm-linux-gnueabi"), - "arm-frc-linux-gnueabi" => Some("arm-frc-linux-gnueabi"), - "arm-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "arm-unknown-linux-musleabi" => Some("arm-linux-musleabi"), - "arm-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "arm-unknown-netbsd-eabi" => Some("arm--netbsdelf-eabi"), - "armv6-unknown-netbsd-eabihf" => Some("armv6--netbsdelf-eabihf"), - "armv7-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "armv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "armv7neon-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "armv7neon-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "thumbv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "thumbv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "thumbv7neon-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "thumbv7neon-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "armv7-unknown-netbsd-eabihf" => Some("armv7--netbsdelf-eabihf"), - "hexagon-unknown-linux-musl" => Some("hexagon-linux-musl"), - "i586-unknown-linux-musl" => Some("musl"), - "i686-pc-windows-gnu" => Some("i686-w64-mingw32"), - "i686-uwp-windows-gnu" => Some("i686-w64-mingw32"), - "i686-unknown-linux-gnu" => self.find_working_gnu_prefix(&[ - "i686-linux-gnu", - "x86_64-linux-gnu", // transparently support gcc-multilib - ]), // explicit None if not found, so caller knows to fall back - "i686-unknown-linux-musl" => Some("musl"), - "i686-unknown-netbsd" => Some("i486--netbsdelf"), - "loongarch64-unknown-linux-gnu" => Some("loongarch64-linux-gnu"), - "mips-unknown-linux-gnu" => Some("mips-linux-gnu"), - "mips-unknown-linux-musl" => Some("mips-linux-musl"), - "mipsel-unknown-linux-gnu" => Some("mipsel-linux-gnu"), - "mipsel-unknown-linux-musl" => Some("mipsel-linux-musl"), - "mips64-unknown-linux-gnuabi64" => Some("mips64-linux-gnuabi64"), - "mips64el-unknown-linux-gnuabi64" => Some("mips64el-linux-gnuabi64"), - "mipsisa32r6-unknown-linux-gnu" => Some("mipsisa32r6-linux-gnu"), - "mipsisa32r6el-unknown-linux-gnu" => Some("mipsisa32r6el-linux-gnu"), - "mipsisa64r6-unknown-linux-gnuabi64" => Some("mipsisa64r6-linux-gnuabi64"), - "mipsisa64r6el-unknown-linux-gnuabi64" => Some("mipsisa64r6el-linux-gnuabi64"), - "powerpc-unknown-linux-gnu" => Some("powerpc-linux-gnu"), - "powerpc-unknown-linux-gnuspe" => Some("powerpc-linux-gnuspe"), - "powerpc-unknown-netbsd" => Some("powerpc--netbsd"), - "powerpc64-unknown-linux-gnu" => Some("powerpc-linux-gnu"), - "powerpc64le-unknown-linux-gnu" => Some("powerpc64le-linux-gnu"), - "riscv32i-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv32imac-esp-espidf" => Some("riscv32-esp-elf"), - "riscv32imac-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv32imac-unknown-xous-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv32imc-esp-espidf" => Some("riscv32-esp-elf"), - "riscv32imc-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv64gc-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv64-unknown-elf", - "riscv32-unknown-elf", - "riscv-none-embed", - ]), - "riscv64imac-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv64-unknown-elf", - "riscv32-unknown-elf", - "riscv-none-embed", - ]), - "riscv64gc-unknown-linux-gnu" => Some("riscv64-linux-gnu"), - "riscv32gc-unknown-linux-gnu" => Some("riscv32-linux-gnu"), - "riscv64gc-unknown-linux-musl" => Some("riscv64-linux-musl"), - "riscv32gc-unknown-linux-musl" => Some("riscv32-linux-musl"), - "riscv64gc-unknown-netbsd" => Some("riscv64--netbsd"), - "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu"), - "sparc-unknown-linux-gnu" => Some("sparc-linux-gnu"), - "sparc64-unknown-linux-gnu" => Some("sparc64-linux-gnu"), - "sparc64-unknown-netbsd" => Some("sparc64--netbsd"), - "sparcv9-sun-solaris" => Some("sparcv9-sun-solaris"), - "armv7a-none-eabi" => Some("arm-none-eabi"), - "armv7a-none-eabihf" => Some("arm-none-eabi"), - "armebv7r-none-eabi" => Some("arm-none-eabi"), - "armebv7r-none-eabihf" => Some("arm-none-eabi"), - "armv7r-none-eabi" => Some("arm-none-eabi"), - "armv7r-none-eabihf" => Some("arm-none-eabi"), - "armv8r-none-eabihf" => Some("arm-none-eabi"), - "thumbv6m-none-eabi" => Some("arm-none-eabi"), - "thumbv7em-none-eabi" => Some("arm-none-eabi"), - "thumbv7em-none-eabihf" => Some("arm-none-eabi"), - "thumbv7m-none-eabi" => Some("arm-none-eabi"), - "thumbv8m.base-none-eabi" => Some("arm-none-eabi"), - "thumbv8m.main-none-eabi" => Some("arm-none-eabi"), - "thumbv8m.main-none-eabihf" => Some("arm-none-eabi"), - "x86_64-pc-windows-gnu" => Some("x86_64-w64-mingw32"), - "x86_64-pc-windows-gnullvm" => Some("x86_64-w64-mingw32"), - "x86_64-uwp-windows-gnu" => Some("x86_64-w64-mingw32"), - "x86_64-rumprun-netbsd" => Some("x86_64-rumprun-netbsd"), - "x86_64-unknown-linux-gnu" => self.find_working_gnu_prefix(&[ - "x86_64-linux-gnu", // rustfmt wrap - ]), // explicit None if not found, so caller knows to fall back - "x86_64-unknown-linux-musl" => Some("musl"), - "x86_64-unknown-netbsd" => Some("x86_64--netbsd"), - _ => None, - } - .map(|x| x.to_owned())) + self.getenv("CROSS_COMPILE") + .as_deref() + .map(|s| s.to_string_lossy().trim_end_matches('-').to_owned()) + .map(Cow::Owned) + .or_else(|| { + // Put aside RUSTC_LINKER's prefix to be used as second choice, after CROSS_COMPILE + self.getenv("RUSTC_LINKER").and_then(|var| { + var.to_string_lossy() + .strip_suffix("-gcc") + .map(str::to_string) + .map(Cow::Owned) + }) + }) + .or_else(|| { + match target { + // Note: there is no `aarch64-pc-windows-gnu` target, only `-gnullvm` + "aarch64-pc-windows-gnullvm" => Some("aarch64-w64-mingw32"), + "aarch64-uwp-windows-gnu" => Some("aarch64-w64-mingw32"), + "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu"), + "aarch64-unknown-linux-musl" => Some("aarch64-linux-musl"), + "aarch64-unknown-netbsd" => Some("aarch64--netbsd"), + "arm-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), + "armv4t-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), + "armv5te-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), + "armv5te-unknown-linux-musleabi" => Some("arm-linux-gnueabi"), + "arm-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), + "arm-unknown-linux-musleabi" => Some("arm-linux-musleabi"), + "arm-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), + "arm-unknown-netbsd-eabi" => Some("arm--netbsdelf-eabi"), + "armv6-unknown-netbsd-eabihf" => Some("armv6--netbsdelf-eabihf"), + "armv7-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), + "armv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), + "armv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), + "armv7neon-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), + "armv7neon-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), + "thumbv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), + "thumbv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), + "thumbv7neon-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), + "thumbv7neon-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), + "armv7-unknown-netbsd-eabihf" => Some("armv7--netbsdelf-eabihf"), + "hexagon-unknown-linux-musl" => Some("hexagon-linux-musl"), + "i586-unknown-linux-musl" => Some("musl"), + "i686-pc-windows-gnu" => Some("i686-w64-mingw32"), + "i686-pc-windows-gnullvm" => Some("i686-w64-mingw32"), + "i686-uwp-windows-gnu" => Some("i686-w64-mingw32"), + "i686-unknown-linux-gnu" => self.find_working_gnu_prefix(&[ + "i686-linux-gnu", + "x86_64-linux-gnu", // transparently support gcc-multilib + ]), // explicit None if not found, so caller knows to fall back + "i686-unknown-linux-musl" => Some("musl"), + "i686-unknown-netbsd" => Some("i486--netbsdelf"), + "loongarch64-unknown-linux-gnu" => Some("loongarch64-linux-gnu"), + "mips-unknown-linux-gnu" => Some("mips-linux-gnu"), + "mips-unknown-linux-musl" => Some("mips-linux-musl"), + "mipsel-unknown-linux-gnu" => Some("mipsel-linux-gnu"), + "mipsel-unknown-linux-musl" => Some("mipsel-linux-musl"), + "mips64-unknown-linux-gnuabi64" => Some("mips64-linux-gnuabi64"), + "mips64el-unknown-linux-gnuabi64" => Some("mips64el-linux-gnuabi64"), + "mipsisa32r6-unknown-linux-gnu" => Some("mipsisa32r6-linux-gnu"), + "mipsisa32r6el-unknown-linux-gnu" => Some("mipsisa32r6el-linux-gnu"), + "mipsisa64r6-unknown-linux-gnuabi64" => Some("mipsisa64r6-linux-gnuabi64"), + "mipsisa64r6el-unknown-linux-gnuabi64" => Some("mipsisa64r6el-linux-gnuabi64"), + "powerpc-unknown-linux-gnu" => Some("powerpc-linux-gnu"), + "powerpc-unknown-linux-gnuspe" => Some("powerpc-linux-gnuspe"), + "powerpc-unknown-netbsd" => Some("powerpc--netbsd"), + "powerpc64-unknown-linux-gnu" => Some("powerpc-linux-gnu"), + "powerpc64le-unknown-linux-gnu" => Some("powerpc64le-linux-gnu"), + "riscv32i-unknown-none-elf" => self.find_working_gnu_prefix(&[ + "riscv32-unknown-elf", + "riscv64-unknown-elf", + "riscv-none-embed", + ]), + "riscv32imac-esp-espidf" => Some("riscv32-esp-elf"), + "riscv32imac-unknown-none-elf" => self.find_working_gnu_prefix(&[ + "riscv32-unknown-elf", + "riscv64-unknown-elf", + "riscv-none-embed", + ]), + "riscv32imac-unknown-xous-elf" => self.find_working_gnu_prefix(&[ + "riscv32-unknown-elf", + "riscv64-unknown-elf", + "riscv-none-embed", + ]), + "riscv32imc-esp-espidf" => Some("riscv32-esp-elf"), + "riscv32imc-unknown-none-elf" => self.find_working_gnu_prefix(&[ + "riscv32-unknown-elf", + "riscv64-unknown-elf", + "riscv-none-embed", + ]), + "riscv64gc-unknown-none-elf" => self.find_working_gnu_prefix(&[ + "riscv64-unknown-elf", + "riscv32-unknown-elf", + "riscv-none-embed", + ]), + "riscv64imac-unknown-none-elf" => self.find_working_gnu_prefix(&[ + "riscv64-unknown-elf", + "riscv32-unknown-elf", + "riscv-none-embed", + ]), + "riscv64gc-unknown-linux-gnu" => Some("riscv64-linux-gnu"), + "riscv32gc-unknown-linux-gnu" => Some("riscv32-linux-gnu"), + "riscv64gc-unknown-linux-musl" => Some("riscv64-linux-musl"), + "riscv32gc-unknown-linux-musl" => Some("riscv32-linux-musl"), + "riscv64gc-unknown-netbsd" => Some("riscv64--netbsd"), + "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu"), + "sparc-unknown-linux-gnu" => Some("sparc-linux-gnu"), + "sparc64-unknown-linux-gnu" => Some("sparc64-linux-gnu"), + "sparc64-unknown-netbsd" => Some("sparc64--netbsd"), + "sparcv9-sun-solaris" => Some("sparcv9-sun-solaris"), + "armv7a-none-eabi" => Some("arm-none-eabi"), + "armv7a-none-eabihf" => Some("arm-none-eabi"), + "armebv7r-none-eabi" => Some("arm-none-eabi"), + "armebv7r-none-eabihf" => Some("arm-none-eabi"), + "armv7r-none-eabi" => Some("arm-none-eabi"), + "armv7r-none-eabihf" => Some("arm-none-eabi"), + "armv8r-none-eabihf" => Some("arm-none-eabi"), + "thumbv6m-none-eabi" => Some("arm-none-eabi"), + "thumbv7em-none-eabi" => Some("arm-none-eabi"), + "thumbv7em-none-eabihf" => Some("arm-none-eabi"), + "thumbv7m-none-eabi" => Some("arm-none-eabi"), + "thumbv8m.base-none-eabi" => Some("arm-none-eabi"), + "thumbv8m.main-none-eabi" => Some("arm-none-eabi"), + "thumbv8m.main-none-eabihf" => Some("arm-none-eabi"), + "x86_64-pc-windows-gnu" => Some("x86_64-w64-mingw32"), + "x86_64-pc-windows-gnullvm" => Some("x86_64-w64-mingw32"), + "x86_64-uwp-windows-gnu" => Some("x86_64-w64-mingw32"), + "x86_64-rumprun-netbsd" => Some("x86_64-rumprun-netbsd"), + "x86_64-unknown-linux-gnu" => self.find_working_gnu_prefix(&[ + "x86_64-linux-gnu", // rustfmt wrap + ]), // explicit None if not found, so caller knows to fall back + "x86_64-unknown-linux-musl" => Some("musl"), + "x86_64-unknown-netbsd" => Some("x86_64--netbsd"), + _ => None, + } + .map(Cow::Borrowed) + }) } /// Some platforms have multiple, compatible, canonical prefixes. Look through @@ -3335,7 +3486,7 @@ impl Build { // Loop through PATH entries searching for each toolchain. This ensures that we // are more likely to discover the toolchain early on, because chances are good // that the desired toolchain is in one of the higher-priority paths. - env::var_os("PATH") + self.getenv("PATH") .as_ref() .and_then(|path_entries| { env::split_paths(path_entries).find_map(|path_entry| { @@ -3348,56 +3499,70 @@ impl Build { None }) }) - .map(|prefix| *prefix) - .or_else(|| + .copied() // If no toolchain was found, provide the first toolchain that was passed in. // This toolchain has been shown not to exist, however it will appear in the // error that is shown to the user which should make it easier to search for // where it should be obtained. - prefixes.first().map(|prefix| *prefix)) + .or_else(|| prefixes.first().copied()) } - fn get_target(&self) -> Result, Error> { + fn get_target(&self) -> Result, Error> { match &self.target { - Some(t) => Ok(t.clone()), - None => self.getenv_unwrap("TARGET"), + Some(t) if Some(&**t) != self.getenv_unwrap_str("TARGET").ok().as_deref() => t.parse(), + // Fetch target information from environment if not set, or if the + // target was the same as the TARGET environment variable, in + // case the user did `build.target(&env::var("TARGET").unwrap())`. + _ => self + .build_cache + .target_info_parser + .parse_from_cargo_environment_variables(), } } - fn get_host(&self) -> Result, Error> { - match &self.host { - Some(h) => Ok(h.clone()), - None => self.getenv_unwrap("HOST"), + fn get_raw_target(&self) -> Result, Error> { + match &self.target { + Some(t) => Ok(Cow::Borrowed(t)), + None => self.getenv_unwrap_str("TARGET").map(Cow::Owned), } } - fn get_opt_level(&self) -> Result, Error> { + fn get_is_cross_compile(&self) -> Result { + let target = self.get_raw_target()?; + let host: Cow<'_, str> = match &self.host { + Some(h) => Cow::Borrowed(h), + None => Cow::Owned(self.getenv_unwrap_str("HOST")?), + }; + Ok(host != target) + } + + fn get_opt_level(&self) -> Result, Error> { match &self.opt_level { - Some(ol) => Ok(ol.clone()), - None => self.getenv_unwrap("OPT_LEVEL"), + Some(ol) => Ok(Cow::Borrowed(ol)), + None => self.getenv_unwrap_str("OPT_LEVEL").map(Cow::Owned), } } fn get_debug(&self) -> bool { - self.debug.unwrap_or_else(|| match self.getenv("DEBUG") { - Some(s) => &*s != "false", - None => false, - }) + self.debug.unwrap_or_else(|| self.getenv_boolean("DEBUG")) + } + + fn get_shell_escaped_flags(&self) -> bool { + self.shell_escaped_flags + .unwrap_or_else(|| self.getenv_boolean("CC_SHELL_ESCAPED_FLAGS")) } fn get_dwarf_version(&self) -> Option { // Tentatively matches the DWARF version defaults as of rustc 1.62. let target = self.get_target().ok()?; - if target.contains("android") - || target.contains("apple") - || target.contains("dragonfly") - || target.contains("freebsd") - || target.contains("netbsd") - || target.contains("openbsd") - || target.contains("windows-gnu") + if matches!( + target.os, + "android" | "dragonfly" | "freebsd" | "netbsd" | "openbsd" + ) || target.vendor == "apple" + || (target.os == "windows" && target.env == "gnu") { Some(2) - } else if target.contains("linux") { + } else if target.os == "linux" { Some(4) } else { None @@ -3411,7 +3576,9 @@ impl Build { fn get_out_dir(&self) -> Result, Error> { match &self.out_dir { Some(p) => Ok(Cow::Borrowed(&**p)), - None => env::var_os("OUT_DIR") + None => self + .getenv("OUT_DIR") + .as_deref() .map(PathBuf::from) .map(Cow::Owned) .ok_or_else(|| { @@ -3423,7 +3590,8 @@ impl Build { } } - fn getenv(&self, v: &str) -> Option> { + #[allow(clippy::disallowed_methods)] + fn getenv(&self, v: &str) -> Option> { // Returns true for environment variables cargo sets for build scripts: // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts // @@ -3438,22 +3606,38 @@ impl Build { _ => false, } } - let mut cache = self.env_cache.lock().unwrap(); - if let Some(val) = cache.get(v) { - return val.clone(); + if let Some(val) = self.build_cache.env_cache.read().unwrap().get(v).cloned() { + return val; } - if self.emit_rerun_if_env_changed && !provided_by_cargo(v) { + // Excluding `PATH` prevents spurious rebuilds on Windows, see + // for details. + if self.emit_rerun_if_env_changed && !provided_by_cargo(v) && v != "PATH" { self.cargo_output .print_metadata(&format_args!("cargo:rerun-if-env-changed={}", v)); } - let r = env::var(v).ok().map(Arc::from); - self.cargo_output - .print_metadata(&format_args!("{} = {:?}", v, r)); - cache.insert(v.to_string(), r.clone()); + let r = env::var_os(v).map(Arc::from); + self.cargo_output.print_metadata(&format_args!( + "{} = {}", + v, + OptionOsStrDisplay(r.as_deref()) + )); + self.build_cache + .env_cache + .write() + .unwrap() + .insert(v.into(), r.clone()); r } - fn getenv_unwrap(&self, v: &str) -> Result, Error> { + /// get boolean flag that is either true or false + fn getenv_boolean(&self, v: &str) -> bool { + match self.getenv(v) { + Some(s) => &*s != "0" && &*s != "false" && !s.is_empty(), + None => false, + } + } + + fn getenv_unwrap(&self, v: &str) -> Result, Error> { match self.getenv(v) { Some(s) => Ok(s), None => Err(Error::new( @@ -3463,10 +3647,23 @@ impl Build { } } - fn getenv_with_target_prefixes(&self, var_base: &str) -> Result, Error> { - let target = self.get_target()?; - let host = self.get_host()?; - let kind = if host == target { "HOST" } else { "TARGET" }; + fn getenv_unwrap_str(&self, v: &str) -> Result { + let env = self.getenv_unwrap(v)?; + env.to_str().map(String::from).ok_or_else(|| { + Error::new( + ErrorKind::EnvVarNotFound, + format!("Environment variable {} is not valid utf-8.", v), + ) + }) + } + + fn getenv_with_target_prefixes(&self, var_base: &str) -> Result, Error> { + let target = self.get_raw_target()?; + let kind = if self.get_is_cross_compile()? { + "TARGET" + } else { + "HOST" + }; let target_u = target.replace('-', "_"); let res = self .getenv(&format!("{}_{}", var_base, target)) @@ -3484,17 +3681,22 @@ impl Build { } fn envflags(&self, name: &str) -> Result, Error> { - Ok(self - .getenv_with_target_prefixes(name)? - .split_ascii_whitespace() - .map(|slice| slice.to_string()) - .collect()) + let env_os = self.getenv_with_target_prefixes(name)?; + let env = env_os.to_string_lossy(); + + if self.get_shell_escaped_flags() { + Ok(Shlex::new(&env).collect()) + } else { + Ok(env + .split_ascii_whitespace() + .map(ToString::to_string) + .collect()) + } } fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> { let target = self.get_target()?; - let host = self.get_host()?; - if host.contains("apple-darwin") && target.contains("apple-darwin") { + if cfg!(target_os = "macos") && target.os == "macos" { // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld", // although this is apparently ignored when using the linker at "/usr/bin/ld". @@ -3503,17 +3705,37 @@ impl Build { Ok(()) } - fn apple_sdk_root(&self, sdk: &str) -> Result { - if let Some(sdkroot) = env::var_os("SDKROOT") { - return Ok(sdkroot); - } - - let mut cache = self - .apple_sdk_root_cache - .lock() - .expect("apple_sdk_root_cache lock failed"); - if let Some(ret) = cache.get(sdk) { - return Ok(ret.clone()); + fn apple_sdk_root_inner(&self, sdk: &str) -> Result, Error> { + // Code copied from rustc's compiler/rustc_codegen_ssa/src/back/link.rs. + if let Some(sdkroot) = self.getenv("SDKROOT") { + let p = Path::new(&sdkroot); + let does_sdkroot_contain = |strings: &[&str]| { + let sdkroot_str = p.to_string_lossy(); + strings.iter().any(|s| sdkroot_str.contains(s)) + }; + match sdk { + // Ignore `SDKROOT` if it's clearly set for the wrong platform. + "appletvos" + if does_sdkroot_contain(&["TVSimulator.platform", "MacOSX.platform"]) => {} + "appletvsimulator" + if does_sdkroot_contain(&["TVOS.platform", "MacOSX.platform"]) => {} + "iphoneos" + if does_sdkroot_contain(&["iPhoneSimulator.platform", "MacOSX.platform"]) => {} + "iphonesimulator" + if does_sdkroot_contain(&["iPhoneOS.platform", "MacOSX.platform"]) => {} + "macosx10.15" + if does_sdkroot_contain(&["iPhoneOS.platform", "iPhoneSimulator.platform"]) => { + } + "watchos" + if does_sdkroot_contain(&["WatchSimulator.platform", "MacOSX.platform"]) => {} + "watchsimulator" + if does_sdkroot_contain(&["WatchOS.platform", "MacOSX.platform"]) => {} + "xros" if does_sdkroot_contain(&["XRSimulator.platform", "MacOSX.platform"]) => {} + "xrsimulator" if does_sdkroot_contain(&["XROS.platform", "MacOSX.platform"]) => {} + // Ignore `SDKROOT` if it's not a valid path. + _ if !p.is_absolute() || p == Path::new("/") || !p.exists() => {} + _ => return Ok(sdkroot), + } } let sdk_path = run_output( @@ -3534,25 +3756,48 @@ impl Build { )); } }; - let ret: OsString = sdk_path.trim().into(); - cache.insert(sdk.into(), ret.clone()); - Ok(ret) + Ok(Arc::from(OsStr::new(sdk_path.trim()))) } - fn apple_deployment_version(&self, os: AppleOs, arch_str: Option<&str>, sdk: &str) -> String { - let default_deployment_from_sdk = || { - let mut cache = self - .apple_versions_cache - .lock() - .expect("apple_versions_cache lock failed"); + fn apple_sdk_root(&self, target: &TargetInfo<'_>) -> Result, Error> { + let sdk = target.apple_sdk_name(); - if let Some(ret) = cache.get(sdk) { - return Some(ret.clone()); - } + if let Some(ret) = self + .build_cache + .apple_sdk_root_cache + .read() + .expect("apple_sdk_root_cache lock failed") + .get(sdk) + .cloned() + { + return Ok(ret); + } + let sdk_path = self.apple_sdk_root_inner(sdk)?; + self.build_cache + .apple_sdk_root_cache + .write() + .expect("apple_sdk_root_cache lock failed") + .insert(sdk.into(), sdk_path.clone()); + Ok(sdk_path) + } + + fn apple_deployment_target(&self, target: &TargetInfo<'_>) -> Arc { + let sdk = target.apple_sdk_name(); + if let Some(ret) = self + .build_cache + .apple_versions_cache + .read() + .expect("apple_versions_cache lock failed") + .get(sdk) + .cloned() + { + return ret; + } + let default_deployment_from_sdk = || -> Option> { let version = run_output( self.cmd("xcrun") - .arg("--show-sdk-platform-version") + .arg("--show-sdk-version") .arg("--sdk") .arg(sdk), "xcrun", @@ -3560,27 +3805,27 @@ impl Build { ) .ok()?; - let version = std::str::from_utf8(&version).ok()?.trim().to_owned(); - - cache.insert(sdk.into(), version.clone()); - Some(version) + Some(Arc::from(std::str::from_utf8(&version).ok()?.trim())) }; - let deployment_from_env = |name: &str| { - // note this isn't hit in production codepaths, its mostly just for tests which don't + let deployment_from_env = |name: &str| -> Option> { + // note that self.env isn't hit in production codepaths, its mostly just for tests which don't // set the real env - if let Some((_, v)) = self.env.iter().find(|(k, _)| &**k == OsStr::new(name)) { - Some(v.to_str().unwrap().to_string()) - } else { - env::var(name).ok() - } + self.env + .iter() + .find(|(k, _)| &**k == OsStr::new(name)) + .map(|(_, v)| v) + .cloned() + .or_else(|| self.getenv(name))? + .to_str() + .map(Arc::from) }; // Determines if the acquired deployment target is too low to support modern C++ on some Apple platform. // // A long time ago they used libstdc++, but since macOS 10.9 and iOS 7 libc++ has been the library the SDKs provide to link against. // If a `cc`` config wants to use C++, we round up to these versions as the baseline. - let maybe_cpp_version_baseline = |deployment_target_ver: String| -> Option { + let maybe_cpp_version_baseline = |deployment_target_ver: Arc| -> Option> { if !self.cpp { return Some(deployment_target_ver); } @@ -3589,8 +3834,8 @@ impl Build { .split('.') .map(|v| v.parse::().expect("integer version")); - match os { - AppleOs::MacOs => { + match target.os { + "macos" => { let major = deployment_target.next().unwrap_or(0); let minor = deployment_target.next().unwrap_or(0); @@ -3603,7 +3848,7 @@ impl Build { return None; } } - AppleOs::Ios => { + "ios" => { let major = deployment_target.next().unwrap_or(0); // If below 10.7, we ignore it and let the SDK's target definitions handle it. @@ -3615,7 +3860,7 @@ impl Build { return None; } } - // watchOS, tvOS, and others are all new enough that libc++ is their baseline. + // watchOS, tvOS, visionOS, and others are all new enough that libc++ is their baseline. _ => {} } @@ -3634,31 +3879,56 @@ impl Build { // // The ordering of env -> XCode SDK -> old rustc defaults is intentional for performance when using // an explicit target. - match os { - AppleOs::MacOs => deployment_from_env("MACOSX_DEPLOYMENT_TARGET") + let version: Arc = match target.os { + "macos" => deployment_from_env("MACOSX_DEPLOYMENT_TARGET") .and_then(maybe_cpp_version_baseline) .or_else(default_deployment_from_sdk) .unwrap_or_else(|| { - if arch_str == Some("aarch64") { + if target.arch == "aarch64" { "11.0".into() } else { - let default = "10.7"; - maybe_cpp_version_baseline(default.into()).unwrap_or_else(|| default.into()) + let default: Arc = Arc::from("10.7"); + maybe_cpp_version_baseline(default.clone()).unwrap_or(default) } }), - AppleOs::Ios => deployment_from_env("IPHONEOS_DEPLOYMENT_TARGET") + "ios" => deployment_from_env("IPHONEOS_DEPLOYMENT_TARGET") .and_then(maybe_cpp_version_baseline) .or_else(default_deployment_from_sdk) .unwrap_or_else(|| "7.0".into()), - AppleOs::WatchOs => deployment_from_env("WATCHOS_DEPLOYMENT_TARGET") + "watchos" => deployment_from_env("WATCHOS_DEPLOYMENT_TARGET") .or_else(default_deployment_from_sdk) .unwrap_or_else(|| "5.0".into()), - AppleOs::TvOs => deployment_from_env("TVOS_DEPLOYMENT_TARGET") + "tvos" => deployment_from_env("TVOS_DEPLOYMENT_TARGET") .or_else(default_deployment_from_sdk) .unwrap_or_else(|| "9.0".into()), + + "visionos" => deployment_from_env("XROS_DEPLOYMENT_TARGET") + .or_else(default_deployment_from_sdk) + .unwrap_or_else(|| "1.0".into()), + + os => unreachable!("unknown Apple OS: {}", os), + }; + + self.build_cache + .apple_versions_cache + .write() + .expect("apple_versions_cache lock failed") + .insert(sdk.into(), version.clone()); + + version + } + + fn wasi_sysroot(&self) -> Result, Error> { + if let Some(wasi_sysroot_path) = self.getenv("WASI_SYSROOT") { + Ok(wasi_sysroot_path) + } else { + Err(Error::new( + ErrorKind::EnvVarNotFound, + "Environment variable WASI_SYSROOT not defined. Download sysroot from GitHub & setup environment variable WASI_SYSROOT targeting the folder.", + )) } } @@ -3668,70 +3938,86 @@ impl Build { .filter(|file| file.extension() == Some(OsStr::new("cu"))) .count() } -} -impl Default for Build { - fn default() -> Build { - Build::new() - } -} + fn which(&self, tool: &Path, path_entries: Option<&OsStr>) -> Option { + fn check_exe(mut exe: PathBuf) -> Option { + let exe_ext = std::env::consts::EXE_EXTENSION; + let check = + exe.exists() || (!exe_ext.is_empty() && exe.set_extension(exe_ext) && exe.exists()); + check.then_some(exe) + } -fn fail(s: &str) -> ! { - eprintln!("\n\nerror occurred: {}\n\n", s); - std::process::exit(1); -} + // Loop through PATH entries searching for the |tool|. + let find_exe_in_path = |path_entries: &OsStr| -> Option { + env::split_paths(path_entries).find_map(|path_entry| check_exe(path_entry.join(tool))) + }; -#[derive(Clone, Copy, PartialEq)] -enum AppleOs { - MacOs, - Ios, - WatchOs, - TvOs, -} -impl std::fmt::Debug for AppleOs { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match self { - AppleOs::MacOs => f.write_str("macOS"), - AppleOs::Ios => f.write_str("iOS"), - AppleOs::WatchOs => f.write_str("WatchOS"), - AppleOs::TvOs => f.write_str("AppleTVOS"), + // If |tool| is not just one "word," assume it's an actual path... + if tool.components().count() > 1 { + check_exe(PathBuf::from(tool)) + } else { + path_entries + .and_then(find_exe_in_path) + .or_else(|| find_exe_in_path(&self.getenv("PATH")?)) } } -} -struct AppleSdkTargetParts { - sdk_prefix: &'static str, - sim_prefix: &'static str, - sdk: Cow<'static, str>, -} + /// search for |prog| on 'programs' path in '|cc| -print-search-dirs' output + fn search_programs( + &self, + cc: &mut Command, + prog: &Path, + cargo_output: &CargoOutput, + ) -> Option { + let search_dirs = run_output( + cc.arg("-print-search-dirs"), + "cc", + // this doesn't concern the compilation so we always want to show warnings. + cargo_output, + ) + .ok()?; + // clang driver appears to be forcing UTF-8 output even on Windows, + // hence from_utf8 is assumed to be usable in all cases. + let search_dirs = std::str::from_utf8(&search_dirs).ok()?; + for dirs in search_dirs.split(['\r', '\n']) { + if let Some(path) = dirs.strip_prefix("programs: =") { + return self.which(prog, Some(OsStr::new(path))); + } + } + None + } -fn apple_os_sdk_parts(os: AppleOs, arch: &AppleArchSpec) -> AppleSdkTargetParts { - let (sdk_prefix, sim_prefix) = match os { - AppleOs::MacOs => ("macosx", ""), - AppleOs::Ios => ("iphone", "ios-"), - AppleOs::WatchOs => ("watch", "watch"), - AppleOs::TvOs => ("appletv", "appletv"), - }; - let sdk = match arch { - AppleArchSpec::Device(_) if os == AppleOs::MacOs => Cow::Borrowed("macosx"), - AppleArchSpec::Device(_) => format!("{}os", sdk_prefix).into(), - AppleArchSpec::Simulator(_) => format!("{}simulator", sdk_prefix).into(), - AppleArchSpec::Catalyst(_) => Cow::Borrowed("macosx"), - }; + fn windows_registry_find(&self, target: &TargetInfo<'_>, tool: &str) -> Option { + self.windows_registry_find_tool(target, tool) + .map(|c| c.to_command()) + } + + fn windows_registry_find_tool(&self, target: &TargetInfo<'_>, tool: &str) -> Option { + struct BuildEnvGetter<'s>(&'s Build); + + impl windows_registry::EnvGetter for BuildEnvGetter<'_> { + fn get_env(&self, name: &str) -> Option { + self.0.getenv(name).map(windows_registry::Env::Arced) + } + } + + if target.env != "msvc" { + return None; + } - AppleSdkTargetParts { - sdk_prefix, - sim_prefix, - sdk, + windows_registry::find_tool_inner(target.full_arch, tool, &BuildEnvGetter(self)) } } -#[allow(dead_code)] -enum AppleArchSpec { - Device(&'static str), - Simulator(&'static str), - #[allow(dead_code)] - Catalyst(&'static str), +impl Default for Build { + fn default() -> Build { + Build::new() + } +} + +fn fail(s: &str) -> ! { + eprintln!("\n\nerror occurred in cc-rs: {}\n\n", s); + std::process::exit(1); } // Use by default minimum available API level @@ -3763,29 +4049,9 @@ fn android_clang_compiler_uses_target_arg_internally(clang_path: &Path) -> bool false } -#[test] -fn test_android_clang_compiler_uses_target_arg_internally() { - for version in 16..21 { - assert!(android_clang_compiler_uses_target_arg_internally( - &PathBuf::from(format!("armv7a-linux-androideabi{}-clang", version)) - )); - assert!(android_clang_compiler_uses_target_arg_internally( - &PathBuf::from(format!("armv7a-linux-androideabi{}-clang++", version)) - )); - } - assert!(!android_clang_compiler_uses_target_arg_internally( - &PathBuf::from("clang-i686-linux-android") - )); - assert!(!android_clang_compiler_uses_target_arg_internally( - &PathBuf::from("clang") - )); - assert!(!android_clang_compiler_uses_target_arg_internally( - &PathBuf::from("clang++") - )); -} - -fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) -> String { - let new_clang_key = match target { +// FIXME: Use parsed target. +fn autodetect_android_compiler(raw_target: &str, gnu: &str, clang: &str) -> String { + let new_clang_key = match raw_target { "aarch64-linux-android" => Some("aarch64"), "armv7-linux-androideabi" => Some("armv7a"), "i686-linux-android" => Some("i686"), @@ -3807,7 +4073,7 @@ fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) } } - let target = target + let target = raw_target .replace("armv7neon", "arm") .replace("armv7", "arm") .replace("thumbv7neon", "arm") @@ -3824,7 +4090,7 @@ fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) // if not, use clang if Command::new(&gnu_compiler).output().is_ok() { gnu_compiler - } else if host.contains("windows") && Command::new(&clang_compiler_cmd).output().is_ok() { + } else if cfg!(windows) && Command::new(&clang_compiler_cmd).output().is_ok() { clang_compiler_cmd } else { clang_compiler @@ -3832,68 +4098,21 @@ fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) } // Rust and clang/cc don't agree on how to name the target. -fn map_darwin_target_from_rust_to_compiler_architecture(target: &str) -> Option<&'static str> { - if target.contains("x86_64h") { - Some("x86_64h") - } else if target.contains("x86_64") { - Some("x86_64") - } else if target.contains("arm64e") { - Some("arm64e") - } else if target.contains("aarch64") { - Some("arm64") - } else if target.contains("i686") { - Some("i386") - } else if target.contains("powerpc") { - Some("ppc") - } else if target.contains("powerpc64") { - Some("ppc64") - } else { - None - } -} - -fn which(tool: &Path, path_entries: Option) -> Option { - fn check_exe(exe: &mut PathBuf) -> bool { - let exe_ext = std::env::consts::EXE_EXTENSION; - exe.exists() || (!exe_ext.is_empty() && exe.set_extension(exe_ext) && exe.exists()) - } - - // If |tool| is not just one "word," assume it's an actual path... - if tool.components().count() > 1 { - let mut exe = PathBuf::from(tool); - return if check_exe(&mut exe) { Some(exe) } else { None }; +fn map_darwin_target_from_rust_to_compiler_architecture<'a>(target: &TargetInfo<'a>) -> &'a str { + match target.full_arch { + "aarch64" => "arm64", + "arm64_32" => "arm64_32", + "arm64e" => "arm64e", + "armv7k" => "armv7k", + "armv7s" => "armv7s", + "i386" => "i386", + "i686" => "i386", + "powerpc" => "ppc", + "powerpc64" => "ppc64", + "x86_64" => "x86_64", + "x86_64h" => "x86_64h", + arch => arch, } - - // Loop through PATH entries searching for the |tool|. - let path_entries = path_entries.or(env::var_os("PATH"))?; - env::split_paths(&path_entries).find_map(|path_entry| { - let mut exe = path_entry.join(tool); - if check_exe(&mut exe) { - Some(exe) - } else { - None - } - }) -} - -// search for |prog| on 'programs' path in '|cc| -print-search-dirs' output -fn search_programs(cc: &mut Command, prog: &str, cargo_output: &CargoOutput) -> Option { - let search_dirs = run_output( - cc.arg("-print-search-dirs"), - "cc", - // this doesn't concern the compilation so we always want to show warnings. - cargo_output, - ) - .ok()?; - // clang driver appears to be forcing UTF-8 output even on Windows, - // hence from_utf8 is assumed to be usable in all cases. - let search_dirs = std::str::from_utf8(&search_dirs).ok()?; - for dirs in search_dirs.split(|c| c == '\r' || c == '\n') { - if let Some(path) = dirs.strip_prefix("programs: =") { - return which(Path::new(prog), Some(OsString::from(path))); - } - } - None } #[derive(Clone, Copy, PartialEq)] @@ -3920,3 +4139,73 @@ impl AsmFileExt { None } } + +/// Returns true if `cc` has been disabled by `CC_FORCE_DISABLE`. +fn is_disabled() -> bool { + static CACHE: AtomicU8 = AtomicU8::new(0); + + let val = CACHE.load(Relaxed); + // We manually cache the environment var, since we need it in some places + // where we don't have access to a `Build` instance. + #[allow(clippy::disallowed_methods)] + fn compute_is_disabled() -> bool { + match std::env::var_os("CC_FORCE_DISABLE") { + // Not set? Not disabled. + None => false, + // Respect `CC_FORCE_DISABLE=0` and some simple synonyms, otherwise + // we're disabled. This intentionally includes `CC_FORCE_DISABLE=""` + Some(v) => &*v != "0" && &*v != "false" && &*v != "no", + } + } + match val { + 2 => true, + 1 => false, + 0 => { + let truth = compute_is_disabled(); + let encoded_truth = if truth { 2u8 } else { 1 }; + // Might race against another thread, but we'd both be setting the + // same value so it should be fine. + CACHE.store(encoded_truth, Relaxed); + truth + } + _ => unreachable!(), + } +} + +/// Automates the `if is_disabled() { return error }` check and ensures +/// we produce a consistent error message for it. +fn check_disabled() -> Result<(), Error> { + if is_disabled() { + return Err(Error::new( + ErrorKind::Disabled, + "the `cc` crate's functionality has been disabled by the `CC_FORCE_DISABLE` environment variable." + )); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_android_clang_compiler_uses_target_arg_internally() { + for version in 16..21 { + assert!(android_clang_compiler_uses_target_arg_internally( + &PathBuf::from(format!("armv7a-linux-androideabi{}-clang", version)) + )); + assert!(android_clang_compiler_uses_target_arg_internally( + &PathBuf::from(format!("armv7a-linux-androideabi{}-clang++", version)) + )); + } + assert!(!android_clang_compiler_uses_target_arg_internally( + &PathBuf::from("clang-i686-linux-android") + )); + assert!(!android_clang_compiler_uses_target_arg_internally( + &PathBuf::from("clang") + )); + assert!(!android_clang_compiler_uses_target_arg_internally( + &PathBuf::from("clang++") + )); + } +} diff --git a/third_party/rust/cc/src/parallel/job_token.rs b/third_party/rust/cc/src/parallel/job_token.rs index 4fec982f8539d9..5528e1f8b49bc6 100644 --- a/third_party/rust/cc/src/parallel/job_token.rs +++ b/third_party/rust/cc/src/parallel/job_token.rs @@ -1,6 +1,6 @@ -use std::{marker::PhantomData, mem::MaybeUninit, sync::Once}; +use std::marker::PhantomData; -use crate::Error; +use crate::{utilities::OnceLock, Error}; pub(crate) struct JobToken(PhantomData<()>); @@ -34,19 +34,14 @@ impl JobTokenServer { /// that has to be static so that it will be shared by all cc /// compilation. fn new() -> &'static Self { - static INIT: Once = Once::new(); - static mut JOBSERVER: MaybeUninit = MaybeUninit::uninit(); - - unsafe { - INIT.call_once(|| { - let server = inherited_jobserver::JobServer::from_env() - .map(Self::Inherited) - .unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new())); - JOBSERVER = MaybeUninit::new(server); - }); - // TODO: Poor man's assume_init_ref, as that'd require a MSRV of 1.55. - &*JOBSERVER.as_ptr() - } + // TODO: Replace with a OnceLock once MSRV is 1.70 + static JOBSERVER: OnceLock = OnceLock::new(); + + JOBSERVER.get_or_init(|| { + unsafe { inherited_jobserver::JobServer::from_env() } + .map(Self::Inherited) + .unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new())) + }) } } @@ -56,19 +51,17 @@ pub(crate) enum ActiveJobTokenServer { } impl ActiveJobTokenServer { - pub(crate) fn new() -> Result { + pub(crate) fn new() -> Self { match JobTokenServer::new() { JobTokenServer::Inherited(inherited_jobserver) => { - inherited_jobserver.enter_active().map(Self::Inherited) - } - JobTokenServer::InProcess(inprocess_jobserver) => { - Ok(Self::InProcess(inprocess_jobserver)) + Self::Inherited(inherited_jobserver.enter_active()) } + JobTokenServer::InProcess(inprocess_jobserver) => Self::InProcess(inprocess_jobserver), } } - pub(crate) async fn acquire(&self) -> Result { - match &self { + pub(crate) async fn acquire(&mut self) -> Result { + match self { Self::Inherited(jobserver) => jobserver.acquire().await, Self::InProcess(jobserver) => Ok(jobserver.acquire().await), } @@ -139,32 +132,40 @@ mod inherited_jobserver { } } - pub(super) fn enter_active(&self) -> Result, Error> { - ActiveJobServer::new(self) + pub(super) fn enter_active(&self) -> ActiveJobServer<'_> { + ActiveJobServer { + jobserver: self, + helper_thread: None, + } } } - pub(crate) struct ActiveJobServer<'a> { - jobserver: &'a JobServer, - helper_thread: jobserver::HelperThread, + struct HelperThread { + inner: jobserver::HelperThread, /// When rx is dropped, all the token stored within it will be dropped. rx: mpsc::Receiver>, } - impl<'a> ActiveJobServer<'a> { - fn new(jobserver: &'a JobServer) -> Result { + impl HelperThread { + fn new(jobserver: &JobServer) -> Result { let (tx, rx) = mpsc::channel(); Ok(Self { rx, - helper_thread: jobserver.inner.clone().into_helper_thread(move |res| { + inner: jobserver.inner.clone().into_helper_thread(move |res| { let _ = tx.send(res); })?, - jobserver, }) } + } - pub(super) async fn acquire(&self) -> Result { + pub(crate) struct ActiveJobServer<'a> { + jobserver: &'a JobServer, + helper_thread: Option, + } + + impl<'a> ActiveJobServer<'a> { + pub(super) async fn acquire(&mut self) -> Result { let mut has_requested_token = false; loop { @@ -173,26 +174,41 @@ mod inherited_jobserver { break Ok(JobToken::new()); } - // Cold path, no global implicit token, obtain one - match self.rx.try_recv() { - Ok(res) => { - let acquired = res?; + match self.jobserver.inner.try_acquire() { + Ok(Some(acquired)) => { acquired.drop_without_releasing(); break Ok(JobToken::new()); } - Err(mpsc::TryRecvError::Disconnected) => { - break Err(Error::new( - ErrorKind::JobserverHelpThreadError, - "jobserver help thread has returned before ActiveJobServer is dropped", - )) - } - Err(mpsc::TryRecvError::Empty) => { - if !has_requested_token { - self.helper_thread.request_token(); - has_requested_token = true; + Ok(None) => YieldOnce::default().await, + Err(err) if err.kind() == io::ErrorKind::Unsupported => { + // Fallback to creating a help thread with blocking acquire + let helper_thread = if let Some(thread) = self.helper_thread.as_ref() { + thread + } else { + self.helper_thread + .insert(HelperThread::new(self.jobserver)?) + }; + + match helper_thread.rx.try_recv() { + Ok(res) => { + let acquired = res?; + acquired.drop_without_releasing(); + break Ok(JobToken::new()); + } + Err(mpsc::TryRecvError::Disconnected) => break Err(Error::new( + ErrorKind::JobserverHelpThreadError, + "jobserver help thread has returned before ActiveJobServer is dropped", + )), + Err(mpsc::TryRecvError::Empty) => { + if !has_requested_token { + helper_thread.inner.request_token(); + has_requested_token = true; + } + YieldOnce::default().await + } } - YieldOnce::default().await } + Err(err) => break Err(err.into()), } } } diff --git a/third_party/rust/cc/src/parallel/mod.rs b/third_party/rust/cc/src/parallel/mod.rs index d69146dc59a1e3..019eae102f51bd 100644 --- a/third_party/rust/cc/src/parallel/mod.rs +++ b/third_party/rust/cc/src/parallel/mod.rs @@ -1,20 +1,3 @@ pub(crate) mod async_executor; pub(crate) mod job_token; pub(crate) mod stderr; - -/// Remove all element in `vec` which `f(element)` returns `false`. -/// -/// TODO: Remove this once the MSRV is bumped to v1.61 -pub(crate) fn retain_unordered_mut(vec: &mut Vec, mut f: F) -where - F: FnMut(&mut T) -> bool, -{ - let mut i = 0; - while i < vec.len() { - if f(&mut vec[i]) { - i += 1; - } else { - vec.swap_remove(i); - } - } -} diff --git a/third_party/rust/cc/src/parallel/stderr.rs b/third_party/rust/cc/src/parallel/stderr.rs index 47fa085dba430a..701868609f661f 100644 --- a/third_party/rust/cc/src/parallel/stderr.rs +++ b/third_party/rust/cc/src/parallel/stderr.rs @@ -1,9 +1,10 @@ +#![cfg_attr(target_family = "wasm", allow(unused))] /// Helpers functions for [ChildStderr]. use std::{convert::TryInto, process::ChildStderr}; use crate::{Error, ErrorKind}; -#[cfg(all(not(unix), not(windows)))] +#[cfg(all(not(unix), not(windows), not(target_family = "wasm")))] compile_error!("Only unix and windows support non-blocking pipes! For other OSes, disable the parallel feature."); #[cfg(unix)] diff --git a/third_party/rust/cc/src/target.rs b/third_party/rust/cc/src/target.rs new file mode 100644 index 00000000000000..3d6cb9110b5561 --- /dev/null +++ b/third_party/rust/cc/src/target.rs @@ -0,0 +1,132 @@ +//! Very basic parsing of `rustc` target triples. +//! +//! See the `target-lexicon` crate for a more principled approach to this. + +use std::str::FromStr; + +use crate::{Error, ErrorKind}; + +mod apple; +mod generated; +mod llvm; +mod parser; + +pub(crate) use parser::TargetInfoParser; + +/// Information specific to a `rustc` target. +/// +/// See . +#[derive(Debug, PartialEq, Clone)] +pub(crate) struct TargetInfo<'a> { + /// The full architecture, including the subarchitecture. + /// + /// This differs from `cfg!(target_arch)`, which only specifies the + /// overall architecture, which is too coarse for certain cases. + pub full_arch: &'a str, + /// The overall target architecture. + /// + /// This is the same as the value of `cfg!(target_arch)`. + pub arch: &'a str, + /// The target vendor. + /// + /// This is the same as the value of `cfg!(target_vendor)`. + pub vendor: &'a str, + /// The operating system, or `none` on bare-metal targets. + /// + /// This is the same as the value of `cfg!(target_os)`. + pub os: &'a str, + /// The environment on top of the operating system. + /// + /// This is the same as the value of `cfg!(target_env)`. + pub env: &'a str, + /// The ABI on top of the operating system. + /// + /// This is the same as the value of `cfg!(target_abi)`. + pub abi: &'a str, + /// The unversioned LLVM/Clang target triple. + /// + /// NOTE: You should never need to match on this explicitly, use the other + /// fields on [`TargetInfo`] instead. + pub llvm_target: &'a str, +} + +impl FromStr for TargetInfo<'_> { + type Err = Error; + + /// This will fail when using a custom target triple unknown to `rustc`. + fn from_str(target_triple: &str) -> Result { + if let Ok(index) = + generated::LIST.binary_search_by_key(&target_triple, |(target_triple, _)| target_triple) + { + let (_, info) = &generated::LIST[index]; + Ok(info.clone()) + } else { + Err(Error::new( + ErrorKind::UnknownTarget, + format!( + "unknown target `{target_triple}`. + +NOTE: `cc-rs` only supports a fixed set of targets when not in a build script. +- If adding a new target, you will need to fork of `cc-rs` until the target + has landed on nightly and the auto-generated list has been updated. See also + the `rustc` dev guide on adding a new target: + https://rustc-dev-guide.rust-lang.org/building/new-target.html +- If using a custom target, prefer to upstream it to `rustc` if possible, + otherwise open an issue with `cc-rs`: + https://github.com/rust-lang/cc-rs/issues/new +" + ), + )) + } + } +} + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use super::TargetInfo; + + // Test tier 1 targets + #[test] + fn tier1() { + let targets = [ + "aarch64-unknown-linux-gnu", + "aarch64-apple-darwin", + "i686-pc-windows-gnu", + "i686-pc-windows-msvc", + "i686-unknown-linux-gnu", + "x86_64-apple-darwin", + "x86_64-pc-windows-gnu", + "x86_64-pc-windows-msvc", + "x86_64-unknown-linux-gnu", + ]; + + for target in targets { + // Check that it parses + let _ = TargetInfo::from_str(target).unwrap(); + } + } + + // Various custom target triples not (or no longer) known by `rustc` + #[test] + fn cannot_parse_extra() { + let targets = [ + "aarch64-unknown-none-gnu", + "aarch64-uwp-windows-gnu", + "arm-frc-linux-gnueabi", + "arm-unknown-netbsd-eabi", + "armv7neon-unknown-linux-gnueabihf", + "armv7neon-unknown-linux-musleabihf", + "thumbv7-unknown-linux-gnueabihf", + "thumbv7-unknown-linux-musleabihf", + "x86_64-rumprun-netbsd", + "x86_64-unknown-linux", + ]; + + for target in targets { + // Check that it does not parse + let _ = TargetInfo::from_str(target).unwrap_err(); + } + } +} diff --git a/third_party/rust/cc/src/target/apple.rs b/third_party/rust/cc/src/target/apple.rs new file mode 100644 index 00000000000000..bd65e00f90bd3a --- /dev/null +++ b/third_party/rust/cc/src/target/apple.rs @@ -0,0 +1,49 @@ +use super::TargetInfo; + +impl TargetInfo<'_> { + pub(crate) fn apple_sdk_name(&self) -> &'static str { + match (self.os, self.abi) { + ("macos", "") => "macosx", + ("ios", "") => "iphoneos", + ("ios", "sim") => "iphonesimulator", + ("ios", "macabi") => "macosx", + ("tvos", "") => "appletvos", + ("tvos", "sim") => "appletvsimulator", + ("watchos", "") => "watchos", + ("watchos", "sim") => "watchsimulator", + ("visionos", "") => "xros", + ("visionos", "sim") => "xrsimulator", + (os, _) => panic!("invalid Apple target OS {}", os), + } + } + + pub(crate) fn apple_version_flag(&self, min_version: &str) -> String { + // There are many aliases for these, and `-mtargetos=` is preferred on Clang nowadays, but + // for compatibility with older Clang, we use the earliest supported name here. + // + // NOTE: GCC does not support `-miphoneos-version-min=` etc. (because it does not support + // iOS in general), but we specify them anyhow in case we actually have a Clang-like + // compiler disguised as a GNU-like compiler, or in case GCC adds support for these in the + // future. + // + // See also: + // https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mmacos-version-min + // https://clang.llvm.org/docs/AttributeReference.html#availability + // https://gcc.gnu.org/onlinedocs/gcc/Darwin-Options.html#index-mmacosx-version-min + match (self.os, self.abi) { + ("macos", "") => format!("-mmacosx-version-min={min_version}"), + ("ios", "") => format!("-miphoneos-version-min={min_version}"), + ("ios", "sim") => format!("-mios-simulator-version-min={min_version}"), + ("ios", "macabi") => format!("-mtargetos=ios{min_version}-macabi"), + ("tvos", "") => format!("-mappletvos-version-min={min_version}"), + ("tvos", "sim") => format!("-mappletvsimulator-version-min={min_version}"), + ("watchos", "") => format!("-mwatchos-version-min={min_version}"), + ("watchos", "sim") => format!("-mwatchsimulator-version-min={min_version}"), + // `-mxros-version-min` does not exist + // https://github.com/llvm/llvm-project/issues/88271 + ("visionos", "") => format!("-mtargetos=xros{min_version}"), + ("visionos", "sim") => format!("-mtargetos=xros{min_version}-simulator"), + (os, _) => panic!("invalid Apple target OS {}", os), + } + } +} diff --git a/third_party/rust/cc/src/target/generated.rs b/third_party/rust/cc/src/target/generated.rs new file mode 100644 index 00000000000000..c52cf53bdb1d58 --- /dev/null +++ b/third_party/rust/cc/src/target/generated.rs @@ -0,0 +1,3511 @@ +//! This file is generated code. Please edit the generator +//! in dev-tools/gen-target-info if you need to make changes. + +use super::TargetInfo; + +pub(crate) const LIST: &[(&str, TargetInfo<'static>)] = &[ + ( + "aarch64-apple-darwin", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "macos", + env: "", + abi: "", + llvm_target: "arm64-apple-macosx", + }, + ), + ( + "aarch64-apple-ios", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "ios", + env: "", + abi: "", + llvm_target: "arm64-apple-ios", + }, + ), + ( + "aarch64-apple-ios-macabi", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "ios", + env: "", + abi: "macabi", + llvm_target: "arm64-apple-ios-macabi", + }, + ), + ( + "aarch64-apple-ios-sim", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "ios", + env: "", + abi: "sim", + llvm_target: "arm64-apple-ios-simulator", + }, + ), + ( + "aarch64-apple-tvos", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "tvos", + env: "", + abi: "", + llvm_target: "arm64-apple-tvos", + }, + ), + ( + "aarch64-apple-tvos-sim", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "tvos", + env: "", + abi: "sim", + llvm_target: "arm64-apple-tvos-simulator", + }, + ), + ( + "aarch64-apple-visionos", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "visionos", + env: "", + abi: "", + llvm_target: "arm64-apple-xros", + }, + ), + ( + "aarch64-apple-visionos-sim", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "visionos", + env: "", + abi: "sim", + llvm_target: "arm64-apple-xros-simulator", + }, + ), + ( + "aarch64-apple-watchos", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "watchos", + env: "", + abi: "", + llvm_target: "arm64-apple-watchos", + }, + ), + ( + "aarch64-apple-watchos-sim", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "apple", + os: "watchos", + env: "", + abi: "sim", + llvm_target: "arm64-apple-watchos-simulator", + }, + ), + ( + "aarch64-fuchsia", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "fuchsia", + env: "", + abi: "", + llvm_target: "aarch64-fuchsia", + }, + ), + ( + "aarch64-kmc-solid_asp3", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "kmc", + os: "solid_asp3", + env: "", + abi: "", + llvm_target: "aarch64-unknown-none", + }, + ), + ( + "aarch64-linux-android", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "android", + env: "", + abi: "", + llvm_target: "aarch64-linux-android", + }, + ), + ( + "aarch64-nintendo-switch-freestanding", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "nintendo", + os: "horizon", + env: "", + abi: "", + llvm_target: "aarch64-unknown-none", + }, + ), + ( + "aarch64-pc-windows-gnullvm", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "pc", + os: "windows", + env: "gnu", + abi: "llvm", + llvm_target: "aarch64-pc-windows-gnu", + }, + ), + ( + "aarch64-pc-windows-msvc", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "pc", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "aarch64-pc-windows-msvc", + }, + ), + ( + "aarch64-unknown-freebsd", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "aarch64-unknown-freebsd", + }, + ), + ( + "aarch64-unknown-fuchsia", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "fuchsia", + env: "", + abi: "", + llvm_target: "aarch64-unknown-fuchsia", + }, + ), + ( + "aarch64-unknown-hermit", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "hermit", + env: "", + abi: "", + llvm_target: "aarch64-unknown-hermit", + }, + ), + ( + "aarch64-unknown-illumos", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "illumos", + env: "", + abi: "", + llvm_target: "aarch64-unknown-solaris2.11", + }, + ), + ( + "aarch64-unknown-linux-gnu", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "aarch64-unknown-linux-gnu", + }, + ), + ( + "aarch64-unknown-linux-gnu_ilp32", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "ilp32", + llvm_target: "aarch64-unknown-linux-gnu_ilp32", + }, + ), + ( + "aarch64-unknown-linux-musl", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "aarch64-unknown-linux-musl", + }, + ), + ( + "aarch64-unknown-linux-ohos", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "linux", + env: "ohos", + abi: "", + llvm_target: "aarch64-unknown-linux-ohos", + }, + ), + ( + "aarch64-unknown-netbsd", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "aarch64-unknown-netbsd", + }, + ), + ( + "aarch64-unknown-none", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "aarch64-unknown-none", + }, + ), + ( + "aarch64-unknown-none-softfloat", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "none", + env: "", + abi: "softfloat", + llvm_target: "aarch64-unknown-none", + }, + ), + ( + "aarch64-unknown-nto-qnx700", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "nto", + env: "nto70", + abi: "", + llvm_target: "aarch64-unknown-unknown", + }, + ), + ( + "aarch64-unknown-nto-qnx710", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "nto", + env: "nto71", + abi: "", + llvm_target: "aarch64-unknown-unknown", + }, + ), + ( + "aarch64-unknown-nto-qnx710_iosock", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "nto", + env: "nto71_iosock", + abi: "", + llvm_target: "aarch64-unknown-unknown", + }, + ), + ( + "aarch64-unknown-nto-qnx800", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "nto", + env: "nto80", + abi: "", + llvm_target: "aarch64-unknown-unknown", + }, + ), + ( + "aarch64-unknown-nuttx", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "", + llvm_target: "aarch64-unknown-none", + }, + ), + ( + "aarch64-unknown-openbsd", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "aarch64-unknown-openbsd", + }, + ), + ( + "aarch64-unknown-redox", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "redox", + env: "relibc", + abi: "", + llvm_target: "aarch64-unknown-redox", + }, + ), + ( + "aarch64-unknown-teeos", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "teeos", + env: "", + abi: "", + llvm_target: "aarch64-unknown-none", + }, + ), + ( + "aarch64-unknown-trusty", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "trusty", + env: "", + abi: "", + llvm_target: "aarch64-unknown-unknown-musl", + }, + ), + ( + "aarch64-unknown-uefi", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "unknown", + os: "uefi", + env: "", + abi: "", + llvm_target: "aarch64-unknown-windows-gnu", + }, + ), + ( + "aarch64-uwp-windows-msvc", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "uwp", + os: "windows", + env: "msvc", + abi: "uwp", + llvm_target: "aarch64-pc-windows-msvc", + }, + ), + ( + "aarch64-wrs-vxworks", + TargetInfo { + full_arch: "aarch64", + arch: "aarch64", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "aarch64-unknown-linux-gnu", + }, + ), + ( + "aarch64_be-unknown-linux-gnu", + TargetInfo { + full_arch: "aarch64_be", + arch: "aarch64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "aarch64_be-unknown-linux-gnu", + }, + ), + ( + "aarch64_be-unknown-linux-gnu_ilp32", + TargetInfo { + full_arch: "aarch64_be", + arch: "aarch64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "ilp32", + llvm_target: "aarch64_be-unknown-linux-gnu_ilp32", + }, + ), + ( + "aarch64_be-unknown-netbsd", + TargetInfo { + full_arch: "aarch64_be", + arch: "aarch64", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "aarch64_be-unknown-netbsd", + }, + ), + ( + "arm-linux-androideabi", + TargetInfo { + full_arch: "arm", + arch: "arm", + vendor: "unknown", + os: "android", + env: "", + abi: "eabi", + llvm_target: "arm-linux-androideabi", + }, + ), + ( + "arm-unknown-linux-gnueabi", + TargetInfo { + full_arch: "arm", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabi", + llvm_target: "arm-unknown-linux-gnueabi", + }, + ), + ( + "arm-unknown-linux-gnueabihf", + TargetInfo { + full_arch: "arm", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabihf", + llvm_target: "arm-unknown-linux-gnueabihf", + }, + ), + ( + "arm-unknown-linux-musleabi", + TargetInfo { + full_arch: "arm", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "eabi", + llvm_target: "arm-unknown-linux-musleabi", + }, + ), + ( + "arm-unknown-linux-musleabihf", + TargetInfo { + full_arch: "arm", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "eabihf", + llvm_target: "arm-unknown-linux-musleabihf", + }, + ), + ( + "arm64_32-apple-watchos", + TargetInfo { + full_arch: "arm64_32", + arch: "aarch64", + vendor: "apple", + os: "watchos", + env: "", + abi: "", + llvm_target: "arm64_32-apple-watchos", + }, + ), + ( + "arm64e-apple-darwin", + TargetInfo { + full_arch: "arm64e", + arch: "aarch64", + vendor: "apple", + os: "macos", + env: "", + abi: "", + llvm_target: "arm64e-apple-macosx", + }, + ), + ( + "arm64e-apple-ios", + TargetInfo { + full_arch: "arm64e", + arch: "aarch64", + vendor: "apple", + os: "ios", + env: "", + abi: "", + llvm_target: "arm64e-apple-ios", + }, + ), + ( + "arm64e-apple-tvos", + TargetInfo { + full_arch: "arm64e", + arch: "aarch64", + vendor: "apple", + os: "tvos", + env: "", + abi: "", + llvm_target: "arm64e-apple-tvos", + }, + ), + ( + "arm64ec-pc-windows-msvc", + TargetInfo { + full_arch: "arm64ec", + arch: "arm64ec", + vendor: "pc", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "arm64ec-pc-windows-msvc", + }, + ), + ( + "armeb-unknown-linux-gnueabi", + TargetInfo { + full_arch: "armeb", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabi", + llvm_target: "armeb-unknown-linux-gnueabi", + }, + ), + ( + "armebv7r-none-eabi", + TargetInfo { + full_arch: "armebv7r", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "armebv7r-none-eabi", + }, + ), + ( + "armebv7r-none-eabihf", + TargetInfo { + full_arch: "armebv7r", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabihf", + llvm_target: "armebv7r-none-eabihf", + }, + ), + ( + "armv4t-none-eabi", + TargetInfo { + full_arch: "armv4t", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "armv4t-none-eabi", + }, + ), + ( + "armv4t-unknown-linux-gnueabi", + TargetInfo { + full_arch: "armv4t", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabi", + llvm_target: "armv4t-unknown-linux-gnueabi", + }, + ), + ( + "armv5te-none-eabi", + TargetInfo { + full_arch: "armv5te", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "armv5te-none-eabi", + }, + ), + ( + "armv5te-unknown-linux-gnueabi", + TargetInfo { + full_arch: "armv5te", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabi", + llvm_target: "armv5te-unknown-linux-gnueabi", + }, + ), + ( + "armv5te-unknown-linux-musleabi", + TargetInfo { + full_arch: "armv5te", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "eabi", + llvm_target: "armv5te-unknown-linux-musleabi", + }, + ), + ( + "armv5te-unknown-linux-uclibceabi", + TargetInfo { + full_arch: "armv5te", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "uclibc", + abi: "eabi", + llvm_target: "armv5te-unknown-linux-uclibcgnueabi", + }, + ), + ( + "armv6-unknown-freebsd", + TargetInfo { + full_arch: "armv6", + arch: "arm", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "eabihf", + llvm_target: "armv6-unknown-freebsd-gnueabihf", + }, + ), + ( + "armv6-unknown-netbsd-eabihf", + TargetInfo { + full_arch: "armv6", + arch: "arm", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "eabihf", + llvm_target: "armv6-unknown-netbsdelf-eabihf", + }, + ), + ( + "armv6k-nintendo-3ds", + TargetInfo { + full_arch: "armv6k", + arch: "arm", + vendor: "nintendo", + os: "horizon", + env: "newlib", + abi: "eabihf", + llvm_target: "armv6k-none-eabihf", + }, + ), + ( + "armv7-apple-ios", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "apple", + os: "ios", + env: "", + abi: "", + llvm_target: "armv7-apple-ios", + }, + ), + ( + "armv7-linux-androideabi", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "android", + env: "", + abi: "eabi", + llvm_target: "armv7-none-linux-android", + }, + ), + ( + "armv7-rtems-eabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "rtems", + env: "newlib", + abi: "eabihf", + llvm_target: "armv7-unknown-none-eabihf", + }, + ), + ( + "armv7-sony-vita-newlibeabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "sony", + os: "vita", + env: "newlib", + abi: "eabihf", + llvm_target: "thumbv7a-vita-eabihf", + }, + ), + ( + "armv7-unknown-freebsd", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "eabihf", + llvm_target: "armv7-unknown-freebsd-gnueabihf", + }, + ), + ( + "armv7-unknown-linux-gnueabi", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabi", + llvm_target: "armv7-unknown-linux-gnueabi", + }, + ), + ( + "armv7-unknown-linux-gnueabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabihf", + llvm_target: "armv7-unknown-linux-gnueabihf", + }, + ), + ( + "armv7-unknown-linux-musleabi", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "eabi", + llvm_target: "armv7-unknown-linux-musleabi", + }, + ), + ( + "armv7-unknown-linux-musleabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "eabihf", + llvm_target: "armv7-unknown-linux-musleabihf", + }, + ), + ( + "armv7-unknown-linux-ohos", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "ohos", + abi: "eabi", + llvm_target: "armv7-unknown-linux-ohos", + }, + ), + ( + "armv7-unknown-linux-uclibceabi", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "uclibc", + abi: "eabi", + llvm_target: "armv7-unknown-linux-gnueabi", + }, + ), + ( + "armv7-unknown-linux-uclibceabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "uclibc", + abi: "eabihf", + llvm_target: "armv7-unknown-linux-gnueabihf", + }, + ), + ( + "armv7-unknown-netbsd-eabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "eabihf", + llvm_target: "armv7-unknown-netbsdelf-eabihf", + }, + ), + ( + "armv7-unknown-trusty", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "unknown", + os: "trusty", + env: "", + abi: "eabi", + llvm_target: "armv7-unknown-unknown-gnueabi", + }, + ), + ( + "armv7-wrs-vxworks-eabihf", + TargetInfo { + full_arch: "armv7", + arch: "arm", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "eabihf", + llvm_target: "armv7-unknown-linux-gnueabihf", + }, + ), + ( + "armv7a-kmc-solid_asp3-eabi", + TargetInfo { + full_arch: "armv7a", + arch: "arm", + vendor: "kmc", + os: "solid_asp3", + env: "", + abi: "eabi", + llvm_target: "armv7a-none-eabi", + }, + ), + ( + "armv7a-kmc-solid_asp3-eabihf", + TargetInfo { + full_arch: "armv7a", + arch: "arm", + vendor: "kmc", + os: "solid_asp3", + env: "", + abi: "eabihf", + llvm_target: "armv7a-none-eabihf", + }, + ), + ( + "armv7a-none-eabi", + TargetInfo { + full_arch: "armv7a", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "armv7a-none-eabi", + }, + ), + ( + "armv7a-none-eabihf", + TargetInfo { + full_arch: "armv7a", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabihf", + llvm_target: "armv7a-none-eabihf", + }, + ), + ( + "armv7a-nuttx-eabi", + TargetInfo { + full_arch: "armv7a", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "armv7a-none-eabi", + }, + ), + ( + "armv7a-nuttx-eabihf", + TargetInfo { + full_arch: "armv7a", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabihf", + llvm_target: "armv7a-none-eabihf", + }, + ), + ( + "armv7k-apple-watchos", + TargetInfo { + full_arch: "armv7k", + arch: "arm", + vendor: "apple", + os: "watchos", + env: "", + abi: "", + llvm_target: "armv7k-apple-watchos", + }, + ), + ( + "armv7r-none-eabi", + TargetInfo { + full_arch: "armv7r", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "armv7r-none-eabi", + }, + ), + ( + "armv7r-none-eabihf", + TargetInfo { + full_arch: "armv7r", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabihf", + llvm_target: "armv7r-none-eabihf", + }, + ), + ( + "armv7s-apple-ios", + TargetInfo { + full_arch: "armv7s", + arch: "arm", + vendor: "apple", + os: "ios", + env: "", + abi: "", + llvm_target: "armv7s-apple-ios", + }, + ), + ( + "armv8r-none-eabihf", + TargetInfo { + full_arch: "armv8r", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabihf", + llvm_target: "armv8r-none-eabihf", + }, + ), + ( + "asmjs-unknown-emscripten", + TargetInfo { + full_arch: "asmjs", + arch: "wasm32", + vendor: "unknown", + os: "emscripten", + env: "", + abi: "", + llvm_target: "wasm32-unknown-emscripten", + }, + ), + ( + "avr-unknown-gnu-atmega328", + TargetInfo { + full_arch: "avr", + arch: "avr", + vendor: "unknown", + os: "none", + env: "gnu", + abi: "", + llvm_target: "avr-unknown-unknown", + }, + ), + ( + "bpfeb-unknown-none", + TargetInfo { + full_arch: "bpfeb", + arch: "bpf", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "bpfeb", + }, + ), + ( + "bpfel-unknown-none", + TargetInfo { + full_arch: "bpfel", + arch: "bpf", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "bpfel", + }, + ), + ( + "csky-unknown-linux-gnuabiv2", + TargetInfo { + full_arch: "csky", + arch: "csky", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "abiv2", + llvm_target: "csky-unknown-linux-gnuabiv2", + }, + ), + ( + "csky-unknown-linux-gnuabiv2hf", + TargetInfo { + full_arch: "csky", + arch: "csky", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "abiv2hf", + llvm_target: "csky-unknown-linux-gnuabiv2", + }, + ), + ( + "hexagon-unknown-linux-musl", + TargetInfo { + full_arch: "hexagon", + arch: "hexagon", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "hexagon-unknown-linux-musl", + }, + ), + ( + "hexagon-unknown-none-elf", + TargetInfo { + full_arch: "hexagon", + arch: "hexagon", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "hexagon-unknown-none-elf", + }, + ), + ( + "i386-apple-ios", + TargetInfo { + full_arch: "i386", + arch: "x86", + vendor: "apple", + os: "ios", + env: "", + abi: "sim", + llvm_target: "i386-apple-ios-simulator", + }, + ), + ( + "i586-pc-nto-qnx700", + TargetInfo { + full_arch: "i586", + arch: "x86", + vendor: "pc", + os: "nto", + env: "nto70", + abi: "", + llvm_target: "i586-pc-unknown", + }, + ), + ( + "i586-pc-windows-msvc", + TargetInfo { + full_arch: "i586", + arch: "x86", + vendor: "pc", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "i586-pc-windows-msvc", + }, + ), + ( + "i586-unknown-linux-gnu", + TargetInfo { + full_arch: "i586", + arch: "x86", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "i586-unknown-linux-gnu", + }, + ), + ( + "i586-unknown-linux-musl", + TargetInfo { + full_arch: "i586", + arch: "x86", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "i586-unknown-linux-musl", + }, + ), + ( + "i586-unknown-netbsd", + TargetInfo { + full_arch: "i586", + arch: "x86", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "i586-unknown-netbsdelf", + }, + ), + ( + "i686-apple-darwin", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "apple", + os: "macos", + env: "", + abi: "", + llvm_target: "i686-apple-macosx", + }, + ), + ( + "i686-linux-android", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "android", + env: "", + abi: "", + llvm_target: "i686-linux-android", + }, + ), + ( + "i686-pc-windows-gnu", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "pc", + os: "windows", + env: "gnu", + abi: "", + llvm_target: "i686-pc-windows-gnu", + }, + ), + ( + "i686-pc-windows-gnullvm", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "pc", + os: "windows", + env: "gnu", + abi: "llvm", + llvm_target: "i686-pc-windows-gnu", + }, + ), + ( + "i686-pc-windows-msvc", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "pc", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "i686-pc-windows-msvc", + }, + ), + ( + "i686-unknown-freebsd", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "i686-unknown-freebsd", + }, + ), + ( + "i686-unknown-haiku", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "haiku", + env: "", + abi: "", + llvm_target: "i686-unknown-haiku", + }, + ), + ( + "i686-unknown-hurd-gnu", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "hurd", + env: "gnu", + abi: "", + llvm_target: "i686-unknown-hurd-gnu", + }, + ), + ( + "i686-unknown-linux-gnu", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "i686-unknown-linux-gnu", + }, + ), + ( + "i686-unknown-linux-musl", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "i686-unknown-linux-musl", + }, + ), + ( + "i686-unknown-netbsd", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "i686-unknown-netbsdelf", + }, + ), + ( + "i686-unknown-openbsd", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "i686-unknown-openbsd", + }, + ), + ( + "i686-unknown-redox", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "redox", + env: "relibc", + abi: "", + llvm_target: "i686-unknown-redox", + }, + ), + ( + "i686-unknown-uefi", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "unknown", + os: "uefi", + env: "", + abi: "", + llvm_target: "i686-unknown-windows-gnu", + }, + ), + ( + "i686-uwp-windows-gnu", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "uwp", + os: "windows", + env: "gnu", + abi: "uwp", + llvm_target: "i686-pc-windows-gnu", + }, + ), + ( + "i686-uwp-windows-msvc", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "uwp", + os: "windows", + env: "msvc", + abi: "uwp", + llvm_target: "i686-pc-windows-msvc", + }, + ), + ( + "i686-win7-windows-gnu", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "win7", + os: "windows", + env: "gnu", + abi: "", + llvm_target: "i686-pc-windows-gnu", + }, + ), + ( + "i686-win7-windows-msvc", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "win7", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "i686-pc-windows-msvc", + }, + ), + ( + "i686-wrs-vxworks", + TargetInfo { + full_arch: "i686", + arch: "x86", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "i686-unknown-linux-gnu", + }, + ), + ( + "loongarch64-unknown-linux-gnu", + TargetInfo { + full_arch: "loongarch64", + arch: "loongarch64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "loongarch64-unknown-linux-gnu", + }, + ), + ( + "loongarch64-unknown-linux-musl", + TargetInfo { + full_arch: "loongarch64", + arch: "loongarch64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "loongarch64-unknown-linux-musl", + }, + ), + ( + "loongarch64-unknown-linux-ohos", + TargetInfo { + full_arch: "loongarch64", + arch: "loongarch64", + vendor: "unknown", + os: "linux", + env: "ohos", + abi: "", + llvm_target: "loongarch64-unknown-linux-ohos", + }, + ), + ( + "loongarch64-unknown-none", + TargetInfo { + full_arch: "loongarch64", + arch: "loongarch64", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "loongarch64-unknown-none", + }, + ), + ( + "loongarch64-unknown-none-softfloat", + TargetInfo { + full_arch: "loongarch64", + arch: "loongarch64", + vendor: "unknown", + os: "none", + env: "", + abi: "softfloat", + llvm_target: "loongarch64-unknown-none", + }, + ), + ( + "m68k-unknown-linux-gnu", + TargetInfo { + full_arch: "m68k", + arch: "m68k", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "m68k-unknown-linux-gnu", + }, + ), + ( + "m68k-unknown-none-elf", + TargetInfo { + full_arch: "m68k", + arch: "m68k", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "m68k", + }, + ), + ( + "mips-mti-none-elf", + TargetInfo { + full_arch: "mips", + arch: "mips", + vendor: "mti", + os: "none", + env: "", + abi: "", + llvm_target: "mips", + }, + ), + ( + "mips-unknown-linux-gnu", + TargetInfo { + full_arch: "mips", + arch: "mips", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "mips-unknown-linux-gnu", + }, + ), + ( + "mips-unknown-linux-musl", + TargetInfo { + full_arch: "mips", + arch: "mips", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "mips-unknown-linux-musl", + }, + ), + ( + "mips-unknown-linux-uclibc", + TargetInfo { + full_arch: "mips", + arch: "mips", + vendor: "unknown", + os: "linux", + env: "uclibc", + abi: "", + llvm_target: "mips-unknown-linux-uclibc", + }, + ), + ( + "mips64-openwrt-linux-musl", + TargetInfo { + full_arch: "mips64", + arch: "mips64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "abi64", + llvm_target: "mips64-unknown-linux-musl", + }, + ), + ( + "mips64-unknown-linux-gnuabi64", + TargetInfo { + full_arch: "mips64", + arch: "mips64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "abi64", + llvm_target: "mips64-unknown-linux-gnuabi64", + }, + ), + ( + "mips64-unknown-linux-muslabi64", + TargetInfo { + full_arch: "mips64", + arch: "mips64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "abi64", + llvm_target: "mips64-unknown-linux-musl", + }, + ), + ( + "mips64el-unknown-linux-gnuabi64", + TargetInfo { + full_arch: "mips64el", + arch: "mips64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "abi64", + llvm_target: "mips64el-unknown-linux-gnuabi64", + }, + ), + ( + "mips64el-unknown-linux-muslabi64", + TargetInfo { + full_arch: "mips64el", + arch: "mips64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "abi64", + llvm_target: "mips64el-unknown-linux-musl", + }, + ), + ( + "mipsel-mti-none-elf", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "mti", + os: "none", + env: "", + abi: "", + llvm_target: "mipsel", + }, + ), + ( + "mipsel-sony-psp", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "sony", + os: "psp", + env: "", + abi: "", + llvm_target: "mipsel-sony-psp", + }, + ), + ( + "mipsel-sony-psx", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "sony", + os: "psx", + env: "", + abi: "", + llvm_target: "mipsel-sony-psx", + }, + ), + ( + "mipsel-unknown-linux-gnu", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "mipsel-unknown-linux-gnu", + }, + ), + ( + "mipsel-unknown-linux-musl", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "mipsel-unknown-linux-musl", + }, + ), + ( + "mipsel-unknown-linux-uclibc", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "unknown", + os: "linux", + env: "uclibc", + abi: "", + llvm_target: "mipsel-unknown-linux-uclibc", + }, + ), + ( + "mipsel-unknown-netbsd", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "mipsel-unknown-netbsd", + }, + ), + ( + "mipsel-unknown-none", + TargetInfo { + full_arch: "mipsel", + arch: "mips", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "mipsel-unknown-none", + }, + ), + ( + "mipsisa32r6-unknown-linux-gnu", + TargetInfo { + full_arch: "mipsisa32r6", + arch: "mips32r6", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "mipsisa32r6-unknown-linux-gnu", + }, + ), + ( + "mipsisa32r6el-unknown-linux-gnu", + TargetInfo { + full_arch: "mipsisa32r6el", + arch: "mips32r6", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "mipsisa32r6el-unknown-linux-gnu", + }, + ), + ( + "mipsisa64r6-unknown-linux-gnuabi64", + TargetInfo { + full_arch: "mipsisa64r6", + arch: "mips64r6", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "abi64", + llvm_target: "mipsisa64r6-unknown-linux-gnuabi64", + }, + ), + ( + "mipsisa64r6el-unknown-linux-gnuabi64", + TargetInfo { + full_arch: "mipsisa64r6el", + arch: "mips64r6", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "abi64", + llvm_target: "mipsisa64r6el-unknown-linux-gnuabi64", + }, + ), + ( + "msp430-none-elf", + TargetInfo { + full_arch: "msp430", + arch: "msp430", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "msp430-none-elf", + }, + ), + ( + "nvptx64-nvidia-cuda", + TargetInfo { + full_arch: "nvptx64", + arch: "nvptx64", + vendor: "nvidia", + os: "cuda", + env: "", + abi: "", + llvm_target: "nvptx64-nvidia-cuda", + }, + ), + ( + "powerpc-unknown-freebsd", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "powerpc-unknown-freebsd13.0", + }, + ), + ( + "powerpc-unknown-linux-gnu", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "powerpc-unknown-linux-gnu", + }, + ), + ( + "powerpc-unknown-linux-gnuspe", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "spe", + llvm_target: "powerpc-unknown-linux-gnuspe", + }, + ), + ( + "powerpc-unknown-linux-musl", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "powerpc-unknown-linux-musl", + }, + ), + ( + "powerpc-unknown-linux-muslspe", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "spe", + llvm_target: "powerpc-unknown-linux-muslspe", + }, + ), + ( + "powerpc-unknown-netbsd", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "powerpc-unknown-netbsd", + }, + ), + ( + "powerpc-unknown-openbsd", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "powerpc-unknown-openbsd", + }, + ), + ( + "powerpc-wrs-vxworks", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "powerpc-unknown-linux-gnu", + }, + ), + ( + "powerpc-wrs-vxworks-spe", + TargetInfo { + full_arch: "powerpc", + arch: "powerpc", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "spe", + llvm_target: "powerpc-unknown-linux-gnuspe", + }, + ), + ( + "powerpc64-ibm-aix", + TargetInfo { + full_arch: "powerpc64", + arch: "powerpc64", + vendor: "ibm", + os: "aix", + env: "", + abi: "vec-extabi", + llvm_target: "powerpc64-ibm-aix", + }, + ), + ( + "powerpc64-unknown-freebsd", + TargetInfo { + full_arch: "powerpc64", + arch: "powerpc64", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "powerpc64-unknown-freebsd", + }, + ), + ( + "powerpc64-unknown-linux-gnu", + TargetInfo { + full_arch: "powerpc64", + arch: "powerpc64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "powerpc64-unknown-linux-gnu", + }, + ), + ( + "powerpc64-unknown-linux-musl", + TargetInfo { + full_arch: "powerpc64", + arch: "powerpc64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "powerpc64-unknown-linux-musl", + }, + ), + ( + "powerpc64-unknown-openbsd", + TargetInfo { + full_arch: "powerpc64", + arch: "powerpc64", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "powerpc64-unknown-openbsd", + }, + ), + ( + "powerpc64-wrs-vxworks", + TargetInfo { + full_arch: "powerpc64", + arch: "powerpc64", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "powerpc64-unknown-linux-gnu", + }, + ), + ( + "powerpc64le-unknown-freebsd", + TargetInfo { + full_arch: "powerpc64le", + arch: "powerpc64", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "powerpc64le-unknown-freebsd", + }, + ), + ( + "powerpc64le-unknown-linux-gnu", + TargetInfo { + full_arch: "powerpc64le", + arch: "powerpc64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "powerpc64le-unknown-linux-gnu", + }, + ), + ( + "powerpc64le-unknown-linux-musl", + TargetInfo { + full_arch: "powerpc64le", + arch: "powerpc64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "powerpc64le-unknown-linux-musl", + }, + ), + ( + "riscv32-wrs-vxworks", + TargetInfo { + full_arch: "riscv32", + arch: "riscv32", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32e-unknown-none-elf", + TargetInfo { + full_arch: "riscv32e", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "ilp32e", + llvm_target: "riscv32", + }, + ), + ( + "riscv32em-unknown-none-elf", + TargetInfo { + full_arch: "riscv32em", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "ilp32e", + llvm_target: "riscv32", + }, + ), + ( + "riscv32emc-unknown-none-elf", + TargetInfo { + full_arch: "riscv32emc", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "ilp32e", + llvm_target: "riscv32", + }, + ), + ( + "riscv32gc-unknown-linux-gnu", + TargetInfo { + full_arch: "riscv32gc", + arch: "riscv32", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "riscv32-unknown-linux-gnu", + }, + ), + ( + "riscv32gc-unknown-linux-musl", + TargetInfo { + full_arch: "riscv32gc", + arch: "riscv32", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "riscv32-unknown-linux-musl", + }, + ), + ( + "riscv32i-unknown-none-elf", + TargetInfo { + full_arch: "riscv32i", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32im-risc0-zkvm-elf", + TargetInfo { + full_arch: "riscv32im", + arch: "riscv32", + vendor: "risc0", + os: "zkvm", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32im-unknown-none-elf", + TargetInfo { + full_arch: "riscv32im", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32ima-unknown-none-elf", + TargetInfo { + full_arch: "riscv32ima", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imac-esp-espidf", + TargetInfo { + full_arch: "riscv32imac", + arch: "riscv32", + vendor: "espressif", + os: "espidf", + env: "newlib", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imac-unknown-none-elf", + TargetInfo { + full_arch: "riscv32imac", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imac-unknown-nuttx-elf", + TargetInfo { + full_arch: "riscv32imac", + arch: "riscv32", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imac-unknown-xous-elf", + TargetInfo { + full_arch: "riscv32imac", + arch: "riscv32", + vendor: "unknown", + os: "xous", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imafc-esp-espidf", + TargetInfo { + full_arch: "riscv32imafc", + arch: "riscv32", + vendor: "espressif", + os: "espidf", + env: "newlib", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imafc-unknown-none-elf", + TargetInfo { + full_arch: "riscv32imafc", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imafc-unknown-nuttx-elf", + TargetInfo { + full_arch: "riscv32imafc", + arch: "riscv32", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imc-esp-espidf", + TargetInfo { + full_arch: "riscv32imc", + arch: "riscv32", + vendor: "espressif", + os: "espidf", + env: "newlib", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imc-unknown-none-elf", + TargetInfo { + full_arch: "riscv32imc", + arch: "riscv32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv32imc-unknown-nuttx-elf", + TargetInfo { + full_arch: "riscv32imc", + arch: "riscv32", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "", + llvm_target: "riscv32", + }, + ), + ( + "riscv64-linux-android", + TargetInfo { + full_arch: "riscv64", + arch: "riscv64", + vendor: "unknown", + os: "android", + env: "", + abi: "", + llvm_target: "riscv64-linux-android", + }, + ), + ( + "riscv64-wrs-vxworks", + TargetInfo { + full_arch: "riscv64", + arch: "riscv64", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "riscv64", + }, + ), + ( + "riscv64gc-unknown-freebsd", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "riscv64-unknown-freebsd", + }, + ), + ( + "riscv64gc-unknown-fuchsia", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "fuchsia", + env: "", + abi: "", + llvm_target: "riscv64-unknown-fuchsia", + }, + ), + ( + "riscv64gc-unknown-hermit", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "hermit", + env: "", + abi: "", + llvm_target: "riscv64-unknown-hermit", + }, + ), + ( + "riscv64gc-unknown-linux-gnu", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "riscv64-unknown-linux-gnu", + }, + ), + ( + "riscv64gc-unknown-linux-musl", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "riscv64-unknown-linux-musl", + }, + ), + ( + "riscv64gc-unknown-netbsd", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "riscv64-unknown-netbsd", + }, + ), + ( + "riscv64gc-unknown-none-elf", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv64", + }, + ), + ( + "riscv64gc-unknown-nuttx-elf", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "", + llvm_target: "riscv64", + }, + ), + ( + "riscv64gc-unknown-openbsd", + TargetInfo { + full_arch: "riscv64gc", + arch: "riscv64", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "riscv64-unknown-openbsd", + }, + ), + ( + "riscv64imac-unknown-none-elf", + TargetInfo { + full_arch: "riscv64imac", + arch: "riscv64", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "riscv64", + }, + ), + ( + "riscv64imac-unknown-nuttx-elf", + TargetInfo { + full_arch: "riscv64imac", + arch: "riscv64", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "", + llvm_target: "riscv64", + }, + ), + ( + "s390x-unknown-linux-gnu", + TargetInfo { + full_arch: "s390x", + arch: "s390x", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "s390x-unknown-linux-gnu", + }, + ), + ( + "s390x-unknown-linux-musl", + TargetInfo { + full_arch: "s390x", + arch: "s390x", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "s390x-unknown-linux-musl", + }, + ), + ( + "sparc-unknown-linux-gnu", + TargetInfo { + full_arch: "sparc", + arch: "sparc", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "sparc-unknown-linux-gnu", + }, + ), + ( + "sparc-unknown-none-elf", + TargetInfo { + full_arch: "sparc", + arch: "sparc", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "sparc-unknown-none-elf", + }, + ), + ( + "sparc64-unknown-linux-gnu", + TargetInfo { + full_arch: "sparc64", + arch: "sparc64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "sparc64-unknown-linux-gnu", + }, + ), + ( + "sparc64-unknown-netbsd", + TargetInfo { + full_arch: "sparc64", + arch: "sparc64", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "sparc64-unknown-netbsd", + }, + ), + ( + "sparc64-unknown-openbsd", + TargetInfo { + full_arch: "sparc64", + arch: "sparc64", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "sparc64-unknown-openbsd", + }, + ), + ( + "sparcv9-sun-solaris", + TargetInfo { + full_arch: "sparcv9", + arch: "sparc64", + vendor: "sun", + os: "solaris", + env: "", + abi: "", + llvm_target: "sparcv9-sun-solaris", + }, + ), + ( + "thumbv4t-none-eabi", + TargetInfo { + full_arch: "thumbv4t", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv4t-none-eabi", + }, + ), + ( + "thumbv5te-none-eabi", + TargetInfo { + full_arch: "thumbv5te", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv5te-none-eabi", + }, + ), + ( + "thumbv6m-none-eabi", + TargetInfo { + full_arch: "thumbv6m", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv6m-none-eabi", + }, + ), + ( + "thumbv6m-nuttx-eabi", + TargetInfo { + full_arch: "thumbv6m", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "thumbv6m-none-eabi", + }, + ), + ( + "thumbv7a-nuttx-eabi", + TargetInfo { + full_arch: "thumbv7a", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "thumbv7a-none-eabi", + }, + ), + ( + "thumbv7a-nuttx-eabihf", + TargetInfo { + full_arch: "thumbv7a", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabihf", + llvm_target: "thumbv7a-none-eabihf", + }, + ), + ( + "thumbv7a-pc-windows-msvc", + TargetInfo { + full_arch: "thumbv7a", + arch: "arm", + vendor: "pc", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "thumbv7a-pc-windows-msvc", + }, + ), + ( + "thumbv7a-uwp-windows-msvc", + TargetInfo { + full_arch: "thumbv7a", + arch: "arm", + vendor: "uwp", + os: "windows", + env: "msvc", + abi: "uwp", + llvm_target: "thumbv7a-pc-windows-msvc", + }, + ), + ( + "thumbv7em-none-eabi", + TargetInfo { + full_arch: "thumbv7em", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv7em-none-eabi", + }, + ), + ( + "thumbv7em-none-eabihf", + TargetInfo { + full_arch: "thumbv7em", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabihf", + llvm_target: "thumbv7em-none-eabihf", + }, + ), + ( + "thumbv7em-nuttx-eabi", + TargetInfo { + full_arch: "thumbv7em", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "thumbv7em-none-eabi", + }, + ), + ( + "thumbv7em-nuttx-eabihf", + TargetInfo { + full_arch: "thumbv7em", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabihf", + llvm_target: "thumbv7em-none-eabihf", + }, + ), + ( + "thumbv7m-none-eabi", + TargetInfo { + full_arch: "thumbv7m", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv7m-none-eabi", + }, + ), + ( + "thumbv7m-nuttx-eabi", + TargetInfo { + full_arch: "thumbv7m", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "thumbv7m-none-eabi", + }, + ), + ( + "thumbv7neon-linux-androideabi", + TargetInfo { + full_arch: "thumbv7neon", + arch: "arm", + vendor: "unknown", + os: "android", + env: "", + abi: "eabi", + llvm_target: "armv7-none-linux-android", + }, + ), + ( + "thumbv7neon-unknown-linux-gnueabihf", + TargetInfo { + full_arch: "thumbv7neon", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "eabihf", + llvm_target: "armv7-unknown-linux-gnueabihf", + }, + ), + ( + "thumbv7neon-unknown-linux-musleabihf", + TargetInfo { + full_arch: "thumbv7neon", + arch: "arm", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "eabihf", + llvm_target: "armv7-unknown-linux-musleabihf", + }, + ), + ( + "thumbv8m.base-none-eabi", + TargetInfo { + full_arch: "thumbv8m.base", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv8m.base-none-eabi", + }, + ), + ( + "thumbv8m.base-nuttx-eabi", + TargetInfo { + full_arch: "thumbv8m.base", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "thumbv8m.base-none-eabi", + }, + ), + ( + "thumbv8m.main-none-eabi", + TargetInfo { + full_arch: "thumbv8m.main", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabi", + llvm_target: "thumbv8m.main-none-eabi", + }, + ), + ( + "thumbv8m.main-none-eabihf", + TargetInfo { + full_arch: "thumbv8m.main", + arch: "arm", + vendor: "unknown", + os: "none", + env: "", + abi: "eabihf", + llvm_target: "thumbv8m.main-none-eabihf", + }, + ), + ( + "thumbv8m.main-nuttx-eabi", + TargetInfo { + full_arch: "thumbv8m.main", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabi", + llvm_target: "thumbv8m.main-none-eabi", + }, + ), + ( + "thumbv8m.main-nuttx-eabihf", + TargetInfo { + full_arch: "thumbv8m.main", + arch: "arm", + vendor: "unknown", + os: "nuttx", + env: "", + abi: "eabihf", + llvm_target: "thumbv8m.main-none-eabihf", + }, + ), + ( + "wasm32-unknown-emscripten", + TargetInfo { + full_arch: "wasm32", + arch: "wasm32", + vendor: "unknown", + os: "emscripten", + env: "", + abi: "", + llvm_target: "wasm32-unknown-emscripten", + }, + ), + ( + "wasm32-unknown-unknown", + TargetInfo { + full_arch: "wasm32", + arch: "wasm32", + vendor: "unknown", + os: "unknown", + env: "", + abi: "", + llvm_target: "wasm32-unknown-unknown", + }, + ), + ( + "wasm32-wasi", + TargetInfo { + full_arch: "wasm32", + arch: "wasm32", + vendor: "unknown", + os: "wasi", + env: "", + abi: "", + llvm_target: "wasm32-wasi", + }, + ), + ( + "wasm32-wasip1", + TargetInfo { + full_arch: "wasm32", + arch: "wasm32", + vendor: "unknown", + os: "wasi", + env: "p1", + abi: "", + llvm_target: "wasm32-wasip1", + }, + ), + ( + "wasm32-wasip1-threads", + TargetInfo { + full_arch: "wasm32", + arch: "wasm32", + vendor: "unknown", + os: "wasi", + env: "p1", + abi: "", + llvm_target: "wasm32-wasi", + }, + ), + ( + "wasm32-wasip2", + TargetInfo { + full_arch: "wasm32", + arch: "wasm32", + vendor: "unknown", + os: "wasi", + env: "p2", + abi: "", + llvm_target: "wasm32-wasip2", + }, + ), + ( + "wasm32v1-none", + TargetInfo { + full_arch: "wasm32v1", + arch: "wasm32", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "wasm32-unknown-unknown", + }, + ), + ( + "wasm64-unknown-unknown", + TargetInfo { + full_arch: "wasm64", + arch: "wasm64", + vendor: "unknown", + os: "unknown", + env: "", + abi: "", + llvm_target: "wasm64-unknown-unknown", + }, + ), + ( + "x86_64-apple-darwin", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "apple", + os: "macos", + env: "", + abi: "", + llvm_target: "x86_64-apple-macosx", + }, + ), + ( + "x86_64-apple-ios", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "apple", + os: "ios", + env: "", + abi: "sim", + llvm_target: "x86_64-apple-ios-simulator", + }, + ), + ( + "x86_64-apple-ios-macabi", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "apple", + os: "ios", + env: "", + abi: "macabi", + llvm_target: "x86_64-apple-ios-macabi", + }, + ), + ( + "x86_64-apple-tvos", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "apple", + os: "tvos", + env: "", + abi: "sim", + llvm_target: "x86_64-apple-tvos-simulator", + }, + ), + ( + "x86_64-apple-watchos-sim", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "apple", + os: "watchos", + env: "", + abi: "sim", + llvm_target: "x86_64-apple-watchos-simulator", + }, + ), + ( + "x86_64-fortanix-unknown-sgx", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "fortanix", + os: "unknown", + env: "sgx", + abi: "fortanix", + llvm_target: "x86_64-elf", + }, + ), + ( + "x86_64-fuchsia", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "fuchsia", + env: "", + abi: "", + llvm_target: "x86_64-fuchsia", + }, + ), + ( + "x86_64-linux-android", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "android", + env: "", + abi: "", + llvm_target: "x86_64-linux-android", + }, + ), + ( + "x86_64-pc-nto-qnx710", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "nto", + env: "nto71", + abi: "", + llvm_target: "x86_64-pc-unknown", + }, + ), + ( + "x86_64-pc-nto-qnx710_iosock", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "nto", + env: "nto71_iosock", + abi: "", + llvm_target: "x86_64-pc-unknown", + }, + ), + ( + "x86_64-pc-nto-qnx800", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "nto", + env: "nto80", + abi: "", + llvm_target: "x86_64-pc-unknown", + }, + ), + ( + "x86_64-pc-solaris", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "solaris", + env: "", + abi: "", + llvm_target: "x86_64-pc-solaris", + }, + ), + ( + "x86_64-pc-windows-gnu", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "windows", + env: "gnu", + abi: "", + llvm_target: "x86_64-pc-windows-gnu", + }, + ), + ( + "x86_64-pc-windows-gnullvm", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "windows", + env: "gnu", + abi: "llvm", + llvm_target: "x86_64-pc-windows-gnu", + }, + ), + ( + "x86_64-pc-windows-msvc", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "pc", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "x86_64-pc-windows-msvc", + }, + ), + ( + "x86_64-sun-solaris", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "sun", + os: "solaris", + env: "", + abi: "", + llvm_target: "x86_64-pc-solaris", + }, + ), + ( + "x86_64-unikraft-linux-musl", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unikraft", + os: "linux", + env: "musl", + abi: "", + llvm_target: "x86_64-unknown-linux-musl", + }, + ), + ( + "x86_64-unknown-dragonfly", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "dragonfly", + env: "", + abi: "", + llvm_target: "x86_64-unknown-dragonfly", + }, + ), + ( + "x86_64-unknown-freebsd", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "freebsd", + env: "", + abi: "", + llvm_target: "x86_64-unknown-freebsd", + }, + ), + ( + "x86_64-unknown-fuchsia", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "fuchsia", + env: "", + abi: "", + llvm_target: "x86_64-unknown-fuchsia", + }, + ), + ( + "x86_64-unknown-haiku", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "haiku", + env: "", + abi: "", + llvm_target: "x86_64-unknown-haiku", + }, + ), + ( + "x86_64-unknown-hermit", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "hermit", + env: "", + abi: "", + llvm_target: "x86_64-unknown-hermit", + }, + ), + ( + "x86_64-unknown-hurd-gnu", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "hurd", + env: "gnu", + abi: "", + llvm_target: "x86_64-unknown-hurd-gnu", + }, + ), + ( + "x86_64-unknown-illumos", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "illumos", + env: "", + abi: "", + llvm_target: "x86_64-pc-solaris", + }, + ), + ( + "x86_64-unknown-l4re-uclibc", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "l4re", + env: "uclibc", + abi: "", + llvm_target: "x86_64-unknown-l4re-uclibc", + }, + ), + ( + "x86_64-unknown-linux-gnu", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "", + llvm_target: "x86_64-unknown-linux-gnu", + }, + ), + ( + "x86_64-unknown-linux-gnux32", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "linux", + env: "gnu", + abi: "x32", + llvm_target: "x86_64-unknown-linux-gnux32", + }, + ), + ( + "x86_64-unknown-linux-musl", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "linux", + env: "musl", + abi: "", + llvm_target: "x86_64-unknown-linux-musl", + }, + ), + ( + "x86_64-unknown-linux-none", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "linux", + env: "", + abi: "", + llvm_target: "x86_64-unknown-linux-none", + }, + ), + ( + "x86_64-unknown-linux-ohos", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "linux", + env: "ohos", + abi: "", + llvm_target: "x86_64-unknown-linux-ohos", + }, + ), + ( + "x86_64-unknown-netbsd", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "netbsd", + env: "", + abi: "", + llvm_target: "x86_64-unknown-netbsd", + }, + ), + ( + "x86_64-unknown-none", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "none", + env: "", + abi: "", + llvm_target: "x86_64-unknown-none-elf", + }, + ), + ( + "x86_64-unknown-none-linuxkernel", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "none", + env: "gnu", + abi: "", + llvm_target: "x86_64-unknown-none-elf", + }, + ), + ( + "x86_64-unknown-openbsd", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "openbsd", + env: "", + abi: "", + llvm_target: "x86_64-unknown-openbsd", + }, + ), + ( + "x86_64-unknown-redox", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "redox", + env: "relibc", + abi: "", + llvm_target: "x86_64-unknown-redox", + }, + ), + ( + "x86_64-unknown-trusty", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "trusty", + env: "", + abi: "", + llvm_target: "x86_64-unknown-unknown-musl", + }, + ), + ( + "x86_64-unknown-uefi", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "unknown", + os: "uefi", + env: "", + abi: "", + llvm_target: "x86_64-unknown-windows-gnu", + }, + ), + ( + "x86_64-uwp-windows-gnu", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "uwp", + os: "windows", + env: "gnu", + abi: "uwp", + llvm_target: "x86_64-pc-windows-gnu", + }, + ), + ( + "x86_64-uwp-windows-msvc", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "uwp", + os: "windows", + env: "msvc", + abi: "uwp", + llvm_target: "x86_64-pc-windows-msvc", + }, + ), + ( + "x86_64-win7-windows-gnu", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "win7", + os: "windows", + env: "gnu", + abi: "", + llvm_target: "x86_64-pc-windows-gnu", + }, + ), + ( + "x86_64-win7-windows-msvc", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "win7", + os: "windows", + env: "msvc", + abi: "", + llvm_target: "x86_64-pc-windows-msvc", + }, + ), + ( + "x86_64-wrs-vxworks", + TargetInfo { + full_arch: "x86_64", + arch: "x86_64", + vendor: "wrs", + os: "vxworks", + env: "gnu", + abi: "", + llvm_target: "x86_64-unknown-linux-gnu", + }, + ), + ( + "x86_64h-apple-darwin", + TargetInfo { + full_arch: "x86_64h", + arch: "x86_64", + vendor: "apple", + os: "macos", + env: "", + abi: "", + llvm_target: "x86_64h-apple-macosx", + }, + ), + ( + "xtensa-esp32-espidf", + TargetInfo { + full_arch: "xtensa", + arch: "xtensa", + vendor: "espressif", + os: "espidf", + env: "newlib", + abi: "", + llvm_target: "xtensa-none-elf", + }, + ), + ( + "xtensa-esp32-none-elf", + TargetInfo { + full_arch: "xtensa", + arch: "xtensa", + vendor: "espressif", + os: "none", + env: "", + abi: "", + llvm_target: "xtensa-none-elf", + }, + ), + ( + "xtensa-esp32s2-espidf", + TargetInfo { + full_arch: "xtensa", + arch: "xtensa", + vendor: "espressif", + os: "espidf", + env: "newlib", + abi: "", + llvm_target: "xtensa-none-elf", + }, + ), + ( + "xtensa-esp32s2-none-elf", + TargetInfo { + full_arch: "xtensa", + arch: "xtensa", + vendor: "espressif", + os: "none", + env: "", + abi: "", + llvm_target: "xtensa-none-elf", + }, + ), + ( + "xtensa-esp32s3-espidf", + TargetInfo { + full_arch: "xtensa", + arch: "xtensa", + vendor: "espressif", + os: "espidf", + env: "newlib", + abi: "", + llvm_target: "xtensa-none-elf", + }, + ), + ( + "xtensa-esp32s3-none-elf", + TargetInfo { + full_arch: "xtensa", + arch: "xtensa", + vendor: "espressif", + os: "none", + env: "", + abi: "", + llvm_target: "xtensa-none-elf", + }, + ), +]; diff --git a/third_party/rust/cc/src/target/llvm.rs b/third_party/rust/cc/src/target/llvm.rs new file mode 100644 index 00000000000000..7d5fe9ab277210 --- /dev/null +++ b/third_party/rust/cc/src/target/llvm.rs @@ -0,0 +1,60 @@ +/// Rust and Clang don't really agree on naming, so do a best-effort +/// conversion to support out-of-tree / custom target-spec targets. +pub(crate) fn guess_llvm_target_triple( + full_arch: &str, + vendor: &str, + os: &str, + env: &str, + abi: &str, +) -> String { + let arch = match full_arch { + riscv32 if riscv32.starts_with("riscv32") => "riscv32", + riscv64 if riscv64.starts_with("riscv64") => "riscv64", + arch => arch, + }; + let os = match os { + "darwin" => "macosx", + "visionos" => "xros", + "uefi" => "windows", + os => os, + }; + let env = match env { + "newlib" | "nto70" | "nto71" | "ohos" | "p1" | "p2" | "relibc" | "sgx" | "uclibc" => "", + env => env, + }; + let abi = match abi { + "sim" => "simulator", + "llvm" | "softfloat" | "uwp" | "vec-extabi" => "", + "ilp32" => "_ilp32", + abi => abi, + }; + match (env, abi) { + ("", "") => format!("{arch}-{vendor}-{os}"), + (env, abi) => format!("{arch}-{vendor}-{os}-{env}{abi}"), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basic_llvm_triple_guessing() { + assert_eq!( + guess_llvm_target_triple("aarch64", "unknown", "linux", "", ""), + "aarch64-unknown-linux" + ); + assert_eq!( + guess_llvm_target_triple("x86_64", "unknown", "linux", "gnu", ""), + "x86_64-unknown-linux-gnu" + ); + assert_eq!( + guess_llvm_target_triple("x86_64", "unknown", "linux", "gnu", "eabi"), + "x86_64-unknown-linux-gnueabi" + ); + assert_eq!( + guess_llvm_target_triple("x86_64", "apple", "darwin", "", ""), + "x86_64-apple-macosx" + ); + } +} diff --git a/third_party/rust/cc/src/target/parser.rs b/third_party/rust/cc/src/target/parser.rs new file mode 100644 index 00000000000000..eb88669870d1ac --- /dev/null +++ b/third_party/rust/cc/src/target/parser.rs @@ -0,0 +1,130 @@ +use std::{env, str::FromStr}; + +use crate::{ + target::{llvm, TargetInfo}, + utilities::OnceLock, + Error, ErrorKind, +}; + +#[derive(Debug)] +struct TargetInfoParserInner { + full_arch: Box, + arch: Box, + vendor: Box, + os: Box, + env: Box, + abi: Box, + llvm_target: Box, +} + +impl TargetInfoParserInner { + fn from_cargo_environment_variables() -> Result { + // `TARGET` must be present. + // + // No need to emit `rerun-if-env-changed` for this, + // as it is controlled by Cargo itself. + #[allow(clippy::disallowed_methods)] + let target_triple = env::var("TARGET").map_err(|err| { + Error::new( + ErrorKind::EnvVarNotFound, + format!("failed reading TARGET: {err}"), + ) + })?; + + // Parse the full architecture name from the target triple. + let (full_arch, _rest) = target_triple.split_once('-').ok_or(Error::new( + ErrorKind::InvalidTarget, + format!("target `{target_triple}` had an unknown architecture"), + ))?; + + let cargo_env = |name, fallback: Option<&str>| -> Result, Error> { + // No need to emit `rerun-if-env-changed` for these, + // as they are controlled by Cargo itself. + #[allow(clippy::disallowed_methods)] + match env::var(name) { + Ok(var) => Ok(var.into_boxed_str()), + Err(err) => match fallback { + Some(fallback) => Ok(fallback.into()), + None => Err(Error::new( + ErrorKind::EnvVarNotFound, + format!("did not find fallback information for target `{target_triple}`, and failed reading {name}: {err}"), + )), + }, + } + }; + + // Prefer to use `CARGO_ENV_*` if set, since these contain the most + // correct information relative to the current `rustc`, and makes it + // possible to support custom target JSON specs unknown to `rustc`. + // + // NOTE: If the user is using an older `rustc`, that data may be older + // than our pre-generated data, but we still prefer Cargo's view of + // the world, since at least `cc` won't differ from `rustc` in that + // case. + // + // These may not be set in case the user depended on being able to + // just set `TARGET` outside of build scripts; in those cases, fall + // back back to data from the known set of target triples instead. + // + // See discussion in #1225 for further details. + let fallback_target = TargetInfo::from_str(&target_triple).ok(); + let ft = fallback_target.as_ref(); + let arch = cargo_env("CARGO_CFG_TARGET_ARCH", ft.map(|t| t.arch))?; + let vendor = cargo_env("CARGO_CFG_TARGET_VENDOR", ft.map(|t| t.vendor))?; + let os = cargo_env("CARGO_CFG_TARGET_OS", ft.map(|t| t.os))?; + let env = cargo_env("CARGO_CFG_TARGET_ENV", ft.map(|t| t.env))?; + // `target_abi` was stabilized in Rust 1.78, which is higher than our + // MSRV, so it may not always be available; In that case, fall back to + // `""`, which is _probably_ correct for unknown target triples. + let abi = cargo_env("CARGO_CFG_TARGET_ABI", ft.map(|t| t.abi)) + .unwrap_or_else(|_| String::default().into_boxed_str()); + + // Prefer `rustc`'s LLVM target triple information. + let llvm_target = match fallback_target { + Some(ft) => ft.llvm_target.to_string(), + None => llvm::guess_llvm_target_triple(full_arch, &vendor, &os, &env, &abi), + }; + + Ok(Self { + full_arch: full_arch.to_string().into_boxed_str(), + arch, + vendor, + os, + env, + abi, + llvm_target: llvm_target.into_boxed_str(), + }) + } +} + +/// Parser for [`TargetInfo`], contains cached information. +#[derive(Default, Debug)] +pub(crate) struct TargetInfoParser(OnceLock>); + +impl TargetInfoParser { + pub fn parse_from_cargo_environment_variables(&self) -> Result, Error> { + match self + .0 + .get_or_init(TargetInfoParserInner::from_cargo_environment_variables) + { + Ok(TargetInfoParserInner { + full_arch, + arch, + vendor, + os, + env, + abi, + llvm_target, + }) => Ok(TargetInfo { + full_arch, + arch, + vendor, + os, + env, + abi, + llvm_target, + }), + Err(e) => Err(e.clone()), + } + } +} diff --git a/third_party/rust/cc/src/tempfile.rs b/third_party/rust/cc/src/tempfile.rs new file mode 100644 index 00000000000000..833e6f28c7824b --- /dev/null +++ b/third_party/rust/cc/src/tempfile.rs @@ -0,0 +1,86 @@ +#![cfg_attr(target_family = "wasm", allow(unused))] + +use std::{ + collections::hash_map::RandomState, + fs::{remove_file, File, OpenOptions}, + hash::{BuildHasher, Hasher}, + io, os, + path::{Path, PathBuf}, +}; + +#[cfg(not(any(unix, target_family = "wasm", windows)))] +compile_error!("Your system is not supported since cc cannot create named tempfile"); + +fn rand() -> u64 { + RandomState::new().build_hasher().finish() +} + +fn tmpname(suffix: &str) -> String { + format!("{}{}", rand(), suffix) +} + +fn create_named(path: &Path) -> io::Result { + let mut open_options = OpenOptions::new(); + + open_options.read(true).write(true).create_new(true); + + #[cfg(all(unix, not(target_os = "wasi")))] + ::mode(&mut open_options, 0o600); + + #[cfg(windows)] + ::custom_flags( + &mut open_options, + crate::windows::windows_sys::FILE_ATTRIBUTE_TEMPORARY, + ); + + open_options.open(path) +} + +pub(super) struct NamedTempfile { + path: PathBuf, + file: Option, +} + +impl NamedTempfile { + pub(super) fn new(base: &Path, suffix: &str) -> io::Result { + for _ in 0..10 { + let path = base.join(tmpname(suffix)); + match create_named(&path) { + Ok(file) => { + return Ok(Self { + file: Some(file), + path, + }) + } + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + }; + } + + Err(io::Error::new( + io::ErrorKind::AlreadyExists, + format!( + "too many temporary files exist in base `{}` with suffix `{}`", + base.display(), + suffix + ), + )) + } + + pub(super) fn path(&self) -> &Path { + &self.path + } + + pub(super) fn take_file(&mut self) -> Option { + self.file.take() + } +} + +impl Drop for NamedTempfile { + fn drop(&mut self) { + // On Windows you have to close all handle to it before + // removing the file. + self.file.take(); + let _ = remove_file(&self.path); + } +} diff --git a/third_party/rust/cc/src/tool.rs b/third_party/rust/cc/src/tool.rs index a193a90ff7b9bd..04d366dfa1025e 100644 --- a/third_party/rust/cc/src/tool.rs +++ b/third_party/rust/cc/src/tool.rs @@ -1,12 +1,22 @@ use std::{ + borrow::Cow, collections::HashMap, - ffi::OsString, + env, + ffi::{OsStr, OsString}, + io::Write, path::{Path, PathBuf}, - process::Command, - sync::Mutex, + process::{Command, Stdio}, + sync::RwLock, }; -use crate::command_helpers::{run_output, CargoOutput}; +use crate::{ + command_helpers::{run_output, CargoOutput}, + run, + tempfile::NamedTempfile, + Error, ErrorKind, OutputKind, +}; + +pub(crate) type CompilerFamilyLookupCache = HashMap]>, ToolFamily>; /// Configuration used to represent an invocation of a C compiler. /// @@ -32,24 +42,34 @@ pub struct Tool { impl Tool { pub(crate) fn new( path: PathBuf, - cached_compiler_family: &Mutex, ToolFamily>>, + cached_compiler_family: &RwLock, cargo_output: &CargoOutput, + out_dir: Option<&Path>, ) -> Self { - Self::with_features(path, None, false, cached_compiler_family, cargo_output) + Self::with_features( + path, + vec![], + false, + cached_compiler_family, + cargo_output, + out_dir, + ) } - pub(crate) fn with_clang_driver( + pub(crate) fn with_args( path: PathBuf, - clang_driver: Option<&str>, - cached_compiler_family: &Mutex, ToolFamily>>, + args: Vec, + cached_compiler_family: &RwLock, cargo_output: &CargoOutput, + out_dir: Option<&Path>, ) -> Self { Self::with_features( path, - clang_driver, + args, false, cached_compiler_family, cargo_output, + out_dir, ) } @@ -70,74 +90,180 @@ impl Tool { pub(crate) fn with_features( path: PathBuf, - clang_driver: Option<&str>, + args: Vec, cuda: bool, - cached_compiler_family: &Mutex, ToolFamily>>, + cached_compiler_family: &RwLock, cargo_output: &CargoOutput, + out_dir: Option<&Path>, ) -> Self { - fn detect_family_inner(path: &Path, cargo_output: &CargoOutput) -> ToolFamily { - let mut cmd = Command::new(path); - cmd.arg("--version"); - - let stdout = match run_output( - &mut cmd, - &path.to_string_lossy(), + fn is_zig_cc(path: &Path, cargo_output: &CargoOutput) -> bool { + run_output( + Command::new(path).arg("--version"), + path, // tool detection issues should always be shown as warnings cargo_output, ) - .ok() - .and_then(|o| String::from_utf8(o).ok()) - { - Some(s) => s, - None => { - // --version failed. fallback to gnu - cargo_output.print_warning(&format_args!("Failed to run: {:?}", cmd)); - return ToolFamily::Gnu; + .map(|o| String::from_utf8_lossy(&o).contains("ziglang")) + .unwrap_or_default() + || { + match path.file_name().map(OsStr::to_string_lossy) { + Some(fname) => fname.contains("zig"), + _ => false, + } } - }; - if stdout.contains("clang") { - ToolFamily::Clang - } else if stdout.contains("GCC") { - ToolFamily::Gnu + } + + fn guess_family_from_stdout( + stdout: &str, + path: &Path, + args: &[String], + cargo_output: &CargoOutput, + ) -> Result { + cargo_output.print_debug(&stdout); + + // https://gitlab.kitware.com/cmake/cmake/-/blob/69a2eeb9dff5b60f2f1e5b425002a0fd45b7cadb/Modules/CMakeDetermineCompilerId.cmake#L267-271 + // stdin is set to null to ensure that the help output is never paginated. + let accepts_cl_style_flags = run( + Command::new(path).args(args).arg("-?").stdin(Stdio::null()), + path, + &{ + // the errors are not errors! + let mut cargo_output = cargo_output.clone(); + cargo_output.warnings = cargo_output.debug; + cargo_output.output = OutputKind::Discard; + cargo_output + }, + ) + .is_ok(); + + let clang = stdout.contains(r#""clang""#); + let gcc = stdout.contains(r#""gcc""#); + let emscripten = stdout.contains(r#""emscripten""#); + let vxworks = stdout.contains(r#""VxWorks""#); + + match (clang, accepts_cl_style_flags, gcc, emscripten, vxworks) { + (clang_cl, true, _, false, false) => Ok(ToolFamily::Msvc { clang_cl }), + (true, _, _, _, false) | (_, _, _, true, false) => Ok(ToolFamily::Clang { + zig_cc: is_zig_cc(path, cargo_output), + }), + (false, false, true, _, false) | (_, _, _, _, true) => Ok(ToolFamily::Gnu), + (false, false, false, false, false) => { + cargo_output.print_warning(&"Compiler family detection failed since it does not define `__clang__`, `__GNUC__`, `__EMSCRIPTEN__` or `__VXWORKS__`, also does not accept cl style flag `-?`, fallback to treating it as GNU"); + Err(Error::new( + ErrorKind::ToolFamilyMacroNotFound, + "Expects macro `__clang__`, `__GNUC__` or `__EMSCRIPTEN__`, `__VXWORKS__` or accepts cl style flag `-?`, but found none", + )) + } + } + } + + fn detect_family_inner( + path: &Path, + args: &[String], + cargo_output: &CargoOutput, + out_dir: Option<&Path>, + ) -> Result { + let out_dir = out_dir + .map(Cow::Borrowed) + .unwrap_or_else(|| Cow::Owned(env::temp_dir())); + + // Ensure all the parent directories exist otherwise temp file creation + // will fail + std::fs::create_dir_all(&out_dir).map_err(|err| Error { + kind: ErrorKind::IOError, + message: format!("failed to create OUT_DIR '{}': {}", out_dir.display(), err) + .into(), + })?; + + let mut tmp = + NamedTempfile::new(&out_dir, "detect_compiler_family.c").map_err(|err| Error { + kind: ErrorKind::IOError, + message: format!( + "failed to create detect_compiler_family.c temp file in '{}': {}", + out_dir.display(), + err + ) + .into(), + })?; + let mut tmp_file = tmp.take_file().unwrap(); + tmp_file.write_all(include_bytes!("detect_compiler_family.c"))?; + // Close the file handle *now*, otherwise the compiler may fail to open it on Windows + // (#1082). The file stays on disk and its path remains valid until `tmp` is dropped. + tmp_file.flush()?; + tmp_file.sync_data()?; + drop(tmp_file); + + // When expanding the file, the compiler prints a lot of information to stderr + // that it is not an error, but related to expanding itself. + // + // cc would have to disable warning here to prevent generation of too many warnings. + let mut compiler_detect_output = cargo_output.clone(); + compiler_detect_output.warnings = compiler_detect_output.debug; + + let stdout = run_output( + Command::new(path).arg("-E").arg(tmp.path()), + path, + &compiler_detect_output, + )?; + let stdout = String::from_utf8_lossy(&stdout); + + if stdout.contains("-Wslash-u-filename") { + let stdout = run_output( + Command::new(path).arg("-E").arg("--").arg(tmp.path()), + path, + &compiler_detect_output, + )?; + let stdout = String::from_utf8_lossy(&stdout); + guess_family_from_stdout(&stdout, path, args, cargo_output) } else { - // --version doesn't include clang for GCC - cargo_output.print_warning(&format_args!( - "Compiler version doesn't include clang or GCC: {:?}", - cmd - )); - ToolFamily::Gnu + guess_family_from_stdout(&stdout, path, args, cargo_output) } } - let detect_family = |path: &Path| -> ToolFamily { - if let Some(family) = cached_compiler_family.lock().unwrap().get(path) { - return *family; + let detect_family = |path: &Path, args: &[String]| -> Result { + let cache_key = [path.as_os_str()] + .iter() + .cloned() + .chain(args.iter().map(OsStr::new)) + .map(Into::into) + .collect(); + if let Some(family) = cached_compiler_family.read().unwrap().get(&cache_key) { + return Ok(*family); } - let family = detect_family_inner(path, cargo_output); + let family = detect_family_inner(path, args, cargo_output, out_dir)?; cached_compiler_family - .lock() + .write() .unwrap() - .insert(path.into(), family); - family + .insert(cache_key, family); + Ok(family) }; - // Try to detect family of the tool from its name, falling back to Gnu. - let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) { - if fname.contains("clang-cl") { - ToolFamily::Msvc { clang_cl: true } - } else if fname.ends_with("cl") || fname == "cl.exe" { - ToolFamily::Msvc { clang_cl: false } - } else if fname.contains("clang") { - match clang_driver { - Some("cl") => ToolFamily::Msvc { clang_cl: true }, - _ => ToolFamily::Clang, + let family = detect_family(&path, &args).unwrap_or_else(|e| { + cargo_output.print_warning(&format_args!( + "Compiler family detection failed due to error: {}", + e + )); + match path.file_name().map(OsStr::to_string_lossy) { + Some(fname) if fname.contains("clang-cl") => ToolFamily::Msvc { clang_cl: true }, + Some(fname) if fname.ends_with("cl") || fname == "cl.exe" => { + ToolFamily::Msvc { clang_cl: false } } - } else { - detect_family(&path) + Some(fname) if fname.contains("clang") => { + let is_clang_cl = args + .iter() + .any(|a| a.strip_prefix("--driver-mode=") == Some("cl")); + if is_clang_cl { + ToolFamily::Msvc { clang_cl: true } + } else { + ToolFamily::Clang { + zig_cc: is_zig_cc(&path, cargo_output), + } + } + } + Some(fname) if fname.contains("zig") => ToolFamily::Clang { zig_cc: true }, + _ => ToolFamily::Gnu, } - } else { - detect_family(&path) - }; + }); Tool { path, @@ -184,10 +310,8 @@ impl Tool { if chars.next() != Some('/') { return false; } - } else if self.is_like_gnu() || self.is_like_clang() { - if chars.next() != Some('-') { - return false; - } + } else if (self.is_like_gnu() || self.is_like_clang()) && chars.next() != Some('-') { + return false; } // Check for existing optimization flags (-O, /O) @@ -205,7 +329,7 @@ impl Tool { /// Don't push optimization arg if it conflicts with existing args. pub(crate) fn push_opt_unless_duplicate(&mut self, flag: OsString) { if self.is_duplicate_opt_arg(&flag) { - println!("Info: Ignoring duplicate arg {:?}", &flag); + eprintln!("Info: Ignoring duplicate arg {:?}", &flag); } else { self.push_cc_arg(flag); } @@ -303,7 +427,7 @@ impl Tool { /// Whether the tool is Clang-like. pub fn is_like_clang(&self) -> bool { - self.family == ToolFamily::Clang + matches!(self.family, ToolFamily::Clang { .. }) } /// Whether the tool is AppleClang under .xctoolchain @@ -319,10 +443,18 @@ impl Tool { /// Whether the tool is MSVC-like. pub fn is_like_msvc(&self) -> bool { - match self.family { - ToolFamily::Msvc { .. } => true, - _ => false, - } + matches!(self.family, ToolFamily::Msvc { .. }) + } + + /// Whether the tool is `clang-cl`-based MSVC-like. + pub fn is_like_clang_cl(&self) -> bool { + matches!(self.family, ToolFamily::Msvc { clang_cl: true }) + } + + /// Supports using `--` delimiter to separate arguments and path to source files. + pub(crate) fn supports_path_delimiter(&self) -> bool { + // homebrew clang and zig-cc does not support this while stock version does + matches!(self.family, ToolFamily::Msvc { clang_cl: true }) && !self.cuda } } @@ -337,7 +469,7 @@ pub enum ToolFamily { Gnu, /// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags /// and its cross-compilation approach is different. - Clang, + Clang { zig_cc: bool }, /// Tool is the MSVC cl.exe. Msvc { clang_cl: bool }, } @@ -349,7 +481,7 @@ impl ToolFamily { ToolFamily::Msvc { .. } => { cmd.push_cc_arg("-Z7".into()); } - ToolFamily::Gnu | ToolFamily::Clang => { + ToolFamily::Gnu | ToolFamily::Clang { .. } => { cmd.push_cc_arg( dwarf_version .map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v)) @@ -362,7 +494,7 @@ impl ToolFamily { /// What the flag to force frame pointers. pub(crate) fn add_force_frame_pointer(&self, cmd: &mut Tool) { match *self { - ToolFamily::Gnu | ToolFamily::Clang => { + ToolFamily::Gnu | ToolFamily::Clang { .. } => { cmd.push_cc_arg("-fno-omit-frame-pointer".into()); } _ => (), @@ -373,7 +505,7 @@ impl ToolFamily { pub(crate) fn warnings_flags(&self) -> &'static str { match *self { ToolFamily::Msvc { .. } => "-W4", - ToolFamily::Gnu | ToolFamily::Clang => "-Wall", + ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Wall", } } @@ -381,7 +513,7 @@ impl ToolFamily { pub(crate) fn extra_warnings_flags(&self) -> Option<&'static str> { match *self { ToolFamily::Msvc { .. } => None, - ToolFamily::Gnu | ToolFamily::Clang => Some("-Wextra"), + ToolFamily::Gnu | ToolFamily::Clang { .. } => Some("-Wextra"), } } @@ -389,11 +521,11 @@ impl ToolFamily { pub(crate) fn warnings_to_errors_flag(&self) -> &'static str { match *self { ToolFamily::Msvc { .. } => "-WX", - ToolFamily::Gnu | ToolFamily::Clang => "-Werror", + ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Werror", } } pub(crate) fn verbose_stderr(&self) -> bool { - *self == ToolFamily::Clang + matches!(*self, ToolFamily::Clang { .. }) } } diff --git a/third_party/rust/cc/src/utilities.rs b/third_party/rust/cc/src/utilities.rs new file mode 100644 index 00000000000000..7a8a9493369542 --- /dev/null +++ b/third_party/rust/cc/src/utilities.rs @@ -0,0 +1,130 @@ +use std::{ + cell::UnsafeCell, + ffi::OsStr, + fmt::{self, Write}, + marker::PhantomData, + mem::MaybeUninit, + panic::{RefUnwindSafe, UnwindSafe}, + path::Path, + sync::Once, +}; + +pub(super) struct JoinOsStrs<'a, T> { + pub(super) slice: &'a [T], + pub(super) delimiter: char, +} + +impl fmt::Display for JoinOsStrs<'_, T> +where + T: AsRef, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let len = self.slice.len(); + for (index, os_str) in self.slice.iter().enumerate() { + // TODO: Use OsStr::display once it is stablised, + // Path and OsStr has the same `Display` impl + write!(f, "{}", Path::new(os_str).display())?; + if index + 1 < len { + f.write_char(self.delimiter)?; + } + } + Ok(()) + } +} + +pub(super) struct OptionOsStrDisplay(pub(super) Option); + +impl fmt::Display for OptionOsStrDisplay +where + T: AsRef, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // TODO: Use OsStr::display once it is stablised + // Path and OsStr has the same `Display` impl + if let Some(os_str) = self.0.as_ref() { + write!(f, "Some({})", Path::new(os_str).display()) + } else { + f.write_str("None") + } + } +} + +pub(crate) struct OnceLock { + once: Once, + value: UnsafeCell>, + _marker: PhantomData, +} + +impl Default for OnceLock { + fn default() -> Self { + Self::new() + } +} + +impl OnceLock { + pub(crate) const fn new() -> Self { + Self { + once: Once::new(), + value: UnsafeCell::new(MaybeUninit::uninit()), + _marker: PhantomData, + } + } + + #[inline] + fn is_initialized(&self) -> bool { + self.once.is_completed() + } + + unsafe fn get_unchecked(&self) -> &T { + debug_assert!(self.is_initialized()); + #[allow(clippy::needless_borrow)] + #[allow(unused_unsafe)] + unsafe { + (&*self.value.get()).assume_init_ref() + } + } + + pub(crate) fn get_or_init(&self, f: impl FnOnce() -> T) -> &T { + self.once.call_once(|| { + unsafe { &mut *self.value.get() }.write(f()); + }); + unsafe { self.get_unchecked() } + } + + pub(crate) fn get(&self) -> Option<&T> { + if self.is_initialized() { + // Safe b/c checked is_initialized + Some(unsafe { self.get_unchecked() }) + } else { + None + } + } +} + +impl fmt::Debug for OnceLock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_tuple("OnceLock"); + match self.get() { + Some(v) => d.field(v), + None => d.field(&format_args!("")), + }; + d.finish() + } +} + +unsafe impl Sync for OnceLock {} +unsafe impl Send for OnceLock {} + +impl RefUnwindSafe for OnceLock {} +impl UnwindSafe for OnceLock {} + +impl Drop for OnceLock { + #[inline] + fn drop(&mut self) { + if self.once.is_completed() { + // SAFETY: The cell is initialized and being dropped, so it can't + // be accessed again. + unsafe { self.value.get_mut().assume_init_drop() }; + } + } +} diff --git a/third_party/rust/cc/src/windows/com.rs b/third_party/rust/cc/src/windows/com.rs index e81bb1d3c34fb3..0391b5affed1fd 100644 --- a/third_party/rust/cc/src/windows/com.rs +++ b/third_party/rust/cc/src/windows/com.rs @@ -5,8 +5,6 @@ // All files in the project carrying such notice may not be copied, modified, or distributed // except according to those terms. -#![allow(unused)] - use crate::windows::{ winapi::{IUnknown, Interface}, windows_sys::{ @@ -16,10 +14,9 @@ use crate::windows::{ }; use std::{ convert::TryInto, - ffi::{OsStr, OsString}, - mem::ManuallyDrop, + ffi::OsString, ops::Deref, - os::windows::ffi::{OsStrExt, OsStringExt}, + os::windows::ffi::OsStringExt, ptr::{null, null_mut}, slice::from_raw_parts, }; @@ -48,19 +45,6 @@ where assert!(!ptr.is_null()); ComPtr(ptr) } - /// Casts up the inheritance chain - pub fn up(self) -> ComPtr - where - T: Deref, - U: Interface, - { - ComPtr(self.into_raw() as *mut U) - } - /// Extracts the raw pointer. - /// You are now responsible for releasing it yourself. - pub fn into_raw(self) -> *mut T { - ManuallyDrop::new(self).0 - } /// For internal use only. fn as_unknown(&self) -> &IUnknown { unsafe { &*(self.0 as *mut IUnknown) } @@ -124,34 +108,3 @@ impl Drop for BStr { unsafe { SysFreeString(self.0) }; } } - -pub trait ToWide { - fn to_wide(&self) -> Vec; - fn to_wide_null(&self) -> Vec; -} -impl ToWide for T -where - T: AsRef, -{ - fn to_wide(&self) -> Vec { - self.as_ref().encode_wide().collect() - } - fn to_wide_null(&self) -> Vec { - self.as_ref().encode_wide().chain(Some(0)).collect() - } -} -pub trait FromWide -where - Self: Sized, -{ - fn from_wide(wide: &[u16]) -> Self; - fn from_wide_null(wide: &[u16]) -> Self { - let len = wide.iter().take_while(|&&c| c != 0).count(); - Self::from_wide(&wide[..len]) - } -} -impl FromWide for OsString { - fn from_wide(wide: &[u16]) -> OsString { - OsStringExt::from_wide(wide) - } -} diff --git a/third_party/rust/cc/src/windows/find_tools.rs b/third_party/rust/cc/src/windows/find_tools.rs index 4f0ed87411b59e..ce4f0c62196709 100644 --- a/third_party/rust/cc/src/windows/find_tools.rs +++ b/third_party/rust/cc/src/windows/find_tools.rs @@ -14,67 +14,158 @@ #![allow(clippy::upper_case_acronyms)] -use std::process::Command; +use std::{ + env, + ffi::{OsStr, OsString}, + ops::Deref, + path::PathBuf, + process::Command, + sync::Arc, +}; use crate::Tool; use crate::ToolFamily; const MSVC_FAMILY: ToolFamily = ToolFamily::Msvc { clang_cl: false }; -#[derive(Copy, Clone)] -struct TargetArch<'a>(pub &'a str); +/// The target provided by the user. +#[derive(Copy, Clone, PartialEq, Eq)] +enum TargetArch { + X86, + X64, + Arm, + Arm64, + Arm64ec, +} +impl TargetArch { + /// Parse the `TargetArch` from a str. Returns `None` if the arch is unrecognized. + fn new(arch: &str) -> Option { + // NOTE: Keep up to date with docs in [`find`]. + match arch { + "x64" | "x86_64" => Some(Self::X64), + "arm64" | "aarch64" => Some(Self::Arm64), + "arm64ec" => Some(Self::Arm64ec), + "x86" | "i686" | "i586" => Some(Self::X86), + "arm" | "thumbv7a" => Some(Self::Arm), + _ => None, + } + } -impl PartialEq<&str> for TargetArch<'_> { - fn eq(&self, other: &&str) -> bool { - self.0 == *other + #[cfg(windows)] + /// Gets the Visual Studio name for the architecture. + fn as_vs_arch(&self) -> &'static str { + match self { + Self::X64 => "x64", + Self::Arm64 | Self::Arm64ec => "arm64", + Self::X86 => "x86", + Self::Arm => "arm", + } } } -impl<'a> From> for &'a str { - fn from(target: TargetArch<'a>) -> Self { - target.0 +pub(crate) enum Env { + Owned(OsString), + Arced(Arc), +} + +impl AsRef for Env { + fn as_ref(&self) -> &OsStr { + self.deref() + } +} + +impl Deref for Env { + type Target = OsStr; + + fn deref(&self) -> &Self::Target { + match self { + Env::Owned(os_str) => os_str, + Env::Arced(os_str) => os_str, + } + } +} + +impl From for PathBuf { + fn from(env: Env) -> Self { + match env { + Env::Owned(os_str) => PathBuf::from(os_str), + Env::Arced(os_str) => PathBuf::from(os_str.deref()), + } + } +} + +pub(crate) trait EnvGetter { + fn get_env(&self, name: &'static str) -> Option; +} + +struct StdEnvGetter; + +impl EnvGetter for StdEnvGetter { + #[allow(clippy::disallowed_methods)] + fn get_env(&self, name: &'static str) -> Option { + env::var_os(name).map(Env::Owned) } } /// Attempts to find a tool within an MSVC installation using the Windows /// registry as a point to search from. /// -/// The `target` argument is the target that the tool should work for (e.g. -/// compile or link for) and the `tool` argument is the tool to find (e.g. -/// `cl.exe` or `link.exe`). +/// The `arch_or_target` argument is the architecture or the Rust target +/// triple that the tool should work for (e.g. compile or link for). The +/// supported architecture names are: +/// - `"x64"` or `"x86_64"` +/// - `"arm64"` or `"aarch64"` +/// - `"arm64ec"` +/// - `"x86"`, `"i586"` or `"i686"` +/// - `"arm"` or `"thumbv7a"` +/// +/// The `tool` argument is the tool to find (e.g. `cl.exe` or `link.exe`). /// /// This function will return `None` if the tool could not be found, or it will /// return `Some(cmd)` which represents a command that's ready to execute the /// tool with the appropriate environment variables set. /// -/// Note that this function always returns `None` for non-MSVC targets. -pub fn find(target: &str, tool: &str) -> Option { - find_tool(target, tool).map(|c| c.to_command()) +/// Note that this function always returns `None` for non-MSVC targets (if a +/// full target name was specified). +pub fn find(arch_or_target: &str, tool: &str) -> Option { + find_tool(arch_or_target, tool).map(|c| c.to_command()) } /// Similar to the `find` function above, this function will attempt the same /// operation (finding a MSVC tool in a local install) but instead returns a /// `Tool` which may be introspected. -pub fn find_tool(target: &str, tool: &str) -> Option { - // This logic is all tailored for MSVC, if we're not that then bail out - // early. - if !target.contains("msvc") { - return None; - } +pub fn find_tool(arch_or_target: &str, tool: &str) -> Option { + let full_arch = if let Some((full_arch, rest)) = arch_or_target.split_once("-") { + // The logic is all tailored for MSVC, if the target is not that then + // bail out early. + if !rest.contains("msvc") { + return None; + } + full_arch + } else { + arch_or_target + }; + find_tool_inner(full_arch, tool, &StdEnvGetter) +} - // Split the target to get the arch. - let target = TargetArch(target.split_once('-')?.0); +pub(crate) fn find_tool_inner( + full_arch: &str, + tool: &str, + env_getter: &dyn EnvGetter, +) -> Option { + // We only need the arch. + let target = TargetArch::new(full_arch)?; // Looks like msbuild isn't located in the same location as other tools like // cl.exe and lib.exe. if tool.contains("msbuild") { - return impl_::find_msbuild(target); + return impl_::find_msbuild(target, env_getter); } // Looks like devenv isn't located in the same location as other tools like // cl.exe and lib.exe. if tool.contains("devenv") { - return impl_::find_devenv(target); + return impl_::find_devenv(target, env_getter); } // Ok, if we're here, now comes the fun part of the probing. Default shells @@ -84,11 +175,9 @@ pub fn find_tool(target: &str, tool: &str) -> Option { // environment variables like `LIB`, `INCLUDE`, and `PATH` to ensure that // the tool is actually usable. - impl_::find_msvc_environment(tool, target) - .or_else(|| impl_::find_msvc_15plus(tool, target)) - .or_else(|| impl_::find_msvc_14(tool, target)) - .or_else(|| impl_::find_msvc_12(tool, target)) - .or_else(|| impl_::find_msvc_11(tool, target)) + impl_::find_msvc_environment(tool, target, env_getter) + .or_else(|| impl_::find_msvc_15plus(tool, target, env_getter)) + .or_else(|| impl_::find_msvc_14(tool, target, env_getter)) } /// A version of Visual Studio @@ -96,6 +185,9 @@ pub fn find_tool(target: &str, tool: &str) -> Option { #[non_exhaustive] pub enum VsVers { /// Visual Studio 12 (2013) + #[deprecated( + note = "Visual Studio 12 is no longer supported. cc will never return this value." + )] Vs12, /// Visual Studio 14 (2015) Vs14, @@ -111,14 +203,18 @@ pub enum VsVers { /// /// This is used by the cmake crate to figure out the correct /// generator. +#[allow(clippy::disallowed_methods)] pub fn find_vs_version() -> Result { + fn has_msbuild_version(version: &str) -> bool { + impl_::has_msbuild_version(version, &StdEnvGetter) + } + match std::env::var("VisualStudioVersion") { Ok(version) => match &version[..] { "17.0" => Ok(VsVers::Vs17), "16.0" => Ok(VsVers::Vs16), "15.0" => Ok(VsVers::Vs15), "14.0" => Ok(VsVers::Vs14), - "12.0" => Ok(VsVers::Vs12), vers => Err(format!( "\n\n\ unsupported or unknown VisualStudio version: {}\n\ @@ -132,25 +228,22 @@ pub fn find_vs_version() -> Result { _ => { // Check for the presence of a specific registry key // that indicates visual studio is installed. - if impl_::has_msbuild_version("17.0") { + if has_msbuild_version("17.0") { Ok(VsVers::Vs17) - } else if impl_::has_msbuild_version("16.0") { + } else if has_msbuild_version("16.0") { Ok(VsVers::Vs16) - } else if impl_::has_msbuild_version("15.0") { + } else if has_msbuild_version("15.0") { Ok(VsVers::Vs15) - } else if impl_::has_msbuild_version("14.0") { + } else if has_msbuild_version("14.0") { Ok(VsVers::Vs14) - } else if impl_::has_msbuild_version("12.0") { - Ok(VsVers::Vs12) } else { - Err(format!( - "\n\n\ + Err("\n\n\ couldn't determine visual studio generator\n\ if VisualStudio is installed, however, consider \ running the appropriate vcvars script before building \ this crate\n\ " - )) + .to_string()) } } } @@ -164,7 +257,7 @@ mod impl_ { use crate::windows::setup_config::SetupConfiguration; use crate::windows::vs_instances::{VsInstances, VswhereInstance}; use crate::windows::windows_sys::{ - FreeLibrary, GetMachineTypeAttributes, GetProcAddress, LoadLibraryA, UserEnabled, HMODULE, + GetMachineTypeAttributes, GetProcAddress, LoadLibraryA, UserEnabled, HMODULE, IMAGE_FILE_MACHINE_AMD64, MACHINE_ATTRIBUTES, S_OK, }; use std::convert::TryFrom; @@ -180,7 +273,7 @@ mod impl_ { use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Once; - use super::{TargetArch, MSVC_FAMILY}; + use super::{EnvGetter, TargetArch, MSVC_FAMILY}; use crate::Tool; struct MsvcTool { @@ -195,22 +288,20 @@ mod impl_ { impl LibraryHandle { fn new(name: &[u8]) -> Option { let handle = unsafe { LoadLibraryA(name.as_ptr() as _) }; - (!handle.is_null()).then(|| Self(handle)) + (!handle.is_null()).then_some(Self(handle)) } /// Get a function pointer to a function in the library. - /// SAFETY: The caller must ensure that the function signature matches the actual function. - /// The easiest way to do this is to add an entry to windows_sys_no_link.list and use the + /// # SAFETY + /// + /// The caller must ensure that the function signature matches the actual function. + /// The easiest way to do this is to add an entry to `windows_sys_no_link.list` and use the /// generated function for `func_signature`. + /// + /// The function returned cannot be used after the handle is dropped. unsafe fn get_proc_address(&self, name: &[u8]) -> Option { - let symbol = unsafe { GetProcAddress(self.0, name.as_ptr() as _) }; - symbol.map(|symbol| unsafe { mem::transmute_copy(&symbol) }) - } - } - - impl Drop for LibraryHandle { - fn drop(&mut self) { - unsafe { FreeLibrary(self.0) }; + let symbol = GetProcAddress(self.0, name.as_ptr() as _); + symbol.map(|symbol| mem::transmute_copy(&symbol)) } } @@ -265,7 +356,7 @@ mod impl_ { } } - fn into_tool(self) -> Tool { + fn into_tool(self, env_getter: &dyn EnvGetter) -> Tool { let MsvcTool { tool, libs, @@ -273,45 +364,88 @@ mod impl_ { include, } = self; let mut tool = Tool::with_family(tool, MSVC_FAMILY); - add_env(&mut tool, "LIB", libs); - add_env(&mut tool, "PATH", path); - add_env(&mut tool, "INCLUDE", include); + add_env(&mut tool, "LIB", libs, env_getter); + add_env(&mut tool, "PATH", path, env_getter); + add_env(&mut tool, "INCLUDE", include, env_getter); tool } } + /// Checks to see if the target's arch matches the VS environment. Returns `None` if the + /// environment is unknown. + fn is_vscmd_target(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + is_vscmd_target_env(target, env_getter).or_else(|| is_vscmd_target_cl(target, env_getter)) + } + /// Checks to see if the `VSCMD_ARG_TGT_ARCH` environment variable matches the /// given target's arch. Returns `None` if the variable does not exist. - fn is_vscmd_target(target: TargetArch<'_>) -> Option { - let vscmd_arch = env::var("VSCMD_ARG_TGT_ARCH").ok()?; - // Convert the Rust target arch to its VS arch equivalent. - let arch = match target.into() { - "x86_64" => "x64", - "aarch64" | "arm64ec" => "arm64", - "i686" | "i586" => "x86", - "thumbv7a" => "arm", - // An unrecognized arch. - _ => return Some(false), - }; - Some(vscmd_arch == arch) + fn is_vscmd_target_env(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + let vscmd_arch = env_getter.get_env("VSCMD_ARG_TGT_ARCH")?; + Some(target.as_vs_arch() == vscmd_arch.as_ref()) + } + + /// Checks if the cl.exe target matches the given target's arch. Returns `None` if anything + /// fails. + fn is_vscmd_target_cl(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + let cmd_target = vscmd_target_cl(env_getter)?; + Some(target.as_vs_arch() == cmd_target) + } + + /// Detect the target architecture of `cl.exe` in the current path, and return `None` if this + /// fails for any reason. + fn vscmd_target_cl(env_getter: &dyn EnvGetter) -> Option<&'static str> { + let cl_exe = env_getter.get_env("PATH").and_then(|path| { + env::split_paths(&path) + .map(|p| p.join("cl.exe")) + .find(|p| p.exists()) + })?; + let mut cl = Command::new(cl_exe); + cl.stderr(std::process::Stdio::piped()) + .stdout(std::process::Stdio::null()); + + let out = cl.output().ok()?; + let cl_arch = out + .stderr + .split(|&b| b == b'\n' || b == b'\r') + .next()? + .rsplit(|&b| b == b' ') + .next()?; + + match cl_arch { + b"x64" => Some("x64"), + b"x86" => Some("x86"), + b"ARM64" => Some("arm64"), + b"ARM" => Some("arm"), + _ => None, + } } /// Attempt to find the tool using environment variables set by vcvars. - pub(super) fn find_msvc_environment(tool: &str, target: TargetArch<'_>) -> Option { - // Early return if the environment doesn't contain a VC install. - if env::var_os("VCINSTALLDIR").is_none() { + pub(super) fn find_msvc_environment( + tool: &str, + target: TargetArch, + env_getter: &dyn EnvGetter, + ) -> Option { + // Early return if the environment isn't one that is known to have compiler toolsets in PATH + // `VCINSTALLDIR` is set from vcvarsall.bat (developer command prompt) + // `VSTEL_MSBuildProjectFullPath` is set by msbuild when invoking custom build steps + // NOTE: `VisualStudioDir` used to be used but this isn't set when invoking msbuild from the commandline + if env_getter.get_env("VCINSTALLDIR").is_none() + && env_getter.get_env("VSTEL_MSBuildProjectFullPath").is_none() + { return None; } - let vs_install_dir = env::var_os("VSINSTALLDIR")?.into(); // If the vscmd target differs from the requested target then // attempt to get the tool using the VS install directory. - if is_vscmd_target(target) == Some(false) { + if is_vscmd_target(target, env_getter) == Some(false) { // We will only get here with versions 15+. - tool_from_vs15plus_instance(tool, target, &vs_install_dir) + let vs_install_dir: PathBuf = env_getter.get_env("VSINSTALLDIR")?.into(); + tool_from_vs15plus_instance(tool, target, &vs_install_dir, env_getter) } else { // Fallback to simply using the current environment. - env::var_os("PATH") + env_getter + .get_env("PATH") .and_then(|path| { env::split_paths(&path) .map(|p| p.join(tool)) @@ -321,25 +455,26 @@ mod impl_ { } } - fn find_msbuild_vs17(target: TargetArch<'_>) -> Option { - find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "17") + fn find_msbuild_vs17(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "17", env_getter) } #[allow(bare_trait_objects)] fn vs16plus_instances( - target: TargetArch<'_>, + target: TargetArch, version: &'static str, + env_getter: &dyn EnvGetter, ) -> Box> { - let instances = if let Some(instances) = vs15plus_instances(target) { + let instances = if let Some(instances) = vs15plus_instances(target, env_getter) { instances } else { return Box::new(iter::empty()); }; Box::new(instances.into_iter().filter_map(move |instance| { let installation_name = instance.installation_name()?; - if installation_name.starts_with(&format!("VisualStudio/{}.", version)) { - Some(instance.installation_path()?) - } else if installation_name.starts_with(&format!("VisualStudioPreview/{}.", version)) { + if installation_name.starts_with(&format!("VisualStudio/{}.", version)) + || installation_name.starts_with(&format!("VisualStudioPreview/{}.", version)) + { Some(instance.installation_path()?) } else { None @@ -349,20 +484,21 @@ mod impl_ { fn find_tool_in_vs16plus_path( tool: &str, - target: TargetArch<'_>, + target: TargetArch, version: &'static str, + env_getter: &dyn EnvGetter, ) -> Option { - vs16plus_instances(target, version) + vs16plus_instances(target, version, env_getter) .filter_map(|path| { let path = path.join(tool); if !path.is_file() { return None; } let mut tool = Tool::with_family(path, MSVC_FAMILY); - if target == "x86_64" { + if target == TargetArch::X64 { tool.env.push(("Platform".into(), "X64".into())); } - if target == "aarch64" || target == "arm64ec" { + if matches!(target, TargetArch::Arm64 | TargetArch::Arm64ec) { tool.env.push(("Platform".into(), "ARM64".into())); } Some(tool) @@ -370,8 +506,8 @@ mod impl_ { .next() } - fn find_msbuild_vs16(target: TargetArch<'_>) -> Option { - find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "16") + fn find_msbuild_vs16(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "16", env_getter) } // In MSVC 15 (2017) MS once again changed the scheme for locating @@ -386,8 +522,9 @@ mod impl_ { // // However, on ARM64 this method doesn't work because VS Installer fails to register COM component on ARM64. // Hence, as the last resort we try to use vswhere.exe to list available instances. - fn vs15plus_instances(target: TargetArch<'_>) -> Option { - vs15plus_instances_using_com().or_else(|| vs15plus_instances_using_vswhere(target)) + fn vs15plus_instances(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + vs15plus_instances_using_com() + .or_else(|| vs15plus_instances_using_vswhere(target, env_getter)) } fn vs15plus_instances_using_com() -> Option { @@ -399,11 +536,15 @@ mod impl_ { Some(VsInstances::ComBased(enum_setup_instances)) } - fn vs15plus_instances_using_vswhere(target: TargetArch<'_>) -> Option { - let program_files_path: PathBuf = env::var("ProgramFiles(x86)") - .or_else(|_| env::var("ProgramFiles")) - .ok()? - .into(); + fn vs15plus_instances_using_vswhere( + target: TargetArch, + env_getter: &dyn EnvGetter, + ) -> Option { + let program_files_path = env_getter + .get_env("ProgramFiles(x86)") + .or_else(|| env_getter.get_env("ProgramFiles"))?; + + let program_files_path = Path::new(program_files_path.as_ref()); let vswhere_path = program_files_path.join(r"Microsoft Visual Studio\Installer\vswhere.exe"); @@ -412,15 +553,14 @@ mod impl_ { return None; } - let tools_arch = match target.into() { - "i586" | "i686" | "x86_64" => Some("x86.x64"), - "arm" | "thumbv7a" => Some("ARM"), - "aarch64" | "arm64ec" => Some("ARM64"), - _ => None, + let tools_arch = match target { + TargetArch::X86 | TargetArch::X64 => Some("x86.x64"), + TargetArch::Arm => Some("ARM"), + TargetArch::Arm64 | TargetArch::Arm64ec => Some("ARM64"), }; let vswhere_output = Command::new(vswhere_path) - .args(&[ + .args([ "-latest", "-products", "*", @@ -449,13 +589,17 @@ mod impl_ { .collect() } - pub(super) fn find_msvc_15plus(tool: &str, target: TargetArch<'_>) -> Option { - let iter = vs15plus_instances(target)?; + pub(super) fn find_msvc_15plus( + tool: &str, + target: TargetArch, + env_getter: &dyn EnvGetter, + ) -> Option { + let iter = vs15plus_instances(target, env_getter)?; iter.into_iter() .filter_map(|instance| { let version = parse_version(&instance.installation_version()?)?; let instance_path = instance.installation_path()?; - let tool = tool_from_vs15plus_instance(tool, target, &instance_path)?; + let tool = tool_from_vs15plus_instance(tool, target, &instance_path, env_getter)?; Some((version, tool)) }) .max_by(|(a_version, _), (b_version, _)| a_version.cmp(b_version)) @@ -469,8 +613,12 @@ mod impl_ { // we keep the registry method as a fallback option. // // [more reliable]: https://github.com/rust-lang/cc-rs/pull/331 - fn find_tool_in_vs15_path(tool: &str, target: TargetArch<'_>) -> Option { - let mut path = match vs15plus_instances(target) { + fn find_tool_in_vs15_path( + tool: &str, + target: TargetArch, + env_getter: &dyn EnvGetter, + ) -> Option { + let mut path = match vs15plus_instances(target, env_getter) { Some(instances) => instances .into_iter() .filter_map(|instance| instance.installation_path()) @@ -491,9 +639,9 @@ mod impl_ { path.map(|path| { let mut tool = Tool::with_family(path, MSVC_FAMILY); - if target == "x86_64" { + if target == TargetArch::X64 { tool.env.push(("Platform".into(), "X64".into())); - } else if target == "aarch64" { + } else if matches!(target, TargetArch::Arm64 | TargetArch::Arm64ec) { tool.env.push(("Platform".into(), "ARM64".into())); } tool @@ -502,11 +650,12 @@ mod impl_ { fn tool_from_vs15plus_instance( tool: &str, - target: TargetArch<'_>, - instance_path: &PathBuf, + target: TargetArch, + instance_path: &Path, + env_getter: &dyn EnvGetter, ) -> Option { let (root_path, bin_path, host_dylib_path, lib_path, alt_lib_path, include_path) = - vs15plus_vc_paths(target, instance_path)?; + vs15plus_vc_paths(target, instance_path, env_getter)?; let tool_path = bin_path.join(tool); if !tool_path.exists() { return None; @@ -526,14 +675,15 @@ mod impl_ { tool.include.push(atl_include_path); } - add_sdks(&mut tool, target)?; + add_sdks(&mut tool, target, env_getter)?; - Some(tool.into_tool()) + Some(tool.into_tool(env_getter)) } fn vs15plus_vc_paths( - target: TargetArch<'_>, + target_arch: TargetArch, instance_path: &Path, + env_getter: &dyn EnvGetter, ) -> Option<(PathBuf, PathBuf, PathBuf, PathBuf, Option, PathBuf)> { let version = vs15plus_vc_read_version(instance_path)?; @@ -553,13 +703,13 @@ mod impl_ { } _ => return None, }; - let target = lib_subdir(target)?; + let target_dir = target_arch.as_vs_arch(); // The directory layout here is MSVC/bin/Host$host/$target/ let path = instance_path.join(r"VC\Tools\MSVC").join(version); // We use the first available host architecture that can build for the target let (host_path, host) = hosts.iter().find_map(|&x| { let candidate = path.join("bin").join(format!("Host{}", x)); - if candidate.join(target).exists() { + if candidate.join(target_dir).exists() { Some((candidate, x)) } else { None @@ -567,13 +717,19 @@ mod impl_ { })?; // This is the path to the toolchain for a particular target, running // on a given host - let bin_path = host_path.join(target); + let bin_path = host_path.join(target_dir); // But! we also need PATH to contain the target directory for the host // architecture, because it contains dlls like mspdb140.dll compiled for // the host architecture. let host_dylib_path = host_path.join(host.to_lowercase()); - let lib_path = path.join("lib").join(target); - let alt_lib_path = (target == "arm64ec").then(|| path.join("lib").join("arm64ec")); + let lib_fragment = if use_spectre_mitigated_libs(env_getter) { + r"lib\spectre" + } else { + "lib" + }; + let lib_path = path.join(lib_fragment).join(target_dir); + let alt_lib_path = + (target_arch == TargetArch::Arm64ec).then(|| path.join(lib_fragment).join("arm64ec")); let include_path = path.join("include"); Some(( path, @@ -622,9 +778,16 @@ mod impl_ { Some(version) } - fn atl_paths(target: TargetArch<'_>, path: &Path) -> Option<(PathBuf, PathBuf)> { + fn use_spectre_mitigated_libs(env_getter: &dyn EnvGetter) -> bool { + env_getter + .get_env("VSCMD_ARG_VCVARS_SPECTRE") + .map(|env| env.as_ref() == "spectre") + .unwrap_or_default() + } + + fn atl_paths(target: TargetArch, path: &Path) -> Option<(PathBuf, PathBuf)> { let atl_path = path.join("atlmfc"); - let sub = lib_subdir(target)?; + let sub = target.as_vs_arch(); if atl_path.exists() { Some((atl_path.join("lib").join(sub), atl_path.join("include"))) } else { @@ -634,15 +797,19 @@ mod impl_ { // For MSVC 14 we need to find the Universal CRT as well as either // the Windows 10 SDK or Windows 8.1 SDK. - pub(super) fn find_msvc_14(tool: &str, target: TargetArch<'_>) -> Option { + pub(super) fn find_msvc_14( + tool: &str, + target: TargetArch, + env_getter: &dyn EnvGetter, + ) -> Option { let vcdir = get_vc_dir("14.0")?; let mut tool = get_tool(tool, &vcdir, target)?; - add_sdks(&mut tool, target)?; - Some(tool.into_tool()) + add_sdks(&mut tool, target, env_getter)?; + Some(tool.into_tool(env_getter)) } - fn add_sdks(tool: &mut MsvcTool, target: TargetArch<'_>) -> Option<()> { - let sub = lib_subdir(target)?; + fn add_sdks(tool: &mut MsvcTool, target: TargetArch, env_getter: &dyn EnvGetter) -> Option<()> { + let sub = target.as_vs_arch(); let (ucrt, ucrt_version) = get_ucrt_dir()?; let host = match host_arch() { @@ -661,7 +828,7 @@ mod impl_ { let ucrt_lib = ucrt.join("lib").join(&ucrt_version); tool.libs.push(ucrt_lib.join("ucrt").join(sub)); - if let Some((sdk, version)) = get_sdk10_dir() { + if let Some((sdk, version)) = get_sdk10_dir(env_getter) { tool.path.push(sdk.join("bin").join(host)); let sdk_lib = sdk.join("lib").join(&version); tool.libs.push(sdk_lib.join("um").join(sub)); @@ -683,40 +850,14 @@ mod impl_ { Some(()) } - // For MSVC 12 we need to find the Windows 8.1 SDK. - pub(super) fn find_msvc_12(tool: &str, target: TargetArch<'_>) -> Option { - let vcdir = get_vc_dir("12.0")?; - let mut tool = get_tool(tool, &vcdir, target)?; - let sub = lib_subdir(target)?; - let sdk81 = get_sdk81_dir()?; - tool.path.push(sdk81.join("bin").join(sub)); - let sdk_lib = sdk81.join("lib").join("winv6.3"); - tool.libs.push(sdk_lib.join("um").join(sub)); - let sdk_include = sdk81.join("include"); - tool.include.push(sdk_include.join("shared")); - tool.include.push(sdk_include.join("um")); - tool.include.push(sdk_include.join("winrt")); - Some(tool.into_tool()) - } - - // For MSVC 11 we need to find the Windows 8 SDK. - pub(super) fn find_msvc_11(tool: &str, target: TargetArch<'_>) -> Option { - let vcdir = get_vc_dir("11.0")?; - let mut tool = get_tool(tool, &vcdir, target)?; - let sub = lib_subdir(target)?; - let sdk8 = get_sdk8_dir()?; - tool.path.push(sdk8.join("bin").join(sub)); - let sdk_lib = sdk8.join("lib").join("win8"); - tool.libs.push(sdk_lib.join("um").join(sub)); - let sdk_include = sdk8.join("include"); - tool.include.push(sdk_include.join("shared")); - tool.include.push(sdk_include.join("um")); - tool.include.push(sdk_include.join("winrt")); - Some(tool.into_tool()) - } - - fn add_env(tool: &mut Tool, env: &str, paths: Vec) { - let prev = env::var_os(env).unwrap_or(OsString::new()); + fn add_env( + tool: &mut Tool, + env: &'static str, + paths: Vec, + env_getter: &dyn EnvGetter, + ) { + let prev = env_getter.get_env(env); + let prev = prev.as_ref().map(AsRef::as_ref).unwrap_or_default(); let prev = env::split_paths(&prev); let new = paths.into_iter().chain(prev); tool.env @@ -725,7 +866,7 @@ mod impl_ { // Given a possible MSVC installation directory, we look for the linker and // then add the MSVC library path. - fn get_tool(tool: &str, path: &Path, target: TargetArch<'_>) -> Option { + fn get_tool(tool: &str, path: &Path, target: TargetArch) -> Option { bin_subdir(target) .into_iter() .map(|(sub, host)| { @@ -741,7 +882,7 @@ mod impl_ { tool }) .filter_map(|mut tool| { - let sub = vc_lib_subdir(target)?; + let sub = vc_lib_subdir(target); tool.libs.push(path.join("lib").join(sub)); tool.include.push(path.join("include")); let atlmfc_path = path.join("atlmfc"); @@ -801,10 +942,18 @@ mod impl_ { // Before doing that, we check the "WindowsSdkDir" and "WindowsSDKVersion" // environment variables set by vcvars to use the environment sdk version // if one is already configured. - fn get_sdk10_dir() -> Option<(PathBuf, String)> { - if let (Ok(root), Ok(version)) = (env::var("WindowsSdkDir"), env::var("WindowsSDKVersion")) - { - return Some((root.into(), version.trim_end_matches('\\').to_string())); + fn get_sdk10_dir(env_getter: &dyn EnvGetter) -> Option<(PathBuf, String)> { + if let (Some(root), Some(version)) = ( + env_getter.get_env("WindowsSdkDir"), + env_getter + .get_env("WindowsSDKVersion") + .as_ref() + .and_then(|version| version.as_ref().to_str()), + ) { + return Some(( + PathBuf::from(root), + version.trim_end_matches('\\').to_string(), + )); } let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0"; @@ -819,8 +968,7 @@ mod impl_ { let dir = dirs .into_iter() .rev() - .filter(|dir| dir.join("um").join("x64").join("kernel32.lib").is_file()) - .next()?; + .find(|dir| dir.join("um").join("x64").join("kernel32.lib").is_file())?; let version = dir.components().last().unwrap(); let version = version.as_os_str().to_str().unwrap().to_string(); Some((root.into(), version)) @@ -837,13 +985,6 @@ mod impl_ { Some(root.into()) } - fn get_sdk8_dir() -> Option { - let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0"; - let key = LOCAL_MACHINE.open(key.as_ref()).ok()?; - let root = key.query_str("InstallationFolder").ok()?; - Some(root.into()) - } - const PROCESSOR_ARCHITECTURE_INTEL: u16 = 0; const PROCESSOR_ARCHITECTURE_AMD64: u16 = 9; const PROCESSOR_ARCHITECTURE_ARM64: u16 = 12; @@ -863,36 +1004,25 @@ mod impl_ { // linkers that can target the architecture we desire. The 64-bit host // linker is preferred, and hence first, due to 64-bit allowing it more // address space to work with and potentially being faster. - fn bin_subdir(target: TargetArch<'_>) -> Vec<(&'static str, &'static str)> { - match (target.into(), host_arch()) { - ("i586", X86) | ("i686", X86) => vec![("", "")], - ("i586", X86_64) | ("i686", X86_64) => vec![("amd64_x86", "amd64"), ("", "")], - ("x86_64", X86) => vec![("x86_amd64", "")], - ("x86_64", X86_64) => vec![("amd64", "amd64"), ("x86_amd64", "")], - ("arm", X86) | ("thumbv7a", X86) => vec![("x86_arm", "")], - ("arm", X86_64) | ("thumbv7a", X86_64) => vec![("amd64_arm", "amd64"), ("x86_arm", "")], + fn bin_subdir(target: TargetArch) -> Vec<(&'static str, &'static str)> { + match (target, host_arch()) { + (TargetArch::X86, X86) => vec![("", "")], + (TargetArch::X86, X86_64) => vec![("amd64_x86", "amd64"), ("", "")], + (TargetArch::X64, X86) => vec![("x86_amd64", "")], + (TargetArch::X64, X86_64) => vec![("amd64", "amd64"), ("x86_amd64", "")], + (TargetArch::Arm, X86) => vec![("x86_arm", "")], + (TargetArch::Arm, X86_64) => vec![("amd64_arm", "amd64"), ("x86_arm", "")], _ => vec![], } } - fn lib_subdir(target: TargetArch<'_>) -> Option<&'static str> { - match target.into() { - "i586" | "i686" => Some("x86"), - "x86_64" => Some("x64"), - "arm" | "thumbv7a" => Some("arm"), - "aarch64" | "arm64ec" => Some("arm64"), - _ => None, - } - } - // MSVC's x86 libraries are not in a subfolder - fn vc_lib_subdir(target: TargetArch<'_>) -> Option<&'static str> { - match target.into() { - "i586" | "i686" => Some(""), - "x86_64" => Some("amd64"), - "arm" | "thumbv7a" => Some("arm"), - "aarch64" => Some("arm64"), - _ => None, + fn vc_lib_subdir(target: TargetArch) -> &'static str { + match target { + TargetArch::X86 => "", + TargetArch::X64 => "amd64", + TargetArch::Arm => "arm", + TargetArch::Arm64 | TargetArch::Arm64ec => "arm64", } } @@ -939,7 +1069,7 @@ mod impl_ { for subkey in key.iter().filter_map(|k| k.ok()) { let val = subkey .to_str() - .and_then(|s| s.trim_left_matches("v").replace('.', "").parse().ok()); + .and_then(|s| s.trim_start_matches('v').replace('.', "").parse().ok()); let val = match val { Some(s) => s, None => continue, @@ -954,24 +1084,25 @@ mod impl_ { max_key } - pub(super) fn has_msbuild_version(version: &str) -> bool { + #[inline(always)] + pub(super) fn has_msbuild_version(version: &str, env_getter: &dyn EnvGetter) -> bool { match version { "17.0" => { - find_msbuild_vs17(TargetArch("x86_64")).is_some() - || find_msbuild_vs17(TargetArch("i686")).is_some() - || find_msbuild_vs17(TargetArch("aarch64")).is_some() + find_msbuild_vs17(TargetArch::X64, env_getter).is_some() + || find_msbuild_vs17(TargetArch::X86, env_getter).is_some() + || find_msbuild_vs17(TargetArch::Arm64, env_getter).is_some() } "16.0" => { - find_msbuild_vs16(TargetArch("x86_64")).is_some() - || find_msbuild_vs16(TargetArch("i686")).is_some() - || find_msbuild_vs16(TargetArch("aarch64")).is_some() + find_msbuild_vs16(TargetArch::X64, env_getter).is_some() + || find_msbuild_vs16(TargetArch::X86, env_getter).is_some() + || find_msbuild_vs16(TargetArch::Arm64, env_getter).is_some() } "15.0" => { - find_msbuild_vs15(TargetArch("x86_64")).is_some() - || find_msbuild_vs15(TargetArch("i686")).is_some() - || find_msbuild_vs15(TargetArch("aarch64")).is_some() + find_msbuild_vs15(TargetArch::X64, env_getter).is_some() + || find_msbuild_vs15(TargetArch::X86, env_getter).is_some() + || find_msbuild_vs15(TargetArch::Arm64, env_getter).is_some() } - "12.0" | "14.0" => LOCAL_MACHINE + "14.0" => LOCAL_MACHINE .open(&OsString::from(format!( "SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\{}", version @@ -981,33 +1112,33 @@ mod impl_ { } } - pub(super) fn find_devenv(target: TargetArch<'_>) -> Option { - find_devenv_vs15(target) + pub(super) fn find_devenv(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + find_devenv_vs15(target, env_getter) } - fn find_devenv_vs15(target: TargetArch<'_>) -> Option { - find_tool_in_vs15_path(r"Common7\IDE\devenv.exe", target) + fn find_devenv_vs15(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + find_tool_in_vs15_path(r"Common7\IDE\devenv.exe", target, env_getter) } // see http://stackoverflow.com/questions/328017/path-to-msbuild - pub(super) fn find_msbuild(target: TargetArch<'_>) -> Option { + pub(super) fn find_msbuild(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { // VS 15 (2017) changed how to locate msbuild - if let Some(r) = find_msbuild_vs17(target) { + if let Some(r) = find_msbuild_vs17(target, env_getter) { Some(r) - } else if let Some(r) = find_msbuild_vs16(target) { + } else if let Some(r) = find_msbuild_vs16(target, env_getter) { return Some(r); - } else if let Some(r) = find_msbuild_vs15(target) { + } else if let Some(r) = find_msbuild_vs15(target, env_getter) { return Some(r); } else { find_old_msbuild(target) } } - fn find_msbuild_vs15(target: TargetArch<'_>) -> Option { - find_tool_in_vs15_path(r"MSBuild\15.0\Bin\MSBuild.exe", target) + fn find_msbuild_vs15(target: TargetArch, env_getter: &dyn EnvGetter) -> Option { + find_tool_in_vs15_path(r"MSBuild\15.0\Bin\MSBuild.exe", target, env_getter) } - fn find_old_msbuild(target: TargetArch<'_>) -> Option { + fn find_old_msbuild(target: TargetArch) -> Option { let key = r"SOFTWARE\Microsoft\MSBuild\ToolsVersions"; LOCAL_MACHINE .open(key.as_ref()) @@ -1019,7 +1150,7 @@ mod impl_ { let mut path = PathBuf::from(path); path.push("MSBuild.exe"); let mut tool = Tool::with_family(path, MSVC_FAMILY); - if target == "x86_64" { + if target == TargetArch::X64 { tool.env.push(("Platform".into(), "X64".into())); } tool @@ -1030,71 +1161,78 @@ mod impl_ { /// Non-Windows Implementation. #[cfg(not(windows))] mod impl_ { - use std::{env, ffi::OsString}; + use std::{env, ffi::OsStr}; - use super::{TargetArch, MSVC_FAMILY}; + use super::{EnvGetter, TargetArch, MSVC_FAMILY}; use crate::Tool; /// Finding msbuild.exe tool under unix system is not currently supported. /// Maybe can check it using an environment variable looks like `MSBUILD_BIN`. - pub(super) fn find_msbuild(_target: TargetArch<'_>) -> Option { + #[inline(always)] + pub(super) fn find_msbuild(_target: TargetArch, _: &dyn EnvGetter) -> Option { None } // Finding devenv.exe tool under unix system is not currently supported. // Maybe can check it using an environment variable looks like `DEVENV_BIN`. - pub(super) fn find_devenv(_target: TargetArch<'_>) -> Option { + #[inline(always)] + pub(super) fn find_devenv(_target: TargetArch, _: &dyn EnvGetter) -> Option { None } /// Attempt to find the tool using environment variables set by vcvars. - pub(super) fn find_msvc_environment(tool: &str, _target: TargetArch<'_>) -> Option { + pub(super) fn find_msvc_environment( + tool: &str, + _target: TargetArch, + env_getter: &dyn EnvGetter, + ) -> Option { // Early return if the environment doesn't contain a VC install. - let vc_install_dir = env::var_os("VCINSTALLDIR")?; - let vs_install_dir = env::var_os("VSINSTALLDIR")?; + let vc_install_dir = env_getter.get_env("VCINSTALLDIR")?; + let vs_install_dir = env_getter.get_env("VSINSTALLDIR")?; - let get_tool = |install_dir: OsString| { - env::split_paths(&install_dir) + let get_tool = |install_dir: &OsStr| { + env::split_paths(install_dir) .map(|p| p.join(tool)) .find(|p| p.exists()) - .map(|path| Tool::with_family(path.into(), MSVC_FAMILY)) + .map(|path| Tool::with_family(path, MSVC_FAMILY)) }; // Take the path of tool for the vc install directory. - get_tool(vc_install_dir) + get_tool(vc_install_dir.as_ref()) // Take the path of tool for the vs install directory. - .or_else(|| get_tool(vs_install_dir)) + .or_else(|| get_tool(vs_install_dir.as_ref())) // Take the path of tool for the current path environment. - .or_else(|| env::var_os("PATH").and_then(|path| get_tool(path))) + .or_else(|| { + env_getter + .get_env("PATH") + .as_ref() + .map(|path| path.as_ref()) + .and_then(get_tool) + }) } - pub(super) fn find_msvc_15plus(_tool: &str, _target: TargetArch<'_>) -> Option { + #[inline(always)] + pub(super) fn find_msvc_15plus( + _tool: &str, + _target: TargetArch, + _: &dyn EnvGetter, + ) -> Option { None } // For MSVC 14 we need to find the Universal CRT as well as either // the Windows 10 SDK or Windows 8.1 SDK. - pub(super) fn find_msvc_14(_tool: &str, _target: TargetArch<'_>) -> Option { - None - } - - // For MSVC 12 we need to find the Windows 8.1 SDK. - pub(super) fn find_msvc_12(_tool: &str, _target: TargetArch<'_>) -> Option { - None - } - - // For MSVC 11 we need to find the Windows 8 SDK. - pub(super) fn find_msvc_11(_tool: &str, _target: TargetArch<'_>) -> Option { + #[inline(always)] + pub(super) fn find_msvc_14( + _tool: &str, + _target: TargetArch, + _: &dyn EnvGetter, + ) -> Option { None } - pub(super) fn has_msbuild_version(version: &str) -> bool { - match version { - "17.0" => false, - "16.0" => false, - "15.0" => false, - "12.0" | "14.0" => false, - _ => false, - } + #[inline(always)] + pub(super) fn has_msbuild_version(_version: &str, _: &dyn EnvGetter) -> bool { + false } } diff --git a/third_party/rust/cc/src/windows/mod.rs b/third_party/rust/cc/src/windows/mod.rs index 9b6f297e1a6dbe..ccf22b0eb55cdd 100644 --- a/third_party/rust/cc/src/windows/mod.rs +++ b/third_party/rust/cc/src/windows/mod.rs @@ -6,6 +6,8 @@ pub mod find_tools; #[cfg(windows)] pub(crate) mod windows_sys; +#[cfg(windows)] +mod windows_targets; #[cfg(windows)] mod registry; diff --git a/third_party/rust/cc/src/windows/vs_instances.rs b/third_party/rust/cc/src/windows/vs_instances.rs index e863dadabbb5ad..3e6eeed9d66268 100644 --- a/third_party/rust/cc/src/windows/vs_instances.rs +++ b/third_party/rust/cc/src/windows/vs_instances.rs @@ -73,7 +73,7 @@ impl TryFrom<&Vec> for VswhereInstance { fn try_from(output: &Vec) -> Result { let map: HashMap<_, _> = output .lines() - .filter_map(Result::ok) + .map_while(Result::ok) .filter_map(|s| { let mut splitn = s.splitn(2, ": "); Some((splitn.next()?.to_owned(), splitn.next()?.to_owned())) diff --git a/third_party/rust/cc/src/windows/windows_sys.rs b/third_party/rust/cc/src/windows/windows_sys.rs index 8b98ce97f8c657..376a281fbe6b41 100644 --- a/third_party/rust/cc/src/windows/windows_sys.rs +++ b/third_party/rust/cc/src/windows/windows_sys.rs @@ -6,7 +6,7 @@ // cd generate-windows-sys/ // cargo run // ``` -// Bindings generated by `windows-bindgen` 0.53.0 +// Bindings generated by `windows-bindgen` 0.59.0 #![allow( non_snake_case, @@ -15,112 +15,23 @@ dead_code, clippy::all )] -#[link(name = "advapi32")] -extern "system" { - pub fn RegCloseKey(hkey: HKEY) -> WIN32_ERROR; -} -#[link(name = "advapi32")] -extern "system" { - pub fn RegEnumKeyExW( - hkey: HKEY, - dwindex: u32, - lpname: PWSTR, - lpcchname: *mut u32, - lpreserved: *const u32, - lpclass: PWSTR, - lpcchclass: *mut u32, - lpftlastwritetime: *mut FILETIME, - ) -> WIN32_ERROR; -} -#[link(name = "advapi32")] -extern "system" { - pub fn RegOpenKeyExW( - hkey: HKEY, - lpsubkey: PCWSTR, - uloptions: u32, - samdesired: REG_SAM_FLAGS, - phkresult: *mut HKEY, - ) -> WIN32_ERROR; -} -#[link(name = "advapi32")] -extern "system" { - pub fn RegQueryValueExW( - hkey: HKEY, - lpvaluename: PCWSTR, - lpreserved: *const u32, - lptype: *mut REG_VALUE_TYPE, - lpdata: *mut u8, - lpcbdata: *mut u32, - ) -> WIN32_ERROR; -} -#[link(name = "kernel32")] -extern "system" { - pub fn FreeLibrary(hlibmodule: HMODULE) -> BOOL; -} -#[link(name = "kernel32")] -extern "system" { - pub fn GetMachineTypeAttributes( - machine: u16, - machinetypeattributes: *mut MACHINE_ATTRIBUTES, - ) -> HRESULT; -} -#[link(name = "kernel32")] -extern "system" { - pub fn GetProcAddress(hmodule: HMODULE, lpprocname: PCSTR) -> FARPROC; -} -#[link(name = "kernel32")] -extern "system" { - pub fn LoadLibraryA(lplibfilename: PCSTR) -> HMODULE; -} -#[link(name = "kernel32")] -extern "system" { - pub fn OpenSemaphoreA(dwdesiredaccess: u32, binherithandle: BOOL, lpname: PCSTR) -> HANDLE; -} -#[link(name = "kernel32")] -extern "system" { - pub fn PeekNamedPipe( - hnamedpipe: HANDLE, - lpbuffer: *mut ::core::ffi::c_void, - nbuffersize: u32, - lpbytesread: *mut u32, - lptotalbytesavail: *mut u32, - lpbytesleftthismessage: *mut u32, - ) -> BOOL; -} -#[link(name = "kernel32")] -extern "system" { - pub fn ReleaseSemaphore( - hsemaphore: HANDLE, - lreleasecount: i32, - lppreviouscount: *mut i32, - ) -> BOOL; -} -#[link(name = "kernel32")] -extern "system" { - pub fn WaitForSingleObject(hhandle: HANDLE, dwmilliseconds: u32) -> WAIT_EVENT; -} -#[link(name = "ole32")] -extern "system" { - pub fn CoCreateInstance( - rclsid: *const GUID, - punkouter: *mut ::core::ffi::c_void, - dwclscontext: CLSCTX, - riid: *const GUID, - ppv: *mut *mut ::core::ffi::c_void, - ) -> HRESULT; -} -#[link(name = "ole32")] -extern "system" { - pub fn CoInitializeEx(pvreserved: *const ::core::ffi::c_void, dwcoinit: u32) -> HRESULT; -} -#[link(name = "oleaut32")] -extern "system" { - pub fn SysFreeString(bstrstring: BSTR); -} -#[link(name = "oleaut32")] -extern "system" { - pub fn SysStringLen(pbstr: BSTR) -> u32; -} + +windows_targets::link!("ole32.dll" "system" fn CoCreateInstance(rclsid : *const GUID, punkouter : * mut core::ffi::c_void, dwclscontext : CLSCTX, riid : *const GUID, ppv : *mut *mut core::ffi::c_void) -> HRESULT); +windows_targets::link!("ole32.dll" "system" fn CoInitializeEx(pvreserved : *const core::ffi::c_void, dwcoinit : u32) -> HRESULT); +windows_targets::link!("kernel32.dll" "system" fn FreeLibrary(hlibmodule : HMODULE) -> BOOL); +windows_targets::link!("kernel32.dll" "system" fn GetMachineTypeAttributes(machine : u16, machinetypeattributes : *mut MACHINE_ATTRIBUTES) -> HRESULT); +windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(hmodule : HMODULE, lpprocname : PCSTR) -> FARPROC); +windows_targets::link!("kernel32.dll" "system" fn LoadLibraryA(lplibfilename : PCSTR) -> HMODULE); +windows_targets::link!("kernel32.dll" "system" fn OpenSemaphoreA(dwdesiredaccess : u32, binherithandle : BOOL, lpname : PCSTR) -> HANDLE); +windows_targets::link!("kernel32.dll" "system" fn PeekNamedPipe(hnamedpipe : HANDLE, lpbuffer : *mut core::ffi::c_void, nbuffersize : u32, lpbytesread : *mut u32, lptotalbytesavail : *mut u32, lpbytesleftthismessage : *mut u32) -> BOOL); +windows_targets::link!("advapi32.dll" "system" fn RegCloseKey(hkey : HKEY) -> WIN32_ERROR); +windows_targets::link!("advapi32.dll" "system" fn RegEnumKeyExW(hkey : HKEY, dwindex : u32, lpname : PWSTR, lpcchname : *mut u32, lpreserved : *const u32, lpclass : PWSTR, lpcchclass : *mut u32, lpftlastwritetime : *mut FILETIME) -> WIN32_ERROR); +windows_targets::link!("advapi32.dll" "system" fn RegOpenKeyExW(hkey : HKEY, lpsubkey : PCWSTR, uloptions : u32, samdesired : REG_SAM_FLAGS, phkresult : *mut HKEY) -> WIN32_ERROR); +windows_targets::link!("advapi32.dll" "system" fn RegQueryValueExW(hkey : HKEY, lpvaluename : PCWSTR, lpreserved : *const u32, lptype : *mut REG_VALUE_TYPE, lpdata : *mut u8, lpcbdata : *mut u32) -> WIN32_ERROR); +windows_targets::link!("kernel32.dll" "system" fn ReleaseSemaphore(hsemaphore : HANDLE, lreleasecount : i32, lppreviouscount : *mut i32) -> BOOL); +windows_targets::link!("oleaut32.dll" "system" fn SysFreeString(bstrstring : BSTR)); +windows_targets::link!("oleaut32.dll" "system" fn SysStringLen(pbstr : BSTR) -> u32); +windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT); pub type ADVANCED_FEATURE_FLAGS = u16; pub type BOOL = i32; pub type BSTR = *const u16; @@ -131,31 +42,23 @@ pub const COINIT_MULTITHREADED: COINIT = 0i32; pub const ERROR_NO_MORE_ITEMS: WIN32_ERROR = 259u32; pub const ERROR_SUCCESS: WIN32_ERROR = 0u32; pub const FALSE: BOOL = 0i32; -pub type FARPROC = ::core::option::Option isize>; +pub type FARPROC = Option isize>; #[repr(C)] +#[derive(Clone, Copy)] pub struct FILETIME { pub dwLowDateTime: u32, pub dwHighDateTime: u32, } -impl ::core::marker::Copy for FILETIME {} -impl ::core::clone::Clone for FILETIME { - fn clone(&self) -> Self { - *self - } -} +pub const FILE_ATTRIBUTE_TEMPORARY: FILE_FLAGS_AND_ATTRIBUTES = 256u32; +pub type FILE_FLAGS_AND_ATTRIBUTES = u32; #[repr(C)] +#[derive(Clone, Copy)] pub struct GUID { pub data1: u32, pub data2: u16, pub data3: u16, pub data4: [u8; 8], } -impl ::core::marker::Copy for GUID {} -impl ::core::clone::Clone for GUID { - fn clone(&self) -> Self { - *self - } -} impl GUID { pub const fn from_u128(uuid: u128) -> Self { Self { @@ -166,13 +69,25 @@ impl GUID { } } } -pub type HANDLE = *mut ::core::ffi::c_void; -pub type HKEY = *mut ::core::ffi::c_void; +pub type HANDLE = *mut core::ffi::c_void; +pub type HINSTANCE = *mut core::ffi::c_void; +pub type HKEY = *mut core::ffi::c_void; pub const HKEY_LOCAL_MACHINE: HKEY = -2147483646i32 as _; -pub type HMODULE = *mut ::core::ffi::c_void; +pub type HMODULE = *mut core::ffi::c_void; pub type HRESULT = i32; pub type IMAGE_FILE_MACHINE = u16; pub const IMAGE_FILE_MACHINE_AMD64: IMAGE_FILE_MACHINE = 34404u16; +pub const IID_IUnknown: GUID = GUID::from_u128(0x00000000_0000_0000_c000_000000000046); +#[repr(C)] +pub struct IUnknown_Vtbl { + pub QueryInterface: unsafe extern "system" fn( + this: *mut core::ffi::c_void, + iid: *const GUID, + interface: *mut *mut core::ffi::c_void, + ) -> HRESULT, + pub AddRef: unsafe extern "system" fn(this: *mut core::ffi::c_void) -> u32, + pub Release: unsafe extern "system" fn(this: *mut core::ffi::c_void) -> u32, +} pub const KEY_READ: REG_SAM_FLAGS = 131097u32; pub const KEY_WOW64_32KEY: REG_SAM_FLAGS = 512u32; pub type MACHINE_ATTRIBUTES = i32; @@ -183,31 +98,21 @@ pub type REG_SAM_FLAGS = u32; pub const REG_SZ: REG_VALUE_TYPE = 1u32; pub type REG_VALUE_TYPE = u32; #[repr(C)] +#[derive(Clone, Copy)] pub struct SAFEARRAY { pub cDims: u16, pub fFeatures: ADVANCED_FEATURE_FLAGS, pub cbElements: u32, pub cLocks: u32, - pub pvData: *mut ::core::ffi::c_void, + pub pvData: *mut core::ffi::c_void, pub rgsabound: [SAFEARRAYBOUND; 1], } -impl ::core::marker::Copy for SAFEARRAY {} -impl ::core::clone::Clone for SAFEARRAY { - fn clone(&self) -> Self { - *self - } -} #[repr(C)] +#[derive(Clone, Copy)] pub struct SAFEARRAYBOUND { pub cElements: u32, pub lLbound: i32, } -impl ::core::marker::Copy for SAFEARRAYBOUND {} -impl ::core::clone::Clone for SAFEARRAYBOUND { - fn clone(&self) -> Self { - *self - } -} pub const SEMAPHORE_MODIFY_STATE: SYNCHRONIZATION_ACCESS_RIGHTS = 2u32; pub type SYNCHRONIZATION_ACCESS_RIGHTS = u32; pub const S_FALSE: HRESULT = 0x1_u32 as _; @@ -221,3 +126,9 @@ pub const WAIT_FAILED: WAIT_EVENT = 4294967295u32; pub const WAIT_OBJECT_0: WAIT_EVENT = 0u32; pub const WAIT_TIMEOUT: WAIT_EVENT = 258u32; pub type WIN32_ERROR = u32; + +#[link(name = "advapi32")] +#[link(name = "ole32")] +#[link(name = "oleaut32")] +extern "C" {} +use super::windows_targets; diff --git a/third_party/rust/cc/src/windows/windows_targets.rs b/third_party/rust/cc/src/windows/windows_targets.rs new file mode 100644 index 00000000000000..d08affe0041700 --- /dev/null +++ b/third_party/rust/cc/src/windows/windows_targets.rs @@ -0,0 +1,19 @@ +//! Provides the `link!` macro used by the generated windows bindings. +//! +//! This is a simple wrapper around an `extern` block with a `#[link]` attribute. +//! It's very roughly equivalent to the windows-targets crate. + +macro_rules! link_macro { + ($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => ( + // Note: the windows-targets crate uses a pre-built Windows.lib import library which we don't + // have in this repo. So instead we always link kernel32.lib and add the rest of the import + // libraries below by using an empty extern block. This works because extern blocks are not + // connected to the library given in the #[link] attribute. + #[link(name = "kernel32")] + extern $abi { + $(#[link_name=$link_name])? + pub fn $($function)*; + } + ) +} +pub(crate) use link_macro as link;