Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 stdenv,
4 pkgsBuildHost,
5 pkgsBuildTarget,
6 pkgsTargetTarget,
7}:
8
9rec {
10 # These environment variables must be set when using `cargo-c` and
11 # several other tools which do not deal well with cross
12 # compilation. The symptom of the problem they fix is errors due
13 # to buildPlatform CFLAGS being passed to the
14 # hostPlatform-targeted compiler -- for example, `-m64` being
15 # passed on a build=x86_64/host=aarch64 compilation.
16 envVars =
17 let
18
19 ccForBuild = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}cc";
20 cxxForBuild = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}c++";
21
22 ccForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
23 cxxForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
24
25 # Unfortunately we must use the dangerous `pkgsTargetTarget` here
26 # because hooks are artificially phase-shifted one slot earlier
27 # (they go in nativeBuildInputs, so the hostPlatform looks like
28 # a targetPlatform to them).
29 ccForTarget = "${pkgsTargetTarget.stdenv.cc}/bin/${pkgsTargetTarget.stdenv.cc.targetPrefix}cc";
30 cxxForTarget = "${pkgsTargetTarget.stdenv.cc}/bin/${pkgsTargetTarget.stdenv.cc.targetPrefix}c++";
31
32 rustBuildPlatform = stdenv.buildPlatform.rust.rustcTarget;
33 rustBuildPlatformSpec = stdenv.buildPlatform.rust.rustcTargetSpec;
34 rustHostPlatform = stdenv.hostPlatform.rust.rustcTarget;
35 rustHostPlatformSpec = stdenv.hostPlatform.rust.rustcTargetSpec;
36 rustTargetPlatform = stdenv.targetPlatform.rust.rustcTarget;
37 rustTargetPlatformSpec = stdenv.targetPlatform.rust.rustcTargetSpec;
38 in
39 {
40 inherit
41 ccForBuild
42 cxxForBuild
43 rustBuildPlatform
44 rustBuildPlatformSpec
45 ccForHost
46 cxxForHost
47 rustHostPlatform
48 rustHostPlatformSpec
49 ccForTarget
50 cxxForTarget
51 rustTargetPlatform
52 rustTargetPlatformSpec
53 ;
54
55 # Prefix this onto a command invocation in order to set the
56 # variables needed by cargo.
57 #
58 setEnv = ''
59 env \
60 "CC_${stdenv.buildPlatform.rust.cargoEnvVarTarget}=${ccForBuild}" \
61 "CXX_${stdenv.buildPlatform.rust.cargoEnvVarTarget}=${cxxForBuild}" \
62 "CARGO_TARGET_${stdenv.buildPlatform.rust.cargoEnvVarTarget}_LINKER=${ccForBuild}" \
63 "CARGO_BUILD_TARGET=${rustBuildPlatform}" \
64 "HOST_CC=${pkgsBuildHost.stdenv.cc}/bin/cc" \
65 "HOST_CXX=${pkgsBuildHost.stdenv.cc}/bin/c++" \
66 ''
67 + ''
68 "CC_${stdenv.hostPlatform.rust.cargoEnvVarTarget}=${ccForHost}" \
69 "CXX_${stdenv.hostPlatform.rust.cargoEnvVarTarget}=${cxxForHost}" \
70 "CARGO_TARGET_${stdenv.hostPlatform.rust.cargoEnvVarTarget}_LINKER=${ccForHost}" \
71 ''
72 # Due to a bug in how splicing and pkgsTargetTarget works, in
73 # situations where pkgsTargetTarget is irrelevant
74 # pkgsTargetTarget.stdenv.cc is often simply wrong. We must omit
75 # the following lines when rustTargetPlatform collides with
76 # rustHostPlatform.
77 + lib.optionalString (rustTargetPlatform != rustHostPlatform) ''
78 "CC_${stdenv.targetPlatform.rust.cargoEnvVarTarget}=${ccForTarget}" \
79 "CXX_${stdenv.targetPlatform.rust.cargoEnvVarTarget}=${cxxForTarget}" \
80 "CARGO_TARGET_${stdenv.targetPlatform.rust.cargoEnvVarTarget}_LINKER=${ccForTarget}" \
81 '';
82 };
83}
84//
85 lib.mapAttrs
86 (
87 old: new: platform:
88 lib.warn
89 "`rust.${old} platform` is deprecated. Use `platform.rust.${lib.showAttrPath new}` instead."
90 lib.getAttrFromPath
91 new
92 platform.rust
93 )
94 {
95 toTargetArch = [
96 "platform"
97 "arch"
98 ];
99 toTargetOs = [
100 "platform"
101 "os"
102 ];
103 toTargetFamily = [
104 "platform"
105 "target-family"
106 ];
107 toTargetVendor = [
108 "platform"
109 "vendor"
110 ];
111 toRustTarget = [ "rustcTarget" ];
112 toRustTargetSpec = [ "rustcTargetSpec" ];
113 toRustTargetSpecShort = [ "cargoShortTarget" ];
114 toRustTargetForUseInEnvVars = [ "cargoEnvVarTarget" ];
115 IsNoStdTarget = [ "isNoStdTarget" ];
116 }