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

Add CARGO_PATH environment variable #6909

Closed
wants to merge 1 commit into from

Conversation

dalance
Copy link

@dalance dalance commented May 6, 2019

This PR adds an environment variable CARGO_PATH to specify search path for programs spawned by cargo.

If there is a newer compiler than system compiler on PATH, some rust compilation fail.
This is reported at rust-lang/rust#58394 and #6582.

To resolve the issue, this PR adds CARGO_PATH to specify compiler and linker path.
For example, rust-lang/rust#58394 can be resolved like below.

$ echo $PATH
~/.linuxbrew/bin:~/.cargo/bin:/usr/bin:/bin
$ cargo build
/// failed
$ export CARGO_PATH=~/.cargo/bin:/usr/bin:/bin
$ cargo build
/// success

In this case, cargo without CARGO_PATH calls ~/.linuxbrew/bin/cc as linker, but rustc expects system linker, therefore the compilation fails. Alternatively cargo with CARGO_PATH calls system linker and the compilation succeeds.

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Eh2406 (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 6, 2019
@Eh2406
Copy link
Contributor

Eh2406 commented May 6, 2019

@bors r? @alexcrichton

@rust-highfive rust-highfive assigned alexcrichton and unassigned Eh2406 May 6, 2019
@alexcrichton
Copy link
Member

Thanks for the PR! I would personally be hesitant to merge this feature though. This seems like a bit of hack in Cargo to work around something that may not be fully understood? Are there perhaps other ways to solve this problem without resorting to this hammer?

@dalance
Copy link
Author

dalance commented May 8, 2019

In my understanding about rust-lang/rust#58394, the followings must be system toolchain because official rustc binary is linked to system libc.

  • Linker called by cargo
  • C compiler called by cc-rs

Because both of them use PATH to search toolchain, I think the issue can be resolved by CARGO_PATH.
Although I can understand this feature is too large.
Currently I have two other ways, but I think both of them are not good.

individual setting

Both of them can be set individually. Linker called by cargo can be set by ~/.cargo/config like below:

[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/cc"

C compiler called by cc-rs can be set by CC environment variable.

export CC=/usr/bin/cc

Pros

No modification is needed.

Cons

CC environment variable affects other programs like make.
If another crate like cc-rs will be created, another setting will be needed.

wrapper of cargo

A wrapper of cargo can set PATH passed to cargo.
For example, zsh function like below:

function cargo() {
      PATH=~/.cargo/bin:/usr/bin:/bin command cargo $@
}

Pros

No modification is needed.

Cons

This is not a feature of cargo/rustc. So I would be hesitant to announce this solution to other linuxbrew users.

@alexcrichton
Copy link
Member

If the goal here is to switch linkers then configuring .cargo/config is the way to do that, but I'm personally still not really understanding what the motivation is to switch out PATH

@dalance
Copy link
Author

dalance commented May 9, 2019

Linker called by cargo can be specified through .cargo/config, but C compiler/linker used at build.rs is still searched in PATH. So I want to switch PATH.

@alexcrichton
Copy link
Member

Ok if this is purely about switching linkers I'm going to close this since that's done with various other configuration that we have in Cargo. Switching rustc's linker is done via .cargo/config, and build scripts should provide their own way of configuring linkers. For crates using cc, for example, there's a variety of env vars to configure.

@dalance
Copy link
Author

dalance commented May 10, 2019

Ok. I will resolve this issue by cargo wrapper function.
Thanks.

@shijunti19
Copy link

shijunti19 commented Sep 21, 2019

How Windows 10 Solves this Problem

error: linker cc not found
|
= note: 系统找不到指定的文件。 (os error 2)

error: aborting due to previous error

@jameshilliard
Copy link
Contributor

Switching rustc's linker is done via .cargo/config, and build scripts should provide their own way of configuring linkers.

This doesn't really work in a number of cases without something like #9322.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants