21 , tinygoTests ? [ "smoketest" ]
25 llvmMajor = lib.versions.major llvm.version;
26 inherit (llvmPackages) llvm clang compiler-rt lld;
33 src = fetchFromGitHub {
37 sha256 = "sha256-Rxdxum1UIaz8tpEAGqpLvKd25nHdj4Se+IoN29EJEHg=";
38 fetchSubmodules = true;
41 vendorSha256 = "sha256-QxLY4KT05PtA/W7d1vKxsq5w35YZ6MJL3Lh726b+E9w=";
47 src = ./0002-Add-clang-header-path.patch;
48 clang_include = "${clang.cc.lib}/lib/clang/${clang.cc.version}/include";
51 #TODO(muscaln): Find a better way to fix build ID on darwin
52 ./0003-Use-out-path-as-build-id-on-darwin.patch
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);
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"
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"
116 export PATH=$out/libexec/tinygo:$PATH
121 tinygoForBuild = if (stdenv.buildPlatform.canExecute stdenv.hostPlatform)
123 else "${buildPackages.tinygo}/bin/tinygo";
127 mv $GOPATH/bin/tinygo build/tinygo
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
142 checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) ''
143 make ''${tinygoTests[@]} XTENSA=0 ${lib.optionalString stdenv.isDarwin "AVR=0"}
151 wrapProgram $out/bin/tinygo \
152 --prefix PATH : ${lib.makeBinPath [ go avrdude openocd avrgcc binaryen ]}:$out/libexec/tinygo
157 disallowedReferences = [ wasi-libc ];
160 homepage = "https://tinygo.org/";
161 description = "Go compiler for small places";
162 license = licenses.bsd3;
163 maintainers = with maintainers; [ Madouura muscaln ];