summary refs log tree commit diff
path: root/pkgs/development/compilers/tinygo
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/development/compilers/tinygo')
-rw-r--r--pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch74
-rw-r--r--pkgs/development/compilers/tinygo/0004-fix-darwin-build.patch12
-rw-r--r--pkgs/development/compilers/tinygo/default.nix49
3 files changed, 57 insertions, 78 deletions
diff --git a/pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch b/pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch
index 7e4c8eded67..2c0bfba4f3b 100644
--- a/pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch
+++ b/pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch
@@ -1,55 +1,25 @@
-From 301b2d82cdbfaffe4dfba1d2cfed068a4115f730 Mon Sep 17 00:00:00 2001
-From: =?UTF-8?q?Mustafa=20=C3=87al=C4=B1=C5=9Fkan?= <muscaln@protonmail.com>
-Date: Sat, 30 Apr 2022 16:18:31 +0300
-Subject: [PATCH 2/3] Add clang header path
-
-
-diff --git a/builder/builtins.go b/builder/builtins.go
-index 121398fa..a589988b 100644
---- a/builder/builtins.go
-+++ b/builder/builtins.go
-@@ -170,7 +170,7 @@ var aeabiBuiltins = []string{
- var CompilerRT = Library{
- 	name: "compiler-rt",
- 	cflags: func(target, headerPath string) []string {
--		return []string{"-Werror", "-Wall", "-std=c11", "-nostdlibinc"}
-+		return []string{"-Werror", "-Wall", "-std=c11", "-isystem", "@clang_include@"}
- 	},
- 	sourceDir: func() string {
- 		llvmDir := filepath.Join(goenv.Get("TINYGOROOT"), "llvm-project/compiler-rt/lib/builtins")
-diff --git a/builder/picolibc.go b/builder/picolibc.go
-index d0786ee3..9a5cf9b0 100644
---- a/builder/picolibc.go
-+++ b/builder/picolibc.go
-@@ -30,7 +30,7 @@ var Picolibc = Library{
- 			"-D_IEEE_LIBM",
- 			"-D__OBSOLETE_MATH_FLOAT=1", // use old math code that doesn't expect a FPU
- 			"-D__OBSOLETE_MATH_DOUBLE=0",
--			"-nostdlibinc",
-+			"-isystem", "@clang_include@",
- 			"-isystem", newlibDir + "/libc/include",
- 			"-I" + newlibDir + "/libc/tinystdio",
- 			"-I" + newlibDir + "/libm/common",
+diff --git a/builder/library.go b/builder/library.go
+index 6517355b..b8de1894 100644
+--- a/builder/library.go
++++ b/builder/library.go
+@@ -142,7 +142,7 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
+ 	// Note: -fdebug-prefix-map is necessary to make the output archive
+ 	// reproducible. Otherwise the temporary directory is stored in the archive
+ 	// itself, which varies each run.
+-	args := append(l.cflags(target, headerPath), "-c", "-Oz", "-gdwarf-4", "-ffunction-sections", "-fdata-sections", "-Wno-macro-redefined", "--target="+target, "-fdebug-prefix-map="+dir+"="+remapDir)
++	args := append(l.cflags(target, headerPath), "-c", "-Oz", "-gdwarf-4", "-ffunction-sections", "-fdata-sections", "-Wno-macro-redefined", "--target="+target, "-fdebug-prefix-map="+dir+"="+remapDir, "-isystem", "@clang_include@")
+ 	cpu := config.CPU()
+ 	if cpu != "" {
+ 		// X86 has deprecated the -mcpu flag, so we need to use -march instead.
 diff --git a/compileopts/config.go b/compileopts/config.go
-index a006b673..3a105b49 100644
+index 39fc4f2a..8711b5a8 100644
 --- a/compileopts/config.go
 +++ b/compileopts/config.go
-@@ -279,6 +279,7 @@ func (c *Config) CFlags() []string {
- 		path, _ := c.LibcPath("picolibc")
- 		cflags = append(cflags,
- 			"--sysroot="+path,
-+			"-isystem", "@clang_include@",
- 			"-isystem", filepath.Join(path, "include"), // necessary for Xtensa
- 			"-isystem", filepath.Join(picolibcDir, "include"),
- 			"-isystem", filepath.Join(picolibcDir, "tinystdio"),
-@@ -288,7 +289,6 @@ func (c *Config) CFlags() []string {
- 		path, _ := c.LibcPath("musl")
- 		arch := MuslArchitecture(c.Triple())
- 		cflags = append(cflags,
--			"-nostdlibinc",
- 			"-isystem", filepath.Join(path, "include"),
- 			"-isystem", filepath.Join(root, "lib", "musl", "arch", arch),
- 			"-isystem", filepath.Join(root, "lib", "musl", "include"),
--- 
-2.37.2
-
+@@ -264,6 +264,7 @@ func (c *Config) CFlags() []string {
+ 	for _, flag := range c.Target.CFlags {
+ 		cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
+ 	}
++	cflags = append([]string{"-isystem", "@clang_include@"}, cflags...)
+ 	switch c.Target.Libc {
+ 	case "darwin-libSystem":
+ 		root := goenv.Get("TINYGOROOT")
diff --git a/pkgs/development/compilers/tinygo/0004-fix-darwin-build.patch b/pkgs/development/compilers/tinygo/0004-fix-darwin-build.patch
new file mode 100644
index 00000000000..924533a4913
--- /dev/null
+++ b/pkgs/development/compilers/tinygo/0004-fix-darwin-build.patch
@@ -0,0 +1,12 @@
+diff --git a/compileopts/config.go b/compileopts/config.go
+index 39fc4f2a..fb5d4575 100644
+--- a/compileopts/config.go
++++ b/compileopts/config.go
+@@ -269,6 +269,7 @@ func (c *Config) CFlags() []string {
+ 		root := goenv.Get("TINYGOROOT")
+ 		cflags = append(cflags,
+ 			"--sysroot="+filepath.Join(root, "lib/macos-minimal-sdk/src"),
++			"-isystem", filepath.Join(root, "lib/macos-minimal-sdk/src/usr/include"), // necessary for Nix
+ 		)
+ 	case "picolibc":
+ 		root := goenv.Get("TINYGOROOT")
diff --git a/pkgs/development/compilers/tinygo/default.nix b/pkgs/development/compilers/tinygo/default.nix
index 6e59699ea9d..5901ab7f960 100644
--- a/pkgs/development/compilers/tinygo/default.nix
+++ b/pkgs/development/compilers/tinygo/default.nix
@@ -13,7 +13,6 @@
 , libxml2
 , xar
 , wasi-libc
-, avrgcc
 , binaryen
 , avrdude
 , gdb
@@ -33,37 +32,48 @@ let
     ln -s ${lib.getBin clang.cc}/bin/clang $out/clang-${llvmMajor}
     ln -s ${lib.getBin lld}/bin/ld.lld $out/ld.lld-${llvmMajor}
     ln -s ${lib.getBin lld}/bin/wasm-ld $out/wasm-ld-${llvmMajor}
-    ln -s ${gdb}/bin/gdb $out/gdb-multiarch
+    # GDB upstream does not support ARM darwin
+    ${lib.optionalString (!(stdenv.isDarwin && stdenv.isAarch64)) "ln -s ${gdb}/bin/gdb $out/gdb-multiarch" }
   '';
 in
 
 buildGoModule rec {
   pname = "tinygo";
-  version = "0.26.0";
+  version = "0.30.0";
 
   src = fetchFromGitHub {
     owner = "tinygo-org";
     repo = "tinygo";
     rev = "v${version}";
-    sha256 = "rI8CADPWKdNvfknEsrpp2pCeZobf9fAp0GDIWjupzZA=";
+    sha256 = "sha256-hOccfMKuvTKYKDRcEgTJ8k/c/H+qNDpvotWIqk6p2u8=";
     fetchSubmodules = true;
   };
 
-  vendorHash = "sha256-ihQd/RAjAQhgQZHbNiWmAD0eOo1MvqAR/OwIOUWtdAM=";
+  vendorHash = "sha256-2q3N6QhfRmwbs4CTWrFWr1wyhf2jPS2ECAn/wrrpXdM=";
 
   patches = [
     ./0001-Makefile.patch
 
+    # clang.cc does not have any paths in the include path.
+    # For TinyGo, we want to have no include paths, _except_ for the built-in
+    # Clang header files (things like stdint.h). That's why we use -nostdlibinc.
+    # So to make Clang work like we want, we will have to manually add this one
+    # include path.
+    # We can't use a regular clang command (something like
+    # llvmPackages.clangUseLLVM) because there are various bugs, see:
+    # https://github.com/NixOS/nixpkgs/issues/259397
+    # https://github.com/NixOS/nixpkgs/issues/259386
     (substituteAll {
       src = ./0002-Add-clang-header-path.patch;
-      clang_include = "${clang.cc.lib}/lib/clang/${clang.cc.version}/include";
+      clang_include = "${clang.cc.lib}/lib/clang/${llvmMajor}/include";
     })
 
     #TODO(muscaln): Find a better way to fix build ID on darwin
     ./0003-Use-out-path-as-build-id-on-darwin.patch
+    ./0004-fix-darwin-build.patch
   ];
 
-  nativeCheckInputs = [ avrgcc binaryen ];
+  nativeCheckInputs = [ binaryen ];
   nativeBuildInputs = [ makeWrapper ];
   buildInputs = [ llvm clang.cc ]
     ++ lib.optionals stdenv.isDarwin [ zlib ncurses libffi libxml2 xar ];
@@ -107,10 +117,9 @@ buildGoModule rec {
     substituteInPlace builder/buildid.go \
       --replace "OUT_PATH" "$out"
 
-    # TODO: Fix mingw and darwin
-    # Disable windows and darwin cross-compile tests
+    # TODO: Fix mingw
+    # Disable windows cross-compile tests
     sed -i "/GOOS=windows/d" Makefile
-    sed -i "/GOOS=darwin/d" Makefile
   '' + lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) ''
     substituteInPlace Makefile \
       --replace "./build/tinygo" "${buildPackages.tinygo}/bin/tinygo"
@@ -121,30 +130,18 @@ buildGoModule rec {
     export HOME=$TMPDIR
   '';
 
-  postBuild = let
-    tinygoForBuild = if (stdenv.buildPlatform.canExecute stdenv.hostPlatform)
-      then "build/tinygo"
-      else "${buildPackages.tinygo}/bin/tinygo";
-    in ''
+  postBuild = ''
     # Move binary
     mkdir -p build
     mv $GOPATH/bin/tinygo build/tinygo
 
-    make gen-device
+    make gen-device -j $NIX_BUILD_CORES
 
     export TINYGOROOT=$(pwd)
-    finalRoot=$out/share/tinygo
-
-    for target in thumbv6m-unknown-unknown-eabi-cortex-m0 thumbv6m-unknown-unknown-eabi-cortex-m0plus thumbv7em-unknown-unknown-eabi-cortex-m4; do
-      mkdir -p $finalRoot/pkg/$target
-      for lib in compiler-rt picolibc; do
-        ${tinygoForBuild} build-library -target=''${target#*eabi-} -o $finalRoot/pkg/$target/$lib $lib
-      done
-    done
   '';
 
   checkPhase = lib.optionalString (tinygoTests != [ ] && tinygoTests != null) ''
-    make ''${tinygoTests[@]} XTENSA=0 ${lib.optionalString stdenv.isDarwin "AVR=0"}
+    make ''${tinygoTests[@]} XTENSA=0
   '';
 
   installPhase = ''
@@ -153,7 +150,7 @@ buildGoModule rec {
     make build/release
 
     wrapProgram $out/bin/tinygo \
-      --prefix PATH : ${lib.makeBinPath [ go avrdude openocd avrgcc binaryen ]}:${bootstrapTools}
+      --prefix PATH : ${lib.makeBinPath [ go avrdude openocd binaryen ]}:${bootstrapTools}
 
     runHook postInstall
   '';