Merge "Support empty srcs for configurable attrs"
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..9f386ca
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+ "imports": [
+ {
+ "path": "packages/modules/SdkExtensions"
+ }
+ ]
+}
diff --git a/android/apex.go b/android/apex.go
index 4b74436..4618fe9 100644
--- a/android/apex.go
+++ b/android/apex.go
@@ -117,19 +117,6 @@
return false
}
-// InApexByBaseName tells whether this apex variant of the module is part of the given apexVariant
-// or not, where the APEX is specified by its canonical base name, i.e. typically beginning with
-// "com.android.". In particular this function doesn't differentiate between source and prebuilt
-// APEXes, where the latter may have "prebuilt_" prefixes.
-func (i ApexInfo) InApexVariantByBaseName(apexVariant string) bool {
- for _, a := range i.InApexVariants {
- if RemoveOptionalPrebuiltPrefix(a) == apexVariant {
- return true
- }
- }
- return false
-}
-
func (i ApexInfo) InApexModule(apexModuleName string) bool {
for _, a := range i.InApexModules {
if a == apexModuleName {
@@ -669,8 +656,8 @@
mctx.VisitDirectDeps(func(dep Module) {
if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok {
depBase := dep.(ApexModule).apexModuleBase()
- base.ApexProperties.DirectlyInAnyApex = depBase.ApexProperties.DirectlyInAnyApex
- base.ApexProperties.InAnyApex = depBase.ApexProperties.InAnyApex
+ depBase.ApexProperties.DirectlyInAnyApex = base.ApexProperties.DirectlyInAnyApex
+ depBase.ApexProperties.InAnyApex = base.ApexProperties.InAnyApex
}
})
diff --git a/apex/apex.go b/apex/apex.go
index 3448327..926085b 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -2020,7 +2020,7 @@
a.filesInfo = filesInfo
// Set suffix and primaryApexType depending on the ApexType
- buildFlattenedAsDefault := ctx.Config().FlattenApex() && !ctx.Config().UnbundledBuildApps()
+ buildFlattenedAsDefault := ctx.Config().FlattenApex()
switch a.properties.ApexType {
case imageApex:
if buildFlattenedAsDefault {
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 364013f..4d00944 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -7611,6 +7611,80 @@
}))
}
+func TestApexJavaCoverage(t *testing.T) {
+ bp := `
+ apex {
+ name: "myapex",
+ key: "myapex.key",
+ java_libs: ["mylib"],
+ bootclasspath_fragments: ["mybootclasspathfragment"],
+ systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
+ updatable: false,
+ }
+
+ apex_key {
+ name: "myapex.key",
+ public_key: "testkey.avbpubkey",
+ private_key: "testkey.pem",
+ }
+
+ java_library {
+ name: "mylib",
+ srcs: ["mylib.java"],
+ apex_available: ["myapex"],
+ compile_dex: true,
+ }
+
+ bootclasspath_fragment {
+ name: "mybootclasspathfragment",
+ contents: ["mybootclasspathlib"],
+ apex_available: ["myapex"],
+ }
+
+ java_library {
+ name: "mybootclasspathlib",
+ srcs: ["mybootclasspathlib.java"],
+ apex_available: ["myapex"],
+ compile_dex: true,
+ }
+
+ systemserverclasspath_fragment {
+ name: "mysystemserverclasspathfragment",
+ contents: ["mysystemserverclasspathlib"],
+ apex_available: ["myapex"],
+ }
+
+ java_library {
+ name: "mysystemserverclasspathlib",
+ srcs: ["mysystemserverclasspathlib.java"],
+ apex_available: ["myapex"],
+ compile_dex: true,
+ }
+ `
+
+ result := android.GroupFixturePreparers(
+ PrepareForTestWithApexBuildComponents,
+ prepareForTestWithMyapex,
+ java.PrepareForTestWithJavaDefaultModules,
+ android.PrepareForTestWithAndroidBuildComponents,
+ android.FixtureWithRootAndroidBp(bp),
+ android.FixtureMergeEnv(map[string]string{
+ "EMMA_INSTRUMENT": "true",
+ }),
+ ).RunTest(t)
+
+ // Make sure jacoco ran on both mylib and mybootclasspathlib
+ if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
+ t.Errorf("Failed to find jacoco rule for mylib")
+ }
+ if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
+ t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
+ }
+ if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
+ t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
+ }
+}
+
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
diff --git a/apex/prebuilt.go b/apex/prebuilt.go
index 6125ef5..81bfc86 100644
--- a/apex/prebuilt.go
+++ b/apex/prebuilt.go
@@ -228,10 +228,11 @@
})
// Create an ApexInfo for the prebuilt_apex.
+ apexVariationName := android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName())
apexInfo := android.ApexInfo{
- ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
- InApexVariants: []string{mctx.ModuleName()},
- InApexModules: []string{mctx.ModuleName()},
+ ApexVariationName: apexVariationName,
+ InApexVariants: []string{apexVariationName},
+ InApexModules: []string{apexVariationName},
ApexContents: []*android.ApexContents{apexContents},
ForPrebuiltApex: true,
}
diff --git a/bazel/properties.go b/bazel/properties.go
index 3904756..b9d6ead 100644
--- a/bazel/properties.go
+++ b/bazel/properties.go
@@ -331,49 +331,174 @@
HasConfigurableValues() bool
}
-// Represents an attribute whose value is a single label
-type LabelAttribute struct {
- Value Label
+type labelArchValues struct {
X86 Label
X86_64 Label
Arm Label
Arm64 Label
+
+ ConditionsDefault Label
+}
+
+type labelTargetValue struct {
+ // E.g. for android
+ OsValue Label
+
+ // E.g. for android_arm, android_arm64, ...
+ ArchValues labelArchValues
+}
+
+type labelTargetValues struct {
+ Android labelTargetValue
+ Darwin labelTargetValue
+ Fuchsia labelTargetValue
+ Linux labelTargetValue
+ LinuxBionic labelTargetValue
+ Windows labelTargetValue
+
+ ConditionsDefault labelTargetValue
+}
+
+// Represents an attribute whose value is a single label
+type LabelAttribute struct {
+ Value Label
+
+ ArchValues labelArchValues
+
+ TargetValues labelTargetValues
}
func (attr *LabelAttribute) GetValueForArch(arch string) Label {
- switch arch {
- case ARCH_ARM:
- return attr.Arm
- case ARCH_ARM64:
- return attr.Arm64
- case ARCH_X86:
- return attr.X86
- case ARCH_X86_64:
- return attr.X86_64
- case CONDITIONS_DEFAULT:
- return attr.Value
- default:
- panic("Invalid arch type")
+ var v *Label
+ if v = attr.archValuePtrs()[arch]; v == nil {
+ panic(fmt.Errorf("Unknown arch: %s", arch))
}
+ return *v
}
func (attr *LabelAttribute) SetValueForArch(arch string, value Label) {
- switch arch {
- case ARCH_ARM:
- attr.Arm = value
- case ARCH_ARM64:
- attr.Arm64 = value
- case ARCH_X86:
- attr.X86 = value
- case ARCH_X86_64:
- attr.X86_64 = value
- default:
- panic("Invalid arch type")
+ var v *Label
+ if v = attr.archValuePtrs()[arch]; v == nil {
+ panic(fmt.Errorf("Unknown arch: %s", arch))
+ }
+ *v = value
+}
+
+func (attr *LabelAttribute) archValuePtrs() map[string]*Label {
+ return map[string]*Label{
+ ARCH_X86: &attr.ArchValues.X86,
+ ARCH_X86_64: &attr.ArchValues.X86_64,
+ ARCH_ARM: &attr.ArchValues.Arm,
+ ARCH_ARM64: &attr.ArchValues.Arm64,
+ CONDITIONS_DEFAULT: &attr.ArchValues.ConditionsDefault,
}
}
func (attr LabelAttribute) HasConfigurableValues() bool {
- return attr.Arm.Label != "" || attr.Arm64.Label != "" || attr.X86.Label != "" || attr.X86_64.Label != ""
+ for arch := range PlatformArchMap {
+ if attr.GetValueForArch(arch).Label != "" {
+ return true
+ }
+ }
+
+ for os := range PlatformOsMap {
+ if attr.GetOsValueForTarget(os).Label != "" {
+ return true
+ }
+ // TODO(b/187530594): Should we also check arch=CONDITIONS_DEFAULT (not in AllArches)
+ for _, arch := range AllArches {
+ if attr.GetOsArchValueForTarget(os, arch).Label != "" {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func (attr *LabelAttribute) getValueForTarget(os string) labelTargetValue {
+ var v *labelTargetValue
+ if v = attr.targetValuePtrs()[os]; v == nil {
+ panic(fmt.Errorf("Unknown os: %s", os))
+ }
+ return *v
+}
+
+func (attr *LabelAttribute) GetOsValueForTarget(os string) Label {
+ var v *labelTargetValue
+ if v = attr.targetValuePtrs()[os]; v == nil {
+ panic(fmt.Errorf("Unknown os: %s", os))
+ }
+ return v.OsValue
+}
+
+func (attr *LabelAttribute) GetOsArchValueForTarget(os string, arch string) Label {
+ var v *labelTargetValue
+ if v = attr.targetValuePtrs()[os]; v == nil {
+ panic(fmt.Errorf("Unknown os: %s", os))
+ }
+ switch arch {
+ case ARCH_X86:
+ return v.ArchValues.X86
+ case ARCH_X86_64:
+ return v.ArchValues.X86_64
+ case ARCH_ARM:
+ return v.ArchValues.Arm
+ case ARCH_ARM64:
+ return v.ArchValues.Arm64
+ case CONDITIONS_DEFAULT:
+ return v.ArchValues.ConditionsDefault
+ default:
+ panic(fmt.Errorf("Unknown arch: %s\n", arch))
+ }
+}
+
+func (attr *LabelAttribute) setValueForTarget(os string, value labelTargetValue) {
+ var v *labelTargetValue
+ if v = attr.targetValuePtrs()[os]; v == nil {
+ panic(fmt.Errorf("Unknown os: %s", os))
+ }
+ *v = value
+}
+
+func (attr *LabelAttribute) SetOsValueForTarget(os string, value Label) {
+ var v *labelTargetValue
+ if v = attr.targetValuePtrs()[os]; v == nil {
+ panic(fmt.Errorf("Unknown os: %s", os))
+ }
+ v.OsValue = value
+}
+
+func (attr *LabelAttribute) SetOsArchValueForTarget(os string, arch string, value Label) {
+ var v *labelTargetValue
+ if v = attr.targetValuePtrs()[os]; v == nil {
+ panic(fmt.Errorf("Unknown os: %s", os))
+ }
+ switch arch {
+ case ARCH_X86:
+ v.ArchValues.X86 = value
+ case ARCH_X86_64:
+ v.ArchValues.X86_64 = value
+ case ARCH_ARM:
+ v.ArchValues.Arm = value
+ case ARCH_ARM64:
+ v.ArchValues.Arm64 = value
+ case CONDITIONS_DEFAULT:
+ v.ArchValues.ConditionsDefault = value
+ default:
+ panic(fmt.Errorf("Unknown arch: %s\n", arch))
+ }
+}
+
+func (attr *LabelAttribute) targetValuePtrs() map[string]*labelTargetValue {
+ return map[string]*labelTargetValue{
+ OS_ANDROID: &attr.TargetValues.Android,
+ OS_DARWIN: &attr.TargetValues.Darwin,
+ OS_FUCHSIA: &attr.TargetValues.Fuchsia,
+ OS_LINUX: &attr.TargetValues.Linux,
+ OS_LINUX_BIONIC: &attr.TargetValues.LinuxBionic,
+ OS_WINDOWS: &attr.TargetValues.Windows,
+ CONDITIONS_DEFAULT: &attr.TargetValues.ConditionsDefault,
+ }
}
// Arch-specific label_list typed Bazel attribute values. This should correspond
@@ -383,7 +508,6 @@
X86_64 LabelList
Arm LabelList
Arm64 LabelList
- Common LabelList
ConditionsDefault LabelList
}
@@ -629,7 +753,6 @@
X86_64 []string
Arm []string
Arm64 []string
- Common []string
ConditionsDefault []string
}
diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go
index 6aa148e..49f1f42 100644
--- a/bp2build/cc_library_conversion_test.go
+++ b/bp2build/cc_library_conversion_test.go
@@ -808,3 +808,46 @@
)`},
})
}
+
+func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
+ runCcLibraryTestCase(t, bp2buildTestCase{
+ description: "cc_library GetTargetProperties on a LabelAttribute",
+ moduleTypeUnderTest: "cc_library",
+ moduleTypeUnderTestFactory: cc.LibraryFactory,
+ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
+ depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
+ dir: "foo/bar",
+ filesystem: map[string]string{
+ "foo/bar/Android.bp": `
+ cc_library {
+ name: "a",
+ srcs: ["a.cpp"],
+ target: {
+ android_arm: {
+ version_script: "android_arm.map",
+ },
+ linux_bionic_arm64: {
+ version_script: "linux_bionic_arm64.map",
+ },
+ },
+
+ bazel_module: { bp2build_available: true },
+ }
+ `,
+ },
+ blueprint: soongCcLibraryPreamble,
+ expectedBazelTargets: []string{`cc_library(
+ name = "a",
+ copts = [
+ "-Ifoo/bar",
+ "-I$(BINDIR)/foo/bar",
+ ],
+ srcs = ["a.cpp"],
+ version_script = select({
+ "//build/bazel/platforms:android_arm": "android_arm.map",
+ "//build/bazel/platforms:linux_bionic_arm64": "linux_bionic_arm64.map",
+ "//conditions:default": None,
+ }),
+)`},
+ })
+}
diff --git a/bp2build/configurability.go b/bp2build/configurability.go
index 42ff197..7e1a298 100644
--- a/bp2build/configurability.go
+++ b/bp2build/configurability.go
@@ -60,19 +60,67 @@
}
func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) {
- var value reflect.Value
- var archSelects selects
-
- if label.HasConfigurableValues() {
- archSelects = map[string]reflect.Value{}
- for arch, selectKey := range bazel.PlatformArchMap {
- archSelects[selectKey] = reflect.ValueOf(label.GetValueForArch(arch))
- }
- } else {
- value = reflect.ValueOf(label.Value)
+ value := reflect.ValueOf(label.Value)
+ if !label.HasConfigurableValues() {
+ return value, []selects{}
}
- return value, []selects{archSelects}
+ // Keep track of which arches and oses have been used in case we need to raise a warning
+ usedArches := make(map[string]bool)
+ usedOses := make(map[string]bool)
+
+ archSelects := map[string]reflect.Value{}
+ for arch, selectKey := range bazel.PlatformArchMap {
+ archSelects[selectKey] = reflect.ValueOf(label.GetValueForArch(arch))
+ if archSelects[selectKey].IsValid() && !isZero(archSelects[selectKey]) {
+ usedArches[arch] = true
+ }
+ }
+
+ osSelects := map[string]reflect.Value{}
+ for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) {
+ selectKey := bazel.PlatformOsMap[os]
+ osSelects[selectKey] = reflect.ValueOf(label.GetOsValueForTarget(os))
+ if osSelects[selectKey].IsValid() && !isZero(osSelects[selectKey]) {
+ usedOses[os] = true
+ }
+ }
+
+ osArchSelects := make([]selects, 0)
+ for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) {
+ archSelects := make(map[string]reflect.Value)
+ // TODO(b/187530594): Should we also check arch=CONDITIONS_DEFAULT? (not in AllArches)
+ for _, arch := range bazel.AllArches {
+ target := os + "_" + arch
+ selectKey := bazel.PlatformTargetMap[target]
+ archSelects[selectKey] = reflect.ValueOf(label.GetOsArchValueForTarget(os, arch))
+ if archSelects[selectKey].IsValid() && !isZero(archSelects[selectKey]) {
+ if _, ok := usedArches[arch]; ok {
+ fmt.Printf("WARNING: Same arch used twice in LabelAttribute select: arch '%s'\n", arch)
+ }
+ if _, ok := usedOses[os]; ok {
+ fmt.Printf("WARNING: Same os used twice in LabelAttribute select: os '%s'\n", os)
+ }
+ }
+ }
+ osArchSelects = append(osArchSelects, archSelects)
+ }
+
+ // Because we have to return a single Label, we can only use one select statement
+ combinedSelects := map[string]reflect.Value{}
+ for k, v := range archSelects {
+ combinedSelects[k] = v
+ }
+ for k, v := range osSelects {
+ combinedSelects[k] = v
+ }
+ for _, osArchSelect := range osArchSelects {
+ for k, v := range osArchSelect {
+ combinedSelects[k] = v
+ }
+ }
+
+ return value, []selects{combinedSelects}
}
func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) {
diff --git a/cc/bp2build.go b/cc/bp2build.go
index d96fd26..5d024f8 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -572,6 +572,10 @@
linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
+ if baseLinkerProps.Version_script != nil {
+ versionScript.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
+ }
+
sharedLibs := baseLinkerProps.Shared_libs
dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
}
@@ -586,6 +590,10 @@
linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
+ if baseLinkerProps.Version_script != nil {
+ versionScript.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
+ }
+
sharedLibs := baseLinkerProps.Shared_libs
dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
}
diff --git a/cc/cc.go b/cc/cc.go
index 8a34dad..aa218bf 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -729,12 +729,6 @@
llndkStubDepTag = dependencyTag{name: "llndk stub"}
)
-type copyDirectlyInAnyApexDependencyTag dependencyTag
-
-func (copyDirectlyInAnyApexDependencyTag) CopyDirectlyInAnyApex() {}
-
-var _ android.CopyDirectlyInAnyApexTag = copyDirectlyInAnyApexDependencyTag{}
-
func IsSharedDepTag(depTag blueprint.DependencyTag) bool {
ccLibDepTag, ok := depTag.(libraryDependencyTag)
return ok && ccLibDepTag.shared()
diff --git a/cc/image.go b/cc/image.go
index 47a424b..c6b209f 100644
--- a/cc/image.go
+++ b/cc/image.go
@@ -579,7 +579,7 @@
// If using a snapshot, the recovery variant under AOSP directories is not needed,
// except for kernel headers, which needs all variants.
- if m.KernelHeadersDecorator() &&
+ if !m.KernelHeadersDecorator() &&
!m.IsSnapshotPrebuilt() &&
usingRecoverySnapshot &&
!isRecoveryProprietaryModule(mctx) {
diff --git a/cmd/go2bp/Android.bp b/cmd/go2bp/Android.bp
new file mode 100644
index 0000000..53d70b6
--- /dev/null
+++ b/cmd/go2bp/Android.bp
@@ -0,0 +1,26 @@
+// Copyright 2021 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+blueprint_go_binary {
+ name: "go2bp",
+ deps: [
+ "blueprint-proptools",
+ "bpfix-lib",
+ ],
+ srcs: ["go2bp.go"],
+}
diff --git a/cmd/go2bp/go2bp.go b/cmd/go2bp/go2bp.go
new file mode 100644
index 0000000..67138f1
--- /dev/null
+++ b/cmd/go2bp/go2bp.go
@@ -0,0 +1,361 @@
+// Copyright 2021 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/json"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "sort"
+ "strings"
+ "text/template"
+
+ "github.com/google/blueprint/proptools"
+
+ "android/soong/bpfix/bpfix"
+)
+
+type RewriteNames []RewriteName
+type RewriteName struct {
+ prefix string
+ repl string
+}
+
+func (r *RewriteNames) String() string {
+ return ""
+}
+
+func (r *RewriteNames) Set(v string) error {
+ split := strings.SplitN(v, "=", 2)
+ if len(split) != 2 {
+ return fmt.Errorf("Must be in the form of <prefix>=<replace>")
+ }
+ *r = append(*r, RewriteName{
+ prefix: split[0],
+ repl: split[1],
+ })
+ return nil
+}
+
+func (r *RewriteNames) GoToBp(name string) string {
+ ret := name
+ for _, r := range *r {
+ prefix := r.prefix
+ if name == prefix {
+ ret = r.repl
+ break
+ }
+ prefix += "/"
+ if strings.HasPrefix(name, prefix) {
+ ret = r.repl + "-" + strings.TrimPrefix(name, prefix)
+ }
+ }
+ return strings.ReplaceAll(ret, "/", "-")
+}
+
+var rewriteNames = RewriteNames{}
+
+type Exclude map[string]bool
+
+func (e Exclude) String() string {
+ return ""
+}
+
+func (e Exclude) Set(v string) error {
+ e[v] = true
+ return nil
+}
+
+var excludes = make(Exclude)
+var excludeDeps = make(Exclude)
+var excludeSrcs = make(Exclude)
+
+type GoModule struct {
+ Dir string
+}
+
+type GoPackage struct {
+ Dir string
+ ImportPath string
+ Name string
+ Imports []string
+ GoFiles []string
+ TestGoFiles []string
+ TestImports []string
+
+ Module *GoModule
+}
+
+func (g GoPackage) IsCommand() bool {
+ return g.Name == "main"
+}
+
+func (g GoPackage) BpModuleType() string {
+ if g.IsCommand() {
+ return "blueprint_go_binary"
+ }
+ return "bootstrap_go_package"
+}
+
+func (g GoPackage) BpName() string {
+ if g.IsCommand() {
+ return filepath.Base(g.ImportPath)
+ }
+ return rewriteNames.GoToBp(g.ImportPath)
+}
+
+func (g GoPackage) BpDeps(deps []string) []string {
+ var ret []string
+ for _, d := range deps {
+ // Ignore stdlib dependencies
+ if !strings.Contains(d, ".") {
+ continue
+ }
+ if _, ok := excludeDeps[d]; ok {
+ continue
+ }
+ name := rewriteNames.GoToBp(d)
+ ret = append(ret, name)
+ }
+ return ret
+}
+
+func (g GoPackage) BpSrcs(srcs []string) []string {
+ var ret []string
+ prefix, err := filepath.Rel(g.Module.Dir, g.Dir)
+ if err != nil {
+ panic(err)
+ }
+ for _, f := range srcs {
+ f = filepath.Join(prefix, f)
+ if _, ok := excludeSrcs[f]; ok {
+ continue
+ }
+ ret = append(ret, f)
+ }
+ return ret
+}
+
+// AllImports combines Imports and TestImports, as blueprint does not differentiate these.
+func (g GoPackage) AllImports() []string {
+ imports := append([]string(nil), g.Imports...)
+ imports = append(imports, g.TestImports...)
+
+ if len(imports) == 0 {
+ return nil
+ }
+
+ // Sort and de-duplicate
+ sort.Strings(imports)
+ j := 0
+ for i := 1; i < len(imports); i++ {
+ if imports[i] == imports[j] {
+ continue
+ }
+ j++
+ imports[j] = imports[i]
+ }
+ return imports[:j+1]
+}
+
+var bpTemplate = template.Must(template.New("bp").Parse(`
+{{.BpModuleType}} {
+ name: "{{.BpName}}",
+ {{- if not .IsCommand}}
+ pkgPath: "{{.ImportPath}}",
+ {{- end}}
+ {{- if .BpDeps .AllImports}}
+ deps: [
+ {{- range .BpDeps .AllImports}}
+ "{{.}}",
+ {{- end}}
+ ],
+ {{- end}}
+ {{- if .BpSrcs .GoFiles}}
+ srcs: [
+ {{- range .BpSrcs .GoFiles}}
+ "{{.}}",
+ {{- end}}
+ ],
+ {{- end}}
+ {{- if .BpSrcs .TestGoFiles}}
+ testSrcs: [
+ {{- range .BpSrcs .TestGoFiles}}
+ "{{.}}",
+ {{- end}}
+ ],
+ {{- end}}
+}
+`))
+
+func rerunForRegen(filename string) error {
+ buf, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+
+ scanner := bufio.NewScanner(bytes.NewBuffer(buf))
+
+ // Skip the first line in the file
+ for i := 0; i < 2; i++ {
+ if !scanner.Scan() {
+ if scanner.Err() != nil {
+ return scanner.Err()
+ } else {
+ return fmt.Errorf("unexpected EOF")
+ }
+ }
+ }
+
+ // Extract the old args from the file
+ line := scanner.Text()
+ if strings.HasPrefix(line, "// go2bp ") {
+ line = strings.TrimPrefix(line, "// go2bp ")
+ } else {
+ return fmt.Errorf("unexpected second line: %q", line)
+ }
+ args := strings.Split(line, " ")
+ lastArg := args[len(args)-1]
+ args = args[:len(args)-1]
+
+ // Append all current command line args except -regen <file> to the ones from the file
+ for i := 1; i < len(os.Args); i++ {
+ if os.Args[i] == "-regen" || os.Args[i] == "--regen" {
+ i++
+ } else {
+ args = append(args, os.Args[i])
+ }
+ }
+ args = append(args, lastArg)
+
+ cmd := os.Args[0] + " " + strings.Join(args, " ")
+ // Re-exec pom2bp with the new arguments
+ output, err := exec.Command("/bin/sh", "-c", cmd).Output()
+ if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
+ return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
+ } else if err != nil {
+ return err
+ }
+
+ return ioutil.WriteFile(filename, output, 0666)
+}
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, `go2bp, a tool to create Android.bp files from go modules
+
+The tool will extract the necessary information from Go files to create an Android.bp that can
+compile them. This needs to be run from the same directory as the go.mod file.
+
+Usage: %s [--rewrite <pkg-prefix>=<replace>] [-exclude <package>] [-regen <file>]
+
+ -rewrite <pkg-prefix>=<replace>
+ rewrite can be used to specify mappings between go package paths and Android.bp modules. The -rewrite
+ option can be specified multiple times. When determining the Android.bp module for a given Go
+ package, mappings are searched in the order they were specified. The first <pkg-prefix> matching
+ either the package directly, or as the prefix '<pkg-prefix>/' will be replaced with <replace>.
+ After all replacements are finished, all '/' characters are replaced with '-'.
+ -exclude <package>
+ Don't put the specified go package in the Android.bp file.
+ -exclude-deps <package>
+ Don't put the specified go package in the dependency lists.
+ -exclude-srcs <module>
+ Don't put the specified source files in srcs or testSrcs lists.
+ -regen <file>
+ Read arguments from <file> and overwrite it.
+
+`, os.Args[0])
+ }
+
+ var regen string
+
+ flag.Var(&excludes, "exclude", "Exclude go package")
+ flag.Var(&excludeDeps, "exclude-dep", "Exclude go package from deps")
+ flag.Var(&excludeSrcs, "exclude-src", "Exclude go file from source lists")
+ flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
+ flag.StringVar(®en, "regen", "", "Rewrite specified file")
+ flag.Parse()
+
+ if regen != "" {
+ err := rerunForRegen(regen)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ os.Exit(0)
+ }
+
+ if flag.NArg() != 0 {
+ fmt.Fprintf(os.Stderr, "Unused argument detected: %v\n", flag.Args())
+ os.Exit(1)
+ }
+
+ if _, err := os.Stat("go.mod"); err != nil {
+ fmt.Fprintln(os.Stderr, "go.mod file not found")
+ os.Exit(1)
+ }
+
+ cmd := exec.Command("go", "list", "-json", "./...")
+ output, err := cmd.Output()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to dump the go packages: %v\n", err)
+ os.Exit(1)
+ }
+ decoder := json.NewDecoder(bytes.NewReader(output))
+
+ pkgs := []GoPackage{}
+ for decoder.More() {
+ pkg := GoPackage{}
+ err := decoder.Decode(&pkg)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to parse json: %v\n", err)
+ os.Exit(1)
+ }
+ pkgs = append(pkgs, pkg)
+ }
+
+ buf := &bytes.Buffer{}
+
+ fmt.Fprintln(buf, "// Automatically generated with:")
+ fmt.Fprintln(buf, "// go2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
+
+ for _, pkg := range pkgs {
+ if excludes[pkg.ImportPath] {
+ continue
+ }
+ if len(pkg.BpSrcs(pkg.GoFiles)) == 0 && len(pkg.BpSrcs(pkg.TestGoFiles)) == 0 {
+ continue
+ }
+ err := bpTemplate.Execute(buf, pkg)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error writing", pkg.Name, err)
+ os.Exit(1)
+ }
+ }
+
+ out, err := bpfix.Reformat(buf.String())
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error formatting output", err)
+ os.Exit(1)
+ }
+
+ os.Stdout.WriteString(out)
+}
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 7abb67f..0336fb6 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -17,7 +17,6 @@
import (
"flag"
"fmt"
- "io/fs"
"io/ioutil"
"os"
"path/filepath"
@@ -364,54 +363,41 @@
// - won't be overwritten by corresponding bp2build generated files
//
// And return their paths so they can be left out of the Bazel workspace dir (i.e. ignored)
-func getPathsToIgnoredBuildFiles(topDir string, outDir string, generatedRoot string) ([]string, error) {
+func getPathsToIgnoredBuildFiles(topDir string, generatedRoot string, srcDirBazelFiles []string) []string {
paths := make([]string, 0)
- err := filepath.WalkDir(topDir, func(fFullPath string, fDirEntry fs.DirEntry, err error) error {
+ for _, srcDirBazelFileRelativePath := range srcDirBazelFiles {
+ srcDirBazelFileFullPath := shared.JoinPath(topDir, srcDirBazelFileRelativePath)
+ fileInfo, err := os.Stat(srcDirBazelFileFullPath)
if err != nil {
- // Warn about error, but continue trying to walk the directory tree
- fmt.Fprintf(os.Stderr, "WARNING: Error accessing path '%s', err: %s\n", fFullPath, err)
- return nil
+ // Warn about error, but continue trying to check files
+ fmt.Fprintf(os.Stderr, "WARNING: Error accessing path '%s', err: %s\n", srcDirBazelFileFullPath, err)
+ continue
}
- if fDirEntry.IsDir() {
+ if fileInfo.IsDir() {
// Don't ignore entire directories
- return nil
+ continue
}
- if !(fDirEntry.Name() == "BUILD" || fDirEntry.Name() == "BUILD.bazel") {
+ if !(fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") {
// Don't ignore this file - it is not a build file
- return nil
+ continue
}
- f := strings.TrimPrefix(fFullPath, topDir+"/")
- if strings.HasPrefix(f, ".repo/") {
- // Don't check for files to ignore in the .repo dir (recursively)
- return fs.SkipDir
- }
- if strings.HasPrefix(f, outDir+"/") {
- // Don't check for files to ignore in the out dir (recursively)
- return fs.SkipDir
- }
- if strings.HasPrefix(f, generatedRoot) {
- // Don't check for files to ignore in the bp2build dir (recursively)
- // NOTE: This is usually under outDir
- return fs.SkipDir
- }
- fDir := filepath.Dir(f)
- if android.ShouldKeepExistingBuildFileForDir(fDir) {
+ srcDirBazelFileDir := filepath.Dir(srcDirBazelFileRelativePath)
+ if android.ShouldKeepExistingBuildFileForDir(srcDirBazelFileDir) {
// Don't ignore this existing build file
- return nil
+ continue
}
- f_bp2build := shared.JoinPath(topDir, generatedRoot, f)
- if _, err := os.Stat(f_bp2build); err == nil {
+ correspondingBp2BuildFile := shared.JoinPath(topDir, generatedRoot, srcDirBazelFileRelativePath)
+ if _, err := os.Stat(correspondingBp2BuildFile); err == nil {
// If bp2build generated an alternate BUILD file, don't exclude this workspace path
// BUILD file clash resolution happens later in the symlink forest creation
- return nil
+ continue
}
- fmt.Fprintf(os.Stderr, "Ignoring existing BUILD file: %s\n", f)
- paths = append(paths, f)
- return nil
- })
+ fmt.Fprintf(os.Stderr, "Ignoring existing BUILD file: %s\n", srcDirBazelFileRelativePath)
+ paths = append(paths, srcDirBazelFileRelativePath)
+ }
- return paths, err
+ return paths
}
// Returns temporary symlink forest excludes necessary for bazel build //external/... (and bazel build //frameworks/...) to work
@@ -431,6 +417,22 @@
return excludes
}
+// Read the bazel.list file that the Soong Finder already dumped earlier (hopefully)
+// It contains the locations of BUILD files, BUILD.bazel files, etc. in the source dir
+func getExistingBazelRelatedFiles(topDir string) ([]string, error) {
+ bazelFinderFile := filepath.Join(filepath.Dir(bootstrap.CmdlineArgs.ModuleListFile), "bazel.list")
+ if !filepath.IsAbs(bazelFinderFile) {
+ // Assume this was a relative path under topDir
+ bazelFinderFile = filepath.Join(topDir, bazelFinderFile)
+ }
+ data, err := ioutil.ReadFile(bazelFinderFile)
+ if err != nil {
+ return nil, err
+ }
+ files := strings.Split(strings.TrimSpace(string(data)), "\n")
+ return files, nil
+}
+
// Run Soong in the bp2build mode. This creates a standalone context that registers
// an alternate pipeline of mutators and singletons specifically for generating
// Bazel BUILD files instead of Ninja files.
@@ -489,14 +491,13 @@
excludes = append(excludes, bootstrap.CmdlineArgs.NinjaBuildDir)
}
- // FIXME: Don't hardcode this here
- topLevelOutDir := "out"
-
- pathsToIgnoredBuildFiles, err := getPathsToIgnoredBuildFiles(topDir, topLevelOutDir, generatedRoot)
+ existingBazelRelatedFiles, err := getExistingBazelRelatedFiles(topDir)
if err != nil {
- fmt.Fprintf(os.Stderr, "Error walking SrcDir: '%s': %s\n", configuration.SrcDir(), err)
+ fmt.Fprintf(os.Stderr, "Error determining existing Bazel-related files: %s\n", err)
os.Exit(1)
}
+
+ pathsToIgnoredBuildFiles := getPathsToIgnoredBuildFiles(topDir, generatedRoot, existingBazelRelatedFiles)
excludes = append(excludes, pathsToIgnoredBuildFiles...)
excludes = append(excludes, getTemporaryExcludes()...)
diff --git a/java/base.go b/java/base.go
index f7989b8..440b004 100644
--- a/java/base.go
+++ b/java/base.go
@@ -1778,3 +1778,9 @@
func (j *Module) ProvidesUsesLib() *string {
return j.usesLibraryProperties.Provides_uses_lib
}
+
+type ModuleWithStem interface {
+ Stem() string
+}
+
+var _ ModuleWithStem = (*Module)(nil)
diff --git a/java/bootclasspath_fragment.go b/java/bootclasspath_fragment.go
index 188d362..792193f 100644
--- a/java/bootclasspath_fragment.go
+++ b/java/bootclasspath_fragment.go
@@ -22,6 +22,7 @@
"android/soong/android"
"android/soong/dexpreopt"
+
"github.com/google/blueprint/proptools"
"github.com/google/blueprint"
@@ -76,12 +77,17 @@
return true
}
+// Contents of bootclasspath fragments in an apex are considered to be directly in the apex, as if
+// they were listed in java_libs.
+func (b bootclasspathFragmentContentDependencyTag) CopyDirectlyInAnyApex() {}
+
// The tag used for the dependency between the bootclasspath_fragment module and its contents.
var bootclasspathFragmentContentDepTag = bootclasspathFragmentContentDependencyTag{}
var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathFragmentContentDepTag
var _ android.ReplaceSourceWithPrebuilt = bootclasspathFragmentContentDepTag
var _ android.SdkMemberTypeDependencyTag = bootclasspathFragmentContentDepTag
+var _ android.CopyDirectlyInAnyApexTag = bootclasspathFragmentContentDepTag
func IsBootclasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool {
return tag == bootclasspathFragmentContentDepTag
@@ -496,10 +502,21 @@
global := dexpreopt.GetGlobalConfig(ctx)
+ // Convert content names to their appropriate stems, in case a test library is overriding an actual boot jar
+ var stems []string
+ for _, name := range b.properties.Contents {
+ dep := ctx.GetDirectDepWithTag(name, bootclasspathFragmentContentDepTag)
+ if m, ok := dep.(ModuleWithStem); ok {
+ stems = append(stems, m.Stem())
+ } else {
+ ctx.PropertyErrorf("contents", "%v is not a ModuleWithStem", name)
+ }
+ }
+
// Only create configs for updatable boot jars. Non-updatable boot jars must be part of the
// platform_bootclasspath's classpath proto config to guarantee that they come before any
// updatable jars at runtime.
- return global.UpdatableBootJars.Filter(b.properties.Contents)
+ return global.UpdatableBootJars.Filter(stems)
}
func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
diff --git a/java/hiddenapi_singleton.go b/java/hiddenapi_singleton.go
index bdf055a..52934a3 100644
--- a/java/hiddenapi_singleton.go
+++ b/java/hiddenapi_singleton.go
@@ -171,7 +171,7 @@
// A platform variant is required but this is for an apex so ignore it.
return false
}
- } else if !apexInfo.InApexVariantByBaseName(requiredApex) {
+ } else if !apexInfo.InApexVariant(requiredApex) {
// An apex variant for a specific apex is required but this is the wrong apex.
return false
}
diff --git a/java/systemserver_classpath_fragment.go b/java/systemserver_classpath_fragment.go
index 9111c30..7ffb056 100644
--- a/java/systemserver_classpath_fragment.go
+++ b/java/systemserver_classpath_fragment.go
@@ -17,6 +17,7 @@
import (
"android/soong/android"
"android/soong/dexpreopt"
+
"github.com/google/blueprint"
)
@@ -97,16 +98,33 @@
func (s *SystemServerClasspathModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
global := dexpreopt.GetGlobalConfig(ctx)
+ // Convert content names to their appropriate stems, in case a test library is overriding an actual boot jar
+ var stems []string
+ for _, name := range s.properties.Contents {
+ dep := ctx.GetDirectDepWithTag(name, systemServerClasspathFragmentContentDepTag)
+ if m, ok := dep.(ModuleWithStem); ok {
+ stems = append(stems, m.Stem())
+ } else {
+ ctx.PropertyErrorf("contents", "%v is not a ModuleWithStem", name)
+ }
+ }
+
// Only create configs for updatable boot jars. Non-updatable system server jars must be part of the
// platform_systemserverclasspath's classpath proto config to guarantee that they come before any
// updatable jars at runtime.
- return global.UpdatableSystemServerJars.Filter(s.properties.Contents)
+ return global.UpdatableSystemServerJars.Filter(stems)
}
type systemServerClasspathFragmentContentDependencyTag struct {
blueprint.BaseDependencyTag
}
+// Contents of system server fragments in an apex are considered to be directly in the apex, as if
+// they were listed in java_libs.
+func (systemServerClasspathFragmentContentDependencyTag) CopyDirectlyInAnyApex() {}
+
+var _ android.CopyDirectlyInAnyApexTag = systemServerClasspathFragmentContentDepTag
+
// The tag used for the dependency between the systemserverclasspath_fragment module and its contents.
var systemServerClasspathFragmentContentDepTag = systemServerClasspathFragmentContentDependencyTag{}