nixpkgs/pkgs/development/compilers/tinygo/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

135 lines
3.7 KiB
Nix
Raw Normal View History

2022-01-27 14:01:52 +00:00
{ stdenv
, lib
, buildGoModule
, fetchFromGitHub
, makeWrapper
, llvmPackages
, go
, xar
, binaryen
, avrdude
, gdb
, openocd
2023-07-25 21:30:39 +00:00
, runCommand
2022-07-21 18:02:02 +00:00
, tinygoTests ? [ "smoketest" ]
2022-01-27 14:01:52 +00:00
}:
2019-12-08 07:00:34 +00:00
2022-01-27 14:01:52 +00:00
let
llvmMajor = lib.versions.major llvm.version;
inherit (llvmPackages) llvm clang compiler-rt lld;
2023-07-25 21:30:39 +00:00
# only doing this because only on darwin placing clang.cc in nativeBuildInputs
# doesn't build
2023-09-25 04:17:17 +00:00
bootstrapTools = runCommand "tinygo-bootstrap-tools" { } ''
2023-07-25 21:30:39 +00:00
mkdir -p $out
ln -s ${lib.getBin clang.cc}/bin/clang $out/clang-${llvmMajor}
'';
in
2022-01-27 14:01:52 +00:00
2019-12-08 07:00:34 +00:00
buildGoModule rec {
pname = "tinygo";
2024-10-26 17:21:07 +00:00
version = "0.34.0";
2019-12-08 07:00:34 +00:00
src = fetchFromGitHub {
owner = "tinygo-org";
repo = "tinygo";
rev = "v${version}";
2024-10-26 17:21:07 +00:00
hash = "sha256-jntixwHGg9buWQuu9I47DHUxK1xJbDRHxRMEwKoOhHo=";
fetchSubmodules = true;
# The public hydra server on `hydra.nixos.org` is configured with
# `max_output_size` of 3GB. The purpose of this `postFetch` step
# is to stay below that limit and save 4.1GiB and 428MiB in output
# size respectively. These folders are not referenced in tinygo.
postFetch = ''
rm -r $out/lib/cmsis-svd/data/{SiliconLabs,Freescale}
'';
2019-12-08 07:00:34 +00:00
};
2024-10-26 17:21:07 +00:00
vendorHash = "sha256-aY1gX++Dc5/G6VFXnP7sBdekk2IKHlenOC0erlB/Quw=";
2022-01-27 14:01:52 +00:00
patches = [
./0001-GNUmakefile.patch
2022-01-27 14:01:52 +00:00
];
2019-12-08 07:00:34 +00:00
2023-07-22 21:09:32 +00:00
nativeCheckInputs = [ binaryen ];
nativeBuildInputs = [ makeWrapper lld ];
2022-01-27 14:01:52 +00:00
buildInputs = [ llvm clang.cc ]
++ lib.optionals stdenv.hostPlatform.isDarwin [ xar ];
2022-07-21 18:02:02 +00:00
doCheck = (stdenv.buildPlatform.canExecute stdenv.hostPlatform);
inherit tinygoTests;
2022-01-27 14:01:52 +00:00
allowGoReference = true;
ldflags = [
"-X github.com/tinygo-org/tinygo/goenv.TINYGOROOT=${placeholder "out"}/share/tinygo"
"-X github.com/tinygo-org/tinygo/goenv.clangResourceDir=${clang.cc.lib}/lib/clang/${llvmMajor}"
];
2022-01-27 14:01:52 +00:00
subPackages = [ "." ];
2022-01-27 14:01:52 +00:00
# Output contains static libraries for different arm cpus
# and stripping could mess up these so only strip the compiler
stripDebugList = [ "bin" ];
2022-01-27 14:01:52 +00:00
postPatch = ''
# Borrow compiler-rt builtins from our source
# See https://github.com/tinygo-org/tinygo/pull/2471
mkdir -p lib/compiler-rt-builtins
cp -a ${compiler-rt.src}/compiler-rt/lib/builtins/* lib/compiler-rt-builtins/
substituteInPlace GNUmakefile \
2022-01-27 14:01:52 +00:00
--replace "build/release/tinygo/bin" "$out/bin" \
--replace "build/release/" "$out/share/"
'';
preBuild = ''
2023-07-25 21:30:39 +00:00
export PATH=${bootstrapTools}:$PATH
2022-07-21 18:02:02 +00:00
export HOME=$TMPDIR
2024-02-07 12:14:08 +00:00
ldflags=("''$ldflags[@]/\"-buildid=\"")
2019-12-08 07:00:34 +00:00
'';
2023-07-22 21:09:32 +00:00
postBuild = ''
2022-01-27 14:01:52 +00:00
# Move binary
mkdir -p build
mv $GOPATH/bin/tinygo build/tinygo
# Build our own custom wasi-libc.
# This is necessary because we modify the build a bit for our needs (disable
# heap, enable debug symbols, etc).
make wasi-libc \
CLANG="${lib.getBin clang.cc}/bin/clang -resource-dir ${clang.cc.lib}/lib/clang/${llvmMajor}" \
LLVM_AR=${lib.getBin llvm}/bin/llvm-ar \
LLVM_NM=${lib.getBin llvm}/bin/llvm-nm
2023-07-22 21:09:32 +00:00
make gen-device -j $NIX_BUILD_CORES
2022-07-21 18:02:02 +00:00
export TINYGOROOT=$(pwd)
2022-01-27 14:01:52 +00:00
'';
2022-07-21 18:02:02 +00:00
checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) ''
make ''${tinygoTests[@]} TINYGO="$(pwd)/build/tinygo" MD5SUM=md5sum XTENSA=0
2022-01-27 14:01:52 +00:00
'';
# GDB upstream does not support ARM darwin
runtimeDeps = [ go clang.cc lld avrdude openocd binaryen ]
++ lib.optionals (!(stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64)) [ gdb ];
2022-01-27 14:01:52 +00:00
installPhase = ''
runHook preInstall
2024-08-04 17:04:19 +00:00
make build/release USE_SYSTEM_BINARYEN=1
2022-01-27 14:01:52 +00:00
wrapProgram $out/bin/tinygo \
--prefix PATH : ${lib.makeBinPath runtimeDeps }
2022-01-27 14:01:52 +00:00
runHook postInstall
'';
2019-12-08 07:00:34 +00:00
meta = with lib; {
homepage = "https://tinygo.org/";
description = "Go compiler for small places";
license = licenses.bsd3;
2022-01-27 14:01:52 +00:00
maintainers = with maintainers; [ Madouura muscaln ];
2019-12-08 07:00:34 +00:00
};
}