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:
Этот коммит содержится в:
Ayke van Laethem 2019-11-13 14:25:09 +01:00 коммит произвёл Ron Evans
родитель 8266d2ff58
коммит b7b548a8d0

Просмотреть файл

@ -9,6 +9,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
"strings" "strings"
) )
@ -100,12 +101,24 @@ func getClangHeaderPath(TINYGOROOT string) string {
// /usr/lib/llvm-8/lib/clang/8.0.1/include/ // /usr/lib/llvm-8/lib/clang/8.0.1/include/
llvmRoot := filepath.Dir(filepath.Dir(binpath)) llvmRoot := filepath.Dir(filepath.Dir(binpath))
clangVersionRoot := filepath.Join(llvmRoot, "lib", "clang") clangVersionRoot := filepath.Join(llvmRoot, "lib", "clang")
dirnames, err := ioutil.ReadDir(clangVersionRoot) dirs, err := ioutil.ReadDir(clangVersionRoot)
if err != nil || len(dirnames) != 1 { if err != nil {
// Unexpected. // 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")
} }
} }