From 977a1c0092f14e18a4785319519a0736853d424c Mon Sep 17 00:00:00 2001 From: Victor Fuentes Date: Mon, 26 Aug 2024 21:34:42 -0700 Subject: [PATCH] WIP: cross compile gcc --- tidepool/src/export.nix | 5 +- tidepool/src/packages/default.nix | 46 +++- .../packages/foundation/binutils/default.nix | 15 +- .../src/packages/foundation/gcc/default.nix | 84 +++---- .../src/packages/foundation/gcc/newlib.nix | 238 ++++++++++++++++++ .../src/packages/foundation/glibc/default.nix | 2 +- 6 files changed, 339 insertions(+), 51 deletions(-) create mode 100644 tidepool/src/packages/foundation/gcc/newlib.nix diff --git a/tidepool/src/export.nix b/tidepool/src/export.nix index dd23814..4c8f541 100644 --- a/tidepool/src/export.nix +++ b/tidepool/src/export.nix @@ -33,7 +33,10 @@ in # platform.host = lib.modules.overrides.force "x86_64-linux"; # }; cross-aux-a-x86_64-linux = config.packages.cross.x86_64-linux.aux.a; - cross-foundation-gcc-x86_64-linux = config.packages.cross.x86_64-linux.foundation.gcc; + cross-foundation-gcc-x86_64-linux = config.packages.cross-tools.x86_64-linux.foundation.gcc-newlib; + + cross-foundation-binutils-x86_64-linux = config.packages.cross.x86_64-linux.foundation.binutils; + example-a = config.packages.foundation.gcc.versions.latest.extend { platform = { diff --git a/tidepool/src/packages/default.nix b/tidepool/src/packages/default.nix index 1807094..8df35b6 100644 --- a/tidepool/src/packages/default.nix +++ b/tidepool/src/packages/default.nix @@ -4,7 +4,7 @@ let doubles = lib.systems.doubles.all; - packages = builtins.removeAttrs config.packages [ "cross" ]; + packages = builtins.removeAttrs config.packages [ "cross" "cross-tools" ]; in { includes = [ @@ -21,6 +21,14 @@ in freeform = lib.types.packages; options = { + cross-tools = lib.attrs.generate doubles ( + system: + lib.options.create { + description = "The cross-compilation tools for the ${system} target."; + type = lib.types.packages; + default.value = { }; + } + ); cross = lib.attrs.generate doubles ( system: lib.options.create { @@ -54,6 +62,36 @@ in }; config = { + packages.cross-tools = lib.attrs.generate doubles ( + system: + builtins.mapAttrs + ( + namespace: + builtins.mapAttrs ( + name: alias: + let + setTarget = + package: + package + // { + __modules__ = package.__modules__ ++ [ + { + config.platform = { + target = lib.modules.override 5 system; + }; + } + ]; + }; + + updated = alias // { + versions = builtins.mapAttrs (version: package: setTarget package) alias.versions; + }; + in + updated + ) + ) + packages + ); packages.cross = lib.attrs.generate doubles ( system: builtins.mapAttrs @@ -62,14 +100,14 @@ in builtins.mapAttrs ( name: alias: let - setHost = + setPlatform = package: package // { __modules__ = package.__modules__ ++ [ { config.platform = { - host = lib.modules.override 5 system; + build = lib.modules.override 5 system; target = lib.modules.override 5 system; }; } @@ -77,7 +115,7 @@ in }; updated = alias // { - versions = builtins.mapAttrs (version: package: setHost package) alias.versions; + versions = builtins.mapAttrs (version: package: setPlatform package) alias.versions; }; in updated diff --git a/tidepool/src/packages/foundation/binutils/default.nix b/tidepool/src/packages/foundation/binutils/default.nix index 7289cee..641b683 100644 --- a/tidepool/src/packages/foundation/binutils/default.nix +++ b/tidepool/src/packages/foundation/binutils/default.nix @@ -33,10 +33,12 @@ in isHostBootstrapped = config.platform.host.double == "i686-linux"; isBootstrapped = isBuildBootstrapped && isHostBootstrapped; + + isCross = (config.platform.build.triple != config.platform.host.triple) && (config.platform.build.triple == config.platform.target.triple); in { meta = { - platforms = [ "i686-linux" ]; + platforms = [ "i686-linux" "x86_64-linux" ]; }; pname = "binutils"; @@ -47,7 +49,13 @@ in deps = { build = { only = { - gcc = lib.modules.when (!isBootstrapped) packages.foundation.gcc; + # gcc = lib.modules.when (!isBootstrapped) packages.foundation.gcc; + gcc = lib.modules.when (isCross) (packages.foundation.gcc.versions.latest.extend { + platform = { + inherit (config.platform) host target; + build = config.platform.target; + }; + }); }; }; }; @@ -116,6 +124,9 @@ in ''; configure = '' + echo "BUILD: ${config.platform.build.triple}" + echo "HOST: ${config.platform.host.triple}" + echo "TARGET: ${config.platform.target.triple}" bash ./configure ${builtins.concatStringsSep " " configureFlags} ''; diff --git a/tidepool/src/packages/foundation/gcc/default.nix b/tidepool/src/packages/foundation/gcc/default.nix index 3887773..3ebb943 100644 --- a/tidepool/src/packages/foundation/gcc/default.nix +++ b/tidepool/src/packages/foundation/gcc/default.nix @@ -11,6 +11,10 @@ let ; in { + includes = [ + ./newlib.nix + ]; + config.packages.context.options = { "foundation:cflags" = lib.options.create { type = lib.types.list.of lib.types.string; @@ -110,35 +114,36 @@ in # }; deps = - let - platform = - if !isBuildBootstrapped then - { - build = "i686-linux"; - host = lib.modules.override 0 config.platform.build.triple; - target = lib.modules.override 0 config.platform.build.triple; - } - else - { - build = "i686-linux"; - host = "i686-linux"; - target = lib.modules.override 0 config.platform.host.triple; - }; - in + # let + # platform = + # if !isBuildBootstrapped then + # { + # build = "i686-linux"; + # host = lib.modules.override 0 config.platform.build.triple; + # target = lib.modules.override 0 config.platform.build.triple; + # } + # else + # { + # build = "i686-linux"; + # host = "i686-linux"; + # target = lib.modules.override 0 config.platform.host.triple; + # }; + # in { build = { only = { - gcc = - lib.modules.when - (!isBootstrapped) - (packages.foundation.gcc.versions.latest.extend { - inherit platform; - }); + # gcc = + # lib.modules.when + # (!isBootstrapped) + # (packages.foundation.gcc.versions.latest.extend { + # inherit platform; + # }); + # binutils = lib.modules.when + # (!isBootstrapped) packages.foundation.binutils; }; build = { - binutils = packages.foundation.binutils; - linux-headers = packages.foundation.linux-headers; - glibc = packages.foundation.glibc; + # linux-headers = packages.foundation.linux-headers; + # glibc = packages.foundation.glibc; }; }; }; @@ -155,7 +160,7 @@ in (isBootstrapped) [ foundation.stage2-gcc - # foundation.stage2-binutils + foundation.stage2-binutils ] ++ [ foundation.stage2-gnumake @@ -172,19 +177,7 @@ in ); }; - phases = - let - host = lib.systems.withBuildInfo config.platform.host; - - compiler = if isBootstrapped then "gcc" else "${config.platform.build.triple}-gcc"; - - mbits = if host.system.cpu.family == "x86" then if host.is64bit then "-m64" else "-m32" else ""; - in - { - "aux:compiler:setup" = '' - : ''${AUX_COMPILER_LIBRARY_PATH:=} - ''; - + phases = { unpack = '' # Unpack tar xf ${config.src} @@ -207,10 +200,15 @@ in ''; configure = '' + echo "BUILD: ${config.platform.build.triple}" + echo "HOST: ${config.platform.host.triple}" + echo "TARGET: ${config.platform.target.triple}" + exit 1 # Configure - export CC="gcc -Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" - export CXX="g++ -Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" - export CFLAGS_FOR_TARGET="-Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" + export CC="gcc -Wl,-static -static-libgcc" + export CXX="g++ -Wl,-static -static-libgcc" + # export CC_FOR_BUILD="gcc -Wl,-static -static-libgcc" + export CFLAGS_FOR_TARGET="-Wl,-static -static-libgcc" export LIBRARY_PATH="${foundation.stage1-musl}/lib" bash ./configure \ @@ -226,8 +224,8 @@ in --disable-lto \ --disable-multilib \ --disable-plugin \ - CFLAGS=-static \ - CXXFLAGS=-static + --disable-libatomic + ''; build = '' diff --git a/tidepool/src/packages/foundation/gcc/newlib.nix b/tidepool/src/packages/foundation/gcc/newlib.nix new file mode 100644 index 0000000..d1989ee --- /dev/null +++ b/tidepool/src/packages/foundation/gcc/newlib.nix @@ -0,0 +1,238 @@ +{ config, options }: +let + inherit (config) + lib + mirrors + builders + # These are the upstream foundational packages exported from the Aux Foundation project. + + foundation + packages + ; +in +{ + config.packages.foundation.gcc-newlib = { + versions = { + "latest" = + { config, meta }: + { + options = { + src = lib.options.create { + type = lib.types.derivation; + description = "Source for the package."; + }; + + cc = { + src = lib.options.create { + type = lib.types.derivation; + description = "The cc source for the package."; + }; + }; + + gmp = { + src = lib.options.create { + type = lib.types.derivation; + description = "The gmp source for the package."; + }; + + version = lib.options.create { + type = lib.types.string; + description = "Version of gmp."; + }; + }; + + mpfr = { + src = lib.options.create { + type = lib.types.derivation; + description = "The mpfr source for the package."; + }; + + version = lib.options.create { + type = lib.types.string; + description = "Version of mpfr."; + }; + }; + + mpc = { + src = lib.options.create { + type = lib.types.derivation; + description = "The mpc source for the package."; + }; + + version = lib.options.create { + type = lib.types.string; + description = "Version of mpc."; + }; + }; + + isl = { + src = lib.options.create { + type = lib.types.derivation; + description = "The isl source for the package."; + }; + version = lib.options.create { + type = lib.types.string; + description = "Version of isl."; + }; + }; + }; + + config = + let + isBuildBootstrapped = config.platform.build.double == "i686-linux"; + isHostBootstrapped = config.platform.host.double == "i686-linux"; + + isBootstrapped = isBuildBootstrapped && isHostBootstrapped; + in + { + meta = { + platforms = [ "i686-linux" ]; + }; + + pname = "gcc-${config.platform.build.double}--${config.platform.host.double}--${config.platform.target.double}"; + version = "13.2.0"; + + builder = builders.basic; + + deps = { + build = { + build = { + binutils = (packages.foundation.binutils.versions.latest.extend { + platform = lib.modules.override 0 config.platform; + }); + }; + }; + }; + + env = { + PATH = lib.paths.bin ( + lib.lists.when + (isBootstrapped) + [ + foundation.stage2-gcc + foundation.stage2-binutils + ] + ++ [ + foundation.stage2-gnumake + foundation.stage2-gnused + foundation.stage2-gnugrep + foundation.stage2-gawk + foundation.stage2-diffutils + foundation.stage2-findutils + foundation.stage2-gnutar + foundation.stage2-gzip + foundation.stage2-bzip2 + foundation.stage1-xz + ] + ); + }; + + phases = { + unpack = '' + # Unpack + tar xf ${config.src} + tar xf ${config.gmp.src} + tar xf ${config.mpfr.src} + tar xf ${config.mpc.src} + tar xf ${config.isl.src} + cd gcc-${config.version} + + ln -s ../gmp-${config.gmp.version} gmp + ln -s ../mpfr-${config.mpfr.version} mpfr + ln -s ../mpc-${config.mpc.version} mpc + ln -s ../isl-${config.isl.version} isl + ''; + + patch = '' + # Patch + # force musl even if host triple is gnu + sed -i 's|"os/gnu-linux"|"os/generic"|' libstdc++-v3/configure.host + ''; + + configure = '' + # Configure + export CC="gcc -Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" + export CXX="g++ -Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" + export CFLAGS_FOR_TARGET="-Wl,-dynamic-linker -Wl,${foundation.stage1-musl}/lib/libc.so" + export LIBRARY_PATH="${foundation.stage1-musl}/lib" + + mkdir build + cd build + + bash ../configure \ + --prefix=$out \ + --build=${config.platform.build.triple} \ + --host=${config.platform.host.triple} \ + --target=${config.platform.target.triple} \ + --with-as=${config.deps.build.build.binutils.package}/bin/${config.platform.target.triple}-as \ + --with-ld=${config.deps.build.build.binutils.package}/bin/${config.platform.target.triple}-ld \ + --enable-languages=c \ + --disable-bootstrap \ + --disable-libsanitizer \ + --disable-lto \ + --disable-multilib \ + --disable-plugin \ + --disable-libssp \ + --disable-libquadmath \ + --disable-threads \ + --disable-decimal-float \ + --disable-shared \ + --disable-libmudflap \ + --disable-libgomp \ + --disable-libatomic \ + --without-headers \ + --with-newlib + ''; + + build = '' + # Build + make -j $NIX_BUILD_CORES + ''; + + install = '' + # Install + make -j $NIX_BUILD_CORES install + ''; + }; + + src = builtins.fetchurl { + url = "${mirrors.gnu}/gcc/gcc-${config.version}/gcc-${config.version}.tar.xz"; + sha256 = "4nXnZEKmBnNBon8Exca4PYYTFEAEwEE1KIY9xrXHQ9o="; + }; + + gmp = { + version = "6.3.0"; + src = builtins.fetchurl { + url = "${mirrors.gnu}/gmp/gmp-${config.gmp.version}.tar.xz"; + sha256 = "o8K4AgG4nmhhb0rTC8Zq7kknw85Q4zkpyoGdXENTiJg="; + }; + }; + + mpfr = { + version = "4.2.1"; + src = builtins.fetchurl { + url = "${mirrors.gnu}/mpfr/mpfr-${config.mpfr.version}.tar.xz"; + sha256 = "J3gHNTpnJpeJlpRa8T5Sgp46vXqaW3+yeTiU4Y8fy7I="; + }; + }; + + mpc = { + version = "1.3.1"; + src = builtins.fetchurl { + url = "${mirrors.gnu}/mpc/mpc-${config.mpc.version}.tar.gz"; + sha256 = "q2QkkvXPiCt0qgy3MM1BCoHtzb7IlRg86TDnBsHHWbg="; + }; + }; + + isl = { + version = "0.24"; + src = builtins.fetchurl { + url = "https://gcc.gnu.org/pub/gcc/infrastructure/isl-${config.isl.version}.tar.bz2"; + sha256 = "/PeN2WVsEOuM+fvV9ZoLawE4YgX+GTSzsoegoYmBRcA="; + }; + }; + }; + }; + }; + }; +} diff --git a/tidepool/src/packages/foundation/glibc/default.nix b/tidepool/src/packages/foundation/glibc/default.nix index 3eb9449..89655d4 100644 --- a/tidepool/src/packages/foundation/glibc/default.nix +++ b/tidepool/src/packages/foundation/glibc/default.nix @@ -28,7 +28,7 @@ in config = { meta = { - platforms = [ "i686-linux" ]; + platforms = [ "i686-linux" "x86_64-linux" ]; }; pname = "gcc";