@@ -5,11 +5,13 @@
55# script that sets up the right environment variables so that the
66# compiler and the linker just "work".
77
8−{ name ? "", stdenv, nativeTools, nativeLibc, nativePrefix ? ""
8+{ name ? "", stdenv, nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
99, cc ? null, libc ? null, binutils ? null, coreutils ? null, shell ? stdenv.shell
1010, zlib ? null, extraPackages ? [], extraBuildCommands ? ""
1111, dyld ? null # TODO: should this be a setup-hook on dyld?
1212, isGNU ? false, isClang ? cc.isClang or false, gnugrep ? null
13+, hostPlatform, targetPlatform
14+, runCommand ? null
1315}:
1416
1517with stdenv.lib;
@@ -17,32 +19,113 @@ with stdenv.lib;
1719assert nativeTools -> nativePrefix != "";
1820assert !nativeTools ->
1921 cc != null && binutils != null && coreutils != null && gnugrep != null;
20−assert !nativeLibc -> libc != null;
22+assert !(nativeLibc && noLibc);
23+assert (noLibc || nativeLibc) == (libc == null);
24+
25+assert targetPlatform != hostPlatform -> runCommand != null;
2126
2227# For ghdl (the vhdl language provider to gcc) we need zlib in the wrapper.
2328assert cc.langVhdl or false -> zlib != null;
2429
2530let
31+ # Prefix for binaries. Customarily ends with a dash separator.
32+ #
33+ # TODO(@Ericson2314) Make unconditional, or optional but always true by
34+ # default.
35+ prefix = stdenv.lib.optionalString (targetPlatform != hostPlatform)
36+ (targetPlatform.config + "-");
2637
2738 ccVersion = (builtins.parseDrvName cc.name).version;
2839 ccName = (builtins.parseDrvName cc.name).name;
2940
30− libc_bin = if nativeLibc then null else getBin libc;
31− libc_dev = if nativeLibc then null else getDev libc;
32− libc_lib = if nativeLibc then null else getLib libc;
41+ libc_bin = if libc == null then null else getBin libc;
42+ libc_dev = if libc == null then null else getDev libc;
43+ libc_lib = if libc == null then null else getLib libc;
3344 cc_solib = getLib cc;
3445 binutils_bin = if nativeTools then "" else getBin binutils;
3546 # The wrapper scripts use 'cat' and 'grep', so we may need coreutils.
3647 coreutils_bin = if nativeTools then "" else getBin coreutils;
3748
38− default_cxx_stdlib_compile=optionalString (stdenv.isLinux && !(cc.isGNU or false))
49+ default_cxx_stdlib_compile=optionalString (targetPlatform.isLinux && !(cc.isGNU or false))
3950 "-isystem $(echo -n ${cc.gcc}/include/c++/*) -isystem $(echo -n ${cc.gcc}/include/c++/*)/$(${cc.gcc}/bin/gcc -dumpmachine)";
51+
52+ dashlessTarget = stdenv.lib.replaceStrings ["-"] ["_"] targetPlatform.config;
53+ # TODO(@Ericson2314) Make unconditional
54+ infixSalt = stdenv.lib.optionalString (targetPlatform != hostPlatform) dashlessTarget;
55+ infixSalt_ = stdenv.lib.optionalString (targetPlatform != hostPlatform) (dashlessTarget + "_");
56+ _infixSalt = stdenv.lib.optionalString (targetPlatform != hostPlatform) ("_" + dashlessTarget);
57+
58+ # We want to prefix all NIX_ flags with the target triple
59+ preWrap = textFile:
60+ # TODO: Do even when not cross on next mass-rebuild
61+ # TODO: use @target_tripple@ for consistency
62+ if targetPlatform == hostPlatform
63+ then textFile
64+ else runCommand "sed-nix-env-vars" {} (''
65+ cp --no-preserve=mode ${textFile} $out
66+
67+ sed -i $out \
68+ -e 's^NIX_^NIX_${infixSalt_}^g' \
69+ -e 's^addCVars^addCVars${_infixSalt}^g' \
70+ -e 's^\[ -z "\$crossConfig" \]^\[\[ "${builtins.toString (targetPlatform != hostPlatform)}" || -z "$crossConfig" \]\]^g'
71+
72+ '' + stdenv.lib.optionalString (textFile == ./setup-hook.sh) ''
73+ cat << 'EOF' >> $out
74+ for CMD in ar as nm objcopy ranlib strip strings size ld
75+ do
76+ # which is not part of stdenv, but compgen will do for now
77+ if
78+ PATH=$_PATH type -p ${prefix}$CMD > /dev/null
79+ then
80+ export ''$(echo "$CMD" | tr "[:lower:]" "[:upper:]")=${prefix}''${CMD};
81+ fi
82+ done
83+ EOF
84+
85+ sed -i $out -e 's_envHooks_crossEnvHooks_g'
86+ '' + ''
87+
88+ # NIX_ things which we don't both use and define, we revert them
89+ #asymmetric=$(
90+ # for pre in "" "\\$"
91+ # do
92+ # grep -E -ho $pre'NIX_[a-zA-Z_]*' ./* | sed 's/\$//' | sort | uniq
93+ # done | sort | uniq -c | sort -nr | sed -n 's/^1 NIX_//gp')
94+
95+ # hard-code for now
96+ asymmetric=("CXXSTDLIB_COMPILE" "CC")
97+
98+ # The ([^a-zA-Z_]|$) bussiness is to ensure environment variables that
99+ # begin with `NIX_CC` don't also get blacklisted.
100+ for var in "''${asymmetric[@]}"
101+ do
102+ sed -i $out -E -e "s~NIX_${infixSalt_}$var([^a-zA-Z_]|$)~NIX_$var\1~g"
103+ done
104+ '');
105+
106+ # The dynamic linker has different names on different platforms.
107+ dynamicLinker =
108+ if !nativeLibc then
109+ (if targetPlatform.system == "i686-linux" then "ld-linux.so.2" else
110+ if targetPlatform.system == "x86_64-linux" then "ld-linux-x86-64.so.2" else
111+ # ARM with a wildcard, which can be "" or "-armhf".
112+ if targetPlatform.isArm32 then "ld-linux*.so.3" else
113+ if targetPlatform.system == "aarch64-linux" then "ld-linux-aarch64.so.1" else
114+ if targetPlatform.system == "powerpc-linux" then "ld.so.1" else
115+ if targetPlatform.system == "mips64el-linux" then "ld.so.1" else
116+ if targetPlatform.system == "x86_64-darwin" then "/usr/lib/dyld" else
117+ if stdenv.lib.hasSuffix "pc-gnu" targetPlatform.config then "ld.so.1" else
118+ builtins.trace
119+ "Don't know the name of the dynamic linker for platform ${targetPlatform.config}, so guessing instead."
120+ null)
121+ else "";
122+
40123in
41124
42125stdenv.mkDerivation {
43− name =
44− (if name != "" then name else ccName + "-wrapper") +
45− (if cc != null && ccVersion != "" then "-" + ccVersion else "");
126+ name = prefix
127+ + (if name != "" then name else "${ccName}-wrapper")
128+ + (stdenv.lib.optionalString (cc != null && ccVersion != "") "-${ccVersion}");
46129
47130 preferLocalBuild = true;
48131
@@ -51,17 +134,18 @@ stdenv.mkDerivation {
51134
52135
53136 passthru = {
54− inherit libc nativeTools nativeLibc nativePrefix isGNU isClang default_cxx_stdlib_compile;
137+ inherit libc nativeTools nativeLibc nativePrefix isGNU isClang default_cxx_stdlib_compile
138+ prefix infixSalt infixSalt_ _infixSalt;
55139
56140 emacsBufferSetup = pkgs: ''
57141 ; We should handle propagation here too
58142 (mapc (lambda (arg)
59143 (when (file-directory-p (concat arg "/include"))
60− (setenv "NIX_CFLAGS_COMPILE" (concat (getenv "NIX_CFLAGS_COMPILE") " -isystem " arg "/include")))
144+ (setenv "NIX_${infixSalt_}CFLAGS_COMPILE" (concat (getenv "NIX_${infixSalt_}CFLAGS_COMPILE") " -isystem " arg "/include")))
61145 (when (file-directory-p (concat arg "/lib"))
62− (setenv "NIX_LDFLAGS" (concat (getenv "NIX_LDFLAGS") " -L" arg "/lib")))
146+ (setenv "NIX_${infixSalt_}LDFLAGS" (concat (getenv "NIX_${infixSalt_}LDFLAGS") " -L" arg "/lib")))
63147 (when (file-directory-p (concat arg "/lib64"))
64− (setenv "NIX_LDFLAGS" (concat (getenv "NIX_LDFLAGS") " -L" arg "/lib64")))) '(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
148+ (setenv "NIX_${infixSalt_}LDFLAGS" (concat (getenv "NIX_${infixSalt_}LDFLAGS") " -L" arg "/lib64")))) '(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
65149 '';
66150 };
67151
@@ -78,7 +162,12 @@ stdenv.mkDerivation {
78162 }
79163 ''
80164
81− + optionalString (!nativeLibc) (if (!stdenv.isDarwin) then ''
165+ # TODO(@Ericson2314): Unify logic next hash break
166+ + optionalString (libc != null) (if (targetPlatform.isDarwin) then ''
167+ echo $dynamicLinker > $out/nix-support/dynamic-linker
168+
169+ echo "export LD_DYLD_PATH=\"$dynamicLinker\"" >> $out/nix-support/setup-hook
170+ '' else if dynamicLinker != null then ''
82171 dynamicLinker="${libc_lib}/lib/$dynamicLinker"
83172 echo $dynamicLinker > $out/nix-support/dynamic-linker
84173
@@ -91,12 +180,24 @@ stdenv.mkDerivation {
91180 # (the *last* value counts, so ours should come first).
92181 echo "-dynamic-linker" $dynamicLinker > $out/nix-support/libc-ldflags-before
93182 '' else ''
94− echo $dynamicLinker > $out/nix-support/dynamic-linker
183+ dynamicLinker=`eval 'echo $libc/lib/ld*.so.?'`
184+ if [ -n "$dynamicLinker" ]; then
185+ echo $dynamicLinker > $out/nix-support/dynamic-linker
95186
96− echo "export LD_DYLD_PATH=\"$dynamicLinker\"" >> $out/nix-support/setup-hook
187+ if [ -e ${libc_lib}/lib/32/ld-linux.so.2 ]; then
188+ echo ${libc_lib}/lib/32/ld-linux.so.2 > $out/nix-support/dynamic-linker-m32
189+ fi
190+
191+ ldflagsBefore="-dynamic-linker $dlinker"
192+ fi
193+
194+ # The dynamic linker is passed in `ldflagsBefore' to allow
195+ # explicit overrides of the dynamic linker by callers to gcc/ld
196+ # (the *last* value counts, so ours should come first).
197+ echo "$ldflagsBefore" > $out/nix-support/libc-ldflags-before
97198 '')
98199
99− + optionalString (!nativeLibc) ''
200+ + optionalString (libc != null) ''
100201 # The "-B${libc_lib}/lib/" flag is a quick hack to force gcc to link
101202 # against the crt1.o from our own glibc, rather than the one in
102203 # /usr/lib. (This is only an issue when using an `impure'
@@ -117,7 +218,7 @@ stdenv.mkDerivation {
117218 ''
118219
119220 + (if nativeTools then ''
120− ccPath="${if stdenv.isDarwin then cc else nativePrefix}/bin"
221+ ccPath="${if targetPlatform.isDarwin then cc else nativePrefix}/bin"
121222 ldPath="${nativePrefix}/bin"
122223 '' else ''
123224 echo $cc > $out/nix-support/orig-cc
@@ -161,136 +262,114 @@ stdenv.mkDerivation {
161262 # Propagate the wrapped cc so that if you install the wrapper,
162263 # you get tools like gcov, the manpages, etc. as well (including
163264 # for binutils and Glibc).
164− echo ${cc} ${cc.man or ""} ${binutils_bin} ${libc_bin} > $out/nix-support/propagated-user-env-packages
265+ echo ${cc} ${cc.man or ""} ${binutils_bin} ${if libc == null then "" else libc_bin} > $out/nix-support/propagated-user-env-packages
165266
166267 echo ${toString extraPackages} > $out/nix-support/propagated-native-build-inputs
167268 ''
168269
169− + optionalString (stdenv.isSunOS && nativePrefix != "") ''
270+ + optionalString (targetPlatform.isSunOS && nativePrefix != "") ''
170271 # Solaris needs an additional ld wrapper.
171272 ldPath="${nativePrefix}/bin"
172− exec="$ldPath/ld"
173− wrap ld-solaris ${./ld-solaris-wrapper.sh}
273+ exec="$ldPath/${prefix}ld"
274+ wrap ld-solaris ${preWrap ./ld-solaris-wrapper.sh}
174275 '')
175276
176277 + ''
177278 # Create a symlink to as (the assembler). This is useful when a
178279 # cc-wrapper is installed in a user environment, as it ensures that
179280 # the right assembler is called.
180− if [ -e $ldPath/as ]; then
181− ln -s $ldPath/as $out/bin/as
281+ if [ -e $ldPath/${prefix}as ]; then
282+ ln -s $ldPath/${prefix}as $out/bin/${prefix}as
182283 fi
183284
184− wrap ld ${./ld-wrapper.sh} ''${ld:-$ldPath/ld}
285+ wrap ${prefix}ld ${preWrap ./ld-wrapper.sh} ''${ld:-$ldPath/${prefix}ld}
185286
186− if [ -e ${binutils_bin}/bin/ld.gold ]; then
187− wrap ld.gold ${./ld-wrapper.sh} ${binutils_bin}/bin/ld.gold
287+ if [ -e ${binutils_bin}/bin/${prefix}ld.gold ]; then
288+ wrap ${prefix}ld.gold ${preWrap ./ld-wrapper.sh} ${binutils_bin}/bin/${prefix}ld.gold
188289 fi
189290
190291 if [ -e ${binutils_bin}/bin/ld.bfd ]; then
191− wrap ld.bfd ${./ld-wrapper.sh} ${binutils_bin}/bin/ld.bfd
292+ wrap ${prefix}ld.bfd ${preWrap ./ld-wrapper.sh} ${binutils_bin}/bin/${prefix}ld.bfd
192293 fi
193294
194− export real_cc=cc
195− export real_cxx=c++
295+ export real_cc=${prefix}cc
296+ export real_cxx=${prefix}c++
196297 export default_cxx_stdlib_compile="${default_cxx_stdlib_compile}"
197298
198− if [ -e $ccPath/gcc ]; then
199− wrap gcc ${./cc-wrapper.sh} $ccPath/gcc
200− ln -s gcc $out/bin/cc
201− export real_cc=gcc
202− export real_cxx=g++
299+ if [ -e $ccPath/${prefix}gcc ]; then
300+ wrap ${prefix}gcc ${preWrap ./cc-wrapper.sh} $ccPath/${prefix}gcc
301+ ln -s ${prefix}gcc $out/bin/${prefix}cc
302+ export real_cc=${prefix}gcc
303+ export real_cxx=${prefix}g++
203304 elif [ -e $ccPath/clang ]; then
204− wrap clang ${./cc-wrapper.sh} $ccPath/clang
205− ln -s clang $out/bin/cc
305+ wrap ${prefix}clang ${preWrap ./cc-wrapper.sh} $ccPath/clang
306+ ln -s ${prefix}clang $out/bin/${prefix}cc
206307 export real_cc=clang
207308 export real_cxx=clang++
208309 fi
209310
210− if [ -e $ccPath/g++ ]; then
211− wrap g++ ${./cc-wrapper.sh} $ccPath/g++
212− ln -s g++ $out/bin/c++
311+ if [ -e $ccPath/${prefix}g++ ]; then
312+ wrap ${prefix}g++ ${preWrap ./cc-wrapper.sh} $ccPath/${prefix}g++
313+ ln -s ${prefix}g++ $out/bin/${prefix}c++
213314 elif [ -e $ccPath/clang++ ]; then
214− wrap clang++ ${./cc-wrapper.sh} $ccPath/clang++
215− ln -s clang++ $out/bin/c++
315+ wrap ${prefix}clang++ ${preWrap ./cc-wrapper.sh} $ccPath/clang++
316+ ln -s ${prefix}clang++ $out/bin/${prefix}c++
216317 fi
217318
218319 if [ -e $ccPath/cpp ]; then
219− wrap cpp ${./cc-wrapper.sh} $ccPath/cpp
320+ wrap ${prefix}cpp ${preWrap ./cc-wrapper.sh} $ccPath/cpp
220321 fi
221322 ''
222323
223324 + optionalString cc.langFortran or false ''
224− wrap gfortran ${./cc-wrapper.sh} $ccPath/gfortran
225− ln -sv gfortran $out/bin/g77
226− ln -sv gfortran $out/bin/f77
325+ wrap ${prefix}gfortran ${preWrap ./cc-wrapper.sh} $ccPath/${prefix}gfortran
326+ ln -sv ${prefix}gfortran $out/bin/${prefix}g77
327+ ln -sv ${prefix}gfortran $out/bin/${prefix}f77
227328 ''
228329
229330 + optionalString cc.langJava or false ''
230− wrap gcj ${./cc-wrapper.sh} $ccPath/gcj
331+ wrap ${prefix}gcj ${preWrap ./cc-wrapper.sh} $ccPath/${prefix}gcj
231332 ''
232333
233334 + optionalString cc.langGo or false ''
234− wrap gccgo ${./cc-wrapper.sh} $ccPath/gccgo
335+ wrap ${prefix}gccgo ${preWrap ./cc-wrapper.sh} $ccPath/${prefix}gccgo
235336 ''
236337
237338 + optionalString cc.langAda or false ''
238− wrap gnatgcc ${./cc-wrapper.sh} $ccPath/gnatgcc
239− wrap gnatmake ${./gnat-wrapper.sh} $ccPath/gnatmake
240− wrap gnatbind ${./gnat-wrapper.sh} $ccPath/gnatbind
241− wrap gnatlink ${./gnatlink-wrapper.sh} $ccPath/gnatlink
339+ wrap ${prefix}gnatgcc ${preWrap ./cc-wrapper.sh} $ccPath/${prefix}gnatgcc
340+ wrap ${prefix}gnatmake ${preWrap ./gnat-wrapper.sh} $ccPath/${prefix}gnatmake
341+ wrap ${prefix}gnatbind ${preWrap ./gnat-wrapper.sh} $ccPath/${prefix}gnatbind
342+ wrap ${prefix}gnatlink ${preWrap ./gnatlink-wrapper.sh} $ccPath/${prefix}gnatlink
242343 ''
243344
244345 + optionalString cc.langVhdl or false ''
245− ln -s $ccPath/ghdl $out/bin/ghdl
346+ ln -s $ccPath/${prefix}ghdl $out/bin/${prefix}ghdl
246347 ''
247348
248349 + ''
249− substituteAll ${./setup-hook.sh} $out/nix-support/setup-hook.tmp
350+ substituteAll ${preWrap ./setup-hook.sh} $out/nix-support/setup-hook.tmp
250351 cat $out/nix-support/setup-hook.tmp >> $out/nix-support/setup-hook
251352 rm $out/nix-support/setup-hook.tmp
252353
253354 # some linkers on some platforms don't support specific -z flags
254355 hardening_unsupported_flags=""
255− if [[ "$($ldPath/ld -z now 2>&1 || true)" =~ un(recognized|known)\ option ]]; then
356+ if [[ "$($ldPath/${prefix}ld -z now 2>&1 || true)" =~ un(recognized|known)\ option ]]; then
256357 hardening_unsupported_flags+=" bindnow"
257358 fi
258− if [[ "$($ldPath/ld -z relro 2>&1 || true)" =~ un(recognized|known)\ option ]]; then
359+ if [[ "$($ldPath/${prefix}ld -z relro 2>&1 || true)" =~ un(recognized|known)\ option ]]; then
259360 hardening_unsupported_flags+=" relro"
260361 fi
261362
262− substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
263− substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh
264− cp -p ${./utils.sh} $out/nix-support/utils.sh
363+ substituteAll ${preWrap ./add-flags.sh} $out/nix-support/add-flags.sh
364+ substituteAll ${preWrap ./add-hardening.sh} $out/nix-support/add-hardening.sh
365+ cp -p ${preWrap ./utils.sh} $out/nix-support/utils.sh
265366 ''
266367 + extraBuildCommands;
267368
268− # The dynamic linker has different names on different Linux platforms.
269− dynamicLinker =
270− if !nativeLibc then
271− (if stdenv.system == "i686-linux" then "ld-linux.so.2" else
272− if stdenv.system == "x86_64-linux" then "ld-linux-x86-64.so.2" else
273− # ARM with a wildcard, which can be "" or "-armhf".
274− if stdenv.isArm then "ld-linux*.so.3" else
275− if stdenv.system == "aarch64-linux" then "ld-linux-aarch64.so.1" else
276− if stdenv.system == "powerpc-linux" then "ld.so.1" else
277− if stdenv.system == "mips64el-linux" then "ld.so.1" else
278− if stdenv.system == "x86_64-darwin" then "/usr/lib/dyld" else
279− abort "Don't know the name of the dynamic linker for this platform.")
280− else "";
369+ inherit dynamicLinker;
281370
282371 crossAttrs = {
283372 shell = shell.crossDrv + shell.crossDrv.shellPath;
284− libc = stdenv.ccCross.libc;
285− #
286− # This is not the best way to do this. I think the reference should be
287− # the style in the gcc-cross-wrapper, but to keep a stable stdenv now I
288− # do this sufficient if/else.
289− dynamicLinker =
290− (if stdenv.cross.arch == "arm" then "ld-linux.so.3" else
291− if stdenv.cross.arch == "mips" then "ld.so.1" else
292− if stdenv.lib.hasSuffix "pc-gnu" stdenv.cross.config then "ld.so.1" else
293− abort "don't know the name of the dynamic linker for this platform");
294373 };
295374
296375 meta =