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

Commit 93036a5f authored by Inseob Kim's avatar Inseob Kim
Browse files

Add DirectoryPath for nsjail genrule

This adds a new interface DirectoryPath representing directories
specified by dirgroup modules. As directories are not regular files,
DirectoryPath is meant to be incompatible with regular Path.

Bug: 375551969
Test: m nsjail_genrule_test
Change-Id: I55806121a3a222a8b02f1a080f25448d425447b3
parent a8bf946a
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -39,8 +39,7 @@ type dirGroup struct {
}

type DirInfo struct {
	// TODO(b/358302178): Use DirectoryPaths instead of Paths
	Dirs Paths
	Dirs DirectoryPaths
}

var DirProvider = blueprint.NewProvider[DirInfo]()
+26 −6
Original line number Diff line number Diff line
@@ -550,15 +550,35 @@ func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
	return ret
}

type directoryPath struct {
	basePath
}

func (d *directoryPath) String() string {
	return d.basePath.String()
}

func (d *directoryPath) base() basePath {
	return d.basePath
}

// DirectoryPath represents a source path for directories. Incompatible with Path by design.
type DirectoryPath interface {
	String() string
	base() basePath
}

var _ DirectoryPath = (*directoryPath)(nil)

type DirectoryPaths []DirectoryPath

// DirectoryPathsForModuleSrcExcludes returns a Paths{} containing the resolved references in
// directory paths. Elements of paths are resolved as:
//   - filepath, relative to local module directory, resolves as a filepath relative to the local
//     source directory
//   - other modules using the ":name" syntax. These modules must implement DirProvider.
//
// TODO(b/358302178): Implement DirectoryPath and change the return type.
func DirectoryPathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
	var ret Paths
func DirectoryPathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) DirectoryPaths {
	var ret DirectoryPaths

	for _, path := range paths {
		if m, t := SrcIsModuleWithTag(path); m != "" {
@@ -587,12 +607,12 @@ func DirectoryPathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string
			} else if !isDir {
				ReportPathErrorf(ctx, "module directory path %q is not a directory", p)
			} else {
				ret = append(ret, p)
				ret = append(ret, &directoryPath{basePath{path: p.path, rel: p.rel}})
			}
		}
	}

	seen := make(map[Path]bool, len(ret))
	seen := make(map[DirectoryPath]bool, len(ret))
	for _, path := range ret {
		if seen[path] {
			ReportPathErrorf(ctx, "duplicated path %q", path)
+6 −0
Original line number Diff line number Diff line
@@ -1592,6 +1592,12 @@ func TestPathRelativeToTop(t *testing.T) {
	})
}

func TestDirectoryPathIsIncompatibleWithPath(t *testing.T) {
	d := (DirectoryPath)(&directoryPath{})
	_, ok := d.(Path)
	AssertBoolEquals(t, "directoryPath shouldn't implement Path", ok, false)
}

func ExampleOutputPath_ReplaceExtension() {
	ctx := &configErrorWrapper{
		config: TestConfig("out", nil, "", nil),
+39 −21
Original line number Diff line number Diff line
@@ -575,25 +575,28 @@ func (r *RuleBuilder) build(name string, desc string, ninjaEscapeCommandString b
		nsjailCmd.WriteString(r.outDir.String())
		nsjailCmd.WriteString(":nsjail_build_sandbox/out")

		for _, input := range inputs {
		addBindMount := func(src, dst string) {
			nsjailCmd.WriteString(" -R $PWD/")
			nsjailCmd.WriteString(input.String())
			nsjailCmd.WriteString(src)
			nsjailCmd.WriteString(":nsjail_build_sandbox/")
			nsjailCmd.WriteString(r.nsjailPathForInputRel(input))
			nsjailCmd.WriteString(dst)
		}

		for _, input := range inputs {
			addBindMount(input.String(), r.nsjailPathForInputRel(input))
		}
		for _, tool := range tools {
			nsjailCmd.WriteString(" -R $PWD/")
			nsjailCmd.WriteString(tool.String())
			nsjailCmd.WriteString(":nsjail_build_sandbox/")
			nsjailCmd.WriteString(nsjailPathForToolRel(r.ctx, tool))
			addBindMount(tool.String(), nsjailPathForToolRel(r.ctx, tool))
		}
		inputs = append(inputs, tools...)
		for _, c := range r.commands {
			for _, directory := range c.implicitDirectories {
				addBindMount(directory.String(), directory.String())
				// TODO(b/375551969): Add implicitDirectories to BuildParams, rather than relying on implicits
				inputs = append(inputs, SourcePath{basePath: directory.base()})
			}
			for _, tool := range c.packagedTools {
				nsjailCmd.WriteString(" -R $PWD/")
				nsjailCmd.WriteString(tool.srcPath.String())
				nsjailCmd.WriteString(":nsjail_build_sandbox/")
				nsjailCmd.WriteString(nsjailPathForPackagedToolRel(tool))
				addBindMount(tool.srcPath.String(), nsjailPathForPackagedToolRel(tool))
				inputs = append(inputs, tool.srcPath)
			}
		}
@@ -927,6 +930,7 @@ type RuleBuilderCommand struct {
	tools               Paths
	packagedTools       []PackagingSpec
	rspFiles            []rspFileAndPaths
	implicitDirectories DirectoryPaths
}

type rspFileAndPaths struct {
@@ -951,6 +955,10 @@ func (c *RuleBuilderCommand) addImplicit(path Path) {
	c.implicits = append(c.implicits, path)
}

func (c *RuleBuilderCommand) addImplicitDirectory(path DirectoryPath) {
	c.implicitDirectories = append(c.implicitDirectories, path)
}

func (c *RuleBuilderCommand) addOrderOnly(path Path) {
	checkPathNotNil(path)
	c.orderOnlys = append(c.orderOnlys, path)
@@ -1313,6 +1321,16 @@ func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
	return c
}

// ImplicitDirectory adds the specified input directory to the dependencies without modifying the
// command line. Added directories will be bind-mounted for the nsjail.
func (c *RuleBuilderCommand) ImplicitDirectory(path DirectoryPath) *RuleBuilderCommand {
	if !c.rule.nsjail {
		panic("ImplicitDirectory() must be called after Nsjail()")
	}
	c.addImplicitDirectory(path)
	return c
}

// GetImplicits returns the command's implicit inputs.
func (c *RuleBuilderCommand) GetImplicits() Paths {
	return c.implicits
+3 −2
Original line number Diff line number Diff line
@@ -231,7 +231,7 @@ type generateTask struct {

	// For nsjail tasks
	useNsjail bool
	dirSrcs   android.Paths
	dirSrcs   android.DirectoryPaths
}

func (g *Module) GeneratedSourceFiles() android.Paths {
@@ -604,7 +604,8 @@ func (g *Module) generateCommonBuildActions(ctx android.ModuleContext) {

		if task.useNsjail {
			for _, input := range task.dirSrcs {
				cmd.Implicit(input)
				cmd.ImplicitDirectory(input)
				// TODO(b/375551969): remove glob
				if paths, err := ctx.GlobWithDeps(filepath.Join(input.String(), "**/*"), nil); err == nil {
					rule.NsjailImplicits(android.PathsForSource(ctx, paths))
				} else {