btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / compilers / go / 1.23.nix
blob14a80eae8046d31e48e6aa2c53c677af989cc5ee
1 { lib
2 , stdenv
3 , fetchurl
4 , tzdata
5 , substituteAll
6 , iana-etc
7 , apple-sdk_11
8 , xcbuild
9 , mailcap
10 , buildPackages
11 , pkgsBuildTarget
12 , threadsCross
13 , testers
14 , skopeo
15 , buildGo123Module
18 let
19   goBootstrap = buildPackages.callPackage ./bootstrap121.nix { };
21   skopeoTest = skopeo.override { buildGoModule = buildGo123Module; };
23   goarch = platform: {
24     "aarch64" = "arm64";
25     "arm" = "arm";
26     "armv5tel" = "arm";
27     "armv6l" = "arm";
28     "armv7l" = "arm";
29     "i686" = "386";
30     "mips" = "mips";
31     "mips64el" = "mips64le";
32     "mipsel" = "mipsle";
33     "powerpc64" = "ppc64";
34     "powerpc64le" = "ppc64le";
35     "riscv64" = "riscv64";
36     "s390x" = "s390x";
37     "x86_64" = "amd64";
38     "wasm32" = "wasm";
39   }.${platform.parsed.cpu.name} or (throw "Unsupported system: ${platform.parsed.cpu.name}");
41   # We need a target compiler which is still runnable at build time,
42   # to handle the cross-building case where build != host == target
43   targetCC = pkgsBuildTarget.targetPackages.stdenv.cc;
45   isCross = stdenv.buildPlatform != stdenv.targetPlatform;
47 stdenv.mkDerivation (finalAttrs: {
48   pname = "go";
49   version = "1.23.3";
51   src = fetchurl {
52     url = "https://go.dev/dl/go${finalAttrs.version}.src.tar.gz";
53     hash = "sha256-jWp3MySHVXxq+iQhExtQ+D20rjxXnDvHLmcO4faWhZk=";
54   };
56   strictDeps = true;
57   buildInputs = [ ]
58     ++ lib.optionals stdenv.hostPlatform.isLinux [ stdenv.cc.libc.out ]
59     ++ lib.optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
61   depsTargetTargetPropagated = lib.optionals stdenv.targetPlatform.isDarwin [ apple-sdk_11 xcbuild ];
63   depsBuildTarget = lib.optional isCross targetCC;
65   depsTargetTarget = lib.optional stdenv.targetPlatform.isWindows threadsCross.package;
67   postPatch = ''
68     patchShebangs .
69   '';
71   patches = [
72     (substituteAll {
73       src = ./iana-etc-1.17.patch;
74       iana = iana-etc;
75     })
76     # Patch the mimetype database location which is missing on NixOS.
77     # but also allow static binaries built with NixOS to run outside nix
78     (substituteAll {
79       src = ./mailcap-1.17.patch;
80       inherit mailcap;
81     })
82     # prepend the nix path to the zoneinfo files but also leave the original value for static binaries
83     # that run outside a nix server
84     (substituteAll {
85       src = ./tzdata-1.19.patch;
86       inherit tzdata;
87     })
88     ./remove-tools-1.11.patch
89     ./go_no_vendor_checks-1.22.patch
90   ];
92   GOOS = if stdenv.targetPlatform.isWasi then "wasip1" else stdenv.targetPlatform.parsed.kernel.name;
93   GOARCH = goarch stdenv.targetPlatform;
94   # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
95   # Go will nevertheless build a for host system that we will copy over in
96   # the install phase.
97   GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
98   GOHOSTARCH = goarch stdenv.buildPlatform;
100   # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
101   # to be different from CC/CXX
102   CC_FOR_TARGET =
103     if isCross then
104       "${targetCC}/bin/${targetCC.targetPrefix}cc"
105     else
106       null;
107   CXX_FOR_TARGET =
108     if isCross then
109       "${targetCC}/bin/${targetCC.targetPrefix}c++"
110     else
111       null;
113   GOARM = toString (lib.intersectLists [ (stdenv.hostPlatform.parsed.cpu.version or "") ] [ "5" "6" "7" ]);
114   GO386 = "softfloat"; # from Arch: don't assume sse2 on i686
115   # Wasi does not support CGO
116   CGO_ENABLED = if stdenv.targetPlatform.isWasi then 0 else 1;
118   GOROOT_BOOTSTRAP = "${goBootstrap}/share/go";
120   buildPhase = ''
121     runHook preBuild
122     export GOCACHE=$TMPDIR/go-cache
124     export PATH=$(pwd)/bin:$PATH
126     ${lib.optionalString isCross ''
127     # Independent from host/target, CC should produce code for the building system.
128     # We only set it when cross-compiling.
129     export CC=${buildPackages.stdenv.cc}/bin/cc
130     ''}
131     ulimit -a
133     pushd src
134     ./make.bash
135     popd
136     runHook postBuild
137   '';
139   preInstall = ''
140     # Contains the wrong perl shebang when cross compiling,
141     # since it is not used for anything we can deleted as well.
142     rm src/regexp/syntax/make_perl_groups.pl
143   '' + (if (stdenv.buildPlatform.system != stdenv.hostPlatform.system) then ''
144     mv bin/*_*/* bin
145     rmdir bin/*_*
146     ${lib.optionalString (!(finalAttrs.GOHOSTARCH == finalAttrs.GOARCH && finalAttrs.GOOS == finalAttrs.GOHOSTOS)) ''
147       rm -rf pkg/${finalAttrs.GOHOSTOS}_${finalAttrs.GOHOSTARCH} pkg/tool/${finalAttrs.GOHOSTOS}_${finalAttrs.GOHOSTARCH}
148     ''}
149   '' else lib.optionalString (stdenv.hostPlatform.system != stdenv.targetPlatform.system) ''
150     rm -rf bin/*_*
151     ${lib.optionalString (!(finalAttrs.GOHOSTARCH == finalAttrs.GOARCH && finalAttrs.GOOS == finalAttrs.GOHOSTOS)) ''
152       rm -rf pkg/${finalAttrs.GOOS}_${finalAttrs.GOARCH} pkg/tool/${finalAttrs.GOOS}_${finalAttrs.GOARCH}
153     ''}
154   '');
156   installPhase = ''
157     runHook preInstall
158     mkdir -p $out/share/go
159     cp -a bin pkg src lib misc api doc go.env $out/share/go
160     mkdir -p $out/bin
161     ln -s $out/share/go/bin/* $out/bin
162     runHook postInstall
163   '';
165   disallowedReferences = [ goBootstrap ];
167   passthru = {
168     inherit goBootstrap skopeoTest;
169     tests = {
170       skopeo = testers.testVersion { package = skopeoTest; };
171       version = testers.testVersion {
172         package = finalAttrs.finalPackage;
173         command = "go version";
174         version = "go${finalAttrs.version}";
175       };
176     };
177   };
179   meta = with lib; {
180     changelog = "https://go.dev/doc/devel/release#go${lib.versions.majorMinor finalAttrs.version}";
181     description = "Go Programming language";
182     homepage = "https://go.dev/";
183     license = licenses.bsd3;
184     maintainers = teams.golang.members;
185     platforms = platforms.darwin ++ platforms.linux ++ platforms.wasi ++ platforms.freebsd;
186     mainProgram = "go";
187   };