| 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. |
| 5 | host-arch := if memq(string->symbol("aarch64"), features()) { "aarch64" } else { "x86_64" } |
| 6 | selected := get-environment-variable("VPAC_TARGET_ARCH") |
| 7 | arch := if string?(selected) { selected } else { host-arch } |
| 8 | triplet := string-append(arch, "-linux-musl") |
| 9 | cross := not(string=?(arch, host-arch)) |
| 10 | libc := 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. |
| 14 | checkpoint := get-environment-variable("VINIX_GCC_BUILD_DIR") |
| 15 | persistent := if string?(checkpoint) { not(string=?("", checkpoint)) } else { #f } |
| 16 | checkpoint-recipe := if persistent { string-append(checkpoint, "/recipe.vile") } else { "" } |
| 17 | cached := if persistent { file-exists?(checkpoint-recipe) } else { #f } |
| 18 | build-dir := fn() { if persistent { checkpoint } else { srcdir() } } |
| 19 | local-archive := get-environment-variable("VINIX_GCC_LOCAL_SOURCE") |
| 20 | source-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 | |
| 26 | itm-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 | |
| 38 | defpkgbuild(gcc, |
| 39 | #:pkgname, "gcc", |
| 40 | #:pkgver, "14.2.0", |
| 41 | #:pkgrel, 1, |
| 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 | runtime := string-append(build-dir(), "/", triplet) |
| 120 | libraries := string-append(runtime, "/libstdc++-v3/src/.libs:", runtime, "/libgcc") |
| 121 | run("vinix-run", "--library-path", libraries, driver, binaries, "-dumpmachine") |
| 122 | } else { |
| 123 | run(driver, binaries, "-dumpmachine") |
| 124 | } |
| 125 | }, |
| 126 | #:package, fn() { |
| 127 | run("make", "-C", build-dir(), string-append("DESTDIR=", pkgdir()), "install-gcc", "install-libcc1", "install-lto-plugin", "install-target-libgcc", |
| 128 | "install-target-libstdc++-v3", "install-target-libatomic", "install-target-libgomp") |
| 129 | run("make", "-C", build-dir(), string-append("DESTDIR=", pkgdir()), itm-compiler(), "install-target-libitm") |
| 130 | ; gcc-libs owns the versioned ELF files. Point the development links at |
| 131 | ; their SONAMEs so they work with that package's installed filenames. |
| 132 | libdir := string-append(pkgdir(), "/usr/lib/") |
| 133 | for-each(fn(lib, version) { |
| 134 | linker := string-append(libdir, lib, ".so") |
| 135 | soname := string-append(lib, ".so.", version) |
| 136 | run("test", "-f", string-append(libdir, soname)) |
| 137 | if not(string=?("libgcc_s", lib)) { |
| 138 | run("ln", "-sfn", soname, linker) |
| 139 | } |
| 140 | for-each(fn(file) { run("rm", file) }, glob(string-append(linker, ".*"))) |
| 141 | }, list("libgcc_s", "libstdc++", "libatomic", "libgomp"), list("1", "6", "1", "1")) |
| 142 | for-each(fn(la) { run("rm", la) }, glob(string-append(libdir, "*.la"))) |
| 143 | run("mv", string-append(libdir, "libitm.spec"), string-append(libdir, "gcc/", triplet, "/14.2.0/libitm.spec")) |
| 144 | run("ln", "-s", "gcc", string-append(pkgdir(), "/usr/bin/cc")) |
| 145 | for-each(fn(standard) { |
| 146 | path := string-append(pkgdir(), "/usr/bin/", standard) |
| 147 | compatible := if string=?(standard, "c89") { "-ansi|-std=iso9899:1990" } else { "-std=iso9899:1999" } |
| 148 | call-with-output-file(path, fn(port) { |
| 149 | display(string-append("#!/bin/sh\nfor arg do\n case \"$arg\" in\n -std=", standard, "|", compatible, |
| 150 | ") ;;\n -std=*) echo \"", standard, ": incompatible C standard: $arg\" >&2; exit 1 ;;\n esac\ndone\nexec /usr/bin/gcc -std=", |
| 151 | standard, " \"$@\"\n"), port) |
| 152 | }) |
| 153 | run("chmod", "755", path) |
| 154 | }, list("c89", "c99")) |
| 155 | plugin := string-append("libexec/gcc/", triplet, "/14.2.0/liblto_plugin.so") |
| 156 | run("test", "-f", string-append(pkgdir(), "/usr/", plugin)) |
| 157 | run("mkdir", "-p", string-append(pkgdir(), "/usr/lib/bfd-plugins")) |
| 158 | run("ln", "-s", string-append("../../", plugin), string-append(pkgdir(), "/usr/lib/bfd-plugins/liblto_plugin.so")) |
| 159 | run("rm", "-f", string-append(pkgdir(), "/usr/share/info/dir")) |
| 160 | run("install", "-Dm644", string-append(build-dir(), "/gcc-14.2.0/COPYING3"), string-append(pkgdir(), "/usr/share/licenses/gcc/COPYING3")) |
| 161 | run("install", "-Dm644", string-append(build-dir(), "/gcc-14.2.0/COPYING.RUNTIME"), string-append(pkgdir(), "/usr/share/licenses/gcc/COPYING.RUNTIME")) |
| 162 | }) |
| 163 | |