Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 stdenv,
4 bashNonInteractive,
5 binutils,
6 bootBinutils,
7 bootGCC,
8 buildPackages,
9 busyboxMinimal,
10 bzip2,
11 coreutilsMinimal,
12 diffutils,
13 findutils,
14 gawk,
15 gmpxx,
16 gnugrep,
17 gnumake,
18 gnused,
19 gzip,
20 libc,
21 libmpc,
22 mpfr,
23 patch,
24 patchelf,
25 runCommand,
26 tarMinimal,
27 zlib,
28}:
29let
30 # ${libc.src}/sysdeps/unix/sysv/linux/loongarch/lp64/libnsl.abilist does not exist!
31 withLibnsl = !stdenv.hostPlatform.isLoongArch64;
32in
33stdenv.mkDerivation (finalAttrs: {
34 name = "stdenv-bootstrap-tools";
35
36 meta = {
37 # Increase priority to unblock nixpkgs-unstable
38 # https://github.com/NixOS/nixpkgs/pull/104679#issuecomment-732267288
39 schedulingPriority = 200;
40 };
41
42 nativeBuildInputs = [
43 buildPackages.nukeReferences
44 buildPackages.cpio
45 ];
46
47 buildCommand = ''
48 set -x
49 mkdir -p $out/bin $out/lib $out/libexec
50
51 ''
52 + (
53 if (stdenv.hostPlatform.libc == "glibc") then
54 ''
55 # Copy what we need of Glibc.
56 cp -d ${libc.out}/lib/ld*.so* $out/lib
57 cp -d ${libc.out}/lib/libc*.so* $out/lib
58 cp -d ${libc.out}/lib/libc_nonshared.a $out/lib
59 cp -d ${libc.out}/lib/libm*.so* $out/lib
60 cp -d ${libc.out}/lib/libdl*.so* $out/lib
61 cp -d ${libc.out}/lib/librt*.so* $out/lib
62 cp -d ${libc.out}/lib/libpthread*.so* $out/lib
63 ''
64 + lib.optionalString withLibnsl ''
65 cp -d ${libc.out}/lib/libnsl*.so* $out/lib
66 ''
67 + ''
68 cp -d ${libc.out}/lib/libnss*.so* $out/lib
69 cp -d ${libc.out}/lib/libresolv*.so* $out/lib
70 # Copy all runtime files to enable non-PIE, PIE, static PIE and profile-generated builds
71 cp -d ${libc.out}/lib/*.o $out/lib
72
73 # Hacky compat with our current unpack-bootstrap-tools.sh
74 ln -s librt.so "$out"/lib/librt-dummy.so
75
76 cp -rL ${libc.dev}/include $out
77 chmod -R u+w "$out"
78
79 # libc can contain linker scripts: find them, copy their deps,
80 # and get rid of absolute paths (nuke-refs would make them useless)
81 local lScripts=$(grep --files-with-matches --max-count=1 'GNU ld script' -R "$out/lib")
82 cp -d -t "$out/lib/" $(cat $lScripts | tr " " "\n" | grep -F '${libc.out}' | sort -u)
83 for f in $lScripts; do
84 substituteInPlace "$f" --replace '${libc.out}/lib/' ""
85 done
86
87 # Hopefully we won't need these.
88 rm -rf $out/include/mtd $out/include/rdma $out/include/sound $out/include/video
89 find $out/include -name .install -exec rm {} \;
90 find $out/include -name ..install.cmd -exec rm {} \;
91 mv $out/include $out/include-glibc
92 ''
93 else if (stdenv.hostPlatform.libc == "musl") then
94 ''
95 # Copy what we need from musl
96 cp ${libc.out}/lib/* $out/lib
97 cp -rL ${libc.dev}/include $out
98 chmod -R u+w "$out"
99
100 rm -rf $out/include/mtd $out/include/rdma $out/include/sound $out/include/video
101 find $out/include -name .install -exec rm {} \;
102 find $out/include -name ..install.cmd -exec rm {} \;
103 mv $out/include $out/include-libc
104 ''
105 else
106 throw "unsupported libc for bootstrap tools"
107 )
108 + ''
109 # Copy coreutils, bash, etc.
110 cp -d ${coreutilsMinimal.out}/bin/* $out/bin
111 (cd $out/bin && rm vdir dir sha*sum pinky factor pathchk runcon shuf who whoami shred users)
112
113 cp ${bashNonInteractive.out}/bin/bash $out/bin
114 cp ${findutils.out}/bin/find $out/bin
115 cp ${findutils.out}/bin/xargs $out/bin
116 cp -d ${diffutils.out}/bin/* $out/bin
117 cp -d ${gnused.out}/bin/* $out/bin
118 cp -d ${gnugrep.out}/bin/grep $out/bin
119 cp ${gawk.out}/bin/gawk $out/bin
120 cp -d ${gawk.out}/bin/awk $out/bin
121 cp ${tarMinimal.out}/bin/tar $out/bin
122 cp ${gzip.out}/bin/.gzip-wrapped $out/bin/gzip
123 cp ${bzip2.bin}/bin/bzip2 $out/bin
124 cp -d ${gnumake.out}/bin/* $out/bin
125 cp -d ${patch}/bin/* $out/bin
126 cp ${patchelf}/bin/* $out/bin
127
128 cp -d ${gnugrep.pcre2.out}/lib/libpcre2*.so* $out/lib # needed by grep
129
130 # Copy what we need of GCC.
131 cp -d ${bootGCC.out}/bin/gcc $out/bin
132 cp -d ${bootGCC.out}/bin/cpp $out/bin
133 cp -d ${bootGCC.out}/bin/g++ $out/bin
134 cp ${bootGCC.lib}/lib/libgcc_s.so* $out/lib
135 cp -d ${bootGCC.lib}/lib/libstdc++.so* $out/lib
136 cp -d ${bootGCC.out}/lib/libssp.a* $out/lib
137 cp -d ${bootGCC.out}/lib/libssp_nonshared.a $out/lib
138 cp -rd ${bootGCC.out}/lib/gcc $out/lib
139 chmod -R u+w $out/lib
140 rm -f $out/lib/gcc/*/*/include*/linux
141 rm -f $out/lib/gcc/*/*/include*/sound
142 rm -rf $out/lib/gcc/*/*/include*/root
143 rm -f $out/lib/gcc/*/*/include-fixed/asm
144 rm -rf $out/lib/gcc/*/*/plugin
145 #rm -f $out/lib/gcc/*/*/*.a
146 cp -rd ${bootGCC.out}/libexec/* $out/libexec
147 chmod -R u+w $out/libexec
148 rm -rf $out/libexec/gcc/*/*/plugin
149 mkdir -p $out/include
150 cp -rd ${bootGCC.out}/include/c++ $out/include
151 chmod -R u+w $out/include
152 rm -rf $out/include/c++/*/ext/pb_ds
153 rm -rf $out/include/c++/*/ext/parallel
154
155 cp -d ${gmpxx.out}/lib/libgmp*.so* $out/lib
156 cp -d ${mpfr.out}/lib/libmpfr*.so* $out/lib
157 cp -d ${libmpc.out}/lib/libmpc*.so* $out/lib
158 cp -d ${zlib.out}/lib/libz.so* $out/lib
159
160 ''
161 + lib.optionalString (stdenv.hostPlatform.isRiscV) ''
162 # libatomic is required on RiscV platform for C/C++ atomics and pthread
163 # even though they may be translated into native instructions.
164 cp -d ${bootGCC.out}/lib/libatomic.a* $out/lib
165
166 ''
167 + ''
168 cp -d ${bzip2.out}/lib/libbz2.so* $out/lib
169
170 # Copy binutils.
171 for i in as ld ar ranlib nm strip readelf objdump; do
172 cp ${bootBinutils.out}/bin/$i $out/bin
173 done
174 cp -r '${lib.getLib binutils.bintools}'/lib/* "$out/lib/"
175
176 chmod -R u+w $out
177
178 # Strip executables even further.
179 for i in $out/bin/* $out/libexec/gcc/*/*/*; do
180 if test -x $i -a ! -L $i; then
181 chmod +w $i
182 $STRIP -s $i || true
183 fi
184 done
185
186 nuke-refs $out/bin/*
187 nuke-refs $out/lib/*
188 nuke-refs $out/lib/*/*
189 nuke-refs $out/libexec/gcc/*/*/*
190 nuke-refs $out/lib/gcc/*/*/*
191 nuke-refs $out/lib/gcc/*/*/include-fixed/*{,/*}
192
193 mkdir $out/.pack
194 mv $out/* $out/.pack
195 mv $out/.pack $out/pack
196
197 mkdir $out/on-server
198 XZ_OPT="-9 -e" tar cvJf $out/on-server/bootstrap-tools.tar.xz --hard-dereference --sort=name --numeric-owner --owner=0 --group=0 --mtime=@1 -C $out/pack .
199 cp ${busyboxMinimal}/bin/busybox $out/on-server
200 chmod u+w $out/on-server/busybox
201 nuke-refs $out/on-server/busybox
202 ''; # */
203
204 # The result should not contain any references (store paths) so
205 # that we can safely copy them out of the store and to other
206 # locations in the store.
207 allowedReferences = [ ];
208
209 passthru = {
210 bootstrapFiles = {
211 # Make them their own store paths to test that busybox still works when the binary is named /nix/store/HASH-busybox
212 busybox = runCommand "busybox" { } "cp ${finalAttrs.finalPackage}/on-server/busybox $out";
213 bootstrapTools =
214 runCommand "bootstrap-tools.tar.xz" { }
215 "cp ${finalAttrs.finalPackage}/on-server/bootstrap-tools.tar.xz $out";
216 };
217 };
218})