1{ stdenv, lib, autoPatchelfHook, fetchzip, xz, ncurses5, readline, gmp, mpfr
2, expat, libipt, zlib, dejagnu, sourceHighlight, python3, elfutils, guile, glibc
3, majorVersion
4}:
5
6let
7 throwUnsupportedSystem = throw "Unsupported system: ${stdenv.hostPlatform.system}";
8
9 versionMap = rec {
10 "11" = {
11 gccVersion = "11.2.0";
12 alireRevision = "4";
13 } // {
14 x86_64-darwin = {
15 hash = "sha256-FmBgD20PPQlX/ddhJliCTb/PRmKxe9z7TFPa2/SK4GY=";
16 upstreamTriplet = "x86_64-apple-darwin19.6.0";
17 };
18 x86_64-linux = {
19 hash = "sha256-8fMBJp6igH+Md5jE4LMubDmC4GLt4A+bZG/Xcz2LAJQ=";
20 upstreamTriplet = "x86_64-pc-linux-gnu";
21 };
22 }.${stdenv.hostPlatform.system} or throwUnsupportedSystem;
23 "12" = {
24 gccVersion = "12.1.0";
25 alireRevision = "2";
26 } // {
27 x86_64-darwin = {
28 hash = "sha256-zrcVFvFZMlGUtkG0p1wST6kGInRI64Icdsvkcf25yVs=";
29 upstreamTriplet = "x86_64-apple-darwin19.6.0";
30 };
31 x86_64-linux = {
32 hash = "sha256-EPDPOOjWJnJsUM7GGxj20/PXumjfLoMIEFX1EDtvWVY=";
33 upstreamTriplet = "x86_64-pc-linux-gnu";
34 };
35 }.${stdenv.hostPlatform.system} or throwUnsupportedSystem;
36 };
37
38in with versionMap.${majorVersion};
39
40stdenv.mkDerivation rec {
41 pname = "gnat-bootstrap";
42 inherit gccVersion alireRevision;
43
44 version = "${gccVersion}-${alireRevision}";
45
46 src = fetchzip {
47 url = "https://github.com/alire-project/GNAT-FSF-builds/releases/download/gnat-${version}/gnat-${stdenv.hostPlatform.system}-${version}.tar.gz";
48 inherit hash;
49 };
50
51 nativeBuildInputs = [
52 dejagnu
53 expat
54 gmp
55 guile
56 libipt
57 mpfr
58 ncurses5
59 python3
60 readline
61 sourceHighlight
62 xz
63 zlib
64 ] ++ lib.optionals stdenv.buildPlatform.isLinux [
65 autoPatchelfHook
66 elfutils
67 glibc
68 ];
69
70 postPatch = lib.optionalString (stdenv.hostPlatform.isDarwin) ''
71 substituteInPlace lib/gcc/${upstreamTriplet}/${gccVersion}/install-tools/mkheaders.conf \
72 --replace "SYSTEM_HEADER_DIR=\"/usr/include\"" "SYSTEM_HEADER_DIR=\"/include\""
73 ''
74 # The included fixincl binary that is called during header fixup has a
75 # hardcoded execvp("/usr/bin/sed", ...) call, but /usr/bin/sed isn't
76 # available in the Nix Darwin stdenv. Fortunately, execvp() will search the
77 # PATH environment variable for the executable if its first argument does not
78 # contain a slash, so we can just change the string to "sed" and zero the
79 # other bytes.
80 + ''
81 sed -i "s,/usr/bin/sed,sed\x00\x00\x00\x00\x00\x00\x00\x00\x00," libexec/gcc/${upstreamTriplet}/${gccVersion}/install-tools/fixincl
82 '';
83
84 installPhase = ''
85 mkdir -p $out
86 cp -ar * $out/
87 ''
88
89 # So far with the Darwin gnat-bootstrap binary packages, there have been two
90 # types of dylib path references to other dylibs that need fixups:
91 #
92 # 1. Dylibs in $out/lib with paths starting with
93 # /Users/runner/.../gcc/install that refer to other dylibs in $out/lib
94 # 2. Dylibs in $out/lib/gcc/*/*/adalib with paths starting with
95 # @rpath that refer to other dylibs in $out/lib/gcc/*/*/adalib
96 #
97 # Additionally, per Section 14.4 Fixed Headers in the GCC 12.2.0 manual [2],
98 # we have to update the fixed header files in current Alire GCC package, since it
99 # was built against macOS 10.15 (Darwin 19.6.0), but Nix currently
100 # builds against macOS 10.12, and the two header file structures differ.
101 # For example, the current Alire GCC package has a fixed <stdio.h>
102 # from macOS 10.15 that contains a #include <_stdio.h>, but neither the Alire
103 # GCC package nor macOS 10.12 have such a header (<xlocale/_stdio.h> and
104 # <secure/_stdio.h> in 10.12 are not equivalent; indeed, 10.15 <_stdio.h>
105 # says it contains code shared by <stdio.h> and <xlocale/_stdio.h>).
106 #
107 # [2]: https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Fixed-Headers.html
108
109 + lib.optionalString (stdenv.hostPlatform.isDarwin) ''
110 upstreamBuildPrefix="/Users/runner/work/GNAT-FSF-builds/GNAT-FSF-builds/sbx/x86_64-darwin/gcc/install"
111 for i in "$out"/lib/*.dylib "$out"/lib/gcc/*/*/adalib/*.dylib; do
112 if [[ -f "$i" && ! -h "$i" ]]; then
113 install_name_tool -id "$i" "$i" || true
114 for old_path in $(otool -L "$i" | grep "$upstreamBuildPrefix" | awk '{print $1}'); do
115 new_path=`echo "$old_path" | sed "s,$upstreamBuildPrefix,$out,"`
116 install_name_tool -change "$old_path" "$new_path" "$i" || true
117 done
118 for old_path in $(otool -L "$i" | grep "@rpath" | awk '{print $1}'); do
119 new_path=$(echo "$old_path" | sed "s,@rpath,$(dirname "$i"),")
120 install_name_tool -change "$old_path" "$new_path" "$i" || true
121 done
122 fi
123 done
124
125 "$out"/libexec/gcc/${upstreamTriplet}/${gccVersion}/install-tools/mkheaders -v -v \
126 "$out" "${stdenv.cc.libc}"
127 '';
128
129 passthru = {
130 langC = true; # TRICK for gcc-wrapper to wrap it
131 langCC = false;
132 langFortran = false;
133 langAda = true;
134 isGNU = true;
135 };
136
137 meta = with lib; {
138 description = "GNAT, the GNU Ada Translator";
139 homepage = "https://www.gnu.org/software/gnat";
140 license = licenses.gpl3;
141 maintainers = with maintainers; [ ethindp ];
142 platforms = [ "x86_64-linux" "x86_64-darwin" ];
143 sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
144 };
145}