Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turn relative path to absolute path in environment's cgo's flags #1536

Merged
merged 1 commit into from Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/tools/builders/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func run(args []string) error {
// Build source with the assembler.
goargs := goenv.goTool("asm", toolArgs...)
goargs = append(goargs, source)
absArgs(goargs, []string{"I", "o", "trimpath"})
absArgs(goargs, []string{"-I", "-o", "-trimpath"})
return goenv.runCommand(goargs)
}

Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func run(args []string) error {
for _, f := range files {
goargs = append(goargs, f.filename)
}
absArgs(goargs, []string{"I", "o", "trimpath"})
absArgs(goargs, []string{"-I", "-o", "-trimpath"})
return goenv.runCommand(goargs)
}

Expand Down
46 changes: 28 additions & 18 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import (
"strings"
)

var (
// cgoEnvVars is the list of all cgo environment variable
cgoEnvVars = []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_CPPFLAGS", "CGO_LDFLAGS"}
// cgoAbsEnvFlags are all the flags that need absolute path in cgoEnvVars
cgoAbsEnvFlags = []string{"-I", "-L", "-isysroot", "-isystem", "-iquote", "-include", "-gcc-toolchain", "--sysroot"}
)

// env holds a small amount of Go environment and toolchain information
// which is common to multiple builders. Most Bazel-agnostic build information
// is collected in go/build.Default though.
Expand Down Expand Up @@ -100,6 +107,17 @@ func (e *env) runCommandToFile(w io.Writer, args []string) error {
return runAndLogCommand(cmd, e.verbose)
}

func absEnv(envNameList []string, argList []string) error {
for _, envName := range envNameList {
splitedEnv := strings.Fields(os.Getenv(envName))
absArgs(splitedEnv, argList)
if err := os.Setenv(envName, strings.Join(splitedEnv, " ")); err != nil {
return err
}
}
return nil
}

func runAndLogCommand(cmd *exec.Cmd, verbose bool) error {
if verbose {
formatCommand(os.Stderr, cmd)
Expand Down Expand Up @@ -144,29 +162,21 @@ func absArgs(args []string, flags []string) {
absNext = false
continue
}
if !strings.HasPrefix(args[i], "-") {
continue
}
var flag, value string
var separate bool
if j := strings.IndexByte(args[i], '='); j >= 0 {
flag = args[i][:j]
value = args[i][j+1:]
} else {
separate = true
flag = args[i]
}
flag = strings.TrimLeft(args[i], "-")
for _, f := range flags {
if flag != f {
if !strings.HasPrefix(args[i], f) {
continue
}
if separate {
possibleValue := args[i][len(f):]
if len(possibleValue) == 0 {
absNext = true
} else {
value = abs(value)
args[i] = fmt.Sprintf("-%s=%s", flag, value)
break
}
separator := ""
if possibleValue[0] == '=' {
possibleValue = possibleValue[1:]
separator = "="
}
args[i] = fmt.Sprintf("%s%s%s", f, separator, abs(possibleValue))
break
}
}
Expand Down
6 changes: 6 additions & 0 deletions go/tools/builders/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func run(args []string) error {
installArgs = append(installArgs, "-ldflags="+allSlug+strings.Join(ldflags, " "))
installArgs = append(installArgs, "-asmflags="+allSlug+strings.Join(asmflags, " "))

// Modifying CGO flags to use only absolute path
// because go is having its own sandbox, all CGO flags must use absolute path
if err := absEnv(cgoEnvVars, cgoAbsEnvFlags); err != nil {
return fmt.Errorf("error modifying cgo environment to absolute path: %v", err)
}

for _, target := range []string{"std", "runtime/cgo"} {
if err := goenv.runCommand(append(installArgs, target)); err != nil {
return err
Expand Down