diff options
author | 2024-10-25 17:02:21 +0900 | |
---|---|---|
committer | 2024-11-04 10:31:20 +0900 | |
commit | 93036a5ff25c7e559e110899b81b09eff58f62db (patch) | |
tree | 0af4298b269650a6b4a4102833e224a136a728aa | |
parent | a8bf946a22d5e7022c5fd2c748da226f0ee0a16e (diff) |
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
-rw-r--r-- | android/dirgroup.go | 3 | ||||
-rw-r--r-- | android/paths.go | 32 | ||||
-rw-r--r-- | android/paths_test.go | 6 | ||||
-rw-r--r-- | android/rule_builder.go | 60 | ||||
-rw-r--r-- | genrule/genrule.go | 5 |
5 files changed, 75 insertions, 31 deletions
diff --git a/android/dirgroup.go b/android/dirgroup.go index 20c4d13a6..62fbaa5c8 100644 --- a/android/dirgroup.go +++ b/android/dirgroup.go @@ -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]() diff --git a/android/paths.go b/android/paths.go index 371aed86d..339fb2bd9 100644 --- a/android/paths.go +++ b/android/paths.go @@ -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) diff --git a/android/paths_test.go b/android/paths_test.go index 941f0ca78..5e618f914 100644 --- a/android/paths_test.go +++ b/android/paths_test.go @@ -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), diff --git a/android/rule_builder.go b/android/rule_builder.go index 56de9cd00..403c18418 100644 --- a/android/rule_builder.go +++ b/android/rule_builder.go @@ -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) } } @@ -917,16 +920,17 @@ func (r *RuleBuilder) build(name string, desc string, ninjaEscapeCommandString b type RuleBuilderCommand struct { rule *RuleBuilder - buf strings.Builder - inputs Paths - implicits Paths - orderOnlys Paths - validations Paths - outputs WritablePaths - depFiles WritablePaths - tools Paths - packagedTools []PackagingSpec - rspFiles []rspFileAndPaths + buf strings.Builder + inputs Paths + implicits Paths + orderOnlys Paths + validations Paths + outputs WritablePaths + depFiles WritablePaths + 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 diff --git a/genrule/genrule.go b/genrule/genrule.go index 1ab137837..1282bfbae 100644 --- a/genrule/genrule.go +++ b/genrule/genrule.go @@ -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 { |