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