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:
This commit is contained in:
Ayke van Laethem
2019-11-13 14:25:09 +01:00
committed by Ron Evans
parent 8266d2ff58
commit b7b548a8d0
+17 -4
View File
@@ -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")
}
}