openvswitch: generalize builder
[NixPkgs.git] / pkgs / development / compilers / tinygo / default.nix
blob43b981ffd068b0feaef1873f3648cad3b492186e
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 , tinygoTests ? [ "smoketest" ]
24 let
25   llvmMajor = lib.versions.major llvm.version;
26   inherit (llvmPackages) llvm clang compiler-rt lld;
29 buildGoModule rec {
30   pname = "tinygo";
31   version = "0.25.0";
33   src = fetchFromGitHub {
34     owner = "tinygo-org";
35     repo = "tinygo";
36     rev = "v${version}";
37     sha256 = "sha256-Rxdxum1UIaz8tpEAGqpLvKd25nHdj4Se+IoN29EJEHg=";
38     fetchSubmodules = true;
39   };
41   vendorSha256 = "sha256-QxLY4KT05PtA/W7d1vKxsq5w35YZ6MJL3Lh726b+E9w=";
43   patches = [
44     ./0001-Makefile.patch
46     (substituteAll {
47       src = ./0002-Add-clang-header-path.patch;
48       clang_include = "${clang.cc.lib}/lib/clang/${clang.cc.version}/include";
49     })
51     #TODO(muscaln): Find a better way to fix build ID on darwin
52     ./0003-Use-out-path-as-build-id-on-darwin.patch
53   ];
55   checkInputs = [ avrgcc binaryen ];
56   nativeBuildInputs = [ makeWrapper ];
57   buildInputs = [ llvm clang.cc ]
58     ++ lib.optionals stdenv.isDarwin [ zlib ncurses libffi libxml2 xar ];
60   doCheck = (stdenv.buildPlatform.canExecute stdenv.hostPlatform);
61   inherit tinygoTests;
63   allowGoReference = true;
64   tags = [ "llvm${llvmMajor}" ];
65   ldflags = [ "-X github.com/tinygo-org/tinygo/goenv.TINYGOROOT=${placeholder "out"}/share/tinygo" ];
66   subPackages = [ "." ];
68   # Output contains static libraries for different arm cpus
69   # and stripping could mess up these so only strip the compiler
70   stripDebugList = [ "bin" ];
72   postConfigure = lib.optionalString stdenv.isDarwin ''
73     for i in vendor/tinygo.org/x/go-llvm/llvm_config_darwin*; do
74       substituteInPlace $i --replace "curses" "ncurses"
75     done
76   '';
78   postPatch = ''
79     # Copy wasi-libc, symlink seems not working
80     rm -rf lib/wasi-libc/*
81     mkdir -p lib/wasi-libc/sysroot/lib/wasm32-wasi lib/wasi-libc/sysroot/include
82     cp -a ${wasi-libc}/lib/* lib/wasi-libc/sysroot/lib/wasm32-wasi/
83     cp -a ${wasi-libc.dev}/include/* lib/wasi-libc/sysroot/include/
85     # Borrow compiler-rt builtins from our source
86     # See https://github.com/tinygo-org/tinygo/pull/2471
87     mkdir -p lib/compiler-rt-builtins
88     cp -a ${compiler-rt.src}/compiler-rt/lib/builtins/* lib/compiler-rt-builtins/
90     substituteInPlace Makefile \
91       --replace "\$(TINYGO)" "$(pwd)/build/tinygo" \
92       --replace "@\$(MD5SUM)" "md5sum" \
93       --replace "build/release/tinygo/bin" "$out/bin" \
94       --replace "build/release/" "$out/share/"
96     substituteInPlace builder/buildid.go \
97       --replace "OUT_PATH" "$out"
99     # TODO: Fix mingw and darwin
100     # Disable windows and darwin cross-compile tests
101     sed -i "/GOOS=windows/d" Makefile
102     sed -i "/GOOS=darwin/d" Makefile
104     # tinygo needs versioned binaries
105     mkdir -p $out/libexec/tinygo
106     ln -s ${lib.getBin clang.cc}/bin/clang $out/libexec/tinygo/clang-${llvmMajor}
107     ln -s ${lib.getBin lld}/bin/ld.lld $out/libexec/tinygo/ld.lld-${llvmMajor}
108     ln -s ${lib.getBin lld}/bin/wasm-ld $out/libexec/tinygo/wasm-ld-${llvmMajor}
109     ln -s ${gdb}/bin/gdb $out/libexec/tinygo/gdb-multiarch
110   '' + lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) ''
111     substituteInPlace Makefile \
112       --replace "./build/tinygo" "${buildPackages.tinygo}/bin/tinygo"
113   '';
115   preBuild = ''
116     export PATH=$out/libexec/tinygo:$PATH
117     export HOME=$TMPDIR
118   '';
120   postBuild = let
121     tinygoForBuild = if (stdenv.buildPlatform.canExecute stdenv.hostPlatform)
122       then "build/tinygo"
123       else "${buildPackages.tinygo}/bin/tinygo";
124     in ''
125     # Move binary
126     mkdir -p build
127     mv $GOPATH/bin/tinygo build/tinygo
129     make gen-device
131     export TINYGOROOT=$(pwd)
132     finalRoot=$out/share/tinygo
134     for target in thumbv6m-unknown-unknown-eabi-cortex-m0 thumbv6m-unknown-unknown-eabi-cortex-m0plus thumbv7em-unknown-unknown-eabi-cortex-m4; do
135       mkdir -p $finalRoot/pkg/$target
136       for lib in compiler-rt picolibc; do
137         ${tinygoForBuild} build-library -target=''${target#*eabi-} -o $finalRoot/pkg/$target/$lib $lib
138       done
139     done
140   '';
142   checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) ''
143     make ''${tinygoTests[@]} XTENSA=0 ${lib.optionalString stdenv.isDarwin "AVR=0"}
144   '';
146   installPhase = ''
147     runHook preInstall
149     make build/release
151     wrapProgram $out/bin/tinygo \
152       --prefix PATH : ${lib.makeBinPath [ go avrdude openocd avrgcc binaryen ]}:$out/libexec/tinygo
154     runHook postInstall
155   '';
157   disallowedReferences = [ wasi-libc ];
159   meta = with lib; {
160     homepage = "https://tinygo.org/";
161     description = "Go compiler for small places";
162     license = licenses.bsd3;
163     maintainers = with maintainers; [ Madouura muscaln ];
164   };