Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build script is using code to compile Windows target instead of WASM target #14881

Closed
FaceFTW opened this issue Dec 2, 2024 · 5 comments
Closed
Labels
A-build-scripts Area: build.rs scripts C-bug Category: bug

Comments

@FaceFTW
Copy link

FaceFTW commented Dec 2, 2024

This is a bug related to a build.rs file using conditional compilation macros to change it's behavior based on the target being compiled to (either WASM or Windows). The minimal example I was able to create is found in this repository here: https://github.com/FaceFTW/cfg-bug

Originally, I found the issue in this repository and had a hacky workaround in CI: https://github.com/FaceFTW/rust-pipes

I tried this:
Compiling the library target of the project for the wasm32-unknown-unknown target with Cargo using the following build script:

build.rs

use std::io;

#[cfg(not(target_os = "windows"))]
fn main() -> io::Result<()> {
    println!("WASM Build script is being run");
    dbg!(cfg!(target_os = "unknown"));
    Ok(())
}

#[cfg(all(target_os = "windows"))]
fn main() -> io::Result<()> {
    println!("Windows Build Script is being Run");
    dbg!(cfg!(target_os = "unknown"));

    Ok(())
}

I expected to see this happen:
When running the following command, I should be able to find the following output from the build script indicating the build script is using the specific code for WASM targets:

cargo +nightly build --lib --release --target wasm32-unknown-unknown -vv

[cfg-bug 0.1.0] WASM Build Script is being Run
[cfg-bug 0.1.0] [build.rs:13:5] cfg!(target_os = "unknown") = true

Instead, this happened:
When running the same command, I instead see that the build script is using code for Windows targets. The expected target from my understanding of the triple is unknown in wasm32-unknown-unknown.

cargo +nightly build --lib --release --target wasm32-unknown-unknown -vv

[cfg-bug 0.1.0] Windows Build Script is being Run
[cfg-bug 0.1.0] [build.rs:13:5] cfg!(target_os = "unknown") = false

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-pc-windows-msvc
release: 1.83.0
LLVM version: 19.1.1
Backtrace

   Compiling cfg-bug v0.1.0 (C:\Users\awest\Workspaces\cfg-bug)
     Running `set CARGO=\\?\C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe&& set CARGO_CRATE_NAME=build_script_build&& set CARGO_MANIFEST_DIR=C:\Users\awest\Workspaces\cfg-bug&& set CARGO_MANIFEST_PATH=C:\Users\awest\Workspaces\cfg-bug\Cargo.toml&& set CARGO_PKG_AUTHORS=""&& set CARGO_PKG_DESCRIPTION=""&& set CARGO_PKG_HOMEPAGE=""&& set CARGO_PKG_LICENSE=""&& set CARGO_PKG_LICENSE_FILE=""&& set CARGO_PKG_NAME=cfg-bug&& set CARGO_PKG_README=""&& set CARGO_PKG_REPOSITORY=""&& set CARGO_PKG_RUST_VERSION=""&& set CARGO_PKG_VERSION=0.1.0&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=1&& set CARGO_PKG_VERSION_PATCH=0&& set CARGO_PKG_VERSION_PRE=""&& set CARGO_PRIMARY_PACKAGE=1&& set PATH="C:\Users\awest\Workspaces\cfg-bug\target\release\deps;C:\Program Files\PowerShell\7;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files\Microsoft\jdk-11.0.25.9-hotspot\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;%NVM_HOME%;%NVM_SYMLINK%;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\Git LFS;C:\Program Files\CMake\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PuTTY\;C:\Program Files (x86)\gnupg\bin;C:\Program Files (x86)\Gpg4win\..\GnuPG\bin;C:\Program Files (x86)\dotnet-core-uninstall\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Users\awest\AppData\Local\Programs\Python\Python312\Scripts\;C:\Users\awest\AppData\Local\Programs\Python\Python312\;C:\Users\awest\AppData\Local\Programs\Python\Launcher\;C:\Users\awest\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\awest\AppData\Local\Programs\Python\Python311\;C:\Users\awest\.cargo\bin;C:\Users\awest\AppData\Local\Microsoft\WindowsApps;C:\Users\awest\AppData\Local\Microsoft\WinGet\Links;C:\Users\awest\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\awest\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\awest\.local\bin;C:\Users\awest.local\bin\vim"&& C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\rustc.exe --crate-name build_script_build --edition=2021 build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=261 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off --check-cfg cfg(docsrs) --check-cfg "cfg(feature, values())" -C metadata=5a1ee0679c54dc42 -C extra-filename=-5a1ee0679c54dc42 --out-dir C:\Users\awest\Workspaces\cfg-bug\target\release\build\cfg-bug-5a1ee0679c54dc42 -C strip=debuginfo -L dependency=C:\Users\awest\Workspaces\cfg-bug\target\release\deps`
     Running `set CARGO=\\?\C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe&& set CARGO_CFG_PANIC=abort&& set CARGO_CFG_TARGET_ABI=""&& set CARGO_CFG_TARGET_ARCH=wasm32&& set CARGO_CFG_TARGET_ENDIAN=little&& set CARGO_CFG_TARGET_ENV=""&& set CARGO_CFG_TARGET_FAMILY=wasm&& set CARGO_CFG_TARGET_FEATURE=mutable-globals,sign-ext&& set CARGO_CFG_TARGET_HAS_ATOMIC=16,32,64,8,ptr&& set CARGO_CFG_TARGET_OS=unknown&& set CARGO_CFG_TARGET_POINTER_WIDTH=32&& set CARGO_CFG_TARGET_VENDOR=unknown&& set CARGO_ENCODED_RUSTFLAGS=""&& set CARGO_MANIFEST_DIR=C:\Users\awest\Workspaces\cfg-bug&& set CARGO_MANIFEST_PATH=C:\Users\awest\Workspaces\cfg-bug\Cargo.toml&& set CARGO_PKG_AUTHORS=""&& set CARGO_PKG_DESCRIPTION=""&& set CARGO_PKG_HOMEPAGE=""&& set CARGO_PKG_LICENSE=""&& set CARGO_PKG_LICENSE_FILE=""&& set CARGO_PKG_NAME=cfg-bug&& set CARGO_PKG_README=""&& set CARGO_PKG_REPOSITORY=""&& set CARGO_PKG_RUST_VERSION=""&& set CARGO_PKG_VERSION=0.1.0&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=1&& set CARGO_PKG_VERSION_PATCH=0&& set CARGO_PKG_VERSION_PRE=""&& set DEBUG=false&& set HOST=x86_64-pc-windows-msvc&& set NUM_JOBS=16&& set OPT_LEVEL=3&& set OUT_DIR=C:\Users\awest\Workspaces\cfg-bug\target\wasm32-unknown-unknown\release\build\cfg-bug-4e9b01acbcb67e7b\out&& set PATH="C:\Users\awest\Workspaces\cfg-bug\target\release\deps;C:\Users\awest\Workspaces\cfg-bug\target\release;C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib;C:\Program Files\PowerShell\7;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files\Microsoft\jdk-11.0.25.9-hotspot\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;%NVM_HOME%;%NVM_SYMLINK%;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\Git LFS;C:\Program Files\CMake\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PuTTY\;C:\Program Files (x86)\gnupg\bin;C:\Program Files (x86)\Gpg4win\..\GnuPG\bin;C:\Program Files (x86)\dotnet-core-uninstall\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Users\awest\AppData\Local\Programs\Python\Python312\Scripts\;C:\Users\awest\AppData\Local\Programs\Python\Python312\;C:\Users\awest\AppData\Local\Programs\Python\Launcher\;C:\Users\awest\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\awest\AppData\Local\Programs\Python\Python311\;C:\Users\awest\.cargo\bin;C:\Users\awest\AppData\Local\Microsoft\WindowsApps;C:\Users\awest\AppData\Local\Microsoft\WinGet\Links;C:\Users\awest\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\awest\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\awest\.local\bin;C:\Users\awest.local\bin\vim"&& set PROFILE=release&& set RUSTC=C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\rustc.exe&& set RUSTDOC=C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\rustdoc.exe&& set TARGET=wasm32-unknown-unknown&& C:\Users\awest\Workspaces\cfg-bug\target\release\build\cfg-bug-5a1ee0679c54dc42\build-script-build`
[cfg-bug 0.1.0] Windows Build Script is being Run
[cfg-bug 0.1.0] [build.rs:13:5] cfg!(target_os = "unknown") = false
     Running `set CARGO=\\?\C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe&& set CARGO_CRATE_NAME=cfg_bug&& set CARGO_MANIFEST_DIR=C:\Users\awest\Workspaces\cfg-bug&& set CARGO_MANIFEST_PATH=C:\Users\awest\Workspaces\cfg-bug\Cargo.toml&& set CARGO_PKG_AUTHORS=""&& set CARGO_PKG_DESCRIPTION=""&& set CARGO_PKG_HOMEPAGE=""&& set CARGO_PKG_LICENSE=""&& set CARGO_PKG_LICENSE_FILE=""&& set CARGO_PKG_NAME=cfg-bug&& set CARGO_PKG_README=""&& set CARGO_PKG_REPOSITORY=""&& set CARGO_PKG_RUST_VERSION=""&& set CARGO_PKG_VERSION=0.1.0&& set CARGO_PKG_VERSION_MAJOR=0&& set CARGO_PKG_VERSION_MINOR=1&& set CARGO_PKG_VERSION_PATCH=0&& set CARGO_PKG_VERSION_PRE=""&& set CARGO_PRIMARY_PACKAGE=1&& set OUT_DIR=C:\Users\awest\Workspaces\cfg-bug\target\wasm32-unknown-unknown\release\build\cfg-bug-4e9b01acbcb67e7b\out&& set PATH="C:\Users\awest\Workspaces\cfg-bug\target\release\deps;C:\Program Files\PowerShell\7;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files\Microsoft\jdk-11.0.25.9-hotspot\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;%NVM_HOME%;%NVM_SYMLINK%;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\Git LFS;C:\Program Files\CMake\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PuTTY\;C:\Program Files (x86)\gnupg\bin;C:\Program Files (x86)\Gpg4win\..\GnuPG\bin;C:\Program Files (x86)\dotnet-core-uninstall\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Users\awest\AppData\Local\Programs\Python\Python312\Scripts\;C:\Users\awest\AppData\Local\Programs\Python\Python312\;C:\Users\awest\AppData\Local\Programs\Python\Launcher\;C:\Users\awest\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\awest\AppData\Local\Programs\Python\Python311\;C:\Users\awest\.cargo\bin;C:\Users\awest\AppData\Local\Microsoft\WindowsApps;C:\Users\awest\AppData\Local\Microsoft\WinGet\Links;C:\Users\awest\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\awest\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\awest\.local\bin;C:\Users\awest.local\bin\vim"&& C:\Users\awest\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\rustc.exe --crate-name cfg_bug --edition=2021 src\lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=261 --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no --check-cfg cfg(docsrs) --check-cfg "cfg(feature, values())" -C metadata=fd6989a280b3860f -C extra-filename=-fd6989a280b3860f --out-dir C:\Users\awest\Workspaces\cfg-bug\target\wasm32-unknown-unknown\release\deps --target wasm32-unknown-unknown -C strip=debuginfo -L dependency=C:\Users\awest\Workspaces\cfg-bug\target\wasm32-unknown-unknown\release\deps -L dependency=C:\Users\awest\Workspaces\cfg-bug\target\release\deps`
    Finished `release` profile [optimized] target(s) in 1.31s```

</p>
</details>
@FaceFTW FaceFTW added the C-bug Category: bug label Dec 2, 2024
@FaceFTW
Copy link
Author

FaceFTW commented Dec 2, 2024

@rustbot label +A-cfg

@rustbot rustbot added the A-cfg label Dec 2, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Dec 2, 2024

AFAICT this is cargo's build.rs implementation and not rustc. Transferring this to the cargo issue tracker for further triage (T-cargo please send it back here if it's a rustc concern).

@rustbot transfer cargo

@rustbot rustbot transferred this issue from rust-lang/rust Dec 2, 2024
@weihanglo
Copy link
Member

weihanglo commented Dec 2, 2024

To run your build script on your host machine, Cargo always compiles it to your host platform, which is x86_64-pc-windows-msvc. cfg! value is determined at "build script compile time" and resolved to x86_64-pc-windows-msvc.

To get the target platform you main package compile to at "build script execution time", you'll need to read either TARGET or CARGO_CFG_TARGET_OS environment variable via std::env::var{_os}, which will be wasm32-unknown-unknown. See Environment variables Cargo sets for build scripts

If you do want to compile your build script to a certain target platform, see the unstable feature artifact dependencies.

@weihanglo weihanglo added A-build-scripts Area: build.rs scripts and removed needs-triage labels Dec 2, 2024
@weihanglo
Copy link
Member

As this seems to be a misuse of cfg! when other environment variables set at build script execution time ought to be used, I am going to close this. If you have more questions, feel free to ask here or on forums like https://users.rust-lang.org/.

@FaceFTW
Copy link
Author

FaceFTW commented Dec 2, 2024

Thanks for the quick response/triage. I'll add a quick README to the example repository with a reference to this information in case anyone stumbles upon it in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants