From b7b548a8d01470f9af01e49a887a70f5fb33f29b Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 13 Nov 2019 14:25:09 +0100 Subject: [PATCH] builder: make Clang header detection more robust The header detection code failed way too easily, bailing out when there was more than one Clang version directory. This fixes the following problem in LLVM 9 on Debian: testdata/cgo/main.h:1:10: fatal: 'stdbool.h' file not found testdata/cgo/main.go:5:10: note: in file included from testdata/cgo/main.go!cgo.c:3: --- builder/env.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/builder/env.go b/builder/env.go index 370de728..1decd08b 100644 --- a/builder/env.go +++ b/builder/env.go @@ -9,6 +9,7 @@ import ( "os/exec" "path/filepath" "regexp" + "sort" "strings" ) @@ -100,12 +101,24 @@ func getClangHeaderPath(TINYGOROOT string) string { // /usr/lib/llvm-8/lib/clang/8.0.1/include/ llvmRoot := filepath.Dir(filepath.Dir(binpath)) clangVersionRoot := filepath.Join(llvmRoot, "lib", "clang") - dirnames, err := ioutil.ReadDir(clangVersionRoot) - if err != nil || len(dirnames) != 1 { + dirs, err := ioutil.ReadDir(clangVersionRoot) + if err != nil { // Unexpected. - return "" + continue + } + dirnames := make([]string, len(dirs)) + for i, d := range dirs { + dirnames[i] = d.Name() + } + sort.Strings(dirnames) + // Check for the highest version first. + for i := len(dirnames) - 1; i >= 0; i-- { + path := filepath.Join(clangVersionRoot, dirnames[i], "include") + _, err := os.Stat(filepath.Join(path, "stdint.h")) + if err == nil { + return path + } } - return filepath.Join(clangVersionRoot, dirnames[0].Name(), "include") } }