lol
1{ lib
2, buildPlatform
3, hostPlatform
4, fetchurl
5, bash
6, tinycc
7, gnumake
8, gnupatch
9, gnused
10, gnugrep
11, gnutar
12, gzip
13}:
14
15let
16 inherit (import ./common.nix { inherit lib; }) pname meta;
17 version = "1.1.24";
18
19 src = fetchurl {
20 url = "https://musl.libc.org/releases/musl-${version}.tar.gz";
21 hash = "sha256-E3DJqBKyzyp9koAlEMygBYzDfmanvt1wBR8KNAFQIqM=";
22 };
23
24 # Thanks to the live-bootstrap project!
25 # See https://github.com/fosslinux/live-bootstrap/blob/d98f97e21413efc32c770d0356f1feda66025686/sysa/musl-1.1.24/musl-1.1.24.sh
26 liveBootstrap = "https://github.com/fosslinux/live-bootstrap/raw/d98f97e21413efc32c770d0356f1feda66025686/sysa/musl-1.1.24";
27 patches = [
28 (fetchurl {
29 url = "${liveBootstrap}/patches/avoid_set_thread_area.patch";
30 hash = "sha256-TsbBZXk4/KMZG9EKi7cF+sullVXrxlizLNH0UHGXsPs=";
31 })
32 (fetchurl {
33 url = "${liveBootstrap}/patches/avoid_sys_clone.patch";
34 hash = "sha256-/ZmH64J57MmbxdfQ4RNjamAiBdkImMTlHsHdgV4gMj4=";
35 })
36 (fetchurl {
37 url = "${liveBootstrap}/patches/fenv.patch";
38 hash = "sha256-vMVGjoN4deAJW5gsSqA207SJqAbvhrnOsGK49DdEiTI=";
39 })
40 (fetchurl {
41 url = "${liveBootstrap}/patches/makefile.patch";
42 hash = "sha256-03iYBAUnsrEdLIIhhhq5mM6BGnPn2EfUmIHu51opxbw=";
43 })
44 (fetchurl {
45 url = "${liveBootstrap}/patches/musl_weak_symbols.patch";
46 hash = "sha256-/d9a2eUkpe9uyi1ye6T4CiYc9MR3FZ9na0Gb90+g4v0=";
47 })
48 (fetchurl {
49 url = "${liveBootstrap}/patches/set_thread_area.patch";
50 hash = "sha256-RIZYqbbRSx4X/0iFUhriwwBRmoXVR295GNBUjf2UrM0=";
51 })
52 (fetchurl {
53 url = "${liveBootstrap}/patches/sigsetjmp.patch";
54 hash = "sha256-wd2Aev1zPJXy3q933aiup5p1IMKzVJBquAyl3gbK4PU=";
55 })
56 # FIXME: this patch causes the build to fail
57 # (fetchurl {
58 # url = "${liveBootstrap}/patches/stdio_flush_on_exit.patch";
59 # hash = "sha256-/z5ze3h3QTysay8nRvyvwPv3pmTcKptdkBIaMCoeLDg=";
60 # })
61 # HACK: always flush stdio immediately
62 ./always-flush.patch
63 (fetchurl {
64 url = "${liveBootstrap}/patches/va_list.patch";
65 hash = "sha256-UmcMIl+YCi3wIeVvjbsCyqFlkyYsM4ECNwTfXP+s7vg=";
66 })
67 ];
68in
69bash.runCommand "${pname}-${version}" {
70 inherit pname version meta;
71
72 nativeBuildInputs = [
73 tinycc.compiler
74 gnumake
75 gnupatch
76 gnused
77 gnugrep
78 gnutar
79 gzip
80 ];
81} ''
82 # Unpack
83 tar xzf ${src}
84 cd musl-${version}
85
86 # Patch
87 ${lib.concatMapStringsSep "\n" (f: "patch -Np0 -i ${f}") patches}
88 # tcc does not support complex types
89 rm -rf src/complex
90 # Configure fails without this
91 mkdir -p /dev
92 # https://github.com/ZilchOS/bootstrap-from-tcc/blob/2e0c68c36b3437386f786d619bc9a16177f2e149/using-nix/2a3-intermediate-musl.nix
93 sed -i 's|/bin/sh|${bash}/bin/bash|' \
94 tools/*.sh
95 chmod 755 tools/*.sh
96 # patch popen/system to search in PATH instead of hardcoding /bin/sh
97 sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \
98 src/stdio/popen.c src/process/system.c
99 sed -i 's|execl("/bin/sh", "sh", "-c",|execlp("sh", "-c",|'\
100 src/misc/wordexp.c
101
102 # Configure
103 bash ./configure \
104 --prefix=$out \
105 --build=${buildPlatform.config} \
106 --host=${hostPlatform.config} \
107 --disable-shared \
108 CC=tcc
109
110 # Build
111 make AR="tcc -ar" RANLIB=true CFLAGS="-DSYSCALL_NO_TLS"
112
113 # Install
114 make install
115 cp ${tinycc.libs}/lib/libtcc1.a $out/lib
116''