Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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" ] 22}: 23 24let 25 llvmMajor = lib.versions.major llvm.version; 26 inherit (llvmPackages) llvm clang compiler-rt lld; 27in 28 29buildGoModule rec { 30 pname = "tinygo"; 31 version = "0.26.0"; 32 33 src = fetchFromGitHub { 34 owner = "tinygo-org"; 35 repo = "tinygo"; 36 rev = "v${version}"; 37 sha256 = "rI8CADPWKdNvfknEsrpp2pCeZobf9fAp0GDIWjupzZA="; 38 fetchSubmodules = true; 39 }; 40 41 vendorSha256 = "sha256-ihQd/RAjAQhgQZHbNiWmAD0eOo1MvqAR/OwIOUWtdAM="; 42 43 patches = [ 44 ./0001-Makefile.patch 45 46 (substituteAll { 47 src = ./0002-Add-clang-header-path.patch; 48 clang_include = "${clang.cc.lib}/lib/clang/${clang.cc.version}/include"; 49 }) 50 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 ]; 54 55 nativeCheckInputs = [ avrgcc binaryen ]; 56 nativeBuildInputs = [ makeWrapper ]; 57 buildInputs = [ llvm clang.cc ] 58 ++ lib.optionals stdenv.isDarwin [ zlib ncurses libffi libxml2 xar ]; 59 60 doCheck = (stdenv.buildPlatform.canExecute stdenv.hostPlatform); 61 inherit tinygoTests; 62 63 allowGoReference = true; 64 tags = [ "llvm${llvmMajor}" ]; 65 ldflags = [ "-X github.com/tinygo-org/tinygo/goenv.TINYGOROOT=${placeholder "out"}/share/tinygo" ]; 66 subPackages = [ "." ]; 67 68 # Output contains static libraries for different arm cpus 69 # and stripping could mess up these so only strip the compiler 70 stripDebugList = [ "bin" ]; 71 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 ''; 77 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/ 84 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/ 89 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/" 95 96 substituteInPlace builder/buildid.go \ 97 --replace "OUT_PATH" "$out" 98 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 103 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 ''; 114 115 preBuild = '' 116 export PATH=$out/libexec/tinygo:$PATH 117 export HOME=$TMPDIR 118 ''; 119 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 128 129 make gen-device 130 131 export TINYGOROOT=$(pwd) 132 finalRoot=$out/share/tinygo 133 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 ''; 141 142 checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) '' 143 make ''${tinygoTests[@]} XTENSA=0 ${lib.optionalString stdenv.isDarwin "AVR=0"} 144 ''; 145 146 installPhase = '' 147 runHook preInstall 148 149 make build/release 150 151 wrapProgram $out/bin/tinygo \ 152 --prefix PATH : ${lib.makeBinPath [ go avrdude openocd avrgcc binaryen ]}:$out/libexec/tinygo 153 154 runHook postInstall 155 ''; 156 157 disallowedReferences = [ wasi-libc ]; 158 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 }; 165}