nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ stdenv
2, lib
3, buildPackages
4, buildGoModule
5, fetchFromGitHub
6, makeWrapper
7, substituteAll
8, llvmPackages
9, go
10, libffi
11, zlib
12, ncurses
13, libxml2
14, xar
15, wasi-libc
16, avrgcc
17, binaryen
18, avrdude
19, gdb
20, openocd
21}:
22
23let
24 llvmMajor = lib.versions.major llvm.version;
25 inherit (llvmPackages) llvm clang compiler-rt lld;
26in
27
28buildGoModule rec {
29 pname = "tinygo";
30 version = "0.23.0";
31
32 src = fetchFromGitHub {
33 owner = "tinygo-org";
34 repo = "tinygo";
35 rev = "v${version}";
36 sha256 = "sha256-YgQGAQJw9Xyw5BF2d9uZTQHfjHsu2evZGo4RV9DtStE=";
37 fetchSubmodules = true;
38 };
39
40 vendorSha256 = "sha256-fK8BlCh+1NtHW6MwW68iSIB+Sw6AK+g3y4lMyMYrXkk=";
41
42 patches = [
43 ./0001-Makefile.patch
44
45 (substituteAll {
46 src = ./0002-Add-clang-header-path.patch;
47 clang_include = "${clang.cc.lib}/lib/clang/${clang.cc.version}/include";
48 })
49 ];
50
51 checkInputs = [ avrgcc binaryen ];
52 nativeBuildInputs = [ go makeWrapper ];
53 buildInputs = [ llvm clang.cc ]
54 ++ lib.optionals stdenv.isDarwin [ zlib ncurses libffi libxml2 xar ];
55
56 doCheck = stdenv.buildPlatform == stdenv.hostPlatform;
57
58 allowGoReference = true;
59 tags = [ "llvm${llvmMajor}" ];
60 subPackages = [ "." ];
61
62 # Output contains static libraries for different arm cpus
63 # and stripping could mess up these so only strip the compiler
64 stripDebugList = [ "bin" ];
65
66 postConfigure = lib.optionalString stdenv.isDarwin ''
67 for i in vendor/tinygo.org/x/go-llvm/llvm_config_darwin*; do
68 substituteInPlace $i --replace "curses" "ncurses"
69 done
70 '';
71
72 postPatch = ''
73 # Copy wasi-libc, symlink seems not working
74 rm -rf lib/wasi-libc/*
75 mkdir -p lib/wasi-libc/sysroot/lib/wasm32-wasi lib/wasi-libc/sysroot/include
76 cp -a ${wasi-libc}/lib/* lib/wasi-libc/sysroot/lib/wasm32-wasi/
77 cp -a ${wasi-libc.dev}/include/* lib/wasi-libc/sysroot/include/
78
79 # Borrow compiler-rt builtins from our source
80 # See https://github.com/tinygo-org/tinygo/pull/2471
81 mkdir -p lib/compiler-rt-builtins
82 cp -a ${compiler-rt.src}/compiler-rt/lib/builtins/* lib/compiler-rt-builtins/
83
84 substituteInPlace Makefile \
85 --replace "\$(TINYGO)" "$(pwd)/build/tinygo" \
86 --replace "build/release/tinygo/bin" "$out/bin" \
87 --replace "build/release/" "$out/share/"
88
89 # TODO: Fix mingw and darwin
90 # Disable windows and darwin cross-compile tests
91 sed -i "/GOOS=windows/d" Makefile
92 sed -i "/GOOS=darwin/d" Makefile
93
94 # tinygo needs versioned binaries
95 mkdir -p $out/libexec/tinygo
96 ln -s ${lib.getBin clang.cc}/bin/clang $out/libexec/tinygo/clang-${llvmMajor}
97 ln -s ${lib.getBin lld}/bin/ld.lld $out/libexec/tinygo/ld.lld-${llvmMajor}
98 ln -s ${lib.getBin lld}/bin/wasm-ld $out/libexec/tinygo/wasm-ld-${llvmMajor}
99 ln -s ${gdb}/bin/gdb $out/libexec/tinygo/gdb-multiarch
100 '' + lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) ''
101 substituteInPlace Makefile \
102 --replace "./build/tinygo" "${buildPackages.tinygo}/bin/tinygo"
103 '';
104
105 preBuild = ''
106 export HOME=$TMPDIR
107 export GOCACHE=$TMPDIR/go-cache
108 export GOPATH=$TMPDIR/go
109 export PATH=$out/libexec/tinygo:$PATH
110 '';
111
112 postBuild = ''
113 # Move binary
114 mkdir -p build
115 mv $GOPATH/bin/tinygo build/tinygo
116
117 make gen-device
118 '';
119
120 checkPhase = ''
121 runHook preCheck
122 make smoketest XTENSA=0
123 runHook postCheck
124 '';
125
126 installPhase = ''
127 runHook preInstall
128
129 make build/release
130
131 wrapProgram $out/bin/tinygo \
132 --set TINYGOROOT $out/share/tinygo \
133 --prefix PATH : ${lib.makeBinPath [ go avrdude openocd avrgcc binaryen ]}:$out/libexec/tinygo
134
135 runHook postInstall
136 '';
137
138 disallowedReferences = [ wasi-libc ];
139
140 meta = with lib; {
141 homepage = "https://tinygo.org/";
142 description = "Go compiler for small places";
143 license = licenses.bsd3;
144 maintainers = with maintainers; [ Madouura muscaln ];
145 broken = stdenv.isDarwin;
146 };
147}