Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1af783fa authored by Colin Cross's avatar Colin Cross Committed by Gerrit Code Review
Browse files

Merge "Replace android.WriteFile rule with android.WriteFileRule"

parents f15c0558 cf371cc1
Loading
Loading
Loading
Loading
+5 −19
Original line number Original line Diff line number Diff line
@@ -598,36 +598,22 @@ func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion
	var fullContent strings.Builder
	var fullContent strings.Builder
	var flatContent strings.Builder
	var flatContent strings.Builder


	fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
	fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion)
	for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
	for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
		info := depInfos[key]
		info := depInfos[key]
		toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
		toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
		if info.IsExternal {
		if info.IsExternal {
			toName = toName + " (external)"
			toName = toName + " (external)"
		}
		}
		fmt.Fprintf(&fullContent, "  %s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
		fmt.Fprintf(&fullContent, "  %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
		fmt.Fprintf(&flatContent, "%s\\n", toName)
		fmt.Fprintf(&flatContent, "%s\n", toName)
	}
	}


	d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
	d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
	ctx.Build(pctx, BuildParams{
	WriteFileRule(ctx, d.fullListPath, fullContent.String())
		Rule:        WriteFile,
		Description: "Full Dependency Info",
		Output:      d.fullListPath,
		Args: map[string]string{
			"content": fullContent.String(),
		},
	})


	d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
	d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
	ctx.Build(pctx, BuildParams{
	WriteFileRule(ctx, d.flatListPath, flatContent.String())
		Rule:        WriteFile,
		Description: "Flat Dependency Info",
		Output:      d.flatListPath,
		Args: map[string]string{
			"content": flatContent.String(),
		},
	})
}
}


// TODO(b/158059172): remove minSdkVersion allowlist
// TODO(b/158059172): remove minSdkVersion allowlist
+1 −8
Original line number Original line Diff line number Diff line
@@ -225,14 +225,7 @@ func createApiLevelsJson(ctx SingletonContext, file WritablePath,
		ctx.Errorf(err.Error())
		ctx.Errorf(err.Error())
	}
	}


	ctx.Build(pctx, BuildParams{
	WriteFileRule(ctx, file, string(jsonStr))
		Rule:        WriteFile,
		Description: "generate " + file.Base(),
		Output:      file,
		Args: map[string]string{
			"content": string(jsonStr[:]),
		},
	})
}
}


func GetApiLevelsJson(ctx PathContext) WritablePath {
func GetApiLevelsJson(ctx PathContext) WritablePath {
+67 −2
Original line number Original line Diff line number Diff line
@@ -15,8 +15,12 @@
package android
package android


import (
import (
	"strings"
	"testing"

	"github.com/google/blueprint"
	"github.com/google/blueprint"
	_ "github.com/google/blueprint/bootstrap"
	_ "github.com/google/blueprint/bootstrap"
	"github.com/google/blueprint/proptools"
)
)


var (
var (
@@ -91,9 +95,9 @@ var (
	// ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command
	// ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command
	// doesn't support -e option. Therefore we force to use /bin/bash when writing out
	// doesn't support -e option. Therefore we force to use /bin/bash when writing out
	// content to file.
	// content to file.
	WriteFile = pctx.AndroidStaticRule("WriteFile",
	writeFile = pctx.AndroidStaticRule("writeFile",
		blueprint.RuleParams{
		blueprint.RuleParams{
			Command:     "/bin/bash -c 'echo -e $$0 > $out' '$content'",
			Command:     `/bin/bash -c 'echo -e "$$0" > $out' $content`,
			Description: "writing file $out",
			Description: "writing file $out",
		},
		},
		"content")
		"content")
@@ -111,3 +115,64 @@ var (
func init() {
func init() {
	pctx.Import("github.com/google/blueprint/bootstrap")
	pctx.Import("github.com/google/blueprint/bootstrap")
}
}

var (
	// echoEscaper escapes a string such that passing it to "echo -e" will produce the input value.
	echoEscaper = strings.NewReplacer(
		`\`, `\\`, // First escape existing backslashes so they aren't interpreted by `echo -e`.
		"\n", `\n`, // Then replace newlines with \n
	)

	// echoEscaper reverses echoEscaper.
	echoUnescaper = strings.NewReplacer(
		`\n`, "\n",
		`\\`, `\`,
	)

	// shellUnescaper reverses the replacer in proptools.ShellEscape
	shellUnescaper = strings.NewReplacer(`'\''`, `'`)
)

// WriteFileRule creates a ninja rule to write contents to a file.  The contents will be escaped
// so that the file contains exactly the contents passed to the function, plus a trailing newline.
func WriteFileRule(ctx BuilderContext, outputFile WritablePath, content string) {
	content = echoEscaper.Replace(content)
	content = proptools.ShellEscape(content)
	if content == "" {
		content = "''"
	}
	ctx.Build(pctx, BuildParams{
		Rule:        writeFile,
		Output:      outputFile,
		Description: "write " + outputFile.Base(),
		Args: map[string]string{
			"content": content,
		},
	})
}

// shellUnescape reverses proptools.ShellEscape
func shellUnescape(s string) string {
	// Remove leading and trailing quotes if present
	if len(s) >= 2 && s[0] == '\'' {
		s = s[1 : len(s)-1]
	}
	s = shellUnescaper.Replace(s)
	return s
}

// ContentFromFileRuleForTests returns the content that was passed to a WriteFileRule for use
// in tests.
func ContentFromFileRuleForTests(t *testing.T, params TestingBuildParams) string {
	t.Helper()
	if g, w := params.Rule, writeFile; g != w {
		t.Errorf("expected params.Rule to be %q, was %q", w, g)
		return ""
	}

	content := params.Args["content"]
	content = shellUnescape(content)
	content = echoUnescaper.Replace(content)

	return content
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -5447,7 +5447,7 @@ func TestAppBundle(t *testing.T) {
		}
		}
		`, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
		`, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))


	bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("Bundle Config")
	bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
	content := bundleConfigRule.Args["content"]
	content := bundleConfigRule.Args["content"]


	ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
	ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
@@ -5473,7 +5473,7 @@ func TestAppSetBundle(t *testing.T) {
			set: "AppSet.apks",
			set: "AppSet.apks",
		}`)
		}`)
	mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
	mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
	bundleConfigRule := mod.Description("Bundle Config")
	bundleConfigRule := mod.Output("bundle_config.json")
	content := bundleConfigRule.Args["content"]
	content := bundleConfigRule.Args["content"]
	ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
	ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
	s := mod.Rule("apexRule").Args["copy_commands"]
	s := mod.Rule("apexRule").Args["copy_commands"]
+1 −8
Original line number Original line Diff line number Diff line
@@ -373,14 +373,7 @@ func (a *apexBundle) buildBundleConfig(ctx android.ModuleContext) android.Output
		panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
		panic(fmt.Errorf("error while marshalling to %q: %#v", output, err))
	}
	}


	ctx.Build(pctx, android.BuildParams{
	android.WriteFileRule(ctx, output, string(j))
		Rule:        android.WriteFile,
		Output:      output,
		Description: "Bundle Config " + output.String(),
		Args: map[string]string{
			"content": string(j),
		},
	})


	return output.OutputPath
	return output.OutputPath
}
}
Loading