2022-06-16 17:23:12 +00:00
|
|
|
{ stdenv
|
|
|
|
, lib
|
|
|
|
, buildPackages
|
|
|
|
, buildGoModule
|
|
|
|
, fetchFromGitHub
|
|
|
|
, makeWrapper
|
|
|
|
, llvmPackages
|
|
|
|
, go
|
|
|
|
, xar
|
|
|
|
, binaryen
|
|
|
|
, avrdude
|
|
|
|
, gdb
|
|
|
|
, openocd
|
2023-08-22 20:05:09 +00:00
|
|
|
, runCommand
|
2022-10-06 18:32:54 +00:00
|
|
|
, tinygoTests ? [ "smoketest" ]
|
2022-06-16 17:23:12 +00:00
|
|
|
}:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
let
|
|
|
|
llvmMajor = lib.versions.major llvm.version;
|
|
|
|
inherit (llvmPackages) llvm clang compiler-rt lld;
|
2023-08-22 20:05:09 +00:00
|
|
|
|
|
|
|
# only doing this because only on darwin placing clang.cc in nativeBuildInputs
|
|
|
|
# doesn't build
|
2024-04-21 15:54:59 +00:00
|
|
|
bootstrapTools = runCommand "tinygo-bootstrap-tools" { } ''
|
2023-08-22 20:05:09 +00:00
|
|
|
mkdir -p $out
|
|
|
|
ln -s ${lib.getBin clang.cc}/bin/clang $out/clang-${llvmMajor}
|
|
|
|
'';
|
2020-05-15 21:57:56 +00:00
|
|
|
in
|
2022-06-16 17:23:12 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
buildGoModule rec {
|
|
|
|
pname = "tinygo";
|
2024-04-21 15:54:59 +00:00
|
|
|
version = "0.31.2";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "tinygo-org";
|
|
|
|
repo = "tinygo";
|
|
|
|
rev = "v${version}";
|
2024-04-21 15:54:59 +00:00
|
|
|
sha256 = "sha256-e0zXxIdAtJZXJdP/S6lHRnPm5Rsf638Fhox8XcqOWrk=";
|
2021-01-05 17:05:55 +00:00
|
|
|
fetchSubmodules = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
vendorHash = "sha256-HZiyAgsTEBQv+Qp0T9RXTV1lkxvIGh7Q45rd45cfvjo=";
|
2022-06-16 17:23:12 +00:00
|
|
|
|
|
|
|
patches = [
|
2024-04-21 15:54:59 +00:00
|
|
|
./0001-GNUmakefile.patch
|
2022-06-16 17:23:12 +00:00
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-19 13:55:26 +00:00
|
|
|
nativeCheckInputs = [ binaryen ];
|
2024-04-21 15:54:59 +00:00
|
|
|
nativeBuildInputs = [ makeWrapper lld ];
|
2022-06-16 17:23:12 +00:00
|
|
|
buildInputs = [ llvm clang.cc ]
|
2024-04-21 15:54:59 +00:00
|
|
|
++ lib.optionals stdenv.isDarwin [ xar ];
|
2020-05-15 21:57:56 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
doCheck = (stdenv.buildPlatform.canExecute stdenv.hostPlatform);
|
|
|
|
inherit tinygoTests;
|
2021-01-05 17:05:55 +00:00
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
allowGoReference = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
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-06-16 17:23:12 +00:00
|
|
|
subPackages = [ "." ];
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
# Output contains static libraries for different arm cpus
|
|
|
|
# and stripping could mess up these so only strip the compiler
|
|
|
|
stripDebugList = [ "bin" ];
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2022-06-16 17:23:12 +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/
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
substituteInPlace GNUmakefile \
|
2022-06-16 17:23:12 +00:00
|
|
|
--replace "build/release/tinygo/bin" "$out/bin" \
|
|
|
|
--replace "build/release/" "$out/share/"
|
|
|
|
'';
|
|
|
|
|
|
|
|
preBuild = ''
|
2023-08-22 20:05:09 +00:00
|
|
|
export PATH=${bootstrapTools}:$PATH
|
2022-10-06 18:32:54 +00:00
|
|
|
export HOME=$TMPDIR
|
2024-04-21 15:54:59 +00:00
|
|
|
|
|
|
|
ldflags=("''$ldflags[@]/\"-buildid=\"")
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2023-10-19 13:55:26 +00:00
|
|
|
postBuild = ''
|
2022-06-16 17:23:12 +00:00
|
|
|
# Move binary
|
|
|
|
mkdir -p build
|
|
|
|
mv $GOPATH/bin/tinygo build/tinygo
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
# 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-10-19 13:55:26 +00:00
|
|
|
make gen-device -j $NIX_BUILD_CORES
|
2022-10-06 18:32:54 +00:00
|
|
|
|
|
|
|
export TINYGOROOT=$(pwd)
|
2022-06-16 17:23:12 +00:00
|
|
|
'';
|
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) ''
|
2024-04-21 15:54:59 +00:00
|
|
|
make ''${tinygoTests[@]} TINYGO="$(pwd)/build/tinygo" MD5SUM=md5sum XTENSA=0
|
2022-06-16 17:23:12 +00:00
|
|
|
'';
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
# GDB upstream does not support ARM darwin
|
|
|
|
runtimeDeps = [ go clang.cc lld avrdude openocd binaryen ]
|
|
|
|
++ lib.optionals (!(stdenv.isDarwin && stdenv.isAarch64)) [ gdb ];
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
|
|
|
|
make build/release
|
|
|
|
|
|
|
|
wrapProgram $out/bin/tinygo \
|
2024-04-21 15:54:59 +00:00
|
|
|
--prefix PATH : ${lib.makeBinPath runtimeDeps }
|
2022-06-16 17:23:12 +00:00
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
meta = with lib; {
|
|
|
|
homepage = "https://tinygo.org/";
|
|
|
|
description = "Go compiler for small places";
|
|
|
|
license = licenses.bsd3;
|
2022-06-16 17:23:12 +00:00
|
|
|
maintainers = with maintainers; [ Madouura muscaln ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|