Merge pull request #119126 from fabaff/pycomfoconnect
[NixPkgs.git] / pkgs / development / compilers / go / 2-dev.nix
blob21347cbd65a9149018af2a13c552b5d09e9a2835
1 { pkgs, lib, stdenv, fetchurl, fetchgit, tzdata, iana-etc, runCommand
2 , perl, which, pkg-config, patch, procps, pcre, cacert, Security, Foundation
3 , mailcap, runtimeShell
4 , buildPackages
5 , pkgsBuildTarget
6 , fetchpatch
7 , callPackage
8 }:
10 let
12   inherit (lib) optionals optionalString;
14   go_bootstrap = buildPackages.callPackage ./bootstrap.nix { };
16   goBootstrap = runCommand "go-bootstrap" {} ''
17     mkdir $out
18     cp -rf ${go_bootstrap}/* $out/
19     chmod -R u+w $out
20     find $out -name "*.c" -delete
21     cp -rf $out/bin/* $out/share/go/bin/
22   '';
24   goarch = platform: {
25     "i686" = "386";
26     "x86_64" = "amd64";
27     "aarch64" = "arm64";
28     "arm" = "arm";
29     "armv5tel" = "arm";
30     "armv6l" = "arm";
31     "armv7l" = "arm";
32     "powerpc64le" = "ppc64le";
33   }.${platform.parsed.cpu.name} or (throw "Unsupported system");
35   # We need a target compiler which is still runnable at build time,
36   # to handle the cross-building case where build != host == target
37   targetCC = pkgsBuildTarget.targetPackages.stdenv.cc;
40 stdenv.mkDerivation rec {
41   pname = "go2-unstable";
42   version = "2021-03-22";
44   src = fetchgit {
45     url = https://go.googlesource.com/go;
46     rev = "a4b4db4cdeefb7b4ea5adb09073dd123846b3588";
47     sha256 = "sha256:1wqqnywcrfazydi5wcg04s6zgsfh4m879vxfgacgrnigd23ynhvr";
48   };
50   # perl is used for testing go vet
51   nativeBuildInputs = [ perl which pkg-config patch procps ];
52   buildInputs = [ cacert pcre ]
53     ++ optionals stdenv.isLinux [ stdenv.cc.libc.out ]
54     ++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
56   depsTargetTargetPropagated = optionals stdenv.isDarwin [ Security Foundation ];
58   hardeningDisable = [ "all" ];
60   prePatch = ''
61     echo '${version}' > VERSION
62     patchShebangs ./ # replace /bin/bash
64     # This source produces shell script at run time,
65     # and thus it is not corrected by patchShebangs.
66     substituteInPlace misc/cgo/testcarchive/carchive_test.go \
67       --replace '#!/usr/bin/env bash' '#!${runtimeShell}'
69     # Patch the mimetype database location which is missing on NixOS.
70     # but also allow static binaries built with NixOS to run outside nix
71     sed -i 's,\"/etc/mime.types,"${mailcap}/etc/mime.types\"\,\n\t&,' src/mime/type_unix.go
73     # Disabling the 'os/http/net' tests (they want files not available in
74     # chroot builds)
75     rm src/net/{listen,parse}_test.go
76     rm src/syscall/exec_linux_test.go
78     # !!! substituteInPlace does not seems to be effective.
79     # The os test wants to read files in an existing path. Just don't let it be /usr/bin.
80     sed -i 's,/usr/bin,'"`pwd`", src/os/os_test.go
81     sed -i 's,/bin/pwd,'"`type -P pwd`", src/os/os_test.go
82     # Fails on aarch64
83     sed -i '/TestFallocate/aif true \{ return\; \}' src/cmd/link/internal/ld/fallocate_test.go
84     # Skip this test since ssl patches mess it up.
85     sed -i '/TestLoadSystemCertsLoadColonSeparatedDirs/aif true \{ return\; \}' src/crypto/x509/root_unix_test.go
86     # Disable another PIE test which breaks.
87     sed -i '/TestTrivialPIE/aif true \{ return\; \}' misc/cgo/testshared/shared_test.go
88     # Disable the BuildModePie test
89     sed -i '/TestBuildmodePIE/aif true \{ return\; \}' src/cmd/go/go_test.go
90     # Disable the unix socket test
91     sed -i '/TestShutdownUnix/aif true \{ return\; \}' src/net/net_test.go
92     # Disable the hostname test
93     sed -i '/TestHostname/aif true \{ return\; \}' src/os/os_test.go
94     # ParseInLocation fails the test
95     sed -i '/TestParseInSydney/aif true \{ return\; \}' src/time/format_test.go
96     # Remove the api check as it never worked
97     sed -i '/src\/cmd\/api\/run.go/ireturn nil' src/cmd/dist/test.go
98     # Remove the coverage test as we have removed this utility
99     sed -i '/TestCoverageWithCgo/aif true \{ return\; \}' src/cmd/go/go_test.go
100     # Remove the timezone naming test
101     sed -i '/TestLoadFixed/aif true \{ return\; \}' src/time/time_test.go
102     # Remove disable setgid test
103     sed -i '/TestRespectSetgidDir/aif true \{ return\; \}' src/cmd/go/internal/work/build_test.go
104     # Remove cert tests that conflict with NixOS's cert resolution
105     sed -i '/TestEnvVars/aif true \{ return\; \}' src/crypto/x509/root_unix_test.go
106     # TestWritevError hangs sometimes
107     sed -i '/TestWritevError/aif true \{ return\; \}' src/net/writev_test.go
108     # TestVariousDeadlines fails sometimes
109     sed -i '/TestVariousDeadlines/aif true \{ return\; \}' src/net/timeout_test.go
111     sed -i 's,/etc/protocols,${iana-etc}/etc/protocols,' src/net/lookup_unix.go
112     sed -i 's,/etc/services,${iana-etc}/etc/services,' src/net/port_unix.go
114     # Disable cgo lookup tests not works, they depend on resolver
115     rm src/net/cgo_unix_test.go
117   '' + optionalString stdenv.isLinux ''
118     # prepend the nix path to the zoneinfo files but also leave the original value for static binaries
119     # that run outside a nix server
120     sed -i 's,\"/usr/share/zoneinfo/,"${tzdata}/share/zoneinfo/\"\,\n\t&,' src/time/zoneinfo_unix.go
122   '' + optionalString stdenv.isAarch32 ''
123     echo '#!${runtimeShell}' > misc/cgo/testplugin/test.bash
124   '' + optionalString stdenv.isDarwin ''
125     substituteInPlace src/race.bash --replace \
126       "sysctl machdep.cpu.extfeatures | grep -qv EM64T" true
127     sed -i 's,strings.Contains(.*sysctl.*,true {,' src/cmd/dist/util.go
128     sed -i 's,"/etc","'"$TMPDIR"'",' src/os/os_test.go
129     sed -i 's,/_go_os_test,'"$TMPDIR"'/_go_os_test,' src/os/path_test.go
131     sed -i '/TestChdirAndGetwd/aif true \{ return\; \}' src/os/os_test.go
132     sed -i '/TestCredentialNoSetGroups/aif true \{ return\; \}' src/os/exec/exec_posix_test.go
133     sed -i '/TestRead0/aif true \{ return\; \}' src/os/os_test.go
134     sed -i '/TestSystemRoots/aif true \{ return\; \}' src/crypto/x509/root_darwin_test.go
136     sed -i '/TestGoInstallRebuildsStalePackagesInOtherGOPATH/aif true \{ return\; \}' src/cmd/go/go_test.go
137     sed -i '/TestBuildDashIInstallsDependencies/aif true \{ return\; \}' src/cmd/go/go_test.go
139     sed -i '/TestDisasmExtld/aif true \{ return\; \}' src/cmd/objdump/objdump_test.go
141     sed -i 's/unrecognized/unknown/' src/cmd/link/internal/ld/lib.go
143     # TestCurrent fails because Current is not implemented on Darwin
144     sed -i 's/TestCurrent/testCurrent/g' src/os/user/user_test.go
145     sed -i 's/TestLookup/testLookup/g' src/os/user/user_test.go
147     touch $TMPDIR/group $TMPDIR/hosts $TMPDIR/passwd
148   '';
150   patches = [
151     ./remove-tools-1.11.patch
152     ./ssl-cert-file-2-dev.patch
153     ./remove-test-pie-1.15.patch
154     ./creds-test.patch
155     ./go-1.9-skip-flaky-19608.patch
156     ./go-1.9-skip-flaky-20072.patch
157     ./skip-external-network-tests-1.16.patch
158     ./skip-nohup-tests.patch
159     ./skip-cgo-tests-1.15.patch
160   ] ++ [
161     # breaks under load: https://github.com/golang/go/issues/25628
162     (if stdenv.isAarch32
163     then ./skip-test-extra-files-on-aarch32-1.14.patch
164     else ./skip-test-extra-files-on-386-1.14.patch)
165   ];
167   postPatch = ''
168     find . -name '*.orig' -exec rm {} ';'
169   '';
171   GOOS = stdenv.targetPlatform.parsed.kernel.name;
172   GOARCH = goarch stdenv.targetPlatform;
173   # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
174   # Go will nevertheless build a for host system that we will copy over in
175   # the install phase.
176   GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
177   GOHOSTARCH = goarch stdenv.buildPlatform;
179   # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
180   # to be different from CC/CXX
181   CC_FOR_TARGET = if (stdenv.buildPlatform != stdenv.targetPlatform) then
182       "${targetCC}/bin/${targetCC.targetPrefix}cc"
183     else
184       null;
185   CXX_FOR_TARGET = if (stdenv.buildPlatform != stdenv.targetPlatform) then
186       "${targetCC}/bin/${targetCC.targetPrefix}c++"
187     else
188       null;
190   GOARM = toString (lib.intersectLists [(stdenv.hostPlatform.parsed.cpu.version or "")] ["5" "6" "7"]);
191   GO386 = "softfloat"; # from Arch: don't assume sse2 on i686
192   CGO_ENABLED = 1;
193   # Hopefully avoids test timeouts on Hydra
194   GO_TEST_TIMEOUT_SCALE = 3;
196   # Indicate that we are running on build infrastructure
197   # Some tests assume things like home directories and users exists
198   GO_BUILDER_NAME = "nix";
200   GOROOT_BOOTSTRAP="${goBootstrap}/share/go";
202   postConfigure = ''
203     export GOCACHE=$TMPDIR/go-cache
204     # this is compiled into the binary
205     export GOROOT_FINAL=$out/share/go
207     export PATH=$(pwd)/bin:$PATH
209     ${optionalString (stdenv.buildPlatform != stdenv.targetPlatform) ''
210     # Independent from host/target, CC should produce code for the building system.
211     # We only set it when cross-compiling.
212     export CC=${buildPackages.stdenv.cc}/bin/cc
213     ''}
214     ulimit -a
215   '';
217   postBuild = ''
218     (cd src && ./make.bash)
219   '';
221   doCheck = stdenv.hostPlatform == stdenv.targetPlatform && !stdenv.isDarwin;
223   checkPhase = ''
224     runHook preCheck
225     (cd src && HOME=$TMPDIR GOCACHE=$TMPDIR/go-cache ./run.bash --no-rebuild)
226     runHook postCheck
227   '';
229   preInstall = ''
230     rm -r pkg/obj
231     # Contains the wrong perl shebang when cross compiling,
232     # since it is not used for anything we can deleted as well.
233     rm src/regexp/syntax/make_perl_groups.pl
234   '' + (if (stdenv.buildPlatform != stdenv.hostPlatform) then ''
235     mv bin/*_*/* bin
236     rmdir bin/*_*
237     ${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
238       rm -rf pkg/${GOHOSTOS}_${GOHOSTARCH} pkg/tool/${GOHOSTOS}_${GOHOSTARCH}
239     ''}
240   '' else if (stdenv.hostPlatform != stdenv.targetPlatform) then ''
241     rm -rf bin/*_*
242     ${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
243       rm -rf pkg/${GOOS}_${GOARCH} pkg/tool/${GOOS}_${GOARCH}
244     ''}
245   '' else "");
247   installPhase = ''
248     runHook preInstall
249     mkdir -p $GOROOT_FINAL
250     cp -a bin pkg src lib misc api doc $GOROOT_FINAL
251     ln -s $GOROOT_FINAL/bin $out/bin
252     runHook postInstall
253   '';
255   disallowedReferences = [ goBootstrap ];
257   meta = with lib; {
258     homepage = "http://golang.org/";
259     description = "The Go Programming language";
260     license = licenses.bsd3;
261     maintainers = teams.golang.members ++ [ maintainers._3noch ];
262     platforms = platforms.linux ++ platforms.darwin;
263   };