Browse Source

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
Jeff Gaston 6 years ago
parent
commit
31603067b0
1 changed files with 4 additions and 1 deletions
  1. 4 1
      genrule/genrule.go

+ 4 - 1
genrule/genrule.go

@@ -277,7 +277,10 @@ func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	}
 
 	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,