From 993efa596bdece03c8898ae444bf4ab87b4a42f8 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 11 Oct 2023 16:11:04 +0200 Subject: [PATCH] tinygo: clean up Clang header path patch See the comment in default.nix for details. But basically, there are only two place that needs a modification to set the correct path. As a side effect, this makes cross compiling CGo code to Darwin work. --- .../tinygo/0002-Add-clang-header-path.patch | 65 +++++++------------ pkgs/development/compilers/tinygo/default.nix | 14 +++- 2 files changed, 33 insertions(+), 46 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 1cf15a219fa2..2c0bfba4f3b5 100644 --- a/pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch +++ b/pkgs/development/compilers/tinygo/0002-Add-clang-header-path.patch @@ -1,46 +1,25 @@ -diff --git a/builder/builtins.go b/builder/builtins.go -index a1066b67..f4f8ca79 100644 ---- a/builder/builtins.go -+++ b/builder/builtins.go -@@ -179,7 +179,7 @@ var avrBuiltins = []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 1b7c748b..8a6b9ddd 100644 ---- a/builder/picolibc.go -+++ b/builder/picolibc.go -@@ -32,7 +32,7 @@ var Picolibc = Library{ - "-D__OBSOLETE_MATH_FLOAT=1", // use old math code that doesn't expect a FPU - "-D__OBSOLETE_MATH_DOUBLE=0", - "-D_WANT_IO_C99_FORMATS", -- "-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 9a4bc310..424421ae 100644 +index 39fc4f2a..8711b5a8 100644 --- a/compileopts/config.go +++ b/compileopts/config.go -@@ -276,6 +276,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"), -@@ -285,7 +286,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"), +@@ -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/default.nix b/pkgs/development/compilers/tinygo/default.nix index d032f139e334..5901ab7f9604 100644 --- a/pkgs/development/compilers/tinygo/default.nix +++ b/pkgs/development/compilers/tinygo/default.nix @@ -54,6 +54,15 @@ buildGoModule rec { 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/${llvmMajor}/include"; @@ -108,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"