nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 stdenv,
3 lib,
4 buildGoModule,
5 fetchFromGitHub,
6 makeWrapper,
7 llvmPackages,
8 go,
9 xar,
10 binaryen,
11 avrdude,
12 gdb,
13 openocd,
14 runCommand,
15 tinygoTests ? [ "smoketest" ],
16}:
17
18let
19 llvmMajor = lib.versions.major llvm.version;
20 inherit (llvmPackages)
21 llvm
22 clang
23 compiler-rt
24 lld
25 ;
26
27 # only doing this because only on darwin placing clang.cc in nativeBuildInputs
28 # doesn't build
29 bootstrapTools = runCommand "tinygo-bootstrap-tools" { } ''
30 mkdir -p $out
31 ln -s ${lib.getBin clang.cc}/bin/clang $out/clang-${llvmMajor}
32 '';
33in
34
35buildGoModule rec {
36 pname = "tinygo";
37 version = "0.37.0";
38
39 src = fetchFromGitHub {
40 owner = "tinygo-org";
41 repo = "tinygo";
42 rev = "v${version}";
43 hash = "sha256-I/9JXjt6aF/80Mh3iRgUYXv4l+m3XIpmKsIBviOuWCo=";
44 fetchSubmodules = true;
45 # The public hydra server on `hydra.nixos.org` is configured with
46 # `max_output_size` of 3GB. The purpose of this `postFetch` step
47 # is to stay below that limit and save 4.1GiB and 428MiB in output
48 # size respectively. These folders are not referenced in tinygo.
49 postFetch = ''
50 rm -r $out/lib/cmsis-svd/data/{SiliconLabs,Freescale}
51 '';
52 };
53
54 vendorHash = "sha256-juADakh+s8oEY9UXUwxknvVeL1TgB/zRi8Xtzt/4qPA=";
55
56 patches = [
57 ./0001-GNUmakefile.patch
58 ];
59
60 nativeCheckInputs = [ binaryen ];
61 nativeBuildInputs = [
62 makeWrapper
63 lld
64 ];
65 buildInputs = [
66 llvm
67 clang.cc
68 ]
69 ++ lib.optionals stdenv.hostPlatform.isDarwin [ xar ];
70
71 doCheck = (stdenv.buildPlatform.canExecute stdenv.hostPlatform);
72 inherit tinygoTests;
73
74 allowGoReference = true;
75 ldflags = [
76 "-X github.com/tinygo-org/tinygo/goenv.TINYGOROOT=${placeholder "out"}/share/tinygo"
77 "-X github.com/tinygo-org/tinygo/goenv.clangResourceDir=${clang.cc.lib}/lib/clang/${llvmMajor}"
78 ];
79 subPackages = [ "." ];
80
81 # Output contains static libraries for different arm cpus
82 # and stripping could mess up these so only strip the compiler
83 stripDebugList = [ "bin" ];
84
85 postPatch = ''
86 # Borrow compiler-rt builtins from our source
87 # See https://github.com/tinygo-org/tinygo/pull/2471
88 mkdir -p lib/compiler-rt-builtins
89 cp -a ${compiler-rt.src}/compiler-rt/lib/builtins/* lib/compiler-rt-builtins/
90
91 substituteInPlace GNUmakefile \
92 --replace "build/release/tinygo/bin" "$out/bin" \
93 --replace "build/release/" "$out/share/"
94 '';
95
96 preBuild = ''
97 export PATH=${bootstrapTools}:$PATH
98 export HOME=$TMPDIR
99
100 ldflags=("''$ldflags[@]/\"-buildid=\"")
101 '';
102
103 postBuild = ''
104 # Move binary
105 mkdir -p build
106 mv $GOPATH/bin/tinygo build/tinygo
107
108 # Build our own custom wasi-libc.
109 # This is necessary because we modify the build a bit for our needs (disable
110 # heap, enable debug symbols, etc).
111 make wasi-libc \
112 CLANG="${lib.getBin clang.cc}/bin/clang -resource-dir ${clang.cc.lib}/lib/clang/${llvmMajor}" \
113 LLVM_AR=${lib.getBin llvm}/bin/llvm-ar \
114 LLVM_NM=${lib.getBin llvm}/bin/llvm-nm
115
116 make gen-device -j $NIX_BUILD_CORES
117
118 export TINYGOROOT=$(pwd)
119 '';
120
121 checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) ''
122 make ''${tinygoTests[@]} TINYGO="$(pwd)/build/tinygo" MD5SUM=md5sum XTENSA=0
123 '';
124
125 # GDB upstream does not support ARM darwin
126 runtimeDeps = [
127 go
128 clang.cc
129 lld
130 avrdude
131 openocd
132 binaryen
133 ]
134 ++ lib.optionals (!(stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64)) [ gdb ];
135
136 installPhase = ''
137 runHook preInstall
138
139 make build/release USE_SYSTEM_BINARYEN=1
140
141 wrapProgram $out/bin/tinygo \
142 --prefix PATH : ${lib.makeBinPath runtimeDeps}
143
144 runHook postInstall
145 '';
146
147 meta = with lib; {
148 homepage = "https://tinygo.org/";
149 description = "Go compiler for small places";
150 license = licenses.bsd3;
151 maintainers = with maintainers; [
152 Madouura
153 muscaln
154 ];
155 };
156}