From a19feeb31b3e39caccb8f6d275664fc6baf2c894 Mon Sep 17 00:00:00 2001 From: Benny Scetbun Date: Fri, 15 Jun 2018 19:02:44 +0200 Subject: [PATCH] turn relative path to absolute path in environment's cgo's flags --- go/tools/builders/asm.go | 2 +- go/tools/builders/compile.go | 2 +- go/tools/builders/env.go | 46 ++++++++++++++++++++++-------------- go/tools/builders/stdlib.go | 6 +++++ 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/go/tools/builders/asm.go b/go/tools/builders/asm.go index b3a2a71bec..1370fc3a40 100644 --- a/go/tools/builders/asm.go +++ b/go/tools/builders/asm.go @@ -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) } diff --git a/go/tools/builders/compile.go b/go/tools/builders/compile.go index 5acce68cae..30c8e2f3c8 100644 --- a/go/tools/builders/compile.go +++ b/go/tools/builders/compile.go @@ -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) } diff --git a/go/tools/builders/env.go b/go/tools/builders/env.go index b0dd62a3c5..ec1b6a1367 100644 --- a/go/tools/builders/env.go +++ b/go/tools/builders/env.go @@ -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. @@ -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) @@ -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 } } diff --git a/go/tools/builders/stdlib.go b/go/tools/builders/stdlib.go index 53e00142ce..622f06a8fa 100644 --- a/go/tools/builders/stdlib.go +++ b/go/tools/builders/stdlib.go @@ -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