Merge "Upgrade to golang protobuf api v2"
diff --git a/android/arch.go b/android/arch.go
index 583793e..7ca7336 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -255,7 +255,7 @@
// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
// with or without the Bionic libc runtime.
func (os OsType) Linux() bool {
- return os == Android || os == Linux || os == LinuxBionic
+ return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
}
// newOsType constructs an OsType and adds it to the global lists.
@@ -305,6 +305,8 @@
NoOsType OsType
// Linux is the OS for the Linux kernel plus the glibc runtime.
Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
+ // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
+ LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64)
// Darwin is the OS for MacOS/Darwin host machines.
Darwin = newOsType("darwin", Host, false, X86_64)
// LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
@@ -863,6 +865,8 @@
"Android64",
"Android32",
"Bionic",
+ "Glibc",
+ "Musl",
"Linux",
"Not_windows",
"Arm_on_x86",
@@ -1108,6 +1112,30 @@
}
}
+ if os == Linux {
+ field := "Glibc"
+ prefix := "target.glibc"
+ if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
+ mergePropertyStruct(ctx, genProps, bionicProperties)
+ }
+ }
+
+ if os == LinuxMusl {
+ field := "Musl"
+ prefix := "target.musl"
+ if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
+ mergePropertyStruct(ctx, genProps, bionicProperties)
+ }
+
+ // Special case: to ease the transition from glibc to musl, apply linux_glibc
+ // properties (which has historically mean host linux) to musl variants.
+ field = "Linux_glibc"
+ prefix = "target.linux_glibc"
+ if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
+ mergePropertyStruct(ctx, genProps, bionicProperties)
+ }
+ }
+
// Handle target OS properties in the form:
// target: {
// linux_glibc: {
@@ -1310,6 +1338,16 @@
if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
result = append(result, osArchProperties)
}
+
+ if os == LinuxMusl {
+ // Special case: to ease the transition from glibc to musl, apply linux_glibc
+ // properties (which has historically mean host linux) to musl variants.
+ field := "Linux_glibc_" + archType.Name
+ userFriendlyField := "target.linux_glibc_" + archType.Name
+ if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
+ result = append(result, osArchProperties)
+ }
+ }
}
// Handle arm on x86 properties in the form:
@@ -1375,11 +1413,14 @@
}
// determineBuildOS stores the OS and architecture used for host targets used during the build into
-// config based on the runtime OS and architecture determined by Go.
+// config based on the runtime OS and architecture determined by Go and the product configuration.
func determineBuildOS(config *config) {
config.BuildOS = func() OsType {
switch runtime.GOOS {
case "linux":
+ if Bool(config.productVariables.HostMusl) {
+ return LinuxMusl
+ }
return Linux
case "darwin":
return Darwin
diff --git a/android/arch_test.go b/android/arch_test.go
index 2a2fd45..a828321 100644
--- a/android/arch_test.go
+++ b/android/arch_test.go
@@ -508,9 +508,12 @@
bionic: { a: ["bionic"] },
host: { a: ["host"] },
android: { a: ["android"] },
+ glibc: { a: ["glibc"] },
+ musl: { a: ["musl"] },
linux_bionic: { a: ["linux_bionic"] },
linux: { a: ["linux"] },
linux_glibc: { a: ["linux_glibc"] },
+ linux_musl: { a: ["linux_musl"] },
windows: { a: ["windows"], enabled: true },
darwin: { a: ["darwin"] },
not_windows: { a: ["not_windows"] },
@@ -522,6 +525,8 @@
linux_x86_64: { a: ["linux_x86_64"] },
linux_glibc_x86: { a: ["linux_glibc_x86"] },
linux_glibc_x86_64: { a: ["linux_glibc_x86_64"] },
+ linux_musl_x86: { a: ["linux_musl_x86"] },
+ linux_musl_x86_64: { a: ["linux_musl_x86_64"] },
darwin_x86_64: { a: ["darwin_x86_64"] },
windows_x86: { a: ["windows_x86"] },
windows_x86_64: { a: ["windows_x86_64"] },
@@ -563,12 +568,12 @@
{
module: "foo",
variant: "linux_glibc_x86_64",
- property: []string{"root", "host", "linux", "linux_glibc", "not_windows", "x86_64", "lib64", "linux_x86_64", "linux_glibc_x86_64"},
+ property: []string{"root", "host", "linux", "glibc", "linux_glibc", "not_windows", "x86_64", "lib64", "linux_x86_64", "linux_glibc_x86_64"},
},
{
module: "foo",
variant: "linux_glibc_x86",
- property: []string{"root", "host", "linux", "linux_glibc", "not_windows", "x86", "lib32", "linux_x86", "linux_glibc_x86"},
+ property: []string{"root", "host", "linux", "glibc", "linux_glibc", "not_windows", "x86", "lib32", "linux_x86", "linux_glibc_x86"},
},
},
},
@@ -595,6 +600,23 @@
},
},
{
+ name: "linux_musl",
+ goOS: "linux",
+ preparer: FixtureModifyConfig(modifyTestConfigForMusl),
+ results: []result{
+ {
+ module: "foo",
+ variant: "linux_musl_x86_64",
+ property: []string{"root", "host", "linux", "musl", "linux_glibc", "linux_musl", "not_windows", "x86_64", "lib64", "linux_x86_64", "linux_musl_x86_64", "linux_glibc_x86_64"},
+ },
+ {
+ module: "foo",
+ variant: "linux_musl_x86",
+ property: []string{"root", "host", "linux", "musl", "linux_glibc", "linux_musl", "not_windows", "x86", "lib32", "linux_x86", "linux_musl_x86", "linux_glibc_x86"},
+ },
+ },
+ },
+ {
name: "darwin",
goOS: "darwin",
results: []result{
diff --git a/android/bazel.go b/android/bazel.go
index d40e650..26e7deb 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -127,11 +127,22 @@
var (
// Keep any existing BUILD files (and do not generate new BUILD files) for these directories
+ // in the synthetic Bazel workspace.
bp2buildKeepExistingBuildFile = map[string]bool{
// This is actually build/bazel/build.BAZEL symlinked to ./BUILD
".":/*recursive = */ false,
- "build/bazel":/* recursive = */ true,
+ // build/bazel/examples/apex/... BUILD files should be generated, so
+ // build/bazel is not recursive. Instead list each subdirectory under
+ // build/bazel explicitly.
+ "build/bazel":/* recursive = */ false,
+ "build/bazel/examples/android_app":/* recursive = */ true,
+ "build/bazel/bazel_skylib":/* recursive = */ true,
+ "build/bazel/rules":/* recursive = */ true,
+ "build/bazel/rules_cc":/* recursive = */ true,
+ "build/bazel/tests":/* recursive = */ true,
+ "build/bazel/platforms":/* recursive = */ true,
+ "build/bazel/product_variables":/* recursive = */ true,
"build/pesto":/* recursive = */ true,
// external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
@@ -152,6 +163,8 @@
"system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
"system/libbase": Bp2BuildDefaultTrueRecursively,
"system/logging/liblog": Bp2BuildDefaultTrueRecursively,
+ "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
+ "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
"external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
"external/fmtlib": Bp2BuildDefaultTrueRecursively,
"external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
@@ -208,6 +221,9 @@
"libjemalloc5_integrationtest",
"libjemalloc5_stresstestlib",
"libjemalloc5_unittest",
+
+ // APEX support
+ "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' and 'libc_malloc_hooks'
}
// Per-module denylist of cc_library modules to only generate the static
diff --git a/android/bazel_paths.go b/android/bazel_paths.go
index 26cacdb..b050774 100644
--- a/android/bazel_paths.go
+++ b/android/bazel_paths.go
@@ -147,6 +147,10 @@
return BazelLabelForModuleSrcExcludes(ctx, []string{path}, []string(nil)).Includes[0]
}
+func BazelLabelForModuleDepSingle(ctx BazelConversionPathContext, path string) bazel.Label {
+ return BazelLabelForModuleDepsExcludes(ctx, []string{path}, []string(nil)).Includes[0]
+}
+
// BazelLabelForModuleSrc expects a list of path (relative to local module directory) and module
// references (":<module>") and returns a bazel.LabelList{} containing the resolved references in
// paths, relative to the local module, or Bazel-labels (absolute if in a different package or
diff --git a/android/config.go b/android/config.go
index 871986c..b3b8f3c 100644
--- a/android/config.go
+++ b/android/config.go
@@ -362,6 +362,19 @@
config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
}
+func modifyTestConfigForMusl(config Config) {
+ delete(config.Targets, config.BuildOS)
+ config.productVariables.HostMusl = boolPtr(true)
+ determineBuildOS(config.config)
+ config.Targets[config.BuildOS] = []Target{
+ {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
+ {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
+ }
+
+ config.BuildOSTarget = config.Targets[config.BuildOS][0]
+ config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
+}
+
// TestArchConfig returns a Config object suitable for using for tests that
// need to run the arch mutator.
func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
@@ -1838,16 +1851,16 @@
func (c *config) BootJars() []string {
return c.Once(earlyBootJarsKey, func() interface{} {
list := c.productVariables.BootJars.CopyOfJars()
- return append(list, c.productVariables.UpdatableBootJars.CopyOfJars()...)
+ return append(list, c.productVariables.ApexBootJars.CopyOfJars()...)
}).([]string)
}
-func (c *config) NonUpdatableBootJars() ConfiguredJarList {
+func (c *config) NonApexBootJars() ConfiguredJarList {
return c.productVariables.BootJars
}
-func (c *config) UpdatableBootJars() ConfiguredJarList {
- return c.productVariables.UpdatableBootJars
+func (c *config) ApexBootJars() ConfiguredJarList {
+ return c.productVariables.ApexBootJars
}
func (c *config) RBEWrapper() string {
diff --git a/android/module.go b/android/module.go
index 5f34e62..5e2e06a 100644
--- a/android/module.go
+++ b/android/module.go
@@ -1529,7 +1529,7 @@
var installDeps []*installPathsDepSet
var packagingSpecs []*packagingSpecsDepSet
ctx.VisitDirectDeps(func(dep Module) {
- if IsInstallDepNeeded(ctx.OtherModuleDependencyTag(dep)) && !dep.IsHideFromMake() {
+ if IsInstallDepNeeded(ctx.OtherModuleDependencyTag(dep)) && !dep.IsHideFromMake() && !dep.IsSkipInstall() {
installDeps = append(installDeps, dep.base().installFilesDepSet)
packagingSpecs = append(packagingSpecs, dep.base().packagingSpecsDepSet)
}
@@ -2389,8 +2389,7 @@
if !b.BazelConversionMode() {
panic("cannot call ModuleFromName if not in bazel conversion mode")
}
- if len(name) > 1 && (name[0] == ':' || (name[0] == '/' && name[1] == '/')) {
- moduleName, _ := SrcIsModuleWithTag(name)
+ if moduleName, _ := SrcIsModuleWithTag(name); moduleName != "" {
return b.bp.ModuleFromName(moduleName)
} else {
return b.bp.ModuleFromName(name)
diff --git a/android/packaging_test.go b/android/packaging_test.go
index f91dc5d..ff7446c 100644
--- a/android/packaging_test.go
+++ b/android/packaging_test.go
@@ -18,13 +18,15 @@
"testing"
"github.com/google/blueprint"
+ "github.com/google/blueprint/proptools"
)
// Module to be packaged
type componentTestModule struct {
ModuleBase
props struct {
- Deps []string
+ Deps []string
+ Skip_install *bool
}
}
@@ -49,6 +51,9 @@
builtFile := PathForModuleOut(ctx, m.Name())
dir := ctx.Target().Arch.ArchType.Multilib
installDir := PathForModuleInstall(ctx, dir)
+ if proptools.Bool(m.props.Skip_install) {
+ m.SkipInstall()
+ }
ctx.InstallFile(installDir, m.Name(), builtFile)
}
@@ -365,3 +370,31 @@
}
`, []string{"lib64/foo"})
}
+
+func TestPackagingWithSkipInstallDeps(t *testing.T) {
+ // package -[dep]-> foo -[dep]-> bar -[dep]-> baz
+ // OK SKIPPED
+ multiTarget := false
+ runPackagingTest(t, multiTarget,
+ `
+ component {
+ name: "foo",
+ deps: ["bar"],
+ }
+
+ component {
+ name: "bar",
+ deps: ["baz"],
+ skip_install: true,
+ }
+
+ component {
+ name: "baz",
+ }
+
+ package_module {
+ name: "package",
+ deps: ["foo"],
+ }
+ `, []string{"lib64/foo"})
+}
diff --git a/android/paths.go b/android/paths.go
index bec8a51..99db22f 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -1667,7 +1667,7 @@
partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
} else {
osName := os.String()
- if os == Linux {
+ if os == Linux || os == LinuxMusl {
// instead of linux_glibc
osName = "linux"
}
diff --git a/android/variable.go b/android/variable.go
index d0a23aa..0fb9078 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -203,6 +203,7 @@
HostArch *string `json:",omitempty"`
HostSecondaryArch *string `json:",omitempty"`
+ HostMusl *bool `json:",omitempty"`
CrossHost *string `json:",omitempty"`
CrossHostArch *string `json:",omitempty"`
@@ -252,8 +253,8 @@
UncompressPrivAppDex *bool `json:",omitempty"`
ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
- BootJars ConfiguredJarList `json:",omitempty"`
- UpdatableBootJars ConfiguredJarList `json:",omitempty"`
+ BootJars ConfiguredJarList `json:",omitempty"`
+ ApexBootJars ConfiguredJarList `json:",omitempty"`
IntegerOverflowExcludePaths []string `json:",omitempty"`
@@ -440,8 +441,8 @@
Malloc_pattern_fill_contents: boolPtr(false),
Safestack: boolPtr(false),
- BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
- UpdatableBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
+ BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
+ ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
}
if runtime.GOOS == "linux" {
diff --git a/androidmk/parser/make_strings.go b/androidmk/parser/make_strings.go
index 3c4815e..aac4c4e 100644
--- a/androidmk/parser/make_strings.go
+++ b/androidmk/parser/make_strings.go
@@ -278,6 +278,15 @@
}
}
+// If MakeString is $(var) after trimming, returns var
+func (ms *MakeString) SingleVariable() (*MakeString, bool) {
+ if len(ms.Strings) != 2 || strings.TrimSpace(ms.Strings[0]) != "" ||
+ strings.TrimSpace(ms.Strings[1]) != "" {
+ return nil, false
+ }
+ return ms.Variables[0].Name, true
+}
+
func splitAnyN(s, sep string, n int) []string {
ret := []string{}
for n == -1 || n > 1 {
diff --git a/androidmk/parser/parser.go b/androidmk/parser/parser.go
index 5afef65..d24efc1 100644
--- a/androidmk/parser/parser.go
+++ b/androidmk/parser/parser.go
@@ -216,13 +216,14 @@
// Nothing
case "else":
p.ignoreSpaces()
- if p.tok != '\n' {
+ if p.tok != '\n' && p.tok != '#' {
d = p.scanner.TokenText()
p.accept(scanner.Ident)
if d == "ifdef" || d == "ifndef" || d == "ifeq" || d == "ifneq" {
d = "el" + d
p.ignoreSpaces()
expression = p.parseExpression()
+ expression.TrimRightSpaces()
} else {
p.errorf("expected ifdef/ifndef/ifeq/ifneq, found %s", d)
}
diff --git a/apex/apex.go b/apex/apex.go
index d385ac1..149f782 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -3187,7 +3187,17 @@
// For Bazel / bp2build
type bazelApexBundleAttributes struct {
- Manifest bazel.LabelAttribute
+ Manifest bazel.LabelAttribute
+ Android_manifest bazel.LabelAttribute
+ File_contexts bazel.LabelAttribute
+ Key bazel.LabelAttribute
+ Certificate bazel.LabelAttribute
+ Min_sdk_version string
+ Updatable bazel.BoolAttribute
+ Installable bazel.BoolAttribute
+ Native_shared_libs bazel.LabelListAttribute
+ Binaries bazel.StringListAttribute
+ Prebuilts bazel.LabelListAttribute
}
type bazelApexBundle struct {
@@ -3220,14 +3230,68 @@
func apexBundleBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexBundle) {
var manifestLabelAttribute bazel.LabelAttribute
-
- manifestStringPtr := module.properties.Manifest
if module.properties.Manifest != nil {
- manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *manifestStringPtr))
+ manifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Manifest))
+ }
+
+ var androidManifestLabelAttribute bazel.LabelAttribute
+ if module.properties.AndroidManifest != nil {
+ androidManifestLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.AndroidManifest))
+ }
+
+ var fileContextsLabelAttribute bazel.LabelAttribute
+ if module.properties.File_contexts != nil {
+ fileContextsLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *module.properties.File_contexts))
+ }
+
+ var minSdkVersion string
+ if module.properties.Min_sdk_version != nil {
+ minSdkVersion = *module.properties.Min_sdk_version
+ }
+
+ var keyLabelAttribute bazel.LabelAttribute
+ if module.overridableProperties.Key != nil {
+ keyLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *module.overridableProperties.Key))
+ }
+
+ var certificateLabelAttribute bazel.LabelAttribute
+ if module.overridableProperties.Certificate != nil {
+ certificateLabelAttribute.SetValue(android.BazelLabelForModuleDepSingle(ctx, *module.overridableProperties.Certificate))
+ }
+
+ nativeSharedLibs := module.properties.ApexNativeDependencies.Native_shared_libs
+ nativeSharedLibsLabelList := android.BazelLabelForModuleDeps(ctx, nativeSharedLibs)
+ nativeSharedLibsLabelListAttribute := bazel.MakeLabelListAttribute(nativeSharedLibsLabelList)
+
+ prebuilts := module.properties.Prebuilts
+ prebuiltsLabelList := android.BazelLabelForModuleDeps(ctx, prebuilts)
+ prebuiltsLabelListAttribute := bazel.MakeLabelListAttribute(prebuiltsLabelList)
+
+ binaries := module.properties.ApexNativeDependencies.Binaries
+ binariesStringListAttribute := bazel.MakeStringListAttribute(binaries)
+
+ var updatableAttribute bazel.BoolAttribute
+ if module.properties.Updatable != nil {
+ updatableAttribute.Value = module.properties.Updatable
+ }
+
+ var installableAttribute bazel.BoolAttribute
+ if module.properties.Installable != nil {
+ installableAttribute.Value = module.properties.Installable
}
attrs := &bazelApexBundleAttributes{
- Manifest: manifestLabelAttribute,
+ Manifest: manifestLabelAttribute,
+ Android_manifest: androidManifestLabelAttribute,
+ File_contexts: fileContextsLabelAttribute,
+ Min_sdk_version: minSdkVersion,
+ Key: keyLabelAttribute,
+ Certificate: certificateLabelAttribute,
+ Updatable: updatableAttribute,
+ Installable: installableAttribute,
+ Native_shared_libs: nativeSharedLibsLabelListAttribute,
+ Binaries: binariesStringListAttribute,
+ Prebuilts: prebuiltsLabelListAttribute,
}
props := bazel.BazelTargetModuleProperties{
diff --git a/apex/apex_test.go b/apex/apex_test.go
index d6c7142..f58bf6c 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -7264,7 +7264,7 @@
})
}
-func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
+func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
t.Helper()
bp += `
apex_key {
@@ -7289,11 +7289,11 @@
PrepareForTestWithApexBuildComponents,
android.PrepareForTestWithNeverallowRules(rules),
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
- updatableBootJars := make([]string, 0, len(apexBootJars))
- for _, apexBootJar := range apexBootJars {
- updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
+ apexBootJars := make([]string, 0, len(bootJars))
+ for _, apexBootJar := range bootJars {
+ apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
}
- variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
+ variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
}),
fs.AddToFixture(),
).
diff --git a/apex/bootclasspath_fragment_test.go b/apex/bootclasspath_fragment_test.go
index 5cd3eab..6098989 100644
--- a/apex/bootclasspath_fragment_test.go
+++ b/apex/bootclasspath_fragment_test.go
@@ -748,7 +748,7 @@
prepareForTestWithMyapex,
// Configure bootclasspath jars to ensure that hidden API encoding is performed on them.
java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz"),
- java.FixtureConfigureUpdatableBootJars("myapex:foo", "myapex:bar"),
+ java.FixtureConfigureApexBootJars("myapex:foo", "myapex:bar"),
// Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
// is disabled.
android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
diff --git a/apex/key.go b/apex/key.go
index 8b33b59..32a7ce1 100644
--- a/apex/key.go
+++ b/apex/key.go
@@ -20,6 +20,7 @@
"strings"
"android/soong/android"
+ "android/soong/bazel"
"github.com/google/blueprint/proptools"
)
@@ -33,10 +34,13 @@
func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
ctx.RegisterModuleType("apex_key", ApexKeyFactory)
ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
+
+ android.RegisterBp2BuildMutator("apex_key", ApexKeyBp2Build)
}
type apexKey struct {
android.ModuleBase
+ android.BazelModuleBase
properties apexKeyProperties
@@ -61,6 +65,7 @@
module := &apexKey{}
module.AddProperties(&module.properties)
android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
+ android.InitBazelModule(module)
return module
}
@@ -190,3 +195,68 @@
func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
}
+
+// For Bazel / bp2build
+
+type bazelApexKeyAttributes struct {
+ Public_key bazel.LabelAttribute
+ Private_key bazel.LabelAttribute
+}
+
+type bazelApexKey struct {
+ android.BazelTargetModuleBase
+ bazelApexKeyAttributes
+}
+
+func BazelApexKeyFactory() android.Module {
+ module := &bazelApexKey{}
+ module.AddProperties(&module.bazelApexKeyAttributes)
+ android.InitBazelTargetModule(module)
+ return module
+}
+
+func ApexKeyBp2Build(ctx android.TopDownMutatorContext) {
+ module, ok := ctx.Module().(*apexKey)
+ if !ok {
+ // Not an APEX key
+ return
+ }
+ if !module.ConvertWithBp2build(ctx) {
+ return
+ }
+ if ctx.ModuleType() != "apex_key" {
+ return
+ }
+
+ apexKeyBp2BuildInternal(ctx, module)
+}
+
+func apexKeyBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexKey) {
+ var privateKeyLabelAttribute bazel.LabelAttribute
+ if module.properties.Private_key != nil {
+ privateKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Private_key))
+ }
+
+ var publicKeyLabelAttribute bazel.LabelAttribute
+ if module.properties.Public_key != nil {
+ publicKeyLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Public_key))
+ }
+
+ attrs := &bazelApexKeyAttributes{
+ Private_key: privateKeyLabelAttribute,
+ Public_key: publicKeyLabelAttribute,
+ }
+
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "apex_key",
+ Bzl_load_location: "//build/bazel/rules:apex_key.bzl",
+ }
+
+ ctx.CreateBazelTargetModule(BazelApexKeyFactory, module.Name(), props, attrs)
+}
+
+func (m *bazelApexKey) Name() string {
+ return m.BaseModuleName()
+}
+
+func (m *bazelApexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
diff --git a/apex/platform_bootclasspath_test.go b/apex/platform_bootclasspath_test.go
index 7209c02..eaee20d 100644
--- a/apex/platform_bootclasspath_test.go
+++ b/apex/platform_bootclasspath_test.go
@@ -173,7 +173,7 @@
prepareForTestWithMyapex,
// Configure some libraries in the art and framework boot images.
java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz", "platform:foo"),
- java.FixtureConfigureUpdatableBootJars("myapex:bar"),
+ java.FixtureConfigureApexBootJars("myapex:bar"),
java.PrepareForTestWithJavaSdkLibraryFiles,
java.FixtureWithLastReleaseApis("foo"),
).RunTestWithBp(t, `
@@ -288,7 +288,7 @@
"com.android.art:quuz",
"platform:foo",
- // The configured contents of UpdatableBootJars.
+ // The configured contents of ApexBootJars.
"myapex:bar",
})
@@ -313,7 +313,7 @@
`com.android.art:quuz`,
`platform:foo`,
- // The configured contents of UpdatableBootJars.
+ // The configured contents of ApexBootJars.
`myapex:bar`,
// The fragments.
@@ -348,7 +348,7 @@
// if the dependency on myapex:foo is filtered out because of either of those conditions then
// the dependencies resolved by the platform_bootclasspath will not match the configured list
// and so will fail the test.
- java.FixtureConfigureUpdatableBootJars("myapex:foo", "myapex:bar"),
+ java.FixtureConfigureApexBootJars("myapex:foo", "myapex:bar"),
java.PrepareForTestWithJavaSdkLibraryFiles,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
@@ -490,7 +490,7 @@
result := android.GroupFixturePreparers(
prepareForTestWithPlatformBootclasspath,
prepareForTestWithMyapex,
- java.FixtureConfigureUpdatableBootJars("myapex:foo"),
+ java.FixtureConfigureApexBootJars("myapex:foo"),
android.FixtureWithRootAndroidBp(`
platform_bootclasspath {
name: "platform-bootclasspath",
diff --git a/bazel/configurability.go b/bazel/configurability.go
index 3277bd0..f5f0913 100644
--- a/bazel/configurability.go
+++ b/bazel/configurability.go
@@ -30,6 +30,7 @@
osAndroid = "android"
osDarwin = "darwin"
osLinux = "linux_glibc"
+ osLinuxMusl = "linux_musl"
osLinuxBionic = "linux_bionic"
osWindows = "windows"
@@ -41,6 +42,8 @@
osArchDarwinX86_64 = "darwin_x86_64"
osArchLinuxX86 = "linux_glibc_x86"
osArchLinuxX86_64 = "linux_glibc_x86_64"
+ osArchLinuxMuslX86 = "linux_musl_x86"
+ osArchLinuxMuslX86_64 = "linux_musl_x86_64"
osArchLinuxBionicArm64 = "linux_bionic_arm64"
osArchLinuxBionicX86_64 = "linux_bionic_x86_64"
osArchWindowsX86 = "windows_x86"
@@ -82,6 +85,7 @@
osAndroid: "//build/bazel/platforms/os:android",
osDarwin: "//build/bazel/platforms/os:darwin",
osLinux: "//build/bazel/platforms/os:linux",
+ osLinuxMusl: "//build/bazel/platforms/os:linux_musl",
osLinuxBionic: "//build/bazel/platforms/os:linux_bionic",
osWindows: "//build/bazel/platforms/os:windows",
conditionsDefault: ConditionsDefaultSelectKey, // The default condition of an os select map.
@@ -100,6 +104,8 @@
osArchDarwinX86_64: "//build/bazel/platforms/os_arch:darwin_x86_64",
osArchLinuxX86: "//build/bazel/platforms/os_arch:linux_glibc_x86",
osArchLinuxX86_64: "//build/bazel/platforms/os_arch:linux_glibc_x86_64",
+ osArchLinuxMuslX86: "//build/bazel/platforms/os_arch:linux_musl_x86",
+ osArchLinuxMuslX86_64: "//build/bazel/platforms/os_arch:linux_musl_x86_64",
osArchLinuxBionicArm64: "//build/bazel/platforms/os_arch:linux_bionic_arm64",
osArchLinuxBionicX86_64: "//build/bazel/platforms/os_arch:linux_bionic_x86_64",
osArchWindowsX86: "//build/bazel/platforms/os_arch:windows_x86",
diff --git a/bp2build/Android.bp b/bp2build/Android.bp
index dded14b..9ec637a 100644
--- a/bp2build/Android.bp
+++ b/bp2build/Android.bp
@@ -23,12 +23,15 @@
"soong-bazel",
"soong-cc",
"soong-cc-config",
+ "soong-etc",
"soong-genrule",
"soong-python",
"soong-sh",
],
testSrcs: [
+ "android_app_certificate_conversion_test.go",
"apex_conversion_test.go",
+ "apex_key_conversion_test.go",
"build_conversion_test.go",
"bzl_conversion_test.go",
"cc_library_conversion_test.go",
@@ -36,6 +39,7 @@
"cc_library_static_conversion_test.go",
"cc_object_conversion_test.go",
"conversion_test.go",
+ "prebuilt_etc_conversion_test.go",
"python_binary_conversion_test.go",
"sh_conversion_test.go",
"testing.go",
diff --git a/bp2build/android_app_certificate_conversion_test.go b/bp2build/android_app_certificate_conversion_test.go
new file mode 100644
index 0000000..022c687
--- /dev/null
+++ b/bp2build/android_app_certificate_conversion_test.go
@@ -0,0 +1,49 @@
+// 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 bp2build
+
+import (
+ "android/soong/android"
+ "android/soong/java"
+
+ "testing"
+)
+
+func runAndroidAppCertificateTestCase(t *testing.T, tc bp2buildTestCase) {
+ t.Helper()
+ runBp2BuildTestCase(t, registerAndroidAppCertificateModuleTypes, tc)
+}
+
+func registerAndroidAppCertificateModuleTypes(ctx android.RegistrationContext) {
+}
+
+func TestAndroidAppCertificateSimple(t *testing.T) {
+ runAndroidAppCertificateTestCase(t, bp2buildTestCase{
+ description: "Android app certificate - simple example",
+ moduleTypeUnderTest: "android_app_certificate",
+ moduleTypeUnderTestFactory: java.AndroidAppCertificateFactory,
+ moduleTypeUnderTestBp2BuildMutator: java.AndroidAppCertificateBp2Build,
+ filesystem: map[string]string{},
+ blueprint: `
+android_app_certificate {
+ name: "com.android.apogee.cert",
+ certificate: "chamber_of_secrets_dir",
+}
+`,
+ expectedBazelTargets: []string{`android_app_certificate(
+ name = "com.android.apogee.cert",
+ certificate = "chamber_of_secrets_dir",
+)`}})
+}
diff --git a/bp2build/apex_conversion_test.go b/bp2build/apex_conversion_test.go
index f4a1016..456f18a 100644
--- a/bp2build/apex_conversion_test.go
+++ b/bp2build/apex_conversion_test.go
@@ -17,6 +17,9 @@
import (
"android/soong/android"
"android/soong/apex"
+ "android/soong/cc"
+ "android/soong/java"
+
"testing"
)
@@ -26,6 +29,13 @@
}
func registerApexModuleTypes(ctx android.RegistrationContext) {
+ // CC module types needed as they can be APEX dependencies
+ cc.RegisterCCBuildComponents(ctx)
+
+ ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
+ ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory)
+ ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
+ ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
}
func TestApexBundleSimple(t *testing.T) {
@@ -36,14 +46,114 @@
moduleTypeUnderTestBp2BuildMutator: apex.ApexBundleBp2Build,
filesystem: map[string]string{},
blueprint: `
+apex_key {
+ name: "com.android.apogee.key",
+ public_key: "com.android.apogee.avbpubkey",
+ private_key: "com.android.apogee.pem",
+ bazel_module: { bp2build_available: false },
+}
+
+android_app_certificate {
+ name: "com.android.apogee.certificate",
+ certificate: "com.android.apogee",
+ bazel_module: { bp2build_available: false },
+}
+
+cc_library {
+ name: "native_shared_lib_1",
+ bazel_module: { bp2build_available: false },
+}
+
+cc_library {
+ name: "native_shared_lib_2",
+ bazel_module: { bp2build_available: false },
+}
+
+// TODO(b/194878861): Add bp2build support for prebuilt_etc
+cc_library {
+ name: "pretend_prebuilt_1",
+ bazel_module: { bp2build_available: false },
+}
+
+// TODO(b/194878861): Add bp2build support for prebuilt_etc
+cc_library {
+ name: "pretend_prebuilt_2",
+ bazel_module: { bp2build_available: false },
+}
+
+filegroup {
+ name: "com.android.apogee-file_contexts",
+ srcs: [
+ "com.android.apogee-file_contexts",
+ ],
+ bazel_module: { bp2build_available: false },
+}
+
apex {
- name: "apogee",
- manifest: "manifest.json",
+ name: "com.android.apogee",
+ manifest: "apogee_manifest.json",
+ androidManifest: "ApogeeAndroidManifest.xml",
+ file_contexts: "com.android.apogee-file_contexts",
+ min_sdk_version: "29",
+ key: "com.android.apogee.key",
+ certificate: "com.android.apogee.certificate",
+ updatable: false,
+ installable: false,
+ native_shared_libs: [
+ "native_shared_lib_1",
+ "native_shared_lib_2",
+ ],
+ binaries: [
+ "binary_1",
+ "binary_2",
+ ],
+ prebuilts: [
+ "pretend_prebuilt_1",
+ "pretend_prebuilt_2",
+ ],
}
`,
expectedBazelTargets: []string{`apex(
- name = "apogee",
- manifest = "manifest.json",
+ name = "com.android.apogee",
+ android_manifest = "ApogeeAndroidManifest.xml",
+ binaries = [
+ "binary_1",
+ "binary_2",
+ ],
+ certificate = ":com.android.apogee.certificate",
+ file_contexts = ":com.android.apogee-file_contexts",
+ installable = False,
+ key = ":com.android.apogee.key",
+ manifest = "apogee_manifest.json",
+ min_sdk_version = "29",
+ native_shared_libs = [
+ ":native_shared_lib_1",
+ ":native_shared_lib_2",
+ ],
+ prebuilts = [
+ ":pretend_prebuilt_1",
+ ":pretend_prebuilt_2",
+ ],
+ updatable = False,
+)`}})
+}
+
+func TestApexBundleDefaultPropertyValues(t *testing.T) {
+ runApexTestCase(t, bp2buildTestCase{
+ description: "apex - default property values",
+ moduleTypeUnderTest: "apex",
+ moduleTypeUnderTestFactory: apex.BundleFactory,
+ moduleTypeUnderTestBp2BuildMutator: apex.ApexBundleBp2Build,
+ filesystem: map[string]string{},
+ blueprint: `
+apex {
+ name: "com.android.apogee",
+ manifest: "apogee_manifest.json",
+}
+`,
+ expectedBazelTargets: []string{`apex(
+ name = "com.android.apogee",
+ manifest = "apogee_manifest.json",
)`}})
}
diff --git a/bp2build/apex_key_conversion_test.go b/bp2build/apex_key_conversion_test.go
new file mode 100644
index 0000000..8e1aa09
--- /dev/null
+++ b/bp2build/apex_key_conversion_test.go
@@ -0,0 +1,51 @@
+// 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 bp2build
+
+import (
+ "android/soong/android"
+ "android/soong/apex"
+
+ "testing"
+)
+
+func runApexKeyTestCase(t *testing.T, tc bp2buildTestCase) {
+ t.Helper()
+ runBp2BuildTestCase(t, registerApexKeyModuleTypes, tc)
+}
+
+func registerApexKeyModuleTypes(ctx android.RegistrationContext) {
+}
+
+func TestApexKeySimple(t *testing.T) {
+ runApexKeyTestCase(t, bp2buildTestCase{
+ description: "apex key - simple example",
+ moduleTypeUnderTest: "apex_key",
+ moduleTypeUnderTestFactory: apex.ApexKeyFactory,
+ moduleTypeUnderTestBp2BuildMutator: apex.ApexKeyBp2Build,
+ filesystem: map[string]string{},
+ blueprint: `
+apex_key {
+ name: "com.android.apogee.key",
+ public_key: "com.android.apogee.avbpubkey",
+ private_key: "com.android.apogee.pem",
+}
+`,
+ expectedBazelTargets: []string{`apex_key(
+ name = "com.android.apogee.key",
+ private_key = "com.android.apogee.pem",
+ public_key = "com.android.apogee.avbpubkey",
+)`}})
+}
diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go
index df4924b..9ac28a5 100644
--- a/bp2build/cc_object_conversion_test.go
+++ b/bp2build/cc_object_conversion_test.go
@@ -46,7 +46,7 @@
blueprint: `cc_object {
name: "foo",
local_include_dirs: ["include"],
- default_shared_libs: [],
+ system_shared_libs: [],
cflags: [
"-Wno-gcc-compat",
"-Wall",
@@ -84,7 +84,7 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
local_include_dirs: ["include"],
srcs: [
"a/b/*.h",
@@ -137,14 +137,14 @@
},
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
srcs: ["a/b/c.c"],
objs: ["bar"],
}
cc_object {
name: "bar",
- default_shared_libs: [],
+ system_shared_libs: [],
srcs: ["x/y/z.c"],
}
`,
@@ -182,7 +182,7 @@
},
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
srcs: ["a/b/c.c"],
include_build_directory: false,
}
@@ -204,7 +204,7 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
include_build_directory: false,
product_variables: {
platform_sdk_version: {
@@ -235,7 +235,7 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
srcs: ["a.cpp"],
arch: {
x86: {
@@ -275,7 +275,7 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
srcs: ["base.cpp"],
arch: {
x86: {
@@ -331,7 +331,7 @@
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
- default_shared_libs: [],
+ system_shared_libs: [],
srcs: ["base.cpp"],
target: {
android: {
diff --git a/bp2build/prebuilt_etc_conversion_test.go b/bp2build/prebuilt_etc_conversion_test.go
new file mode 100644
index 0000000..4e25d1b
--- /dev/null
+++ b/bp2build/prebuilt_etc_conversion_test.go
@@ -0,0 +1,55 @@
+// 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 bp2build
+
+import (
+ "android/soong/android"
+ "android/soong/etc"
+
+ "testing"
+)
+
+func runPrebuiltEtcTestCase(t *testing.T, tc bp2buildTestCase) {
+ t.Helper()
+ runBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc)
+}
+
+func registerPrebuiltEtcModuleTypes(ctx android.RegistrationContext) {
+}
+
+func TestPrebuiltEtcSimple(t *testing.T) {
+ runPrebuiltEtcTestCase(t, bp2buildTestCase{
+ description: "prebuilt_etc - simple example",
+ moduleTypeUnderTest: "prebuilt_etc",
+ moduleTypeUnderTestFactory: etc.PrebuiltEtcFactory,
+ moduleTypeUnderTestBp2BuildMutator: etc.PrebuiltEtcBp2Build,
+ filesystem: map[string]string{},
+ blueprint: `
+prebuilt_etc {
+ name: "apex_tz_version",
+ src: "version/tz_version",
+ filename: "tz_version",
+ sub_dir: "tz",
+ installable: false,
+}
+`,
+ expectedBazelTargets: []string{`prebuilt_etc(
+ name = "apex_tz_version",
+ filename = "tz_version",
+ installable = False,
+ src = "version/tz_version",
+ sub_dir = "tz",
+)`}})
+}
diff --git a/cc/binary.go b/cc/binary.go
index c6d61ab..763d2b9 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -335,7 +335,7 @@
if flags.DynamicLinker != "" {
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
- } else if ctx.toolchain().Bionic() && !binary.static() {
+ } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
}
diff --git a/cc/cc.go b/cc/cc.go
index e3ca4d7..7aec7f2 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -554,8 +554,7 @@
sharedLibs []string
// Note nil and [] are semantically distinct. [] prevents linking against the defaults (usually
// libc, libm, etc.)
- systemSharedLibs []string
- defaultSharedLibs []string
+ systemSharedLibs []string
}
// installer is the interface for an installer helper object. This helper is responsible for
diff --git a/cc/compiler.go b/cc/compiler.go
index b01ba43..34ac47a 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -189,6 +189,11 @@
// variant of the C/C++ module.
Cflags []string
}
+ Platform struct {
+ // List of additional cflags that should be used to build the platform
+ // variant of the C/C++ module.
+ Cflags []string
+ }
}
Proto struct {
@@ -310,6 +315,7 @@
CheckBadCompilerFlags(ctx, "product.cflags", compiler.Properties.Target.Product.Cflags)
CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
CheckBadCompilerFlags(ctx, "vendor_ramdisk.cflags", compiler.Properties.Target.Vendor_ramdisk.Cflags)
+ CheckBadCompilerFlags(ctx, "platform.cflags", compiler.Properties.Target.Platform.Cflags)
esc := proptools.NinjaAndShellEscapeList
@@ -502,6 +508,9 @@
if ctx.inVendorRamdisk() {
flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor_ramdisk.Cflags)...)
}
+ if !ctx.useSdk() {
+ flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Platform.Cflags)...)
+ }
// We can enforce some rules more strictly in the code we own. strict
// indicates if this is code that we can be stricter with. If we have
diff --git a/cc/config/toolchain.go b/cc/config/toolchain.go
index ff556f1..20384a8 100644
--- a/cc/config/toolchain.go
+++ b/cc/config/toolchain.go
@@ -118,6 +118,8 @@
DefaultSharedLibraries() []string
Bionic() bool
+ Glibc() bool
+ Musl() bool
}
type toolchainBase struct {
@@ -194,6 +196,14 @@
return false
}
+func (toolchainBase) Glibc() bool {
+ return false
+}
+
+func (toolchainBase) Musl() bool {
+ return false
+}
+
func (t toolchainBase) ToolPath() string {
return ""
}
diff --git a/cc/config/vndk.go b/cc/config/vndk.go
index 425e349..24e8fa4 100644
--- a/cc/config/vndk.go
+++ b/cc/config/vndk.go
@@ -19,49 +19,66 @@
// has VndkUseCoreVariant set.
// TODO(b/150578172): clean up unstable and non-versioned aidl module
var VndkMustUseVendorVariantList = []string{
- "android.hardware.authsecret-unstable-ndk_platform",
- "android.hardware.authsecret-ndk_platform",
+ "android.hardware.authsecret-V1-ndk",
"android.hardware.authsecret-V1-ndk_platform",
- "android.hardware.automotive.occupant_awareness-ndk_platform",
+ "android.hardware.authsecret-ndk_platform",
+ "android.hardware.authsecret-unstable-ndk_platform",
+ "android.hardware.automotive.occupant_awareness-V1-ndk",
"android.hardware.automotive.occupant_awareness-V1-ndk_platform",
+ "android.hardware.health.storage-V1-ndk",
"android.hardware.health.storage-V1-ndk_platform",
"android.hardware.health.storage-ndk_platform",
"android.hardware.health.storage-unstable-ndk_platform",
- "android.hardware.light-V1-ndk_platform",
- "android.hardware.light-ndk_platform",
+ "android.hardware.identity-V2-ndk",
"android.hardware.identity-V2-ndk_platform",
"android.hardware.identity-ndk_platform",
- "android.hardware.nfc@1.2",
+ "android.hardware.light-V1-ndk",
+ "android.hardware.light-V1-ndk_platform",
+ "android.hardware.light-ndk_platform",
+ "android.hardware.memtrack-V1-ndk",
"android.hardware.memtrack-V1-ndk_platform",
"android.hardware.memtrack-ndk_platform",
"android.hardware.memtrack-unstable-ndk_platform",
+ "android.hardware.nfc@1.2",
+ "android.hardware.oemlock-V1-ndk",
"android.hardware.oemlock-V1-ndk_platform",
"android.hardware.oemlock-ndk_platform",
"android.hardware.oemlock-unstable-ndk_platform",
+ "android.hardware.power-V1-ndk",
"android.hardware.power-V1-ndk_platform",
"android.hardware.power-ndk_platform",
+ "android.hardware.rebootescrow-V1-ndk",
"android.hardware.rebootescrow-V1-ndk_platform",
+ "android.hardware.power.stats-V1-ndk",
"android.hardware.power.stats-V1-ndk_platform",
"android.hardware.power.stats-ndk_platform",
"android.hardware.power.stats-unstable-ndk_platform",
"android.hardware.rebootescrow-ndk_platform",
+ "android.hardware.security.keymint-V1-ndk",
"android.hardware.security.keymint-V1-ndk_platform",
"android.hardware.security.keymint-ndk_platform",
"android.hardware.security.keymint-unstable-ndk_platform",
+ "android.hardware.security.secureclock-V1-ndk",
"android.hardware.security.secureclock-V1-ndk_platform",
- "android.hardware.security.secureclock-unstable-ndk_platform",
"android.hardware.security.secureclock-ndk_platform",
+ "android.hardware.security.secureclock-unstable-ndk_platform",
+ "android.hardware.security.sharedsecret-V1-ndk",
"android.hardware.security.sharedsecret-V1-ndk_platform",
"android.hardware.security.sharedsecret-ndk_platform",
"android.hardware.security.sharedsecret-unstable-ndk_platform",
+ "android.hardware.vibrator-V1-ndk",
"android.hardware.vibrator-V1-ndk_platform",
"android.hardware.vibrator-ndk_platform",
+ "android.hardware.weaver-V1-ndk",
"android.hardware.weaver-V1-ndk_platform",
"android.hardware.weaver-ndk_platform",
"android.hardware.weaver-unstable-ndk_platform",
+ "android.system.keystore2-V1-ndk",
"android.system.keystore2-V1-ndk_platform",
"android.system.keystore2-ndk_platform",
"android.system.keystore2-unstable-ndk_platform",
+ "android.system.suspend-V1-ndk",
+ "android.system.suspend-V1-ndk_platform",
"libbinder",
"libcrypto",
"libexpat",
diff --git a/cc/config/x86_darwin_host.go b/cc/config/x86_darwin_host.go
index d8e70e1..23fe1ab 100644
--- a/cc/config/x86_darwin_host.go
+++ b/cc/config/x86_darwin_host.go
@@ -49,6 +49,7 @@
"-Wl,-syslibroot,${macSdkRoot}",
"-mmacosx-version-min=${macMinVersion}",
"-m64",
+ "-mlinker-version=305",
}
darwinSupportedSdkVersions = []string{
diff --git a/cc/config/x86_linux_host.go b/cc/config/x86_linux_host.go
index 85d95d8..1d66cb7 100644
--- a/cc/config/x86_linux_host.go
+++ b/cc/config/x86_linux_host.go
@@ -36,10 +36,18 @@
"-D__STDC_CONSTANT_MACROS",
"--gcc-toolchain=${LinuxGccRoot}",
- "--sysroot ${LinuxGccRoot}/sysroot",
"-fstack-protector-strong",
}
+ linuxGlibcCflags = []string{
+ "--sysroot ${LinuxGccRoot}/sysroot",
+ }
+
+ linuxMuslCflags = []string{
+ "-D_LIBCPP_HAS_MUSL_LIBC",
+ "-nostdlibinc",
+ }
+
linuxLdflags = []string{
"-Wl,-z,noexecstack",
"-Wl,-z,relro",
@@ -47,9 +55,17 @@
"-Wl,--no-undefined-version",
"--gcc-toolchain=${LinuxGccRoot}",
+ }
+
+ linuxGlibcLdflags = []string{
"--sysroot ${LinuxGccRoot}/sysroot",
}
+ linuxMuslLdflags = []string{
+ "-nostdlib",
+ "-lgcc", "-lgcc_eh",
+ }
+
// Extended cflags
linuxX86Cflags = []string{
"-msse3",
@@ -89,6 +105,12 @@
"rt",
"util",
}, "-l")
+
+ muslCrtBeginStaticBinary, muslCrtEndStaticBinary = []string{"libc_musl_crtbegin_static"}, []string{"crtend_android"}
+ muslCrtBeginSharedBinary, muslCrtEndSharedBinary = []string{"libc_musl_crtbegin_dynamic", "musl_linker_script"}, []string{"libc_musl_crtend"}
+ muslCrtBeginSharedLibrary, muslCrtEndSharedLibrary = []string{"libc_musl_crtbegin_so"}, []string{"libc_musl_crtend_so"}
+
+ muslDefaultSharedLibraries = []string{"libjemalloc5", "libc_musl"}
)
const (
@@ -124,6 +146,13 @@
// Yasm flags
pctx.StaticVariable("LinuxX86YasmFlags", "-f elf32 -m x86")
pctx.StaticVariable("LinuxX8664YasmFlags", "-f elf64 -m amd64")
+
+ pctx.StaticVariable("LinuxGlibcCflags", strings.Join(linuxGlibcCflags, " "))
+ pctx.StaticVariable("LinuxGlibcLdflags", strings.Join(linuxGlibcLdflags, " "))
+ pctx.StaticVariable("LinuxGlibcLldflags", strings.Join(linuxGlibcLdflags, " "))
+ pctx.StaticVariable("LinuxMuslCflags", strings.Join(linuxMuslCflags, " "))
+ pctx.StaticVariable("LinuxMuslLdflags", strings.Join(linuxMuslLdflags, " "))
+ pctx.StaticVariable("LinuxMuslLldflags", strings.Join(linuxMuslLdflags, " "))
}
type toolchainLinux struct {
@@ -224,18 +253,146 @@
return linuxAvailableLibraries
}
-var toolchainLinuxX86Singleton Toolchain = &toolchainLinuxX86{}
-var toolchainLinuxX8664Singleton Toolchain = &toolchainLinuxX8664{}
+// glibc specialization of the linux toolchain
-func linuxX86ToolchainFactory(arch android.Arch) Toolchain {
- return toolchainLinuxX86Singleton
+type toolchainGlibc struct {
}
-func linuxX8664ToolchainFactory(arch android.Arch) Toolchain {
- return toolchainLinuxX8664Singleton
+func (toolchainGlibc) Glibc() bool { return true }
+
+func (toolchainGlibc) Cflags() string {
+ return "${config.LinuxGlibcCflags}"
+}
+
+func (toolchainGlibc) Ldflags() string {
+ return "${config.LinuxGlibcLdflags}"
+}
+
+func (toolchainGlibc) Lldflags() string {
+ return "${config.LinuxGlibcLldflags}"
+}
+
+type toolchainLinuxGlibcX86 struct {
+ toolchainLinuxX86
+ toolchainGlibc
+}
+
+type toolchainLinuxGlibcX8664 struct {
+ toolchainLinuxX8664
+ toolchainGlibc
+}
+
+func (t *toolchainLinuxGlibcX86) Cflags() string {
+ return t.toolchainLinuxX86.Cflags() + " " + t.toolchainGlibc.Cflags()
+}
+
+func (t *toolchainLinuxGlibcX86) Ldflags() string {
+ return t.toolchainLinuxX86.Ldflags() + " " + t.toolchainGlibc.Ldflags()
+}
+
+func (t *toolchainLinuxGlibcX86) Lldflags() string {
+ return t.toolchainLinuxX86.Lldflags() + " " + t.toolchainGlibc.Lldflags()
+}
+
+func (t *toolchainLinuxGlibcX8664) Cflags() string {
+ return t.toolchainLinuxX8664.Cflags() + " " + t.toolchainGlibc.Cflags()
+}
+
+func (t *toolchainLinuxGlibcX8664) Ldflags() string {
+ return t.toolchainLinuxX8664.Ldflags() + " " + t.toolchainGlibc.Ldflags()
+}
+
+func (t *toolchainLinuxGlibcX8664) Lldflags() string {
+ return t.toolchainLinuxX8664.Lldflags() + " " + t.toolchainGlibc.Lldflags()
+}
+
+var toolchainLinuxGlibcX86Singleton Toolchain = &toolchainLinuxGlibcX86{}
+var toolchainLinuxGlibcX8664Singleton Toolchain = &toolchainLinuxGlibcX8664{}
+
+func linuxGlibcX86ToolchainFactory(arch android.Arch) Toolchain {
+ return toolchainLinuxGlibcX86Singleton
+}
+
+func linuxGlibcX8664ToolchainFactory(arch android.Arch) Toolchain {
+ return toolchainLinuxGlibcX8664Singleton
+}
+
+// musl specialization of the linux toolchain
+
+type toolchainMusl struct {
+}
+
+func (toolchainMusl) Musl() bool { return true }
+
+func (toolchainMusl) CrtBeginStaticBinary() []string { return muslCrtBeginStaticBinary }
+func (toolchainMusl) CrtBeginSharedBinary() []string { return muslCrtBeginSharedBinary }
+func (toolchainMusl) CrtBeginSharedLibrary() []string { return muslCrtBeginSharedLibrary }
+func (toolchainMusl) CrtEndStaticBinary() []string { return muslCrtEndStaticBinary }
+func (toolchainMusl) CrtEndSharedBinary() []string { return muslCrtEndSharedBinary }
+func (toolchainMusl) CrtEndSharedLibrary() []string { return muslCrtEndSharedLibrary }
+
+func (toolchainMusl) DefaultSharedLibraries() []string { return muslDefaultSharedLibraries }
+
+func (toolchainMusl) Cflags() string {
+ return "${config.LinuxMuslCflags}"
+}
+
+func (toolchainMusl) Ldflags() string {
+ return "${config.LinuxMuslLdflags}"
+}
+
+func (toolchainMusl) Lldflags() string {
+ return "${config.LinuxMuslLldflags}"
+}
+
+type toolchainLinuxMuslX86 struct {
+ toolchainLinuxX86
+ toolchainMusl
+}
+
+type toolchainLinuxMuslX8664 struct {
+ toolchainLinuxX8664
+ toolchainMusl
+}
+
+func (t *toolchainLinuxMuslX86) Cflags() string {
+ return t.toolchainLinuxX86.Cflags() + " " + t.toolchainMusl.Cflags()
+}
+
+func (t *toolchainLinuxMuslX86) Ldflags() string {
+ return t.toolchainLinuxX86.Ldflags() + " " + t.toolchainMusl.Ldflags()
+}
+
+func (t *toolchainLinuxMuslX86) Lldflags() string {
+ return t.toolchainLinuxX86.Lldflags() + " " + t.toolchainMusl.Lldflags()
+}
+
+func (t *toolchainLinuxMuslX8664) Cflags() string {
+ return t.toolchainLinuxX8664.Cflags() + " " + t.toolchainMusl.Cflags()
+}
+
+func (t *toolchainLinuxMuslX8664) Ldflags() string {
+ return t.toolchainLinuxX8664.Ldflags() + " " + t.toolchainMusl.Ldflags()
+}
+
+func (t *toolchainLinuxMuslX8664) Lldflags() string {
+ return t.toolchainLinuxX8664.Lldflags() + " " + t.toolchainMusl.Lldflags()
+}
+
+var toolchainLinuxMuslX86Singleton Toolchain = &toolchainLinuxMuslX86{}
+var toolchainLinuxMuslX8664Singleton Toolchain = &toolchainLinuxMuslX8664{}
+
+func linuxMuslX86ToolchainFactory(arch android.Arch) Toolchain {
+ return toolchainLinuxMuslX86Singleton
+}
+
+func linuxMuslX8664ToolchainFactory(arch android.Arch) Toolchain {
+ return toolchainLinuxMuslX8664Singleton
}
func init() {
- registerToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory)
- registerToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory)
+ registerToolchainFactory(android.Linux, android.X86, linuxGlibcX86ToolchainFactory)
+ registerToolchainFactory(android.Linux, android.X86_64, linuxGlibcX8664ToolchainFactory)
+ registerToolchainFactory(android.LinuxMusl, android.X86, linuxMuslX86ToolchainFactory)
+ registerToolchainFactory(android.LinuxMusl, android.X86_64, linuxMuslX8664ToolchainFactory)
}
diff --git a/cc/library.go b/cc/library.go
index 56c460c..8f302fc 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -147,12 +147,11 @@
Cflags []string `android:"arch_variant"`
- Enabled *bool `android:"arch_variant"`
- Whole_static_libs []string `android:"arch_variant"`
- Static_libs []string `android:"arch_variant"`
- Shared_libs []string `android:"arch_variant"`
- System_shared_libs []string `android:"arch_variant"`
- Default_shared_libs []string `android:"arch_variant"`
+ Enabled *bool `android:"arch_variant"`
+ Whole_static_libs []string `android:"arch_variant"`
+ Static_libs []string `android:"arch_variant"`
+ Shared_libs []string `android:"arch_variant"`
+ System_shared_libs []string `android:"arch_variant"`
Export_shared_lib_headers []string `android:"arch_variant"`
Export_static_lib_headers []string `android:"arch_variant"`
@@ -1163,17 +1162,11 @@
if library.StaticProperties.Static.System_shared_libs != nil {
library.baseLinker.Properties.System_shared_libs = library.StaticProperties.Static.System_shared_libs
}
- if library.StaticProperties.Static.Default_shared_libs != nil {
- library.baseLinker.Properties.Default_shared_libs = library.StaticProperties.Static.Default_shared_libs
- }
} else if library.shared() {
// Compare with nil because an empty list needs to be propagated.
if library.SharedProperties.Shared.System_shared_libs != nil {
library.baseLinker.Properties.System_shared_libs = library.SharedProperties.Shared.System_shared_libs
}
- if library.SharedProperties.Shared.Default_shared_libs != nil {
- library.baseLinker.Properties.Default_shared_libs = library.SharedProperties.Shared.Default_shared_libs
- }
}
deps = library.baseLinker.linkerDeps(ctx, deps)
@@ -1255,11 +1248,6 @@
} else {
specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, properties.System_shared_libs...)
}
- if specifiedDeps.defaultSharedLibs == nil {
- specifiedDeps.defaultSharedLibs = properties.Default_shared_libs
- } else {
- specifiedDeps.defaultSharedLibs = append(specifiedDeps.defaultSharedLibs, properties.Default_shared_libs...)
- }
specifiedDeps.sharedLibs = android.FirstUniqueStrings(specifiedDeps.sharedLibs)
if len(specifiedDeps.systemSharedLibs) > 0 {
@@ -1267,11 +1255,6 @@
// retained.
specifiedDeps.systemSharedLibs = android.FirstUniqueStrings(specifiedDeps.systemSharedLibs)
}
- if len(specifiedDeps.defaultSharedLibs) > 0 {
- // Skip this if defaultSharedLibs is either nil or [], to ensure they are
- // retained.
- specifiedDeps.defaultSharedLibs = android.FirstUniqueStrings(specifiedDeps.defaultSharedLibs)
- }
return specifiedDeps
}
diff --git a/cc/library_sdk_member.go b/cc/library_sdk_member.go
index 9ad2742..9010a1a 100644
--- a/cc/library_sdk_member.go
+++ b/cc/library_sdk_member.go
@@ -258,12 +258,6 @@
outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
}
- // SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
- // so check for non-nil instead of nonzero length.
- if libInfo.DefaultSharedLibs != nil {
- outputProperties.AddPropertyWithTag("default_shared_libs", libInfo.DefaultSharedLibs, builder.SdkMemberReferencePropertyTag(false))
- }
-
// Map from property name to the include dirs to add to the prebuilt module in the snapshot.
includeDirs := make(map[string][]string)
@@ -393,12 +387,6 @@
// This field is exported as its contents may not be arch specific.
SystemSharedLibs []string `android:"arch_variant"`
- // The set of default shared libraries. Note nil and [] are semantically
- // distinct - see BaseLinkerProperties.Default_shared_libs.
- //
- // This field is exported as its contents may not be arch specific.
- DefaultSharedLibs []string `android:"arch_variant"`
-
// The specific stubs version for the lib variant, or empty string if stubs
// are not in use.
//
@@ -474,7 +462,6 @@
}
}
p.SystemSharedLibs = specifiedDeps.systemSharedLibs
- p.DefaultSharedLibs = specifiedDeps.defaultSharedLibs
}
p.ExportedGeneratedHeaders = exportedInfo.GeneratedHeaders
diff --git a/cc/library_test.go b/cc/library_test.go
index ba372a8..6b349b6 100644
--- a/cc/library_test.go
+++ b/cc/library_test.go
@@ -286,3 +286,37 @@
gotFlags := entries.EntryMap["LOCAL_EXPORT_CFLAGS"]
android.AssertDeepEquals(t, "androidmk exported cflags", expectedFlags, gotFlags)
}
+
+func TestLibraryVersionScript(t *testing.T) {
+ result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
+ cc_library {
+ name: "libfoo",
+ srcs: ["foo.c"],
+ version_script: "foo.map.txt",
+ }`)
+
+ libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
+
+ android.AssertStringListContains(t, "missing dependency on version_script",
+ libfoo.Implicits.Strings(), "foo.map.txt")
+ android.AssertStringDoesContain(t, "missing flag for version_script",
+ libfoo.Args["ldFlags"], "-Wl,--version-script,foo.map.txt")
+
+}
+
+func TestLibraryDynamicList(t *testing.T) {
+ result := PrepareForIntegrationTestWithCc.RunTestWithBp(t, `
+ cc_library {
+ name: "libfoo",
+ srcs: ["foo.c"],
+ dynamic_list: "foo.dynamic.txt",
+ }`)
+
+ libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
+
+ android.AssertStringListContains(t, "missing dependency on dynamic_list",
+ libfoo.Implicits.Strings(), "foo.dynamic.txt")
+ android.AssertStringDoesContain(t, "missing flag for dynamic_list",
+ libfoo.Args["ldFlags"], "-Wl,--dynamic-list,foo.dynamic.txt")
+
+}
diff --git a/cc/linker.go b/cc/linker.go
index 82449a6..7c710d7 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -46,18 +46,11 @@
// list of module-specific flags that will be used for all link steps
Ldflags []string `android:"arch_variant"`
- // list of system libraries that will be dynamically linked to shared library and executable
- // modules that build against bionic (device or Linux bionic modules). If unset, generally
- // defaults to libc, libm, and libdl. Set to [] to prevent linking against the defaults.
- // Equivalent to default_shared_libs for modules that build against bionic, and ignored on
- // modules that do not build against bionic.
+ // list of system libraries that will be dynamically linked to
+ // shared library and executable modules. If unset, generally defaults to libc,
+ // libm, and libdl. Set to [] to prevent linking against the defaults.
System_shared_libs []string `android:"arch_variant"`
- // list of system libraries that will be dynamically linked to shared library and executable
- // modules. If unset, generally defaults to libc, libm, and libdl. Set to [] to prevent
- // linking against the defaults. Equivalent to system_shared_libs, but applies to all modules.
- Default_shared_libs []string `android:"arch_variant"`
-
// allow the module to contain undefined symbols. By default,
// modules cannot contain undefined symbols that are not satisified by their immediate
// dependencies. Set this flag to true to remove --no-undefined from the linker flags.
@@ -112,6 +105,10 @@
// product variant of the C/C++ module.
Static_libs []string
+ // list of ehader libs that only should be used to build vendor or product
+ // variant of the C/C++ module.
+ Header_libs []string
+
// list of shared libs that should not be used to build vendor or
// product variant of the C/C++ module.
Exclude_shared_libs []string
@@ -180,6 +177,14 @@
// in most cases the same libraries are available for the SDK and platform
// variants.
Shared_libs []string
+
+ // list of ehader libs that only should be used to build platform variant of
+ // the C/C++ module.
+ Header_libs []string
+
+ // list of shared libs that should not be used to build the platform variant
+ // of the C/C++ module.
+ Exclude_shared_libs []string
}
Apex struct {
// list of shared libs that should not be used to build the apex variant of
@@ -201,6 +206,9 @@
// local file name to pass to the linker as --version_script
Version_script *string `android:"path,arch_variant"`
+ // local file name to pass to the linker as --dynamic-list
+ Dynamic_list *string `android:"path,arch_variant"`
+
// list of static libs that should not be used to build this module
Exclude_static_libs []string `android:"arch_variant"`
@@ -239,18 +247,6 @@
linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
}
-// overrideDefaultSharedLibraries returns the contents of the default_shared_libs or
-// system_shared_libs properties, and records an error if both are set.
-func (linker *baseLinker) overrideDefaultSharedLibraries(ctx BaseModuleContext) []string {
- if linker.Properties.System_shared_libs != nil && linker.Properties.Default_shared_libs != nil {
- ctx.PropertyErrorf("system_shared_libs", "cannot be specified if default_shared_libs is also specified")
- }
- if linker.Properties.System_shared_libs != nil {
- return linker.Properties.System_shared_libs
- }
- return linker.Properties.Default_shared_libs
-}
-
// linkerInit initializes dynamic properties of the linker (such as runpath).
func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
if ctx.toolchain().Is64Bit() {
@@ -300,6 +296,7 @@
deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Vendor.Static_libs...)
deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
+ deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Vendor.Header_libs...)
deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
@@ -349,15 +346,17 @@
if !ctx.useSdk() {
deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_libs...)
+ deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Platform.Exclude_shared_libs)
+ deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Platform.Header_libs...)
}
- deps.SystemSharedLibs = linker.overrideDefaultSharedLibraries(ctx)
+ deps.SystemSharedLibs = linker.Properties.System_shared_libs
// In Bazel conversion mode, variations have not been specified, so SystemSharedLibs may
// inaccuarately appear unset, which can cause issues with circular dependencies.
if deps.SystemSharedLibs == nil && !ctx.BazelConversionMode() {
- // Provide a default set of shared libraries if default_shared_libs and system_shared_libs
- // are unspecified. Note: If an empty list [] is specified, it implies that the module
- // declines the default shared libraries.
+ // Provide a default system_shared_libs if it is unspecified. Note: If an
+ // empty list [] is specified, it implies that the module declines the
+ // default system_shared_libs.
deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
}
@@ -385,6 +384,10 @@
indexList("libdl", deps.SystemSharedLibs) < indexList("libc", deps.SystemSharedLibs) {
ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
}
+ } else if ctx.toolchain().Musl() {
+ if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
+ deps.LateStaticLibs = append(deps.LateStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
+ }
}
deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
@@ -472,7 +475,7 @@
flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Ldflags())
}
- if !ctx.toolchain().Bionic() {
+ if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl {
CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
flags.Local.LdFlags = append(flags.Local.LdFlags, linker.Properties.Host_ldlibs...)
@@ -557,6 +560,17 @@
}
}
}
+
+ dynamicList := android.OptionalPathForModuleSrc(ctx, linker.Properties.Dynamic_list)
+ if dynamicList.Valid() {
+ if ctx.Darwin() {
+ ctx.PropertyErrorf("dynamic_list", "Not supported on Darwin")
+ } else {
+ flags.Local.LdFlags = append(flags.Local.LdFlags,
+ "-Wl,--dynamic-list,"+dynamicList.String())
+ flags.LdFlagsDeps = append(flags.LdFlagsDeps, dynamicList.Path())
+ }
+ }
}
return flags
@@ -577,11 +591,6 @@
} else {
specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, linker.Properties.System_shared_libs...)
}
- if specifiedDeps.defaultSharedLibs == nil {
- specifiedDeps.defaultSharedLibs = linker.Properties.Default_shared_libs
- } else {
- specifiedDeps.defaultSharedLibs = append(specifiedDeps.defaultSharedLibs, linker.Properties.Default_shared_libs...)
- }
return specifiedDeps
}
diff --git a/cc/object.go b/cc/object.go
index 0f983c8..bd9f228 100644
--- a/cc/object.go
+++ b/cc/object.go
@@ -78,7 +78,7 @@
// list of default libraries that will provide headers for this module. If unset, generally
// defaults to libc, libm, and libdl. Set to [] to prevent using headers from the defaults.
- Default_shared_libs []string `android:"arch_variant"`
+ System_shared_libs []string `android:"arch_variant"`
// names of other cc_object modules to link into this module using partial linking
Objs []string `android:"arch_variant"`
@@ -219,9 +219,9 @@
deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs...)
deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
- deps.SystemSharedLibs = object.Properties.Default_shared_libs
+ deps.SystemSharedLibs = object.Properties.System_shared_libs
if deps.SystemSharedLibs == nil {
- // Provide a default set of shared libraries if default_shared_libs is unspecified.
+ // Provide a default set of shared libraries if system_shared_libs is unspecified.
// Note: If an empty list [] is specified, it implies that the module declines the
// default shared libraries.
deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
@@ -278,12 +278,12 @@
func (object *objectLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs...)
- // Must distinguish nil and [] in default_shared_libs - ensure that [] in
+ // Must distinguish nil and [] in system_shared_libs - ensure that [] in
// either input list doesn't come out as nil.
- if specifiedDeps.defaultSharedLibs == nil {
- specifiedDeps.defaultSharedLibs = object.Properties.Default_shared_libs
+ if specifiedDeps.systemSharedLibs == nil {
+ specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs
} else {
- specifiedDeps.defaultSharedLibs = append(specifiedDeps.defaultSharedLibs, object.Properties.Default_shared_libs...)
+ specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...)
}
return specifiedDeps
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 3911f48..b244394 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -472,8 +472,8 @@
s.Diag.Cfi = nil
}
- // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
- if !ctx.Os().Linux() {
+ // Disable sanitizers that depend on the UBSan runtime for windows/darwin/musl builds.
+ if !ctx.Os().Linux() || ctx.Os() == android.LinuxMusl {
s.Cfi = nil
s.Diag.Cfi = nil
s.Misc_undefined = nil
diff --git a/cc/testing.go b/cc/testing.go
index 19513e3..e8f3481 100644
--- a/cc/testing.go
+++ b/cc/testing.go
@@ -365,7 +365,7 @@
stl: "none",
min_sdk_version: "16",
crt: true,
- default_shared_libs: [],
+ system_shared_libs: [],
apex_available: [
"//apex_available:platform",
"//apex_available:anyapex",
@@ -450,6 +450,25 @@
cc_library_static {
name: "note_memtag_heap_sync",
}
+
+
+ cc_library {
+ name: "libjemalloc5",
+ host_supported: true,
+ no_libcrt: true,
+ nocrt: true,
+ system_shared_libs: [],
+ stl: "none",
+ }
+
+ cc_library {
+ name: "libc_musl",
+ host_supported: true,
+ no_libcrt: true,
+ nocrt: true,
+ system_shared_libs: [],
+ stl: "none",
+ }
`
}
diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go
index 22922c0..02c5229 100644
--- a/cmd/soong_ui/main.go
+++ b/cmd/soong_ui/main.go
@@ -16,6 +16,7 @@
import (
"context"
+ "encoding/json"
"flag"
"fmt"
"io/ioutil"
@@ -34,6 +35,11 @@
"android/soong/ui/tracer"
)
+const (
+ configDir = "vendor/google/tools/soong_config"
+ jsonSuffix = "json"
+)
+
// A command represents an operation to be executed in the soong build
// system.
type command struct {
@@ -110,6 +116,34 @@
return indexList(s, list) != -1
}
+func loadEnvConfig() error {
+ bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
+ if bc == "" {
+ return nil
+ }
+ cfgFile := filepath.Join(os.Getenv("TOP"), configDir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
+
+ envVarsJSON, err := ioutil.ReadFile(cfgFile)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "\033[33mWARNING:\033[0m failed to open config file %s: %s\n", cfgFile, err.Error())
+ return nil
+ }
+
+ var envVars map[string]map[string]string
+ if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
+ return fmt.Errorf("env vars config file: %s did not parse correctly: %s", cfgFile, err.Error())
+ }
+ for k, v := range envVars["env"] {
+ if os.Getenv(k) != "" {
+ continue
+ }
+ if err := os.Setenv(k, v); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// Main execution of soong_ui. The command format is as follows:
//
// soong_ui <command> [<arg 1> <arg 2> ... <arg n>]
@@ -171,6 +205,11 @@
Status: stat,
}}
+ if err := loadEnvConfig(); err != nil {
+ fmt.Fprintf(os.Stderr, "failed to parse env config files: %v", err)
+ os.Exit(1)
+ }
+
config := c.config(buildCtx, args...)
build.SetupOutDir(buildCtx, config)
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index 0bcec17..7a74506 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -44,15 +44,15 @@
DisableGenerateProfile bool // don't generate profiles
ProfileDir string // directory to find profiles in
- BootJars android.ConfiguredJarList // modules for jars that form the boot class path
- UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
+ BootJars android.ConfiguredJarList // modules for jars that form the boot class path
+ ApexBootJars android.ConfiguredJarList // jars within apex that form the boot class path
ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
- SystemServerJars android.ConfiguredJarList // jars that form the system server
- SystemServerApps []string // apps that are loaded into system server
- UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
- SpeedApps []string // apps that should be speed optimized
+ SystemServerJars android.ConfiguredJarList // jars that form the system server
+ SystemServerApps []string // apps that are loaded into system server
+ ApexSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
+ SpeedApps []string // apps that should be speed optimized
BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
@@ -531,7 +531,7 @@
return config, nil
}
-// checkBootJarsConfigConsistency checks the consistency of BootJars and UpdatableBootJars fields in
+// checkBootJarsConfigConsistency checks the consistency of BootJars and ApexBootJars fields in
// DexpreoptGlobalConfig and Config.productVariables.
func checkBootJarsConfigConsistency(ctx android.SingletonContext, dexpreoptConfig *GlobalConfig, config android.Config) {
compareBootJars := func(property string, dexpreoptJars, variableJars android.ConfiguredJarList) {
@@ -545,8 +545,8 @@
}
}
- compareBootJars("BootJars", dexpreoptConfig.BootJars, config.NonUpdatableBootJars())
- compareBootJars("UpdatableBootJars", dexpreoptConfig.UpdatableBootJars, config.UpdatableBootJars())
+ compareBootJars("BootJars", dexpreoptConfig.BootJars, config.NonApexBootJars())
+ compareBootJars("ApexBootJars", dexpreoptConfig.ApexBootJars, config.ApexBootJars())
}
func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
@@ -614,11 +614,11 @@
DisableGenerateProfile: false,
ProfileDir: "",
BootJars: android.EmptyConfiguredJarList(),
- UpdatableBootJars: android.EmptyConfiguredJarList(),
+ ApexBootJars: android.EmptyConfiguredJarList(),
ArtApexJars: android.EmptyConfiguredJarList(),
SystemServerJars: android.EmptyConfiguredJarList(),
SystemServerApps: nil,
- UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
+ ApexSystemServerJars: android.EmptyConfiguredJarList(),
SpeedApps: nil,
PreoptFlags: nil,
DefaultCompilerFilter: "",
diff --git a/dexpreopt/dexpreopt.go b/dexpreopt/dexpreopt.go
index da015a3..4c6ae82 100644
--- a/dexpreopt/dexpreopt.go
+++ b/dexpreopt/dexpreopt.go
@@ -111,7 +111,7 @@
}
// Don't preopt system server jars that are updatable.
- if global.UpdatableSystemServerJars.ContainsJar(module.Name) {
+ if global.ApexSystemServerJars.ContainsJar(module.Name) {
return true
}
@@ -234,7 +234,7 @@
invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
- systemServerJars := NonUpdatableSystemServerJars(ctx, global)
+ systemServerJars := NonApexSystemServerJars(ctx, global)
rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
rule.Command().FlagWithOutput("rm -f ", odexPath)
@@ -523,13 +523,13 @@
}
}
-var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars")
+var nonApexSystemServerJarsKey = android.NewOnceKey("nonApexSystemServerJars")
// TODO: eliminate the superficial global config parameter by moving global config definition
// from java subpackage to dexpreopt.
-func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string {
- return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} {
- return android.RemoveListFromList(global.SystemServerJars.CopyOfJars(), global.UpdatableSystemServerJars.CopyOfJars())
+func NonApexSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string {
+ return ctx.Config().Once(nonApexSystemServerJarsKey, func() interface{} {
+ return android.RemoveListFromList(global.SystemServerJars.CopyOfJars(), global.ApexSystemServerJars.CopyOfJars())
}).([]string)
}
@@ -556,7 +556,7 @@
mctx, isModule := ctx.(android.ModuleContext)
if isModule {
config := GetGlobalConfig(ctx)
- jars := NonUpdatableSystemServerJars(ctx, config)
+ jars := NonApexSystemServerJars(ctx, config)
mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
depIndex := android.IndexList(dep.Name(), jars)
if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars {
diff --git a/dexpreopt/testing.go b/dexpreopt/testing.go
index c0ba5ca..2f99655 100644
--- a/dexpreopt/testing.go
+++ b/dexpreopt/testing.go
@@ -118,10 +118,10 @@
})
}
-// FixtureSetUpdatableBootJars sets the UpdatableBootJars property in the global config.
-func FixtureSetUpdatableBootJars(bootJars ...string) android.FixturePreparer {
+// FixtureSetApexBootJars sets the ApexBootJars property in the global config.
+func FixtureSetApexBootJars(bootJars ...string) android.FixturePreparer {
return FixtureModifyGlobalConfig(func(dexpreoptConfig *GlobalConfig) {
- dexpreoptConfig.UpdatableBootJars = android.CreateTestConfiguredJarList(bootJars)
+ dexpreoptConfig.ApexBootJars = android.CreateTestConfiguredJarList(bootJars)
})
}
diff --git a/etc/Android.bp b/etc/Android.bp
index 06a2fa1..c670236 100644
--- a/etc/Android.bp
+++ b/etc/Android.bp
@@ -13,9 +13,11 @@
],
srcs: [
"prebuilt_etc.go",
+ "snapshot_etc.go",
],
testSrcs: [
"prebuilt_etc_test.go",
+ "snapshot_etc_test.go",
],
pluginFor: ["soong_build"],
}
diff --git a/etc/prebuilt_etc.go b/etc/prebuilt_etc.go
index 4107916..8aeb0dd 100644
--- a/etc/prebuilt_etc.go
+++ b/etc/prebuilt_etc.go
@@ -36,6 +36,7 @@
"github.com/google/blueprint/proptools"
"android/soong/android"
+ "android/soong/bazel"
"android/soong/snapshot"
)
@@ -61,6 +62,8 @@
ctx.RegisterModuleType("prebuilt_rfsa", PrebuiltRFSAFactory)
ctx.RegisterModuleType("prebuilt_defaults", defaultsFactory)
+
+ android.RegisterBp2BuildMutator("prebuilt_etc", PrebuiltEtcBp2Build)
}
var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
@@ -131,6 +134,7 @@
type PrebuiltEtc struct {
android.ModuleBase
android.DefaultableModuleBase
+ android.BazelModuleBase
snapshot.VendorSnapshotModuleInterface
snapshot.RecoverySnapshotModuleInterface
@@ -406,6 +410,7 @@
// This module is device-only
android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
android.InitDefaultableModule(module)
+ android.InitBazelModule(module)
return module
}
@@ -647,3 +652,82 @@
return snapshotOutputs
}
+
+// For Bazel / bp2build
+
+type bazelPrebuiltEtcAttributes struct {
+ Src bazel.LabelAttribute
+ Filename string
+ Sub_dir string
+ Installable bazel.BoolAttribute
+}
+
+type bazelPrebuiltEtc struct {
+ android.BazelTargetModuleBase
+ bazelPrebuiltEtcAttributes
+}
+
+func BazelPrebuiltEtcFactory() android.Module {
+ module := &bazelPrebuiltEtc{}
+ module.AddProperties(&module.bazelPrebuiltEtcAttributes)
+ android.InitBazelTargetModule(module)
+ return module
+}
+
+func PrebuiltEtcBp2Build(ctx android.TopDownMutatorContext) {
+ module, ok := ctx.Module().(*PrebuiltEtc)
+ if !ok {
+ // Not an prebuilt_etc
+ return
+ }
+ if !module.ConvertWithBp2build(ctx) {
+ return
+ }
+ if ctx.ModuleType() != "prebuilt_etc" {
+ return
+ }
+
+ prebuiltEtcBp2BuildInternal(ctx, module)
+}
+
+func prebuiltEtcBp2BuildInternal(ctx android.TopDownMutatorContext, module *PrebuiltEtc) {
+ var srcLabelAttribute bazel.LabelAttribute
+ if module.properties.Src != nil {
+ srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Src))
+ }
+
+ var filename string
+ if module.properties.Filename != nil {
+ filename = *module.properties.Filename
+ }
+
+ var subDir string
+ if module.subdirProperties.Sub_dir != nil {
+ subDir = *module.subdirProperties.Sub_dir
+ }
+
+ var installableBoolAttribute bazel.BoolAttribute
+ if module.properties.Installable != nil {
+ installableBoolAttribute.Value = module.properties.Installable
+ }
+
+ attrs := &bazelPrebuiltEtcAttributes{
+ Src: srcLabelAttribute,
+ Filename: filename,
+ Sub_dir: subDir,
+ Installable: installableBoolAttribute,
+ }
+
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "prebuilt_etc",
+ Bzl_load_location: "//build/bazel/rules:prebuilt_etc.bzl",
+ }
+
+ ctx.CreateBazelTargetModule(BazelPrebuiltEtcFactory, module.Name(), props, attrs)
+}
+
+func (m *bazelPrebuiltEtc) Name() string {
+ return m.BaseModuleName()
+}
+
+func (m *bazelPrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
diff --git a/etc/snapshot_etc.go b/etc/snapshot_etc.go
new file mode 100644
index 0000000..9a25d5a
--- /dev/null
+++ b/etc/snapshot_etc.go
@@ -0,0 +1,186 @@
+// Copyright 2021 The Android Open Source Project
+//
+// 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 etc
+
+// This file implements snapshot module of 'prebuilt_etc' type
+// 'snapshot_etc' module defines android.PrebuiltInterface so it can be handled
+// as prebuilt of 'prebuilt_etc' type.
+// Properties of 'snapshot_etc' follows properties from snapshotJsonFlags type
+
+import (
+ "android/soong/android"
+ "fmt"
+ "strings"
+
+ "github.com/google/blueprint"
+ "github.com/google/blueprint/proptools"
+)
+
+func RegisterSnapshotEtcModule(ctx android.RegistrationContext) {
+ ctx.RegisterModuleType("snapshot_etc", SnapshotEtcFactory)
+}
+
+func init() {
+ RegisterSnapshotEtcModule(android.InitRegistrationContext)
+}
+
+// snapshot_etc is a prebuilt module type to be installed under etc which is auto-generated by
+// development/vendor_snapshot/update.py. This module will override prebuilt_etc module with same
+// name when 'prefer' property is true.
+func SnapshotEtcFactory() android.Module {
+ module := &SnapshotEtc{}
+ module.AddProperties(&module.properties)
+
+ var srcsSupplier = func(_ android.BaseModuleContext, prebuilt android.Module) []string {
+ s, ok := prebuilt.(*SnapshotEtc)
+ if !ok || s.properties.Src == nil {
+ return []string{}
+ }
+
+ return []string{*s.properties.Src}
+ }
+
+ android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
+ android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "src")
+ return module
+}
+
+type snapshotEtcProperties struct {
+ Src *string `android:"path,arch_variant"` // Source of snapshot_etc file
+ Filename *string `android:"arch_variant"` // Target file name when it differs from module name
+ Relative_install_path *string `android:"arch_variant"` // Relative install path when it should be installed subdirectory of etc
+}
+
+type SnapshotEtc struct {
+ android.ModuleBase
+ prebuilt android.Prebuilt
+ properties snapshotEtcProperties
+
+ outputFilePath android.OutputPath
+ installDirPath android.InstallPath
+}
+
+func (s *SnapshotEtc) Prebuilt() *android.Prebuilt {
+ return &s.prebuilt
+}
+
+func (s *SnapshotEtc) Name() string {
+ return s.prebuilt.Name(s.BaseModuleName())
+}
+
+func (s *SnapshotEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+ if s.properties.Src == nil {
+ ctx.PropertyErrorf("src", "missing prebuilt source file")
+ return
+ }
+
+ sourceFilePath := s.prebuilt.SingleSourcePath(ctx)
+
+ // Determine the output file basename.
+ // If Filename is set, use the name specified by the property.
+ // Otherwise use the module name.
+ filename := proptools.String(s.properties.Filename)
+ if filename == "" {
+ filename = ctx.ModuleName()
+ }
+
+ s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
+
+ if strings.Contains(filename, "/") {
+ ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
+ return
+ }
+
+ subDir := ""
+ if s.properties.Relative_install_path != nil {
+ subDir = *s.properties.Relative_install_path
+ }
+
+ s.installDirPath = android.PathForModuleInstall(ctx, "etc", subDir)
+
+ // This ensures that outputFilePath has the correct name for others to
+ // use, as the source file may have a different name.
+ ctx.Build(pctx, android.BuildParams{
+ Rule: android.Cp,
+ Input: sourceFilePath,
+ Output: s.outputFilePath,
+ Description: "Install snapshot etc module " + s.BaseModuleName(),
+ })
+
+ ctx.InstallFile(s.installDirPath, s.outputFilePath.Base(), sourceFilePath)
+}
+
+func (p *SnapshotEtc) AndroidMkEntries() []android.AndroidMkEntries {
+ return []android.AndroidMkEntries{{
+ Class: "ETC",
+ OutputFile: android.OptionalPathForPath(p.outputFilePath),
+ ExtraEntries: []android.AndroidMkExtraEntriesFunc{
+ func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
+ entries.SetString("LOCAL_MODULE_TAGS", "optional")
+ entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
+ entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
+ },
+ },
+ }}
+}
+
+type snapshotEtcDependencyTag struct {
+ blueprint.DependencyTag
+}
+
+var tag = snapshotEtcDependencyTag{}
+
+func (s *SnapshotEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
+ return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk() &&
+ !s.ModuleBase.InstallInVendorRamdisk() && !s.ModuleBase.InstallInDebugRamdisk()
+}
+
+func (p *SnapshotEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+ return p.ModuleBase.InstallInRamdisk()
+}
+
+func (p *SnapshotEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+ return p.ModuleBase.InstallInVendorRamdisk()
+}
+
+func (p *SnapshotEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+ return p.ModuleBase.InstallInDebugRamdisk()
+}
+
+func (p *SnapshotEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
+ return p.ModuleBase.InstallInRecovery()
+}
+
+func (p *SnapshotEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
+ return nil
+}
+
+func (p *SnapshotEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
+}
+
+func (p *SnapshotEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
+
+func (p *SnapshotEtc) OutputFiles(tag string) (android.Paths, error) {
+ switch tag {
+ case "":
+ return android.Paths{p.outputFilePath}, nil
+ default:
+ return nil, fmt.Errorf("unsupported module reference tag %q", tag)
+ }
+
+}
+
+var _ android.PrebuiltInterface = (*SnapshotEtc)(nil)
+var _ android.ImageInterface = (*SnapshotEtc)(nil)
+var _ android.OutputFileProducer = (*SnapshotEtc)(nil)
diff --git a/etc/snapshot_etc_test.go b/etc/snapshot_etc_test.go
new file mode 100644
index 0000000..b9d5504
--- /dev/null
+++ b/etc/snapshot_etc_test.go
@@ -0,0 +1,185 @@
+// Copyright 2021 The Android Open Source Project
+//
+// 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 etc
+
+import (
+ "android/soong/android"
+ "testing"
+
+ "github.com/google/blueprint"
+)
+
+var registerSourceModule = func(ctx android.RegistrationContext) {
+ ctx.RegisterModuleType("source", newSourceModule)
+}
+
+type sourceModuleProperties struct {
+ Deps []string `android:"path,arch_variant"`
+}
+
+type sourceModule struct {
+ android.ModuleBase
+ android.OverridableModuleBase
+
+ properties sourceModuleProperties
+ dependsOnSourceModule, dependsOnPrebuiltModule bool
+ deps android.Paths
+ src android.Path
+}
+
+func newSourceModule() android.Module {
+ m := &sourceModule{}
+ m.AddProperties(&m.properties)
+ android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibFirst)
+ android.InitOverridableModule(m, nil)
+ return m
+}
+
+func (s *sourceModule) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) {
+ // s.properties.Deps are annotated with android:path, so they are
+ // automatically added to the dependency by pathDeps mutator
+}
+
+func (s *sourceModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+ s.deps = android.PathsForModuleSrc(ctx, s.properties.Deps)
+ s.src = android.PathForModuleSrc(ctx, "source_file")
+}
+
+func (s *sourceModule) Srcs() android.Paths {
+ return android.Paths{s.src}
+}
+
+var prepareForSnapshotEtcTest = android.GroupFixturePreparers(
+ android.PrepareForTestWithArchMutator,
+ android.PrepareForTestWithPrebuilts,
+ PrepareForTestWithPrebuiltEtc,
+ android.FixtureRegisterWithContext(RegisterSnapshotEtcModule),
+ android.FixtureRegisterWithContext(registerSourceModule),
+ android.FixtureMergeMockFs(android.MockFS{
+ "foo.conf": nil,
+ "bar.conf": nil,
+ }),
+)
+
+func TestSnapshotWithFilename(t *testing.T) {
+ var androidBp = `
+ snapshot_etc {
+ name: "etc_module",
+ src: "foo.conf",
+ filename: "bar.conf",
+ }
+ `
+
+ result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
+ for _, variant := range result.ModuleVariantsForTests("etc_module") {
+ module := result.ModuleForTests("etc_module", variant)
+ s, ok := module.Module().(*SnapshotEtc)
+ if !ok {
+ t.Errorf("Expected snapshot_etc module type")
+ }
+ if s.outputFilePath.Base() != "bar.conf" {
+ t.Errorf("Output file path does not match with specified filename")
+ }
+ }
+}
+
+func TestSnapshotEtcWithOrigin(t *testing.T) {
+ var androidBp = `
+ prebuilt_etc {
+ name: "etc_module",
+ src: "foo.conf",
+ }
+
+ snapshot_etc {
+ name: "etc_module",
+ src: "bar.conf",
+ }
+
+ source {
+ name: "source",
+ deps: [":etc_module"],
+ }
+ `
+
+ result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
+
+ for _, variant := range result.ModuleVariantsForTests("source") {
+ source := result.ModuleForTests("source", variant)
+
+ result.VisitDirectDeps(source.Module(), func(m blueprint.Module) {
+ if _, ok := m.(*PrebuiltEtc); !ok {
+ t.Errorf("Original prebuilt_etc module expected.")
+ }
+ })
+ }
+}
+
+func TestSnapshotEtcWithOriginAndPrefer(t *testing.T) {
+ var androidBp = `
+ prebuilt_etc {
+ name: "etc_module",
+ src: "foo.conf",
+ }
+
+ snapshot_etc {
+ name: "etc_module",
+ src: "bar.conf",
+ prefer: true,
+ }
+
+ source {
+ name: "source",
+ deps: [":etc_module"],
+ }
+ `
+
+ result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
+
+ for _, variant := range result.ModuleVariantsForTests("source") {
+ source := result.ModuleForTests("source", variant)
+
+ result.VisitDirectDeps(source.Module(), func(m blueprint.Module) {
+ if _, ok := m.(*SnapshotEtc); !ok {
+ t.Errorf("Preferred snapshot_etc module expected.")
+ }
+ })
+ }
+}
+
+func TestSnapshotEtcWithoutOrigin(t *testing.T) {
+ var androidBp = `
+ snapshot_etc {
+ name: "etc_module",
+ src: "bar.conf",
+ }
+
+ source {
+ name: "source",
+ deps: [":etc_module"],
+ }
+ `
+
+ result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp)
+
+ for _, variant := range result.ModuleVariantsForTests("source") {
+ source := result.ModuleForTests("source", variant)
+
+ result.VisitDirectDeps(source.Module(), func(m blueprint.Module) {
+ if _, ok := m.(*SnapshotEtc); !ok {
+ t.Errorf("Only source snapshot_etc module expected.")
+ }
+ })
+ }
+}
diff --git a/java/Android.bp b/java/Android.bp
index e5b8f96..9ffa123 100644
--- a/java/Android.bp
+++ b/java/Android.bp
@@ -10,6 +10,7 @@
"blueprint-pathtools",
"soong",
"soong-android",
+ "soong-bazel",
"soong-cc",
"soong-dexpreopt",
"soong-genrule",
diff --git a/java/app.go b/java/app.go
index 4e967ad..35ed27f 100755
--- a/java/app.go
+++ b/java/app.go
@@ -26,6 +26,7 @@
"github.com/google/blueprint/proptools"
"android/soong/android"
+ "android/soong/bazel"
"android/soong/cc"
"android/soong/dexpreopt"
"android/soong/tradefed"
@@ -42,6 +43,8 @@
ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
+
+ android.RegisterBp2BuildMutator("android_app_certificate", AndroidAppCertificateBp2Build)
}
// AndroidManifest.xml merging
@@ -1104,6 +1107,8 @@
type AndroidAppCertificate struct {
android.ModuleBase
+ android.BazelModuleBase
+
properties AndroidAppCertificateProperties
Certificate Certificate
}
@@ -1119,6 +1124,7 @@
module := &AndroidAppCertificate{}
module.AddProperties(&module.properties)
android.InitAndroidModule(module)
+ android.InitBazelModule(module)
return module
}
@@ -1368,3 +1374,61 @@
outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
return outputFile
}
+
+// For Bazel / bp2build
+
+type bazelAndroidAppCertificateAttributes struct {
+ Certificate string
+}
+
+type bazelAndroidAppCertificate struct {
+ android.BazelTargetModuleBase
+ bazelAndroidAppCertificateAttributes
+}
+
+func BazelAndroidAppCertificateFactory() android.Module {
+ module := &bazelAndroidAppCertificate{}
+ module.AddProperties(&module.bazelAndroidAppCertificateAttributes)
+ android.InitBazelTargetModule(module)
+ return module
+}
+
+func AndroidAppCertificateBp2Build(ctx android.TopDownMutatorContext) {
+ module, ok := ctx.Module().(*AndroidAppCertificate)
+ if !ok {
+ // Not an Android app certificate
+ return
+ }
+ if !module.ConvertWithBp2build(ctx) {
+ return
+ }
+ if ctx.ModuleType() != "android_app_certificate" {
+ return
+ }
+
+ androidAppCertificateBp2BuildInternal(ctx, module)
+}
+
+func androidAppCertificateBp2BuildInternal(ctx android.TopDownMutatorContext, module *AndroidAppCertificate) {
+ var certificate string
+ if module.properties.Certificate != nil {
+ certificate = *module.properties.Certificate
+ }
+
+ attrs := &bazelAndroidAppCertificateAttributes{
+ Certificate: certificate,
+ }
+
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "android_app_certificate",
+ Bzl_load_location: "//build/bazel/rules:android_app_certificate.bzl",
+ }
+
+ ctx.CreateBazelTargetModule(BazelAndroidAppCertificateFactory, module.Name(), props, attrs)
+}
+
+func (m *bazelAndroidAppCertificate) Name() string {
+ return m.BaseModuleName()
+}
+
+func (m *bazelAndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
diff --git a/java/app_test.go b/java/app_test.go
index a99ac62..7997f7a 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -2471,7 +2471,7 @@
PrepareForTestWithJavaSdkLibraryFiles,
FixtureWithLastReleaseApis("runtime-library", "foo", "bar"),
dexpreopt.FixtureSetBootJars("platform:foo"),
- dexpreopt.FixtureSetUpdatableBootJars("platform:bar"),
+ dexpreopt.FixtureSetApexBootJars("platform:bar"),
dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with),
).RunTestWithBp(t, bp)
diff --git a/java/bootclasspath_fragment.go b/java/bootclasspath_fragment.go
index 1ce9911..107d34a 100644
--- a/java/bootclasspath_fragment.go
+++ b/java/bootclasspath_fragment.go
@@ -537,15 +537,11 @@
global := dexpreopt.GetGlobalConfig(ctx)
- possibleUpdatableModules := gatherPossibleUpdatableModuleNamesAndStems(ctx, b.properties.Contents, bootclasspathFragmentContentDepTag)
-
- // 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.
- jars := global.UpdatableBootJars.Filter(possibleUpdatableModules)
+ possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, b.properties.Contents, bootclasspathFragmentContentDepTag)
+ jars := global.ApexBootJars.Filter(possibleUpdatableModules)
// TODO(satayev): for apex_test we want to include all contents unconditionally to classpaths
- // config. However, any test specific jars would not be present in UpdatableBootJars. Instead,
+ // config. However, any test specific jars would not be present in ApexBootJars. Instead,
// we should check if we are creating a config for apex_test via ApexInfo and amend the values.
// This is an exception to support end-to-end test for SdkExtensions, until such support exists.
if android.InList("test_framework-sdkextensions", possibleUpdatableModules) {
diff --git a/java/classpath_fragment.go b/java/classpath_fragment.go
index 12bb711..f63d81d 100644
--- a/java/classpath_fragment.go
+++ b/java/classpath_fragment.go
@@ -91,8 +91,8 @@
maxSdkVersion int32
}
-// gatherPossibleUpdatableModuleNamesAndStems returns a set of module and stem names from the
-// supplied contents that may be in the updatable boot jars.
+// gatherPossibleApexModuleNamesAndStems returns a set of module and stem names from the
+// supplied contents that may be in the apex boot jars.
//
// The module names are included because sometimes the stem is set to just change the name of
// the installed file and it expects the configuration to still use the actual module name.
@@ -100,7 +100,7 @@
// The stem names are included because sometimes the stem is set to change the effective name of the
// module that is used in the configuration as well,e .g. when a test library is overriding an
// actual boot jar
-func gatherPossibleUpdatableModuleNamesAndStems(ctx android.ModuleContext, contents []string, tag blueprint.DependencyTag) []string {
+func gatherPossibleApexModuleNamesAndStems(ctx android.ModuleContext, contents []string, tag blueprint.DependencyTag) []string {
set := map[string]struct{}{}
for _, name := range contents {
dep := ctx.GetDirectDepWithTag(name, tag)
diff --git a/java/dexpreopt_bootjars.go b/java/dexpreopt_bootjars.go
index 2c78d73..1019b4c 100644
--- a/java/dexpreopt_bootjars.go
+++ b/java/dexpreopt_bootjars.go
@@ -167,10 +167,10 @@
// regardless which APEX goes into the product. See also android.ApexInfo.ApexVariationName and
// apex.apexBundleProperties.Apex_name.
//
-// A related variable PRODUCT_UPDATABLE_BOOT_JARS contains bootclasspath libraries that are in
-// APEXes. They are not included in the boot image. The only exception here is core-icu4j.jar that
-// has been historically part of the boot image and is now in a non updatable apex; it is treated
-// as being part of PRODUCT_BOOT_JARS and is in the boot image.
+// A related variable PRODUCT_APEX_BOOT_JARS contains bootclasspath libraries that are in APEXes.
+// They are not included in the boot image. The only exception here are ART jars and core-icu4j.jar
+// that have been historically part of the boot image and are now in apexes; they are in boot images
+// and core-icu4j.jar is generally treated as being part of PRODUCT_BOOT_JARS.
//
// One exception to the above rules are "coverage" builds (a special build flavor which requires
// setting environment variable EMMA_INSTRUMENT_FRAMEWORK=true). In coverage builds the Java code in
@@ -810,10 +810,10 @@
// generateUpdatableBcpPackagesRule generates the rule to create the updatable-bcp-packages.txt file
// and returns a path to the generated file.
-func generateUpdatableBcpPackagesRule(ctx android.ModuleContext, image *bootImageConfig, updatableModules []android.Module) android.WritablePath {
+func generateUpdatableBcpPackagesRule(ctx android.ModuleContext, image *bootImageConfig, apexModules []android.Module) android.WritablePath {
// Collect `permitted_packages` for updatable boot jars.
var updatablePackages []string
- for _, module := range updatableModules {
+ for _, module := range apexModules {
if j, ok := module.(PermittedPackagesForUpdatableBootJars); ok {
pp := j.PermittedPackagesForUpdatableBootJars()
if len(pp) > 0 {
diff --git a/java/dexpreopt_config.go b/java/dexpreopt_config.go
index 1507aaf..415a1d4 100644
--- a/java/dexpreopt_config.go
+++ b/java/dexpreopt_config.go
@@ -142,14 +142,14 @@
return genBootImageConfigs(ctx)[frameworkBootImageName]
}
-// Updatable boot config allows to access build/install paths of updatable boot jars without going
+// Apex boot config allows to access build/install paths of apex boot jars without going
// through the usual trouble of registering dependencies on those modules and extracting build paths
// from those dependencies.
-type updatableBootConfig struct {
- // A list of updatable boot jars.
+type apexBootConfig struct {
+ // A list of apex boot jars.
modules android.ConfiguredJarList
- // A list of predefined build paths to updatable boot jars. They are configured very early,
+ // A list of predefined build paths to apex boot jars. They are configured very early,
// before the modules for these jars are processed and the actual paths are generated, and
// later on a singleton adds commands to copy actual jars to the predefined paths.
dexPaths android.WritablePaths
@@ -161,21 +161,21 @@
dexLocations []string
}
-var updatableBootConfigKey = android.NewOnceKey("updatableBootConfig")
+var updatableBootConfigKey = android.NewOnceKey("apexBootConfig")
-// Returns updatable boot config.
-func GetUpdatableBootConfig(ctx android.PathContext) updatableBootConfig {
+// Returns apex boot config.
+func GetApexBootConfig(ctx android.PathContext) apexBootConfig {
return ctx.Config().Once(updatableBootConfigKey, func() interface{} {
- updatableBootJars := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
+ apexBootJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
- dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "updatable_bootjars")
- dexPaths := updatableBootJars.BuildPaths(ctx, dir)
- dexPathsByModuleName := updatableBootJars.BuildPathsByModule(ctx, dir)
+ dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "apex_bootjars")
+ dexPaths := apexBootJars.BuildPaths(ctx, dir)
+ dexPathsByModuleName := apexBootJars.BuildPathsByModule(ctx, dir)
- dexLocations := updatableBootJars.DevicePaths(ctx.Config(), android.Android)
+ dexLocations := apexBootJars.DevicePaths(ctx.Config(), android.Android)
- return updatableBootConfig{updatableBootJars, dexPaths, dexPathsByModuleName, dexLocations}
- }).(updatableBootConfig)
+ return apexBootConfig{apexBootJars, dexPaths, dexPathsByModuleName, dexLocations}
+ }).(apexBootConfig)
}
// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
@@ -188,10 +188,10 @@
dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
if withUpdatable {
- // Updatable boot jars (they are used only in dexpreopt, but not in the boot image).
- updBootConfig := GetUpdatableBootConfig(ctx)
- dexPaths = append(dexPaths, updBootConfig.dexPaths...)
- dexLocations = append(dexLocations, updBootConfig.dexLocations...)
+ // Apex boot jars (they are used only in dexpreopt, but not in the boot image).
+ apexBootConfig := GetApexBootConfig(ctx)
+ dexPaths = append(dexPaths, apexBootConfig.dexPaths...)
+ dexLocations = append(dexLocations, apexBootConfig.dexLocations...)
}
return dexPaths, dexLocations
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index f901434..30683da 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -118,11 +118,11 @@
}
func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool {
- // Get the configured non-updatable and updatable boot jars.
- nonUpdatableBootJars := ctx.Config().NonUpdatableBootJars()
- updatableBootJars := ctx.Config().UpdatableBootJars()
- active := isModuleInConfiguredList(ctx, module, nonUpdatableBootJars) ||
- isModuleInConfiguredList(ctx, module, updatableBootJars)
+ // Get the configured platform and apex boot jars.
+ nonApexBootJars := ctx.Config().NonApexBootJars()
+ apexBootJars := ctx.Config().ApexBootJars()
+ active := isModuleInConfiguredList(ctx, module, nonApexBootJars) ||
+ isModuleInConfiguredList(ctx, module, apexBootJars)
return active
}
diff --git a/java/java.go b/java/java.go
index e38a714..b6e2a54 100644
--- a/java/java.go
+++ b/java/java.go
@@ -457,7 +457,7 @@
var _ android.ApexModule = (*Library)(nil)
-// Provides access to the list of permitted packages from updatable boot jars.
+// Provides access to the list of permitted packages from apex boot jars.
type PermittedPackagesForUpdatableBootJars interface {
PermittedPackagesForUpdatableBootJars() []string
}
diff --git a/java/lint_defaults.txt b/java/lint_defaults.txt
index 75de7dc..4bc0c5f 100644
--- a/java/lint_defaults.txt
+++ b/java/lint_defaults.txt
@@ -1,10 +1,37 @@
# Treat LintError as fatal to catch invocation errors
--fatal_check LintError
+# Checks which do not apply to the platform (implementation
+# in lint assumes that it's running on app code)
+
+--disable_check AnimatorKeep
+--disable_check AppBundleLocaleChanges
+--disable_check BlockedPrivateApi
+--disable_check CustomSplashScreen
+--disable_check CustomX509TrustManager
+--disable_check Deprecated
+--disable_check ExifInterface
+--disable_check HardwareIds
+--disable_check InvalidWakeLockTag
+--disable_check LibraryCustomView
+--disable_check MissingPermission
+--disable_check NonConstantResourceId
+--disable_check OldTargetApi
+--disable_check Override
+--disable_check PackageManagerGetSignatures
+--disable_check PrivateApi
+--disable_check ProtectedPermissions
+--disable_check QueryPermissionsNeeded
+--disable_check ScopedStorage
+--disable_check ServiceCast
+--disable_check SoonBlockedPrivateApi
+--disable_check SuspiciousImport
+--disable_check UnusedResources
+--disable_check ViewConstructor
+
# Downgrade existing errors to warnings
--warning_check AppCompatResource # 55 occurences in 10 modules
--warning_check AppLinkUrlError # 111 occurences in 53 modules
---warning_check BlockedPrivateApi # 2 occurences in 2 modules
--warning_check ByteOrderMark # 2 occurences in 2 modules
--warning_check DuplicateActivity # 3 occurences in 3 modules
--warning_check DuplicateDefinition # 3623 occurences in 48 modules
@@ -22,9 +49,7 @@
--warning_check Instantiatable # 145 occurences in 19 modules
--warning_check InvalidPermission # 6 occurences in 4 modules
--warning_check InvalidUsesTagAttribute # 6 occurences in 2 modules
---warning_check InvalidWakeLockTag # 111 occurences in 37 modules
--warning_check JavascriptInterface # 3 occurences in 2 modules
---warning_check LibraryCustomView # 9 occurences in 4 modules
--warning_check LogTagMismatch # 81 occurences in 13 modules
--warning_check LongLogTag # 249 occurences in 12 modules
--warning_check MenuTitle # 5 occurences in 4 modules
@@ -35,7 +60,6 @@
--warning_check MissingLeanbackLauncher # 3 occurences in 3 modules
--warning_check MissingLeanbackSupport # 2 occurences in 2 modules
--warning_check MissingOnPlayFromSearch # 1 occurences in 1 modules
---warning_check MissingPermission # 2071 occurences in 150 modules
--warning_check MissingPrefix # 46 occurences in 41 modules
--warning_check MissingQuantity # 100 occurences in 1 modules
--warning_check MissingSuperCall # 121 occurences in 36 modules
@@ -47,9 +71,7 @@
--warning_check ObjectAnimatorBinding # 14 occurences in 5 modules
--warning_check OnClick # 49 occurences in 21 modules
--warning_check Orientation # 77 occurences in 19 modules
---warning_check Override # 385 occurences in 36 modules
--warning_check ParcelCreator # 23 occurences in 2 modules
---warning_check ProtectedPermissions # 2413 occurences in 381 modules
--warning_check Range # 80 occurences in 28 modules
--warning_check RecyclerView # 1 occurences in 1 modules
--warning_check ReferenceType # 4 occurences in 1 modules
@@ -60,8 +82,6 @@
--warning_check ResourceType # 137 occurences in 36 modules
--warning_check RestrictedApi # 28 occurences in 5 modules
--warning_check RtlCompat # 9 occurences in 6 modules
---warning_check ServiceCast # 3 occurences in 1 modules
---warning_check SoonBlockedPrivateApi # 5 occurences in 3 modules
--warning_check StringFormatInvalid # 148 occurences in 11 modules
--warning_check StringFormatMatches # 4800 occurences in 30 modules
--warning_check UnknownId # 8 occurences in 7 modules
@@ -74,11 +94,9 @@
--warning_check WrongThread # 14 occurences in 6 modules
--warning_check WrongViewCast # 1 occurences in 1 modules
-# TODO(b/158390965): remove this when lint doesn't crash
---disable_check HardcodedDebugMode
-
---warning_check QueryAllPackagesPermission
-
--warning_check CoarseFineLocation
--warning_check IntentFilterExportedReceiver
+--warning_check QueryAllPackagesPermission
--warning_check RemoteViewLayout
+--warning_check SupportAnnotationUsage
+--warning_check UniqueConstants
diff --git a/java/platform_bootclasspath.go b/java/platform_bootclasspath.go
index 8bed3e9..3ff4c77 100644
--- a/java/platform_bootclasspath.go
+++ b/java/platform_bootclasspath.go
@@ -32,9 +32,9 @@
// The tags used for the dependencies between the platform bootclasspath and any configured boot
// jars.
var (
- platformBootclasspathArtBootJarDepTag = bootclasspathDependencyTag{name: "art-boot-jar"}
- platformBootclasspathNonUpdatableBootJarDepTag = bootclasspathDependencyTag{name: "non-updatable-boot-jar"}
- platformBootclasspathUpdatableBootJarDepTag = bootclasspathDependencyTag{name: "updatable-boot-jar"}
+ platformBootclasspathArtBootJarDepTag = bootclasspathDependencyTag{name: "art-boot-jar"}
+ platformBootclasspathBootJarDepTag = bootclasspathDependencyTag{name: "platform-boot-jar"}
+ platformBootclasspathApexBootJarDepTag = bootclasspathDependencyTag{name: "apex-boot-jar"}
)
type platformBootclasspathModule struct {
@@ -131,11 +131,11 @@
// Add dependencies on all the non-updatable module configured in the "boot" boot image. That does
// not include modules configured in the "art" boot image.
bootImageConfig := b.getImageConfig(ctx)
- addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules, platformBootclasspathNonUpdatableBootJarDepTag)
+ addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules, platformBootclasspathBootJarDepTag)
- // Add dependencies on all the updatable modules.
- updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
- addDependenciesOntoBootImageModules(ctx, updatableModules, platformBootclasspathUpdatableBootJarDepTag)
+ // Add dependencies on all the apex jars.
+ apexJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
+ addDependenciesOntoBootImageModules(ctx, apexJars, platformBootclasspathApexBootJarDepTag)
// Add dependencies on all the fragments.
b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
@@ -163,16 +163,16 @@
}
func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- // Gather all the dependencies from the art, updatable and non-updatable boot jars.
+ // Gather all the dependencies from the art, platform, and apex boot jars.
artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
- nonUpdatableModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathNonUpdatableBootJarDepTag)
- updatableModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathUpdatableBootJarDepTag)
+ platformModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathBootJarDepTag)
+ apexModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathApexBootJarDepTag)
// Concatenate them all, in order as they would appear on the bootclasspath.
var allModules []android.Module
allModules = append(allModules, artModules...)
- allModules = append(allModules, nonUpdatableModules...)
- allModules = append(allModules, updatableModules...)
+ allModules = append(allModules, platformModules...)
+ allModules = append(allModules, apexModules...)
b.configuredModules = allModules
// Gather all the fragments dependencies.
@@ -180,8 +180,8 @@
// Check the configuration of the boot modules.
// ART modules are checked by the art-bootclasspath-fragment.
- b.checkNonUpdatableModules(ctx, nonUpdatableModules)
- b.checkUpdatableModules(ctx, updatableModules)
+ b.checkPlatformModules(ctx, platformModules)
+ b.checkApexModules(ctx, apexModules)
b.generateClasspathProtoBuildActions(ctx)
@@ -193,7 +193,7 @@
return
}
- b.generateBootImageBuildActions(ctx, nonUpdatableModules, updatableModules)
+ b.generateBootImageBuildActions(ctx, platformModules, apexModules)
}
// Generate classpaths.proto config
@@ -209,7 +209,7 @@
jars := b.getImageConfig(ctx).modules
// Include jars from APEXes that don't populate their classpath proto config.
- remainingJars := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
+ remainingJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
for _, fragment := range b.fragments {
info := ctx.OtherModuleProvider(fragment, ClasspathFragmentProtoContentInfoProvider).(ClasspathFragmentProtoContentInfo)
if info.ClasspathFragmentProtoGenerated {
@@ -223,9 +223,10 @@
return jars
}
-// checkNonUpdatableModules ensures that the non-updatable modules supplied are not part of an
-// updatable module.
-func (b *platformBootclasspathModule) checkNonUpdatableModules(ctx android.ModuleContext, modules []android.Module) {
+// checkPlatformModules ensures that the non-updatable modules supplied are not part of an
+// apex module.
+func (b *platformBootclasspathModule) checkPlatformModules(ctx android.ModuleContext, modules []android.Module) {
+ // TODO(satayev): change this check to only allow core-icu4j, all apex jars should not be here.
for _, m := range modules {
apexInfo := ctx.OtherModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo)
fromUpdatableApex := apexInfo.Updatable
@@ -238,8 +239,8 @@
}
}
-// checkUpdatableModules ensures that the updatable modules supplied are not from the platform.
-func (b *platformBootclasspathModule) checkUpdatableModules(ctx android.ModuleContext, modules []android.Module) {
+// checkApexModules ensures that the apex modules supplied are not from the platform.
+func (b *platformBootclasspathModule) checkApexModules(ctx android.ModuleContext, modules []android.Module) {
for _, m := range modules {
apexInfo := ctx.OtherModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo)
fromUpdatableApex := apexInfo.Updatable
@@ -255,12 +256,12 @@
// modules is complete.
if !ctx.Config().AlwaysUsePrebuiltSdks() {
// error: this jar is part of the platform
- ctx.ModuleErrorf("module %q from platform is not allowed in the updatable boot jars list", name)
+ ctx.ModuleErrorf("module %q from platform is not allowed in the apex boot jars list", name)
}
} else {
// TODO(b/177892522): Treat this as an error.
// Cannot do that at the moment because framework-wifi and framework-tethering are in the
- // PRODUCT_UPDATABLE_BOOT_JARS but not marked as updatable in AOSP.
+ // PRODUCT_APEX_BOOT_JARS but not marked as updatable in AOSP.
}
}
}
@@ -405,7 +406,7 @@
}
// generateBootImageBuildActions generates ninja rules related to the boot image creation.
-func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext, nonUpdatableModules, updatableModules []android.Module) {
+func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext, platformModules, apexModules []android.Module) {
// Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
// GenerateSingletonBuildActions method as it cannot create it for itself.
dexpreopt.GetGlobalSoongConfig(ctx)
@@ -428,17 +429,17 @@
// TODO(b/193889859): Remove when the prebuilts have been updated.
if !ctx.Config().AlwaysUsePrebuiltSdks() {
// Generate the updatable bootclasspath packages rule.
- generateUpdatableBcpPackagesRule(ctx, imageConfig, updatableModules)
+ generateUpdatableBcpPackagesRule(ctx, imageConfig, apexModules)
}
- // Copy non-updatable module dex jars to their predefined locations.
- nonUpdatableBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, nonUpdatableModules)
- copyBootJarsToPredefinedLocations(ctx, nonUpdatableBootDexJarsByModule, imageConfig.dexPathsByModule)
+ // Copy platform module dex jars to their predefined locations.
+ platformBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, platformModules)
+ copyBootJarsToPredefinedLocations(ctx, platformBootDexJarsByModule, imageConfig.dexPathsByModule)
- // Copy updatable module dex jars to their predefined locations.
- config := GetUpdatableBootConfig(ctx)
- updatableBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, updatableModules)
- copyBootJarsToPredefinedLocations(ctx, updatableBootDexJarsByModule, config.dexPathsByModule)
+ // Copy apex module dex jars to their predefined locations.
+ config := GetApexBootConfig(ctx)
+ apexBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, apexModules)
+ copyBootJarsToPredefinedLocations(ctx, apexBootDexJarsByModule, config.dexPathsByModule)
// Build a profile for the image config and then use that to build the boot image.
profile := bootImageProfileRule(ctx, imageConfig)
diff --git a/java/systemserver_classpath_fragment.go b/java/systemserver_classpath_fragment.go
index 28a5a2c..6c2a5b5 100644
--- a/java/systemserver_classpath_fragment.go
+++ b/java/systemserver_classpath_fragment.go
@@ -106,12 +106,8 @@
func (s *SystemServerClasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
global := dexpreopt.GetGlobalConfig(ctx)
- possibleUpdatableModules := gatherPossibleUpdatableModuleNamesAndStems(ctx, s.properties.Contents, systemServerClasspathFragmentContentDepTag)
-
- // 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(possibleUpdatableModules)
+ possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, s.properties.Contents, systemServerClasspathFragmentContentDepTag)
+ return global.ApexSystemServerJars.Filter(possibleUpdatableModules)
}
type systemServerClasspathFragmentContentDependencyTag struct {
diff --git a/java/testing.go b/java/testing.go
index e2ff5cd..8860b45 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -214,15 +214,15 @@
)
}
-// FixtureConfigureUpdatableBootJars configures the updatable boot jars in both the
+// FixtureConfigureApexBootJars configures the apex boot jars in both the
// dexpreopt.GlobalConfig and Config.productVariables structs. As a side effect that enables
// dexpreopt.
-func FixtureConfigureUpdatableBootJars(bootJars ...string) android.FixturePreparer {
+func FixtureConfigureApexBootJars(bootJars ...string) android.FixturePreparer {
return android.GroupFixturePreparers(
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
- variables.UpdatableBootJars = android.CreateTestConfiguredJarList(bootJars)
+ variables.ApexBootJars = android.CreateTestConfiguredJarList(bootJars)
}),
- dexpreopt.FixtureSetUpdatableBootJars(bootJars...),
+ dexpreopt.FixtureSetApexBootJars(bootJars...),
// Add a fake dex2oatd module.
dexpreopt.PrepareForTestWithFakeDex2oatd,
diff --git a/mk2rbc/Android.bp b/mk2rbc/Android.bp
new file mode 100644
index 0000000..3ea3f7f
--- /dev/null
+++ b/mk2rbc/Android.bp
@@ -0,0 +1,39 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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.
+
+blueprint_go_binary {
+ name: "mk2rbc",
+ srcs: ["cmd/mk2rbc.go"],
+ deps: [
+ "mk2rbc-lib",
+ "androidmk-parser",
+ ],
+}
+
+bootstrap_go_package {
+ name: "mk2rbc-lib",
+ pkgPath: "android/soong/mk2rbc",
+ srcs: [
+ "android_products.go",
+ "config_variables.go",
+ "expr.go",
+ "mk2rbc.go",
+ "node.go",
+ "soong_variables.go",
+ "types.go",
+ "variable.go",
+ ],
+ deps: ["androidmk-parser"],
+}
diff --git a/mk2rbc/TODO b/mk2rbc/TODO
new file mode 100644
index 0000000..731deb6
--- /dev/null
+++ b/mk2rbc/TODO
@@ -0,0 +1,14 @@
+* Checking filter/filter-out results is incorrect if pattern contains '%'
+* Need heuristics to recognize that a variable is local. Propose to use lowercase.
+* Need heuristics for the local variable type. Propose '_list' suffix
+* Internal source tree has variables in the inherit-product macro argument. Handle it
+* Enumerate all environment variables that configuration files use.
+* Break mk2rbc.go into multiple files.
+* If variable's type is not yet known, try to divine it from the value assigned to it
+ (it may be a variable of the known type, or a function result)
+* ifneq (,$(VAR)) should translate to
+ if getattr(<>, "VAR", <default>):
+* Launcher file needs to have same suffix as the rest of the generated files
+* Implement $(shell) function
+* Write execution tests
+* Review all TODOs in mk2rbc.go
\ No newline at end of file
diff --git a/mk2rbc/cmd/mk2rbc.go b/mk2rbc/cmd/mk2rbc.go
new file mode 100644
index 0000000..4d3d8d8
--- /dev/null
+++ b/mk2rbc/cmd/mk2rbc.go
@@ -0,0 +1,496 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+// The application to convert product configuration makefiles to Starlark.
+// Converts either given list of files (and optionally the dependent files
+// of the same kind), or all all product configuration makefiles in the
+// given source tree.
+// Previous version of a converted file can be backed up.
+// Optionally prints detailed statistics at the end.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "regexp"
+ "runtime/debug"
+ "sort"
+ "strings"
+ "time"
+
+ "android/soong/androidmk/parser"
+ "android/soong/mk2rbc"
+)
+
+var (
+ rootDir = flag.String("root", ".", "the value of // for load paths")
+ // TODO(asmundak): remove this option once there is a consensus on suffix
+ suffix = flag.String("suffix", ".rbc", "generated files' suffix")
+ dryRun = flag.Bool("dry_run", false, "dry run")
+ recurse = flag.Bool("convert_dependents", false, "convert all dependent files")
+ mode = flag.String("mode", "", `"backup" to back up existing files, "write" to overwrite them`)
+ warn = flag.Bool("warnings", false, "warn about partially failed conversions")
+ verbose = flag.Bool("v", false, "print summary")
+ errstat = flag.Bool("error_stat", false, "print error statistics")
+ traceVar = flag.String("trace", "", "comma-separated list of variables to trace")
+ // TODO(asmundak): this option is for debugging
+ allInSource = flag.Bool("all", false, "convert all product config makefiles in the tree under //")
+ outputTop = flag.String("outdir", "", "write output files into this directory hierarchy")
+ launcher = flag.String("launcher", "", "generated launcher path. If set, the non-flag argument is _product_name_")
+ printProductConfigMap = flag.Bool("print_product_config_map", false, "print product config map and exit")
+ traceCalls = flag.Bool("trace_calls", false, "trace function calls")
+)
+
+func init() {
+ // Poor man's flag aliasing: works, but the usage string is ugly and
+ // both flag and its alias can be present on the command line
+ flagAlias := func(target string, alias string) {
+ if f := flag.Lookup(target); f != nil {
+ flag.Var(f.Value, alias, "alias for --"+f.Name)
+ return
+ }
+ quit("cannot alias unknown flag " + target)
+ }
+ flagAlias("suffix", "s")
+ flagAlias("root", "d")
+ flagAlias("dry_run", "n")
+ flagAlias("convert_dependents", "r")
+ flagAlias("warnings", "w")
+ flagAlias("error_stat", "e")
+}
+
+var backupSuffix string
+var tracedVariables []string
+var errorLogger = errorsByType{data: make(map[string]datum)}
+
+func main() {
+ flag.Usage = func() {
+ cmd := filepath.Base(os.Args[0])
+ fmt.Fprintf(flag.CommandLine.Output(),
+ "Usage: %[1]s flags file...\n"+
+ "or: %[1]s flags --launcher=PATH PRODUCT\n", cmd)
+ flag.PrintDefaults()
+ }
+ flag.Parse()
+
+ // Delouse
+ if *suffix == ".mk" {
+ quit("cannot use .mk as generated file suffix")
+ }
+ if *suffix == "" {
+ quit("suffix cannot be empty")
+ }
+ if *outputTop != "" {
+ if err := os.MkdirAll(*outputTop, os.ModeDir+os.ModePerm); err != nil {
+ quit(err)
+ }
+ s, err := filepath.Abs(*outputTop)
+ if err != nil {
+ quit(err)
+ }
+ *outputTop = s
+ }
+ if *allInSource && len(flag.Args()) > 0 {
+ quit("file list cannot be specified when -all is present")
+ }
+ if *allInSource && *launcher != "" {
+ quit("--all and --launcher are mutually exclusive")
+ }
+
+ // Flag-driven adjustments
+ if (*suffix)[0] != '.' {
+ *suffix = "." + *suffix
+ }
+ if *mode == "backup" {
+ backupSuffix = time.Now().Format("20060102150405")
+ }
+ if *traceVar != "" {
+ tracedVariables = strings.Split(*traceVar, ",")
+ }
+
+ // Find out global variables
+ getConfigVariables()
+ getSoongVariables()
+
+ if *printProductConfigMap {
+ productConfigMap := buildProductConfigMap()
+ var products []string
+ for p := range productConfigMap {
+ products = append(products, p)
+ }
+ sort.Strings(products)
+ for _, p := range products {
+ fmt.Println(p, productConfigMap[p])
+ }
+ os.Exit(0)
+ }
+
+ // Convert!
+ ok := true
+ if *launcher != "" {
+ if len(flag.Args()) != 1 {
+ quit(fmt.Errorf("a launcher can be generated only for a single product"))
+ }
+ product := flag.Args()[0]
+ productConfigMap := buildProductConfigMap()
+ path, found := productConfigMap[product]
+ if !found {
+ quit(fmt.Errorf("cannot generate configuration launcher for %s, it is not a known product",
+ product))
+ }
+ ok = convertOne(path) && ok
+ err := writeGenerated(*launcher, mk2rbc.Launcher(outputFilePath(path), mk2rbc.MakePath2ModuleName(path)))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s:%s", path, err)
+ ok = false
+ }
+
+ } else {
+ files := flag.Args()
+ if *allInSource {
+ productConfigMap := buildProductConfigMap()
+ for _, path := range productConfigMap {
+ files = append(files, path)
+ }
+ }
+ for _, mkFile := range files {
+ ok = convertOne(mkFile) && ok
+ }
+ }
+
+ printStats()
+ if *errstat {
+ errorLogger.printStatistics()
+ }
+ if !ok {
+ os.Exit(1)
+ }
+}
+
+func quit(s interface{}) {
+ fmt.Fprintln(os.Stderr, s)
+ os.Exit(2)
+}
+
+func buildProductConfigMap() map[string]string {
+ const androidProductsMk = "AndroidProducts.mk"
+ // Build the list of AndroidProducts.mk files: it's
+ // build/make/target/product/AndroidProducts.mk plus
+ // device/**/AndroidProducts.mk
+ targetAndroidProductsFile := filepath.Join(*rootDir, "build", "make", "target", "product", androidProductsMk)
+ if _, err := os.Stat(targetAndroidProductsFile); err != nil {
+ fmt.Fprintf(os.Stderr, "%s: %s\n(hint: %s is not a source tree root)\n",
+ targetAndroidProductsFile, err, *rootDir)
+ }
+ productConfigMap := make(map[string]string)
+ if err := mk2rbc.UpdateProductConfigMap(productConfigMap, targetAndroidProductsFile); err != nil {
+ fmt.Fprintf(os.Stderr, "%s: %s\n", targetAndroidProductsFile, err)
+ }
+ _ = filepath.Walk(filepath.Join(*rootDir, "device"),
+ func(path string, info os.FileInfo, err error) error {
+ if info.IsDir() || filepath.Base(path) != androidProductsMk {
+ return nil
+ }
+ if err2 := mk2rbc.UpdateProductConfigMap(productConfigMap, path); err2 != nil {
+ fmt.Fprintf(os.Stderr, "%s: %s\n", path, err)
+ // Keep going, we want to find all such errors in a single run
+ }
+ return nil
+ })
+ return productConfigMap
+}
+
+func getConfigVariables() {
+ path := filepath.Join(*rootDir, "build", "make", "core", "product.mk")
+ if err := mk2rbc.FindConfigVariables(path, mk2rbc.KnownVariables); err != nil {
+ quit(fmt.Errorf("%s\n(check --root[=%s], it should point to the source root)",
+ err, *rootDir))
+ }
+}
+
+// Implements mkparser.Scope, to be used by mkparser.Value.Value()
+type fileNameScope struct {
+ mk2rbc.ScopeBase
+}
+
+func (s fileNameScope) Get(name string) string {
+ if name != "BUILD_SYSTEM" {
+ return fmt.Sprintf("$(%s)", name)
+ }
+ return filepath.Join(*rootDir, "build", "make", "core")
+}
+
+func getSoongVariables() {
+ path := filepath.Join(*rootDir, "build", "make", "core", "soong_config.mk")
+ err := mk2rbc.FindSoongVariables(path, fileNameScope{}, mk2rbc.KnownVariables)
+ if err != nil {
+ quit(err)
+ }
+}
+
+var converted = make(map[string]*mk2rbc.StarlarkScript)
+
+//goland:noinspection RegExpRepeatedSpace
+var cpNormalizer = regexp.MustCompile(
+ "# Copyright \\(C\\) 20.. The Android Open Source Project")
+
+const cpNormalizedCopyright = "# Copyright (C) 20xx The Android Open Source Project"
+const copyright = `#
+# Copyright (C) 20xx The Android Open Source Project
+#
+# 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.
+#
+`
+
+// Convert a single file.
+// Write the result either to the same directory, to the same place in
+// the output hierarchy, or to the stdout.
+// Optionally, recursively convert the files this one includes by
+// $(call inherit-product) or an include statement.
+func convertOne(mkFile string) (ok bool) {
+ if v, ok := converted[mkFile]; ok {
+ return v != nil
+ }
+ converted[mkFile] = nil
+ defer func() {
+ if r := recover(); r != nil {
+ ok = false
+ fmt.Fprintf(os.Stderr, "%s: panic while converting: %s\n%s\n", mkFile, r, debug.Stack())
+ }
+ }()
+
+ mk2starRequest := mk2rbc.Request{
+ MkFile: mkFile,
+ Reader: nil,
+ RootDir: *rootDir,
+ OutputDir: *outputTop,
+ OutputSuffix: *suffix,
+ TracedVariables: tracedVariables,
+ TraceCalls: *traceCalls,
+ WarnPartialSuccess: *warn,
+ }
+ if *errstat {
+ mk2starRequest.ErrorLogger = errorLogger
+ }
+ ss, err := mk2rbc.Convert(mk2starRequest)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, mkFile, ": ", err)
+ return false
+ }
+ script := ss.String()
+ outputPath := outputFilePath(mkFile)
+
+ if *dryRun {
+ fmt.Printf("==== %s ====\n", outputPath)
+ // Print generated script after removing the copyright header
+ outText := cpNormalizer.ReplaceAllString(script, cpNormalizedCopyright)
+ fmt.Println(strings.TrimPrefix(outText, copyright))
+ } else {
+ if err := maybeBackup(outputPath); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return false
+ }
+ if err := writeGenerated(outputPath, script); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ return false
+ }
+ }
+ ok = true
+ if *recurse {
+ for _, sub := range ss.SubConfigFiles() {
+ // File may be absent if it is a conditional load
+ if _, err := os.Stat(sub); os.IsNotExist(err) {
+ continue
+ }
+ ok = convertOne(sub) && ok
+ }
+ }
+ converted[mkFile] = ss
+ return ok
+}
+
+// Optionally saves the previous version of the generated file
+func maybeBackup(filename string) error {
+ stat, err := os.Stat(filename)
+ if os.IsNotExist(err) {
+ return nil
+ }
+ if !stat.Mode().IsRegular() {
+ return fmt.Errorf("%s exists and is not a regular file", filename)
+ }
+ switch *mode {
+ case "backup":
+ return os.Rename(filename, filename+backupSuffix)
+ case "write":
+ return os.Remove(filename)
+ default:
+ return fmt.Errorf("%s already exists, use --mode option", filename)
+ }
+}
+
+func outputFilePath(mkFile string) string {
+ path := strings.TrimSuffix(mkFile, filepath.Ext(mkFile)) + *suffix
+ if *outputTop != "" {
+ path = filepath.Join(*outputTop, path)
+ }
+ return path
+}
+
+func writeGenerated(path string, contents string) error {
+ if err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm); err != nil {
+ return err
+ }
+ if err := ioutil.WriteFile(path, []byte(contents), 0644); err != nil {
+ return err
+ }
+ return nil
+}
+
+func printStats() {
+ var sortedFiles []string
+ if !*warn && !*verbose {
+ return
+ }
+ for p := range converted {
+ sortedFiles = append(sortedFiles, p)
+ }
+ sort.Strings(sortedFiles)
+
+ nOk, nPartial, nFailed := 0, 0, 0
+ for _, f := range sortedFiles {
+ if converted[f] == nil {
+ nFailed++
+ } else if converted[f].HasErrors() {
+ nPartial++
+ } else {
+ nOk++
+ }
+ }
+ if *warn {
+ if nPartial > 0 {
+ fmt.Fprintf(os.Stderr, "Conversion was partially successful for:\n")
+ for _, f := range sortedFiles {
+ if ss := converted[f]; ss != nil && ss.HasErrors() {
+ fmt.Fprintln(os.Stderr, " ", f)
+ }
+ }
+ }
+
+ if nFailed > 0 {
+ fmt.Fprintf(os.Stderr, "Conversion failed for files:\n")
+ for _, f := range sortedFiles {
+ if converted[f] == nil {
+ fmt.Fprintln(os.Stderr, " ", f)
+ }
+ }
+ }
+ }
+ if *verbose {
+ fmt.Fprintf(os.Stderr, "%-16s%5d\n", "Succeeded:", nOk)
+ fmt.Fprintf(os.Stderr, "%-16s%5d\n", "Partial:", nPartial)
+ fmt.Fprintf(os.Stderr, "%-16s%5d\n", "Failed:", nFailed)
+ }
+}
+
+type datum struct {
+ count int
+ formattingArgs []string
+}
+
+type errorsByType struct {
+ data map[string]datum
+}
+
+func (ebt errorsByType) NewError(message string, node parser.Node, args ...interface{}) {
+ v, exists := ebt.data[message]
+ if exists {
+ v.count++
+ } else {
+ v = datum{1, nil}
+ }
+ if strings.Contains(message, "%s") {
+ var newArg1 string
+ if len(args) == 0 {
+ panic(fmt.Errorf(`%s has %%s but args are missing`, message))
+ }
+ newArg1 = fmt.Sprint(args[0])
+ if message == "unsupported line" {
+ newArg1 = node.Dump()
+ } else if message == "unsupported directive %s" {
+ if newArg1 == "include" || newArg1 == "-include" {
+ newArg1 = node.Dump()
+ }
+ }
+ v.formattingArgs = append(v.formattingArgs, newArg1)
+ }
+ ebt.data[message] = v
+}
+
+func (ebt errorsByType) printStatistics() {
+ if len(ebt.data) > 0 {
+ fmt.Fprintln(os.Stderr, "Error counts:")
+ }
+ for message, data := range ebt.data {
+ if len(data.formattingArgs) == 0 {
+ fmt.Fprintf(os.Stderr, "%4d %s\n", data.count, message)
+ continue
+ }
+ itemsByFreq, count := stringsWithFreq(data.formattingArgs, 30)
+ fmt.Fprintf(os.Stderr, "%4d %s [%d unique items]:\n", data.count, message, count)
+ fmt.Fprintln(os.Stderr, " ", itemsByFreq)
+ }
+}
+
+func stringsWithFreq(items []string, topN int) (string, int) {
+ freq := make(map[string]int)
+ for _, item := range items {
+ freq[strings.TrimPrefix(strings.TrimSuffix(item, "]"), "[")]++
+ }
+ var sorted []string
+ for item := range freq {
+ sorted = append(sorted, item)
+ }
+ sort.Slice(sorted, func(i int, j int) bool {
+ return freq[sorted[i]] > freq[sorted[j]]
+ })
+ sep := ""
+ res := ""
+ for i, item := range sorted {
+ if i >= topN {
+ res += " ..."
+ break
+ }
+ count := freq[item]
+ if count > 1 {
+ res += fmt.Sprintf("%s%s(%d)", sep, item, count)
+ } else {
+ res += fmt.Sprintf("%s%s", sep, item)
+ }
+ sep = ", "
+ }
+ return res, len(sorted)
+}
diff --git a/mk2rbc/expr.go b/mk2rbc/expr.go
new file mode 100644
index 0000000..b06ed90
--- /dev/null
+++ b/mk2rbc/expr.go
@@ -0,0 +1,580 @@
+// Copyright 2021 Google LLC
+//
+// 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 mk2rbc
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+
+ mkparser "android/soong/androidmk/parser"
+)
+
+// Represents an expression in the Starlark code. An expression has
+// a type, and it can be evaluated.
+type starlarkExpr interface {
+ starlarkNode
+ typ() starlarkType
+ // Try to substitute variable values. Return substitution result
+ // and whether it is the same as the original expression.
+ eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool)
+ // Emit the code to copy the expression, otherwise we will end up
+ // with source and target pointing to the same list.
+ emitListVarCopy(gctx *generationContext)
+}
+
+func maybeString(expr starlarkExpr) (string, bool) {
+ if x, ok := expr.(*stringLiteralExpr); ok {
+ return x.literal, true
+ }
+ return "", false
+}
+
+type stringLiteralExpr struct {
+ literal string
+}
+
+func (s *stringLiteralExpr) eval(_ map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ res = s
+ same = true
+ return
+}
+
+func (s *stringLiteralExpr) emit(gctx *generationContext) {
+ gctx.writef("%q", s.literal)
+}
+
+func (_ *stringLiteralExpr) typ() starlarkType {
+ return starlarkTypeString
+}
+
+func (s *stringLiteralExpr) emitListVarCopy(gctx *generationContext) {
+ s.emit(gctx)
+}
+
+// Integer literal
+type intLiteralExpr struct {
+ literal int
+}
+
+func (s *intLiteralExpr) eval(_ map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ res = s
+ same = true
+ return
+}
+
+func (s *intLiteralExpr) emit(gctx *generationContext) {
+ gctx.writef("%d", s.literal)
+}
+
+func (_ *intLiteralExpr) typ() starlarkType {
+ return starlarkTypeInt
+}
+
+func (s *intLiteralExpr) emitListVarCopy(gctx *generationContext) {
+ s.emit(gctx)
+}
+
+// interpolateExpr represents Starlark's interpolation operator <string> % list
+// we break <string> into a list of chunks, i.e., "first%second%third" % (X, Y)
+// will have chunks = ["first", "second", "third"] and args = [X, Y]
+type interpolateExpr struct {
+ chunks []string // string chunks, separated by '%'
+ args []starlarkExpr
+}
+
+func (xi *interpolateExpr) emit(gctx *generationContext) {
+ if len(xi.chunks) != len(xi.args)+1 {
+ panic(fmt.Errorf("malformed interpolateExpr: #chunks(%d) != #args(%d)+1",
+ len(xi.chunks), len(xi.args)))
+ }
+ // Generate format as join of chunks, but first escape '%' in them
+ format := strings.ReplaceAll(xi.chunks[0], "%", "%%")
+ for _, chunk := range xi.chunks[1:] {
+ format += "%s" + strings.ReplaceAll(chunk, "%", "%%")
+ }
+ gctx.writef("%q %% ", format)
+ emitarg := func(arg starlarkExpr) {
+ if arg.typ() == starlarkTypeList {
+ gctx.write(`" ".join(`)
+ arg.emit(gctx)
+ gctx.write(`)`)
+ } else {
+ arg.emit(gctx)
+ }
+ }
+ if len(xi.args) == 1 {
+ emitarg(xi.args[0])
+ } else {
+ sep := "("
+ for _, arg := range xi.args {
+ gctx.write(sep)
+ emitarg(arg)
+ sep = ", "
+ }
+ gctx.write(")")
+ }
+}
+
+func (xi *interpolateExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ same = true
+ newChunks := []string{xi.chunks[0]}
+ var newArgs []starlarkExpr
+ for i, arg := range xi.args {
+ newArg, sameArg := arg.eval(valueMap)
+ same = same && sameArg
+ switch x := newArg.(type) {
+ case *stringLiteralExpr:
+ newChunks[len(newChunks)-1] += x.literal + xi.chunks[i+1]
+ same = false
+ continue
+ case *intLiteralExpr:
+ newChunks[len(newChunks)-1] += strconv.Itoa(x.literal) + xi.chunks[i+1]
+ same = false
+ continue
+ default:
+ newChunks = append(newChunks, xi.chunks[i+1])
+ newArgs = append(newArgs, newArg)
+ }
+ }
+ if same {
+ res = xi
+ } else if len(newChunks) == 1 {
+ res = &stringLiteralExpr{newChunks[0]}
+ } else {
+ res = &interpolateExpr{chunks: newChunks, args: newArgs}
+ }
+ return
+}
+
+func (_ *interpolateExpr) typ() starlarkType {
+ return starlarkTypeString
+}
+
+func (xi *interpolateExpr) emitListVarCopy(gctx *generationContext) {
+ xi.emit(gctx)
+}
+
+type variableRefExpr struct {
+ ref variable
+ isDefined bool
+}
+
+func (v *variableRefExpr) eval(map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ predefined, ok := v.ref.(*predefinedVariable)
+ if same = !ok; same {
+ res = v
+ } else {
+ res = predefined.value
+ }
+ return
+}
+
+func (v *variableRefExpr) emit(gctx *generationContext) {
+ v.ref.emitGet(gctx, v.isDefined)
+}
+
+func (v *variableRefExpr) typ() starlarkType {
+ return v.ref.valueType()
+}
+
+func (v *variableRefExpr) emitListVarCopy(gctx *generationContext) {
+ v.emit(gctx)
+ if v.typ() == starlarkTypeList {
+ gctx.write("[:]") // this will copy the list
+ }
+}
+
+type notExpr struct {
+ expr starlarkExpr
+}
+
+func (n *notExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ if x, same := n.expr.eval(valueMap); same {
+ res = n
+ } else {
+ res = ¬Expr{expr: x}
+ }
+ return
+}
+
+func (n *notExpr) emit(ctx *generationContext) {
+ ctx.write("not ")
+ n.expr.emit(ctx)
+}
+
+func (_ *notExpr) typ() starlarkType {
+ return starlarkTypeBool
+}
+
+func (n *notExpr) emitListVarCopy(gctx *generationContext) {
+ n.emit(gctx)
+}
+
+type eqExpr struct {
+ left, right starlarkExpr
+ isEq bool // if false, it's !=
+}
+
+func (eq *eqExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ xLeft, sameLeft := eq.left.eval(valueMap)
+ xRight, sameRight := eq.right.eval(valueMap)
+ if same = sameLeft && sameRight; same {
+ res = eq
+ } else {
+ res = &eqExpr{left: xLeft, right: xRight, isEq: eq.isEq}
+ }
+ return
+}
+
+func (eq *eqExpr) emit(gctx *generationContext) {
+ // Are we checking that a variable is empty?
+ var varRef *variableRefExpr
+ if s, ok := maybeString(eq.left); ok && s == "" {
+ varRef, ok = eq.right.(*variableRefExpr)
+ } else if s, ok := maybeString(eq.right); ok && s == "" {
+ varRef, ok = eq.left.(*variableRefExpr)
+ }
+ if varRef != nil {
+ // Yes.
+ if eq.isEq {
+ gctx.write("not ")
+ }
+ varRef.emit(gctx)
+ return
+ }
+
+ // General case
+ eq.left.emit(gctx)
+ if eq.isEq {
+ gctx.write(" == ")
+ } else {
+ gctx.write(" != ")
+ }
+ eq.right.emit(gctx)
+}
+
+func (_ *eqExpr) typ() starlarkType {
+ return starlarkTypeBool
+}
+
+func (eq *eqExpr) emitListVarCopy(gctx *generationContext) {
+ eq.emit(gctx)
+}
+
+// variableDefinedExpr corresponds to Make's ifdef VAR
+type variableDefinedExpr struct {
+ v variable
+}
+
+func (v *variableDefinedExpr) eval(_ map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ res = v
+ same = true
+ return
+
+}
+
+func (v *variableDefinedExpr) emit(gctx *generationContext) {
+ if v.v != nil {
+ v.v.emitDefined(gctx)
+ return
+ }
+ gctx.writef("%s(%q)", cfnWarning, "TODO(VAR)")
+}
+
+func (_ *variableDefinedExpr) typ() starlarkType {
+ return starlarkTypeBool
+}
+
+func (v *variableDefinedExpr) emitListVarCopy(gctx *generationContext) {
+ v.emit(gctx)
+}
+
+type listExpr struct {
+ items []starlarkExpr
+}
+
+func (l *listExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ newItems := make([]starlarkExpr, len(l.items))
+ same = true
+ for i, item := range l.items {
+ var sameItem bool
+ newItems[i], sameItem = item.eval(valueMap)
+ same = same && sameItem
+ }
+ if same {
+ res = l
+ } else {
+ res = &listExpr{newItems}
+ }
+ return
+}
+
+func (l *listExpr) emit(gctx *generationContext) {
+ if !gctx.inAssignment || len(l.items) < 2 {
+ gctx.write("[")
+ sep := ""
+ for _, item := range l.items {
+ gctx.write(sep)
+ item.emit(gctx)
+ sep = ", "
+ }
+ gctx.write("]")
+ return
+ }
+
+ gctx.write("[")
+ gctx.indentLevel += 2
+
+ for _, item := range l.items {
+ gctx.newLine()
+ item.emit(gctx)
+ gctx.write(",")
+ }
+ gctx.indentLevel -= 2
+ gctx.newLine()
+ gctx.write("]")
+}
+
+func (_ *listExpr) typ() starlarkType {
+ return starlarkTypeList
+}
+
+func (l *listExpr) emitListVarCopy(gctx *generationContext) {
+ l.emit(gctx)
+}
+
+func newStringListExpr(items []string) *listExpr {
+ v := listExpr{}
+ for _, item := range items {
+ v.items = append(v.items, &stringLiteralExpr{item})
+ }
+ return &v
+}
+
+// concatExpr generates epxr1 + expr2 + ... + exprN in Starlark.
+type concatExpr struct {
+ items []starlarkExpr
+}
+
+func (c *concatExpr) emit(gctx *generationContext) {
+ if len(c.items) == 1 {
+ c.items[0].emit(gctx)
+ return
+ }
+
+ if !gctx.inAssignment {
+ c.items[0].emit(gctx)
+ for _, item := range c.items[1:] {
+ gctx.write(" + ")
+ item.emit(gctx)
+ }
+ return
+ }
+ gctx.write("(")
+ c.items[0].emit(gctx)
+ gctx.indentLevel += 2
+ for _, item := range c.items[1:] {
+ gctx.write(" +")
+ gctx.newLine()
+ item.emit(gctx)
+ }
+ gctx.write(")")
+ gctx.indentLevel -= 2
+}
+
+func (c *concatExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ same = true
+ xConcat := &concatExpr{items: make([]starlarkExpr, len(c.items))}
+ for i, item := range c.items {
+ var sameItem bool
+ xConcat.items[i], sameItem = item.eval(valueMap)
+ same = same && sameItem
+ }
+ if same {
+ res = c
+ } else {
+ res = xConcat
+ }
+ return
+}
+
+func (_ *concatExpr) typ() starlarkType {
+ return starlarkTypeList
+}
+
+func (c *concatExpr) emitListVarCopy(gctx *generationContext) {
+ c.emit(gctx)
+}
+
+// inExpr generates <expr> [not] in <list>
+type inExpr struct {
+ expr starlarkExpr
+ list starlarkExpr
+ isNot bool
+}
+
+func (i *inExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ x := &inExpr{isNot: i.isNot}
+ var sameExpr, sameList bool
+ x.expr, sameExpr = i.expr.eval(valueMap)
+ x.list, sameList = i.list.eval(valueMap)
+ if same = sameExpr && sameList; same {
+ res = i
+ } else {
+ res = x
+ }
+ return
+}
+
+func (i *inExpr) emit(gctx *generationContext) {
+ i.expr.emit(gctx)
+ if i.isNot {
+ gctx.write(" not in ")
+ } else {
+ gctx.write(" in ")
+ }
+ i.list.emit(gctx)
+}
+
+func (_ *inExpr) typ() starlarkType {
+ return starlarkTypeBool
+}
+
+func (i *inExpr) emitListVarCopy(gctx *generationContext) {
+ i.emit(gctx)
+}
+
+type indexExpr struct {
+ array starlarkExpr
+ index starlarkExpr
+}
+
+func (ix indexExpr) emit(gctx *generationContext) {
+ ix.array.emit(gctx)
+ gctx.write("[")
+ ix.index.emit(gctx)
+ gctx.write("]")
+}
+
+func (ix indexExpr) typ() starlarkType {
+ return starlarkTypeString
+}
+
+func (ix indexExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ newArray, isSameArray := ix.array.eval(valueMap)
+ newIndex, isSameIndex := ix.index.eval(valueMap)
+ if same = isSameArray && isSameIndex; same {
+ res = ix
+ } else {
+ res = &indexExpr{newArray, newIndex}
+ }
+ return
+}
+
+func (ix indexExpr) emitListVarCopy(gctx *generationContext) {
+ ix.emit(gctx)
+}
+
+type callExpr struct {
+ object starlarkExpr // nil if static call
+ name string
+ args []starlarkExpr
+ returnType starlarkType
+}
+
+func (cx *callExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ newCallExpr := &callExpr{name: cx.name, args: make([]starlarkExpr, len(cx.args)),
+ returnType: cx.returnType}
+ if cx.object != nil {
+ newCallExpr.object, same = cx.object.eval(valueMap)
+ } else {
+ same = true
+ }
+ for i, args := range cx.args {
+ var s bool
+ newCallExpr.args[i], s = args.eval(valueMap)
+ same = same && s
+ }
+ if same {
+ res = cx
+ } else {
+ res = newCallExpr
+ }
+ return
+}
+
+func (cx *callExpr) emit(gctx *generationContext) {
+ if cx.object != nil {
+ gctx.write("(")
+ cx.object.emit(gctx)
+ gctx.write(")")
+ gctx.write(".", cx.name, "(")
+ } else {
+ kf, found := knownFunctions[cx.name]
+ if !found {
+ panic(fmt.Errorf("callExpr with unknown function %q", cx.name))
+ }
+ if kf.runtimeName[0] == '!' {
+ panic(fmt.Errorf("callExpr for %q should not be there", cx.name))
+ }
+ gctx.write(kf.runtimeName, "(")
+ }
+ sep := ""
+ for _, arg := range cx.args {
+ gctx.write(sep)
+ arg.emit(gctx)
+ sep = ", "
+ }
+ gctx.write(")")
+}
+
+func (cx *callExpr) typ() starlarkType {
+ return cx.returnType
+}
+
+func (cx *callExpr) emitListVarCopy(gctx *generationContext) {
+ cx.emit(gctx)
+}
+
+type badExpr struct {
+ node mkparser.Node
+ message string
+}
+
+func (b *badExpr) eval(_ map[string]starlarkExpr) (res starlarkExpr, same bool) {
+ res = b
+ same = true
+ return
+}
+
+func (b *badExpr) emit(_ *generationContext) {
+ panic("implement me")
+}
+
+func (_ *badExpr) typ() starlarkType {
+ return starlarkTypeUnknown
+}
+
+func (b *badExpr) emitListVarCopy(gctx *generationContext) {
+ panic("implement me")
+}
+
+func maybeConvertToStringList(expr starlarkExpr) starlarkExpr {
+ if xString, ok := expr.(*stringLiteralExpr); ok {
+ return newStringListExpr(strings.Fields(xString.literal))
+ }
+ return expr
+}
diff --git a/mk2rbc/mk2rbc.go b/mk2rbc/mk2rbc.go
new file mode 100644
index 0000000..86e647d
--- /dev/null
+++ b/mk2rbc/mk2rbc.go
@@ -0,0 +1,1363 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+// Convert makefile containing device configuration to Starlark file
+// The conversion can handle the following constructs in a makefile:
+// * comments
+// * simple variable assignments
+// * $(call init-product,<file>)
+// * $(call inherit-product-if-exists
+// * if directives
+// All other constructs are carried over to the output starlark file as comments.
+//
+package mk2rbc
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "regexp"
+ "strconv"
+ "strings"
+ "text/scanner"
+
+ mkparser "android/soong/androidmk/parser"
+)
+
+const (
+ baseUri = "//build/make/core:product_config.rbc"
+ // The name of the struct exported by the product_config.rbc
+ // that contains the functions and variables available to
+ // product configuration Starlark files.
+ baseName = "rblf"
+
+ // And here are the functions and variables:
+ cfnGetCfg = baseName + ".cfg"
+ cfnMain = baseName + ".product_configuration"
+ cfnPrintVars = baseName + ".printvars"
+ cfnWarning = baseName + ".warning"
+ cfnLocalAppend = baseName + ".local_append"
+ cfnLocalSetDefault = baseName + ".local_set_default"
+ cfnInherit = baseName + ".inherit"
+ cfnSetListDefault = baseName + ".setdefault"
+)
+
+const (
+ // Phony makefile functions, they are eventually rewritten
+ // according to knownFunctions map
+ fileExistsPhony = "$file_exists"
+ wildcardExistsPhony = "$wildcard_exists"
+)
+
+const (
+ callLoadAlways = "inherit-product"
+ callLoadIf = "inherit-product-if-exists"
+)
+
+var knownFunctions = map[string]struct {
+ // The name of the runtime function this function call in makefiles maps to.
+ // If it starts with !, then this makefile function call is rewritten to
+ // something else.
+ runtimeName string
+ returnType starlarkType
+}{
+ fileExistsPhony: {baseName + ".file_exists", starlarkTypeBool},
+ wildcardExistsPhony: {baseName + ".file_wildcard_exists", starlarkTypeBool},
+ "add-to-product-copy-files-if-exists": {baseName + ".copy_if_exists", starlarkTypeList},
+ "addprefix": {baseName + ".addprefix", starlarkTypeList},
+ "addsuffix": {baseName + ".addsuffix", starlarkTypeList},
+ "enforce-product-packages-exist": {baseName + ".enforce_product_packages_exist", starlarkTypeVoid},
+ "error": {baseName + ".mkerror", starlarkTypeVoid},
+ "findstring": {"!findstring", starlarkTypeInt},
+ "find-copy-subdir-files": {baseName + ".find_and_copy", starlarkTypeList},
+ "find-word-in-list": {"!find-word-in-list", starlarkTypeUnknown}, // internal macro
+ "filter": {baseName + ".filter", starlarkTypeList},
+ "filter-out": {baseName + ".filter_out", starlarkTypeList},
+ "get-vendor-board-platforms": {"!get-vendor-board-platforms", starlarkTypeList}, // internal macro, used by is-board-platform, etc.
+ "info": {baseName + ".mkinfo", starlarkTypeVoid},
+ "is-android-codename": {"!is-android-codename", starlarkTypeBool}, // unused by product config
+ "is-android-codename-in-list": {"!is-android-codename-in-list", starlarkTypeBool}, // unused by product config
+ "is-board-platform": {"!is-board-platform", starlarkTypeBool},
+ "is-board-platform-in-list": {"!is-board-platform-in-list", starlarkTypeBool},
+ "is-chipset-in-board-platform": {"!is-chipset-in-board-platform", starlarkTypeUnknown}, // unused by product config
+ "is-chipset-prefix-in-board-platform": {"!is-chipset-prefix-in-board-platform", starlarkTypeBool}, // unused by product config
+ "is-not-board-platform": {"!is-not-board-platform", starlarkTypeBool}, // defined but never used
+ "is-platform-sdk-version-at-least": {"!is-platform-sdk-version-at-least", starlarkTypeBool}, // unused by product config
+ "is-product-in-list": {"!is-product-in-list", starlarkTypeBool},
+ "is-vendor-board-platform": {"!is-vendor-board-platform", starlarkTypeBool},
+ callLoadAlways: {"!inherit-product", starlarkTypeVoid},
+ callLoadIf: {"!inherit-product-if-exists", starlarkTypeVoid},
+ "match-prefix": {"!match-prefix", starlarkTypeUnknown}, // internal macro
+ "match-word": {"!match-word", starlarkTypeUnknown}, // internal macro
+ "match-word-in-list": {"!match-word-in-list", starlarkTypeUnknown}, // internal macro
+ "patsubst": {baseName + ".mkpatsubst", starlarkTypeString},
+ "produce_copy_files": {baseName + ".produce_copy_files", starlarkTypeList},
+ "require-artifacts-in-path": {baseName + ".require_artifacts_in_path", starlarkTypeVoid},
+ "require-artifacts-in-path-relaxed": {baseName + ".require_artifacts_in_path_relaxed", starlarkTypeVoid},
+ // TODO(asmundak): remove it once all calls are removed from configuration makefiles. see b/183161002
+ "shell": {baseName + ".shell", starlarkTypeString},
+ "strip": {baseName + ".mkstrip", starlarkTypeString},
+ "tb-modules": {"!tb-modules", starlarkTypeUnknown}, // defined in hardware/amlogic/tb_modules/tb_detect.mk, unused
+ "subst": {baseName + ".mksubst", starlarkTypeString},
+ "warning": {baseName + ".mkwarning", starlarkTypeVoid},
+ "word": {baseName + "!word", starlarkTypeString},
+ "wildcard": {baseName + ".expand_wildcard", starlarkTypeList},
+}
+
+var builtinFuncRex = regexp.MustCompile(
+ "^(addprefix|addsuffix|abspath|and|basename|call|dir|error|eval" +
+ "|flavor|foreach|file|filter|filter-out|findstring|firstword|guile" +
+ "|if|info|join|lastword|notdir|or|origin|patsubst|realpath" +
+ "|shell|sort|strip|subst|suffix|value|warning|word|wordlist|words" +
+ "|wildcard)")
+
+// Conversion request parameters
+type Request struct {
+ MkFile string // file to convert
+ Reader io.Reader // if set, read input from this stream instead
+ RootDir string // root directory path used to resolve included files
+ OutputSuffix string // generated Starlark files suffix
+ OutputDir string // if set, root of the output hierarchy
+ ErrorLogger ErrorMonitorCB
+ TracedVariables []string // trace assignment to these variables
+ TraceCalls bool
+ WarnPartialSuccess bool
+}
+
+// An error sink allowing to gather error statistics.
+// NewError is called on every error encountered during processing.
+type ErrorMonitorCB interface {
+ NewError(s string, node mkparser.Node, args ...interface{})
+}
+
+// Derives module name for a given file. It is base name
+// (file name without suffix), with some characters replaced to make it a Starlark identifier
+func moduleNameForFile(mkFile string) string {
+ base := strings.TrimSuffix(filepath.Base(mkFile), filepath.Ext(mkFile))
+ // TODO(asmundak): what else can be in the product file names?
+ return strings.ReplaceAll(base, "-", "_")
+}
+
+func cloneMakeString(mkString *mkparser.MakeString) *mkparser.MakeString {
+ r := &mkparser.MakeString{StringPos: mkString.StringPos}
+ r.Strings = append(r.Strings, mkString.Strings...)
+ r.Variables = append(r.Variables, mkString.Variables...)
+ return r
+}
+
+func isMakeControlFunc(s string) bool {
+ return s == "error" || s == "warning" || s == "info"
+}
+
+// Starlark output generation context
+type generationContext struct {
+ buf strings.Builder
+ starScript *StarlarkScript
+ indentLevel int
+ inAssignment bool
+ tracedCount int
+}
+
+func NewGenerateContext(ss *StarlarkScript) *generationContext {
+ return &generationContext{starScript: ss}
+}
+
+// emit returns generated script
+func (gctx *generationContext) emit() string {
+ ss := gctx.starScript
+
+ // The emitted code has the following layout:
+ // <initial comments>
+ // preamble, i.e.,
+ // load statement for the runtime support
+ // load statement for each unique submodule pulled in by this one
+ // def init(g, handle):
+ // cfg = rblf.cfg(handle)
+ // <statements>
+ // <warning if conversion was not clean>
+
+ iNode := len(ss.nodes)
+ for i, node := range ss.nodes {
+ if _, ok := node.(*commentNode); !ok {
+ iNode = i
+ break
+ }
+ node.emit(gctx)
+ }
+
+ gctx.emitPreamble()
+
+ gctx.newLine()
+ // The arguments passed to the init function are the global dictionary
+ // ('g') and the product configuration dictionary ('cfg')
+ gctx.write("def init(g, handle):")
+ gctx.indentLevel++
+ if gctx.starScript.traceCalls {
+ gctx.newLine()
+ gctx.writef(`print(">%s")`, gctx.starScript.mkFile)
+ }
+ gctx.newLine()
+ gctx.writef("cfg = %s(handle)", cfnGetCfg)
+ for _, node := range ss.nodes[iNode:] {
+ node.emit(gctx)
+ }
+
+ if ss.hasErrors && ss.warnPartialSuccess {
+ gctx.newLine()
+ gctx.writef("%s(%q, %q)", cfnWarning, filepath.Base(ss.mkFile), "partially successful conversion")
+ }
+ if gctx.starScript.traceCalls {
+ gctx.newLine()
+ gctx.writef(`print("<%s")`, gctx.starScript.mkFile)
+ }
+ gctx.indentLevel--
+ gctx.write("\n")
+ return gctx.buf.String()
+}
+
+func (gctx *generationContext) emitPreamble() {
+ gctx.newLine()
+ gctx.writef("load(%q, %q)", baseUri, baseName)
+ // Emit exactly one load statement for each URI.
+ loadedSubConfigs := make(map[string]string)
+ for _, sc := range gctx.starScript.inherited {
+ uri := sc.path
+ if m, ok := loadedSubConfigs[uri]; ok {
+ // No need to emit load statement, but fix module name.
+ sc.moduleLocalName = m
+ continue
+ }
+ if !sc.loadAlways {
+ uri += "|init"
+ }
+ gctx.newLine()
+ gctx.writef("load(%q, %s = \"init\")", uri, sc.entryName())
+ loadedSubConfigs[uri] = sc.moduleLocalName
+ }
+ gctx.write("\n")
+}
+
+func (gctx *generationContext) emitPass() {
+ gctx.newLine()
+ gctx.write("pass")
+}
+
+func (gctx *generationContext) write(ss ...string) {
+ for _, s := range ss {
+ gctx.buf.WriteString(s)
+ }
+}
+
+func (gctx *generationContext) writef(format string, args ...interface{}) {
+ gctx.write(fmt.Sprintf(format, args...))
+}
+
+func (gctx *generationContext) newLine() {
+ if gctx.buf.Len() == 0 {
+ return
+ }
+ gctx.write("\n")
+ gctx.writef("%*s", 2*gctx.indentLevel, "")
+}
+
+type knownVariable struct {
+ name string
+ class varClass
+ valueType starlarkType
+}
+
+type knownVariables map[string]knownVariable
+
+func (pcv knownVariables) NewVariable(name string, varClass varClass, valueType starlarkType) {
+ v, exists := pcv[name]
+ if !exists {
+ pcv[name] = knownVariable{name, varClass, valueType}
+ return
+ }
+ // Conflict resolution:
+ // * config class trumps everything
+ // * any type trumps unknown type
+ match := varClass == v.class
+ if !match {
+ if varClass == VarClassConfig {
+ v.class = VarClassConfig
+ match = true
+ } else if v.class == VarClassConfig {
+ match = true
+ }
+ }
+ if valueType != v.valueType {
+ if valueType != starlarkTypeUnknown {
+ if v.valueType == starlarkTypeUnknown {
+ v.valueType = valueType
+ } else {
+ match = false
+ }
+ }
+ }
+ if !match {
+ fmt.Fprintf(os.Stderr, "cannot redefine %s as %v/%v (already defined as %v/%v)\n",
+ name, varClass, valueType, v.class, v.valueType)
+ }
+}
+
+// All known product variables.
+var KnownVariables = make(knownVariables)
+
+func init() {
+ for _, kv := range []string{
+ // Kernel-related variables that we know are lists.
+ "BOARD_VENDOR_KERNEL_MODULES",
+ "BOARD_VENDOR_RAMDISK_KERNEL_MODULES",
+ "BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD",
+ "BOARD_RECOVERY_KERNEL_MODULES",
+ // Other variables we knwo are lists
+ "ART_APEX_JARS",
+ } {
+ KnownVariables.NewVariable(kv, VarClassSoong, starlarkTypeList)
+ }
+}
+
+type nodeReceiver interface {
+ newNode(node starlarkNode)
+}
+
+// Information about the generated Starlark script.
+type StarlarkScript struct {
+ mkFile string
+ moduleName string
+ mkPos scanner.Position
+ nodes []starlarkNode
+ inherited []*inheritedModule
+ hasErrors bool
+ topDir string
+ traceCalls bool // print enter/exit each init function
+ warnPartialSuccess bool
+}
+
+func (ss *StarlarkScript) newNode(node starlarkNode) {
+ ss.nodes = append(ss.nodes, node)
+}
+
+// varAssignmentScope points to the last assignment for each variable
+// in the current block. It is used during the parsing to chain
+// the assignments to a variable together.
+type varAssignmentScope struct {
+ outer *varAssignmentScope
+ vars map[string]*assignmentNode
+}
+
+// parseContext holds the script we are generating and all the ephemeral data
+// needed during the parsing.
+type parseContext struct {
+ script *StarlarkScript
+ nodes []mkparser.Node // Makefile as parsed by mkparser
+ currentNodeIndex int // Node in it we are processing
+ ifNestLevel int
+ moduleNameCount map[string]int // count of imported modules with given basename
+ fatalError error
+ builtinMakeVars map[string]starlarkExpr
+ outputSuffix string
+ errorLogger ErrorMonitorCB
+ tracedVariables map[string]bool // variables to be traced in the generated script
+ variables map[string]variable
+ varAssignments *varAssignmentScope
+ receiver nodeReceiver // receptacle for the generated starlarkNode's
+ receiverStack []nodeReceiver
+ outputDir string
+}
+
+func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext {
+ predefined := []struct{ name, value string }{
+ {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")},
+ {"LOCAL_PATH", filepath.Dir(ss.mkFile)},
+ {"TOPDIR", ss.topDir},
+ // TODO(asmundak): maybe read it from build/make/core/envsetup.mk?
+ {"TARGET_COPY_OUT_SYSTEM", "system"},
+ {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"},
+ {"TARGET_COPY_OUT_DATA", "data"},
+ {"TARGET_COPY_OUT_ASAN", filepath.Join("data", "asan")},
+ {"TARGET_COPY_OUT_OEM", "oem"},
+ {"TARGET_COPY_OUT_RAMDISK", "ramdisk"},
+ {"TARGET_COPY_OUT_DEBUG_RAMDISK", "debug_ramdisk"},
+ {"TARGET_COPY_OUT_VENDOR_DEBUG_RAMDISK", "vendor_debug_ramdisk"},
+ {"TARGET_COPY_OUT_TEST_HARNESS_RAMDISK", "test_harness_ramdisk"},
+ {"TARGET_COPY_OUT_ROOT", "root"},
+ {"TARGET_COPY_OUT_RECOVERY", "recovery"},
+ {"TARGET_COPY_OUT_VENDOR", "||VENDOR-PATH-PH||"},
+ {"TARGET_COPY_OUT_VENDOR_RAMDISK", "vendor_ramdisk"},
+ {"TARGET_COPY_OUT_PRODUCT", "||PRODUCT-PATH-PH||"},
+ {"TARGET_COPY_OUT_PRODUCT_SERVICES", "||PRODUCT-PATH-PH||"},
+ {"TARGET_COPY_OUT_SYSTEM_EXT", "||SYSTEM_EXT-PATH-PH||"},
+ {"TARGET_COPY_OUT_ODM", "||ODM-PATH-PH||"},
+ {"TARGET_COPY_OUT_VENDOR_DLKM", "||VENDOR_DLKM-PATH-PH||"},
+ {"TARGET_COPY_OUT_ODM_DLKM", "||ODM_DLKM-PATH-PH||"},
+ // TODO(asmundak): to process internal config files, we need the following variables:
+ // BOARD_CONFIG_VENDOR_PATH
+ // TARGET_VENDOR
+ // target_base_product
+ //
+
+ // the following utility variables are set in build/make/common/core.mk:
+ {"empty", ""},
+ {"space", " "},
+ {"comma", ","},
+ {"newline", "\n"},
+ {"pound", "#"},
+ {"backslash", "\\"},
+ }
+ ctx := &parseContext{
+ script: ss,
+ nodes: nodes,
+ currentNodeIndex: 0,
+ ifNestLevel: 0,
+ moduleNameCount: make(map[string]int),
+ builtinMakeVars: map[string]starlarkExpr{},
+ variables: make(map[string]variable),
+ }
+ ctx.pushVarAssignments()
+ for _, item := range predefined {
+ ctx.variables[item.name] = &predefinedVariable{
+ baseVariable: baseVariable{nam: item.name, typ: starlarkTypeString},
+ value: &stringLiteralExpr{item.value},
+ }
+ }
+
+ return ctx
+}
+
+func (ctx *parseContext) lastAssignment(name string) *assignmentNode {
+ for va := ctx.varAssignments; va != nil; va = va.outer {
+ if v, ok := va.vars[name]; ok {
+ return v
+ }
+ }
+ return nil
+}
+
+func (ctx *parseContext) setLastAssignment(name string, asgn *assignmentNode) {
+ ctx.varAssignments.vars[name] = asgn
+}
+
+func (ctx *parseContext) pushVarAssignments() {
+ va := &varAssignmentScope{
+ outer: ctx.varAssignments,
+ vars: make(map[string]*assignmentNode),
+ }
+ ctx.varAssignments = va
+}
+
+func (ctx *parseContext) popVarAssignments() {
+ ctx.varAssignments = ctx.varAssignments.outer
+}
+
+func (ctx *parseContext) pushReceiver(rcv nodeReceiver) {
+ ctx.receiverStack = append(ctx.receiverStack, ctx.receiver)
+ ctx.receiver = rcv
+}
+
+func (ctx *parseContext) popReceiver() {
+ last := len(ctx.receiverStack) - 1
+ if last < 0 {
+ panic(fmt.Errorf("popReceiver: receiver stack empty"))
+ }
+ ctx.receiver = ctx.receiverStack[last]
+ ctx.receiverStack = ctx.receiverStack[0:last]
+}
+
+func (ctx *parseContext) hasNodes() bool {
+ return ctx.currentNodeIndex < len(ctx.nodes)
+}
+
+func (ctx *parseContext) getNode() mkparser.Node {
+ if !ctx.hasNodes() {
+ return nil
+ }
+ node := ctx.nodes[ctx.currentNodeIndex]
+ ctx.currentNodeIndex++
+ return node
+}
+
+func (ctx *parseContext) backNode() {
+ if ctx.currentNodeIndex <= 0 {
+ panic("Cannot back off")
+ }
+ ctx.currentNodeIndex--
+}
+
+func (ctx *parseContext) handleAssignment(a *mkparser.Assignment) {
+ // Handle only simple variables
+ if !a.Name.Const() {
+ ctx.errorf(a, "Only simple variables are handled")
+ return
+ }
+ name := a.Name.Strings[0]
+ lhs := ctx.addVariable(name)
+ if lhs == nil {
+ ctx.errorf(a, "unknown variable %s", name)
+ return
+ }
+ _, isTraced := ctx.tracedVariables[name]
+ asgn := &assignmentNode{lhs: lhs, mkValue: a.Value, isTraced: isTraced}
+ if lhs.valueType() == starlarkTypeUnknown {
+ // Try to divine variable type from the RHS
+ asgn.value = ctx.parseMakeString(a, a.Value)
+ if xBad, ok := asgn.value.(*badExpr); ok {
+ ctx.wrapBadExpr(xBad)
+ return
+ }
+ inferred_type := asgn.value.typ()
+ if inferred_type != starlarkTypeUnknown {
+ lhs.setValueType(inferred_type)
+ }
+ }
+ if lhs.valueType() == starlarkTypeList {
+ xConcat := ctx.buildConcatExpr(a)
+ if xConcat == nil {
+ return
+ }
+ switch len(xConcat.items) {
+ case 0:
+ asgn.value = &listExpr{}
+ case 1:
+ asgn.value = xConcat.items[0]
+ default:
+ asgn.value = xConcat
+ }
+ } else {
+ asgn.value = ctx.parseMakeString(a, a.Value)
+ if xBad, ok := asgn.value.(*badExpr); ok {
+ ctx.wrapBadExpr(xBad)
+ return
+ }
+ }
+
+ // TODO(asmundak): move evaluation to a separate pass
+ asgn.value, _ = asgn.value.eval(ctx.builtinMakeVars)
+
+ asgn.previous = ctx.lastAssignment(name)
+ ctx.setLastAssignment(name, asgn)
+ switch a.Type {
+ case "=", ":=":
+ asgn.flavor = asgnSet
+ case "+=":
+ if asgn.previous == nil && !asgn.lhs.isPreset() {
+ asgn.flavor = asgnMaybeAppend
+ } else {
+ asgn.flavor = asgnAppend
+ }
+ case "?=":
+ asgn.flavor = asgnMaybeSet
+ default:
+ panic(fmt.Errorf("unexpected assignment type %s", a.Type))
+ }
+
+ ctx.receiver.newNode(asgn)
+}
+
+func (ctx *parseContext) buildConcatExpr(a *mkparser.Assignment) *concatExpr {
+ xConcat := &concatExpr{}
+ var xItemList *listExpr
+ addToItemList := func(x ...starlarkExpr) {
+ if xItemList == nil {
+ xItemList = &listExpr{[]starlarkExpr{}}
+ }
+ xItemList.items = append(xItemList.items, x...)
+ }
+ finishItemList := func() {
+ if xItemList != nil {
+ xConcat.items = append(xConcat.items, xItemList)
+ xItemList = nil
+ }
+ }
+
+ items := a.Value.Words()
+ for _, item := range items {
+ // A function call in RHS is supposed to return a list, all other item
+ // expressions return individual elements.
+ switch x := ctx.parseMakeString(a, item).(type) {
+ case *badExpr:
+ ctx.wrapBadExpr(x)
+ return nil
+ case *stringLiteralExpr:
+ addToItemList(maybeConvertToStringList(x).(*listExpr).items...)
+ default:
+ switch x.typ() {
+ case starlarkTypeList:
+ finishItemList()
+ xConcat.items = append(xConcat.items, x)
+ case starlarkTypeString:
+ finishItemList()
+ xConcat.items = append(xConcat.items, &callExpr{
+ object: x,
+ name: "split",
+ args: nil,
+ returnType: starlarkTypeList,
+ })
+ default:
+ addToItemList(x)
+ }
+ }
+ }
+ if xItemList != nil {
+ xConcat.items = append(xConcat.items, xItemList)
+ }
+ return xConcat
+}
+
+func (ctx *parseContext) newInheritedModule(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) *inheritedModule {
+ var path string
+ x, _ := pathExpr.eval(ctx.builtinMakeVars)
+ s, ok := x.(*stringLiteralExpr)
+ if !ok {
+ ctx.errorf(v, "inherit-product/include argument is too complex")
+ return nil
+ }
+
+ path = s.literal
+ moduleName := moduleNameForFile(path)
+ moduleLocalName := "_" + moduleName
+ n, found := ctx.moduleNameCount[moduleName]
+ if found {
+ moduleLocalName += fmt.Sprintf("%d", n)
+ }
+ ctx.moduleNameCount[moduleName] = n + 1
+ ln := &inheritedModule{
+ path: ctx.loadedModulePath(path),
+ originalPath: path,
+ moduleName: moduleName,
+ moduleLocalName: moduleLocalName,
+ loadAlways: loadAlways,
+ }
+ ctx.script.inherited = append(ctx.script.inherited, ln)
+ return ln
+}
+
+func (ctx *parseContext) handleInheritModule(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) {
+ if im := ctx.newInheritedModule(v, pathExpr, loadAlways); im != nil {
+ ctx.receiver.newNode(&inheritNode{im})
+ }
+}
+
+func (ctx *parseContext) handleInclude(v mkparser.Node, pathExpr starlarkExpr, loadAlways bool) {
+ if ln := ctx.newInheritedModule(v, pathExpr, loadAlways); ln != nil {
+ ctx.receiver.newNode(&includeNode{ln})
+ }
+}
+
+func (ctx *parseContext) handleVariable(v *mkparser.Variable) {
+ // Handle:
+ // $(call inherit-product,...)
+ // $(call inherit-product-if-exists,...)
+ // $(info xxx)
+ // $(warning xxx)
+ // $(error xxx)
+ expr := ctx.parseReference(v, v.Name)
+ switch x := expr.(type) {
+ case *callExpr:
+ if x.name == callLoadAlways || x.name == callLoadIf {
+ ctx.handleInheritModule(v, x.args[0], x.name == callLoadAlways)
+ } else if isMakeControlFunc(x.name) {
+ // File name is the first argument
+ args := []starlarkExpr{
+ &stringLiteralExpr{ctx.script.mkFile},
+ x.args[0],
+ }
+ ctx.receiver.newNode(&exprNode{
+ &callExpr{name: x.name, args: args, returnType: starlarkTypeUnknown},
+ })
+ } else {
+ ctx.receiver.newNode(&exprNode{expr})
+ }
+ case *badExpr:
+ ctx.wrapBadExpr(x)
+ return
+ default:
+ ctx.errorf(v, "cannot handle %s", v.Dump())
+ return
+ }
+}
+
+func (ctx *parseContext) handleDefine(directive *mkparser.Directive) {
+ macro_name := strings.Fields(directive.Args.Strings[0])[0]
+ // Ignore the macros that we handle
+ if _, ok := knownFunctions[macro_name]; !ok {
+ ctx.errorf(directive, "define is not supported: %s", macro_name)
+ }
+}
+
+func (ctx *parseContext) handleIfBlock(ifDirective *mkparser.Directive) {
+ ssSwitch := &switchNode{}
+ ctx.pushReceiver(ssSwitch)
+ for ctx.processBranch(ifDirective); ctx.hasNodes() && ctx.fatalError == nil; {
+ node := ctx.getNode()
+ switch x := node.(type) {
+ case *mkparser.Directive:
+ switch x.Name {
+ case "else", "elifdef", "elifndef", "elifeq", "elifneq":
+ ctx.processBranch(x)
+ case "endif":
+ ctx.popReceiver()
+ ctx.receiver.newNode(ssSwitch)
+ return
+ default:
+ ctx.errorf(node, "unexpected directive %s", x.Name)
+ }
+ default:
+ ctx.errorf(ifDirective, "unexpected statement")
+ }
+ }
+ if ctx.fatalError == nil {
+ ctx.fatalError = fmt.Errorf("no matching endif for %s", ifDirective.Dump())
+ }
+ ctx.popReceiver()
+}
+
+// processBranch processes a single branch (if/elseif/else) until the next directive
+// on the same level.
+func (ctx *parseContext) processBranch(check *mkparser.Directive) {
+ block := switchCase{gate: ctx.parseCondition(check)}
+ defer func() {
+ ctx.popVarAssignments()
+ ctx.ifNestLevel--
+
+ }()
+ ctx.pushVarAssignments()
+ ctx.ifNestLevel++
+
+ ctx.pushReceiver(&block)
+ for ctx.hasNodes() {
+ node := ctx.getNode()
+ if ctx.handleSimpleStatement(node) {
+ continue
+ }
+ switch d := node.(type) {
+ case *mkparser.Directive:
+ switch d.Name {
+ case "else", "elifdef", "elifndef", "elifeq", "elifneq", "endif":
+ ctx.popReceiver()
+ ctx.receiver.newNode(&block)
+ ctx.backNode()
+ return
+ case "ifdef", "ifndef", "ifeq", "ifneq":
+ ctx.handleIfBlock(d)
+ default:
+ ctx.errorf(d, "unexpected directive %s", d.Name)
+ }
+ default:
+ ctx.errorf(node, "unexpected statement")
+ }
+ }
+ ctx.fatalError = fmt.Errorf("no matching endif for %s", check.Dump())
+ ctx.popReceiver()
+}
+
+func (ctx *parseContext) newIfDefinedNode(check *mkparser.Directive) (starlarkExpr, bool) {
+ if !check.Args.Const() {
+ return ctx.newBadExpr(check, "ifdef variable ref too complex: %s", check.Args.Dump()), false
+ }
+ v := ctx.addVariable(check.Args.Strings[0])
+ return &variableDefinedExpr{v}, true
+}
+
+func (ctx *parseContext) parseCondition(check *mkparser.Directive) starlarkNode {
+ switch check.Name {
+ case "ifdef", "ifndef", "elifdef", "elifndef":
+ v, ok := ctx.newIfDefinedNode(check)
+ if ok && strings.HasSuffix(check.Name, "ndef") {
+ v = ¬Expr{v}
+ }
+ return &ifNode{
+ isElif: strings.HasPrefix(check.Name, "elif"),
+ expr: v,
+ }
+ case "ifeq", "ifneq", "elifeq", "elifneq":
+ return &ifNode{
+ isElif: strings.HasPrefix(check.Name, "elif"),
+ expr: ctx.parseCompare(check),
+ }
+ case "else":
+ return &elseNode{}
+ default:
+ panic(fmt.Errorf("%s: unknown directive: %s", ctx.script.mkFile, check.Dump()))
+ }
+}
+
+func (ctx *parseContext) newBadExpr(node mkparser.Node, text string, args ...interface{}) starlarkExpr {
+ message := fmt.Sprintf(text, args...)
+ if ctx.errorLogger != nil {
+ ctx.errorLogger.NewError(text, node, args)
+ }
+ ctx.script.hasErrors = true
+ return &badExpr{node, message}
+}
+
+func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr {
+ // Strip outer parentheses
+ mkArg := cloneMakeString(cond.Args)
+ mkArg.Strings[0] = strings.TrimLeft(mkArg.Strings[0], "( ")
+ n := len(mkArg.Strings)
+ mkArg.Strings[n-1] = strings.TrimRight(mkArg.Strings[n-1], ") ")
+ args := mkArg.Split(",")
+ // TODO(asmundak): handle the case where the arguments are in quotes and space-separated
+ if len(args) != 2 {
+ return ctx.newBadExpr(cond, "ifeq/ifneq len(args) != 2 %s", cond.Dump())
+ }
+ args[0].TrimRightSpaces()
+ args[1].TrimLeftSpaces()
+
+ isEq := !strings.HasSuffix(cond.Name, "neq")
+ switch xLeft := ctx.parseMakeString(cond, args[0]).(type) {
+ case *stringLiteralExpr, *variableRefExpr:
+ switch xRight := ctx.parseMakeString(cond, args[1]).(type) {
+ case *stringLiteralExpr, *variableRefExpr:
+ return &eqExpr{left: xLeft, right: xRight, isEq: isEq}
+ case *badExpr:
+ return xRight
+ default:
+ expr, ok := ctx.parseCheckFunctionCallResult(cond, xLeft, args[1])
+ if ok {
+ return expr
+ }
+ return ctx.newBadExpr(cond, "right operand is too complex: %s", args[1].Dump())
+ }
+ case *badExpr:
+ return xLeft
+ default:
+ switch xRight := ctx.parseMakeString(cond, args[1]).(type) {
+ case *stringLiteralExpr, *variableRefExpr:
+ expr, ok := ctx.parseCheckFunctionCallResult(cond, xRight, args[0])
+ if ok {
+ return expr
+ }
+ return ctx.newBadExpr(cond, "left operand is too complex: %s", args[0].Dump())
+ case *badExpr:
+ return xRight
+ default:
+ return ctx.newBadExpr(cond, "operands are too complex: (%s,%s)", args[0].Dump(), args[1].Dump())
+ }
+ }
+}
+
+func (ctx *parseContext) parseCheckFunctionCallResult(directive *mkparser.Directive, xValue starlarkExpr,
+ varArg *mkparser.MakeString) (starlarkExpr, bool) {
+ mkSingleVar, ok := varArg.SingleVariable()
+ if !ok {
+ return nil, false
+ }
+ expr := ctx.parseReference(directive, mkSingleVar)
+ negate := strings.HasSuffix(directive.Name, "neq")
+ checkIsSomethingFunction := func(xCall *callExpr) starlarkExpr {
+ s, ok := maybeString(xValue)
+ if !ok || s != "true" {
+ return ctx.newBadExpr(directive,
+ fmt.Sprintf("the result of %s can be compared only to 'true'", xCall.name))
+ }
+ if len(xCall.args) < 1 {
+ return ctx.newBadExpr(directive, "%s requires an argument", xCall.name)
+ }
+ return nil
+ }
+ switch x := expr.(type) {
+ case *callExpr:
+ switch x.name {
+ case "filter":
+ return ctx.parseCompareFilterFuncResult(directive, x, xValue, !negate), true
+ case "filter-out":
+ return ctx.parseCompareFilterFuncResult(directive, x, xValue, negate), true
+ case "wildcard":
+ return ctx.parseCompareWildcardFuncResult(directive, x, xValue, negate), true
+ case "findstring":
+ return ctx.parseCheckFindstringFuncResult(directive, x, xValue, negate), true
+ case "strip":
+ return ctx.parseCompareStripFuncResult(directive, x, xValue, negate), true
+ case "is-board-platform":
+ if xBad := checkIsSomethingFunction(x); xBad != nil {
+ return xBad, true
+ }
+ return &eqExpr{
+ left: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false},
+ right: x.args[0],
+ isEq: !negate,
+ }, true
+ case "is-board-platform-in-list":
+ if xBad := checkIsSomethingFunction(x); xBad != nil {
+ return xBad, true
+ }
+ return &inExpr{
+ expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false},
+ list: maybeConvertToStringList(x.args[0]),
+ isNot: negate,
+ }, true
+ case "is-product-in-list":
+ if xBad := checkIsSomethingFunction(x); xBad != nil {
+ return xBad, true
+ }
+ return &inExpr{
+ expr: &variableRefExpr{ctx.addVariable("TARGET_PRODUCT"), true},
+ list: maybeConvertToStringList(x.args[0]),
+ isNot: negate,
+ }, true
+ case "is-vendor-board-platform":
+ if xBad := checkIsSomethingFunction(x); xBad != nil {
+ return xBad, true
+ }
+ s, ok := maybeString(x.args[0])
+ if !ok {
+ return ctx.newBadExpr(directive, "cannot handle non-constant argument to is-vendor-board-platform"), true
+ }
+ return &inExpr{
+ expr: &variableRefExpr{ctx.addVariable("TARGET_BOARD_PLATFORM"), false},
+ list: &variableRefExpr{ctx.addVariable(s + "_BOARD_PLATFORMS"), true},
+ isNot: negate,
+ }, true
+ default:
+ return ctx.newBadExpr(directive, "Unknown function in ifeq: %s", x.name), true
+ }
+ case *badExpr:
+ return x, true
+ default:
+ return nil, false
+ }
+}
+
+func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive,
+ filterFuncCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
+ // We handle:
+ // * ifeq/ifneq (,$(filter v1 v2 ..., $(VAR)) becomes if VAR not in/in ["v1", "v2", ...]
+ // * ifeq/ifneq (,$(filter $(VAR), v1 v2 ...) becomes if VAR not in/in ["v1", "v2", ...]
+ // * ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...) becomes if VAR in/not in ["v1", "v2"]
+ // TODO(Asmundak): check the last case works for filter-out, too.
+ xPattern := filterFuncCall.args[0]
+ xText := filterFuncCall.args[1]
+ var xInList *stringLiteralExpr
+ var xVar starlarkExpr
+ var ok bool
+ switch x := xValue.(type) {
+ case *stringLiteralExpr:
+ if x.literal != "" {
+ return ctx.newBadExpr(cond, "filter comparison to non-empty value: %s", xValue)
+ }
+ // Either pattern or text should be const, and the
+ // non-const one should be varRefExpr
+ if xInList, ok = xPattern.(*stringLiteralExpr); ok {
+ xVar = xText
+ } else if xInList, ok = xText.(*stringLiteralExpr); ok {
+ xVar = xPattern
+ }
+ case *variableRefExpr:
+ if v, ok := xPattern.(*variableRefExpr); ok {
+ if xInList, ok = xText.(*stringLiteralExpr); ok && v.ref.name() == x.ref.name() {
+ // ifeq/ifneq ($(VAR),$(filter $(VAR), v1 v2 ...), flip negate,
+ // it's the opposite to what is done when comparing to empty.
+ xVar = xPattern
+ negate = !negate
+ }
+ }
+ }
+ if xVar != nil && xInList != nil {
+ if _, ok := xVar.(*variableRefExpr); ok {
+ slExpr := newStringListExpr(strings.Fields(xInList.literal))
+ // Generate simpler code for the common cases:
+ if xVar.typ() == starlarkTypeList {
+ if len(slExpr.items) == 1 {
+ // Checking that a string belongs to list
+ return &inExpr{isNot: negate, list: xVar, expr: slExpr.items[0]}
+ } else {
+ // TODO(asmundak):
+ panic("TBD")
+ }
+ }
+ return &inExpr{isNot: negate, list: newStringListExpr(strings.Fields(xInList.literal)), expr: xVar}
+ }
+ }
+ return ctx.newBadExpr(cond, "filter arguments are too complex: %s", cond.Dump())
+}
+
+func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive,
+ xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
+ if x, ok := xValue.(*stringLiteralExpr); !ok || x.literal != "" {
+ return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue)
+ }
+ callFunc := wildcardExistsPhony
+ if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") {
+ callFunc = fileExistsPhony
+ }
+ var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool}
+ if !negate {
+ cc = ¬Expr{cc}
+ }
+ return cc
+}
+
+func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive,
+ xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
+ if x, ok := xValue.(*stringLiteralExpr); !ok || x.literal != "" {
+ return ctx.newBadExpr(directive, "findstring result can be compared only to empty: %s", xValue)
+ }
+ return &eqExpr{
+ left: &callExpr{
+ object: xCall.args[1],
+ name: "find",
+ args: []starlarkExpr{xCall.args[0]},
+ returnType: starlarkTypeInt,
+ },
+ right: &intLiteralExpr{-1},
+ isEq: !negate,
+ }
+}
+
+func (ctx *parseContext) parseCompareStripFuncResult(directive *mkparser.Directive,
+ xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
+ if _, ok := xValue.(*stringLiteralExpr); !ok {
+ return ctx.newBadExpr(directive, "strip result can be compared only to string: %s", xValue)
+ }
+ return &eqExpr{
+ left: &callExpr{
+ name: "strip",
+ args: xCall.args,
+ returnType: starlarkTypeString,
+ },
+ right: xValue, isEq: !negate}
+}
+
+// parses $(...), returning an expression
+func (ctx *parseContext) parseReference(node mkparser.Node, ref *mkparser.MakeString) starlarkExpr {
+ ref.TrimLeftSpaces()
+ ref.TrimRightSpaces()
+ refDump := ref.Dump()
+
+ // Handle only the case where the first (or only) word is constant
+ words := ref.SplitN(" ", 2)
+ if !words[0].Const() {
+ return ctx.newBadExpr(node, "reference is too complex: %s", refDump)
+ }
+
+ // If it is a single word, it can be a simple variable
+ // reference or a function call
+ if len(words) == 1 {
+ if isMakeControlFunc(refDump) || refDump == "shell" {
+ return &callExpr{
+ name: refDump,
+ args: []starlarkExpr{&stringLiteralExpr{""}},
+ returnType: starlarkTypeUnknown,
+ }
+ }
+ if v := ctx.addVariable(refDump); v != nil {
+ return &variableRefExpr{v, ctx.lastAssignment(v.name()) != nil}
+ }
+ return ctx.newBadExpr(node, "unknown variable %s", refDump)
+ }
+
+ expr := &callExpr{name: words[0].Dump(), returnType: starlarkTypeUnknown}
+ args := words[1]
+ args.TrimLeftSpaces()
+ // Make control functions and shell need special treatment as everything
+ // after the name is a single text argument
+ if isMakeControlFunc(expr.name) || expr.name == "shell" {
+ x := ctx.parseMakeString(node, args)
+ if xBad, ok := x.(*badExpr); ok {
+ return xBad
+ }
+ expr.args = []starlarkExpr{x}
+ return expr
+ }
+ if expr.name == "call" {
+ words = args.SplitN(",", 2)
+ if words[0].Empty() || !words[0].Const() {
+ return ctx.newBadExpr(node, "cannot handle %s", refDump)
+ }
+ expr.name = words[0].Dump()
+ if len(words) < 2 {
+ return expr
+ }
+ args = words[1]
+ }
+ if kf, found := knownFunctions[expr.name]; found {
+ expr.returnType = kf.returnType
+ } else {
+ return ctx.newBadExpr(node, "cannot handle invoking %s", expr.name)
+ }
+ switch expr.name {
+ case "word":
+ return ctx.parseWordFunc(node, args)
+ case "subst", "patsubst":
+ return ctx.parseSubstFunc(node, expr.name, args)
+ default:
+ for _, arg := range args.Split(",") {
+ arg.TrimLeftSpaces()
+ arg.TrimRightSpaces()
+ x := ctx.parseMakeString(node, arg)
+ if xBad, ok := x.(*badExpr); ok {
+ return xBad
+ }
+ expr.args = append(expr.args, x)
+ }
+ }
+ return expr
+}
+
+func (ctx *parseContext) parseSubstFunc(node mkparser.Node, fname string, args *mkparser.MakeString) starlarkExpr {
+ words := args.Split(",")
+ if len(words) != 3 {
+ return ctx.newBadExpr(node, "%s function should have 3 arguments", fname)
+ }
+ if !words[0].Const() || !words[1].Const() {
+ return ctx.newBadExpr(node, "%s function's from and to arguments should be constant", fname)
+ }
+ from := words[0].Strings[0]
+ to := words[1].Strings[0]
+ words[2].TrimLeftSpaces()
+ words[2].TrimRightSpaces()
+ obj := ctx.parseMakeString(node, words[2])
+ typ := obj.typ()
+ if typ == starlarkTypeString && fname == "subst" {
+ // Optimization: if it's $(subst from, to, string), emit string.replace(from, to)
+ return &callExpr{
+ object: obj,
+ name: "replace",
+ args: []starlarkExpr{&stringLiteralExpr{from}, &stringLiteralExpr{to}},
+ returnType: typ,
+ }
+ }
+ return &callExpr{
+ name: fname,
+ args: []starlarkExpr{&stringLiteralExpr{from}, &stringLiteralExpr{to}, obj},
+ returnType: obj.typ(),
+ }
+}
+
+func (ctx *parseContext) parseWordFunc(node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
+ words := args.Split(",")
+ if len(words) != 2 {
+ return ctx.newBadExpr(node, "word function should have 2 arguments")
+ }
+ var index uint64 = 0
+ if words[0].Const() {
+ index, _ = strconv.ParseUint(strings.TrimSpace(words[0].Strings[0]), 10, 64)
+ }
+ if index < 1 {
+ return ctx.newBadExpr(node, "word index should be constant positive integer")
+ }
+ words[1].TrimLeftSpaces()
+ words[1].TrimRightSpaces()
+ array := ctx.parseMakeString(node, words[1])
+ if xBad, ok := array.(*badExpr); ok {
+ return xBad
+ }
+ if array.typ() != starlarkTypeList {
+ array = &callExpr{object: array, name: "split", returnType: starlarkTypeList}
+ }
+ return indexExpr{array, &intLiteralExpr{int(index - 1)}}
+}
+
+func (ctx *parseContext) parseMakeString(node mkparser.Node, mk *mkparser.MakeString) starlarkExpr {
+ if mk.Const() {
+ return &stringLiteralExpr{mk.Dump()}
+ }
+ if mkRef, ok := mk.SingleVariable(); ok {
+ return ctx.parseReference(node, mkRef)
+ }
+ // If we reached here, it's neither string literal nor a simple variable,
+ // we need a full-blown interpolation node that will generate
+ // "a%b%c" % (X, Y) for a$(X)b$(Y)c
+ xInterp := &interpolateExpr{args: make([]starlarkExpr, len(mk.Variables))}
+ for i, ref := range mk.Variables {
+ arg := ctx.parseReference(node, ref.Name)
+ if x, ok := arg.(*badExpr); ok {
+ return x
+ }
+ xInterp.args[i] = arg
+ }
+ xInterp.chunks = append(xInterp.chunks, mk.Strings...)
+ return xInterp
+}
+
+// Handles the statements whose treatment is the same in all contexts: comment,
+// assignment, variable (which is a macro call in reality) and all constructs that
+// do not handle in any context ('define directive and any unrecognized stuff).
+// Return true if we handled it.
+func (ctx *parseContext) handleSimpleStatement(node mkparser.Node) bool {
+ handled := true
+ switch x := node.(type) {
+ case *mkparser.Comment:
+ ctx.insertComment("#" + x.Comment)
+ case *mkparser.Assignment:
+ ctx.handleAssignment(x)
+ case *mkparser.Variable:
+ ctx.handleVariable(x)
+ case *mkparser.Directive:
+ switch x.Name {
+ case "define":
+ ctx.handleDefine(x)
+ case "include", "-include":
+ ctx.handleInclude(node, ctx.parseMakeString(node, x.Args), x.Name[0] != '-')
+ default:
+ handled = false
+ }
+ default:
+ ctx.errorf(x, "unsupported line %s", x.Dump())
+ }
+ return handled
+}
+
+func (ctx *parseContext) insertComment(s string) {
+ ctx.receiver.newNode(&commentNode{strings.TrimSpace(s)})
+}
+
+func (ctx *parseContext) carryAsComment(failedNode mkparser.Node) {
+ for _, line := range strings.Split(failedNode.Dump(), "\n") {
+ ctx.insertComment("# " + line)
+ }
+}
+
+// records that the given node failed to be converted and includes an explanatory message
+func (ctx *parseContext) errorf(failedNode mkparser.Node, message string, args ...interface{}) {
+ if ctx.errorLogger != nil {
+ ctx.errorLogger.NewError(message, failedNode, args...)
+ }
+ message = fmt.Sprintf(message, args...)
+ ctx.insertComment(fmt.Sprintf("# MK2RBC TRANSLATION ERROR: %s", message))
+ ctx.carryAsComment(failedNode)
+ ctx.script.hasErrors = true
+}
+
+func (ctx *parseContext) wrapBadExpr(xBad *badExpr) {
+ ctx.insertComment(fmt.Sprintf("# MK2RBC TRANSLATION ERROR: %s", xBad.message))
+ ctx.carryAsComment(xBad.node)
+}
+
+func (ctx *parseContext) loadedModulePath(path string) string {
+ // During the transition to Roboleaf some of the product configuration files
+ // will be converted and checked in while the others will be generated on the fly
+ // and run. The runner (rbcrun application) accommodates this by allowing three
+ // different ways to specify the loaded file location:
+ // 1) load(":<file>",...) loads <file> from the same directory
+ // 2) load("//path/relative/to/source/root:<file>", ...) loads <file> source tree
+ // 3) load("/absolute/path/to/<file> absolute path
+ // If the file being generated and the file it wants to load are in the same directory,
+ // generate option 1.
+ // Otherwise, if output directory is not specified, generate 2)
+ // Finally, if output directory has been specified and the file being generated and
+ // the file it wants to load from are in the different directories, generate 2) or 3):
+ // * if the file being loaded exists in the source tree, generate 2)
+ // * otherwise, generate 3)
+ // Finally, figure out the loaded module path and name and create a node for it
+ loadedModuleDir := filepath.Dir(path)
+ base := filepath.Base(path)
+ loadedModuleName := strings.TrimSuffix(base, filepath.Ext(base)) + ctx.outputSuffix
+ if loadedModuleDir == filepath.Dir(ctx.script.mkFile) {
+ return ":" + loadedModuleName
+ }
+ if ctx.outputDir == "" {
+ return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName)
+ }
+ if _, err := os.Stat(filepath.Join(loadedModuleDir, loadedModuleName)); err == nil {
+ return fmt.Sprintf("//%s:%s", loadedModuleDir, loadedModuleName)
+ }
+ return filepath.Join(ctx.outputDir, loadedModuleDir, loadedModuleName)
+}
+
+func (ss *StarlarkScript) String() string {
+ return NewGenerateContext(ss).emit()
+}
+
+func (ss *StarlarkScript) SubConfigFiles() []string {
+ var subs []string
+ for _, src := range ss.inherited {
+ subs = append(subs, src.originalPath)
+ }
+ return subs
+}
+
+func (ss *StarlarkScript) HasErrors() bool {
+ return ss.hasErrors
+}
+
+// Convert reads and parses a makefile. If successful, parsed tree
+// is returned and then can be passed to String() to get the generated
+// Starlark file.
+func Convert(req Request) (*StarlarkScript, error) {
+ reader := req.Reader
+ if reader == nil {
+ mkContents, err := ioutil.ReadFile(req.MkFile)
+ if err != nil {
+ return nil, err
+ }
+ reader = bytes.NewBuffer(mkContents)
+ }
+ parser := mkparser.NewParser(req.MkFile, reader)
+ nodes, errs := parser.Parse()
+ if len(errs) > 0 {
+ for _, e := range errs {
+ fmt.Fprintln(os.Stderr, "ERROR:", e)
+ }
+ return nil, fmt.Errorf("bad makefile %s", req.MkFile)
+ }
+ starScript := &StarlarkScript{
+ moduleName: moduleNameForFile(req.MkFile),
+ mkFile: req.MkFile,
+ topDir: req.RootDir,
+ traceCalls: req.TraceCalls,
+ warnPartialSuccess: req.WarnPartialSuccess,
+ }
+ ctx := newParseContext(starScript, nodes)
+ ctx.outputSuffix = req.OutputSuffix
+ ctx.outputDir = req.OutputDir
+ ctx.errorLogger = req.ErrorLogger
+ if len(req.TracedVariables) > 0 {
+ ctx.tracedVariables = make(map[string]bool)
+ for _, v := range req.TracedVariables {
+ ctx.tracedVariables[v] = true
+ }
+ }
+ ctx.pushReceiver(starScript)
+ for ctx.hasNodes() && ctx.fatalError == nil {
+ node := ctx.getNode()
+ if ctx.handleSimpleStatement(node) {
+ continue
+ }
+ switch x := node.(type) {
+ case *mkparser.Directive:
+ switch x.Name {
+ case "ifeq", "ifneq", "ifdef", "ifndef":
+ ctx.handleIfBlock(x)
+ default:
+ ctx.errorf(x, "unexpected directive %s", x.Name)
+ }
+ default:
+ ctx.errorf(x, "unsupported line")
+ }
+ }
+ if ctx.fatalError != nil {
+ return nil, ctx.fatalError
+ }
+ return starScript, nil
+}
+
+func Launcher(path, name string) string {
+ var buf bytes.Buffer
+ fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
+ fmt.Fprintf(&buf, "load(%q, \"init\")\n", path)
+ fmt.Fprintf(&buf, "g, config = %s(%q, init)\n", cfnMain, name)
+ fmt.Fprintf(&buf, "%s(g, config)\n", cfnPrintVars)
+ return buf.String()
+}
+
+func MakePath2ModuleName(mkPath string) string {
+ return strings.TrimSuffix(mkPath, filepath.Ext(mkPath))
+}
diff --git a/mk2rbc/mk2rbc_test.go b/mk2rbc/mk2rbc_test.go
new file mode 100644
index 0000000..240d0b8
--- /dev/null
+++ b/mk2rbc/mk2rbc_test.go
@@ -0,0 +1,875 @@
+// Copyright 2021 Google LLC
+//
+// 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 mk2rbc
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+)
+
+var testCases = []struct {
+ desc string
+ mkname string
+ in string
+ expected string
+}{
+ {
+ desc: "Comment",
+ mkname: "product.mk",
+ in: `
+# Comment
+# FOO= a\
+ b
+`,
+ expected: `# Comment
+# FOO= a
+# b
+load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+`,
+ },
+ {
+ desc: "Name conversion",
+ mkname: "path/bar-baz.mk",
+ in: `
+# Comment
+`,
+ expected: `# Comment
+load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+`,
+ },
+ {
+ desc: "Item variable",
+ mkname: "pixel3.mk",
+ in: `
+PRODUCT_NAME := Pixel 3
+PRODUCT_MODEL :=
+local_var = foo
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_NAME"] = "Pixel 3"
+ cfg["PRODUCT_MODEL"] = ""
+ _local_var = "foo"
+`,
+ },
+ {
+ desc: "List variable",
+ mkname: "pixel4.mk",
+ in: `
+PRODUCT_PACKAGES = package1 package2
+PRODUCT_COPY_FILES += file2:target
+PRODUCT_PACKAGES += package3
+PRODUCT_COPY_FILES =
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_PACKAGES"] = [
+ "package1",
+ "package2",
+ ]
+ rblf.setdefault(handle, "PRODUCT_COPY_FILES")
+ cfg["PRODUCT_COPY_FILES"] += ["file2:target"]
+ cfg["PRODUCT_PACKAGES"] += ["package3"]
+ cfg["PRODUCT_COPY_FILES"] = []
+`,
+ },
+ {
+ desc: "Unknown function",
+ mkname: "product.mk",
+ in: `
+PRODUCT_NAME := $(call foo, bar)
+`,
+ expected: `# MK2RBC TRANSLATION ERROR: cannot handle invoking foo
+# PRODUCT_NAME := $(call foo, bar)
+load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ rblf.warning("product.mk", "partially successful conversion")
+`,
+ },
+ {
+ desc: "Inherit configuration always",
+ mkname: "product.mk",
+ in: `
+ifdef PRODUCT_NAME
+$(call inherit-product, part.mk)
+else # Comment
+$(call inherit-product, $(LOCAL_PATH)/part.mk)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+load(":part.star", _part_init = "init")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("PRODUCT_NAME") != None:
+ rblf.inherit(handle, "part", _part_init)
+ else:
+ # Comment
+ rblf.inherit(handle, "./part", _part_init)
+`,
+ },
+ {
+ desc: "Inherit configuration if it exists",
+ mkname: "product.mk",
+ in: `
+$(call inherit-product-if-exists, part.mk)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+load(":part.star|init", _part_init = "init")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if _part_init != None:
+ rblf.inherit(handle, "part", _part_init)
+`,
+ },
+
+ {
+ desc: "Include configuration",
+ mkname: "product.mk",
+ in: `
+ifdef PRODUCT_NAME
+include part.mk
+else
+-include $(LOCAL_PATH)/part.mk)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+load(":part.star", _part_init = "init")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("PRODUCT_NAME") != None:
+ _part_init(g, handle)
+ else:
+ if _part_init != None:
+ _part_init(g, handle)
+`,
+ },
+
+ {
+ desc: "Synonymous inherited configurations",
+ mkname: "path/product.mk",
+ in: `
+$(call inherit-product, foo/font.mk)
+$(call inherit-product, bar/font.mk)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+load("//foo:font.star", _font_init = "init")
+load("//bar:font.star", _font1_init = "init")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ rblf.inherit(handle, "foo/font", _font_init)
+ rblf.inherit(handle, "bar/font", _font1_init)
+`,
+ },
+ {
+ desc: "Directive define",
+ mkname: "product.mk",
+ in: `
+define some-macro
+ $(info foo)
+endef
+`,
+ expected: `# MK2RBC TRANSLATION ERROR: define is not supported: some-macro
+# define some-macro
+# $(info foo)
+# endef
+load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ rblf.warning("product.mk", "partially successful conversion")
+`,
+ },
+ {
+ desc: "Ifdef",
+ mkname: "product.mk",
+ in: `
+ifdef PRODUCT_NAME
+ PRODUCT_NAME = gizmo
+else
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("PRODUCT_NAME") != None:
+ cfg["PRODUCT_NAME"] = "gizmo"
+ else:
+ pass
+`,
+ },
+ {
+ desc: "Simple functions",
+ mkname: "product.mk",
+ in: `
+$(warning this is the warning)
+$(warning)
+$(info this is the info)
+$(error this is the error)
+PRODUCT_NAME:=$(shell echo *)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ rblf.mkwarning("product.mk", "this is the warning")
+ rblf.mkwarning("product.mk", "")
+ rblf.mkinfo("product.mk", "this is the info")
+ rblf.mkerror("product.mk", "this is the error")
+ cfg["PRODUCT_NAME"] = rblf.shell("echo *")
+`,
+ },
+ {
+ desc: "Empty if",
+ mkname: "product.mk",
+ in: `
+ifdef PRODUCT_NAME
+# Comment
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("PRODUCT_NAME") != None:
+ # Comment
+ pass
+`,
+ },
+ {
+ desc: "if/else/endif",
+ mkname: "product.mk",
+ in: `
+ifndef PRODUCT_NAME
+ PRODUCT_NAME=gizmo1
+else
+ PRODUCT_NAME=gizmo2
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if not g.get("PRODUCT_NAME") != None:
+ cfg["PRODUCT_NAME"] = "gizmo1"
+ else:
+ cfg["PRODUCT_NAME"] = "gizmo2"
+`,
+ },
+ {
+ desc: "else if",
+ mkname: "product.mk",
+ in: `
+ifdef PRODUCT_NAME
+ PRODUCT_NAME = gizmo
+else ifndef PRODUCT_PACKAGES # Comment
+endif
+ `,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("PRODUCT_NAME") != None:
+ cfg["PRODUCT_NAME"] = "gizmo"
+ elif not g.get("PRODUCT_PACKAGES") != None:
+ # Comment
+ pass
+`,
+ },
+ {
+ desc: "ifeq / ifneq",
+ mkname: "product.mk",
+ in: `
+ifeq (aosp_arm, $(TARGET_PRODUCT))
+ PRODUCT_MODEL = pix2
+else
+ PRODUCT_MODEL = pix21
+endif
+ifneq (aosp_x86, $(TARGET_PRODUCT))
+ PRODUCT_MODEL = pix3
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if "aosp_arm" == g["TARGET_PRODUCT"]:
+ cfg["PRODUCT_MODEL"] = "pix2"
+ else:
+ cfg["PRODUCT_MODEL"] = "pix21"
+ if "aosp_x86" != g["TARGET_PRODUCT"]:
+ cfg["PRODUCT_MODEL"] = "pix3"
+`,
+ },
+ {
+ desc: "Check filter result",
+ mkname: "product.mk",
+ in: `
+ifeq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
+endif
+ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))
+endif
+ifneq (,$(filter plaf,$(PLATFORM_LIST)))
+endif
+ifeq ($(TARGET_BUILD_VARIANT), $(filter $(TARGET_BUILD_VARIANT), userdebug eng))
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g["TARGET_BUILD_VARIANT"] not in ["userdebug", "eng"]:
+ pass
+ if g["TARGET_BUILD_VARIANT"] in ["userdebug"]:
+ pass
+ if "plaf" in g.get("PLATFORM_LIST", []):
+ pass
+ if g["TARGET_BUILD_VARIANT"] in ["userdebug", "eng"]:
+ pass
+`,
+ },
+ {
+ desc: "Get filter result",
+ mkname: "product.mk",
+ in: `
+PRODUCT_LIST2=$(filter-out %/foo.ko,$(wildcard path/*.ko))
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_LIST2"] = rblf.filter_out("%/foo.ko", rblf.expand_wildcard("path/*.ko"))
+`,
+ },
+ {
+ desc: "filter $(VAR), values",
+ mkname: "product.mk",
+ in: `
+ifeq (,$(filter $(TARGET_PRODUCT), yukawa_gms yukawa_sei510_gms)
+ ifneq (,$(filter $(TARGET_PRODUCT), yukawa_gms)
+ endif
+endif
+
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g["TARGET_PRODUCT"] not in ["yukawa_gms", "yukawa_sei510_gms"]:
+ if g["TARGET_PRODUCT"] in ["yukawa_gms"]:
+ pass
+`,
+ },
+ {
+ desc: "ifeq",
+ mkname: "product.mk",
+ in: `
+ifeq (aosp, $(TARGET_PRODUCT)) # Comment
+else ifneq (, $(TARGET_PRODUCT))
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if "aosp" == g["TARGET_PRODUCT"]:
+ # Comment
+ pass
+ elif g["TARGET_PRODUCT"]:
+ pass
+`,
+ },
+ {
+ desc: "Nested if",
+ mkname: "product.mk",
+ in: `
+ifdef PRODUCT_NAME
+ PRODUCT_PACKAGES = pack-if0
+ ifdef PRODUCT_MODEL
+ PRODUCT_PACKAGES = pack-if-if
+ else ifdef PRODUCT_NAME
+ PRODUCT_PACKAGES = pack-if-elif
+ else
+ PRODUCT_PACKAGES = pack-if-else
+ endif
+ PRODUCT_PACKAGES = pack-if
+else ifneq (,$(TARGET_PRODUCT))
+ PRODUCT_PACKAGES = pack-elif
+else
+ PRODUCT_PACKAGES = pack-else
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("PRODUCT_NAME") != None:
+ cfg["PRODUCT_PACKAGES"] = ["pack-if0"]
+ if g.get("PRODUCT_MODEL") != None:
+ cfg["PRODUCT_PACKAGES"] = ["pack-if-if"]
+ elif g.get("PRODUCT_NAME") != None:
+ cfg["PRODUCT_PACKAGES"] = ["pack-if-elif"]
+ else:
+ cfg["PRODUCT_PACKAGES"] = ["pack-if-else"]
+ cfg["PRODUCT_PACKAGES"] = ["pack-if"]
+ elif g["TARGET_PRODUCT"]:
+ cfg["PRODUCT_PACKAGES"] = ["pack-elif"]
+ else:
+ cfg["PRODUCT_PACKAGES"] = ["pack-else"]
+`,
+ },
+ {
+ desc: "Wildcard",
+ mkname: "product.mk",
+ in: `
+ifeq (,$(wildcard foo.mk))
+endif
+ifneq (,$(wildcard foo*.mk))
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if not rblf.file_exists("foo.mk"):
+ pass
+ if rblf.file_wildcard_exists("foo*.mk"):
+ pass
+`,
+ },
+ {
+ desc: "ifneq $(X),true",
+ mkname: "product.mk",
+ in: `
+ifneq ($(VARIABLE),true)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("VARIABLE", "") != "true":
+ pass
+`,
+ },
+ {
+ desc: "Const neq",
+ mkname: "product.mk",
+ in: `
+ifneq (1,0)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if "1" != "0":
+ pass
+`,
+ },
+ {
+ desc: "is-board calls",
+ mkname: "product.mk",
+ in: `
+ifeq ($(call is-board-platform-in-list,msm8998), true)
+else ifneq ($(call is-board-platform,copper),true)
+else ifneq ($(call is-vendor-board-platform,QCOM),true)
+else ifeq ($(call is-product-in-list, $(PLATFORM_LIST)), true)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if g.get("TARGET_BOARD_PLATFORM", "") in ["msm8998"]:
+ pass
+ elif g.get("TARGET_BOARD_PLATFORM", "") != "copper":
+ pass
+ elif g.get("TARGET_BOARD_PLATFORM", "") not in g["QCOM_BOARD_PLATFORMS"]:
+ pass
+ elif g["TARGET_PRODUCT"] in g.get("PLATFORM_LIST", []):
+ pass
+`,
+ },
+ {
+ desc: "findstring call",
+ mkname: "product.mk",
+ in: `
+ifneq ($(findstring foo,$(PRODUCT_PACKAGES)),)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if (cfg.get("PRODUCT_PACKAGES", [])).find("foo") != -1:
+ pass
+`,
+ },
+ {
+ desc: "rhs call",
+ mkname: "product.mk",
+ in: `
+PRODUCT_COPY_FILES = $(call add-to-product-copy-files-if-exists, path:distpath) \
+ $(call find-copy-subdir-files, *, fromdir, todir) $(wildcard foo.*)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_COPY_FILES"] = (rblf.copy_if_exists("path:distpath") +
+ rblf.find_and_copy("*", "fromdir", "todir") +
+ rblf.expand_wildcard("foo.*"))
+`,
+ },
+ {
+ desc: "inferred type",
+ mkname: "product.mk",
+ in: `
+HIKEY_MODS := $(wildcard foo/*.ko)
+BOARD_VENDOR_KERNEL_MODULES += $(HIKEY_MODS)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ g["HIKEY_MODS"] = rblf.expand_wildcard("foo/*.ko")
+ g.setdefault("BOARD_VENDOR_KERNEL_MODULES", [])
+ g["BOARD_VENDOR_KERNEL_MODULES"] += g["HIKEY_MODS"]
+`,
+ },
+ {
+ desc: "list with vars",
+ mkname: "product.mk",
+ in: `
+PRODUCT_COPY_FILES += path1:$(TARGET_PRODUCT)/path1 $(PRODUCT_MODEL)/path2:$(TARGET_PRODUCT)/path2
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ rblf.setdefault(handle, "PRODUCT_COPY_FILES")
+ cfg["PRODUCT_COPY_FILES"] += (("path1:%s/path1" % g["TARGET_PRODUCT"]).split() +
+ ("%s/path2:%s/path2" % (cfg.get("PRODUCT_MODEL", ""), g["TARGET_PRODUCT"])).split())
+`,
+ },
+ {
+ desc: "misc calls",
+ mkname: "product.mk",
+ in: `
+$(call enforce-product-packages-exist,)
+$(call enforce-product-packages-exist, foo)
+$(call require-artifacts-in-path, foo, bar)
+$(call require-artifacts-in-path-relaxed, foo, bar)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ rblf.enforce_product_packages_exist("")
+ rblf.enforce_product_packages_exist("foo")
+ rblf.require_artifacts_in_path("foo", "bar")
+ rblf.require_artifacts_in_path_relaxed("foo", "bar")
+`,
+ },
+ {
+ desc: "list with functions",
+ mkname: "product.mk",
+ in: `
+PRODUCT_COPY_FILES := $(call find-copy-subdir-files,*.kl,from1,to1) \
+ $(call find-copy-subdir-files,*.kc,from2,to2) \
+ foo bar
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_COPY_FILES"] = (rblf.find_and_copy("*.kl", "from1", "to1") +
+ rblf.find_and_copy("*.kc", "from2", "to2") +
+ [
+ "foo",
+ "bar",
+ ])
+`,
+ },
+ {
+ desc: "Text functions",
+ mkname: "product.mk",
+ in: `
+PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
+PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
+PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
+$(info $(patsubst %.pub,%,$(PRODUCT_ADB_KEYS)))
+
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
+ cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
+ cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
+ rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%", g.get("PRODUCT_ADB_KEYS", "")))
+`,
+ },
+ {
+ desc: "subst in list",
+ mkname: "product.mk",
+ in: `
+files = $(call find-copy-subdir-files,*,from,to)
+PRODUCT_COPY_FILES += $(subst foo,bar,$(files))
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ _files = rblf.find_and_copy("*", "from", "to")
+ rblf.setdefault(handle, "PRODUCT_COPY_FILES")
+ cfg["PRODUCT_COPY_FILES"] += rblf.mksubst("foo", "bar", _files)
+`,
+ },
+ {
+ desc: "assignment flavors",
+ mkname: "product.mk",
+ in: `
+PRODUCT_LIST1 := a
+PRODUCT_LIST2 += a
+PRODUCT_LIST1 += b
+PRODUCT_LIST2 += b
+PRODUCT_LIST3 ?= a
+PRODUCT_LIST1 = c
+PLATFORM_LIST += x
+PRODUCT_PACKAGES := $(PLATFORM_LIST)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_LIST1"] = ["a"]
+ rblf.setdefault(handle, "PRODUCT_LIST2")
+ cfg["PRODUCT_LIST2"] += ["a"]
+ cfg["PRODUCT_LIST1"] += ["b"]
+ cfg["PRODUCT_LIST2"] += ["b"]
+ if cfg.get("PRODUCT_LIST3") == None:
+ cfg["PRODUCT_LIST3"] = ["a"]
+ cfg["PRODUCT_LIST1"] = ["c"]
+ g.setdefault("PLATFORM_LIST", [])
+ g["PLATFORM_LIST"] += ["x"]
+ cfg["PRODUCT_PACKAGES"] = g["PLATFORM_LIST"][:]
+`,
+ },
+ {
+ desc: "assigment flavors2",
+ mkname: "product.mk",
+ in: `
+PRODUCT_LIST1 = a
+ifeq (0,1)
+ PRODUCT_LIST1 += b
+ PRODUCT_LIST2 += b
+endif
+PRODUCT_LIST1 += c
+PRODUCT_LIST2 += c
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_LIST1"] = ["a"]
+ if "0" == "1":
+ cfg["PRODUCT_LIST1"] += ["b"]
+ rblf.setdefault(handle, "PRODUCT_LIST2")
+ cfg["PRODUCT_LIST2"] += ["b"]
+ cfg["PRODUCT_LIST1"] += ["c"]
+ rblf.setdefault(handle, "PRODUCT_LIST2")
+ cfg["PRODUCT_LIST2"] += ["c"]
+`,
+ },
+ {
+ desc: "string split",
+ mkname: "product.mk",
+ in: `
+PRODUCT_LIST1 = a
+local = b
+local += c
+FOO = d
+FOO += e
+PRODUCT_LIST1 += $(local)
+PRODUCT_LIST1 += $(FOO)
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_LIST1"] = ["a"]
+ _local = "b"
+ _local += " " + "c"
+ g["FOO"] = "d"
+ g["FOO"] += " " + "e"
+ cfg["PRODUCT_LIST1"] += (_local).split()
+ cfg["PRODUCT_LIST1"] += (g["FOO"]).split()
+`,
+ },
+ {
+ desc: "apex_jars",
+ mkname: "product.mk",
+ in: `
+PRODUCT_BOOT_JARS := $(ART_APEX_JARS) framework-minus-apex
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ cfg["PRODUCT_BOOT_JARS"] = (g.get("ART_APEX_JARS", []) +
+ ["framework-minus-apex"])
+`,
+ },
+ {
+ desc: "strip function",
+ mkname: "product.mk",
+ in: `
+ifeq ($(filter hwaddress,$(PRODUCT_PACKAGES)),)
+ PRODUCT_PACKAGES := $(strip $(PRODUCT_PACKAGES) hwaddress)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if "hwaddress" not in cfg.get("PRODUCT_PACKAGES", []):
+ cfg["PRODUCT_PACKAGES"] = (rblf.mkstrip("%s hwaddress" % " ".join(cfg.get("PRODUCT_PACKAGES", [])))).split()
+`,
+ },
+ {
+ desc: "strip func in condition",
+ mkname: "product.mk",
+ in: `
+ifneq ($(strip $(TARGET_VENDOR)),)
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ if rblf.mkstrip(g.get("TARGET_VENDOR", "")) != "":
+ pass
+`,
+ },
+ {
+ desc: "ref after set",
+ mkname: "product.mk",
+ in: `
+PRODUCT_ADB_KEYS:=value
+FOO := $(PRODUCT_ADB_KEYS)
+ifneq (,$(PRODUCT_ADB_KEYS))
+endif
+`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ g["PRODUCT_ADB_KEYS"] = "value"
+ g["FOO"] = g["PRODUCT_ADB_KEYS"]
+ if g["PRODUCT_ADB_KEYS"]:
+ pass
+`,
+ },
+ {
+ desc: "ref before set",
+ mkname: "product.mk",
+ in: `
+V1 := $(PRODUCT_ADB_KEYS)
+ifeq (,$(PRODUCT_ADB_KEYS))
+ V2 := $(PRODUCT_ADB_KEYS)
+ PRODUCT_ADB_KEYS:=foo
+ V3 := $(PRODUCT_ADB_KEYS)
+endif`,
+ expected: `load("//build/make/core:product_config.rbc", "rblf")
+
+def init(g, handle):
+ cfg = rblf.cfg(handle)
+ g["V1"] = g.get("PRODUCT_ADB_KEYS", "")
+ if not g.get("PRODUCT_ADB_KEYS", ""):
+ g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
+ g["PRODUCT_ADB_KEYS"] = "foo"
+ g["V3"] = g["PRODUCT_ADB_KEYS"]
+`,
+ },
+}
+
+var known_variables = []struct {
+ name string
+ class varClass
+ starlarkType
+}{
+ {"PRODUCT_NAME", VarClassConfig, starlarkTypeString},
+ {"PRODUCT_MODEL", VarClassConfig, starlarkTypeString},
+ {"PRODUCT_PACKAGES", VarClassConfig, starlarkTypeList},
+ {"PRODUCT_BOOT_JARS", VarClassConfig, starlarkTypeList},
+ {"PRODUCT_COPY_FILES", VarClassConfig, starlarkTypeList},
+ {"PRODUCT_IS_64BIT", VarClassConfig, starlarkTypeString},
+ {"PRODUCT_LIST1", VarClassConfig, starlarkTypeList},
+ {"PRODUCT_LIST2", VarClassConfig, starlarkTypeList},
+ {"PRODUCT_LIST3", VarClassConfig, starlarkTypeList},
+ {"TARGET_PRODUCT", VarClassSoong, starlarkTypeString},
+ {"TARGET_BUILD_VARIANT", VarClassSoong, starlarkTypeString},
+ {"TARGET_BOARD_PLATFORM", VarClassSoong, starlarkTypeString},
+ {"QCOM_BOARD_PLATFORMS", VarClassSoong, starlarkTypeString},
+ {"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
+}
+
+func TestGood(t *testing.T) {
+ for _, v := range known_variables {
+ KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
+ }
+ for _, test := range testCases {
+ t.Run(test.desc,
+ func(t *testing.T) {
+ ss, err := Convert(Request{
+ MkFile: test.mkname,
+ Reader: bytes.NewBufferString(test.in),
+ RootDir: ".",
+ OutputSuffix: ".star",
+ WarnPartialSuccess: true,
+ })
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ got := ss.String()
+ if got != test.expected {
+ t.Errorf("%q failed\nExpected:\n%s\nActual:\n%s\n", test.desc,
+ strings.ReplaceAll(test.expected, "\n", "\n"),
+ strings.ReplaceAll(got, "\n", "\n"))
+ }
+ })
+ }
+}
diff --git a/mk2rbc/node.go b/mk2rbc/node.go
new file mode 100644
index 0000000..d4b4222
--- /dev/null
+++ b/mk2rbc/node.go
@@ -0,0 +1,237 @@
+// Copyright 2021 Google LLC
+//
+// 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 mk2rbc
+
+import (
+ "fmt"
+ "strings"
+
+ mkparser "android/soong/androidmk/parser"
+)
+
+// A parsed node for which starlark code will be generated
+// by calling emit().
+type starlarkNode interface {
+ emit(ctx *generationContext)
+}
+
+// Types used to keep processed makefile data:
+type commentNode struct {
+ text string
+}
+
+func (c *commentNode) emit(gctx *generationContext) {
+ chunks := strings.Split(c.text, "\\\n")
+ gctx.newLine()
+ gctx.write(chunks[0]) // It has '#' at the beginning already.
+ for _, chunk := range chunks[1:] {
+ gctx.newLine()
+ gctx.write("#", chunk)
+ }
+}
+
+type inheritedModule struct {
+ path string // Converted Starlark file path
+ originalPath string // Makefile file path
+ moduleName string
+ moduleLocalName string
+ loadAlways bool
+}
+
+func (im inheritedModule) name() string {
+ return MakePath2ModuleName(im.originalPath)
+}
+
+func (im inheritedModule) entryName() string {
+ return im.moduleLocalName + "_init"
+}
+
+type inheritNode struct {
+ *inheritedModule
+}
+
+func (inn *inheritNode) emit(gctx *generationContext) {
+ // Unconditional case:
+ // rblf.inherit(handle, <module>, module_init)
+ // Conditional case:
+ // if <module>_init != None:
+ // same as above
+ gctx.newLine()
+ if inn.loadAlways {
+ gctx.writef("%s(handle, %q, %s)", cfnInherit, inn.name(), inn.entryName())
+ return
+ }
+ gctx.writef("if %s != None:", inn.entryName())
+ gctx.indentLevel++
+ gctx.newLine()
+ gctx.writef("%s(handle, %q, %s)", cfnInherit, inn.name(), inn.entryName())
+ gctx.indentLevel--
+}
+
+type includeNode struct {
+ *inheritedModule
+}
+
+func (inn *includeNode) emit(gctx *generationContext) {
+ gctx.newLine()
+ if inn.loadAlways {
+ gctx.writef("%s(g, handle)", inn.entryName())
+ return
+ }
+ gctx.writef("if %s != None:", inn.entryName())
+ gctx.indentLevel++
+ gctx.newLine()
+ gctx.writef("%s(g, handle)", inn.entryName())
+ gctx.indentLevel--
+}
+
+type assignmentFlavor int
+
+const (
+ // Assignment flavors
+ asgnSet assignmentFlavor = iota // := or =
+ asgnMaybeSet assignmentFlavor = iota // ?= and variable may be unset
+ asgnAppend assignmentFlavor = iota // += and variable has been set before
+ asgnMaybeAppend assignmentFlavor = iota // += and variable may be unset
+)
+
+type assignmentNode struct {
+ lhs variable
+ value starlarkExpr
+ mkValue *mkparser.MakeString
+ flavor assignmentFlavor
+ isTraced bool
+ previous *assignmentNode
+}
+
+func (asgn *assignmentNode) emit(gctx *generationContext) {
+ gctx.newLine()
+ gctx.inAssignment = true
+ asgn.lhs.emitSet(gctx, asgn)
+ gctx.inAssignment = false
+
+ if asgn.isTraced {
+ gctx.newLine()
+ gctx.tracedCount++
+ gctx.writef(`print("%s.%d: %s := ", `, gctx.starScript.mkFile, gctx.tracedCount, asgn.lhs.name())
+ asgn.lhs.emitGet(gctx, true)
+ gctx.writef(")")
+ }
+}
+
+type exprNode struct {
+ expr starlarkExpr
+}
+
+func (exn *exprNode) emit(gctx *generationContext) {
+ gctx.newLine()
+ exn.expr.emit(gctx)
+}
+
+type ifNode struct {
+ isElif bool // true if this is 'elif' statement
+ expr starlarkExpr
+}
+
+func (in *ifNode) emit(gctx *generationContext) {
+ ifElif := "if "
+ if in.isElif {
+ ifElif = "elif "
+ }
+
+ gctx.newLine()
+ if bad, ok := in.expr.(*badExpr); ok {
+ gctx.write("# MK2STAR ERROR converting:")
+ gctx.newLine()
+ gctx.writef("# %s", bad.node.Dump())
+ gctx.newLine()
+ gctx.writef("# %s", bad.message)
+ gctx.newLine()
+ // The init function emits a warning if the conversion was not
+ // fullly successful, so here we (arbitrarily) take the false path.
+ gctx.writef("%sFalse:", ifElif)
+ return
+ }
+ gctx.write(ifElif)
+ in.expr.emit(gctx)
+ gctx.write(":")
+}
+
+type elseNode struct{}
+
+func (br *elseNode) emit(gctx *generationContext) {
+ gctx.newLine()
+ gctx.write("else:")
+}
+
+// switchCase represents as single if/elseif/else branch. All the necessary
+// info about flavor (if/elseif/else) is supposed to be kept in `gate`.
+type switchCase struct {
+ gate starlarkNode
+ nodes []starlarkNode
+}
+
+func (cb *switchCase) newNode(node starlarkNode) {
+ cb.nodes = append(cb.nodes, node)
+}
+
+func (cb *switchCase) emit(gctx *generationContext) {
+ cb.gate.emit(gctx)
+ gctx.indentLevel++
+ hasStatements := false
+ emitNode := func(node starlarkNode) {
+ if _, ok := node.(*commentNode); !ok {
+ hasStatements = true
+ }
+ node.emit(gctx)
+ }
+ if len(cb.nodes) > 0 {
+ emitNode(cb.nodes[0])
+ for _, node := range cb.nodes[1:] {
+ emitNode(node)
+ }
+ if !hasStatements {
+ gctx.emitPass()
+ }
+ } else {
+ gctx.emitPass()
+ }
+ gctx.indentLevel--
+}
+
+// A single complete if ... elseif ... else ... endif sequences
+type switchNode struct {
+ ssCases []*switchCase
+}
+
+func (ssw *switchNode) newNode(node starlarkNode) {
+ switch br := node.(type) {
+ case *switchCase:
+ ssw.ssCases = append(ssw.ssCases, br)
+ default:
+ panic(fmt.Errorf("expected switchCase node, got %t", br))
+ }
+}
+
+func (ssw *switchNode) emit(gctx *generationContext) {
+ if len(ssw.ssCases) == 0 {
+ gctx.emitPass()
+ } else {
+ ssw.ssCases[0].emit(gctx)
+ for _, ssCase := range ssw.ssCases[1:] {
+ ssCase.emit(gctx)
+ }
+ }
+}
diff --git a/mk2rbc/types.go b/mk2rbc/types.go
index 22c8b58..1625464 100644
--- a/mk2rbc/types.go
+++ b/mk2rbc/types.go
@@ -18,6 +18,11 @@
type starlarkType int
const (
+ // Variable types. Initially we only know the types of the product
+ // configuration variables that are lists, and the types of some
+ // hardwired variables. The remaining variables are first entered as
+ // having an unknown type and treated as strings, but sometimes we
+ // can infer variable's type from the value assigned to it.
starlarkTypeUnknown starlarkType = iota
starlarkTypeList starlarkType = iota
starlarkTypeString starlarkType = iota
diff --git a/mk2rbc/variable.go b/mk2rbc/variable.go
new file mode 100644
index 0000000..a650453
--- /dev/null
+++ b/mk2rbc/variable.go
@@ -0,0 +1,305 @@
+// Copyright 2021 Google LLC
+//
+// 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 mk2rbc
+
+import (
+ "fmt"
+ "os"
+ "strings"
+)
+
+type variable interface {
+ name() string
+ emitGet(gctx *generationContext, isDefined bool)
+ emitSet(gctx *generationContext, asgn *assignmentNode)
+ emitDefined(gctx *generationContext)
+ valueType() starlarkType
+ setValueType(t starlarkType)
+ defaultValueString() string
+ isPreset() bool
+}
+
+type baseVariable struct {
+ nam string
+ typ starlarkType
+ preset bool // true if it has been initialized at startup
+}
+
+func (v baseVariable) name() string {
+ return v.nam
+}
+
+func (v baseVariable) valueType() starlarkType {
+ return v.typ
+}
+
+func (v *baseVariable) setValueType(t starlarkType) {
+ v.typ = t
+}
+
+func (v baseVariable) isPreset() bool {
+ return v.preset
+}
+
+var defaultValuesByType = map[starlarkType]string{
+ starlarkTypeUnknown: `""`,
+ starlarkTypeList: "[]",
+ starlarkTypeString: `""`,
+ starlarkTypeInt: "0",
+ starlarkTypeBool: "False",
+ starlarkTypeVoid: "None",
+}
+
+func (v baseVariable) defaultValueString() string {
+ if v, ok := defaultValuesByType[v.valueType()]; ok {
+ return v
+ }
+ panic(fmt.Errorf("%s has unknown type %q", v.name(), v.valueType()))
+}
+
+type productConfigVariable struct {
+ baseVariable
+}
+
+func (pcv productConfigVariable) emitSet(gctx *generationContext, asgn *assignmentNode) {
+ emitAssignment := func() {
+ pcv.emitGet(gctx, true)
+ gctx.write(" = ")
+ asgn.value.emitListVarCopy(gctx)
+ }
+ emitAppend := func() {
+ pcv.emitGet(gctx, true)
+ gctx.write(" += ")
+ if pcv.valueType() == starlarkTypeString {
+ gctx.writef(`" " + `)
+ }
+ asgn.value.emit(gctx)
+ }
+
+ switch asgn.flavor {
+ case asgnSet:
+ emitAssignment()
+ case asgnAppend:
+ emitAppend()
+ case asgnMaybeAppend:
+ // If we are not sure variable has been assigned before, emit setdefault
+ if pcv.typ == starlarkTypeList {
+ gctx.writef("%s(handle, %q)", cfnSetListDefault, pcv.name())
+ } else {
+ gctx.writef("cfg.setdefault(%q, %s)", pcv.name(), pcv.defaultValueString())
+ }
+ gctx.newLine()
+ emitAppend()
+ case asgnMaybeSet:
+ gctx.writef("if cfg.get(%q) == None:", pcv.nam)
+ gctx.indentLevel++
+ gctx.newLine()
+ emitAssignment()
+ gctx.indentLevel--
+ }
+}
+
+func (pcv productConfigVariable) emitGet(gctx *generationContext, isDefined bool) {
+ if isDefined || pcv.isPreset() {
+ gctx.writef("cfg[%q]", pcv.nam)
+ } else {
+ gctx.writef("cfg.get(%q, %s)", pcv.nam, pcv.defaultValueString())
+ }
+}
+
+func (pcv productConfigVariable) emitDefined(gctx *generationContext) {
+ gctx.writef("g.get(%q) != None", pcv.name())
+}
+
+type otherGlobalVariable struct {
+ baseVariable
+}
+
+func (scv otherGlobalVariable) emitSet(gctx *generationContext, asgn *assignmentNode) {
+ emitAssignment := func() {
+ scv.emitGet(gctx, true)
+ gctx.write(" = ")
+ asgn.value.emitListVarCopy(gctx)
+ }
+
+ emitAppend := func() {
+ scv.emitGet(gctx, true)
+ gctx.write(" += ")
+ if scv.valueType() == starlarkTypeString {
+ gctx.writef(`" " + `)
+ }
+ asgn.value.emit(gctx)
+ }
+
+ switch asgn.flavor {
+ case asgnSet:
+ emitAssignment()
+ case asgnAppend:
+ emitAppend()
+ case asgnMaybeAppend:
+ // If we are not sure variable has been assigned before, emit setdefault
+ gctx.writef("g.setdefault(%q, %s)", scv.name(), scv.defaultValueString())
+ gctx.newLine()
+ emitAppend()
+ case asgnMaybeSet:
+ gctx.writef("if g.get(%q) == None:", scv.nam)
+ gctx.indentLevel++
+ gctx.newLine()
+ emitAssignment()
+ gctx.indentLevel--
+ }
+}
+
+func (scv otherGlobalVariable) emitGet(gctx *generationContext, isDefined bool) {
+ if isDefined || scv.isPreset() {
+ gctx.writef("g[%q]", scv.nam)
+ } else {
+ gctx.writef("g.get(%q, %s)", scv.nam, scv.defaultValueString())
+ }
+}
+
+func (scv otherGlobalVariable) emitDefined(gctx *generationContext) {
+ gctx.writef("g.get(%q) != None", scv.name())
+}
+
+type localVariable struct {
+ baseVariable
+}
+
+func (lv localVariable) emitDefined(_ *generationContext) {
+ panic("implement me")
+}
+
+func (lv localVariable) String() string {
+ return "_" + lv.nam
+}
+
+func (lv localVariable) emitSet(gctx *generationContext, asgn *assignmentNode) {
+ switch asgn.flavor {
+ case asgnSet:
+ gctx.writef("%s = ", lv)
+ asgn.value.emitListVarCopy(gctx)
+ case asgnAppend:
+ lv.emitGet(gctx, false)
+ gctx.write(" += ")
+ if lv.valueType() == starlarkTypeString {
+ gctx.writef(`" " + `)
+ }
+ asgn.value.emit(gctx)
+ case asgnMaybeAppend:
+ gctx.writef("%s(%q, ", cfnLocalAppend, lv)
+ asgn.value.emit(gctx)
+ gctx.write(")")
+ case asgnMaybeSet:
+ gctx.writef("%s(%q, ", cfnLocalSetDefault, lv)
+ asgn.value.emit(gctx)
+ gctx.write(")")
+ }
+}
+
+func (lv localVariable) emitGet(gctx *generationContext, _ bool) {
+ gctx.writef("%s", lv)
+}
+
+type predefinedVariable struct {
+ baseVariable
+ value starlarkExpr
+}
+
+func (pv predefinedVariable) emitGet(gctx *generationContext, _ bool) {
+ pv.value.emit(gctx)
+}
+
+func (pv predefinedVariable) emitSet(_ *generationContext, asgn *assignmentNode) {
+ if expectedValue, ok1 := maybeString(pv.value); ok1 {
+ actualValue, ok2 := maybeString(asgn.value)
+ if ok2 {
+ if actualValue == expectedValue {
+ return
+ }
+ fmt.Fprintf(os.Stderr, "cannot set predefined variable %s to %q, its value should be %q",
+ pv.name(), actualValue, expectedValue)
+ return
+ }
+ }
+ panic(fmt.Errorf("cannot set predefined variable %s to %q", pv.name(), asgn.mkValue.Dump()))
+}
+
+func (pv predefinedVariable) emitDefined(gctx *generationContext) {
+ gctx.write("True")
+}
+
+var localProductConfigVariables = map[string]string{
+ "LOCAL_AUDIO_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+ "LOCAL_AUDIO_PRODUCT_COPY_FILES": "PRODUCT_COPY_FILES",
+ "LOCAL_AUDIO_DEVICE_PACKAGE_OVERLAYS": "DEVICE_PACKAGE_OVERLAYS",
+ "LOCAL_DUMPSTATE_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+ "LOCAL_GATEKEEPER_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+ "LOCAL_HEALTH_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+ "LOCAL_SENSOR_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+ "LOCAL_KEYMASTER_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+ "LOCAL_KEYMINT_PRODUCT_PACKAGE": "PRODUCT_PACKAGES",
+}
+
+var presetVariables = map[string]bool{
+ "BUILD_ID": true,
+ "HOST_ARCH": true,
+ "HOST_OS": true,
+ "HOST_BUILD_TYPE": true,
+ "OUT_DIR": true,
+ "PLATFORM_VERSION_CODENAME": true,
+ "PLATFORM_VERSION": true,
+ "TARGET_ARCH": true,
+ "TARGET_ARCH_VARIANT": true,
+ "TARGET_BUILD_TYPE": true,
+ "TARGET_BUILD_VARIANT": true,
+ "TARGET_PRODUCT": true,
+}
+
+// addVariable returns a variable with a given name. A variable is
+// added if it does not exist yet.
+func (ctx *parseContext) addVariable(name string) variable {
+ v, found := ctx.variables[name]
+ if !found {
+ _, preset := presetVariables[name]
+ if vi, found := KnownVariables[name]; found {
+ switch vi.class {
+ case VarClassConfig:
+ v = &productConfigVariable{baseVariable{nam: name, typ: vi.valueType, preset: preset}}
+ case VarClassSoong:
+ v = &otherGlobalVariable{baseVariable{nam: name, typ: vi.valueType, preset: preset}}
+ }
+ } else if name == strings.ToLower(name) {
+ // Heuristics: if variable's name is all lowercase, consider it local
+ // string variable.
+ v = &localVariable{baseVariable{nam: name, typ: starlarkTypeUnknown}}
+ } else {
+ vt := starlarkTypeUnknown
+ if strings.HasPrefix(name, "LOCAL_") {
+ // Heuristics: local variables that contribute to corresponding config variables
+ if cfgVarName, found := localProductConfigVariables[name]; found {
+ vi, found2 := KnownVariables[cfgVarName]
+ if !found2 {
+ panic(fmt.Errorf("unknown config variable %s for %s", cfgVarName, name))
+ }
+ vt = vi.valueType
+ }
+ }
+ v = &otherGlobalVariable{baseVariable{nam: name, typ: vt}}
+ }
+ ctx.variables[name] = v
+ }
+ return v
+}
diff --git a/rust/builder.go b/rust/builder.go
index 523428d..6c44166 100644
--- a/rust/builder.go
+++ b/rust/builder.go
@@ -332,6 +332,9 @@
rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
+ // Silence warnings about renamed lints
+ rustdocFlags = append(rustdocFlags, " -A renamed_and_removed_lints")
+
// Yes, the same out directory is used simultaneously by all rustdoc builds.
// This is what cargo does. The docs for individual crates get generated to
// a subdirectory named for the crate, and rustdoc synchronizes writes to
diff --git a/rust/compiler.go b/rust/compiler.go
index df77759..de59f39 100644
--- a/rust/compiler.go
+++ b/rust/compiler.go
@@ -17,6 +17,7 @@
import (
"fmt"
"path/filepath"
+ "strings"
"github.com/google/blueprint/proptools"
@@ -235,6 +236,25 @@
if err != nil {
ctx.PropertyErrorf("lints", err.Error())
}
+
+ // linkage-related flags are disallowed.
+ for _, s := range compiler.Properties.Ld_flags {
+ if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
+ ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
+ }
+ }
+ for _, s := range compiler.Properties.Flags {
+ if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
+ ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
+ }
+ if strings.HasPrefix(s, "--extern") {
+ ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
+ }
+ if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
+ ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
+ }
+ }
+
flags.RustFlags = append(flags.RustFlags, lintFlags)
flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
diff --git a/rust/compiler_test.go b/rust/compiler_test.go
index 5ca9e7f..c331b4c 100644
--- a/rust/compiler_test.go
+++ b/rust/compiler_test.go
@@ -208,3 +208,73 @@
t.Errorf("libstd is not linked dynamically for dylibs")
}
}
+
+// Ensure that manual link flags are disallowed.
+func TestManualLinkageRejection(t *testing.T) {
+ // rustc flags
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ flags: ["-lbar"],
+ }
+ `)
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ flags: ["--extern=foo"],
+ }
+ `)
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ flags: ["-Clink-args=foo"],
+ }
+ `)
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ flags: ["-C link-args=foo"],
+ }
+ `)
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ flags: ["-L foo/"],
+ }
+ `)
+
+ // lld flags
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ ld_flags: ["-Wl,-L bar/"],
+ }
+ `)
+ testRustError(t, ".* cannot be manually specified", `
+ rust_binary {
+ name: "foo",
+ srcs: [
+ "foo.rs",
+ ],
+ ld_flags: ["-Wl,-lbar"],
+ }
+ `)
+}
diff --git a/rust/config/allowed_list.go b/rust/config/allowed_list.go
index f568f27..ca110a2 100644
--- a/rust/config/allowed_list.go
+++ b/rust/config/allowed_list.go
@@ -28,6 +28,7 @@
"system/security",
"system/tools/aidl",
"tools/security/fuzzing/example_rust_fuzzer",
+ "vendor/",
}
DownstreamRustAllowedPaths = []string{
diff --git a/rust/config/global.go b/rust/config/global.go
index 1b56237..c390711 100644
--- a/rust/config/global.go
+++ b/rust/config/global.go
@@ -24,7 +24,7 @@
var pctx = android.NewPackageContext("android/soong/rust/config")
var (
- RustDefaultVersion = "1.51.0"
+ RustDefaultVersion = "1.53.0"
RustDefaultBase = "prebuilts/rust/"
DefaultEdition = "2018"
Stdlibs = []string{
diff --git a/rust/config/x86_linux_host.go b/rust/config/x86_linux_host.go
index a9fdaed..0aa534f 100644
--- a/rust/config/x86_linux_host.go
+++ b/rust/config/x86_linux_host.go
@@ -37,6 +37,10 @@
registerToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory)
registerToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory)
+ // TODO: musl rust support
+ registerToolchainFactory(android.LinuxMusl, android.X86_64, linuxX8664ToolchainFactory)
+ registerToolchainFactory(android.LinuxMusl, android.X86, linuxX86ToolchainFactory)
+
pctx.StaticVariable("LinuxToolchainRustFlags", strings.Join(LinuxRustFlags, " "))
pctx.StaticVariable("LinuxToolchainLinkFlags", strings.Join(LinuxRustLinkFlags, " "))
pctx.StaticVariable("LinuxToolchainX86RustFlags", strings.Join(linuxX86Rustflags, " "))
diff --git a/rust/rust.go b/rust/rust.go
index 931cb9d..80be496 100644
--- a/rust/rust.go
+++ b/rust/rust.go
@@ -861,6 +861,8 @@
if mod.installable(apexInfo) {
mod.compiler.install(ctx)
}
+
+ ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
}
}
diff --git a/sdk/bootclasspath_fragment_sdk_test.go b/sdk/bootclasspath_fragment_sdk_test.go
index efd2b5b..c7ad798 100644
--- a/sdk/bootclasspath_fragment_sdk_test.go
+++ b/sdk/bootclasspath_fragment_sdk_test.go
@@ -224,7 +224,7 @@
java.PrepareForTestWithJavaDefaultModules,
java.PrepareForTestWithJavaSdkLibraryFiles,
java.FixtureWithLastReleaseApis("mysdklibrary", "myothersdklibrary", "mycoreplatform"),
- java.FixtureConfigureUpdatableBootJars("myapex:mybootlib", "myapex:myothersdklibrary"),
+ java.FixtureConfigureApexBootJars("myapex:mybootlib", "myapex:myothersdklibrary"),
prepareForSdkTestWithApex,
// Add a platform_bootclasspath that depends on the fragment.
@@ -728,7 +728,7 @@
java.PrepareForTestWithJavaDefaultModules,
java.PrepareForTestWithJavaSdkLibraryFiles,
java.FixtureWithLastReleaseApis("mysdklibrary"),
- java.FixtureConfigureUpdatableBootJars("myapex:mybootlib"),
+ java.FixtureConfigureApexBootJars("myapex:mybootlib"),
prepareForSdkTestWithApex,
// Add a platform_bootclasspath that depends on the fragment.
diff --git a/sdk/cc_sdk_test.go b/sdk/cc_sdk_test.go
index 60fbccf..25e35fc 100644
--- a/sdk/cc_sdk_test.go
+++ b/sdk/cc_sdk_test.go
@@ -347,7 +347,7 @@
cc_object {
name: "crtobj",
stl: "none",
- default_shared_libs: [],
+ system_shared_libs: [],
sanitize: {
never: true,
},
@@ -365,7 +365,7 @@
apex_available: ["//apex_available:platform"],
stl: "none",
compile_multilib: "both",
- default_shared_libs: [],
+ system_shared_libs: [],
sanitize: {
never: true,
},
@@ -390,7 +390,7 @@
apex_available: ["//apex_available:platform"],
stl: "none",
compile_multilib: "both",
- default_shared_libs: [],
+ system_shared_libs: [],
sanitize: {
never: true,
},
@@ -2192,7 +2192,7 @@
result := testSdkWithCc(t, `
sdk {
name: "mysdk",
- native_shared_libs: ["sslnil", "sslempty", "sslnonempty", "dslnil", "dslempty", "dslnonempty"],
+ native_shared_libs: ["sslnil", "sslempty", "sslnonempty"],
}
cc_library {
@@ -2209,21 +2209,6 @@
name: "sslnonempty",
system_shared_libs: ["sslnil"],
}
-
- cc_library {
- name: "dslnil",
- host_supported: true,
- }
-
- cc_library {
- name: "dslempty",
- default_shared_libs: [],
- }
-
- cc_library {
- name: "dslnonempty",
- default_shared_libs: ["sslnil"],
- }
`)
CheckSnapshot(t, result, "mysdk", "",
@@ -2279,62 +2264,13 @@
},
},
}
-
-cc_prebuilt_library_shared {
- name: "dslnil",
- prefer: false,
- visibility: ["//visibility:public"],
- apex_available: ["//apex_available:platform"],
- compile_multilib: "both",
- arch: {
- arm64: {
- srcs: ["arm64/lib/dslnil.so"],
- },
- arm: {
- srcs: ["arm/lib/dslnil.so"],
- },
- },
-}
-
-cc_prebuilt_library_shared {
- name: "dslempty",
- prefer: false,
- visibility: ["//visibility:public"],
- apex_available: ["//apex_available:platform"],
- compile_multilib: "both",
- default_shared_libs: [],
- arch: {
- arm64: {
- srcs: ["arm64/lib/dslempty.so"],
- },
- arm: {
- srcs: ["arm/lib/dslempty.so"],
- },
- },
-}
-
-cc_prebuilt_library_shared {
- name: "dslnonempty",
- prefer: false,
- visibility: ["//visibility:public"],
- apex_available: ["//apex_available:platform"],
- compile_multilib: "both",
- default_shared_libs: ["sslnil"],
- arch: {
- arm64: {
- srcs: ["arm64/lib/dslnonempty.so"],
- },
- arm: {
- srcs: ["arm/lib/dslnonempty.so"],
- },
- },
-}`))
+`))
result = testSdkWithCc(t, `
sdk {
name: "mysdk",
host_supported: true,
- native_shared_libs: ["sslvariants", "dslvariants"],
+ native_shared_libs: ["sslvariants"],
}
cc_library {
@@ -2346,16 +2282,6 @@
},
},
}
-
- cc_library {
- name: "dslvariants",
- host_supported: true,
- target: {
- android: {
- default_shared_libs: [],
- },
- },
- }
`)
CheckSnapshot(t, result, "mysdk", "",
@@ -2392,37 +2318,6 @@
},
},
}
-
-cc_prebuilt_library_shared {
- name: "dslvariants",
- prefer: false,
- visibility: ["//visibility:public"],
- apex_available: ["//apex_available:platform"],
- host_supported: true,
- compile_multilib: "both",
- target: {
- host: {
- enabled: false,
- },
- android: {
- default_shared_libs: [],
- },
- android_arm64: {
- srcs: ["android/arm64/lib/dslvariants.so"],
- },
- android_arm: {
- srcs: ["android/arm/lib/dslvariants.so"],
- },
- linux_glibc_x86_64: {
- enabled: true,
- srcs: ["linux_glibc/x86_64/lib/dslvariants.so"],
- },
- linux_glibc_x86: {
- enabled: true,
- srcs: ["linux_glibc/x86/lib/dslvariants.so"],
- },
- },
-}
`),
checkVersionedAndroidBpContents(`
// This is auto-generated. DO NOT EDIT.
@@ -2459,46 +2354,11 @@
},
}
-cc_prebuilt_library_shared {
- name: "mysdk_dslvariants@current",
- sdk_member_name: "dslvariants",
- visibility: ["//visibility:public"],
- apex_available: ["//apex_available:platform"],
- host_supported: true,
- installable: false,
- compile_multilib: "both",
- target: {
- host: {
- enabled: false,
- },
- android: {
- default_shared_libs: [],
- },
- android_arm64: {
- srcs: ["android/arm64/lib/dslvariants.so"],
- },
- android_arm: {
- srcs: ["android/arm/lib/dslvariants.so"],
- },
- linux_glibc_x86_64: {
- enabled: true,
- srcs: ["linux_glibc/x86_64/lib/dslvariants.so"],
- },
- linux_glibc_x86: {
- enabled: true,
- srcs: ["linux_glibc/x86/lib/dslvariants.so"],
- },
- },
-}
-
sdk_snapshot {
name: "mysdk@current",
visibility: ["//visibility:public"],
host_supported: true,
- native_shared_libs: [
- "mysdk_sslvariants@current",
- "mysdk_dslvariants@current",
- ],
+ native_shared_libs: ["mysdk_sslvariants@current"],
target: {
host: {
enabled: false,
diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go
index f3c442e..3d16073 100644
--- a/ui/build/dumpvars.go
+++ b/ui/build/dumpvars.go
@@ -261,6 +261,12 @@
"BUILD_BROKEN_USES_BUILD_STATIC_LIBRARY",
}, exportEnvVars...), BannerVars...)
+ // We need Roboleaf converter and runner in the mixed mode
+ runMicrofactory(ctx, config, ".bootstrap/bin/mk2rbc", "android/soong/mk2rbc/cmd",
+ map[string]string{"android/soong": "build/soong"})
+ runMicrofactory(ctx, config, ".bootstrap/bin/rbcrun", "rbcrun/cmd",
+ map[string]string{"go.starlark.net": "external/starlark-go", "rbcrun": "build/make/tools/rbcrun"})
+
makeVars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true, "")
if err != nil {
ctx.Fatalln("Error dumping make vars:", err)
diff --git a/ui/build/soong.go b/ui/build/soong.go
index 02e7375..190c955 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -265,20 +265,8 @@
}
}()
- var cfg microfactory.Config
- cfg.Map("github.com/google/blueprint", "build/blueprint")
-
- cfg.TrimPath = absPath(ctx, ".")
-
- func() {
- ctx.BeginTrace(metrics.RunSoong, "bpglob")
- defer ctx.EndTrace()
-
- bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob")
- if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil {
- ctx.Fatalln("Failed to build bpglob:", err)
- }
- }()
+ runMicrofactory(ctx, config, ".minibootstrap/bpglob", "github.com/google/blueprint/bootstrap/bpglob",
+ map[string]string{"github.com/google/blueprint": "build/blueprint"})
ninja := func(name, file string) {
ctx.BeginTrace(metrics.RunSoong, name)
@@ -332,6 +320,25 @@
}
}
+func runMicrofactory(ctx Context, config Config, relExePath string, pkg string, mapping map[string]string) {
+ name := filepath.Base(relExePath)
+ ctx.BeginTrace(metrics.RunSoong, name)
+ defer ctx.EndTrace()
+ cfg := microfactory.Config{TrimPath: absPath(ctx, ".")}
+ for pkgPrefix, pathPrefix := range mapping {
+ cfg.Map(pkgPrefix, pathPrefix)
+ }
+
+ exePath := filepath.Join(config.SoongOutDir(), relExePath)
+ dir := filepath.Dir(exePath)
+ if err := os.MkdirAll(dir, 0777); err != nil {
+ ctx.Fatalf("cannot create %s: %s", dir, err)
+ }
+ if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil {
+ ctx.Fatalf("failed to build %s: %s", name, err)
+ }
+}
+
func shouldCollectBuildSoongMetrics(config Config) bool {
// Do not collect metrics protobuf if the soong_build binary ran as the
// bp2build converter or the JSON graph dump.