Commit Graph

24 Commits

Author SHA1 Message Date
Yureka
b8076b893e rust: allow linker to be different from compiler 2024-01-15 22:34:02 +01:00
Alyssa Ross
e3e57b8f18 lib.systems: elaborate Rust metadata
We need this stuff to be available in lib so make-derivation.nix can
access it to construct the Meson cross file.

This has a couple of other advantages:

 - It makes Rust less special.  Now figuring out what Rust calls a
   platform is the same as figuring out what Linux or QEMU call it.

 - We can unify the schema used to define Rust targets, and the schema
   used to access those values later.  Just like you can set "config"
   or "system" in a platform definition, and then access those same
   keys on the elaborated platform, you can now set "rustcTarget" in
   your crossSystem, and then access "stdenv.hostPlatform.rustcTarget"
   in your code.

"rustcTarget", "rustcTargetSpec", "cargoShortTarget", and
"cargoEnvVarTarget" have the "rustc" and "cargo" prefixes because
these are not exposed to code by the compiler, and are not
standardized.  The arch/os/etc. variables are all named to match the
forms in the Rust target spec JSON.

The new rust.target-family only takes a list, since we don't need to
worry about backwards compatibility when that name is used.

The old APIs are all still functional with no warning for now, so that
it's possible for external code to use a single API on both 23.05 and
23.11.  We can introduce the warnings once 23.05 is EOL, and make them
hard errors when 23.11 is EOL.
2023-11-09 10:02:24 +01:00
Alyssa Ross
7262026f0f
rustPlatform.cargoSetupHook: fix platform check (#260068)
Cargo will never need to link for the target platform — that'd be for
the package being built to do at runtime.  Cargo should know about the
build and host linkers.

This fixes e.g. pkgsCross.musl64.fd from x86_64-linux.

Fixes: 67a4f828b4 ("rust: hooks: fix cross compilation")
2023-10-11 23:36:56 +02:00
Alyssa Ross
1cbe5c3e8b rust.toRustTargetForUseInEnvVars: support custom targets
> If using a target spec JSON file, the <triple> value is the filename
> stem. For example --target foo/bar.json would match [target.bar].

- https://doc.rust-lang.org/cargo/reference/config.html#target

I've also exposed toRustTargetSpecShort as a public function, because
it's useful to be able to know what the target subdirectory will be.
2023-10-03 12:30:04 +00:00
Adam Joseph
67a4f828b4 rust: hooks: fix cross compilation
Currently there is a state of severe confusion in
pkgs/build-support/rust/hooks/ regarding host vs target; right now
there is only "host" defined, but whether it means "host" or
"target" seems to fluctuate.

This commit corrects that, ensuring that all variables come in all
three flavors (build, host, target) and are used consistently with
the nixpkgs convention.

This also fixes the cross-compilation of packages which use
`maturinBuildHook` -- hooks go in `nativeBuildInputs` and are
phase-shifted backwards by one platform, so they need to be careful
about distinguishing between build and host.

Closes #247441
2023-09-26 06:30:44 +00:00
Alyssa Ross
470e6130b3
rust: fix overriding rust flags on musl
If RUSTFLAGS is set in the environment, Cargo will ignore rustflags
settings in its TOML configuration.  So setting RUSTFLAGS=-g (like
separateDebugInfo does) to generate debug info breaks
dynamically-linked Rust packages on musl.  This breakage is visible
for any packages that call into C dynamic libraries.  If the binary is
linked directly to a C dynamic library, it will fail to build, and if
it depends on a Rust library which links a C dynamic library, it will
segfault at runtime when it tries to call a function from the C
library.  I noticed this because pkgsMusl.crosvm is broken for this
reason, since it sets separateDebugInfo = true.

It shouldn't be possible to end up with broken binaries just by using
RUSTFLAGS to do something innocuous like enable debug info, so I think
that, even though we liked the approach of modiyfing .cargo/config
better at the time, it's become clear that it's too brittle, and we
should bite the bullet and patch the compiler instead when targetting
musl.  It does not appear to be necessary to modify the compiler at
all when cross-compiling /from/ dynamically-linked Musl to another
target, so I'm only checking whether the target system is
dynamically-linked Musl when deciding whether to make the modification
to the compiler.

This reverts commit c2eaaae50d
("cargoSetupHook: pass host config flags"), and implements the
compiler patching approach instead.
2023-03-16 02:29:46 +00:00
Weijia Wang
39a2b0b3bf rust: remove aarch64-linux workaround
This commit reverts #209113, since aarch64-linux now uses GCC 12 by default.
2023-03-01 18:42:07 +02:00
Artturin
4e3dcf364e treewide: makeSetupHook deps -> propagatedBuildInputs 2023-02-07 21:02:00 +02:00
Winter
e2b092fc52 Revert "rustPlatform.bindgenHook: use the same clang/libclang as rustc"
This reverts commit 46ee37ca1d, as it breaks
anything that uses libcxx on Darwin, as well as cross-compilation to at
least armv6l.

As there's no clear solution at this time, reverting it is the best
option, as this only reduces build time closure size (something we can
arguably live with).

https://github.com/NixOS/nixpkgs/pull/207352#issuecomment-1418363441
https://github.com/NixOS/nixpkgs/pull/207352#issuecomment-1420124250
2023-02-07 00:04:19 -05:00
github-actions[bot]
b60b0a223e
Merge master into staging-next 2023-01-08 06:01:06 +00:00
Nick Cao
46ee37ca1d rustPlatform.bindgenHook: use the same clang/libclang as rustc 2023-01-08 15:22:04 +10:00
Winter
8442601c64 rust: fix on aarch64-linux by using GCC 11 and passing -lgcc
This change switches to using GCC 11 by default on aarch64-linux, as well as passing `-lgcc` to the linker, per #201485.

See #201254 and #208412 for wider context on the issue.
2023-01-04 18:15:20 -05:00
figsoda
0643540f97 rustPlatform.cargoNextestHook: init 2022-11-28 16:59:52 -05:00
Artturin
341e6fd558 splice.nix: start deprecating nativeDrv and crossDrv 2022-11-19 00:04:54 +02:00
Yureka
c2eaaae50d cargoSetupHook: pass host config flags 2022-10-30 18:55:47 +01:00
Yureka
51c62063e3 cargoSetupHook: set crt-static
Tell rust if we want our binaries linked statically or dynamically.
Otherwise the compiler will always produce statically linked binaries for musl
targets, as this is the default.
2022-08-13 15:25:41 +02:00
Yureka
66ac47bdf6 cargoSetupHook: remove unneeded rustflags for aarch64+static cross
The linked issue was resolved upstream and the `-lgcc` is no longer required
since https://github.com/rust-lang/compiler-builtins/pull/377 was merged.
2022-08-13 15:24:14 +02:00
Guillaume Girol
bedabfbcef rustPlatform.bindgenHook: init 2022-02-22 19:37:07 +01:00
Daniël de Kok
11307c1d47 maturinBuildHook: add rustc to deps
maturin 0.10.5 uses rustc -vV to find the host:

e886c85f5a

We now need to make rustc visible to the hook for maturin to work
properly.
2021-05-21 07:51:27 +02:00
Daniël de Kok
05e40e79a8 buildRustPackage: factor out check phase to cargoCheckHook
API change:

`cargoParallelTestThreads` suggests that this attribute sets the
number of threads used during tests, while it is actually a boolean
option (use 1 thread or NIX_BUILD_CORES threads). In the hook, this
is replaced by a more canonical name `dontUseCargoParallelTests`.
2021-02-16 08:09:15 +01:00
Daniël de Kok
9757c7101a buildRustPackage: factor out install phase to cargoInstallHook 2021-02-15 12:17:18 +01:00
Daniël de Kok
160cf87086 rustPlatform.maturinBuildHook: init
This build hook can be used to build Python packages using maturin.
2021-02-12 08:40:43 +01:00
Daniël de Kok
a8efb2053f buildRustPackage: factor out build phase to cargoBuildHook
- API change: remove the `target` argument of `buildRustPackage`, the
  target should always be in sync with the C/C++ compiler that is used.

- Gathering of binaries has moved from `buildPhase` to `installPhase`,
  this simplifies the hook and orders this functionality logically
  with the installation logic.
2021-02-11 20:00:12 +01:00
Daniël de Kok
d083f412fa buildRustPackage: factor out setting up .cargo/config to cargoSetupHook
This makes it possible to reuse this functionality as a hook in
derivations that do not use buildRustPackage.
2021-02-10 07:01:24 +01:00