vinix-ports / gcc / PKGBUILD.vile
172 lines · 169 sloc · 10.44 KB · 1a60ff057a783fef576b44eee3f970207e9291e9
Raw
1; The build machine needs a working musl C++17 compiler. CXXFLAGS and LDFLAGS
2; may point at that compiler's C++ headers and runtime for this first stage.
3; A native bootstrap builds the target runtime with the new compiler. A
4; cross bootstrap needs an existing build-machine compiler for the target.
5host-arch := if memq(string->symbol("aarch64"), features()) { "aarch64" } else { "x86_64" }
6selected := get-environment-variable("VPAC_TARGET_ARCH")
7arch := if string?(selected) { selected } else { host-arch }
8triplet := string-append(arch, "-linux-musl")
9cross := not(string=?(arch, host-arch))
10libc := if string=?(arch, "aarch64") { "musl>=1.2.6-4" } else { "musl" }
11
12; Makefiles keep absolute paths. A worker retry can keep the prepared source
13; and objects in the same dedicated directory.
14checkpoint := get-environment-variable("VINIX_GCC_BUILD_DIR")
15persistent := if string?(checkpoint) { not(string=?("", checkpoint)) } else { #f }
16checkpoint-recipe := if persistent { string-append(checkpoint, "/recipe.vile") } else { "" }
17cached := if persistent { file-exists?(checkpoint-recipe) } else { #f }
18build-dir := fn() { if persistent { checkpoint } else { srcdir() } }
19local-archive := get-environment-variable("VINIX_GCC_LOCAL_SOURCE")
20source-archive := if equal?("1", local-archive) {
21 "gcc-14.2.0.tar.xz"
22} else {
23 "https://distfiles.alpinelinux.org/distfiles/v3.21/gcc-14.2.0.tar.xz"
24}
25
26itm-compiler := fn() {
27 if cross { "CXX_FOR_TARGET=vinix-c++" }
28 else {
29 build := build-dir()
30 cxx := string-append(build, "/", triplet, "/libstdc++-v3")
31 string-append("CXX_FOR_TARGET=", build, "/gcc/xg++ -B", build, "/gcc/ -nostdinc++",
32 " -isystem ", cxx, "/include/", triplet, " -isystem ", cxx, "/include",
33 " -isystem ", build, "/gcc-14.2.0/libstdc++-v3/libsupc++",
34 " -L", cxx, "/src/.libs -L", cxx, "/libsupc++/.libs")
35 }
36}
37
38defpkgbuild(gcc,
39 #:pkgname, "gcc",
40 #:pkgver, "14.2.0",
41 #:pkgrel, 2,
42 #:pkgdesc, "GNU C and C++ compilers, headers and development libraries for Vinix",
43 #:arch, [#:x86-64, #:aarch64],
44 #:url, "https://gcc.gnu.org/",
45 #:license, ["GPL-3.0-or-later", "GPL-3.0-or-later WITH GCC-exception-3.1"],
46 #:depends, list(libc, if string=?(arch, "aarch64") { "gcc-libs=14.2.0-2" } else { "gcc-libs=14.2.0-1" },
47 "gmp", "mpfr", "mpc", "isl25", "zlib", "binutils"),
48 #:provides, ["g++=14.2.0", "libstdc++-dev=14.2.0"],
49 #:makedepends, ["make", "linux-headers"],
50 #:source, if cached { list() } else { list(source-archive) },
51 #:sha256sums, if cached { list() } else { ["a7b39bc69cbf9e25826c5a60ab26477001f7c08d85cec04bc0e29cabed6f3cc9"] },
52 #:prepare, fn() {
53 if cached {
54 run("cmp", string-append(startdir(), "/PKGBUILD.vile"), checkpoint-recipe)
55 run("test", "-x", string-append(build-dir(), "/gcc-14.2.0/configure"))
56 } else {
57 ; Serialize the two memory-intensive instruction-table generators.
58 run("sed", "-i", "$a s-automata: s-attrtab", "gcc-14.2.0/gcc/Makefile.in")
59 ; Match the library directory used by Vinix's musl loader.
60 if string=?(arch, "x86_64") {
61 run("sed", "-i", "s|m64=../lib64|m64=../lib|", "gcc-14.2.0/gcc/config/i386/t-linux64")
62 } else {
63 run("sed", "-i", "s|mabi.lp64=../lib64|mabi.lp64=../lib|", "gcc-14.2.0/gcc/config/aarch64/t-aarch64-linux")
64 ; This default also applies when GCC drives an alternate GNU linker.
65 run("sed", "-i", "s|^#define LINK_SPEC LINUX_TARGET_LINK_SPEC|#define LINK_SPEC \"-z max-page-size=16384 -z common-page-size=16384 \" LINUX_TARGET_LINK_SPEC|",
66 "gcc-14.2.0/gcc/config/aarch64/aarch64-linux.h")
67 }
68 if persistent {
69 run("mkdir", checkpoint)
70 run("mv", "gcc-14.2.0", checkpoint)
71 run("cp", string-append(startdir(), "/PKGBUILD.vile"), checkpoint-recipe)
72 }
73 }
74 },
75 #:build, fn() {
76 root := get-environment-variable("VPAC_MAKEPKG_ROOT")
77 binutils := if cross { get-environment-variable("VINIX_GCC_BUILD_TOOLS") } else { string-append(root, "/usr/bin") }
78 run("test", "-x", string-append(binutils, "/as"))
79 run("test", "-x", string-append(binutils, "/ld"))
80 target-tools := if cross {
81 list("CC_FOR_BUILD=gcc", "CXX_FOR_BUILD=g++", "CFLAGS_FOR_BUILD=-O2 -g0", "CXXFLAGS_FOR_BUILD=-O2 -g0", "LDFLAGS_FOR_BUILD=",
82 "AR_FOR_BUILD=ar", "RANLIB_FOR_BUILD=ranlib",
83 string-append("AR=", binutils, "/ar"), string-append("AS=", binutils, "/as"),
84 string-append("LD=", binutils, "/ld"), string-append("NM=", binutils, "/nm"),
85 string-append("RANLIB=", binutils, "/ranlib"), string-append("STRIP=", binutils, "/strip"),
86 "CC_FOR_TARGET=vinix-cc", "GCC_FOR_TARGET=vinix-cc", "CXX_FOR_TARGET=vinix-c++", "RAW_CXX_FOR_TARGET=vinix-c++",
87 string-append("AR_FOR_TARGET=", binutils, "/ar"),
88 string-append("AS_FOR_TARGET=", binutils, "/as"), string-append("LD_FOR_TARGET=", binutils, "/ld"),
89 string-append("NM_FOR_TARGET=", binutils, "/nm"), string-append("RANLIB_FOR_TARGET=", binutils, "/ranlib"))
90 } else { list() }
91 build-option := if cross { list() } else { list(string-append("--build=", triplet)) }
92 configured := string-append(build-dir(), "/.vinix-configured")
93 if not(file-exists?(configured)) {
94 apply(run, append(list("sh", "-ec", "cd \"$1\"; shift; exec \"$@\"", "gcc-configure", build-dir(),
95 "env", "CC=vinix-cc", "CXX=vinix-c++", "CC_FOR_BUILD=gcc", "CXX_FOR_BUILD=g++",
96 "CFLAGS=-O1 -g0", "CFLAGS_FOR_TARGET=-O2 -g0", "CXXFLAGS_FOR_TARGET=-O2 -g0"), target-tools,
97 list("gcc-14.2.0/configure", "--prefix=/usr", "--libdir=/usr/lib"), build-option,
98 list(string-append("--host=", triplet), string-append("--target=", triplet), "--enable-languages=c,c++", "--disable-bootstrap",
99 "--with-sysroot=/", string-append("--with-build-sysroot=", root), "--with-native-system-header-dir=/usr/include",
100 string-append("--with-build-time-tools=", binutils),
101 string-append("--with-gmp=", root, "/usr"), string-append("--with-mpfr=", root, "/usr"), string-append("--with-mpc=", root, "/usr"),
102 string-append("--with-isl=", root, "/usr"), "--with-system-zlib", "--without-zstd", "--disable-multilib", "--disable-nls",
103 "--disable-libsanitizer", "--disable-libvtv", "--disable-libquadmath", "--enable-libgomp", "--enable-libatomic", "--enable-libitm",
104 "--disable-libstdcxx-pch", "--enable-libcc1", "--enable-lto", "--enable-plugin", "--enable-threads=posix",
105 "--enable-shared", "--enable-default-pie", "--enable-default-ssp", "--enable-initfini-array")))
106 run("touch", configured)
107 }
108 apply(run, append(list("make", "-C", build-dir(), "GENERATOR_CFLAGS=-O2 -g0"), target-tools,
109 list("all-gcc", "all-libcc1", "all-lto-plugin", "all-target-libgcc", "all-target-libstdc++-v3",
110 "all-target-libatomic", "all-target-libgomp")))
111 ; Pass a concrete command to libitm's recursive make. The standard
112 ; CXX_FOR_TARGET recipe expands a shell command to find the C++ headers.
113 apply(run, append(list("make", "-C", build-dir(), itm-compiler()), target-tools, list("all-target-libitm")))
114 },
115 #:check, fn() {
116 driver := string-append(build-dir(), "/gcc/xgcc")
117 binaries := string-append("-B", build-dir(), "/gcc/")
118 if cross {
119 ; Test with the signed gcc-libs dependency used by the installed compiler.
120 run("vinix-run", driver, binaries, "-dumpmachine")
121 } else {
122 run(driver, binaries, "-dumpmachine")
123 }
124 },
125 #:package, fn() {
126 run("make", "-C", build-dir(), string-append("DESTDIR=", pkgdir()), "install-gcc", "install-libcc1", "install-lto-plugin", "install-target-libgcc",
127 "install-target-libstdc++-v3", "install-target-libatomic", "install-target-libgomp")
128 run("make", "-C", build-dir(), string-append("DESTDIR=", pkgdir()), itm-compiler(), "install-target-libitm")
129 libdir := string-append(pkgdir(), "/usr/lib/")
130 ; The cross bootstrap driver can configure libstdc++ to install in lib64.
131 ; The target GCC and musl use /usr/lib.
132 alternate := string-append(pkgdir(), "/usr/lib64")
133 if cross {
134 if file-exists?(alternate) {
135 for-each(fn(file) { run("mv", "-n", "--", file, libdir) }, glob(string-append(alternate, "/*")))
136 run("rmdir", "--", alternate)
137 }
138 }
139 ; gcc-libs owns the versioned ELF files. Point the development links at
140 ; their SONAMEs so they work with that package's installed filenames.
141 for-each(fn(lib, version) {
142 linker := string-append(libdir, lib, ".so")
143 soname := string-append(lib, ".so.", version)
144 run("test", "-f", string-append(libdir, soname))
145 if not(string=?("libgcc_s", lib)) {
146 run("ln", "-sfn", soname, linker)
147 }
148 for-each(fn(file) { run("rm", file) }, glob(string-append(linker, ".*")))
149 }, list("libgcc_s", "libstdc++", "libatomic", "libgomp"), list("1", "6", "1", "1"))
150 for-each(fn(la) { run("rm", la) }, glob(string-append(libdir, "*.la")))
151 run("mv", string-append(libdir, "libitm.spec"), string-append(libdir, "gcc/", triplet, "/14.2.0/libitm.spec"))
152 run("ln", "-s", "gcc", string-append(pkgdir(), "/usr/bin/cc"))
153 for-each(fn(standard) {
154 path := string-append(pkgdir(), "/usr/bin/", standard)
155 compatible := if string=?(standard, "c89") { "-ansi|-std=iso9899:1990" } else { "-std=iso9899:1999" }
156 call-with-output-file(path, fn(port) {
157 display(string-append("#!/bin/sh\nfor arg do\n case \"$arg\" in\n -std=", standard, "|", compatible,
158 ") ;;\n -std=*) echo \"", standard, ": incompatible C standard: $arg\" >&2; exit 1 ;;\n esac\ndone\nexec /usr/bin/gcc -std=",
159 standard, " \"$@\"\n"), port)
160 })
161 run("chmod", "755", path)
162 }, list("c89", "c99"))
163 plugin := string-append("libexec/gcc/", triplet, "/14.2.0/liblto_plugin.so")
164 run("test", "-f", string-append(pkgdir(), "/usr/", plugin))
165 run("mkdir", "-p", string-append(pkgdir(), "/usr/lib/bfd-plugins"))
166 run("ln", "-s", string-append("../../", plugin), string-append(pkgdir(), "/usr/lib/bfd-plugins/liblto_plugin.so"))
167 run("rm", "-f", string-append(pkgdir(), "/usr/share/info/dir"))
168 run("install", "-Dm644", string-append(build-dir(), "/gcc-14.2.0/COPYING3"), string-append(pkgdir(), "/usr/share/licenses/gcc/COPYING3"))
169 run("install", "-Dm644", string-append(build-dir(), "/gcc-14.2.0/COPYING.RUNTIME"), string-append(pkgdir(), "/usr/share/licenses/gcc/COPYING.RUNTIME"))
170 ; The worker's private umask must not restrict public compiler paths.
171 run("find", pkgdir(), "-type", "d", "-exec", "chmod", "755", "{}", "+")
172 })
173