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