From 624058e6a5ec2490ce531fefa6579ca809214bd5 Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Tue, 24 Dec 2019 18:38:06 +0900 Subject: [PATCH] Dedup generating xml file for java_sdk_library Permission XML file for java_sdk_library is generated by java_sdk_library itself now. And, build rule is switched to android.WriteFile since "echo -e" is not supported from build server. (-e is printed to output) Bug: 145474221 Test: m com.android.cronet and check its permissions xml file also, m org.chromium.net.cronet.xml (created dynamically) Change-Id: Iffb119151c49bc4fe6c4386fa267cca193f37dbc --- apex/apex.go | 6 ++-- apex/apex_test.go | 4 +-- java/sdk_library.go | 88 ++++++++++++++++++++------------------------- 3 files changed, 44 insertions(+), 54 deletions(-) diff --git a/apex/apex.go b/apex/apex.go index e0ee1ea40..48957c2e0 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -1024,12 +1024,12 @@ func (a *apexBundle) GenerateAndroidBuildActions(ctx android.ModuleContext) { } filesInfo = append(filesInfo, af) - pf := sdkLib.PermissionFile() - if pf == nil { + pf, _ := sdkLib.OutputFiles(".xml") + if len(pf) != 1 { ctx.PropertyErrorf("java_libs", "%q failed to generate permission XML", depName) return false } - filesInfo = append(filesInfo, newApexFile(ctx, pf, pf.Base(), "etc/permissions", etc, nil)) + filesInfo = append(filesInfo, newApexFile(ctx, pf[0], pf[0].Base(), "etc/permissions", etc, nil)) return true // track transitive dependencies } else { ctx.PropertyErrorf("java_libs", "%q of type %q is not supported", depName, ctx.OtherModuleType(child)) diff --git a/apex/apex_test.go b/apex/apex_test.go index 97600b6c5..a16e31790 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -3288,8 +3288,8 @@ func TestJavaSDKLibrary(t *testing.T) { "etc/permissions/foo.xml", }) // Permission XML should point to the activated path of impl jar of java_sdk_library - genXMLCommand := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml").RuleParams.Command - ensureContains(t, genXMLCommand, ` - - - - -` + permissionsTemplate = `\n` + + `\n` + + `\n` + + ` \n` + + `\n` ) type stubsLibraryDependencyTag struct { @@ -154,7 +152,7 @@ type SdkLibrary struct { systemApiFilePath android.Path testApiFilePath android.Path - permissionFile android.Path + permissionsFile android.Path } var _ Dependency = (*SdkLibrary)(nil) @@ -184,9 +182,7 @@ func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { module.Library.GenerateAndroidBuildActions(ctx) - if module.ApexName() != "" { - module.buildPermissionFile(ctx) - } + module.buildPermissionsFile(ctx) // Record the paths to the header jars of the library (stubs and impl). // When this java_sdk_library is dependened from others via "libs" property, @@ -223,19 +219,28 @@ func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) }) } -func (module *SdkLibrary) buildPermissionFile(ctx android.ModuleContext) { - xmlContent := strings.ReplaceAll(fmt.Sprintf(permissionTemplate, module.BaseModuleName(), module.implPath()), "\n", "\\n") - permissionFile := android.PathForModuleOut(ctx, module.xmlFileName()) +func (module *SdkLibrary) buildPermissionsFile(ctx android.ModuleContext) { + xmlContent := fmt.Sprintf(permissionsTemplate, module.BaseModuleName(), module.implPath()) + permissionsFile := android.PathForModuleOut(ctx, module.xmlFileName()) - rule := android.NewRuleBuilder() - rule.Command().Text("echo -e ").Text(proptools.ShellEscape(xmlContent)).Text(">").Output(permissionFile) - rule.Build(pctx, ctx, "gen_permission_xml", "Generate permission") + ctx.Build(pctx, android.BuildParams{ + Rule: android.WriteFile, + Output: permissionsFile, + Description: "Generating " + module.BaseModuleName() + " permissions", + Args: map[string]string{ + "content": xmlContent, + }, + }) - module.permissionFile = permissionFile + module.permissionsFile = permissionsFile } -func (module *SdkLibrary) PermissionFile() android.Path { - return module.permissionFile +func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) { + switch tag { + case ".xml": + return android.Paths{module.permissionsFile}, nil + } + return module.Library.OutputFiles(tag) } func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries { @@ -578,21 +583,6 @@ func (module *SdkLibrary) createDocs(mctx android.LoadHookContext, apiScope apiS // Creates the xml file that publicizes the runtime library func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) { - - // genrule to generate the xml file content from the template above - // TODO: preserve newlines in the generate xml file. Newlines are being squashed - // in the ninja file. Do we need to have an external tool for this? - xmlContent := fmt.Sprintf(permissionTemplate, module.BaseModuleName(), module.implPath()) - genruleProps := struct { - Name *string - Cmd *string - Out []string - }{} - genruleProps.Name = proptools.StringPtr(module.xmlFileName() + "-gen") - genruleProps.Cmd = proptools.StringPtr("echo '" + xmlContent + "' > $(out)") - genruleProps.Out = []string{module.xmlFileName()} - mctx.CreateModule(genrule.GenRuleFactory, &genruleProps) - // creates a prebuilt_etc module to actually place the xml file under // /etc/permissions etcProps := struct { @@ -605,7 +595,7 @@ func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) { System_ext_specific *bool }{} etcProps.Name = proptools.StringPtr(module.xmlFileName()) - etcProps.Src = proptools.StringPtr(":" + module.xmlFileName() + "-gen") + etcProps.Src = proptools.StringPtr(":" + module.BaseModuleName() + "{.xml}") etcProps.Sub_dir = proptools.StringPtr("permissions") if module.SocSpecific() { etcProps.Soc_specific = proptools.BoolPtr(true)