Skip to content

Commit 9342e73

Browse files
aykevldeadprogram
authored andcommitted
builder: fix picolibc include path
Previously we used --sysroot to set the sysroot explicitly. Unfortunately, this flag is not used directly by Clang to set the include path (<sysroot>/include) but is instead interpreted by the toolchain code. This means that even when the toolchain is explicitly set (using the --sysroot parameter), it may still decide to use a different include path such as <sysroot>/usr/include (such as on baremetal aarch64). This commit uses the Clang-internal -internal-isystem flag which sets the include directory directly (as a system include path). This should be more robust. The reason the --sysroot parameter has so far worked is that all existing targets happened to add <sysroot>/include as an include path. The relevant Clang code is here: https://github.com/llvm/llvm-project/blob/release/9.x/clang/lib/Driver/Driver.cpp#L4693-L4739 So far, RISC-V is handled by RISCVToolchain, Cortex-M targets by BareMetal (which seems to be specific to ARM unlike what the name says) and aarch64 fell back to Generic_ELF.
1 parent fc4857e commit 9342e73

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

builder/picolibc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Picolibc = Library{
1212
name: "picolibc",
1313
cflags: func() []string {
1414
picolibcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib/libc")
15-
return []string{"-Werror", "-Wall", "-std=gnu11", "-D_COMPILING_NEWLIB", "--sysroot=" + picolibcDir, "-I" + picolibcDir + "/tinystdio", "-I" + goenv.Get("TINYGOROOT") + "/lib/picolibc-include"}
15+
return []string{"-Werror", "-Wall", "-std=gnu11", "-D_COMPILING_NEWLIB", "-nostdlibinc", "-Xclang", "-internal-isystem", "-Xclang", picolibcDir + "/include", "-I" + picolibcDir + "/tinystdio", "-I" + goenv.Get("TINYGOROOT") + "/lib/picolibc-include"}
1616
},
1717
sourceDir: "lib/picolibc/newlib/libc",
1818
sources: func(target string) []string {

compileopts/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (c *Config) CFlags() []string {
173173
}
174174
if c.Target.Libc == "picolibc" {
175175
root := goenv.Get("TINYGOROOT")
176-
cflags = append(cflags, "--sysroot="+filepath.Join(root, "lib", "picolibc", "newlib", "libc"))
176+
cflags = append(cflags, "-nostdlibinc", "-Xclang", "-internal-isystem", "-Xclang", filepath.Join(root, "lib", "picolibc", "newlib", "libc", "include"))
177177
cflags = append(cflags, "-I"+filepath.Join(root, "lib/picolibc-include"))
178178
}
179179
return cflags

0 commit comments

Comments
 (0)