Skip to content

Commit

Permalink
builder: remove environment variables when invoking Clang
Browse files Browse the repository at this point in the history
This is a problem on Guix, which sets C_INCLUDE_PATH that is not
affected by `-nostdlibinc`. And therefore it affects the build in
unintended ways.

Removing all environmental variables fixes this issue, and perhaps also
other issues caused by Clang being affected by environment variables.
  • Loading branch information
aykevl authored and deadprogram committed Oct 18, 2024
1 parent 4ef5109 commit c6acaa9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
12 changes: 0 additions & 12 deletions builder/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package builder
import (
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
Expand Down Expand Up @@ -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()
}
25 changes: 19 additions & 6 deletions builder/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c6acaa9

Please sign in to comment.