@@ -27,6 +27,13 @@ import (
27
27
"strings"
28
28
)
29
29
30
+ var (
31
+ // cgoEnvVars is the list of all cgo environment variable
32
+ cgoEnvVars = []string {"CGO_CFLAGS" , "CGO_CXXFLAGS" , "CGO_CPPFLAGS" , "CGO_LDFLAGS" }
33
+ // cgoAbsEnvFlags are all the flags that need absolute path in cgoEnvVars
34
+ cgoAbsEnvFlags = []string {"-I" , "-L" , "-isysroot" , "-isystem" , "-iquote" , "-include" , "-gcc-toolchain" , "--sysroot" }
35
+ )
36
+
30
37
// env holds a small amount of Go environment and toolchain information
31
38
// which is common to multiple builders. Most Bazel-agnostic build information
32
39
// is collected in go/build.Default though.
@@ -100,6 +107,17 @@ func (e *env) runCommandToFile(w io.Writer, args []string) error {
100
107
return runAndLogCommand (cmd , e .verbose )
101
108
}
102
109
110
+ func absEnv (envNameList []string , argList []string ) error {
111
+ for _ , envName := range envNameList {
112
+ splitedEnv := strings .Fields (os .Getenv (envName ))
113
+ absArgs (splitedEnv , argList )
114
+ if err := os .Setenv (envName , strings .Join (splitedEnv , " " )); err != nil {
115
+ return err
116
+ }
117
+ }
118
+ return nil
119
+ }
120
+
103
121
func runAndLogCommand (cmd * exec.Cmd , verbose bool ) error {
104
122
if verbose {
105
123
formatCommand (os .Stderr , cmd )
@@ -144,29 +162,21 @@ func absArgs(args []string, flags []string) {
144
162
absNext = false
145
163
continue
146
164
}
147
- if ! strings .HasPrefix (args [i ], "-" ) {
148
- continue
149
- }
150
- var flag , value string
151
- var separate bool
152
- if j := strings .IndexByte (args [i ], '=' ); j >= 0 {
153
- flag = args [i ][:j ]
154
- value = args [i ][j + 1 :]
155
- } else {
156
- separate = true
157
- flag = args [i ]
158
- }
159
- flag = strings .TrimLeft (args [i ], "-" )
160
165
for _ , f := range flags {
161
- if flag != f {
166
+ if ! strings . HasPrefix ( args [ i ], f ) {
162
167
continue
163
168
}
164
- if separate {
169
+ possibleValue := args [i ][len (f ):]
170
+ if len (possibleValue ) == 0 {
165
171
absNext = true
166
- } else {
167
- value = abs (value )
168
- args [i ] = fmt .Sprintf ("-%s=%s" , flag , value )
172
+ break
173
+ }
174
+ separator := ""
175
+ if possibleValue [0 ] == '=' {
176
+ possibleValue = possibleValue [1 :]
177
+ separator = "="
169
178
}
179
+ args [i ] = fmt .Sprintf ("%s%s%s" , f , separator , abs (possibleValue ))
170
180
break
171
181
}
172
182
}
0 commit comments