rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.8k stars 2.43k forks source link

Tracking Issue for host-config #9452

Open jameshilliard opened 3 years ago

jameshilliard commented 3 years ago

Summary

Original issue: #3349 Implementation: #9322 Documentation: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#host-config Issues: https://github.com/rust-lang/cargo/labels/Z-host-config

The host key in a config file can be used pass flags to host build targets such as build scripts that must run on the host system instead of the target system when cross compiling. It supports both generic and host arch specific tables. Matching host arch tables take precedence over generic host tables.

Nemo157 commented 2 years ago

I'm attempting to use this to fix https://github.com/rust-lang/docs.rs/issues/1580 by setting the configured rustc-args for all the target and host builds, using this command I expected to see the --cfg procmacro2_semver_exempt make it through to the build_script_build rustc invocation, but it doesn't appear to, am I misunderstanding this feature or is something going wrong here?

cargo rustdoc -vv \
--target x86_64-unknown-linux-gnu \
--config 'build.rustflags=["--cfg", "procmacro2_semver_exempt"]' \
--config 'host.rustflags=["--cfg", "procmacro2_semver_exempt"]' \
-Zhost-config -Ztarget-applies-to-host -Zunstable-options \
-- --cfg procmacro2_semver_exempt --cfg doc_cfg
Nemo157 commented 2 years ago

It looks like rustflags are still being explicitly excluded from all build-scripts, it seems like this feature should change this to read from the host.rustflags (and target specific subtables):

https://github.com/rust-lang/cargo/blob/263b1690c25e96cc641b4c2bb149a0d6185f4578/src/cargo/core/compiler/build_context/target_info.rs#L614-L619

rib commented 7 months ago

I'm currently unsure if the unstable host-config implementation is expected to Just Work with cargo clippy but I'm finding that I need to use -Zhost-config, which works with cargo build but doesn't seem to work with cargo clippy currently.

In this project we have certain crates that are considered "modules" which may either be cross compiled as wasm32-unknown-unknown modules and loaded at runtime, or else with --cfg=native_modules they may be compiled natively and directly linked into the runtime.

This --cfg=native_modules option affects multiple crates that form part of a runtime and it also needs to affect our proc macros which generate an FFI interface for our modules.

We can't practically use cargo features for this, while native linking is mutually exclusive with wasm linking and although we can define a precedence for cfg() guards in rust code, we also need to be able to resolve the mutual exclusion when deciding dependencies too.

It's also notable that this project's runtime runs on PC and mobile and in particular we want this native_module linking on iOS where we can't avoid using --target.

It would be good to understand if it's just a short-term limitation of the host-config implementation that it's not handled consistently by cargo clippy (not sure about other cargo subcommands besides build currently) or would it make sense to open some other issue to discuss having cargo clippy also support this?