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

Commit bc139927 authored by Colin Cross's avatar Colin Cross
Browse files

Support sandboxing droiddoc and droidstubs with args properties

args properties can access arbitrary files with $(location) expansions,
so they need to pass them through RuleBuilderCommand.PathsForInputs
to produce a path inside the sandbox.  Extract the arg expansion out
of collectDeps into a new expandArgs method that takes the
RuleBuilderCommand.

Test: TestDroidstubsSandbox
Change-Id: I9022d17bf3cb64c97b2008c4c1b733bf48edca95
parent 6aa5c403
Loading
Loading
Loading
Loading
+13 −9
Original line number Diff line number Diff line
@@ -226,11 +226,8 @@ type Javadoc struct {
	srcJars     android.Paths
	srcFiles    android.Paths
	sourcepaths android.Paths
	argFiles    android.Paths
	implicits   android.Paths

	args []string

	docZip      android.WritablePath
	stubsSrcJar android.WritablePath
}
@@ -480,15 +477,20 @@ func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
		j.sourcepaths = android.PathsForModuleSrc(ctx, []string{"."})
	}

	j.argFiles = android.PathsForModuleSrc(ctx, j.properties.Arg_files)
	return deps
}

func (j *Javadoc) expandArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
	var argFiles android.Paths
	argFilesMap := map[string]string{}
	argFileLabels := []string{}

	for _, label := range j.properties.Arg_files {
		var paths = android.PathsForModuleSrc(ctx, []string{label})
		if _, exists := argFilesMap[label]; !exists {
			argFilesMap[label] = strings.Join(paths.Strings(), " ")
			argFilesMap[label] = strings.Join(cmd.PathsForInputs(paths), " ")
			argFileLabels = append(argFileLabels, label)
			argFiles = append(argFiles, paths...)
		} else {
			ctx.ModuleErrorf("multiple arg_files for %q, %q and %q",
				label, argFilesMap[label], paths)
@@ -508,7 +510,7 @@ func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
	}

	for _, flag := range flags {
		args, err := android.Expand(flag, func(name string) (string, error) {
		expanded, err := android.Expand(flag, func(name string) (string, error) {
			if strings.HasPrefix(name, "location ") {
				label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
				if paths, ok := argFilesMap[label]; ok {
@@ -526,10 +528,10 @@ func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
		if err != nil {
			ctx.PropertyErrorf(argsPropertyName, "%s", err.Error())
		}
		j.args = append(j.args, args)
		cmd.Flag(expanded)
	}

	return deps
	cmd.Implicits(argFiles)
}

func (j *Javadoc) DepsMutator(ctx android.BottomUpMutatorContext) {
@@ -563,6 +565,8 @@ func (j *Javadoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
		Flag("-XDignore.symbol.file").
		Flag("-Xdoclint:none")

	j.expandArgs(ctx, cmd)

	rule.Command().
		BuiltTool("soong_zip").
		Flag("-write_if_changed").
@@ -821,7 +825,7 @@ func (d *Droiddoc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
			deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths)
	}

	cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
	d.expandArgs(ctx, cmd)

	if d.properties.Compat_config != nil {
		compatConfig := android.PathForModuleSrc(ctx, String(d.properties.Compat_config))
+2 −8
Original line number Diff line number Diff line
@@ -284,7 +284,7 @@ func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.Ru
		cmd.Flag("--include-annotations")

		validatingNullability :=
			android.InList("--validate-nullability-from-merged-stubs", d.Javadoc.args) ||
			strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
				String(d.properties.Validate_nullability_from_list) != ""

		migratingNullability := String(d.properties.Previous_api) != ""
@@ -509,14 +509,8 @@ func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	d.inclusionAnnotationsFlags(ctx, cmd)
	d.apiLevelsAnnotationsFlags(ctx, cmd)

	if android.InList("--generate-documentation", d.Javadoc.args) {
		// Currently Metalava have the ability to invoke Javadoc in a separate process.
		// Pass "-nodocs" to suppress the Javadoc invocation when Metalava receives
		// "--generate-documentation" arg. This is not needed when Metalava removes this feature.
		d.Javadoc.args = append(d.Javadoc.args, "-nodocs")
	}
	d.expandArgs(ctx, cmd)

	cmd.Flag(strings.Join(d.Javadoc.args, " ")).Implicits(d.Javadoc.argFiles)
	for _, o := range d.Javadoc.properties.Out {
		cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
	}
+14 −0
Original line number Diff line number Diff line
@@ -81,10 +81,19 @@ func TestDroidstubs(t *testing.T) {

func TestDroidstubsSandbox(t *testing.T) {
	ctx, _ := testJavaWithFS(t, `
		genrule {
			name: "foo",
			out: ["foo.txt"],
			cmd: "touch $(out)",
		}

		droidstubs {
			name: "bar-stubs",
			srcs: ["bar-doc/a.java"],
			sandbox: true,

			args: "--reference $(location :foo)",
			arg_files: [":foo"],
		}
		`,
		map[string][]byte{
@@ -96,6 +105,11 @@ func TestDroidstubsSandbox(t *testing.T) {
	if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
		t.Errorf("Expected inputs %q, got %q", w, g)
	}

	manifest := android.RuleBuilderSboxProtoForTests(t, m.Output("metalava.sbox.textproto"))
	if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
		t.Errorf("Expected command to contain %q, got %q", w, g)
	}
}

func TestDroidstubsWithSystemModules(t *testing.T) {