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:rustc-link-arg to pass custom linker arguments #6298

Merged
merged 5 commits into from
Mar 27, 2019

Conversation

lu-zero
Copy link
Contributor

@lu-zero lu-zero commented Nov 9, 2018

It is useful to produce correct cdylibs on platforms such as Linux and
MacOS.

Groundwork to address #5045 in the future.

@alexcrichton
Copy link
Member

Thanks for the PR! I'm personally a bit wary to add this though without more protections and/or safeguards. In addition to the soft feature freeze this is a feature we have intentionally not added historically as it can be tricky to do so.

I would personally prefer at least that this takes the route of a more formal feature proposal such as a discussion on internals or a mini-rfc.

@lu-zero
Copy link
Contributor Author

lu-zero commented Nov 12, 2018

If you have time to guide me on that I'm more than willing to :)

Keep in mind that we have an env key that already let you pass those values, but it does to all the rustc invocations and my actual need is to pass the right linker flags so the soname is set for cdylibs.

We can restrict it to just cdylibs or just make cargo come up with the os-specific incantations at least for the tier1 platforms.

@alexcrichton
Copy link
Member

For posterity, there's more info on this in https://internals.rust-lang.org/t/pre-rfc-complete-cdylib-support-in-cargo/8843/12

@bors
Copy link
Collaborator

bors commented Nov 18, 2018

☔ The latest upstream changes (presumably #6328) made this pull request unmergeable. Please resolve the merge conflicts.

@lu-zero
Copy link
Contributor Author

lu-zero commented Nov 18, 2018

Here an attempt on restricting, not sure where is the best place to error out if somebody mixes cdylib and dylib.

@alexcrichton
Copy link
Member

Thanks, although I think there's still a number of open questions here:

  • Currently this is ignoring link-args on non-cdylib crates (silently), is that the behavior that we want?
  • What happens if there's mutliple crate types, where do the link args go?
  • Do we want to read this from .cargo/config? Is there a use case for that?

@lu-zero
Copy link
Contributor Author

lu-zero commented Nov 21, 2018

Thanks, although I think there's still a number of open questions here:

* Currently this is ignoring link-args on non-cdylib crates (silently), is that the behavior that we want?

The use-case I'm focusing is to pair it with a build.rs providing the link lines. So non-cdylib or non-cdylib+staticlib could be an hard error.

* What happens if there's mutliple crate types, where do the link args go?

from what I'm seeing for the cdylib+staticlib case it works as expected even if rustc is called only once.

* Do we want to read this from `.cargo/config`? Is there a use case for that?

For my usecase there isn't.

On the other hand this can be reused by cargo-deb/rpm/ebuild later for their distro-specific purposes (e.g. custom rpaths), but I'd rather not conflate it.

@bors
Copy link
Collaborator

bors commented Nov 28, 2018

☔ The latest upstream changes (presumably #6352) made this pull request unmergeable. Please resolve the merge conflicts.

@alexcrichton
Copy link
Member

Sorry for having this fall by the wayside, this fell off my radar by accident!

@lu-zero would you still be willing to help drive this PR forward?

Following up with some of my questions above, for cases where the link-args aren't used I don't really know what the best answer is. I think it's fine for now though to swallow them and document that we swallow them, and if it becomes an issue we can issue warnings or something like that later.

For the many crate types, we probably want to either:

  • Pass the link args to all linked artifacts (except maybe tests)
  • Rename these to rustc-cdylib-link-arg or somethig like that (to make it clear it only applies to cdylib and leave space to customize them in the future)

For .cargo/config what I meant was that you can specify build script outputs in .cargo/config in native library overrides. I think I missed though where this was already handled.

So all in all, the remaining items needed for this I think are:

  • Documentation for this new feature
  • Decision on where to route link args for multiple artifact (aka ignore for all but cdylib or rename to say it's only going to cdylib)

I think I'd personally lean towards renaming this rustc-cdylib-link-arg which leaves us room to add more flavorful configuration for other output artifacts in the future. For example we could have a mode where you specify binary link args but only for one binary and not others.

@lu-zero
Copy link
Contributor Author

lu-zero commented Mar 8, 2019

@lu-zero would you still be willing to help drive this PR forward?

Sure. I'll update the PR during the weekend :)

@alexcrichton
Copy link
Member

Ok sounds great, and sorry again for the delay!

@lu-zero
Copy link
Contributor Author

lu-zero commented Mar 8, 2019

I renamed the key, regarding the documentation to update, where I should I find it?

@alexcrichton alexcrichton added the T-cargo Team: Cargo label Mar 11, 2019
@alexcrichton
Copy link
Member

@rfcbot fcp merge

Ok! This looks great to me, so I'd like to propose that this be merged, but would also like to get a signoff from the rest of the team.

@rfcbot
Copy link
Collaborator

rfcbot commented Mar 11, 2019

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Mar 11, 2019
@joshtriplett
Copy link
Member

I personally would like this (and would use this) for binaries, not just cdylib, but cdylib certainly helps.

@lu-zero
Copy link
Contributor Author

lu-zero commented Mar 19, 2019

I'd relax the constraint later and land it sooner if possible :)

@rfcbot rfcbot added the final-comment-period FCP — a period for last comments before action is taken label Mar 27, 2019
@rfcbot rfcbot removed the proposed-final-comment-period An FCP proposal has started, but not yet signed off. label Mar 27, 2019
@rfcbot
Copy link
Collaborator

rfcbot commented Mar 27, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Mar 27, 2019

📌 Commit f608ed6 has been approved by alexcrichton

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Mar 27, 2019
@alexcrichton alexcrichton added the relnotes Release-note worthy label Mar 27, 2019
@bors
Copy link
Collaborator

bors commented Mar 27, 2019

⌛ Testing commit f608ed6 with merge 5f3f281...

bors added a commit that referenced this pull request Mar 27, 2019
Add cargo:rustc-link-arg to pass custom linker arguments

It is useful to produce correct `cdylibs` on platforms such as Linux and
MacOS.

Groundwork to address #5045 in the future.
@bors
Copy link
Collaborator

bors commented Mar 27, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: alexcrichton
Pushing 5f3f281 to master...

@bors bors merged commit f608ed6 into rust-lang:master Mar 27, 2019
bors added a commit to rust-lang/rust that referenced this pull request Mar 30, 2019
Update cargo

Update cargo

22 commits in 0e35bd8af0ec72d3225c4819b330b94628f0e9d0..63231f438a2b5b84ccf319a5de22343ee0316323
2019-03-13 06:52:51 +0000 to 2019-03-27 12:26:45 +0000
- Code cleanup (rust-lang/cargo#6787)
- Add cargo:rustc-link-arg to pass custom linker arguments (rust-lang/cargo#6298)
- Testsuite: remove some unnecessary is_nightly checks. (rust-lang/cargo#6786)
- cargo metadata: Don't show `null` deps. (rust-lang/cargo#6534)
- Some fingerprint cleanup. (rust-lang/cargo#6785)
- Fix fingerprint for canceled build script. (rust-lang/cargo#6782)
- Canonicalize default target if it ends with `.json` (rust-lang/cargo#6778)
- Fix setting `panic=unwind` compiling lib a extra time. (rust-lang/cargo#6781)
- Always nicely show errors from crates.io if possible (rust-lang/cargo#6771)
- Testsuite: Make `cwd()` relative to project root. (rust-lang/cargo#6768)
- Allow `cargo fix` if gitignore matches root working dir. (rust-lang/cargo#6767)
- Remove redundant imports (rust-lang/cargo#6763)
- Handle backcompat hazard with `toml` crate (rust-lang/cargo#6761)
- Fix spurious error in dirty_both_lib_and_test. (rust-lang/cargo#6756)
- Update toml requirement from 0.4.2 to 0.5.0 (rust-lang/cargo#6760)
- Reuse std::env::consts::EXE_SUFFIX (rust-lang/cargo#6758)
- Proptest 0.9.1 (rust-lang/cargo#6753)
- Don't need extern crate in 2018 (rust-lang/cargo#6752)
- Release a jobserver token while locking a file (rust-lang/cargo#6748)
- Minor doc fix for publish command synopsis (rust-lang/cargo#6749)
- Stricter package change detection. (rust-lang/cargo#6740)
- Fix resolving yanked crates when using a local registry. (rust-lang/cargo#6742)
@rfcbot rfcbot added finished-final-comment-period FCP complete and removed final-comment-period FCP — a period for last comments before action is taken labels Apr 6, 2019
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request May 31, 2019
Version 1.35.0 (2019-05-23)
==========================

Language
--------
- [`FnOnce`, `FnMut`, and the `Fn` traits are now implemented for `Box<FnOnce>`,
  `Box<FnMut>`, and `Box<Fn>` respectively.][59500]
- [You can now coerce closures into unsafe function pointers.][59580] e.g.
  ```rust
  unsafe fn call_unsafe(func: unsafe fn()) {
      func()
  }

  pub fn main() {
      unsafe { call_unsafe(|| {}); }
  }
  ```


Compiler
--------
- [Added the `armv6-unknown-freebsd-gnueabihf` and
  `armv7-unknown-freebsd-gnueabihf` targets.][58080]
- [Added the `wasm32-unknown-wasi` target.][59464]


Libraries
---------
- [`Thread` will now show its ID in `Debug` output.][59460]
- [`StdinLock`, `StdoutLock`, and `StderrLock` now implement `AsRawFd`.][59512]
- [`alloc::System` now implements `Default`.][59451]
- [Expanded `Debug` output (`{:#?}`) for structs now has a trailing comma on the
  last field.][59076]
- [`char::{ToLowercase, ToUppercase}` now
  implement `ExactSizeIterator`.][58778]
- [All `NonZero` numeric types now implement `FromStr`.][58717]
- [Removed the `Read` trait bounds
  on the `BufReader::{get_ref, get_mut, into_inner}` methods.][58423]
- [You can now call the `dbg!` macro without any parameters to print the file
  and line where it is called.][57847]
- [In place ASCII case conversions are now up to 4× faster.][59283]
  e.g. `str::make_ascii_lowercase`
- [`hash_map::{OccupiedEntry, VacantEntry}` now implement `Sync`
  and `Send`.][58369]

Stabilized APIs
---------------
- [`f32::copysign`]
- [`f64::copysign`]
- [`RefCell::replace_with`]
- [`RefCell::map_split`]
- [`ptr::hash`]
- [`Range::contains`]
- [`RangeFrom::contains`]
- [`RangeTo::contains`]
- [`RangeInclusive::contains`]
- [`RangeToInclusive::contains`]
- [`Option::copied`]

Cargo
-----
- [You can now set `cargo:rustc-cdylib-link-arg` at build time to pass custom
  linker arguments when building a `cdylib`.][cargo/6298] Its usage is highly
  platform specific.

Misc
----
- [The Rust toolchain is now available natively for musl based distros.][58575]

[59460]: rust-lang/rust#59460
[59464]: rust-lang/rust#59464
[59500]: rust-lang/rust#59500
[59512]: rust-lang/rust#59512
[59580]: rust-lang/rust#59580
[59283]: rust-lang/rust#59283
[59451]: rust-lang/rust#59451
[59076]: rust-lang/rust#59076
[58778]: rust-lang/rust#58778
[58717]: rust-lang/rust#58717
[58369]: rust-lang/rust#58369
[58423]: rust-lang/rust#58423
[58080]: rust-lang/rust#58080
[57847]: rust-lang/rust#57847
[58575]: rust-lang/rust#58575
[cargo/6298]: rust-lang/cargo#6298
[`f32::copysign`]: https://doc.rust-lang.org/stable/std/primitive.f32.html#method.copysign
[`f64::copysign`]: https://doc.rust-lang.org/stable/std/primitive.f64.html#method.copysign
[`RefCell::replace_with`]: https://doc.rust-lang.org/stable/std/cell/struct.RefCell.html#method.replace_with
[`RefCell::map_split`]: https://doc.rust-lang.org/stable/std/cell/struct.RefCell.html#method.map_split
[`ptr::hash`]: https://doc.rust-lang.org/stable/std/ptr/fn.hash.html
[`Range::contains`]: https://doc.rust-lang.org/std/ops/struct.Range.html#method.contains
[`RangeFrom::contains`]: https://doc.rust-lang.org/std/ops/struct.RangeFrom.html#method.contains
[`RangeTo::contains`]: https://doc.rust-lang.org/std/ops/struct.RangeTo.html#method.contains
[`RangeInclusive::contains`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html#method.contains
[`RangeToInclusive::contains`]: https://doc.rust-lang.org/std/ops/struct.RangeToInclusive.html#method.contains
[`Option::copied`]: https://doc.rust-lang.org/std/option/enum.Option.html#method.copied
@ehuss ehuss added this to the 1.35.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge FCP with intent to merge finished-final-comment-period FCP complete relnotes Release-note worthy S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-cargo Team: Cargo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants