Escape genrule commands
Bug: 70387174
Test: Put this text into an Android.bp:
genrule {
name: "test_genrule",
tool_files: ["foo"],
out: ["foo.c"],
cmd: "for i in a b c; do echo $$i; done > $(out)",
}
cc_library {
name: "libtest_genrule",
srcs: [":test_genrule"],
}
and then run `m -j libtest_genrule`. Although the library
shouldn't compile, check that it produces a foo.c that has
"a\nb\n\c\n" and not "\n\n\n".
Change-Id: I139106479439c9b3a95f1a2ecc23e73570d7bd59
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 8c1969f..4f3ba93 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -277,7 +277,10 @@
}
genDir := android.PathForModuleGen(ctx)
- sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %q %s $allouts", sandboxPath, genDir, rawCommand, depfilePlaceholder)
+ // Escape the command for the shell
+ rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
+ sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
+ sandboxPath, genDir, rawCommand, depfilePlaceholder)
ruleParams := blueprint.RuleParams{
Command: sandboxCommand,