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

Allow specifying target-dependent build.rs scripts #4932

Open
nagisa opened this issue Jan 11, 2018 · 5 comments
Open

Allow specifying target-dependent build.rs scripts #4932

nagisa opened this issue Jan 11, 2018 · 5 comments
Labels
A-build-dependencies Area: [build-dependencies] A-build-scripts Area: build.rs scripts C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.

Comments

@nagisa
Copy link
Member

nagisa commented Jan 11, 2018

Currently it is possible to specify something like following in Cargo.toml:

[target.'cfg(unix)'.build-dependencies.cc]
version = "1.0"

however it is not feasible to use this functionality, because you cannot conditionally extern use crates or specify per-target build.rs scripts (i.e. the following does not work):

[target.'cfg(unix)'.package]
build = "build/unix.rs"
@alexcrichton alexcrichton added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Jan 11, 2018
@sfackler
Copy link
Member

Why can't you conditionally compile the contents of the build script?

@nagisa
Copy link
Member Author

nagisa commented Jan 12, 2018

The #[cfg(...)] attributes in build scripts only affects the build script in terms of where the build script will be executed (a.k.a. $HOST), whereas what I’m looking for is an ability to change the build script depending on the crate target (i.e. $BUILD).

The goal is that the build script example below would not depend on cc when cargo build --target=x86_64-pc-windows-* is invoked from a UNIX machine.

// #[$BUILD_cfg(unix)]
extern crate cc;

fn main() { ... }

...

A lot of the time if statements based on environment variables work, but you cannot remove items from a crate based on environment variables and so dependency on the cc crate must exist even if unused.

Does that explain the issue clearly enough?

NB: I’m using cc here as an example only, because that is a widespread and well known crate.

@sfackler
Copy link
Member

Ah right, that makes sense. One option could be for cargo to set equivalent cfgs for the target - something like #[cfg(cargo_target(unix))], #[cfg(cargo_target(target_os = "ios"))], etc.

@nagisa
Copy link
Member Author

nagisa commented Nov 26, 2020

Another good reason to have target-dependent build-scripts: currently use of build scripts will force compilation of a dependency that's used during cargo check, which costs a fair amount of extra time. This cost does not need to be paid for by targets that don't benefit from the build script.

maan2003 added a commit to maan2003/druid that referenced this issue Jul 6, 2021
maan2003 added a commit to linebender/druid that referenced this issue Aug 15, 2021
* x11 shell keyboard mapping

* allow deref_nullptr in tests

* [CI] install libxkbcommon for Docs

* More keys for x11

* More keys for gtk

* cache looked up mods names and avoid as *mut i8

* Apply suggestions from code review

* Move keycodes to a separate file

* allow non_upper_case_globals in keycodes.rs

* using cfg(target_os) in build scripts is *wrong*

see <rust-lang/cargo#4932>

* UnsafeCell is not needed

UnsafeCell is used to provide `*mut T` through `&T` but we already
have `*mut T`

* Update CHANGELOG
@SamNormcoreWayne
Copy link

SamNormcoreWayne commented Mar 2, 2022

I met this issue when writing a build.rs and .cargo/config.toml for a project cross-compiling from x86_64 linux to aarch64 android.

The reason why I need target-dependable build.rs is:

// build.rs
println!("cargo:rustc-link-search=native=platform/specific/lib/path");
println!("cargo:rustc-link-lib=static=some_aarch64_lib");

which means I want to link different libs by platforms in build.rs in project level. However, .cargo/config.toml only supports sub-module level over-write according to my research.

@epage epage added the S-triage Status: This issue is waiting on initial triage. label Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-dependencies Area: [build-dependencies] A-build-scripts Area: build.rs scripts C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

6 participants