diff --git a/builder/commands.go b/builder/commands.go index 932e9aceaf..d804ee1476 100644 --- a/builder/commands.go +++ b/builder/commands.go @@ -3,7 +3,6 @@ package builder import ( "errors" "fmt" - "os" "os/exec" "runtime" "strings" @@ -76,14 +75,3 @@ func LookupCommand(name string) (string, error) { } return "", errors.New("none of these commands were found in your $PATH: " + strings.Join(commands[name], " ")) } - -func execCommand(name string, args ...string) error { - name, err := LookupCommand(name) - if err != nil { - return err - } - cmd := exec.Command(name, args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} diff --git a/builder/tools.go b/builder/tools.go index bd18aa09d2..b4087828f1 100644 --- a/builder/tools.go +++ b/builder/tools.go @@ -14,16 +14,29 @@ import ( // runCCompiler invokes a C compiler with the given arguments. func runCCompiler(flags ...string) error { + // Find the right command to run Clang. + var cmd *exec.Cmd if hasBuiltinTools { // Compile this with the internal Clang compiler. - cmd := exec.Command(os.Args[0], append([]string{"clang"}, flags...)...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() + cmd = exec.Command(os.Args[0], append([]string{"clang"}, flags...)...) + } else { + // Compile this with an external invocation of the Clang compiler. + name, err := LookupCommand("clang") + if err != nil { + return err + } + cmd = exec.Command(name, flags...) } - // Compile this with an external invocation of the Clang compiler. - return execCommand("clang", flags...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + // Make sure the command doesn't use any environmental variables. + // Most importantly, it should not use C_INCLUDE_PATH and the like. But + // removing all environmental variables also works. + cmd.Env = []string{} + + return cmd.Run() } // link invokes a linker with the given name and flags.