aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper
diff options
context:
space:
mode:
authorTobias Bosch <tbosch@google.com>2019-07-11 04:24:52 -0700
committerTobias Bosch <tbosch@google.com>2019-07-13 12:29:19 +0000
commit584728183f90cf6ef27fa09b52c8ee5203c87e4a (patch)
tree4d725a5553ab177f66b86c27075935ee5827cae3 /compiler_wrapper
parent6652ca32cb1194acb1771121189f1531c3644402 (diff)
downloadtoolchain-utils-584728183f90cf6ef27fa09b52c8ee5203c87e4a.tar.gz
Add -print-cmdline and -print-config
BUG=chromium:773875 TEST=unit test Change-Id: Ib25e582257694f3a3457795873a249735e3aaf82 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1697931 Tested-by: Tobias Bosch <tbosch@google.com> Reviewed-by: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'compiler_wrapper')
-rw-r--r--compiler_wrapper/bisect_flag.go7
-rw-r--r--compiler_wrapper/command.go10
-rw-r--r--compiler_wrapper/compiler_wrapper.go9
-rw-r--r--compiler_wrapper/env.go30
-rw-r--r--compiler_wrapper/oldwrapper.go2
-rw-r--r--compiler_wrapper/print_cmdline_flag.go15
-rw-r--r--compiler_wrapper/print_cmdline_flag_test.go81
-rw-r--r--compiler_wrapper/print_config_flag.go17
-rw-r--r--compiler_wrapper/print_config_flag_test.go32
-rw-r--r--compiler_wrapper/rusage_flag.go5
10 files changed, 192 insertions, 16 deletions
diff --git a/compiler_wrapper/bisect_flag.go b/compiler_wrapper/bisect_flag.go
index 14d84cd4..eb96939a 100644
--- a/compiler_wrapper/bisect_flag.go
+++ b/compiler_wrapper/bisect_flag.go
@@ -1,7 +1,5 @@
package main
-import "path/filepath"
-
const bisectPythonCommand = "import bisect_driver; sys.exit(bisect_driver.bisect_driver(sys.argv[1], sys.argv[2], sys.argv[3:]))"
func getBisectStage(env env) string {
@@ -13,10 +11,7 @@ func calcBisectCommand(env env, bisectStage string, compilerCmd *command) *comma
if bisectDir == "" {
bisectDir = "/tmp/sysroot_bisect"
}
- absCompilerPath := compilerCmd.path
- if !filepath.IsAbs(absCompilerPath) {
- absCompilerPath = filepath.Join(env.getwd(), absCompilerPath)
- }
+ absCompilerPath := getAbsCmdPath(env, compilerCmd)
return &command{
path: "/usr/bin/python2",
args: append([]string{
diff --git a/compiler_wrapper/command.go b/compiler_wrapper/command.go
index b1c6f5f0..4e44e1a7 100644
--- a/compiler_wrapper/command.go
+++ b/compiler_wrapper/command.go
@@ -37,6 +37,14 @@ func ensurePathEnv(cmd *exec.Cmd) {
cmd.Env = append(cmd.Env, "PATH=")
}
+func getAbsCmdPath(env env, cmd *command) string {
+ path := cmd.path
+ if !filepath.IsAbs(path) {
+ path = filepath.Join(env.getwd(), path)
+ }
+ return path
+}
+
func newCommandBuilder(env env, cfg *config, cmd *command) (*commandBuilder, error) {
basename := filepath.Base(cmd.path)
nameParts := strings.Split(basename, "-")
@@ -52,7 +60,7 @@ func newCommandBuilder(env env, cfg *config, cmd *command) (*commandBuilder, err
default:
compilerType = gccType
}
- absWrapperDir, err := getAbsWrapperDir(env, cmd.path)
+ absWrapperDir, err := getAbsWrapperDir(env, cmd)
if err != nil {
return nil, err
}
diff --git a/compiler_wrapper/compiler_wrapper.go b/compiler_wrapper/compiler_wrapper.go
index b0269abe..314a8a71 100644
--- a/compiler_wrapper/compiler_wrapper.go
+++ b/compiler_wrapper/compiler_wrapper.go
@@ -43,6 +43,9 @@ func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int
if err != nil {
return 0, err
}
+ processPrintConfigFlag(mainBuilder)
+ processPrintCmdlineFlag(mainBuilder)
+ env = mainBuilder.env
var compilerCmd *command
clangSyntax := processClangSyntaxFlag(mainBuilder)
if mainBuilder.target.compilerType == clangType {
@@ -133,10 +136,8 @@ func processGomaCCacheFlags(sysroot string, builder *commandBuilder) {
}
}
-func getAbsWrapperDir(env env, wrapperPath string) (string, error) {
- if !filepath.IsAbs(wrapperPath) {
- wrapperPath = filepath.Join(env.getwd(), wrapperPath)
- }
+func getAbsWrapperDir(env env, wrapperCmd *command) (string, error) {
+ wrapperPath := getAbsCmdPath(env, wrapperCmd)
evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath)
if err != nil {
return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
diff --git a/compiler_wrapper/env.go b/compiler_wrapper/env.go
index 871648cd..600366c4 100644
--- a/compiler_wrapper/env.go
+++ b/compiler_wrapper/env.go
@@ -2,8 +2,10 @@ package main
import (
"bytes"
+ "fmt"
"io"
"os"
+ "strings"
"syscall"
)
@@ -96,3 +98,31 @@ func (env *commandRecordingEnv) run(cmd *command, stdout io.Writer, stderr io.Wr
}
return err
}
+
+type printingEnv struct {
+ env
+}
+
+var _env = (*printingEnv)(nil)
+
+func (env *printingEnv) exec(cmd *command) error {
+ printCmd(env, cmd)
+ return env.env.exec(cmd)
+}
+
+func (env *printingEnv) run(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ printCmd(env, cmd)
+ return env.env.run(cmd, stdout, stderr)
+}
+
+func printCmd(env env, cmd *command) {
+ fmt.Fprintf(env.stderr(), "cd '%s' &&", env.getwd())
+ if len(cmd.envUpdates) > 0 {
+ fmt.Fprintf(env.stderr(), " env '%s'", strings.Join(cmd.envUpdates, "' '"))
+ }
+ fmt.Fprintf(env.stderr(), " '%s'", getAbsCmdPath(env, cmd))
+ if len(cmd.args) > 0 {
+ fmt.Fprintf(env.stderr(), " '%s'", strings.Join(cmd.args, "' '"))
+ }
+ io.WriteString(env.stderr(), "\n")
+}
diff --git a/compiler_wrapper/oldwrapper.go b/compiler_wrapper/oldwrapper.go
index e4ec0b81..f2aebed3 100644
--- a/compiler_wrapper/oldwrapper.go
+++ b/compiler_wrapper/oldwrapper.go
@@ -175,7 +175,7 @@ type oldWrapperCmdResult struct {
func newOldWrapperConfig(env env, cfg *config, inputCmd *command) (*oldWrapperConfig, error) {
absOldWrapperPath := cfg.oldWrapperPath
if !filepath.IsAbs(absOldWrapperPath) {
- absWrapperDir, err := getAbsWrapperDir(env, inputCmd.path)
+ absWrapperDir, err := getAbsWrapperDir(env, inputCmd)
if err != nil {
return nil, err
}
diff --git a/compiler_wrapper/print_cmdline_flag.go b/compiler_wrapper/print_cmdline_flag.go
new file mode 100644
index 00000000..181ef891
--- /dev/null
+++ b/compiler_wrapper/print_cmdline_flag.go
@@ -0,0 +1,15 @@
+package main
+
+func processPrintCmdlineFlag(builder *commandBuilder) {
+ printCmd := false
+ builder.transformArgs(func(arg builderArg) string {
+ if arg.value == "-print-cmdline" {
+ printCmd = true
+ return ""
+ }
+ return arg.value
+ })
+ if printCmd {
+ builder.env = &printingEnv{builder.env}
+ }
+}
diff --git a/compiler_wrapper/print_cmdline_flag_test.go b/compiler_wrapper/print_cmdline_flag_test.go
new file mode 100644
index 00000000..f3ccd723
--- /dev/null
+++ b/compiler_wrapper/print_cmdline_flag_test.go
@@ -0,0 +1,81 @@
+package main
+
+import (
+ "fmt"
+ "regexp"
+ "testing"
+)
+
+func TestRemovePrintCmdlineArg(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", mainCc)))
+ if err := verifyArgCount(cmd, 0, "-print-cmdline"); err != nil {
+ t.Error(err)
+ }
+ })
+}
+
+func TestPrintCompilerCommand(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", mainCc)))
+ if matched, _ := regexp.MatchString(`cd '.*' && '.*/x86_64-cros-linux-gnu-gcc.real'.*'main.cc'`, ctx.stderrString()); !matched {
+ t.Errorf("sub command not printed to stderr. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestPrintNestedCommand(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ // Note: -clang-syntax calls clang to check the syntax
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-cmdline", "-clang-syntax", mainCc)))
+ if matched, _ := regexp.MatchString(`cd '.*' && '.*usr/bin/clang'.*'main.cc'.*'-fsyntax-only'`, ctx.stderrString()); !matched {
+ t.Errorf("sub command not printed to stderr. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestPrintCmdWd(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ printCmd(ctx, &command{
+ path: "/somepath",
+ })
+ if ctx.stderrString() != fmt.Sprintf("cd '%s' && '/somepath'\n", ctx.tempDir) {
+ t.Errorf("unexpected result. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestPrintCmdAbsolutePath(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ printCmd(ctx, &command{
+ path: "somepath",
+ })
+ if ctx.stderrString() != fmt.Sprintf("cd '%s' && '%s/somepath'\n", ctx.tempDir, ctx.tempDir) {
+ t.Errorf("unexpected result. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestPrintCmdEnvUpdates(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ printCmd(ctx, &command{
+ path: "/somepath",
+ envUpdates: []string{"a=b"},
+ })
+ if ctx.stderrString() != fmt.Sprintf("cd '%s' && env 'a=b' '/somepath'\n", ctx.tempDir) {
+ t.Errorf("unexpected result. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestPrintCmdArgs(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ printCmd(ctx, &command{
+ path: "/somepath",
+ args: []string{"-a"},
+ })
+ if ctx.stderrString() != fmt.Sprintf("cd '%s' && '/somepath' '-a'\n", ctx.tempDir) {
+ t.Errorf("unexpected result. Got: %s", ctx.stderrString())
+ }
+ })
+}
diff --git a/compiler_wrapper/print_config_flag.go b/compiler_wrapper/print_config_flag.go
new file mode 100644
index 00000000..9031bec2
--- /dev/null
+++ b/compiler_wrapper/print_config_flag.go
@@ -0,0 +1,17 @@
+package main
+
+import "fmt"
+
+func processPrintConfigFlag(builder *commandBuilder) {
+ printConfig := false
+ builder.transformArgs(func(arg builderArg) string {
+ if arg.value == "-print-config" {
+ printConfig = true
+ return ""
+ }
+ return arg.value
+ })
+ if printConfig {
+ fmt.Fprintf(builder.env.stderr(), "wrapper config: %#v\n", *builder.cfg)
+ }
+}
diff --git a/compiler_wrapper/print_config_flag_test.go b/compiler_wrapper/print_config_flag_test.go
new file mode 100644
index 00000000..834716fd
--- /dev/null
+++ b/compiler_wrapper/print_config_flag_test.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestRemovePrintConfigArg(t *testing.T) {
+ withPrintConfigTestContext(t, func(ctx *testContext) {
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-config", mainCc)))
+ if err := verifyArgCount(cmd, 0, "-print-config"); err != nil {
+ t.Error(err)
+ }
+ })
+}
+
+func TestPrintConfig(t *testing.T) {
+ withPrintConfigTestContext(t, func(ctx *testContext) {
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, "-print-config", mainCc)))
+ if !strings.Contains(ctx.stderrString(), "wrapper config: main.config{useCCache:false") {
+ t.Errorf("config not printed to stderr. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func withPrintConfigTestContext(t *testing.T, work func(ctx *testContext)) {
+ withTestContext(t, func(ctx *testContext) {
+ // Not comparing to old wrapper as the old wrapper doesn't have a print-config command.
+ ctx.cfg.oldWrapperPath = ""
+ work(ctx)
+ })
+}
diff --git a/compiler_wrapper/rusage_flag.go b/compiler_wrapper/rusage_flag.go
index b103bb38..d336fc43 100644
--- a/compiler_wrapper/rusage_flag.go
+++ b/compiler_wrapper/rusage_flag.go
@@ -39,10 +39,7 @@ func logRusage(env env, logFileName string, compilerCmd *command) (exitCode int,
// Note: We assume that the compiler takes more heap than any other
// subcommands that we might have executed before.
maxMemUsed := rusageAfter.Maxrss
- absCompilerPath := compilerCmd.path
- if !filepath.IsAbs(absCompilerPath) {
- absCompilerPath = filepath.Join(env.getwd(), absCompilerPath)
- }
+ absCompilerPath := getAbsCmdPath(env, compilerCmd)
if err := os.MkdirAll(filepath.Dir(logFileName), 0777); err != nil {
return 0, wrapErrorwithSourceLocf(err, "error creating rusage log directory %s", logFileName)