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

Commit 52d1cc33 authored by Sam Delmerico's avatar Sam Delmerico Committed by Gerrit Code Review
Browse files

Merge changes from topics...

Merge changes from topics "revert-2746976-revert-2605644-rulebuilder-ninja-vars-OAAWYCDDLT-KMAGKVIXAT", "sandbox-rust-inputs" into main

* changes:
  support sandboxed rust rules
  conditionally escape rule builder command
  Revert^2 "allow Ninja variables in RuleBuilder API"
  Revert^2 "add crate_root property to rust modules"
  Revert^2 "add rust_toolchain_rustc_prebuilt module type"
parents c331812a a588d153
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -95,6 +95,12 @@ func NewDepSet[T depSettableType](order DepSetOrder, direct []T, transitive []*D
	}
}

// AddDirectToDepSet returns a new DepSet with additional elements added to its direct set.
// The transitive sets remain untouched.
func AddDirectToDepSet[T depSettableType](d *DepSet[T], direct ...T) *DepSet[T] {
	return NewDepSet[T](d.order, Concat(d.direct, direct), d.transitive)
}

// DepSetBuilder is used to create an immutable DepSet.
type DepSetBuilder[T depSettableType] struct {
	order      DepSetOrder
@@ -188,3 +194,14 @@ func (d *DepSet[T]) ToList() []T {
	}
	return list
}

// ToListDirect returns the direct elements of a DepSet flattened to a list.
func (d *DepSet[T]) ToListDirect() []T {
	if d == nil {
		return nil
	}
	list := make([]T, len(d.direct))
	copy(list, d.direct)
	list = firstUniqueInPlace(list)
	return list
}
+60 −0
Original line number Diff line number Diff line
@@ -171,6 +171,9 @@ type Path interface {
	// Base returns the last element of the path
	Base() string

	// Dir returns a path pointing the directory containing the path
	Dir() Path

	// Rel returns the portion of the path relative to the directory it was created from.  For
	// example, Rel on a PathsForModuleSrc would return the path relative to the module source
	// directory, and OutputPath.Join("foo").Rel() would return "foo".
@@ -1012,6 +1015,12 @@ func (p basePath) Base() string {
	return filepath.Base(p.path)
}

func (p basePath) Dir() Path {
	p.path = filepath.Dir(p.path)
	p.rel = filepath.Dir(p.rel)
	return p
}

func (p basePath) Rel() string {
	if p.rel != "" {
		return p.rel
@@ -1046,6 +1055,11 @@ func (p SourcePath) withRel(rel string) SourcePath {
	return p
}

func (p SourcePath) Dir() Path {
	p.basePath = p.basePath.Dir().(basePath)
	return p
}

// safePathForSource is for paths that we expect are safe -- only for use by go
// code that is embedding ninja variables in paths
func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
@@ -1248,6 +1262,12 @@ func (p OutputPath) withRel(rel string) OutputPath {
	return p
}

func (p OutputPath) Dir() Path {
	p.basePath = p.basePath.Dir().(basePath)
	p.fullPath = filepath.Dir(p.fullPath)
	return p
}

func (p OutputPath) WithoutRel() OutputPath {
	p.basePath.rel = filepath.Base(p.basePath.path)
	return p
@@ -1280,6 +1300,11 @@ type toolDepPath struct {
	basePath
}

func (p toolDepPath) Dir() Path {
	p.basePath = p.basePath.Dir().(basePath)
	return p
}

func (t toolDepPath) RelativeToTop() Path {
	ensureTestOnly()
	return t
@@ -1463,6 +1488,11 @@ type ModuleOutPath struct {
	OutputPath
}

func (p ModuleOutPath) Dir() Path {
	p.OutputPath = p.OutputPath.Dir().(OutputPath)
	return p
}

func (p ModuleOutPath) RelativeToTop() Path {
	p.OutputPath = p.outputPathRelativeToTop()
	return p
@@ -1507,6 +1537,11 @@ type ModuleGenPath struct {
	ModuleOutPath
}

func (p ModuleGenPath) Dir() Path {
	p.ModuleOutPath = p.ModuleOutPath.Dir().(ModuleOutPath)
	return p
}

func (p ModuleGenPath) RelativeToTop() Path {
	p.OutputPath = p.outputPathRelativeToTop()
	return p
@@ -1546,6 +1581,11 @@ type ModuleObjPath struct {
	ModuleOutPath
}

func (p ModuleObjPath) Dir() Path {
	p.ModuleOutPath = p.ModuleOutPath.Dir().(ModuleOutPath)
	return p
}

func (p ModuleObjPath) RelativeToTop() Path {
	p.OutputPath = p.outputPathRelativeToTop()
	return p
@@ -1570,6 +1610,11 @@ type ModuleResPath struct {
	ModuleOutPath
}

func (p ModuleResPath) Dir() Path {
	p.ModuleOutPath = p.ModuleOutPath.Dir().(ModuleOutPath)
	return p
}

func (p ModuleResPath) RelativeToTop() Path {
	p.OutputPath = p.outputPathRelativeToTop()
	return p
@@ -1606,6 +1651,11 @@ type InstallPath struct {
	makePath bool
}

func (p InstallPath) Dir() Path {
	p.basePath = p.basePath.Dir().(basePath)
	return p
}

// Will panic if called from outside a test environment.
func ensureTestOnly() {
	if PrefixInList(os.Args, "-test.") {
@@ -1922,6 +1972,11 @@ type PhonyPath struct {
	basePath
}

func (p PhonyPath) Dir() Path {
	p.basePath = p.basePath.Dir().(basePath)
	return p
}

func (p PhonyPath) writablePath() {}

func (p PhonyPath) getSoongOutDir() string {
@@ -1947,6 +2002,11 @@ type testPath struct {
	basePath
}

func (p testPath) Dir() Path {
	p.basePath = p.basePath.Dir().(basePath)
	return p
}

func (p testPath) RelativeToTop() Path {
	ensureTestOnly()
	return p
+18 −6
Original line number Diff line number Diff line
@@ -14,10 +14,14 @@

package android

import "path/filepath"
import (
	"path/filepath"

	"github.com/google/blueprint"
)

func init() {
	RegisterModuleType("prebuilt_build_tool", prebuiltBuildToolFactory)
	RegisterModuleType("prebuilt_build_tool", NewPrebuiltBuildTool)
}

type prebuiltBuildToolProperties struct {
@@ -55,6 +59,13 @@ func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) {
	}
}

type PrebuiltBuildToolInfo struct {
	Src  Path
	Deps Paths
}

var PrebuiltBuildToolInfoProvider = blueprint.NewProvider(PrebuiltBuildToolInfo{})

func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) {
	sourcePath := t.prebuilt.SingleSourcePath(ctx)
	installedPath := PathForModuleOut(ctx, t.BaseModuleName())
@@ -82,6 +93,11 @@ func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) {
	}

	t.toolPath = OptionalPathForPath(installedPath)

	ctx.SetProvider(PrebuiltBuildToolInfoProvider, PrebuiltBuildToolInfo{
		Src:  sourcePath,
		Deps: deps,
	})
}

func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) {
@@ -101,10 +117,6 @@ var _ HostToolProvider = &prebuiltBuildTool{}

// prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use
// in genrules with the "tools" property.
func prebuiltBuildToolFactory() Module {
	return NewPrebuiltBuildTool()
}

func NewPrebuiltBuildTool() Module {
	module := &prebuiltBuildTool{}
	module.AddProperties(&module.properties)
+42 −5
Original line number Diff line number Diff line
@@ -474,13 +474,23 @@ func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderComma
		Inputs(depFiles.Paths())
}

// BuildWithNinjaVars adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
// Outputs. This function will not escape Ninja variables, so it may be used to write sandbox manifests using Ninja variables.
func (r *RuleBuilder) BuildWithUnescapedNinjaVars(name string, desc string) {
	r.build(name, desc, false)
}

// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
// Outputs.
func (r *RuleBuilder) Build(name string, desc string) {
	r.build(name, desc, true)
}

func (r *RuleBuilder) build(name string, desc string, ninjaEscapeCommandString bool) {
	name = ninjaNameEscape(name)

	if len(r.missingDeps) > 0 {
		r.ctx.Build(pctx, BuildParams{
		r.ctx.Build(r.pctx, BuildParams{
			Rule:        ErrorRule,
			Outputs:     r.Outputs(),
			Description: desc,
@@ -619,12 +629,35 @@ func (r *RuleBuilder) Build(name string, desc string) {
				name, r.sboxManifestPath.String(), r.outDir.String())
		}

		// Create a rule to write the manifest as a the textproto.
		// Create a rule to write the manifest as textproto.
		pbText, err := prototext.Marshal(&manifest)
		if err != nil {
			ReportPathErrorf(r.ctx, "sbox manifest failed to marshal: %q", err)
		}
		if ninjaEscapeCommandString {
			WriteFileRule(r.ctx, r.sboxManifestPath, string(pbText))
		} else {
			// We need  to have a rule to write files that is
			// defined on the RuleBuilder's pctx in order to
			// write Ninja variables in the string.
			// The WriteFileRule function above rule can only write
			// raw strings because it is defined on the android
			// package's pctx, and it can't access variables defined
			// in another context.
			r.ctx.Build(r.pctx, BuildParams{
				Rule: r.ctx.Rule(r.pctx, "unescapedWriteFile", blueprint.RuleParams{
					Command:        `rm -rf ${out} && cat ${out}.rsp > ${out}`,
					Rspfile:        "${out}.rsp",
					RspfileContent: "${content}",
					Description:    "write file",
				}, "content"),
				Output:      r.sboxManifestPath,
				Description: "write sbox manifest " + r.sboxManifestPath.Base(),
				Args: map[string]string{
					"content": string(pbText),
				},
			})
		}

		// Generate a new string to use as the command line of the sbox rule.  This uses
		// a RuleBuilderCommand as a convenience method of building the command line, then
@@ -723,9 +756,13 @@ func (r *RuleBuilder) Build(name string, desc string) {
		pool = localPool
	}

	if ninjaEscapeCommandString {
		commandString = proptools.NinjaEscape(commandString)
	}

	r.ctx.Build(r.pctx, BuildParams{
		Rule: r.ctx.Rule(pctx, name, blueprint.RuleParams{
			Command:        proptools.NinjaEscape(commandString),
		Rule: r.ctx.Rule(r.pctx, name, blueprint.RuleParams{
			Command:        commandString,
			CommandDeps:    proptools.NinjaEscapeList(tools.Strings()),
			Restat:         r.restat,
			Rspfile:        proptools.NinjaEscape(rspFile),
+75 −11
Original line number Diff line number Diff line
@@ -28,6 +28,17 @@ import (
	"android/soong/shared"
)

var (
	pctx_ruleBuilderTest           = NewPackageContext("android/soong/rule_builder")
	pctx_ruleBuilderTestSubContext = NewPackageContext("android/soong/rule_builder/config")
)

func init() {
	pctx_ruleBuilderTest.Import("android/soong/rule_builder/config")
	pctx_ruleBuilderTest.StaticVariable("cmdFlags", "${config.ConfigFlags}")
	pctx_ruleBuilderTestSubContext.StaticVariable("ConfigFlags", "--some-clang-flag")
}

func builderContext() BuilderContext {
	return BuilderContextForTesting(TestConfig("out", nil, "", map[string][]byte{
		"ld":      nil,
@@ -497,10 +508,12 @@ type testRuleBuilderModule struct {
	ModuleBase
	properties struct {
		Srcs  []string
		Flags []string

		Restat              bool
		Sbox                bool
		Sbox_inputs         bool
		Unescape_ninja_vars bool
	}
}

@@ -518,8 +531,9 @@ func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
	rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"})
	manifestPath := PathForModuleOut(ctx, "sbox.textproto")

	testRuleBuilder_Build(ctx, in, implicit, orderOnly, validation, out, outDep, outDir,
		manifestPath, t.properties.Restat, t.properties.Sbox, t.properties.Sbox_inputs,
	testRuleBuilder_Build(ctx, in, implicit, orderOnly, validation, t.properties.Flags,
		out, outDep, outDir,
		manifestPath, t.properties.Restat, t.properties.Sbox, t.properties.Sbox_inputs, t.properties.Unescape_ninja_vars,
		rspFile, rspFileContents, rspFile2, rspFileContents2)
}

@@ -543,17 +557,18 @@ func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
	rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"})
	manifestPath := PathForOutput(ctx, "singleton/sbox.textproto")

	testRuleBuilder_Build(ctx, in, implicit, orderOnly, validation, out, outDep, outDir,
		manifestPath, true, false, false,
	testRuleBuilder_Build(ctx, in, implicit, orderOnly, validation, nil, out, outDep, outDir,
		manifestPath, true, false, false, false,
		rspFile, rspFileContents, rspFile2, rspFileContents2)
}

func testRuleBuilder_Build(ctx BuilderContext, in Paths, implicit, orderOnly, validation Path,
	flags []string,
	out, outDep, outDir, manifestPath WritablePath,
	restat, sbox, sboxInputs bool,
	restat, sbox, sboxInputs, unescapeNinjaVars bool,
	rspFile WritablePath, rspFileContents Paths, rspFile2 WritablePath, rspFileContents2 Paths) {

	rule := NewRuleBuilder(pctx, ctx)
	rule := NewRuleBuilder(pctx_ruleBuilderTest, ctx)

	if sbox {
		rule.Sbox(outDir, manifestPath)
@@ -564,6 +579,7 @@ func testRuleBuilder_Build(ctx BuilderContext, in Paths, implicit, orderOnly, va

	rule.Command().
		Tool(PathForSource(ctx, "cp")).
		Flags(flags).
		Inputs(in).
		Implicit(implicit).
		OrderOnly(orderOnly).
@@ -577,8 +593,12 @@ func testRuleBuilder_Build(ctx BuilderContext, in Paths, implicit, orderOnly, va
		rule.Restat()
	}

	if unescapeNinjaVars {
		rule.BuildWithUnescapedNinjaVars("rule", "desc")
	} else {
		rule.Build("rule", "desc")
	}
}

var prepareForRuleBuilderTest = FixtureRegisterWithContext(func(ctx RegistrationContext) {
	ctx.RegisterModuleType("rule_builder_test", testRuleBuilderFactory)
@@ -792,3 +812,47 @@ func TestRuleBuilderHashInputs(t *testing.T) {
		})
	}
}

func TestRuleBuilderWithNinjaVarEscaping(t *testing.T) {
	bp := `
		rule_builder_test {
			name: "foo_sbox_escaped_ninja",
			flags: ["${cmdFlags}"],
			sbox: true,
			sbox_inputs: true,
		}
		rule_builder_test {
			name: "foo_sbox",
			flags: ["${cmdFlags}"],
			sbox: true,
			sbox_inputs: true,
			unescape_ninja_vars: true,
		}
	`
	result := GroupFixturePreparers(
		prepareForRuleBuilderTest,
		FixtureWithRootAndroidBp(bp),
	).RunTest(t)

	escapedNinjaMod := result.ModuleForTests("foo_sbox_escaped_ninja", "").Rule("writeFile")
	AssertStringDoesContain(
		t,
		"",
		escapedNinjaMod.BuildParams.Args["content"],
		"$${cmdFlags}",
	)

	unescapedNinjaMod := result.ModuleForTests("foo_sbox", "").Rule("unescapedWriteFile")
	AssertStringDoesContain(
		t,
		"",
		unescapedNinjaMod.BuildParams.Args["content"],
		"${cmdFlags}",
	)
	AssertStringDoesNotContain(
		t,
		"",
		unescapedNinjaMod.BuildParams.Args["content"],
		"$${cmdFlags}",
	)
}
Loading