From dcb61292695c740d1fac37a8e06ddfc763cebe83 Mon Sep 17 00:00:00 2001 From: Sasha Smundak Date: Thu, 8 Dec 2022 10:41:33 -0800 Subject: [PATCH] Streamline AndroidMk generation Add AndroidMkEmitAssignList to emit a line to assign the items from the given list of string arrays. Test: treehugger Change-Id: Id5acbef38ea4e91349bd2461f226db352d4b8123 --- android/androidmk.go | 35 ++++++++++++++++++++++++++----- apex/androidmk.go | 49 ++++++++++---------------------------------- apex/apex_test.go | 22 ++++++++++---------- bpf/bpf.go | 2 +- java/robolectric.go | 5 ++--- 5 files changed, 55 insertions(+), 58 deletions(-) diff --git a/android/androidmk.go b/android/androidmk.go index 846d5061c..6346401c7 100644 --- a/android/androidmk.go +++ b/android/androidmk.go @@ -489,11 +489,11 @@ func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string { // Write the license variables to Make for AndroidMkData.Custom(..) methods that do not call WriteAndroidMkData(..) // It's required to propagate the license metadata even for module types that have non-standard interfaces to Make. func (a *AndroidMkEntries) WriteLicenseVariables(w io.Writer) { - fmt.Fprintln(w, "LOCAL_LICENSE_KINDS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_KINDS"], " ")) - fmt.Fprintln(w, "LOCAL_LICENSE_CONDITIONS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_CONDITIONS"], " ")) - fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(a.EntryMap["LOCAL_NOTICE_FILE"], " ")) + AndroidMkEmitAssignList(w, "LOCAL_LICENSE_KINDS", a.EntryMap["LOCAL_LICENSE_KINDS"]) + AndroidMkEmitAssignList(w, "LOCAL_LICENSE_CONDITIONS", a.EntryMap["LOCAL_LICENSE_CONDITIONS"]) + AndroidMkEmitAssignList(w, "LOCAL_NOTICE_FILE", a.EntryMap["LOCAL_NOTICE_FILE"]) if pn, ok := a.EntryMap["LOCAL_LICENSE_PACKAGE_NAME"]; ok { - fmt.Fprintln(w, "LOCAL_LICENSE_PACKAGE_NAME :=", strings.Join(pn, " ")) + AndroidMkEmitAssignList(w, "LOCAL_LICENSE_PACKAGE_NAME", pn) } } @@ -672,7 +672,7 @@ func (a *AndroidMkEntries) write(w io.Writer) { w.Write(a.header.Bytes()) for _, name := range a.entryOrder { - fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " ")) + AndroidMkEmitAssignList(w, name, a.EntryMap[name]) } w.Write(a.footer.Bytes()) } @@ -972,3 +972,28 @@ func AndroidMkDataPaths(data []DataPath) []string { } return testFiles } + +// AndroidMkEmitAssignList emits the line +// +// VAR := ITEM ... +// +// Items are the elements to the given set of lists +// If all the passed lists are empty, no line will be emitted +func AndroidMkEmitAssignList(w io.Writer, varName string, lists ...[]string) { + doPrint := false + for _, l := range lists { + if doPrint = len(l) > 0; doPrint { + break + } + } + if !doPrint { + return + } + fmt.Fprint(w, varName, " :=") + for _, l := range lists { + for _, item := range l { + fmt.Fprint(w, " ", item) + } + } + fmt.Fprintln(w) +} diff --git a/apex/androidmk.go b/apex/androidmk.go index 0fc971bb0..b76f6bdd3 100644 --- a/apex/androidmk.go +++ b/apex/androidmk.go @@ -154,9 +154,7 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo if a.primaryApexType && !symbolFilesNotNeeded { fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated) } - if len(fi.symlinks) > 0 { - fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " ")) - } + android.AndroidMkEmitAssignList(w, "LOCAL_MODULE_SYMLINKS", fi.symlinks) newDataPaths := []android.DataPath{} for _, path := range fi.dataPaths { dataOutPath := modulePath + ":" + path.SrcPath.Rel() @@ -165,9 +163,7 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo seenDataOutPaths[dataOutPath] = true } } - if len(newDataPaths) > 0 { - fmt.Fprintln(w, "LOCAL_TEST_DATA :=", strings.Join(android.AndroidMkDataPaths(newDataPaths), " ")) - } + android.AndroidMkEmitAssignList(w, "LOCAL_TEST_DATA", android.AndroidMkDataPaths(newDataPaths)) } else { modulePath = pathWhenActivated fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated) @@ -236,9 +232,7 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo // we will have foo.apk.apk fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.stem(), ".apk")) if app, ok := fi.module.(*java.AndroidApp); ok { - if jniCoverageOutputs := app.JniCoverageOutputs(); len(jniCoverageOutputs) > 0 { - fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", strings.Join(jniCoverageOutputs.Strings(), " ")) - } + android.AndroidMkEmitAssignList(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE", app.JniCoverageOutputs().Strings()) if jniLibSymbols := app.JNISymbolsInstalls(modulePath); len(jniLibSymbols) > 0 { fmt.Fprintln(w, "LOCAL_SOONG_JNI_LIBS_SYMBOLS :=", jniLibSymbols.String()) } @@ -275,7 +269,7 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo } for _, name := range commonProperties { if value, ok := apexAndroidMkData.Entries.EntryMap[name]; ok { - fmt.Fprintln(w, name+" := "+strings.Join(value, " ")) + android.AndroidMkEmitAssignList(w, name, value) } } @@ -285,9 +279,7 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo for _, o := range a.overridableProperties.Overrides { patterns = append(patterns, "%."+o+a.suffix) } - if len(patterns) > 0 { - fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " ")) - } + android.AndroidMkEmitAssignList(w, "LOCAL_OVERRIDES_MODULES", patterns) } // File_contexts of flattened APEXes should be merged into file_contexts.bin @@ -306,13 +298,6 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo } func (a *apexBundle) writeRequiredModules(w io.Writer, moduleNames []string) { - if len(moduleNames) > 0 { - fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " ")) - } - if len(a.requiredDeps) > 0 { - fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " ")) - } - var required []string var targetRequired []string var hostRequired []string @@ -324,16 +309,9 @@ func (a *apexBundle) writeRequiredModules(w io.Writer, moduleNames []string) { targetRequired = append(targetRequired, fi.targetRequiredModuleNames...) hostRequired = append(hostRequired, fi.hostRequiredModuleNames...) } - - if len(required) > 0 { - fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " ")) - } - if len(targetRequired) > 0 { - fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " ")) - } - if len(hostRequired) > 0 { - fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " ")) - } + android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", moduleNames, a.requiredDeps, required) + android.AndroidMkEmitAssignList(w, "LOCAL_TARGET_REQUIRED_MODULES", targetRequired) + android.AndroidMkEmitAssignList(w, "LOCAL_HOST_REQUIRED_MODULES", hostRequired) } func (a *apexBundle) androidMkForType() android.AndroidMkData { @@ -383,13 +361,11 @@ func (a *apexBundle) androidMkForType() android.AndroidMkData { } for _, name := range commonProperties { if value, ok := data.Entries.EntryMap[name]; ok { - fmt.Fprintln(w, name+" := "+strings.Join(value, " ")) + android.AndroidMkEmitAssignList(w, name, value) } } - if len(a.overridableProperties.Overrides) > 0 { - fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " ")) - } + android.AndroidMkEmitAssignList(w, "LOCAL_OVERRIDES_MODULES", a.overridableProperties.Overrides) a.writeRequiredModules(w, moduleNames) fmt.Fprintln(w, "include $(BUILD_PREBUILT)") @@ -397,10 +373,7 @@ func (a *apexBundle) androidMkForType() android.AndroidMkData { if apexType == imageApex { fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).BUNDLE :=", a.bundleModuleFile.String()) } - if len(a.lintReports) > 0 { - fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).LINT_REPORTS :=", - strings.Join(a.lintReports.Strings(), " ")) - } + android.AndroidMkEmitAssignList(w, "ALL_MODULES.$(my_register_name).LINT_REPORTS", a.lintReports.Strings()) if a.installedFilesFile != nil { goal := "checkbuild" diff --git a/apex/apex_test.go b/apex/apex_test.go index 876a052b2..1f2be357b 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -2997,7 +2997,7 @@ func TestAndroidMk_VendorApexRequired(t *testing.T) { var builder strings.Builder data.Custom(&builder, name, prefix, "", data) androidMk := builder.String() - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex libc.vendor libm.vendor libdl.vendor\n") } func TestAndroidMkWritesCommonProperties(t *testing.T) { @@ -5699,7 +5699,7 @@ func TestInstallExtraFlattenedApexes(t *testing.T) { var builder strings.Builder mk.Custom(&builder, ab.Name(), "TARGET_", "", mk) androidMk := builder.String() - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n") } func TestErrorsIfDepsAreNotEnabled(t *testing.T) { @@ -7053,9 +7053,9 @@ func TestCarryRequiredModuleNames(t *testing.T) { var builder strings.Builder data.Custom(&builder, name, prefix, "", data) androidMk := builder.String() - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n") - ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n") - ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex a b\n") + ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n") + ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n") } func TestSymlinksFromApexToSystem(t *testing.T) { @@ -7237,7 +7237,7 @@ func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) { ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n") ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n") // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib` - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n") } func TestApexWithJniLibs(t *testing.T) { @@ -8750,7 +8750,7 @@ func TestPreferredPrebuiltSharedLibDep(t *testing.T) { // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't // a thing there. - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex otherlib\n") } func TestExcludeDependency(t *testing.T) { @@ -9144,7 +9144,7 @@ func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) { var builder strings.Builder data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data) androidMk := builder.String() - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex\n") } func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) { @@ -9220,7 +9220,7 @@ func TestAndroidMk_RequiredModules(t *testing.T) { var builder strings.Builder data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data) androidMk := builder.String() - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherapex") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex") } func TestAndroidMk_RequiredDeps(t *testing.T) { @@ -9244,7 +9244,7 @@ func TestAndroidMk_RequiredDeps(t *testing.T) { var builder strings.Builder data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data) androidMk := builder.String() - ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo") + ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n") flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle) flattenedBundle.requiredDeps = append(flattenedBundle.requiredDeps, "foo") @@ -9252,7 +9252,7 @@ func TestAndroidMk_RequiredDeps(t *testing.T) { var flattenedBuilder strings.Builder flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData) flattenedAndroidMk := flattenedBuilder.String() - ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES += foo") + ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n") } func TestApexOutputFileProducer(t *testing.T) { diff --git a/bpf/bpf.go b/bpf/bpf.go index d91180b91..45009c1e7 100644 --- a/bpf/bpf.go +++ b/bpf/bpf.go @@ -240,7 +240,7 @@ func (bpf *bpf) AndroidMk() android.AndroidMkData { fmt.Fprintln(w, "include $(CLEAR_VARS)", " # bpf.bpf") fmt.Fprintln(w, "LOCAL_MODULE := ", name) data.Entries.WriteLicenseVariables(w) - fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(names, " ")) + android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", names) fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") }, } diff --git a/java/robolectric.go b/java/robolectric.go index 938abe138..6e8d5913c 100644 --- a/java/robolectric.go +++ b/java/robolectric.go @@ -330,11 +330,10 @@ func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, test fmt.Fprintln(w, "") fmt.Fprintln(w, "include $(CLEAR_VARS)", " # java.robolectricTest") fmt.Fprintln(w, "LOCAL_MODULE :=", name) - fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module) - fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " ")) + android.AndroidMkEmitAssignList(w, "LOCAL_JAVA_LIBRARIES", []string{module}, r.libs) fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for)) fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String()) - fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " ")) + android.AndroidMkEmitAssignList(w, "LOCAL_ROBOTEST_FILES", tests) if t := r.robolectricProperties.Test_options.Timeout; t != nil { fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t) }