Merge "Disable lint checks which do not apply in the platform"
diff --git a/OWNERS b/OWNERS
index bbfd011..f15bd32 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,17 +1,22 @@
 # This file is included by several other projects as the list of people
 # approving build related projects.
 
+# AMER
 ahumesky@google.com
 asmundak@google.com
 ccross@android.com
 cparsons@google.com
 dwillemsen@google.com
 eakammer@google.com
-jingwen@google.com
 joeo@google.com
-lberki@google.com
+spandandas@google.com
+yuntaoxu@google.com
+
+# APAC
+jingwen@google.com
 ruperts@google.com
 
-# To expedite LON reviews
+# EMEA
 hansson@google.com
+lberki@google.com
 paulduffin@google.com
diff --git a/android/arch.go b/android/arch.go
index 340f136..7ca7336 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -15,13 +15,14 @@
 package android
 
 import (
-	"android/soong/bazel"
 	"encoding"
 	"fmt"
 	"reflect"
 	"runtime"
 	"strings"
 
+	"android/soong/bazel"
+
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/bootstrap"
 	"github.com/google/blueprint/proptools"
@@ -254,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.
@@ -290,28 +291,6 @@
 	return NoOsType
 }
 
-// BuildOs returns the OsType for the OS that the build is running on.
-var BuildOs = func() OsType {
-	switch runtime.GOOS {
-	case "linux":
-		return Linux
-	case "darwin":
-		return Darwin
-	default:
-		panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
-	}
-}()
-
-// BuildArch returns the ArchType for the CPU that the build is running on.
-var BuildArch = func() ArchType {
-	switch runtime.GOARCH {
-	case "amd64":
-		return X86_64
-	default:
-		panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
-	}
-}()
-
 var (
 	// osTypeList contains a list of all the supported OsTypes, including ones not supported
 	// by the current build host or the target device.
@@ -326,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
@@ -336,8 +317,6 @@
 	// Android is the OS for target devices that run all of Android, including the Linux kernel
 	// and the Bionic libc runtime.
 	Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
-	// Fuchsia is the OS for target devices that run Fuchsia.
-	Fuchsia = newOsType("fuchsia", Device, false, Arm64, X86_64)
 
 	// CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
 	// has dependencies on all the OS variants.
@@ -886,6 +865,8 @@
 			"Android64",
 			"Android32",
 			"Bionic",
+			"Glibc",
+			"Musl",
 			"Linux",
 			"Not_windows",
 			"Arm_on_x86",
@@ -1131,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: {
@@ -1333,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:
@@ -1397,6 +1412,34 @@
 	}
 }
 
+// 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 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
+		default:
+			panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
+		}
+	}()
+
+	config.BuildArch = func() ArchType {
+		switch runtime.GOARCH {
+		case "amd64":
+			return X86_64
+		default:
+			panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
+		}
+	}()
+
+}
+
 // Convert the arch product variables into a list of targets for each OsType.
 func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
 	variables := config.productVariables
@@ -1430,9 +1473,9 @@
 		hostCross := false
 		if os.Class == Host {
 			var osSupported bool
-			if os == BuildOs {
+			if os == config.BuildOS {
 				osSupported = true
-			} else if BuildOs.Linux() && os.Linux() {
+			} else if config.BuildOS.Linux() && os.Linux() {
 				// LinuxBionic and Linux are compatible
 				osSupported = true
 			} else {
@@ -1470,11 +1513,11 @@
 	}
 
 	// The primary host target, which must always exist.
-	addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
+	addTarget(config.BuildOS, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
 
 	// An optional secondary host target.
 	if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
-		addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
+		addTarget(config.BuildOS, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
 	}
 
 	// Optional cross-compiled host targets, generally Windows.
@@ -1499,13 +1542,8 @@
 
 	// Optional device targets
 	if variables.DeviceArch != nil && *variables.DeviceArch != "" {
-		var target = Android
-		if Bool(variables.Fuchsia) {
-			target = Fuchsia
-		}
-
 		// The primary device target.
-		addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
+		addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
 			variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
 
 		// An optional secondary device target.
@@ -1987,6 +2025,10 @@
 	axisToProps[bazel.OsConfigurationAxis] = osToProp
 	axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
 
+	axisToProps[bazel.BionicConfigurationAxis] = map[string]interface{}{
+		"bionic": getTargetStruct(ctx, propertySet, archProperties, "Bionic"),
+	}
+
 	return axisToProps
 }
 
diff --git a/android/arch_test.go b/android/arch_test.go
index 3aa4779..a828321 100644
--- a/android/arch_test.go
+++ b/android/arch_test.go
@@ -473,3 +473,186 @@
 		})
 	}
 }
+
+type testArchPropertiesModule struct {
+	ModuleBase
+	properties struct {
+		A []string `android:"arch_variant"`
+	}
+}
+
+func (testArchPropertiesModule) GenerateAndroidBuildActions(ctx ModuleContext) {}
+
+func TestArchProperties(t *testing.T) {
+	bp := `
+		module {
+			name: "foo",
+			a: ["root"],
+			arch: {
+				arm: {
+					a:  ["arm"],
+					armv7_a_neon: { a: ["armv7_a_neon"] },
+				},
+				arm64: {
+					a:  ["arm64"],
+					armv8_a: { a: ["armv8_a"] },
+				},
+				x86: { a:  ["x86"] },
+				x86_64: { a:  ["x86_64"] },
+			},
+			multilib: {
+				lib32: { a:  ["lib32"] },
+				lib64: { a:  ["lib64"] },
+			},
+			target: {
+				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"] },
+				android32: { a:  ["android32"] },
+				android64: { a:  ["android64"] },
+				android_arm: { a:  ["android_arm"] },
+				android_arm64: { a:  ["android_arm64"] },
+				linux_x86: { a:  ["linux_x86"] },
+				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"] },
+			},
+		}
+	`
+
+	type result struct {
+		module   string
+		variant  string
+		property []string
+	}
+
+	testCases := []struct {
+		name     string
+		goOS     string
+		preparer FixturePreparer
+		results  []result
+	}{
+		{
+			name: "default",
+			results: []result{
+				{
+					module:   "foo",
+					variant:  "android_arm64_armv8-a",
+					property: []string{"root", "linux", "bionic", "android", "android64", "arm64", "armv8_a", "lib64", "android_arm64"},
+				},
+				{
+					module:   "foo",
+					variant:  "android_arm_armv7-a-neon",
+					property: []string{"root", "linux", "bionic", "android", "android64", "arm", "armv7_a_neon", "lib32", "android_arm"},
+				},
+			},
+		},
+		{
+			name: "linux",
+			goOS: "linux",
+			results: []result{
+				{
+					module:   "foo",
+					variant:  "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", "glibc", "linux_glibc", "not_windows", "x86", "lib32", "linux_x86", "linux_glibc_x86"},
+				},
+			},
+		},
+		{
+			name: "windows",
+			goOS: "linux",
+			preparer: FixtureModifyConfig(func(config Config) {
+				config.Targets[Windows] = []Target{
+					{Windows, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", true},
+					{Windows, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", true},
+				}
+			}),
+			results: []result{
+				{
+					module:   "foo",
+					variant:  "windows_x86_64",
+					property: []string{"root", "host", "windows", "x86_64", "lib64", "windows_x86_64"},
+				},
+				{
+					module:   "foo",
+					variant:  "windows_x86",
+					property: []string{"root", "host", "windows", "x86", "lib32", "windows_x86"},
+				},
+			},
+		},
+		{
+			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{
+				{
+					module:   "foo",
+					variant:  "darwin_x86_64",
+					property: []string{"root", "host", "darwin", "not_windows", "x86_64", "lib64", "darwin_x86_64"},
+				},
+			},
+		},
+	}
+
+	for _, tt := range testCases {
+		t.Run(tt.name, func(t *testing.T) {
+			if tt.goOS != "" && tt.goOS != runtime.GOOS {
+				t.Skipf("test requires runtime.GOOS==%s, got %s", tt.goOS, runtime.GOOS)
+			}
+			result := GroupFixturePreparers(
+				PrepareForTestWithArchMutator,
+				OptionalFixturePreparer(tt.preparer),
+				FixtureRegisterWithContext(func(ctx RegistrationContext) {
+					ctx.RegisterModuleType("module", func() Module {
+						module := &testArchPropertiesModule{}
+						module.AddProperties(&module.properties)
+						InitAndroidArchModule(module, HostAndDeviceDefault, MultilibBoth)
+						return module
+					})
+				}),
+			).RunTestWithBp(t, bp)
+
+			for _, want := range tt.results {
+				t.Run(want.module+"_"+want.variant, func(t *testing.T) {
+					got := result.ModuleForTests(want.module, want.variant).Module().(*testArchPropertiesModule).properties.A
+					AssertArrayString(t, "arch mutator property", want.property, got)
+				})
+			}
+		})
+	}
+}
diff --git a/android/bazel.go b/android/bazel.go
index 8d13762..d40e650 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -129,7 +129,7 @@
 	// Keep any existing BUILD files (and do not generate new BUILD files) for these directories
 	bp2buildKeepExistingBuildFile = map[string]bool{
 		// This is actually build/bazel/build.BAZEL symlinked to ./BUILD
-		".":/*recrusive = */ false,
+		".":/*recursive = */ false,
 
 		"build/bazel":/* recursive = */ true,
 		"build/pesto":/* recursive = */ true,
@@ -140,13 +140,15 @@
 
 		"prebuilts/sdk":/* recursive = */ false,
 		"prebuilts/sdk/tools":/* recursive = */ false,
+		"packages/apps/Music":/* recursive = */ false,
 	}
 
 	// Configure modules in these directories to enable bp2build_available: true or false by default.
 	bp2buildDefaultConfig = Bp2BuildConfig{
-		"bionic":                Bp2BuildDefaultTrueRecursively,
-		"external/gwp_asan":     Bp2BuildDefaultTrueRecursively,
-		"system/core/libcutils": Bp2BuildDefaultTrueRecursively,
+		"bionic":                            Bp2BuildDefaultTrueRecursively,
+		"build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
+		"external/gwp_asan":                 Bp2BuildDefaultTrueRecursively,
+		"system/core/libcutils":             Bp2BuildDefaultTrueRecursively,
 		"system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
 		"system/libbase":                  Bp2BuildDefaultTrueRecursively,
 		"system/logging/liblog":           Bp2BuildDefaultTrueRecursively,
diff --git a/android/bazel_paths.go b/android/bazel_paths.go
index f74fed1..26cacdb 100644
--- a/android/bazel_paths.go
+++ b/android/bazel_paths.go
@@ -73,6 +73,7 @@
 	EarlyModulePathContext
 
 	GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
+	ModuleFromName(name string) (blueprint.Module, bool)
 	Module() Module
 	ModuleType() string
 	OtherModuleName(m blueprint.Module) string
@@ -331,11 +332,9 @@
 // module. The label will be relative to the current directory if appropriate. The dependency must
 // already be resolved by either deps mutator or path deps mutator.
 func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string, isWholeLibs bool) bazel.Label {
-	m, _ := ctx.GetDirectDep(dep)
+	m, _ := ctx.ModuleFromName(dep)
 	if m == nil {
-		panic(fmt.Errorf(`Cannot get direct dep %q of %q.
-		This is likely because it was not added via AddDependency().
-		This may be due a mutator skipped during bp2build.`, dep, ctx.Module().Name()))
+		panic(fmt.Errorf("No module named %q found, but was a direct dep of %q", dep, ctx.Module().Name()))
 	}
 	otherLabel := bazelModuleLabel(ctx, m, tag)
 	label := bazelModuleLabel(ctx, ctx.Module(), "")
diff --git a/android/config.go b/android/config.go
index 396b1a6..9addf5d 100644
--- a/android/config.go
+++ b/android/config.go
@@ -108,6 +108,12 @@
 
 	ProductVariablesFileName string
 
+	// BuildOS stores the OsType for the OS that the build is running on.
+	BuildOS OsType
+
+	// BuildArch stores the ArchType for the CPU that the build is running on.
+	BuildArch ArchType
+
 	Targets                  map[OsType][]Target
 	BuildOSTarget            Target // the Target for tools run on the build machine
 	BuildOSCommonTarget      Target // the Target for common (java) tools run on the build machine
@@ -326,41 +332,28 @@
 	return Config{config}
 }
 
-func fuchsiaTargets() map[OsType][]Target {
-	return map[OsType][]Target{
-		Fuchsia: {
-			{Fuchsia, Arch{ArchType: Arm64, ArchVariant: "", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
-		},
-		BuildOs: {
-			{BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
-		},
-	}
-}
-
-var PrepareForTestSetDeviceToFuchsia = FixtureModifyConfig(func(config Config) {
-	config.Targets = fuchsiaTargets()
-})
-
 func modifyTestConfigToSupportArchMutator(testConfig Config) {
 	config := testConfig.config
 
+	determineBuildOS(config)
+
 	config.Targets = map[OsType][]Target{
 		Android: []Target{
 			{Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
 			{Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
 		},
-		BuildOs: []Target{
-			{BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
-			{BuildOs, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
+		config.BuildOS: []Target{
+			{config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
+			{config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
 		},
 	}
 
 	if runtime.GOOS == "darwin" {
-		config.Targets[BuildOs] = config.Targets[BuildOs][:1]
+		config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
 	}
 
-	config.BuildOSTarget = config.Targets[BuildOs][0]
-	config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
+	config.BuildOSTarget = config.Targets[config.BuildOS][0]
+	config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
 	config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
 	config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
 	config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
@@ -369,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 {
@@ -439,6 +445,8 @@
 		config.katiEnabled = true
 	}
 
+	determineBuildOS(config)
+
 	// Sets up the map of target OSes to the finer grained compilation targets
 	// that are configured from the product variables.
 	targets, err := decodeTargetProductVariables(config)
@@ -476,8 +484,8 @@
 	config.Targets = targets
 
 	// Compilation targets for host tools.
-	config.BuildOSTarget = config.Targets[BuildOs][0]
-	config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
+	config.BuildOSTarget = config.Targets[config.BuildOS][0]
+	config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
 
 	// Compilation targets for Android.
 	if len(config.Targets[Android]) > 0 {
@@ -837,10 +845,6 @@
 	return Bool(c.productVariables.Skip_boot_jars_check)
 }
 
-func (c *config) Fuchsia() bool {
-	return Bool(c.productVariables.Fuchsia)
-}
-
 func (c *config) MinimizeJavaDebugInfo() bool {
 	return Bool(c.productVariables.MinimizeJavaDebugInfo) && !Bool(c.productVariables.Eng)
 }
diff --git a/android/module.go b/android/module.go
index 11f63bd..84e78d1 100644
--- a/android/module.go
+++ b/android/module.go
@@ -223,6 +223,8 @@
 	// the first DependencyTag.
 	GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
 
+	ModuleFromName(name string) (blueprint.Module, bool)
+
 	// VisitDirectDepsBlueprint calls visit for each direct dependency.  If there are multiple
 	// direct dependencies on the same module visit will be called multiple times on that module
 	// and OtherModuleDependencyTag will return a different tag for each.
@@ -325,7 +327,6 @@
 	Host() bool
 	Device() bool
 	Darwin() bool
-	Fuchsia() bool
 	Windows() bool
 	Debug() bool
 	PrimaryArch() bool
@@ -413,6 +414,7 @@
 	InstallInDebugRamdisk() bool
 	InstallInRecovery() bool
 	InstallInRoot() bool
+	InstallInVendor() bool
 	InstallBypassMake() bool
 	InstallForceOS() (*OsType, *ArchType)
 
@@ -471,6 +473,7 @@
 	InstallInDebugRamdisk() bool
 	InstallInRecovery() bool
 	InstallInRoot() bool
+	InstallInVendor() bool
 	InstallBypassMake() bool
 	InstallForceOS() (*OsType, *ArchType)
 	HideFromMake()
@@ -1579,6 +1582,10 @@
 	return Bool(m.commonProperties.Recovery)
 }
 
+func (m *ModuleBase) InstallInVendor() bool {
+	return Bool(m.commonProperties.Vendor)
+}
+
 func (m *ModuleBase) InstallInRoot() bool {
 	return false
 }
@@ -2032,8 +2039,13 @@
 	tagPath  []blueprint.DependencyTag
 
 	strictVisitDeps bool // If true, enforce that all dependencies are enabled
+
+	bazelConversionMode bool
 }
 
+func (b *baseModuleContext) BazelConversionMode() bool {
+	return b.bazelConversionMode
+}
 func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
 	return b.bp.OtherModuleName(m)
 }
@@ -2373,6 +2385,17 @@
 	return b.getDirectDepFirstTag(name)
 }
 
+func (b *baseModuleContext) ModuleFromName(name string) (blueprint.Module, bool) {
+	if !b.BazelConversionMode() {
+		panic("cannot call ModuleFromName if not in bazel conversion mode")
+	}
+	if moduleName, _ := SrcIsModuleWithTag(name); moduleName != "" {
+		return b.bp.ModuleFromName(moduleName)
+	} else {
+		return b.bp.ModuleFromName(name)
+	}
+}
+
 func (b *baseModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
 	b.bp.VisitDirectDeps(visit)
 }
@@ -2563,10 +2586,6 @@
 	return b.os == Darwin
 }
 
-func (b *baseModuleContext) Fuchsia() bool {
-	return b.os == Fuchsia
-}
-
 func (b *baseModuleContext) Windows() bool {
 	return b.os == Windows
 }
@@ -2645,6 +2664,10 @@
 	return m.module.InstallForceOS()
 }
 
+func (m *moduleContext) InstallInVendor() bool {
+	return m.module.InstallInVendor()
+}
+
 func (m *moduleContext) skipInstall() bool {
 	if m.module.base().commonProperties.SkipInstall {
 		return true
diff --git a/android/mutator.go b/android/mutator.go
index 819dd0f..d895669 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -35,7 +35,7 @@
 //   continue on to GenerateAndroidBuildActions
 
 // RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
-func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, depsMutators, bp2buildMutators []RegisterMutatorFunc) {
+func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators, bp2buildMutators []RegisterMutatorFunc) {
 	mctx := &registerMutatorsContext{
 		bazelConversionMode: true,
 	}
@@ -53,16 +53,6 @@
 		f(mctx)
 	}
 
-	bp2buildDepsMutators = append([]RegisterMutatorFunc{
-		registerDepsMutatorBp2Build,
-		registerPathDepsMutator,
-		registerBp2buildArchPathDepsMutator,
-	}, depsMutators...)
-
-	for _, f := range bp2buildDepsMutators {
-		f(mctx)
-	}
-
 	// Register bp2build mutators
 	for _, f := range bp2buildMutators {
 		f(mctx)
@@ -227,7 +217,6 @@
 }
 
 var bp2buildPreArchMutators = []RegisterMutatorFunc{}
-var bp2buildDepsMutators = []RegisterMutatorFunc{}
 var bp2buildMutators = map[string]RegisterMutatorFunc{}
 
 // See http://b/192523357
@@ -254,12 +243,6 @@
 	bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
 }
 
-// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
-// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
-func DepsBp2BuildMutators(f RegisterMutatorFunc) {
-	bp2buildDepsMutators = append(bp2buildDepsMutators, f)
-}
-
 type BaseMutatorContext interface {
 	BaseModuleContext
 
@@ -269,6 +252,9 @@
 	// Rename all variants of a module.  The new name is not visible to calls to ModuleName,
 	// AddDependency or OtherModuleName until after this mutator pass is complete.
 	Rename(name string)
+
+	// BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
+	BazelConversionMode() bool
 }
 
 type TopDownMutator func(TopDownMutatorContext)
@@ -410,26 +396,24 @@
 	// variant of the current module.  The value should not be modified after being passed to
 	// SetVariationProvider.
 	SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
-
-	// BazelConversionMode returns whether this mutator is being run as part of Bazel Conversion.
-	BazelConversionMode() bool
 }
 
 type bottomUpMutatorContext struct {
 	bp blueprint.BottomUpMutatorContext
 	baseModuleContext
-	finalPhase          bool
-	bazelConversionMode bool
+	finalPhase bool
 }
 
 func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
 	finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
 
+	moduleContext := a.base().baseModuleContextFactory(ctx)
+	moduleContext.bazelConversionMode = bazelConversionMode
+
 	return &bottomUpMutatorContext{
-		bp:                  ctx,
-		baseModuleContext:   a.base().baseModuleContextFactory(ctx),
-		finalPhase:          finalPhase,
-		bazelConversionMode: bazelConversionMode,
+		bp:                ctx,
+		baseModuleContext: a.base().baseModuleContextFactory(ctx),
+		finalPhase:        finalPhase,
 	}
 }
 
@@ -462,9 +446,11 @@
 func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
 	f := func(ctx blueprint.TopDownMutatorContext) {
 		if a, ok := ctx.Module().(Module); ok {
+			moduleContext := a.base().baseModuleContextFactory(ctx)
+			moduleContext.bazelConversionMode = x.bazelConversionMode
 			actx := &topDownMutatorContext{
 				bp:                ctx,
-				baseModuleContext: a.base().baseModuleContextFactory(ctx),
+				baseModuleContext: moduleContext,
 			}
 			m(actx)
 		}
@@ -733,7 +719,3 @@
 func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
 	b.bp.SetVariationProvider(module, provider, value)
 }
-
-func (b *bottomUpMutatorContext) BazelConversionMode() bool {
-	return b.bazelConversionMode
-}
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/prebuilt_build_tool.go b/android/prebuilt_build_tool.go
index 516d042..e5edf91 100644
--- a/android/prebuilt_build_tool.go
+++ b/android/prebuilt_build_tool.go
@@ -86,7 +86,7 @@
 
 func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) {
 	if makeVar := String(t.properties.Export_to_make_var); makeVar != "" {
-		if t.Target().Os != BuildOs {
+		if t.Target().Os != ctx.Config().BuildOS {
 			return
 		}
 		ctx.StrictRaw(makeVar, t.toolPath.String())
diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go
index dcd77ea..a1f8e63 100644
--- a/android/prebuilt_test.go
+++ b/android/prebuilt_test.go
@@ -21,360 +21,362 @@
 	"github.com/google/blueprint"
 )
 
-var prebuiltsTests = []struct {
-	name      string
-	replaceBp bool // modules is added to default bp boilerplate if false.
-	modules   string
-	prebuilt  []OsType
-	preparer  FixturePreparer
-}{
-	{
-		name: "no prebuilt",
-		modules: `
-			source {
-				name: "bar",
-			}`,
-		prebuilt: nil,
-	},
-	{
-		name: "no source prebuilt not preferred",
-		modules: `
-			prebuilt {
-				name: "bar",
-				prefer: false,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "no source prebuilt preferred",
-		modules: `
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "prebuilt not preferred",
-		modules: `
-			source {
-				name: "bar",
-			}
+func TestPrebuilts(t *testing.T) {
+	buildOS := TestArchConfig(t.TempDir(), nil, "", nil).BuildOS
 
-			prebuilt {
-				name: "bar",
-				prefer: false,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: nil,
-	},
-	{
-		name: "prebuilt preferred",
-		modules: `
-			source {
-				name: "bar",
-			}
+	var prebuiltsTests = []struct {
+		name      string
+		replaceBp bool // modules is added to default bp boilerplate if false.
+		modules   string
+		prebuilt  []OsType
+		preparer  FixturePreparer
+	}{
+		{
+			name: "no prebuilt",
+			modules: `
+				source {
+					name: "bar",
+				}`,
+			prebuilt: nil,
+		},
+		{
+			name: "no source prebuilt not preferred",
+			modules: `
+				prebuilt {
+					name: "bar",
+					prefer: false,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "no source prebuilt preferred",
+			modules: `
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "prebuilt not preferred",
+			modules: `
+				source {
+					name: "bar",
+				}
 
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "prebuilt no file not preferred",
-		modules: `
-			source {
-				name: "bar",
-			}
+				prebuilt {
+					name: "bar",
+					prefer: false,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: nil,
+		},
+		{
+			name: "prebuilt preferred",
+			modules: `
+				source {
+					name: "bar",
+				}
 
-			prebuilt {
-				name: "bar",
-				prefer: false,
-			}`,
-		prebuilt: nil,
-	},
-	{
-		name: "prebuilt no file preferred",
-		modules: `
-			source {
-				name: "bar",
-			}
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "prebuilt no file not preferred",
+			modules: `
+				source {
+					name: "bar",
+				}
 
-			prebuilt {
-				name: "bar",
-				prefer: true,
-			}`,
-		prebuilt: nil,
-	},
-	{
-		name: "prebuilt file from filegroup preferred",
-		modules: `
-			filegroup {
-				name: "fg",
-				srcs: ["prebuilt_file"],
-			}
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				srcs: [":fg"],
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "prebuilt module for device only",
-		modules: `
-			source {
-				name: "bar",
-			}
+				prebuilt {
+					name: "bar",
+					prefer: false,
+				}`,
+			prebuilt: nil,
+		},
+		{
+			name: "prebuilt no file preferred",
+			modules: `
+				source {
+					name: "bar",
+				}
 
-			prebuilt {
-				name: "bar",
-				host_supported: false,
-				prefer: true,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: []OsType{Android},
-	},
-	{
-		name: "prebuilt file for host only",
-		modules: `
-			source {
-				name: "bar",
-			}
+				prebuilt {
+					name: "bar",
+					prefer: true,
+				}`,
+			prebuilt: nil,
+		},
+		{
+			name: "prebuilt file from filegroup preferred",
+			modules: `
+				filegroup {
+					name: "fg",
+					srcs: ["prebuilt_file"],
+				}
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					srcs: [":fg"],
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "prebuilt module for device only",
+			modules: `
+				source {
+					name: "bar",
+				}
 
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				target: {
-					host: {
-						srcs: ["prebuilt_file"],
-					},
-				},
-			}`,
-		prebuilt: []OsType{BuildOs},
-	},
-	{
-		name: "prebuilt override not preferred",
-		modules: `
-			source {
-				name: "baz",
-			}
+				prebuilt {
+					name: "bar",
+					host_supported: false,
+					prefer: true,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: []OsType{Android},
+		},
+		{
+			name: "prebuilt file for host only",
+			modules: `
+				source {
+					name: "bar",
+				}
 
-			override_source {
-				name: "bar",
-				base: "baz",
-			}
-
-			prebuilt {
-				name: "bar",
-				prefer: false,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: nil,
-	},
-	{
-		name: "prebuilt override preferred",
-		modules: `
-			source {
-				name: "baz",
-			}
-
-			override_source {
-				name: "bar",
-				base: "baz",
-			}
-
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name:      "prebuilt including default-disabled OS",
-		replaceBp: true,
-		modules: `
-			source {
-				name: "foo",
-				deps: [":bar"],
-				target: {
-					windows: {
-						enabled: true,
-					},
-				},
-			}
-
-			source {
-				name: "bar",
-				target: {
-					windows: {
-						enabled: true,
-					},
-				},
-			}
-
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				srcs: ["prebuilt_file"],
-				target: {
-					windows: {
-						enabled: true,
-					},
-				},
-			}`,
-		prebuilt: []OsType{Android, BuildOs, Windows},
-	},
-	{
-		name:      "fall back to source for default-disabled OS",
-		replaceBp: true,
-		modules: `
-			source {
-				name: "foo",
-				deps: [":bar"],
-				target: {
-					windows: {
-						enabled: true,
-					},
-				},
-			}
-
-			source {
-				name: "bar",
-				target: {
-					windows: {
-						enabled: true,
-					},
-				},
-			}
-
-			prebuilt {
-				name: "bar",
-				prefer: true,
-				srcs: ["prebuilt_file"],
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name:      "prebuilt properties customizable",
-		replaceBp: true,
-		modules: `
-			source {
-				name: "foo",
-				deps: [":bar"],
-			}
-
-			soong_config_module_type {
-				name: "prebuilt_with_config",
-				module_type: "prebuilt",
-				config_namespace: "any_namespace",
-				bool_variables: ["bool_var"],
-				properties: ["prefer"],
-			}
-
-			prebuilt_with_config {
-				name: "bar",
-				prefer: true,
-				srcs: ["prebuilt_file"],
-				soong_config_variables: {
-					bool_var: {
-						prefer: false,
-						conditions_default: {
-							prefer: true,
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					target: {
+						host: {
+							srcs: ["prebuilt_file"],
 						},
 					},
-				},
-			}`,
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "prebuilt use_source_config_var={acme, use_source} - no var specified",
-		modules: `
-			source {
-				name: "bar",
-			}
+				}`,
+			prebuilt: []OsType{buildOS},
+		},
+		{
+			name: "prebuilt override not preferred",
+			modules: `
+				source {
+					name: "baz",
+				}
 
-			prebuilt {
-				name: "bar",
-				use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
-				srcs: ["prebuilt_file"],
-			}`,
-		// When use_source_env is specified then it will use the prebuilt by default if the environment
-		// variable is not set.
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=false",
-		modules: `
-			source {
-				name: "bar",
-			}
+				override_source {
+					name: "bar",
+					base: "baz",
+				}
 
-			prebuilt {
-				name: "bar",
-				use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
-				srcs: ["prebuilt_file"],
-			}`,
-		preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) {
-			variables.VendorVars = map[string]map[string]string{
-				"acme": {
-					"use_source": "false",
-				},
-			}
-		}),
-		// Setting the environment variable named in use_source_env to false will cause the prebuilt to
-		// be used.
-		prebuilt: []OsType{Android, BuildOs},
-	},
-	{
-		name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=true",
-		modules: `
-			source {
-				name: "bar",
-			}
+				prebuilt {
+					name: "bar",
+					prefer: false,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: nil,
+		},
+		{
+			name: "prebuilt override preferred",
+			modules: `
+				source {
+					name: "baz",
+				}
 
-			prebuilt {
-				name: "bar",
-				use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
-				srcs: ["prebuilt_file"],
-			}`,
-		preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) {
-			variables.VendorVars = map[string]map[string]string{
-				"acme": {
-					"use_source": "true",
-				},
-			}
-		}),
-		// Setting the environment variable named in use_source_env to true will cause the source to be
-		// used.
-		prebuilt: nil,
-	},
-	{
-		name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=true, no source",
-		modules: `
-			prebuilt {
-				name: "bar",
-				use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
-				srcs: ["prebuilt_file"],
-			}`,
-		preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) {
-			variables.VendorVars = map[string]map[string]string{
-				"acme": {
-					"use_source": "true",
-				},
-			}
-		}),
-		// Although the environment variable says to use source there is no source available.
-		prebuilt: []OsType{Android, BuildOs},
-	},
-}
+				override_source {
+					name: "bar",
+					base: "baz",
+				}
 
-func TestPrebuilts(t *testing.T) {
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name:      "prebuilt including default-disabled OS",
+			replaceBp: true,
+			modules: `
+				source {
+					name: "foo",
+					deps: [":bar"],
+					target: {
+						windows: {
+							enabled: true,
+						},
+					},
+				}
+
+				source {
+					name: "bar",
+					target: {
+						windows: {
+							enabled: true,
+						},
+					},
+				}
+
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					srcs: ["prebuilt_file"],
+					target: {
+						windows: {
+							enabled: true,
+						},
+					},
+				}`,
+			prebuilt: []OsType{Android, buildOS, Windows},
+		},
+		{
+			name:      "fall back to source for default-disabled OS",
+			replaceBp: true,
+			modules: `
+				source {
+					name: "foo",
+					deps: [":bar"],
+					target: {
+						windows: {
+							enabled: true,
+						},
+					},
+				}
+
+				source {
+					name: "bar",
+					target: {
+						windows: {
+							enabled: true,
+						},
+					},
+				}
+
+				prebuilt {
+					name: "bar",
+					prefer: true,
+					srcs: ["prebuilt_file"],
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name:      "prebuilt properties customizable",
+			replaceBp: true,
+			modules: `
+				source {
+					name: "foo",
+					deps: [":bar"],
+				}
+
+				soong_config_module_type {
+					name: "prebuilt_with_config",
+					module_type: "prebuilt",
+					config_namespace: "any_namespace",
+					bool_variables: ["bool_var"],
+					properties: ["prefer"],
+				}
+
+				prebuilt_with_config {
+					name: "bar",
+					prefer: true,
+					srcs: ["prebuilt_file"],
+					soong_config_variables: {
+						bool_var: {
+							prefer: false,
+							conditions_default: {
+								prefer: true,
+							},
+						},
+					},
+				}`,
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "prebuilt use_source_config_var={acme, use_source} - no var specified",
+			modules: `
+				source {
+					name: "bar",
+				}
+
+				prebuilt {
+					name: "bar",
+					use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
+					srcs: ["prebuilt_file"],
+				}`,
+			// When use_source_env is specified then it will use the prebuilt by default if the environment
+			// variable is not set.
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=false",
+			modules: `
+				source {
+					name: "bar",
+				}
+
+				prebuilt {
+					name: "bar",
+					use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
+					srcs: ["prebuilt_file"],
+				}`,
+			preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) {
+				variables.VendorVars = map[string]map[string]string{
+					"acme": {
+						"use_source": "false",
+					},
+				}
+			}),
+			// Setting the environment variable named in use_source_env to false will cause the prebuilt to
+			// be used.
+			prebuilt: []OsType{Android, buildOS},
+		},
+		{
+			name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=true",
+			modules: `
+				source {
+					name: "bar",
+				}
+
+				prebuilt {
+					name: "bar",
+					use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
+					srcs: ["prebuilt_file"],
+				}`,
+			preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) {
+				variables.VendorVars = map[string]map[string]string{
+					"acme": {
+						"use_source": "true",
+					},
+				}
+			}),
+			// Setting the environment variable named in use_source_env to true will cause the source to be
+			// used.
+			prebuilt: nil,
+		},
+		{
+			name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=true, no source",
+			modules: `
+				prebuilt {
+					name: "bar",
+					use_source_config_var: {config_namespace: "acme", var_name: "use_source"},
+					srcs: ["prebuilt_file"],
+				}`,
+			preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) {
+				variables.VendorVars = map[string]map[string]string{
+					"acme": {
+						"use_source": "true",
+					},
+				}
+			}),
+			// Although the environment variable says to use source there is no source available.
+			prebuilt: []OsType{Android, buildOS},
+		},
+	}
+
 	fs := MockFS{
 		"prebuilt_file": nil,
 		"source_file":   nil,
diff --git a/android/register.go b/android/register.go
index 4c8088d..5984862 100644
--- a/android/register.go
+++ b/android/register.go
@@ -180,7 +180,7 @@
 		bp2buildMutatorList = append(bp2buildMutatorList, f)
 	}
 
-	RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators, bp2buildDepsMutators, bp2buildMutatorList)
+	RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators, bp2buildMutatorList)
 }
 
 // Register the pipeline of singletons, module types, and mutators for
diff --git a/android/sdk.go b/android/sdk.go
index e700031..da740f3 100644
--- a/android/sdk.go
+++ b/android/sdk.go
@@ -239,6 +239,12 @@
 	// to the zip
 	CopyToSnapshot(src Path, dest string)
 
+	// Return the path to an empty file.
+	//
+	// This can be used by sdk member types that need to create an empty file in the snapshot, simply
+	// pass the value returned from this to the CopyToSnapshot() method.
+	EmptyFile() Path
+
 	// Unzip the supplied zip into the snapshot relative directory destDir.
 	UnzipToSnapshot(zipPath Path, destDir string)
 
diff --git a/android/test_suites.go b/android/test_suites.go
index 6b7b909..22f6cf2 100644
--- a/android/test_suites.go
+++ b/android/test_suites.go
@@ -60,7 +60,7 @@
 	for _, module := range SortedStringKeys(files) {
 		installedPaths = append(installedPaths, files[module]...)
 	}
-	testCasesDir := pathForInstall(ctx, BuildOs, X86, "testcases", false).ToMakePath()
+	testCasesDir := pathForInstall(ctx, ctx.Config().BuildOS, X86, "testcases", false).ToMakePath()
 
 	outputFile := PathForOutput(ctx, "packaging", "robolectric-tests.zip")
 	rule := NewRuleBuilder(pctx, ctx)
diff --git a/android/testing.go b/android/testing.go
index 17a812e..6ba8e3c 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -171,9 +171,9 @@
 
 type TestContext struct {
 	*Context
-	preArch, preDeps, postDeps, finalDeps           []RegisterMutatorFunc
-	bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc
-	NameResolver                                    *NameResolver
+	preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
+	bp2buildPreArch, bp2buildMutators     []RegisterMutatorFunc
+	NameResolver                          *NameResolver
 
 	// The list of pre-singletons and singletons registered for the test.
 	preSingletons, singletons sortableComponents
@@ -224,12 +224,6 @@
 	ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f)
 }
 
-// DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
-// Bazel BUILD targets that should run prior to conversion to resolve dependencies.
-func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) {
-	ctx.bp2buildDeps = append(ctx.bp2buildDeps, f)
-}
-
 // registeredComponentOrder defines the order in which a sortableComponent type is registered at
 // runtime and provides support for reordering the components registered for a test in the same
 // way.
@@ -464,7 +458,7 @@
 
 // RegisterForBazelConversion prepares a test context for bp2build conversion.
 func (ctx *TestContext) RegisterForBazelConversion() {
-	RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators)
+	RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch, ctx.bp2buildMutators)
 }
 
 func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
diff --git a/android/variable.go b/android/variable.go
index bbb9868..d32debe 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"`
@@ -299,8 +300,6 @@
 
 	Override_rs_driver *string `json:",omitempty"`
 
-	Fuchsia *bool `json:",omitempty"`
-
 	DeviceKernelHeaders []string `json:",omitempty"`
 
 	ExtraVndkVersions []string `json:",omitempty"`
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 11df288..d385ac1 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -2240,6 +2240,7 @@
 	android.InitDefaultableModule(module)
 	android.InitSdkAwareModule(module)
 	android.InitOverridableModule(module, &module.overridableProperties.Overrides)
+	android.InitBazelModule(module)
 	return module
 }
 
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 422e46c..d6c7142 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -4667,6 +4667,13 @@
 			prebuilt_bootclasspath_fragment {
 				name: "art-bootclasspath-fragment",
 				contents: ["core-oj"],
+				hidden_api: {
+					annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+					metadata: "my-bootclasspath-fragment/metadata.csv",
+					index: "my-bootclasspath-fragment/index.csv",
+					stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+					all_flags: "my-bootclasspath-fragment/all-flags.csv",
+				},
 			}
 
 			java_import {
@@ -4890,7 +4897,7 @@
 		}
 	}
 
-	checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
+	checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
 		t.Helper()
 		platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
 		var rule android.TestingBuildParams
@@ -4899,6 +4906,15 @@
 		java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
 	}
 
+	checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
+		t.Helper()
+		platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
+		var rule android.TestingBuildParams
+
+		rule = platformBootclasspath.Output("hiddenapi-index.csv")
+		java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
+	}
+
 	fragment := java.ApexVariantReference{
 		Apex:   proptools.StringPtr("myapex"),
 		Module: proptools.StringPtr("my-bootclasspath-fragment"),
@@ -4923,6 +4939,13 @@
 			name: "my-bootclasspath-fragment",
 			contents: ["libfoo", "libbar"],
 			apex_available: ["myapex"],
+			hidden_api: {
+				annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+				metadata: "my-bootclasspath-fragment/metadata.csv",
+				index: "my-bootclasspath-fragment/index.csv",
+				stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+				all_flags: "my-bootclasspath-fragment/all-flags.csv",
+			},
 		}
 
 		java_import {
@@ -4946,9 +4969,10 @@
 		checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
 
 		// Verify the correct module jars contribute to the hiddenapi index file.
-		checkHiddenAPIIndexInputs(t, ctx, `
-			out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
-			out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
+		checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
+		checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
+			my-bootclasspath-fragment/index.csv
+			out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
 		`)
 	})
 
@@ -4964,6 +4988,13 @@
 			name: "my-bootclasspath-fragment",
 			contents: ["libfoo", "libbar"],
 			apex_available: ["myapex"],
+			hidden_api: {
+				annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+				metadata: "my-bootclasspath-fragment/metadata.csv",
+				index: "my-bootclasspath-fragment/index.csv",
+				stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+				all_flags: "my-bootclasspath-fragment/all-flags.csv",
+			},
 		}
 
 		java_import {
@@ -4987,9 +5018,10 @@
 		checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
 
 		// Verify the correct module jars contribute to the hiddenapi index file.
-		checkHiddenAPIIndexInputs(t, ctx, `
-			out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
-			out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
+		checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
+		checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
+			my-bootclasspath-fragment/index.csv
+			out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
 		`)
 	})
 
@@ -5012,6 +5044,13 @@
 			name: "my-bootclasspath-fragment",
 			contents: ["libfoo", "libbar"],
 			apex_available: ["myapex"],
+			hidden_api: {
+				annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+				metadata: "my-bootclasspath-fragment/metadata.csv",
+				index: "my-bootclasspath-fragment/index.csv",
+				stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+				all_flags: "my-bootclasspath-fragment/all-flags.csv",
+			},
 		}
 
 		java_import {
@@ -5070,6 +5109,13 @@
 			name: "my-bootclasspath-fragment",
 			contents: ["libfoo", "libbar"],
 			apex_available: ["myapex"],
+			hidden_api: {
+				annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+				metadata: "my-bootclasspath-fragment/metadata.csv",
+				index: "my-bootclasspath-fragment/index.csv",
+				stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+				all_flags: "my-bootclasspath-fragment/all-flags.csv",
+			},
 		}
 
 		java_import {
@@ -5108,9 +5154,10 @@
 		checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
 
 		// Verify the correct module jars contribute to the hiddenapi index file.
-		checkHiddenAPIIndexInputs(t, ctx, `
-			out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
-			out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
+		checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
+		checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
+			my-bootclasspath-fragment/index.csv
+			out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
 		`)
 	})
 
@@ -5146,6 +5193,13 @@
 			name: "my-bootclasspath-fragment",
 			contents: ["libfoo", "libbar"],
 			apex_available: ["myapex"],
+			hidden_api: {
+				annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+				metadata: "my-bootclasspath-fragment/metadata.csv",
+				index: "my-bootclasspath-fragment/index.csv",
+				stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+				all_flags: "my-bootclasspath-fragment/all-flags.csv",
+			},
 		}
 
 		java_import {
@@ -5182,9 +5236,10 @@
 		checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
 
 		// Verify the correct module jars contribute to the hiddenapi index file.
-		checkHiddenAPIIndexInputs(t, ctx, `
-			out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
-			out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
+		checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
+		checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
+			my-bootclasspath-fragment/index.csv
+			out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
 		`)
 	})
 
@@ -5220,6 +5275,13 @@
 			name: "my-bootclasspath-fragment",
 			contents: ["libfoo", "libbar"],
 			apex_available: ["myapex"],
+			hidden_api: {
+				annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+				metadata: "my-bootclasspath-fragment/metadata.csv",
+				index: "my-bootclasspath-fragment/index.csv",
+				stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+				all_flags: "my-bootclasspath-fragment/all-flags.csv",
+			},
 		}
 
 		java_import {
@@ -5258,9 +5320,10 @@
 		checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
 
 		// Verify the correct module jars contribute to the hiddenapi index file.
-		checkHiddenAPIIndexInputs(t, ctx, `
-			out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
-			out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
+		checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
+		checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
+			my-bootclasspath-fragment/index.csv
+			out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
 		`)
 	})
 }
@@ -7183,6 +7246,13 @@
 				name: "my-bootclasspath-fragment",
 				contents: ["libfoo"],
 				apex_available: ["myapex"],
+				hidden_api: {
+					annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
+					metadata: "my-bootclasspath-fragment/metadata.csv",
+					index: "my-bootclasspath-fragment/index.csv",
+					stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
+					all_flags: "my-bootclasspath-fragment/all-flags.csv",
+				},
 			}
 
 			java_import {
diff --git a/apex/bootclasspath_fragment_test.go b/apex/bootclasspath_fragment_test.go
index 4b1600e..5cd3eab 100644
--- a/apex/bootclasspath_fragment_test.go
+++ b/apex/bootclasspath_fragment_test.go
@@ -383,6 +383,13 @@
 				apex_available: [
 					"com.android.art",
 				],
+				hidden_api: {
+					annotation_flags: "mybootclasspathfragment/annotation-flags.csv",
+					metadata: "mybootclasspathfragment/metadata.csv",
+					index: "mybootclasspathfragment/index.csv",
+					stub_flags: "mybootclasspathfragment/stub-flags.csv",
+					all_flags: "mybootclasspathfragment/all-flags.csv",
+				},
 			}
 		`, contentsInsert(contents), prefer)
 		return android.FixtureAddTextFile("prebuilts/module_sdk/art/Android.bp", text)
@@ -582,6 +589,13 @@
 			apex_available: [
 				"com.android.art",
 			],
+			hidden_api: {
+				annotation_flags: "mybootclasspathfragment/annotation-flags.csv",
+				metadata: "mybootclasspathfragment/metadata.csv",
+				index: "mybootclasspathfragment/index.csv",
+				stub_flags: "mybootclasspathfragment/stub-flags.csv",
+				all_flags: "mybootclasspathfragment/all-flags.csv",
+			},
 		}
 	`)
 
diff --git a/apex/builder.go b/apex/builder.go
index 24c049b..148f42f 100644
--- a/apex/builder.go
+++ b/apex/builder.go
@@ -761,7 +761,7 @@
 	rule := java.Signapk
 	args := map[string]string{
 		"certificates": pem.String() + " " + key.String(),
-		"flags":        "-a 4096", //alignment
+		"flags":        "-a 4096 --align-file-size", //alignment
 	}
 	implicits := android.Paths{pem, key}
 	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
diff --git a/bazel/configurability.go b/bazel/configurability.go
index 282c606..f5f0913 100644
--- a/bazel/configurability.go
+++ b/bazel/configurability.go
@@ -29,8 +29,8 @@
 	// OsType names in arch.go
 	osAndroid     = "android"
 	osDarwin      = "darwin"
-	osFuchsia     = "fuchsia"
 	osLinux       = "linux_glibc"
+	osLinuxMusl   = "linux_musl"
 	osLinuxBionic = "linux_bionic"
 	osWindows     = "windows"
 
@@ -40,10 +40,10 @@
 	osArchAndroidX86        = "android_x86"
 	osArchAndroidX86_64     = "android_x86_64"
 	osArchDarwinX86_64      = "darwin_x86_64"
-	osArchFuchsiaArm64      = "fuchsia_arm64"
-	osArchFuchsiaX86_64     = "fuchsia_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"
@@ -84,23 +84,28 @@
 	platformOsMap = map[string]string{
 		osAndroid:         "//build/bazel/platforms/os:android",
 		osDarwin:          "//build/bazel/platforms/os:darwin",
-		osFuchsia:         "//build/bazel/platforms/os:fuchsia",
 		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.
 	}
 
+	platformBionicMap = map[string]string{
+		"bionic":          "//build/bazel/platforms/os:bionic",
+		conditionsDefault: ConditionsDefaultSelectKey, // The default condition of an os select map.
+	}
+
 	platformOsArchMap = map[string]string{
 		osArchAndroidArm:        "//build/bazel/platforms/os_arch:android_arm",
 		osArchAndroidArm64:      "//build/bazel/platforms/os_arch:android_arm64",
 		osArchAndroidX86:        "//build/bazel/platforms/os_arch:android_x86",
 		osArchAndroidX86_64:     "//build/bazel/platforms/os_arch:android_x86_64",
 		osArchDarwinX86_64:      "//build/bazel/platforms/os_arch:darwin_x86_64",
-		osArchFuchsiaArm64:      "//build/bazel/platforms/os_arch:fuchsia_arm64",
-		osArchFuchsiaX86_64:     "//build/bazel/platforms/os_arch:fuchsia_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",
@@ -117,6 +122,7 @@
 	arch
 	os
 	osArch
+	bionic
 	productVariables
 )
 
@@ -126,6 +132,7 @@
 		arch:             "arch",
 		os:               "os",
 		osArch:           "arch_os",
+		bionic:           "bionic",
 		productVariables: "product_variables",
 	}[ct]
 }
@@ -148,6 +155,10 @@
 		if _, ok := platformOsArchMap[config]; !ok {
 			panic(fmt.Errorf("Unknown os+arch: %s", config))
 		}
+	case bionic:
+		if _, ok := platformBionicMap[config]; !ok {
+			panic(fmt.Errorf("Unknown for %s: %s", ct.String(), config))
+		}
 	case productVariables:
 		// do nothing
 	default:
@@ -167,6 +178,8 @@
 		return platformOsMap[config]
 	case osArch:
 		return platformOsArchMap[config]
+	case bionic:
+		return platformBionicMap[config]
 	case productVariables:
 		if config == conditionsDefault {
 			return ConditionsDefaultSelectKey
@@ -186,6 +199,8 @@
 	OsConfigurationAxis = ConfigurationAxis{configurationType: os}
 	// An axis for arch+os-specific configurations
 	OsArchConfigurationAxis = ConfigurationAxis{configurationType: osArch}
+	// An axis for bionic os-specific configurations
+	BionicConfigurationAxis = ConfigurationAxis{configurationType: bionic}
 )
 
 // ProductVariableConfigurationAxis returns an axis for the given product variable
diff --git a/bazel/properties.go b/bazel/properties.go
index 7ecc92b..2656bad 100644
--- a/bazel/properties.go
+++ b/bazel/properties.go
@@ -321,7 +321,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		la.Value = &value
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		if la.ConfigurableValues == nil {
 			la.ConfigurableValues = make(configurableLabels)
 		}
@@ -337,7 +337,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		return *la.Value
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		return *la.ConfigurableValues[axis][config]
 	default:
 		panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
@@ -394,7 +394,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		ba.Value = value
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		if ba.ConfigurableValues == nil {
 			ba.ConfigurableValues = make(configurableBools)
 		}
@@ -410,7 +410,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		return ba.Value
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		if v, ok := ba.ConfigurableValues[axis][config]; ok {
 			return &v
 		} else {
@@ -509,7 +509,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		lla.Value = list
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		if lla.ConfigurableValues == nil {
 			lla.ConfigurableValues = make(configurableLabelLists)
 		}
@@ -525,7 +525,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		return lla.Value
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		return lla.ConfigurableValues[axis][config]
 	default:
 		panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
@@ -682,7 +682,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		sla.Value = list
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		if sla.ConfigurableValues == nil {
 			sla.ConfigurableValues = make(configurableStringLists)
 		}
@@ -698,7 +698,7 @@
 	switch axis.configurationType {
 	case noConfig:
 		return sla.Value
-	case arch, os, osArch, productVariables:
+	case arch, os, osArch, bionic, productVariables:
 		return sla.ConfigurableValues[axis][config]
 	default:
 		panic(fmt.Errorf("Unrecognized ConfigurationAxis %s", axis))
diff --git a/bp2build/apex_conversion_test.go b/bp2build/apex_conversion_test.go
index fbf6fa2..f4a1016 100644
--- a/bp2build/apex_conversion_test.go
+++ b/bp2build/apex_conversion_test.go
@@ -46,3 +46,23 @@
     manifest = "manifest.json",
 )`}})
 }
+
+func TestApexBundleHasBazelModuleProps(t *testing.T) {
+	runApexTestCase(t, bp2buildTestCase{
+		description:                        "apex - has bazel module props",
+		moduleTypeUnderTest:                "apex",
+		moduleTypeUnderTestFactory:         apex.BundleFactory,
+		moduleTypeUnderTestBp2BuildMutator: apex.ApexBundleBp2Build,
+		filesystem:                         map[string]string{},
+		blueprint: `
+apex {
+	name: "apogee",
+	manifest: "manifest.json",
+	bazel_module: { bp2build_available: true },
+}
+`,
+		expectedBazelTargets: []string{`apex(
+    name = "apogee",
+    manifest = "manifest.json",
+)`}})
+}
diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go
index 0e52f2a..e5dbda6 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -556,7 +556,6 @@
 		moduleTypeUnderTestFactory         android.ModuleFactory
 		moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
 		preArchMutators                    []android.RegisterMutatorFunc
-		depsMutators                       []android.RegisterMutatorFunc
 		bp                                 string
 		expectedBazelTargets               []string
 		fs                                 map[string]string
@@ -720,7 +719,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo.tool",
     out: ["foo_tool.out"],
@@ -758,7 +756,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo.tools",
     out: ["foo_tool.out", "foo_tool2.out"],
@@ -798,7 +795,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -822,7 +818,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -846,7 +841,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -873,7 +867,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -900,7 +893,6 @@
 			moduleTypeUnderTest:                "genrule",
 			moduleTypeUnderTestFactory:         genrule.GenRuleFactory,
 			moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
-			depsMutators:                       []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
 			bp: `genrule {
     name: "foo",
     out: ["foo.out"],
@@ -933,9 +925,6 @@
 		config := android.TestConfig(buildDir, nil, testCase.bp, fs)
 		ctx := android.NewTestContext(config)
 		ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
-		for _, m := range testCase.depsMutators {
-			ctx.DepsBp2BuildMutators(m)
-		}
 		ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
 		ctx.RegisterForBazelConversion()
 
@@ -1370,7 +1359,6 @@
 		moduleTypeUnderTestFactory         android.ModuleFactory
 		moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
 		preArchMutators                    []android.RegisterMutatorFunc
-		depsMutators                       []android.RegisterMutatorFunc
 		bp                                 string
 		expectedBazelTargets               []string
 		fs                                 map[string]string
@@ -1487,9 +1475,6 @@
 			config := android.TestConfig(buildDir, nil, testCase.bp, fs)
 			ctx := android.NewTestContext(config)
 			ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
-			for _, m := range testCase.depsMutators {
-				ctx.DepsBp2BuildMutators(m)
-			}
 			ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
 			ctx.RegisterForBazelConversion()
 
diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go
index 4f720f5..8dcba55 100644
--- a/bp2build/cc_library_conversion_test.go
+++ b/bp2build/cc_library_conversion_test.go
@@ -73,9 +73,6 @@
 	registerModuleTypes(ctx)
 	ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
 	ctx.RegisterBp2BuildConfig(bp2buildConfig)
-	for _, m := range tc.depsMutators {
-		ctx.DepsBp2BuildMutators(m)
-	}
 	ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
 	ctx.RegisterForBazelConversion()
 
@@ -118,6 +115,7 @@
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
 		filesystem: map[string]string{
 			"android.cpp": "",
+			"bionic.cpp":  "",
 			"darwin.cpp":  "",
 			// Refer to cc.headerExts for the supported header extensions in Soong.
 			"header.h":         "",
@@ -164,6 +162,9 @@
         darwin: {
             srcs: ["darwin.cpp"],
         },
+        bionic: {
+          srcs: ["bionic.cpp"]
+        },
     },
 }
 `,
@@ -190,6 +191,9 @@
         "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
         "//build/bazel/platforms/os:linux": ["linux.cpp"],
         "//conditions:default": [],
+    }) + select({
+        "//build/bazel/platforms/os:bionic": ["bionic.cpp"],
+        "//conditions:default": [],
     }),
 )`}})
 }
@@ -326,7 +330,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/both.cpp":       "",
@@ -411,7 +414,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -458,7 +460,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/arm.cpp":        "",
@@ -597,7 +598,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/both_source.cpp":   "",
@@ -738,7 +738,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -769,7 +768,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -812,7 +810,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -852,7 +849,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -926,7 +922,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -956,7 +951,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `cc_library {
@@ -1012,7 +1006,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -1054,7 +1047,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library {
@@ -1301,7 +1293,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
@@ -1408,7 +1399,6 @@
 		moduleTypeUnderTest:                "cc_library",
 		moduleTypeUnderTestFactory:         cc.LibraryFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		dir:                                "foo/bar",
 		filesystem: map[string]string{
 			"foo/bar/Android.bp": `
diff --git a/bp2build/cc_library_headers_conversion_test.go b/bp2build/cc_library_headers_conversion_test.go
index db344de..712d0bd 100644
--- a/bp2build/cc_library_headers_conversion_test.go
+++ b/bp2build/cc_library_headers_conversion_test.go
@@ -45,7 +45,6 @@
 	moduleTypeUnderTest                string
 	moduleTypeUnderTestFactory         android.ModuleFactory
 	moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
-	depsMutators                       []android.RegisterMutatorFunc
 	blueprint                          string
 	expectedBazelTargets               []string
 	filesystem                         map[string]string
@@ -181,13 +180,11 @@
 		moduleTypeUnderTest:                "cc_library_headers",
 		moduleTypeUnderTestFactory:         cc.LibraryHeaderFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryPreamble + `
 cc_library_headers { name: "android-lib" }
 cc_library_headers { name: "base-lib" }
 cc_library_headers { name: "darwin-lib" }
-cc_library_headers { name: "fuchsia-lib" }
 cc_library_headers { name: "linux-lib" }
 cc_library_headers { name: "linux_bionic-lib" }
 cc_library_headers { name: "windows-lib" }
@@ -197,7 +194,6 @@
     target: {
         android: { header_libs: ["android-lib"] },
         darwin: { header_libs: ["darwin-lib"] },
-        fuchsia: { header_libs: ["fuchsia-lib"] },
         linux_bionic: { header_libs: ["linux_bionic-lib"] },
         linux_glibc: { header_libs: ["linux-lib"] },
         windows: { header_libs: ["windows-lib"] },
@@ -231,19 +227,12 @@
     implementation_deps = [":base-lib"] + select({
         "//build/bazel/platforms/os:android": [":android-lib"],
         "//build/bazel/platforms/os:darwin": [":darwin-lib"],
-        "//build/bazel/platforms/os:fuchsia": [":fuchsia-lib"],
         "//build/bazel/platforms/os:linux": [":linux-lib"],
         "//build/bazel/platforms/os:linux_bionic": [":linux_bionic-lib"],
         "//build/bazel/platforms/os:windows": [":windows-lib"],
         "//conditions:default": [],
     }),
 )`, `cc_library_headers(
-    name = "fuchsia-lib",
-    copts = [
-        "-I.",
-        "-I$(BINDIR)/.",
-    ],
-)`, `cc_library_headers(
     name = "linux-lib",
     copts = [
         "-I.",
@@ -271,7 +260,6 @@
 		moduleTypeUnderTest:                "cc_library_headers",
 		moduleTypeUnderTestFactory:         cc.LibraryHeaderFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryPreamble + `
 cc_library_headers { name: "android-lib" }
@@ -318,7 +306,6 @@
 		moduleTypeUnderTest:                "cc_library_headers",
 		moduleTypeUnderTestFactory:         cc.LibraryHeaderFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryPreamble + `cc_library_headers {
     name: "foo_headers",
diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go
index c33889f..1dc6713 100644
--- a/bp2build/cc_library_static_conversion_test.go
+++ b/bp2build/cc_library_static_conversion_test.go
@@ -469,7 +469,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static { name: "static_dep" }
@@ -517,7 +516,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static { name: "static_dep" }
@@ -565,7 +563,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static { name: "static_dep" }
@@ -632,7 +629,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":       "",
 			"foo-a.c":        "",
@@ -665,7 +661,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":  "",
 			"foo-arm.c": "",
@@ -697,7 +692,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":           "",
 			"for-arm.c":          "",
@@ -734,7 +728,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":      "",
 			"for-arm.c":     "",
@@ -782,7 +775,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":             "",
 			"for-arm.c":            "",
@@ -856,7 +848,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.cc":       "",
 			"foo-no-arm.cc":   "",
@@ -892,7 +883,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.cc":       "",
 			"foo-no-arm.cc":   "",
@@ -934,7 +924,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static { name: "static_dep" }
@@ -967,7 +956,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":        "",
 			"for-lib32.c":     "",
@@ -1003,7 +991,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":        "",
 			"for-lib32.c":     "",
@@ -1059,7 +1046,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.c":             "",
 			"for-arm.c":            "",
@@ -1151,7 +1137,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem: map[string]string{
 			"common.cpp":             "",
 			"for-x86.cpp":            "",
@@ -1243,7 +1228,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static {
     name: "foo_static",
@@ -1300,8 +1284,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
-		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static {
     name: "foo_static",
@@ -1345,7 +1327,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static {
@@ -1417,7 +1398,6 @@
 		moduleTypeUnderTest:                "cc_library_static",
 		moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
 		moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
-		depsMutators:                       []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
 		filesystem:                         map[string]string{},
 		blueprint: soongCcLibraryStaticPreamble + `
 cc_library_static {
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/cc/Android.bp b/cc/Android.bp
index 46740dc..164d32b 100644
--- a/cc/Android.bp
+++ b/cc/Android.bp
@@ -14,6 +14,7 @@
         "soong-cc-config",
         "soong-etc",
         "soong-genrule",
+        "soong-snapshot",
         "soong-tradefed",
     ],
     srcs: [
@@ -58,6 +59,7 @@
         "binary.go",
         "binary_sdk_member.go",
         "fuzz.go",
+        "fuzz_common.go",
         "library.go",
         "library_headers.go",
         "library_sdk_member.go",
diff --git a/cc/androidmk.go b/cc/androidmk.go
index e58d166..bda1006 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -401,24 +401,24 @@
 	ctx.subAndroidMk(entries, fuzz.binaryDecorator)
 
 	var fuzzFiles []string
-	for _, d := range fuzz.corpus {
+	for _, d := range fuzz.fuzzPackagedModule.Corpus {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.corpusIntermediateDir.String())+":corpus/"+d.Base())
+			filepath.Dir(fuzz.fuzzPackagedModule.CorpusIntermediateDir.String())+":corpus/"+d.Base())
 	}
 
-	for _, d := range fuzz.data {
+	for _, d := range fuzz.fuzzPackagedModule.Data {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.dataIntermediateDir.String())+":data/"+d.Rel())
+			filepath.Dir(fuzz.fuzzPackagedModule.DataIntermediateDir.String())+":data/"+d.Rel())
 	}
 
-	if fuzz.dictionary != nil {
+	if fuzz.fuzzPackagedModule.Dictionary != nil {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.dictionary.String())+":"+fuzz.dictionary.Base())
+			filepath.Dir(fuzz.fuzzPackagedModule.Dictionary.String())+":"+fuzz.fuzzPackagedModule.Dictionary.Base())
 	}
 
-	if fuzz.config != nil {
+	if fuzz.fuzzPackagedModule.Config != nil {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.config.String())+":config.json")
+			filepath.Dir(fuzz.fuzzPackagedModule.Config.String())+":config.json")
 	}
 
 	entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
diff --git a/cc/binary.go b/cc/binary.go
index c177a08..763d2b9 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -215,7 +215,7 @@
 			if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
 				binary.Properties.Static_executable = BoolPtr(true)
 			}
-		} else if !ctx.Fuchsia() {
+		} else {
 			// Static executables are not supported on Darwin or Windows
 			binary.Properties.Static_executable = nil
 		}
@@ -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/bp2build.go b/cc/bp2build.go
index 536f112..68afd0d 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -24,124 +24,6 @@
 	"github.com/google/blueprint/proptools"
 )
 
-// bp2build functions and helpers for converting cc_* modules to Bazel.
-
-func init() {
-	android.DepsBp2BuildMutators(RegisterDepsBp2Build)
-}
-
-func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
-	ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
-}
-
-// A naive deps mutator to add deps on all modules across all combinations of
-// target props for cc modules. This is needed to make module -> bazel label
-// resolution work in the bp2build mutator later. This is probably
-// the wrong way to do it, but it works.
-//
-// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
-func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
-	module, ok := ctx.Module().(*Module)
-	if !ok {
-		// Not a cc module
-		return
-	}
-
-	if !module.ConvertWithBp2build(ctx) {
-		return
-	}
-
-	var allDeps []string
-
-	for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) {
-		for _, props := range configToProps {
-			if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
-				allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
-				allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
-			}
-		}
-	}
-
-	for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
-		for _, props := range configToProps {
-			if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
-				allDeps = append(allDeps, baseLinkerProps.Header_libs...)
-				allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
-				allDeps = append(allDeps, baseLinkerProps.Static_libs...)
-				allDeps = append(allDeps, baseLinkerProps.Exclude_static_libs...)
-				allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
-				allDeps = append(allDeps, baseLinkerProps.Shared_libs...)
-				allDeps = append(allDeps, baseLinkerProps.Exclude_shared_libs...)
-			}
-		}
-	}
-
-	// Deps in the static: { .. } and shared: { .. } props of a cc_library.
-	if lib, ok := module.compiler.(*libraryDecorator); ok {
-		appendDeps := func(deps []string, p StaticOrSharedProperties) []string {
-			deps = append(deps, p.Static_libs...)
-			deps = append(deps, p.Whole_static_libs...)
-			deps = append(deps, p.Shared_libs...)
-			return deps
-		}
-
-		allDeps = appendDeps(allDeps, lib.SharedProperties.Shared)
-		allDeps = appendDeps(allDeps, lib.StaticProperties.Static)
-
-		// TODO(b/186024507, b/186489250): Temporarily exclude adding
-		// system_shared_libs deps until libc and libm builds.
-		if lib.static() {
-			allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
-		} else if lib.shared() {
-			allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
-		}
-
-		// Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library.
-		// target: { <target>: shared: { ... } }
-		for _, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
-			for _, props := range configToProps {
-				if p, ok := props.(*SharedProperties); ok {
-					allDeps = appendDeps(allDeps, p.Shared)
-				}
-			}
-		}
-
-		for _, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
-			for _, props := range configToProps {
-				if p, ok := props.(*StaticProperties); ok {
-					allDeps = appendDeps(allDeps, p.Static)
-				}
-			}
-		}
-	}
-
-	// product variables only support a limited set of fields, this is the full list of field names
-	// related to cc module dependency management that are supported.
-	productVariableDepFields := [4]string{
-		"Shared_libs",
-		"Static_libs",
-		"Exclude_static_libs",
-		"Whole_static_libs",
-	}
-
-	productVariableProps := android.ProductVariableProperties(ctx)
-	for _, name := range productVariableDepFields {
-		props, exists := productVariableProps[name]
-		if !exists {
-			continue
-		}
-		for _, prop := range props {
-			if p, ok := prop.Property.([]string); !ok {
-				ctx.ModuleErrorf("Could not convert product variable %s property", name)
-			} else {
-				allDeps = append(allDeps, p...)
-			}
-		}
-	}
-
-	ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
-}
-
 // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
 // properties which apply to either the shared or static version of a cc_library module.
 type staticOrSharedAttributes struct {
@@ -185,30 +67,33 @@
 	// Convert the filegroup dependencies into the extension-specific filegroups
 	// filtered in the filegroup.bzl macro.
 	cppFilegroup := func(label string) string {
-		ctx.VisitDirectDeps(func(m android.Module) {
-			if isFilegroupNamed(m, label) {
+		m, exists := ctx.ModuleFromName(label)
+		if exists {
+			aModule, _ := m.(android.Module)
+			if isFilegroupNamed(aModule, label) {
 				label = label + "_cpp_srcs"
-				return
 			}
-		})
+		}
 		return label
 	}
 	cFilegroup := func(label string) string {
-		ctx.VisitDirectDeps(func(m android.Module) {
-			if isFilegroupNamed(m, label) {
+		m, exists := ctx.ModuleFromName(label)
+		if exists {
+			aModule, _ := m.(android.Module)
+			if isFilegroupNamed(aModule, label) {
 				label = label + "_c_srcs"
-				return
 			}
-		})
+		}
 		return label
 	}
 	asFilegroup := func(label string) string {
-		ctx.VisitDirectDeps(func(m android.Module) {
-			if isFilegroupNamed(m, label) {
+		m, exists := ctx.ModuleFromName(label)
+		if exists {
+			aModule, _ := m.(android.Module)
+			if isFilegroupNamed(aModule, label) {
 				label = label + "_as_srcs"
-				return
 			}
-		})
+		}
 		return label
 	}
 
diff --git a/cc/builder.go b/cc/builder.go
index b0842ec..842ce85 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -237,7 +237,7 @@
 	// -w has been added since header-abi-dumper does not need to produce any sort of diagnostic information.
 	sAbiDump, sAbiDumpRE = pctx.RemoteStaticRules("sAbiDump",
 		blueprint.RuleParams{
-			Command:     "rm -f $out && $reTemplate$sAbiDumper -o ${out} $in $exportDirs -- $cFlags -w -isystem prebuilts/clang-tools/${config.HostPrebuiltTag}/clang-headers",
+			Command:     "rm -f $out && $reTemplate$sAbiDumper --root-dir . --root-dir $$OUT_DIR:out -o ${out} $in $exportDirs -- $cFlags -w -isystem prebuilts/clang-tools/${config.HostPrebuiltTag}/clang-headers",
 			CommandDeps: []string{"$sAbiDumper"},
 		}, &remoteexec.REParams{
 			Labels:       map[string]string{"type": "abi-dump", "tool": "header-abi-dumper"},
@@ -255,7 +255,7 @@
 	// sAbi dump file.
 	sAbiLink, sAbiLinkRE = pctx.RemoteStaticRules("sAbiLink",
 		blueprint.RuleParams{
-			Command:        "$reTemplate$sAbiLinker -o ${out} $symbolFilter -arch $arch  $exportedHeaderFlags @${out}.rsp ",
+			Command:        "$reTemplate$sAbiLinker --root-dir . --root-dir $$OUT_DIR:out -o ${out} $symbolFilter -arch $arch $exportedHeaderFlags @${out}.rsp",
 			CommandDeps:    []string{"$sAbiLinker"},
 			Rspfile:        "${out}.rsp",
 			RspfileContent: "${in}",
diff --git a/cc/cc.go b/cc/cc.go
index aeebaef..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
@@ -756,14 +755,13 @@
 // members of the cc.Module to this decorator. Thus, a cc_binary module has custom linker and
 // installer logic.
 type Module struct {
-	android.ModuleBase
-	android.DefaultableModuleBase
-	android.ApexModuleBase
+	FuzzModule
+
 	android.SdkBase
 	android.BazelModuleBase
 
-	Properties       BaseProperties
 	VendorProperties VendorProperties
+	Properties       BaseProperties
 
 	// initialize before calling Init
 	hod      android.HostOrDeviceSupported
diff --git a/cc/cc_test.go b/cc/cc_test.go
index 0a3acb9..dd51fe8 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -152,71 +152,6 @@
 	).RunTest(t)
 }
 
-func TestFuchsiaDeps(t *testing.T) {
-	t.Helper()
-
-	bp := `
-		cc_library {
-			name: "libTest",
-			srcs: ["foo.c"],
-			target: {
-				fuchsia: {
-					srcs: ["bar.c"],
-				},
-			},
-		}`
-
-	result := android.GroupFixturePreparers(
-		prepareForCcTest,
-		PrepareForTestOnFuchsia,
-	).RunTestWithBp(t, bp)
-
-	rt := false
-	fb := false
-
-	ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
-	implicits := ld.Implicits
-	for _, lib := range implicits {
-		if strings.Contains(lib.Rel(), "libcompiler_rt") {
-			rt = true
-		}
-
-		if strings.Contains(lib.Rel(), "libbioniccompat") {
-			fb = true
-		}
-	}
-
-	if !rt || !fb {
-		t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
-	}
-}
-
-func TestFuchsiaTargetDecl(t *testing.T) {
-	t.Helper()
-
-	bp := `
-		cc_library {
-			name: "libTest",
-			srcs: ["foo.c"],
-			target: {
-				fuchsia: {
-					srcs: ["bar.c"],
-				},
-			},
-		}`
-
-	result := android.GroupFixturePreparers(
-		prepareForCcTest,
-		PrepareForTestOnFuchsia,
-	).RunTestWithBp(t, bp)
-	ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
-	var objs []string
-	for _, o := range ld.Inputs {
-		objs = append(objs, o.Base())
-	}
-	android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
-}
-
 func TestVendorSrc(t *testing.T) {
 	ctx := testCc(t, `
 		cc_library {
diff --git a/cc/config/Android.bp b/cc/config/Android.bp
index c1d4f17..3e8ee48 100644
--- a/cc/config/Android.bp
+++ b/cc/config/Android.bp
@@ -21,10 +21,8 @@
 
         "arm_device.go",
         "arm64_device.go",
-        "arm64_fuchsia_device.go",
         "x86_device.go",
         "x86_64_device.go",
-        "x86_64_fuchsia_device.go",
 
         "x86_darwin_host.go",
         "x86_linux_host.go",
diff --git a/cc/config/arm64_fuchsia_device.go b/cc/config/arm64_fuchsia_device.go
deleted file mode 100644
index 5ab27a0..0000000
--- a/cc/config/arm64_fuchsia_device.go
+++ /dev/null
@@ -1,81 +0,0 @@
-// Copyright 2018 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 config
-
-import (
-	"android/soong/android"
-)
-
-var fuchsiaArm64SysRoot string = "prebuilts/fuchsia_sdk/arch/arm64/sysroot"
-var fuchsiaArm64PrebuiltLibsRoot string = "fuchsia/prebuilt_libs/"
-
-type toolchainFuchsiaArm64 struct {
-	toolchain64Bit
-	toolchainFuchsia
-}
-
-func (t *toolchainFuchsiaArm64) Name() string {
-	return "arm64"
-}
-
-func (t *toolchainFuchsiaArm64) GccRoot() string {
-	return "${config.Arm64GccRoot}"
-}
-
-func (t *toolchainFuchsiaArm64) GccTriple() string {
-	return "aarch64-linux-android"
-}
-
-func (t *toolchainFuchsiaArm64) GccVersion() string {
-	return arm64GccVersion
-}
-
-func (t *toolchainFuchsiaArm64) IncludeFlags() string {
-	return ""
-}
-
-func (t *toolchainFuchsiaArm64) ClangTriple() string {
-	return "arm64-fuchsia-android"
-}
-
-func (t *toolchainFuchsiaArm64) Cppflags() string {
-	return "-Wno-error=deprecated-declarations"
-}
-
-func (t *toolchainFuchsiaArm64) Ldflags() string {
-	return "--target=arm64-fuchsia --sysroot=" + fuchsiaArm64SysRoot + " -L" + fuchsiaArm64PrebuiltLibsRoot + "/aarch64-fuchsia/lib " + "-Lprebuilts/fuchsia_sdk/arch/arm64/dist/"
-}
-
-func (t *toolchainFuchsiaArm64) Lldflags() string {
-	return "--target=arm64-fuchsia --sysroot=" + fuchsiaArm64SysRoot + " -L" + fuchsiaArm64PrebuiltLibsRoot + "/aarch64-fuchsia/lib " + "-Lprebuilts/fuchsia_sdk/arch/arm64/dist/"
-}
-
-func (t *toolchainFuchsiaArm64) Cflags() string {
-	return "--target=arm64-fuchsia --sysroot=" + fuchsiaArm64SysRoot + " -I" + fuchsiaArm64SysRoot + "/include"
-}
-
-func (t *toolchainFuchsiaArm64) ToolchainCflags() string {
-	return "-march=armv8-a"
-}
-
-var toolchainArm64FuchsiaSingleton Toolchain = &toolchainFuchsiaArm64{}
-
-func arm64FuchsiaToolchainFactory(arch android.Arch) Toolchain {
-	return toolchainArm64FuchsiaSingleton
-}
-
-func init() {
-	registerToolchainFactory(android.Fuchsia, android.Arm64, arm64FuchsiaToolchainFactory)
-}
diff --git a/cc/config/clang.go b/cc/config/clang.go
index 9cfe28f..53a7306 100644
--- a/cc/config/clang.go
+++ b/cc/config/clang.go
@@ -92,64 +92,6 @@
 	"readability-function-cognitive-complexity", // http://b/175055536
 }
 
-func init() {
-	exportStringListStaticVariable("ClangExtraCflags", []string{
-		"-D__compiler_offsetof=__builtin_offsetof",
-
-		// Emit address-significance table which allows linker to perform safe ICF. Clang does
-		// not emit the table by default on Android since NDK still uses GNU binutils.
-		"-faddrsig",
-
-		// Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug
-		// tracking this is http://b/151457797.
-		"-fcommon",
-
-		// Help catch common 32/64-bit errors.
-		"-Werror=int-conversion",
-
-		// Enable the new pass manager.
-		"-fexperimental-new-pass-manager",
-
-		// Disable overly aggressive warning for macros defined with a leading underscore
-		// This happens in AndroidConfig.h, which is included nearly everywhere.
-		// TODO: can we remove this now?
-		"-Wno-reserved-id-macro",
-
-		// Workaround for ccache with clang.
-		// See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
-		"-Wno-unused-command-line-argument",
-
-		// Force clang to always output color diagnostics. Ninja will strip the ANSI
-		// color codes if it is not running in a terminal.
-		"-fcolor-diagnostics",
-
-		// Warnings from clang-7.0
-		"-Wno-sign-compare",
-
-		// Warnings from clang-8.0
-		"-Wno-defaulted-function-deleted",
-
-		// Disable -Winconsistent-missing-override until we can clean up the existing
-		// codebase for it.
-		"-Wno-inconsistent-missing-override",
-
-		// Warnings from clang-10
-		// Nested and array designated initialization is nice to have.
-		"-Wno-c99-designator",
-
-		// Warnings from clang-12
-		"-Wno-gnu-folding-constant",
-
-		// Calls to the APIs that are newer than the min sdk version of the caller should be
-		// guarded with __builtin_available.
-		"-Wunguarded-availability",
-		// This macro allows the bionic versioning.h to indirectly determine whether the
-		// option -Wunguarded-availability is on or not.
-		"-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
-	})
-
-}
-
 func ClangFilterUnknownCflags(cflags []string) []string {
 	result, _ := android.FilterList(cflags, ClangUnknownCflags)
 	return result
diff --git a/cc/config/global.go b/cc/config/global.go
index bcee06a..55e0d79 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -15,6 +15,7 @@
 package config
 
 import (
+	"runtime"
 	"strings"
 
 	"android/soong/android"
@@ -55,6 +56,59 @@
 		"-Werror=pragma-pack-suspicious-include",
 		"-Werror=string-plus-int",
 		"-Werror=unreachable-code-loop-increment",
+
+		"-D__compiler_offsetof=__builtin_offsetof",
+
+		// Emit address-significance table which allows linker to perform safe ICF. Clang does
+		// not emit the table by default on Android since NDK still uses GNU binutils.
+		"-faddrsig",
+
+		// Turn on -fcommon explicitly, since Clang now defaults to -fno-common. The cleanup bug
+		// tracking this is http://b/151457797.
+		"-fcommon",
+
+		// Help catch common 32/64-bit errors.
+		"-Werror=int-conversion",
+
+		// Enable the new pass manager.
+		"-fexperimental-new-pass-manager",
+
+		// Disable overly aggressive warning for macros defined with a leading underscore
+		// This happens in AndroidConfig.h, which is included nearly everywhere.
+		// TODO: can we remove this now?
+		"-Wno-reserved-id-macro",
+
+		// Workaround for ccache with clang.
+		// See http://petereisentraut.blogspot.com/2011/05/ccache-and-clang.html.
+		"-Wno-unused-command-line-argument",
+
+		// Force clang to always output color diagnostics. Ninja will strip the ANSI
+		// color codes if it is not running in a terminal.
+		"-fcolor-diagnostics",
+
+		// Warnings from clang-7.0
+		"-Wno-sign-compare",
+
+		// Warnings from clang-8.0
+		"-Wno-defaulted-function-deleted",
+
+		// Disable -Winconsistent-missing-override until we can clean up the existing
+		// codebase for it.
+		"-Wno-inconsistent-missing-override",
+
+		// Warnings from clang-10
+		// Nested and array designated initialization is nice to have.
+		"-Wno-c99-designator",
+
+		// Warnings from clang-12
+		"-Wno-gnu-folding-constant",
+
+		// Calls to the APIs that are newer than the min sdk version of the caller should be
+		// guarded with __builtin_available.
+		"-Wunguarded-availability",
+		// This macro allows the bionic versioning.h to indirectly determine whether the
+		// option -Wunguarded-availability is on or not.
+		"-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__",
 	}
 
 	commonGlobalConlyflags = []string{}
@@ -229,7 +283,7 @@
 var pctx = android.NewPackageContext("android/soong/cc/config")
 
 func init() {
-	if android.BuildOs == android.Linux {
+	if runtime.GOOS == "linux" {
 		commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
 	}
 
@@ -246,7 +300,6 @@
 	bazelCommonGlobalCflags := append(
 		commonGlobalCflags,
 		[]string{
-			"${ClangExtraCflags}",
 			// Default to zero initialization.
 			"-ftrivial-auto-var-init=zero",
 			"-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang",
@@ -255,7 +308,6 @@
 
 	pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
 		flags := commonGlobalCflags
-		flags = append(flags, "${ClangExtraCflags}")
 
 		// http://b/131390872
 		// Automatically initialize any uninitialized stack variables.
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..112e5a6 100644
--- a/cc/config/vndk.go
+++ b/cc/config/vndk.go
@@ -62,6 +62,7 @@
 	"android.system.keystore2-V1-ndk_platform",
 	"android.system.keystore2-ndk_platform",
 	"android.system.keystore2-unstable-ndk_platform",
+	"android.system.suspend-V1-ndk_platform",
 	"libbinder",
 	"libcrypto",
 	"libexpat",
diff --git a/cc/config/x86_64_fuchsia_device.go b/cc/config/x86_64_fuchsia_device.go
deleted file mode 100644
index 86558a6..0000000
--- a/cc/config/x86_64_fuchsia_device.go
+++ /dev/null
@@ -1,90 +0,0 @@
-// Copyright 2018 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 config
-
-import (
-	"android/soong/android"
-)
-
-var fuchsiaSysRoot string = "prebuilts/fuchsia_sdk/arch/x64/sysroot"
-var fuchsiaPrebuiltLibsRoot string = "fuchsia/prebuilt_libs"
-
-type toolchainFuchsia struct {
-	cFlags, ldFlags string
-}
-
-type toolchainFuchsiaX8664 struct {
-	toolchain64Bit
-	toolchainFuchsia
-}
-
-func (t *toolchainFuchsiaX8664) Name() string {
-	return "x86_64"
-}
-
-func (t *toolchainFuchsiaX8664) GccRoot() string {
-	return "${config.X86_64GccRoot}"
-}
-
-func (t *toolchainFuchsiaX8664) GccTriple() string {
-	return "x86_64-linux-android"
-}
-
-func (t *toolchainFuchsiaX8664) GccVersion() string {
-	return x86_64GccVersion
-}
-
-func (t *toolchainFuchsiaX8664) IncludeFlags() string {
-	return ""
-}
-
-func (t *toolchainFuchsiaX8664) ClangTriple() string {
-	return "x86_64-fuchsia-android"
-}
-
-func (t *toolchainFuchsiaX8664) Cppflags() string {
-	return "-Wno-error=deprecated-declarations"
-}
-
-func (t *toolchainFuchsiaX8664) Ldflags() string {
-	return "--target=x86_64-fuchsia --sysroot=" + fuchsiaSysRoot + " -L" + fuchsiaPrebuiltLibsRoot + "/x86_64-fuchsia/lib " + "-Lprebuilts/fuchsia_sdk/arch/x64/dist/"
-
-}
-
-func (t *toolchainFuchsiaX8664) Lldflags() string {
-	return "--target=x86_64-fuchsia --sysroot=" + fuchsiaSysRoot + " -L" + fuchsiaPrebuiltLibsRoot + "/x86_64-fuchsia/lib " + "-Lprebuilts/fuchsia_sdk/arch/x64/dist/"
-}
-
-func (t *toolchainFuchsiaX8664) Cflags() string {
-	return "--target=x86_64-fuchsia --sysroot=" + fuchsiaSysRoot + " -I" + fuchsiaSysRoot + "/include"
-}
-
-func (t *toolchainFuchsiaX8664) YasmFlags() string {
-	return "-f elf64 -m amd64"
-}
-
-func (t *toolchainFuchsiaX8664) ToolchainCflags() string {
-	return "-mssse3"
-}
-
-var toolchainFuchsiaSingleton Toolchain = &toolchainFuchsiaX8664{}
-
-func fuchsiaToolchainFactory(arch android.Arch) Toolchain {
-	return toolchainFuchsiaSingleton
-}
-
-func init() {
-	registerToolchainFactory(android.Fuchsia, android.X86_64, fuchsiaToolchainFactory)
-}
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/fuzz.go b/cc/fuzz.go
index c780b6f..8b0f93e 100644
--- a/cc/fuzz.go
+++ b/cc/fuzz.go
@@ -94,19 +94,14 @@
 	*binaryDecorator
 	*baseCompiler
 
-	Properties            FuzzProperties
-	dictionary            android.Path
-	corpus                android.Paths
-	corpusIntermediateDir android.Path
-	config                android.Path
-	data                  android.Paths
-	dataIntermediateDir   android.Path
-	installedSharedDeps   []string
+	fuzzPackagedModule FuzzPackagedModule
+
+	installedSharedDeps []string
 }
 
 func (fuzz *fuzzBinary) linkerProps() []interface{} {
 	props := fuzz.binaryDecorator.linkerProps()
-	props = append(props, &fuzz.Properties)
+	props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
 	return props
 }
 
@@ -257,41 +252,41 @@
 		"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
 	fuzz.binaryDecorator.baseInstaller.install(ctx, file)
 
-	fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus)
+	fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
 	builder := android.NewRuleBuilder(pctx, ctx)
 	intermediateDir := android.PathForModuleOut(ctx, "corpus")
-	for _, entry := range fuzz.corpus {
+	for _, entry := range fuzz.fuzzPackagedModule.Corpus {
 		builder.Command().Text("cp").
 			Input(entry).
 			Output(intermediateDir.Join(ctx, entry.Base()))
 	}
 	builder.Build("copy_corpus", "copy corpus")
-	fuzz.corpusIntermediateDir = intermediateDir
+	fuzz.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
 
-	fuzz.data = android.PathsForModuleSrc(ctx, fuzz.Properties.Data)
+	fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
 	builder = android.NewRuleBuilder(pctx, ctx)
 	intermediateDir = android.PathForModuleOut(ctx, "data")
-	for _, entry := range fuzz.data {
+	for _, entry := range fuzz.fuzzPackagedModule.Data {
 		builder.Command().Text("cp").
 			Input(entry).
 			Output(intermediateDir.Join(ctx, entry.Rel()))
 	}
 	builder.Build("copy_data", "copy data")
-	fuzz.dataIntermediateDir = intermediateDir
+	fuzz.fuzzPackagedModule.DataIntermediateDir = intermediateDir
 
-	if fuzz.Properties.Dictionary != nil {
-		fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary)
-		if fuzz.dictionary.Ext() != ".dict" {
+	if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
+		fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
+		if fuzz.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
 			ctx.PropertyErrorf("dictionary",
 				"Fuzzer dictionary %q does not have '.dict' extension",
-				fuzz.dictionary.String())
+				fuzz.fuzzPackagedModule.Dictionary.String())
 		}
 	}
 
-	if fuzz.Properties.Fuzz_config != nil {
+	if fuzz.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
 		configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
-		android.WriteFileRule(ctx, configPath, fuzz.Properties.Fuzz_config.String())
-		fuzz.config = configPath
+		android.WriteFileRule(ctx, configPath, fuzz.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
+		fuzz.fuzzPackagedModule.Config = configPath
 	}
 
 	// Grab the list of required shared libraries.
@@ -359,32 +354,20 @@
 
 // Responsible for generating GNU Make rules that package fuzz targets into
 // their architecture & target/host specific zip file.
-type fuzzPackager struct {
-	packages                android.Paths
+type ccFuzzPackager struct {
+	FuzzPackager
 	sharedLibInstallStrings []string
-	fuzzTargets             map[string]bool
 }
 
 func fuzzPackagingFactory() android.Singleton {
-	return &fuzzPackager{}
+	return &ccFuzzPackager{}
 }
 
-type fileToZip struct {
-	SourceFilePath        android.Path
-	DestinationPathPrefix string
-}
-
-type archOs struct {
-	hostOrTarget string
-	arch         string
-	dir          string
-}
-
-func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
+func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
 	// Map between each architecture + host/device combination, and the files that
 	// need to be packaged (in the tuple of {source file, destination folder in
 	// archive}).
-	archDirs := make(map[archOs][]fileToZip)
+	archDirs := make(map[ArchOs][]FileToZip)
 
 	// Map tracking whether each shared library has an install rule to avoid duplicate install rules from
 	// multiple fuzzers that depend on the same shared library.
@@ -392,12 +375,16 @@
 
 	// List of individual fuzz targets, so that 'make fuzz' also installs the targets
 	// to the correct output directories as well.
-	s.fuzzTargets = make(map[string]bool)
+	s.FuzzTargets = make(map[string]bool)
 
 	ctx.VisitAllModules(func(module android.Module) {
-		// Discard non-fuzz targets.
 		ccModule, ok := module.(*Module)
-		if !ok {
+		if !ok || ccModule.Properties.PreventInstall {
+			return
+		}
+
+		// Discard non-fuzz targets.
+		if ok := IsValid(ccModule.FuzzModule); !ok {
 			return
 		}
 
@@ -406,18 +393,6 @@
 			return
 		}
 
-		// Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of
-		// fuzz targets we're going to package anyway.
-		if !ccModule.Enabled() || ccModule.Properties.PreventInstall ||
-			ccModule.InRamdisk() || ccModule.InVendorRamdisk() || ccModule.InRecovery() {
-			return
-		}
-
-		// Discard modules that are in an unavailable namespace.
-		if !ccModule.ExportedToMake() {
-			return
-		}
-
 		hostOrTargetString := "target"
 		if ccModule.Host() {
 			hostOrTargetString = "host"
@@ -425,42 +400,21 @@
 
 		archString := ccModule.Arch().ArchType.String()
 		archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
-		archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()}
+		archOs := ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
 
 		// Grab the list of required shared libraries.
 		sharedLibraries := collectAllSharedDependencies(ctx, module)
 
-		var files []fileToZip
+		var files []FileToZip
 		builder := android.NewRuleBuilder(pctx, ctx)
 
-		// Package the corpora into a zipfile.
-		if fuzzModule.corpus != nil {
-			corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip")
-			command := builder.Command().BuiltTool("soong_zip").
-				Flag("-j").
-				FlagWithOutput("-o ", corpusZip)
-			rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
-			command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.corpus)
-			files = append(files, fileToZip{corpusZip, ""})
-		}
-
-		// Package the data into a zipfile.
-		if fuzzModule.data != nil {
-			dataZip := archDir.Join(ctx, module.Name()+"_data.zip")
-			command := builder.Command().BuiltTool("soong_zip").
-				FlagWithOutput("-o ", dataZip)
-			for _, f := range fuzzModule.data {
-				intermediateDir := strings.TrimSuffix(f.String(), f.Rel())
-				command.FlagWithArg("-C ", intermediateDir)
-				command.FlagWithInput("-f ", f)
-			}
-			files = append(files, fileToZip{dataZip, ""})
-		}
+		// Package the corpus, data, dict and config into a zipfile.
+		files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
 
 		// Find and mark all the transiently-dependent shared libraries for
 		// packaging.
 		for _, library := range sharedLibraries {
-			files = append(files, fileToZip{library, "lib"})
+			files = append(files, FileToZip{library, "lib"})
 
 			// For each architecture-specific shared library dependency, we need to
 			// install it to the output directory. Setup the install destination here,
@@ -492,83 +446,20 @@
 		}
 
 		// The executable.
-		files = append(files, fileToZip{ccModule.UnstrippedOutputFile(), ""})
+		files = append(files, FileToZip{ccModule.UnstrippedOutputFile(), ""})
 
-		// The dictionary.
-		if fuzzModule.dictionary != nil {
-			files = append(files, fileToZip{fuzzModule.dictionary, ""})
+		archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
+		if !ok {
+			return
 		}
-
-		// Additional fuzz config.
-		if fuzzModule.config != nil {
-			files = append(files, fileToZip{fuzzModule.config, ""})
-		}
-
-		fuzzZip := archDir.Join(ctx, module.Name()+".zip")
-		command := builder.Command().BuiltTool("soong_zip").
-			Flag("-j").
-			FlagWithOutput("-o ", fuzzZip)
-		for _, file := range files {
-			if file.DestinationPathPrefix != "" {
-				command.FlagWithArg("-P ", file.DestinationPathPrefix)
-			} else {
-				command.Flag("-P ''")
-			}
-			command.FlagWithInput("-f ", file.SourceFilePath)
-		}
-
-		builder.Build("create-"+fuzzZip.String(),
-			"Package "+module.Name()+" for "+archString+"-"+hostOrTargetString)
-
-		// Don't add modules to 'make haiku' that are set to not be exported to the
-		// fuzzing infrastructure.
-		if config := fuzzModule.Properties.Fuzz_config; config != nil {
-			if ccModule.Host() && !BoolDefault(config.Fuzz_on_haiku_host, true) {
-				return
-			} else if !BoolDefault(config.Fuzz_on_haiku_device, true) {
-				return
-			}
-		}
-
-		s.fuzzTargets[module.Name()] = true
-		archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""})
 	})
 
-	var archOsList []archOs
-	for archOs := range archDirs {
-		archOsList = append(archOsList, archOs)
-	}
-	sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].dir < archOsList[j].dir })
+	s.CreateFuzzPackage(ctx, archDirs, Cc)
 
-	for _, archOs := range archOsList {
-		filesToZip := archDirs[archOs]
-		arch := archOs.arch
-		hostOrTarget := archOs.hostOrTarget
-		builder := android.NewRuleBuilder(pctx, ctx)
-		outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip")
-		s.packages = append(s.packages, outputFile)
-
-		command := builder.Command().BuiltTool("soong_zip").
-			Flag("-j").
-			FlagWithOutput("-o ", outputFile).
-			Flag("-L 0") // No need to try and re-compress the zipfiles.
-
-		for _, fileToZip := range filesToZip {
-			if fileToZip.DestinationPathPrefix != "" {
-				command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix)
-			} else {
-				command.Flag("-P ''")
-			}
-			command.FlagWithInput("-f ", fileToZip.SourceFilePath)
-		}
-
-		builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget,
-			"Create fuzz target packages for "+arch+"-"+hostOrTarget)
-	}
 }
 
-func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) {
-	packages := s.packages.Strings()
+func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
+	packages := s.Packages.Strings()
 	sort.Strings(packages)
 	sort.Strings(s.sharedLibInstallStrings)
 	// TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
@@ -580,10 +471,5 @@
 		strings.Join(s.sharedLibInstallStrings, " "))
 
 	// Preallocate the slice of fuzz targets to minimise memory allocations.
-	fuzzTargets := make([]string, 0, len(s.fuzzTargets))
-	for target, _ := range s.fuzzTargets {
-		fuzzTargets = append(fuzzTargets, target)
-	}
-	sort.Strings(fuzzTargets)
-	ctx.Strict("ALL_FUZZ_TARGETS", strings.Join(fuzzTargets, " "))
+	s.PreallocateSlice(ctx, "ALL_FUZZ_TARGETS")
 }
diff --git a/cc/fuzz_common.go b/cc/fuzz_common.go
new file mode 100644
index 0000000..98ed7f4
--- /dev/null
+++ b/cc/fuzz_common.go
@@ -0,0 +1,201 @@
+// 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 cc
+
+// This file contains the common code for compiling C/C++ and Rust fuzzers for Android.
+
+import (
+	"sort"
+	"strings"
+
+	"android/soong/android"
+)
+
+type Lang string
+
+const (
+	Cc   Lang = ""
+	Rust Lang = "rust"
+)
+
+type FuzzModule struct {
+	android.ModuleBase
+	android.DefaultableModuleBase
+	android.ApexModuleBase
+}
+
+type FuzzPackager struct {
+	Packages    android.Paths
+	FuzzTargets map[string]bool
+}
+
+type FileToZip struct {
+	SourceFilePath        android.Path
+	DestinationPathPrefix string
+}
+
+type ArchOs struct {
+	HostOrTarget string
+	Arch         string
+	Dir          string
+}
+
+type FuzzPackagedModule struct {
+	FuzzProperties        FuzzProperties
+	Dictionary            android.Path
+	Corpus                android.Paths
+	CorpusIntermediateDir android.Path
+	Config                android.Path
+	Data                  android.Paths
+	DataIntermediateDir   android.Path
+}
+
+func IsValid(fuzzModule FuzzModule) bool {
+	// Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of
+	// fuzz targets we're going to package anyway.
+	if !fuzzModule.Enabled() || fuzzModule.InRamdisk() || fuzzModule.InVendorRamdisk() || fuzzModule.InRecovery() {
+		return false
+	}
+
+	// Discard modules that are in an unavailable namespace.
+	if !fuzzModule.ExportedToMake() {
+		return false
+	}
+
+	return true
+}
+
+func (s *FuzzPackager) PackageArtifacts(ctx android.SingletonContext, module android.Module, fuzzModule FuzzPackagedModule, archDir android.OutputPath, builder *android.RuleBuilder) []FileToZip {
+	// Package the corpora into a zipfile.
+	var files []FileToZip
+	if fuzzModule.Corpus != nil {
+		corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip")
+		command := builder.Command().BuiltTool("soong_zip").
+			Flag("-j").
+			FlagWithOutput("-o ", corpusZip)
+		rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
+		command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.Corpus)
+		files = append(files, FileToZip{corpusZip, ""})
+	}
+
+	// Package the data into a zipfile.
+	if fuzzModule.Data != nil {
+		dataZip := archDir.Join(ctx, module.Name()+"_data.zip")
+		command := builder.Command().BuiltTool("soong_zip").
+			FlagWithOutput("-o ", dataZip)
+		for _, f := range fuzzModule.Data {
+			intermediateDir := strings.TrimSuffix(f.String(), f.Rel())
+			command.FlagWithArg("-C ", intermediateDir)
+			command.FlagWithInput("-f ", f)
+		}
+		files = append(files, FileToZip{dataZip, ""})
+	}
+
+	// The dictionary.
+	if fuzzModule.Dictionary != nil {
+		files = append(files, FileToZip{fuzzModule.Dictionary, ""})
+	}
+
+	// Additional fuzz config.
+	if fuzzModule.Config != nil {
+		files = append(files, FileToZip{fuzzModule.Config, ""})
+	}
+
+	return files
+}
+
+func (s *FuzzPackager) BuildZipFile(ctx android.SingletonContext, module android.Module, fuzzModule FuzzPackagedModule, files []FileToZip, builder *android.RuleBuilder, archDir android.OutputPath, archString string, hostOrTargetString string, archOs ArchOs, archDirs map[ArchOs][]FileToZip) ([]FileToZip, bool) {
+	fuzzZip := archDir.Join(ctx, module.Name()+".zip")
+
+	command := builder.Command().BuiltTool("soong_zip").
+		Flag("-j").
+		FlagWithOutput("-o ", fuzzZip)
+
+	for _, file := range files {
+		if file.DestinationPathPrefix != "" {
+			command.FlagWithArg("-P ", file.DestinationPathPrefix)
+		} else {
+			command.Flag("-P ''")
+		}
+		command.FlagWithInput("-f ", file.SourceFilePath)
+	}
+
+	builder.Build("create-"+fuzzZip.String(),
+		"Package "+module.Name()+" for "+archString+"-"+hostOrTargetString)
+
+	// Don't add modules to 'make haiku-rust' that are set to not be
+	// exported to the fuzzing infrastructure.
+	if config := fuzzModule.FuzzProperties.Fuzz_config; config != nil {
+		if strings.Contains(hostOrTargetString, "host") && !BoolDefault(config.Fuzz_on_haiku_host, true) {
+			return archDirs[archOs], false
+		} else if !BoolDefault(config.Fuzz_on_haiku_device, true) {
+			return archDirs[archOs], false
+		}
+	}
+
+	s.FuzzTargets[module.Name()] = true
+	archDirs[archOs] = append(archDirs[archOs], FileToZip{fuzzZip, ""})
+
+	return archDirs[archOs], true
+}
+
+func (s *FuzzPackager) CreateFuzzPackage(ctx android.SingletonContext, archDirs map[ArchOs][]FileToZip, lang Lang) {
+	var archOsList []ArchOs
+	for archOs := range archDirs {
+		archOsList = append(archOsList, archOs)
+	}
+	sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].Dir < archOsList[j].Dir })
+
+	for _, archOs := range archOsList {
+		filesToZip := archDirs[archOs]
+		arch := archOs.Arch
+		hostOrTarget := archOs.HostOrTarget
+		builder := android.NewRuleBuilder(pctx, ctx)
+		zipFileName := "fuzz-" + hostOrTarget + "-" + arch + ".zip"
+		if lang == Rust {
+			zipFileName = "fuzz-rust-" + hostOrTarget + "-" + arch + ".zip"
+		}
+		outputFile := android.PathForOutput(ctx, zipFileName)
+
+		s.Packages = append(s.Packages, outputFile)
+
+		command := builder.Command().BuiltTool("soong_zip").
+			Flag("-j").
+			FlagWithOutput("-o ", outputFile).
+			Flag("-L 0") // No need to try and re-compress the zipfiles.
+
+		for _, fileToZip := range filesToZip {
+
+			if fileToZip.DestinationPathPrefix != "" {
+				command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix)
+			} else {
+				command.Flag("-P ''")
+			}
+			command.FlagWithInput("-f ", fileToZip.SourceFilePath)
+
+		}
+		builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget,
+			"Create fuzz target packages for "+arch+"-"+hostOrTarget)
+	}
+}
+
+func (s *FuzzPackager) PreallocateSlice(ctx android.MakeVarsContext, targets string) {
+	fuzzTargets := make([]string, 0, len(s.FuzzTargets))
+	for target, _ := range s.FuzzTargets {
+		fuzzTargets = append(fuzzTargets, target)
+	}
+	sort.Strings(fuzzTargets)
+	ctx.Strict(targets, strings.Join(fuzzTargets, " "))
+}
diff --git a/cc/genrule.go b/cc/genrule.go
index b0efc6c..0ca901e 100644
--- a/cc/genrule.go
+++ b/cc/genrule.go
@@ -17,6 +17,7 @@
 import (
 	"android/soong/android"
 	"android/soong/genrule"
+	"android/soong/snapshot"
 )
 
 func init() {
@@ -84,7 +85,7 @@
 	// is not needed.
 	recoverySnapshotVersion := ctx.DeviceConfig().RecoverySnapshotVersion()
 	if recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" &&
-		!isRecoveryProprietaryModule(ctx) {
+		!snapshot.IsRecoveryProprietaryModule(ctx) {
 		return false
 	} else {
 		return Bool(g.Recovery_available)
@@ -103,7 +104,7 @@
 		// If not, we assume modules under proprietary paths are compatible for
 		// BOARD_VNDK_VERSION. The other modules are regarded as AOSP, that is
 		// PLATFORM_VNDK_VERSION.
-		if vndkVersion == "current" || !IsVendorProprietaryModule(ctx) {
+		if vndkVersion == "current" || !snapshot.IsVendorProprietaryModule(ctx) {
 			variants = append(variants, VendorVariationPrefix+ctx.DeviceConfig().PlatformVndkVersion())
 		} else {
 			variants = append(variants, VendorVariationPrefix+vndkVersion)
diff --git a/cc/image.go b/cc/image.go
index 15ec1c8..3a0857b 100644
--- a/cc/image.go
+++ b/cc/image.go
@@ -22,6 +22,7 @@
 	"strings"
 
 	"android/soong/android"
+	"android/soong/snapshot"
 )
 
 var _ android.ImageInterface = (*Module)(nil)
@@ -496,7 +497,7 @@
 		// BOARD_VNDK_VERSION. The other modules are regarded as AOSP, or
 		// PLATFORM_VNDK_VERSION.
 		if m.HasVendorVariant() {
-			if IsVendorProprietaryModule(mctx) {
+			if snapshot.IsVendorProprietaryModule(mctx) {
 				vendorVariants = append(vendorVariants, boardVndkVersion)
 			} else {
 				vendorVariants = append(vendorVariants, platformVndkVersion)
@@ -525,7 +526,7 @@
 				platformVndkVersion,
 				boardVndkVersion,
 			)
-		} else if IsVendorProprietaryModule(mctx) {
+		} else if snapshot.IsVendorProprietaryModule(mctx) {
 			vendorVariants = append(vendorVariants, boardVndkVersion)
 		} else {
 			vendorVariants = append(vendorVariants, platformVndkVersion)
@@ -582,7 +583,7 @@
 	if !m.KernelHeadersDecorator() &&
 		!m.IsSnapshotPrebuilt() &&
 		usingRecoverySnapshot &&
-		!isRecoveryProprietaryModule(mctx) {
+		!snapshot.IsRecoveryProprietaryModule(mctx) {
 		recoveryVariantNeeded = false
 	}
 
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/linkable.go b/cc/linkable.go
index 6232efb..b510508 100644
--- a/cc/linkable.go
+++ b/cc/linkable.go
@@ -3,6 +3,7 @@
 import (
 	"android/soong/android"
 	"android/soong/bazel/cquery"
+	"android/soong/snapshot"
 
 	"github.com/google/blueprint"
 )
@@ -71,15 +72,12 @@
 
 // Snapshottable defines those functions necessary for handling module snapshots.
 type Snapshottable interface {
+	snapshot.VendorSnapshotModuleInterface
+	snapshot.RecoverySnapshotModuleInterface
+
 	// SnapshotHeaders returns a list of header paths provided by this module.
 	SnapshotHeaders() android.Paths
 
-	// ExcludeFromVendorSnapshot returns true if this module should be otherwise excluded from the vendor snapshot.
-	ExcludeFromVendorSnapshot() bool
-
-	// ExcludeFromRecoverySnapshot returns true if this module should be otherwise excluded from the recovery snapshot.
-	ExcludeFromRecoverySnapshot() bool
-
 	// SnapshotLibrary returns true if this module is a snapshot library.
 	IsSnapshotLibrary() bool
 
diff --git a/cc/linker.go b/cc/linker.go
index a712391..a12a018 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -15,9 +15,10 @@
 package cc
 
 import (
+	"fmt"
+
 	"android/soong/android"
 	"android/soong/cc/config"
-	"fmt"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -45,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.
@@ -200,6 +194,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"`
 
@@ -238,19 +235,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 ctx.toolchain().Bionic() && linker.Properties.System_shared_libs != nil {
-		// system_shared_libs is only honored when building against bionic.
-		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() {
@@ -351,13 +335,13 @@
 		deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_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,22 +369,14 @@
 			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...)
 
-	if ctx.Fuchsia() {
-		if ctx.ModuleName() != "libbioniccompat" &&
-			ctx.ModuleName() != "libcompiler_rt-extras" &&
-			ctx.ModuleName() != "libcompiler_rt" {
-			deps.StaticLibs = append(deps.StaticLibs, "libbioniccompat")
-		}
-		if ctx.ModuleName() != "libcompiler_rt" && ctx.ModuleName() != "libcompiler_rt-extras" {
-			deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt")
-		}
-
-	}
-
 	if ctx.Windows() {
 		deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
 	}
@@ -484,7 +460,7 @@
 		flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Ldflags())
 	}
 
-	if !ctx.toolchain().Bionic() && !ctx.Fuchsia() {
+	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...)
@@ -503,10 +479,6 @@
 		}
 	}
 
-	if ctx.Fuchsia() {
-		flags.Global.LdFlags = append(flags.Global.LdFlags, "-lfdio", "-lzircon")
-	}
-
 	if ctx.toolchain().LibclangRuntimeLibraryArch() != "" {
 		flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--exclude-libs="+config.BuiltinsRuntimeLibrary(ctx.toolchain())+".a")
 	}
@@ -573,6 +545,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
@@ -593,11 +576,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/makevars.go b/cc/makevars.go
index 393170a..8d7a163 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -165,7 +165,7 @@
 	sort.Strings(ndkKnownLibs)
 	ctx.Strict("NDK_KNOWN_LIBS", strings.Join(ndkKnownLibs, " "))
 
-	hostTargets := ctx.Config().Targets[android.BuildOs]
+	hostTargets := ctx.Config().Targets[ctx.Config().BuildOS]
 	makeVarsToolchain(ctx, "", hostTargets[0])
 	if len(hostTargets) > 1 {
 		makeVarsToolchain(ctx, "2ND_", hostTargets[1])
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index a458380..63e8261 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -150,12 +150,6 @@
 	if !ctx.Module().Enabled() {
 		return nil
 	}
-	if ctx.Os() != android.Android {
-		// These modules are always android.DeviceEnabled only, but
-		// those include Fuchsia devices, which we don't support.
-		ctx.Module().Disable()
-		return nil
-	}
 	if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
 		ctx.Module().Disable()
 		return nil
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/prebuilt_test.go b/cc/prebuilt_test.go
index fa6dd87..c8f8103 100644
--- a/cc/prebuilt_test.go
+++ b/cc/prebuilt_test.go
@@ -15,6 +15,7 @@
 package cc
 
 import (
+	"runtime"
 	"testing"
 
 	"android/soong/android"
@@ -271,8 +272,8 @@
 }
 
 func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
-	if android.BuildOs != android.Linux {
-		t.Skipf("Skipping host prebuilt testing that is only supported on %s not %s", android.Linux, android.BuildOs)
+	if runtime.GOOS != "linux" {
+		t.Skipf("Skipping host prebuilt testing that is only supported on linux not %s", runtime.GOOS)
 	}
 
 	ctx := testPrebuilt(t, `
diff --git a/cc/proto_test.go b/cc/proto_test.go
index b9c89c7..abcb273 100644
--- a/cc/proto_test.go
+++ b/cc/proto_test.go
@@ -51,7 +51,7 @@
 			},
 		}`)
 
-		buildOS := android.BuildOs.String()
+		buildOS := ctx.Config().BuildOS.String()
 
 		proto := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Output("proto/a.pb.cc")
 		foobar := ctx.ModuleForTests("protoc-gen-foobar", buildOS+"_x86_64")
diff --git a/cc/sabi.go b/cc/sabi.go
index 5fd6f5d..e62ca66 100644
--- a/cc/sabi.go
+++ b/cc/sabi.go
@@ -101,10 +101,6 @@
 // Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
 // ctx should be wrapping a native library type module.
 func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
-	if ctx.Fuchsia() {
-		return false
-	}
-
 	// Only generate ABI dump for device modules.
 	if !ctx.Device() {
 		return false
diff --git a/cc/sanitize.go b/cc/sanitize.go
index defe8fd..b244394 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -25,6 +25,7 @@
 
 	"android/soong/android"
 	"android/soong/cc/config"
+	"android/soong/snapshot"
 )
 
 var (
@@ -265,7 +266,6 @@
 }
 
 type SanitizeProperties struct {
-	// Sanitizers are not supported for Fuchsia.
 	Sanitize          SanitizeUserProps `android:"arch_variant"`
 	SanitizerEnabled  bool              `blueprint:"mutated"`
 	SanitizeDep       bool              `blueprint:"mutated"`
@@ -305,11 +305,6 @@
 		s.Never = BoolPtr(true)
 	}
 
-	// Sanitizers do not work on Fuchsia yet.
-	if ctx.Fuchsia() {
-		s.Never = BoolPtr(true)
-	}
-
 	// Never always wins.
 	if Bool(s.Never) {
 		return
@@ -477,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
@@ -907,7 +902,7 @@
 // as vendor snapshot. Such modules must create both cfi and non-cfi variants,
 // except for ones which explicitly disable cfi.
 func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
-	if IsVendorProprietaryModule(mctx) {
+	if snapshot.IsVendorProprietaryModule(mctx) {
 		return false
 	}
 
diff --git a/cc/sdk.go b/cc/sdk.go
index 69ad311..a83e5ad 100644
--- a/cc/sdk.go
+++ b/cc/sdk.go
@@ -76,7 +76,7 @@
 			}
 			ctx.AliasVariation("")
 		}
-	case *snapshot:
+	case *snapshotModule:
 		ctx.CreateVariations("")
 	}
 }
diff --git a/cc/snapshot_prebuilt.go b/cc/snapshot_prebuilt.go
index 4f031ff..9672c0f 100644
--- a/cc/snapshot_prebuilt.go
+++ b/cc/snapshot_prebuilt.go
@@ -18,57 +18,17 @@
 // snapshot mutators and snapshot information maps which are also defined in this file.
 
 import (
-	"path/filepath"
 	"strings"
 
 	"android/soong/android"
+	"android/soong/snapshot"
 
 	"github.com/google/blueprint"
 )
 
-// Defines the specifics of different images to which the snapshot process is applicable, e.g.,
-// vendor, recovery, ramdisk.
+// This interface overrides snapshot.SnapshotImage to implement cc module specific functions
 type SnapshotImage interface {
-	// Returns true if a snapshot should be generated for this image.
-	shouldGenerateSnapshot(ctx android.SingletonContext) bool
-
-	// Function that returns true if the module is included in this image.
-	// Using a function return instead of a value to prevent early
-	// evalution of a function that may be not be defined.
-	inImage(m LinkableInterface) func() bool
-
-	// Returns true if the module is private and must not be included in the
-	// snapshot. For example VNDK-private modules must return true for the
-	// vendor snapshots. But false for the recovery snapshots.
-	private(m LinkableInterface) bool
-
-	// Returns true if a dir under source tree is an SoC-owned proprietary
-	// directory, such as device/, vendor/, etc.
-	//
-	// For a given snapshot (e.g., vendor, recovery, etc.) if
-	// isProprietaryPath(dir, deviceConfig) returns true, then the module in dir
-	// will be built from sources.
-	isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool
-
-	// Whether to include VNDK in the snapshot for this image.
-	includeVndk() bool
-
-	// Whether a given module has been explicitly excluded from the
-	// snapshot, e.g., using the exclude_from_vendor_snapshot or
-	// exclude_from_recovery_snapshot properties.
-	excludeFromSnapshot(m LinkableInterface) bool
-
-	// Returns true if the build is using a snapshot for this image.
-	isUsingSnapshot(cfg android.DeviceConfig) bool
-
-	// Returns a version of which the snapshot should be used in this target.
-	// This will only be meaningful when isUsingSnapshot is true.
-	targetSnapshotVersion(cfg android.DeviceConfig) string
-
-	// Whether to exclude a given module from the directed snapshot or not.
-	// If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
-	// and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
-	excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
+	snapshot.SnapshotImage
 
 	// The image variant name for this snapshot image.
 	// For example, recovery snapshot image will return "recovery", and vendor snapshot image will
@@ -80,110 +40,12 @@
 	moduleNameSuffix() string
 }
 
-type vendorSnapshotImage struct{}
-type recoverySnapshotImage struct{}
-
-type directoryMap map[string]bool
-
-var (
-	// Modules under following directories are ignored. They are OEM's and vendor's
-	// proprietary modules(device/, kernel/, vendor/, and hardware/).
-	defaultDirectoryExcludedMap = directoryMap{
-		"device":   true,
-		"hardware": true,
-		"kernel":   true,
-		"vendor":   true,
-	}
-
-	// Modules under following directories are included as they are in AOSP,
-	// although hardware/ and kernel/ are normally for vendor's own.
-	defaultDirectoryIncludedMap = directoryMap{
-		"kernel/configs":              true,
-		"kernel/prebuilts":            true,
-		"kernel/tests":                true,
-		"hardware/interfaces":         true,
-		"hardware/libhardware":        true,
-		"hardware/libhardware_legacy": true,
-		"hardware/ril":                true,
-	}
-)
-
-func (vendorSnapshotImage) Init(ctx android.RegistrationContext) {
-	ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
-	ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
-	ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
-	ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
-	ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
-	ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
-	ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
-
-	ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
+type vendorSnapshotImage struct {
+	*snapshot.VendorSnapshotImage
 }
 
-func (vendorSnapshotImage) RegisterAdditionalModule(ctx android.RegistrationContext, name string, factory android.ModuleFactory) {
-	ctx.RegisterModuleType(name, factory)
-}
-
-func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
-	// BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
-	return ctx.DeviceConfig().VndkVersion() == "current"
-}
-
-func (vendorSnapshotImage) inImage(m LinkableInterface) func() bool {
-	return m.InVendor
-}
-
-func (vendorSnapshotImage) private(m LinkableInterface) bool {
-	return m.IsVndkPrivate()
-}
-
-func isDirectoryExcluded(dir string, excludedMap directoryMap, includedMap directoryMap) bool {
-	if dir == "." || dir == "/" {
-		return false
-	}
-	if includedMap[dir] {
-		return false
-	} else if excludedMap[dir] {
-		return true
-	} else if defaultDirectoryIncludedMap[dir] {
-		return false
-	} else if defaultDirectoryExcludedMap[dir] {
-		return true
-	} else {
-		return isDirectoryExcluded(filepath.Dir(dir), excludedMap, includedMap)
-	}
-}
-
-func (vendorSnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
-	return isDirectoryExcluded(dir, deviceConfig.VendorSnapshotDirsExcludedMap(), deviceConfig.VendorSnapshotDirsIncludedMap())
-}
-
-// vendor snapshot includes static/header libraries with vndk: {enabled: true}.
-func (vendorSnapshotImage) includeVndk() bool {
-	return true
-}
-
-func (vendorSnapshotImage) excludeFromSnapshot(m LinkableInterface) bool {
-	return m.ExcludeFromVendorSnapshot()
-}
-
-func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
-	vndkVersion := cfg.VndkVersion()
-	return vndkVersion != "current" && vndkVersion != ""
-}
-
-func (vendorSnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
-	return cfg.VndkVersion()
-}
-
-// returns true iff a given module SHOULD BE EXCLUDED, false if included
-func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
-	// If we're using full snapshot, not directed snapshot, capture every module
-	if !cfg.DirectedVendorSnapshot() {
-		return false
-	}
-	// Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
-	return !cfg.VendorSnapshotModules()[name]
+type recoverySnapshotImage struct {
+	*snapshot.RecoverySnapshotImage
 }
 
 func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
@@ -194,62 +56,6 @@
 	return VendorSuffix
 }
 
-func (recoverySnapshotImage) init(ctx android.RegistrationContext) {
-	ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
-	ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
-	ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
-	ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
-	ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
-	ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
-	ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
-}
-
-func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
-	// RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
-	// snapshot.
-	return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
-}
-
-func (recoverySnapshotImage) inImage(m LinkableInterface) func() bool {
-	return m.InRecovery
-}
-
-// recovery snapshot does not have private libraries.
-func (recoverySnapshotImage) private(m LinkableInterface) bool {
-	return false
-}
-
-func (recoverySnapshotImage) isProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
-	return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap())
-}
-
-// recovery snapshot does NOT treat vndk specially.
-func (recoverySnapshotImage) includeVndk() bool {
-	return false
-}
-
-func (recoverySnapshotImage) excludeFromSnapshot(m LinkableInterface) bool {
-	return m.ExcludeFromRecoverySnapshot()
-}
-
-func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
-	recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
-	return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
-}
-
-func (recoverySnapshotImage) targetSnapshotVersion(cfg android.DeviceConfig) string {
-	return cfg.RecoverySnapshotVersion()
-}
-
-func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
-	// If we're using full snapshot, not directed snapshot, capture every module
-	if !cfg.DirectedRecoverySnapshot() {
-		return false
-	}
-	// Else, checks if name is in RECOVERY_SNAPSHOT_MODULES.
-	return !cfg.RecoverySnapshotModules()[name]
-}
-
 func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
 	return android.RecoveryVariation
 }
@@ -258,12 +64,31 @@
 	return recoverySuffix
 }
 
-var VendorSnapshotImageSingleton vendorSnapshotImage
-var recoverySnapshotImageSingleton recoverySnapshotImage
+// Override existing vendor and recovery snapshot for cc module specific extra functions
+var VendorSnapshotImageSingleton vendorSnapshotImage = vendorSnapshotImage{&snapshot.VendorSnapshotImageSingleton}
+var recoverySnapshotImageSingleton recoverySnapshotImage = recoverySnapshotImage{&snapshot.RecoverySnapshotImageSingleton}
+
+func RegisterVendorSnapshotModules(ctx android.RegistrationContext) {
+	ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
+	ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
+	ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
+	ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
+	ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
+	ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
+}
+
+func RegisterRecoverySnapshotModules(ctx android.RegistrationContext) {
+	ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
+	ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
+	ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
+	ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
+	ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
+	ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
+}
 
 func init() {
-	VendorSnapshotImageSingleton.Init(android.InitRegistrationContext)
-	recoverySnapshotImageSingleton.init(android.InitRegistrationContext)
+	RegisterVendorSnapshotModules(android.InitRegistrationContext)
+	RegisterRecoverySnapshotModules(android.InitRegistrationContext)
 	android.RegisterMakeVarsProvider(pctx, snapshotMakeVarsProvider)
 }
 
@@ -285,8 +110,7 @@
 	Binaries    []string `android:"arch_variant"`
 	Objects     []string `android:"arch_variant"`
 }
-
-type snapshot struct {
+type snapshotModule struct {
 	android.ModuleBase
 
 	properties SnapshotProperties
@@ -296,41 +120,41 @@
 	image SnapshotImage
 }
 
-func (s *snapshot) ImageMutatorBegin(ctx android.BaseModuleContext) {
+func (s *snapshotModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
 	cfg := ctx.DeviceConfig()
-	if !s.image.isUsingSnapshot(cfg) || s.image.targetSnapshotVersion(cfg) != s.baseSnapshot.Version() {
+	if !s.image.IsUsingSnapshot(cfg) || s.image.TargetSnapshotVersion(cfg) != s.baseSnapshot.Version() {
 		s.Disable()
 	}
 }
 
-func (s *snapshot) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
+func (s *snapshotModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
 	return false
 }
 
-func (s *snapshot) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+func (s *snapshotModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
 	return false
 }
 
-func (s *snapshot) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+func (s *snapshotModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
 	return false
 }
 
-func (s *snapshot) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
+func (s *snapshotModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
 	return false
 }
 
-func (s *snapshot) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
+func (s *snapshotModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
 	return false
 }
 
-func (s *snapshot) ExtraImageVariations(ctx android.BaseModuleContext) []string {
+func (s *snapshotModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
 	return []string{s.image.imageVariantName(ctx.DeviceConfig())}
 }
 
-func (s *snapshot) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
+func (s *snapshotModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
 }
 
-func (s *snapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+func (s *snapshotModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	// Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
 }
 
@@ -342,7 +166,7 @@
 	return moduleSuffix + versionSuffix
 }
 
-func (s *snapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
+func (s *snapshotModule) DepsMutator(ctx android.BottomUpMutatorContext) {
 	collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
 		snapshotMap := make(map[string]string)
 		for _, name := range names {
@@ -382,12 +206,12 @@
 
 var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
 
-var _ android.ImageInterface = (*snapshot)(nil)
+var _ android.ImageInterface = (*snapshotModule)(nil)
 
 func snapshotMakeVarsProvider(ctx android.MakeVarsContext) {
 	snapshotSet := map[string]struct{}{}
 	ctx.VisitAllModules(func(m android.Module) {
-		if s, ok := m.(*snapshot); ok {
+		if s, ok := m.(*snapshotModule); ok {
 			if _, ok := snapshotSet[s.Name()]; ok {
 				// arch variant generates duplicated modules
 				// skip this as we only need to know the path of the module.
@@ -411,13 +235,13 @@
 }
 
 func snapshotFactory(image SnapshotImage) android.Module {
-	snapshot := &snapshot{}
-	snapshot.image = image
-	snapshot.AddProperties(
-		&snapshot.properties,
-		&snapshot.baseSnapshot.baseProperties)
-	android.InitAndroidArchModule(snapshot, android.DeviceSupported, android.MultilibBoth)
-	return snapshot
+	snapshotModule := &snapshotModule{}
+	snapshotModule.image = image
+	snapshotModule.AddProperties(
+		&snapshotModule.properties,
+		&snapshotModule.baseSnapshot.baseProperties)
+	android.InitAndroidArchModule(snapshotModule, android.DeviceSupported, android.MultilibBoth)
+	return snapshotModule
 }
 
 type BaseSnapshotDecoratorProperties struct {
@@ -449,7 +273,7 @@
 // will be seen as "libbase.vendor_static.30.arm64" by Soong.
 type BaseSnapshotDecorator struct {
 	baseProperties BaseSnapshotDecoratorProperties
-	image          SnapshotImage
+	Image          SnapshotImage
 }
 
 func (p *BaseSnapshotDecorator) Name(name string) string {
@@ -489,7 +313,7 @@
 		Variation: android.CoreVariation})
 
 	if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
-		p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
+		p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
 		return
 	}
 
@@ -498,14 +322,14 @@
 		Variation: ProductVariationPrefix + ctx.DeviceConfig().PlatformVndkVersion()})
 
 	if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
-		p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
+		p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
 		return
 	}
 
 	images := []SnapshotImage{VendorSnapshotImageSingleton, recoverySnapshotImageSingleton}
 
 	for _, image := range images {
-		if p.image == image {
+		if p.Image == image {
 			continue
 		}
 		variations = append(ctx.Target().Variations(), blueprint.Variation{
@@ -518,7 +342,7 @@
 					image.moduleNameSuffix()+variant,
 					p.Version(),
 					ctx.DeviceConfig().Arches()[0].ArchType.String())) {
-			p.baseProperties.Androidmk_suffix = p.image.moduleNameSuffix()
+			p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
 			return
 		}
 	}
@@ -529,7 +353,7 @@
 // Call this with a module suffix after creating a snapshot module, such as
 // vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
 func (p *BaseSnapshotDecorator) Init(m LinkableInterface, image SnapshotImage, moduleSuffix string) {
-	p.image = image
+	p.Image = image
 	p.baseProperties.ModuleSuffix = image.moduleNameSuffix() + moduleSuffix
 	m.AddProperties(&p.baseProperties)
 	android.AddLoadHook(m, func(ctx android.LoadHookContext) {
diff --git a/cc/snapshot_utils.go b/cc/snapshot_utils.go
index b0538be..24abcce 100644
--- a/cc/snapshot_utils.go
+++ b/cc/snapshot_utils.go
@@ -114,7 +114,7 @@
 	}
 
 	for _, image := range []SnapshotImage{VendorSnapshotImageSingleton, recoverySnapshotImageSingleton} {
-		if isSnapshotAware(ctx.DeviceConfig(), m, image.isProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()), apexInfo, image) {
+		if isSnapshotAware(ctx.DeviceConfig(), m, image.IsProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()), apexInfo, image) {
 			return true
 		}
 	}
diff --git a/cc/stl.go b/cc/stl.go
index 06dc840..0f2a878 100644
--- a/cc/stl.go
+++ b/cc/stl.go
@@ -15,8 +15,9 @@
 package cc
 
 import (
-	"android/soong/android"
 	"fmt"
+
+	"android/soong/android"
 )
 
 func getNdkStlFamily(m LinkableInterface) string {
@@ -92,26 +93,6 @@
 				ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
 				return ""
 			}
-		} else if ctx.Fuchsia() {
-			switch s {
-			case "c++_static":
-				return "libc++_static"
-			case "c++_shared":
-				return "libc++"
-			case "libc++", "libc++_static":
-				return s
-			case "none":
-				return ""
-			case "":
-				if ctx.static() {
-					return "libc++_static"
-				} else {
-					return "libc++"
-				}
-			default:
-				ctx.ModuleErrorf("stl: %q is not a supported STL on Fuchsia", s)
-				return ""
-			}
 		} else {
 			switch s {
 			case "libc++", "libc++_static":
diff --git a/cc/testing.go b/cc/testing.go
index b9d84f6..e8f3481 100644
--- a/cc/testing.go
+++ b/cc/testing.go
@@ -20,6 +20,7 @@
 
 	"android/soong/android"
 	"android/soong/genrule"
+	"android/soong/snapshot"
 )
 
 func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
@@ -44,9 +45,6 @@
 
 	supportLinuxBionic := false
 	for _, os := range oses {
-		if os == android.Fuchsia {
-			ret += withFuchsiaModules()
-		}
 		if os == android.Windows {
 			ret += withWindowsModules()
 		}
@@ -367,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",
@@ -452,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",
+		}
 	`
 }
 
@@ -471,19 +488,6 @@
 		`
 }
 
-func withFuchsiaModules() string {
-	return `
-		cc_library {
-			name: "libbioniccompat",
-			stl: "none",
-		}
-		cc_library {
-			name: "libcompiler_rt",
-			stl: "none",
-		}
-		`
-}
-
 func withLinuxBionic() string {
 	return `
 				cc_binary {
@@ -625,21 +629,15 @@
 	android.FixtureOverrideTextFile(linuxBionicDefaultsPath, withLinuxBionic()),
 )
 
-// The preparer to include if running a cc related test for fuchsia.
-var PrepareForTestOnFuchsia = android.GroupFixturePreparers(
-	// Place the default cc test modules for fuschia in a location that will not conflict with default
-	// test modules defined by other packages.
-	android.FixtureAddTextFile("defaults/cc/fuschia/Android.bp", withFuchsiaModules()),
-	android.PrepareForTestSetDeviceToFuchsia,
-)
-
 // This adds some additional modules and singletons which might negatively impact the performance
 // of tests so they are not included in the PrepareForIntegrationTestWithCc.
 var PrepareForTestWithCcIncludeVndk = android.GroupFixturePreparers(
 	PrepareForIntegrationTestWithCc,
 	android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
-		VendorSnapshotImageSingleton.Init(ctx)
-		recoverySnapshotImageSingleton.init(ctx)
+		snapshot.VendorSnapshotImageSingleton.Init(ctx)
+		snapshot.RecoverySnapshotImageSingleton.Init(ctx)
+		RegisterVendorSnapshotModules(ctx)
+		RegisterRecoverySnapshotModules(ctx)
 		ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
 	}),
 )
@@ -663,14 +661,7 @@
 		mockFS[k] = v
 	}
 
-	var config android.Config
-	if os == android.Fuchsia {
-		panic("Fuchsia not supported use test fixture instead")
-	} else {
-		config = android.TestArchConfig(buildDir, env, bp, mockFS)
-	}
-
-	return config
+	return android.TestArchConfig(buildDir, env, bp, mockFS)
 }
 
 // CreateTestContext is the legacy way of creating a TestContext for testing cc modules.
@@ -687,8 +678,10 @@
 	ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
 	ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
 
-	VendorSnapshotImageSingleton.Init(ctx)
-	recoverySnapshotImageSingleton.init(ctx)
+	snapshot.VendorSnapshotImageSingleton.Init(ctx)
+	snapshot.RecoverySnapshotImageSingleton.Init(ctx)
+	RegisterVendorSnapshotModules(ctx)
+	RegisterRecoverySnapshotModules(ctx)
 	ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
 	RegisterVndkLibraryTxtTypes(ctx)
 
diff --git a/cc/util.go b/cc/util.go
index 1220d84..9bba876 100644
--- a/cc/util.go
+++ b/cc/util.go
@@ -21,6 +21,7 @@
 	"strings"
 
 	"android/soong/android"
+	"android/soong/snapshot"
 )
 
 // Efficiently converts a list of include directories to a single string
@@ -126,20 +127,6 @@
 		"ln -sf " + target + " " + filepath.Join(dir, linkName)
 }
 
-func copyFileRule(ctx android.SingletonContext, path android.Path, out string) android.OutputPath {
-	outPath := android.PathForOutput(ctx, out)
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        android.Cp,
-		Input:       path,
-		Output:      outPath,
-		Description: "copy " + path.String() + " -> " + out,
-		Args: map[string]string{
-			"cpFlags": "-f -L",
-		},
-	})
-	return outPath
-}
-
 func combineNoticesRule(ctx android.SingletonContext, paths android.Paths, out string) android.OutputPath {
 	outPath := android.PathForOutput(ctx, out)
 	ctx.Build(pctx, android.BuildParams{
@@ -151,12 +138,6 @@
 	return outPath
 }
 
-func writeStringToFileRule(ctx android.SingletonContext, content, out string) android.OutputPath {
-	outPath := android.PathForOutput(ctx, out)
-	android.WriteFileRule(ctx, outPath, content)
-	return outPath
-}
-
 // Dump a map to a list file as:
 //
 // {key1} {value1}
@@ -172,5 +153,5 @@
 		txtBuilder.WriteString(" ")
 		txtBuilder.WriteString(m[k])
 	}
-	return writeStringToFileRule(ctx, txtBuilder.String(), path)
+	return snapshot.WriteStringToFileRule(ctx, txtBuilder.String(), path)
 }
diff --git a/cc/vendor_snapshot.go b/cc/vendor_snapshot.go
index 003b7c9..ba4d79f 100644
--- a/cc/vendor_snapshot.go
+++ b/cc/vendor_snapshot.go
@@ -13,141 +13,46 @@
 // limitations under the License.
 package cc
 
-// This file contains singletons to capture vendor and recovery snapshot. They consist of prebuilt
-// modules under AOSP so older vendor and recovery can be built with a newer system in a single
-// source tree.
-
 import (
 	"encoding/json"
 	"path/filepath"
-	"sort"
 	"strings"
 
 	"android/soong/android"
+	"android/soong/snapshot"
 )
 
-var vendorSnapshotSingleton = snapshotSingleton{
-	"vendor",
-	"SOONG_VENDOR_SNAPSHOT_ZIP",
-	android.OptionalPath{},
-	true,
-	VendorSnapshotImageSingleton,
-	false, /* fake */
-}
+// This file defines how to capture cc modules into snapshot package.
 
-var vendorFakeSnapshotSingleton = snapshotSingleton{
-	"vendor",
-	"SOONG_VENDOR_FAKE_SNAPSHOT_ZIP",
-	android.OptionalPath{},
-	true,
-	VendorSnapshotImageSingleton,
-	true, /* fake */
-}
-
-var recoverySnapshotSingleton = snapshotSingleton{
-	"recovery",
-	"SOONG_RECOVERY_SNAPSHOT_ZIP",
-	android.OptionalPath{},
-	false,
-	recoverySnapshotImageSingleton,
-	false, /* fake */
-}
-
-func VendorSnapshotSingleton() android.Singleton {
-	return &vendorSnapshotSingleton
-}
-
-func VendorFakeSnapshotSingleton() android.Singleton {
-	return &vendorFakeSnapshotSingleton
-}
-
-func RecoverySnapshotSingleton() android.Singleton {
-	return &recoverySnapshotSingleton
-}
-
-type snapshotSingleton struct {
-	// Name, e.g., "vendor", "recovery", "ramdisk".
-	name string
-
-	// Make variable that points to the snapshot file, e.g.,
-	// "SOONG_RECOVERY_SNAPSHOT_ZIP".
-	makeVar string
-
-	// Path to the snapshot zip file.
-	snapshotZipFile android.OptionalPath
-
-	// Whether the image supports VNDK extension modules.
-	supportsVndkExt bool
-
-	// Implementation of the image interface specific to the image
-	// associated with this snapshot (e.g., specific to the vendor image,
-	// recovery image, etc.).
-	image SnapshotImage
-
-	// Whether this singleton is for fake snapshot or not.
-	// Fake snapshot is a snapshot whose prebuilt binaries and headers are empty.
-	// It is much faster to generate, and can be used to inspect dependencies.
-	fake bool
-}
-
-// Determine if a dir under source tree is an SoC-owned proprietary directory based
-// on vendor snapshot configuration
-// Examples: device/, vendor/
-func isVendorProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
-	return VendorSnapshotSingleton().(*snapshotSingleton).image.isProprietaryPath(dir, deviceConfig)
-}
-
-// Determine if a dir under source tree is an SoC-owned proprietary directory based
-// on recovery snapshot configuration
-// Examples: device/, vendor/
-func isRecoveryProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
-	return RecoverySnapshotSingleton().(*snapshotSingleton).image.isProprietaryPath(dir, deviceConfig)
-}
-
-func IsVendorProprietaryModule(ctx android.BaseModuleContext) bool {
-	// Any module in a vendor proprietary path is a vendor proprietary
-	// module.
-	if isVendorProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) {
+// Checks if the target image would contain VNDK
+func includeVndk(image snapshot.SnapshotImage) bool {
+	if image.ImageName() == snapshot.VendorSnapshotImageName {
 		return true
 	}
 
-	// However if the module is not in a vendor proprietary path, it may
-	// still be a vendor proprietary module. This happens for cc modules
-	// that are excluded from the vendor snapshot, and it means that the
-	// vendor has assumed control of the framework-provided module.
-	if c, ok := ctx.Module().(LinkableInterface); ok {
-		if c.ExcludeFromVendorSnapshot() {
-			return true
-		}
-	}
-
 	return false
 }
 
-func isRecoveryProprietaryModule(ctx android.BaseModuleContext) bool {
-
-	// Any module in a recovery proprietary path is a recovery proprietary
-	// module.
-	if isRecoveryProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) {
+// Check if the module is VNDK private
+func isPrivate(image snapshot.SnapshotImage, m LinkableInterface) bool {
+	if image.ImageName() == snapshot.VendorSnapshotImageName && m.IsVndkPrivate() {
 		return true
 	}
 
-	// However if the module is not in a recovery proprietary path, it may
-	// still be a recovery proprietary module. This happens for cc modules
-	// that are excluded from the recovery snapshot, and it means that the
-	// vendor has assumed control of the framework-provided module.
+	return false
+}
 
-	if c, ok := ctx.Module().(LinkableInterface); ok {
-		if c.ExcludeFromRecoverySnapshot() {
-			return true
-		}
+// Checks if target image supports VNDK Ext
+func supportsVndkExt(image snapshot.SnapshotImage) bool {
+	if image.ImageName() == snapshot.VendorSnapshotImageName {
+		return true
 	}
 
 	return false
 }
 
 // Determines if the module is a candidate for snapshot.
-func isSnapshotAware(cfg android.DeviceConfig, m LinkableInterface, inProprietaryPath bool, apexInfo android.ApexInfo, image SnapshotImage) bool {
+func isSnapshotAware(cfg android.DeviceConfig, m LinkableInterface, inProprietaryPath bool, apexInfo android.ApexInfo, image snapshot.SnapshotImage) bool {
 	if !m.Enabled() || m.HiddenFromMake() {
 		return false
 	}
@@ -158,12 +63,12 @@
 	}
 	// skip proprietary modules, but (for the vendor snapshot only)
 	// include all VNDK (static)
-	if inProprietaryPath && (!image.includeVndk() || !m.IsVndk()) {
+	if inProprietaryPath && (!includeVndk(image) || !m.IsVndk()) {
 		return false
 	}
 	// If the module would be included based on its path, check to see if
 	// the module is marked to be excluded. If so, skip it.
-	if image.excludeFromSnapshot(m) {
+	if image.ExcludeFromSnapshot(m) {
 		return false
 	}
 	if m.Target().Os.Class != android.Device {
@@ -173,7 +78,7 @@
 		return false
 	}
 	// the module must be installed in target image
-	if !apexInfo.IsForPlatform() || m.IsSnapshotPrebuilt() || !image.inImage(m)() {
+	if !apexInfo.IsForPlatform() || m.IsSnapshotPrebuilt() || !image.InImage(m)() {
 		return false
 	}
 	// skip kernel_headers which always depend on vendor
@@ -203,13 +108,13 @@
 			}
 		}
 		if sanitizable.Static() {
-			return sanitizable.OutputFile().Valid() && !image.private(m)
+			return sanitizable.OutputFile().Valid() && !isPrivate(image, m)
 		}
 		if sanitizable.Shared() || sanitizable.Rlib() {
 			if !sanitizable.OutputFile().Valid() {
 				return false
 			}
-			if image.includeVndk() {
+			if includeVndk(image) {
 				if !sanitizable.IsVndk() {
 					return true
 				}
@@ -256,15 +161,9 @@
 	VintfFragments []string `json:",omitempty"`
 }
 
-func (c *snapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
-	if !c.image.shouldGenerateSnapshot(ctx) {
-		return
-	}
-
-	var snapshotOutputs android.Paths
-
+var ccSnapshotAction snapshot.GenerateSnapshotAction = func(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
 	/*
-		Vendor snapshot zipped artifacts directory structure:
+		Vendor snapshot zipped artifacts directory structure for cc modules:
 		{SNAPSHOT_ARCH}/
 			arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
 				shared/
@@ -296,13 +195,7 @@
 				(header files of same directory structure with source tree)
 	*/
 
-	snapshotDir := c.name + "-snapshot"
-	if c.fake {
-		// If this is a fake snapshot singleton, place all files under fake/ subdirectory to avoid
-		// collision with real snapshot files
-		snapshotDir = filepath.Join("fake", snapshotDir)
-	}
-	snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
+	var snapshotOutputs android.Paths
 
 	includeDir := filepath.Join(snapshotArchDir, "include")
 	configsDir := filepath.Join(snapshotArchDir, "configs")
@@ -317,9 +210,9 @@
 		if fake {
 			// All prebuilt binaries and headers are installed by copyFile function. This makes a fake
 			// snapshot just touch prebuilts and headers, rather than installing real files.
-			return writeStringToFileRule(ctx, "", out)
+			return snapshot.WriteStringToFileRule(ctx, "", out)
 		} else {
-			return copyFileRule(ctx, path, out)
+			return snapshot.CopyFileRule(pctx, ctx, path, out)
 		}
 	}
 
@@ -337,7 +230,7 @@
 
 		// Common properties among snapshots.
 		prop.ModuleName = ctx.ModuleName(m)
-		if c.supportsVndkExt && m.IsVndkExt() {
+		if supportsVndkExt(s.Image) && m.IsVndkExt() {
 			// vndk exts are installed to /vendor/lib(64)?/vndk(-sp)?
 			if m.IsVndkSp() {
 				prop.RelativeInstallPath = "vndk-sp"
@@ -457,7 +350,7 @@
 			ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
 			return nil
 		}
-		ret = append(ret, writeStringToFileRule(ctx, string(j), propOut))
+		ret = append(ret, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
 
 		return ret
 	}
@@ -469,10 +362,10 @@
 		}
 
 		moduleDir := ctx.ModuleDir(module)
-		inProprietaryPath := c.image.isProprietaryPath(moduleDir, ctx.DeviceConfig())
+		inProprietaryPath := s.Image.IsProprietaryPath(moduleDir, ctx.DeviceConfig())
 		apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
 
-		if c.image.excludeFromSnapshot(m) {
+		if s.Image.ExcludeFromSnapshot(m) {
 			if inProprietaryPath {
 				// Error: exclude_from_vendor_snapshot applies
 				// to framework-path modules only.
@@ -481,7 +374,7 @@
 			}
 		}
 
-		if !isSnapshotAware(ctx.DeviceConfig(), m, inProprietaryPath, apexInfo, c.image) {
+		if !isSnapshotAware(ctx.DeviceConfig(), m, inProprietaryPath, apexInfo, s.Image) {
 			return
 		}
 
@@ -489,8 +382,8 @@
 		// list, we will still include the module as if it was a fake module.
 		// The reason is that soong needs all the dependencies to be present, even
 		// if they are not using during the build.
-		installAsFake := c.fake
-		if c.image.excludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
+		installAsFake := s.Fake
+		if s.Image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
 			installAsFake = true
 		}
 
@@ -514,47 +407,12 @@
 
 	// install all headers after removing duplicates
 	for _, header := range android.FirstUniquePaths(headers) {
-		snapshotOutputs = append(snapshotOutputs, copyFile(ctx, header, filepath.Join(includeDir, header.String()), c.fake))
+		snapshotOutputs = append(snapshotOutputs, copyFile(ctx, header, filepath.Join(includeDir, header.String()), s.Fake))
 	}
 
-	// All artifacts are ready. Sort them to normalize ninja and then zip.
-	sort.Slice(snapshotOutputs, func(i, j int) bool {
-		return snapshotOutputs[i].String() < snapshotOutputs[j].String()
-	})
-
-	zipPath := android.PathForOutput(
-		ctx,
-		snapshotDir,
-		c.name+"-"+ctx.Config().DeviceName()+".zip")
-	zipRule := android.NewRuleBuilder(pctx, ctx)
-
-	// filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
-	snapshotOutputList := android.PathForOutput(
-		ctx,
-		snapshotDir,
-		c.name+"-"+ctx.Config().DeviceName()+"_list")
-	rspFile := snapshotOutputList.ReplaceExtension(ctx, "rsp")
-	zipRule.Command().
-		Text("tr").
-		FlagWithArg("-d ", "\\'").
-		FlagWithRspFileInputList("< ", rspFile, snapshotOutputs).
-		FlagWithOutput("> ", snapshotOutputList)
-
-	zipRule.Temporary(snapshotOutputList)
-
-	zipRule.Command().
-		BuiltTool("soong_zip").
-		FlagWithOutput("-o ", zipPath).
-		FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
-		FlagWithInput("-l ", snapshotOutputList)
-
-	zipRule.Build(zipPath.String(), c.name+" snapshot "+zipPath.String())
-	zipRule.DeleteTemporaryFiles()
-	c.snapshotZipFile = android.OptionalPathForPath(zipPath)
+	return snapshotOutputs
 }
 
-func (c *snapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
-	ctx.Strict(
-		c.makeVar,
-		c.snapshotZipFile.String())
+func init() {
+	snapshot.RegisterSnapshotAction(ccSnapshotAction)
 }
diff --git a/cc/vndk.go b/cc/vndk.go
index 499d428..1ae79de 100644
--- a/cc/vndk.go
+++ b/cc/vndk.go
@@ -25,6 +25,7 @@
 	"android/soong/android"
 	"android/soong/cc/config"
 	"android/soong/etc"
+	"android/soong/snapshot"
 
 	"github.com/google/blueprint"
 )
@@ -698,7 +699,7 @@
 
 		libPath := m.outputFile.Path()
 		snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "shared", vndkType, libPath.Base())
-		ret = append(ret, copyFileRule(ctx, libPath, snapshotLibOut))
+		ret = append(ret, snapshot.CopyFileRule(pctx, ctx, libPath, snapshotLibOut))
 
 		if ctx.Config().VndkSnapshotBuildArtifacts() {
 			prop := struct {
@@ -720,7 +721,7 @@
 				ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
 				return nil, false
 			}
-			ret = append(ret, writeStringToFileRule(ctx, string(j), propOut))
+			ret = append(ret, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
 		}
 		return ret, true
 	}
@@ -778,8 +779,8 @@
 
 	// install all headers after removing duplicates
 	for _, header := range android.FirstUniquePaths(headers) {
-		snapshotOutputs = append(snapshotOutputs, copyFileRule(
-			ctx, header, filepath.Join(includeDir, header.String())))
+		snapshotOutputs = append(snapshotOutputs, snapshot.CopyFileRule(
+			pctx, ctx, header, filepath.Join(includeDir, header.String())))
 	}
 
 	// install *.libraries.txt except vndkcorevariant.libraries.txt
@@ -788,8 +789,8 @@
 		if !ok || !m.Enabled() || m.Name() == vndkUsingCoreVariantLibrariesTxt {
 			return
 		}
-		snapshotOutputs = append(snapshotOutputs, copyFileRule(
-			ctx, m.OutputFile(), filepath.Join(configsDir, m.Name())))
+		snapshotOutputs = append(snapshotOutputs, snapshot.CopyFileRule(
+			pctx, ctx, m.OutputFile(), filepath.Join(configsDir, m.Name())))
 	})
 
 	/*
diff --git a/cmd/go2bp/go2bp.go b/cmd/go2bp/go2bp.go
index 67138f1..07cb5df 100644
--- a/cmd/go2bp/go2bp.go
+++ b/cmd/go2bp/go2bp.go
@@ -88,11 +88,24 @@
 var excludeDeps = make(Exclude)
 var excludeSrcs = make(Exclude)
 
+type StringList []string
+
+func (l *StringList) String() string {
+	return strings.Join(*l, " ")
+}
+
+func (l *StringList) Set(v string) error {
+	*l = append(*l, strings.Fields(v)...)
+	return nil
+}
+
 type GoModule struct {
 	Dir string
 }
 
 type GoPackage struct {
+	ExportToAndroid bool
+
 	Dir         string
 	ImportPath  string
 	Name        string
@@ -117,7 +130,7 @@
 
 func (g GoPackage) BpName() string {
 	if g.IsCommand() {
-		return filepath.Base(g.ImportPath)
+		return rewriteNames.GoToBp(filepath.Base(g.ImportPath))
 	}
 	return rewriteNames.GoToBp(g.ImportPath)
 }
@@ -279,6 +292,10 @@
      Don't put the specified go package in the dependency lists.
   -exclude-srcs <module>
      Don't put the specified source files in srcs or testSrcs lists.
+  -limit <package>
+     If set, limit the output to the specified packages and their dependencies.
+  -skip-tests
+     If passed, don't write out any test srcs or dependencies to the Android.bp output.
   -regen <file>
      Read arguments from <file> and overwrite it.
 
@@ -286,11 +303,15 @@
 	}
 
 	var regen string
+	var skipTests bool
+	limit := StringList{}
 
 	flag.Var(&excludes, "exclude", "Exclude go package")
 	flag.Var(&excludeDeps, "exclude-dep", "Exclude go package from deps")
 	flag.Var(&excludeSrcs, "exclude-src", "Exclude go file from source lists")
 	flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
+	flag.Var(&limit, "limit", "If set, only includes the dependencies of the listed packages")
+	flag.BoolVar(&skipTests, "skip-tests", false, "Whether to skip test sources")
 	flag.StringVar(&regen, "regen", "", "Rewrite specified file")
 	flag.Parse()
 
@@ -321,7 +342,8 @@
 	}
 	decoder := json.NewDecoder(bytes.NewReader(output))
 
-	pkgs := []GoPackage{}
+	pkgs := []*GoPackage{}
+	pkgMap := map[string]*GoPackage{}
 	for decoder.More() {
 		pkg := GoPackage{}
 		err := decoder.Decode(&pkg)
@@ -329,7 +351,15 @@
 			fmt.Fprintf(os.Stderr, "Failed to parse json: %v\n", err)
 			os.Exit(1)
 		}
-		pkgs = append(pkgs, pkg)
+		if len(limit) == 0 {
+			pkg.ExportToAndroid = true
+		}
+		if skipTests {
+			pkg.TestGoFiles = nil
+			pkg.TestImports = nil
+		}
+		pkgs = append(pkgs, &pkg)
+		pkgMap[pkg.ImportPath] = &pkg
 	}
 
 	buf := &bytes.Buffer{}
@@ -337,8 +367,27 @@
 	fmt.Fprintln(buf, "// Automatically generated with:")
 	fmt.Fprintln(buf, "// go2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
 
+	var mark func(string)
+	mark = func(pkgName string) {
+		if excludes[pkgName] {
+			return
+		}
+		if pkg, ok := pkgMap[pkgName]; ok && !pkg.ExportToAndroid {
+			pkg.ExportToAndroid = true
+			for _, dep := range pkg.AllImports() {
+				if !excludeDeps[dep] {
+					mark(dep)
+				}
+			}
+		}
+	}
+
+	for _, pkgName := range limit {
+		mark(pkgName)
+	}
+
 	for _, pkg := range pkgs {
-		if excludes[pkg.ImportPath] {
+		if !pkg.ExportToAndroid || excludes[pkg.ImportPath] {
 			continue
 		}
 		if len(pkg.BpSrcs(pkg.GoFiles)) == 0 && len(pkg.BpSrcs(pkg.TestGoFiles)) == 0 {
diff --git a/etc/Android.bp b/etc/Android.bp
index cab7389..06a2fa1 100644
--- a/etc/Android.bp
+++ b/etc/Android.bp
@@ -9,6 +9,7 @@
         "blueprint",
         "soong",
         "soong-android",
+        "soong-snapshot",
     ],
     srcs: [
         "prebuilt_etc.go",
diff --git a/etc/prebuilt_etc.go b/etc/prebuilt_etc.go
index 4dd383d..4107916 100644
--- a/etc/prebuilt_etc.go
+++ b/etc/prebuilt_etc.go
@@ -28,12 +28,15 @@
 // various `prebuilt_*` mutators.
 
 import (
+	"encoding/json"
 	"fmt"
+	"path/filepath"
 	"strings"
 
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
+	"android/soong/snapshot"
 )
 
 var pctx = android.NewPackageContext("android/soong/etc")
@@ -43,6 +46,7 @@
 func init() {
 	pctx.Import("android/soong/android")
 	RegisterPrebuiltEtcBuildComponents(android.InitRegistrationContext)
+	snapshot.RegisterSnapshotAction(generatePrebuiltSnapshot)
 }
 
 func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
@@ -128,6 +132,9 @@
 	android.ModuleBase
 	android.DefaultableModuleBase
 
+	snapshot.VendorSnapshotModuleInterface
+	snapshot.RecoverySnapshotModuleInterface
+
 	properties       prebuiltEtcProperties
 	subdirProperties prebuiltSubdirProperties
 
@@ -183,7 +190,7 @@
 	return p.inDebugRamdisk()
 }
 
-func (p *PrebuiltEtc) inRecovery() bool {
+func (p *PrebuiltEtc) InRecovery() bool {
 	return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
 }
 
@@ -192,7 +199,7 @@
 }
 
 func (p *PrebuiltEtc) InstallInRecovery() bool {
-	return p.inRecovery()
+	return p.InRecovery()
 }
 
 var _ android.ImageInterface = (*PrebuiltEtc)(nil)
@@ -271,6 +278,18 @@
 	return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
 }
 
+func (p *PrebuiltEtc) InVendor() bool {
+	return p.ModuleBase.InstallInVendor()
+}
+
+func (p *PrebuiltEtc) ExcludeFromVendorSnapshot() bool {
+	return false
+}
+
+func (p *PrebuiltEtc) ExcludeFromRecoverySnapshot() bool {
+	return false
+}
+
 func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	if p.properties.Src == nil {
 		ctx.PropertyErrorf("src", "missing prebuilt source file")
@@ -344,7 +363,7 @@
 	if p.inDebugRamdisk() && !p.onlyInDebugRamdisk() {
 		nameSuffix = ".debug_ramdisk"
 	}
-	if p.inRecovery() && !p.onlyInRecovery() {
+	if p.InRecovery() && !p.onlyInRecovery() {
 		nameSuffix = ".recovery"
 	}
 	return []android.AndroidMkEntries{android.AndroidMkEntries{
@@ -494,3 +513,137 @@
 	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
 	return module
 }
+
+// Flags to be included in the snapshot
+type snapshotJsonFlags struct {
+	ModuleName          string `json:",omitempty"`
+	Filename            string `json:",omitempty"`
+	RelativeInstallPath string `json:",omitempty"`
+}
+
+// Copy file into the snapshot
+func copyFile(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
+	if fake {
+		// Create empty file instead for the fake snapshot
+		return snapshot.WriteStringToFileRule(ctx, "", out)
+	} else {
+		return snapshot.CopyFileRule(pctx, ctx, path, out)
+	}
+}
+
+// Check if the module is target of the snapshot
+func isSnapshotAware(ctx android.SingletonContext, m *PrebuiltEtc, image snapshot.SnapshotImage) bool {
+	if !m.Enabled() {
+		return false
+	}
+
+	// Skip if the module is not included in the image
+	if !image.InImage(m)() {
+		return false
+	}
+
+	// When android/prebuilt.go selects between source and prebuilt, it sets
+	// HideFromMake on the other one to avoid duplicate install rules in make.
+	if m.IsHideFromMake() {
+		return false
+	}
+
+	// There are some prebuilt_etc module with multiple definition of same name.
+	// Check if the target would be included from the build
+	if !m.ExportedToMake() {
+		return false
+	}
+
+	// Skip if the module is in the predefined path list to skip
+	if image.IsProprietaryPath(ctx.ModuleDir(m), ctx.DeviceConfig()) {
+		return false
+	}
+
+	// Skip if the module should be excluded
+	if image.ExcludeFromSnapshot(m) || image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
+		return false
+	}
+
+	// Skip from other exceptional cases
+	if m.Target().Os.Class != android.Device {
+		return false
+	}
+	if m.Target().NativeBridge == android.NativeBridgeEnabled {
+		return false
+	}
+
+	return true
+}
+
+func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
+	/*
+		Snapshot zipped artifacts directory structure for etc modules:
+		{SNAPSHOT_ARCH}/
+			arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
+				etc/
+					(prebuilt etc files)
+			arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
+				etc/
+					(prebuilt etc files)
+			NOTICE_FILES/
+				(notice files)
+	*/
+	var snapshotOutputs android.Paths
+	noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
+	installedNotices := make(map[string]bool)
+
+	ctx.VisitAllModules(func(module android.Module) {
+		m, ok := module.(*PrebuiltEtc)
+		if !ok {
+			return
+		}
+
+		if !isSnapshotAware(ctx, m, s.Image) {
+			return
+		}
+
+		targetArch := "arch-" + m.Target().Arch.ArchType.String()
+
+		snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "etc", m.BaseModuleName())
+		snapshotOutputs = append(snapshotOutputs, copyFile(ctx, m.OutputFile(), snapshotLibOut, s.Fake))
+
+		prop := snapshotJsonFlags{}
+		propOut := snapshotLibOut + ".json"
+		prop.ModuleName = m.BaseModuleName()
+		if m.subdirProperties.Relative_install_path != nil {
+			prop.RelativeInstallPath = *m.subdirProperties.Relative_install_path
+		}
+
+		if m.properties.Filename != nil {
+			prop.Filename = *m.properties.Filename
+		}
+
+		j, err := json.Marshal(prop)
+		if err != nil {
+			ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
+			return
+		}
+		snapshotOutputs = append(snapshotOutputs, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
+
+		if len(m.EffectiveLicenseFiles()) > 0 {
+			noticeName := ctx.ModuleName(m) + ".txt"
+			noticeOut := filepath.Join(noticeDir, noticeName)
+			// skip already copied notice file
+			if !installedNotices[noticeOut] {
+				installedNotices[noticeOut] = true
+
+				noticeOutPath := android.PathForOutput(ctx, noticeOut)
+				ctx.Build(pctx, android.BuildParams{
+					Rule:        android.Cat,
+					Inputs:      m.EffectiveLicenseFiles(),
+					Output:      noticeOutPath,
+					Description: "combine notices for " + noticeOut,
+				})
+				snapshotOutputs = append(snapshotOutputs, noticeOutPath)
+			}
+		}
+
+	})
+
+	return snapshotOutputs
+}
diff --git a/etc/prebuilt_etc_test.go b/etc/prebuilt_etc_test.go
index 354f6bb..cf1f6d7 100644
--- a/etc/prebuilt_etc_test.go
+++ b/etc/prebuilt_etc_test.go
@@ -15,11 +15,15 @@
 package etc
 
 import (
+	"fmt"
 	"os"
 	"path/filepath"
 	"testing"
 
+	"github.com/google/blueprint/proptools"
+
 	"android/soong/android"
+	"android/soong/snapshot"
 )
 
 func TestMain(m *testing.M) {
@@ -36,6 +40,18 @@
 	}),
 )
 
+var prepareForPrebuiltEtcSnapshotTest = android.GroupFixturePreparers(
+	prepareForPrebuiltEtcTest,
+	android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
+		snapshot.VendorSnapshotImageSingleton.Init(ctx)
+		snapshot.RecoverySnapshotImageSingleton.Init(ctx)
+	}),
+	android.FixtureModifyConfig(func(config android.Config) {
+		config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
+		config.TestProductVariables.RecoverySnapshotVersion = proptools.StringPtr("current")
+	}),
+)
+
 func TestPrebuiltEtcVariants(t *testing.T) {
 	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
 		prebuilt_etc {
@@ -172,7 +188,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := result.Config.BuildOS.String()
 	p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
 	if !p.Host() {
 		t.Errorf("host bit is not set for a prebuilt_etc_host module.")
@@ -226,7 +242,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := result.Config.BuildOS.String()
 	p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
 	expected := filepath.Join("out/soong/host", result.Config.PrebuiltOS(), "usr", "share", "bar")
 	android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
@@ -346,3 +362,110 @@
 		})
 	}
 }
+
+func checkIfSnapshotTaken(t *testing.T, result *android.TestResult, image string, moduleName string) {
+	checkIfSnapshotExistAsExpected(t, result, image, moduleName, true)
+}
+
+func checkIfSnapshotNotTaken(t *testing.T, result *android.TestResult, image string, moduleName string) {
+	checkIfSnapshotExistAsExpected(t, result, image, moduleName, false)
+}
+
+func checkIfSnapshotExistAsExpected(t *testing.T, result *android.TestResult, image string, moduleName string, expectToExist bool) {
+	snapshotSingleton := result.SingletonForTests(image + "-snapshot")
+	archType := "arm64"
+	archVariant := "armv8-a"
+	archDir := fmt.Sprintf("arch-%s", archType)
+
+	snapshotDir := fmt.Sprintf("%s-snapshot", image)
+	snapshotVariantPath := filepath.Join(snapshotDir, archType)
+	outputDir := filepath.Join(snapshotVariantPath, archDir, "etc")
+	imageVariant := ""
+	if image == "recovery" {
+		imageVariant = "recovery_"
+	}
+	mod := result.ModuleForTests(moduleName, fmt.Sprintf("android_%s%s_%s", imageVariant, archType, archVariant))
+	outputFiles := mod.OutputFiles(t, "")
+	if len(outputFiles) != 1 {
+		t.Errorf("%q must have single output\n", moduleName)
+		return
+	}
+	snapshotPath := filepath.Join(outputDir, moduleName)
+
+	if expectToExist {
+		out := snapshotSingleton.Output(snapshotPath)
+
+		if out.Input.String() != outputFiles[0].String() {
+			t.Errorf("The input of snapshot %q must be %q, but %q", "prebuilt_vendor", out.Input.String(), outputFiles[0])
+		}
+
+		snapshotJsonPath := snapshotPath + ".json"
+
+		if snapshotSingleton.MaybeOutput(snapshotJsonPath).Rule == nil {
+			t.Errorf("%q expected but not found", snapshotJsonPath)
+		}
+	} else {
+		out := snapshotSingleton.MaybeOutput(snapshotPath)
+		if out.Rule != nil {
+			t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
+		}
+	}
+}
+
+func TestPrebuiltTakeSnapshot(t *testing.T) {
+	var testBp = `
+	prebuilt_etc {
+		name: "prebuilt_vendor",
+		src: "foo.conf",
+		vendor: true,
+	}
+
+	prebuilt_etc {
+		name: "prebuilt_vendor_indirect",
+		src: "foo.conf",
+		vendor: true,
+	}
+
+	prebuilt_etc {
+		name: "prebuilt_recovery",
+		src: "bar.conf",
+		recovery: true,
+	}
+
+	prebuilt_etc {
+		name: "prebuilt_recovery_indirect",
+		src: "bar.conf",
+		recovery: true,
+	}
+	`
+
+	t.Run("prebuilt: vendor and recovery snapshot", func(t *testing.T) {
+		result := prepareForPrebuiltEtcSnapshotTest.RunTestWithBp(t, testBp)
+
+		checkIfSnapshotTaken(t, result, "vendor", "prebuilt_vendor")
+		checkIfSnapshotTaken(t, result, "vendor", "prebuilt_vendor_indirect")
+		checkIfSnapshotTaken(t, result, "recovery", "prebuilt_recovery")
+		checkIfSnapshotTaken(t, result, "recovery", "prebuilt_recovery_indirect")
+	})
+
+	t.Run("prebuilt: directed snapshot", func(t *testing.T) {
+		prepareForPrebuiltEtcDirectedSnapshotTest := android.GroupFixturePreparers(
+			prepareForPrebuiltEtcSnapshotTest,
+			android.FixtureModifyConfig(func(config android.Config) {
+				config.TestProductVariables.DirectedVendorSnapshot = true
+				config.TestProductVariables.VendorSnapshotModules = make(map[string]bool)
+				config.TestProductVariables.VendorSnapshotModules["prebuilt_vendor"] = true
+				config.TestProductVariables.DirectedRecoverySnapshot = true
+				config.TestProductVariables.RecoverySnapshotModules = make(map[string]bool)
+				config.TestProductVariables.RecoverySnapshotModules["prebuilt_recovery"] = true
+			}),
+		)
+
+		result := prepareForPrebuiltEtcDirectedSnapshotTest.RunTestWithBp(t, testBp)
+
+		checkIfSnapshotTaken(t, result, "vendor", "prebuilt_vendor")
+		checkIfSnapshotNotTaken(t, result, "vendor", "prebuilt_vendor_indirect")
+		checkIfSnapshotTaken(t, result, "recovery", "prebuilt_recovery")
+		checkIfSnapshotNotTaken(t, result, "recovery", "prebuilt_recovery_indirect")
+	})
+}
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 8372a64..c26b20c 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -68,7 +68,6 @@
 		ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
 	})
 
-	android.DepsBp2BuildMutators(RegisterGenruleBp2BuildDeps)
 	android.RegisterBp2BuildMutator("genrule", GenruleBp2Build)
 }
 
diff --git a/java/bootclasspath_fragment.go b/java/bootclasspath_fragment.go
index bdf0dae..1ce9911 100644
--- a/java/bootclasspath_fragment.go
+++ b/java/bootclasspath_fragment.go
@@ -579,25 +579,9 @@
 	// Create hidden API input structure.
 	input := b.createHiddenAPIFlagInput(ctx, contents, fragments)
 
-	var output *HiddenAPIOutput
-
-	// Hidden API processing is conditional as a temporary workaround as not all
-	// bootclasspath_fragments provide the appropriate information needed for hidden API processing
-	// which leads to breakages of the build.
-	// TODO(b/179354495): Stop hidden API processing being conditional once all bootclasspath_fragment
-	//  modules have been updated to support it.
-	if input.canPerformHiddenAPIProcessing(ctx, b.properties) {
-		// Delegate the production of the hidden API all-flags.csv file to a module type specific method.
-		common := ctx.Module().(commonBootclasspathFragment)
-		output = common.produceHiddenAPIOutput(ctx, contents, input)
-	} else {
-		// As hidden API processing cannot be performed fall back to trying to retrieve the legacy
-		// encoded boot dex files, i.e. those files encoded by the individual libraries and returned
-		// from the DexJarBuildPath() method.
-		output = &HiddenAPIOutput{
-			EncodedBootDexFilesByModule: retrieveLegacyEncodedBootDexFiles(ctx, contents),
-		}
-	}
+	// Delegate the production of the hidden API all-flags.csv file to a module type specific method.
+	common := ctx.Module().(commonBootclasspathFragment)
+	output := common.produceHiddenAPIOutput(ctx, contents, input)
 
 	// Initialize a HiddenAPIInfo structure.
 	hiddenAPIInfo := HiddenAPIInfo{
@@ -615,7 +599,7 @@
 
 	// The monolithic hidden API processing also needs access to all the output files produced by
 	// hidden API processing of this fragment.
-	hiddenAPIInfo.HiddenAPIFlagOutput = (*output).HiddenAPIFlagOutput
+	hiddenAPIInfo.HiddenAPIFlagOutput = output.HiddenAPIFlagOutput
 
 	//  Provide it for use by other modules.
 	ctx.SetProvider(HiddenAPIInfoProvider, hiddenAPIInfo)
@@ -912,10 +896,10 @@
 
 // produceHiddenAPIOutput returns a path to the prebuilt all-flags.csv or nil if none is specified.
 func (module *prebuiltBootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
-	pathForOptionalSrc := func(src *string) android.Path {
+	pathForSrc := func(property string, src *string) android.Path {
 		if src == nil {
-			// TODO(b/179354495): Fail if this is not provided once prebuilts have been updated.
-			return nil
+			ctx.PropertyErrorf(property, "is required but was not specified")
+			return android.PathForModuleSrc(ctx, "missing", property)
 		}
 		return android.PathForModuleSrc(ctx, *src)
 	}
@@ -926,11 +910,11 @@
 
 	output := HiddenAPIOutput{
 		HiddenAPIFlagOutput: HiddenAPIFlagOutput{
-			StubFlagsPath:       pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Stub_flags),
-			AnnotationFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Annotation_flags),
-			MetadataPath:        pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Metadata),
-			IndexPath:           pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Index),
-			AllFlagsPath:        pathForOptionalSrc(module.prebuiltProperties.Hidden_api.All_flags),
+			AnnotationFlagsPath: pathForSrc("hidden_api.annotation_flags", module.prebuiltProperties.Hidden_api.Annotation_flags),
+			MetadataPath:        pathForSrc("hidden_api.metadata", module.prebuiltProperties.Hidden_api.Metadata),
+			IndexPath:           pathForSrc("hidden_api.index", module.prebuiltProperties.Hidden_api.Index),
+			StubFlagsPath:       pathForSrc("hidden_api.stub_flags", module.prebuiltProperties.Hidden_api.Stub_flags),
+			AllFlagsPath:        pathForSrc("hidden_api.all_flags", module.prebuiltProperties.Hidden_api.All_flags),
 		},
 		EncodedBootDexFilesByModule: encodedBootDexJarsByModule,
 	}
diff --git a/java/dexpreopt_bootjars.go b/java/dexpreopt_bootjars.go
index dff9543..2c78d73 100644
--- a/java/dexpreopt_bootjars.go
+++ b/java/dexpreopt_bootjars.go
@@ -523,14 +523,14 @@
 }
 
 // buildBootImageVariantsForBuildOs generates rules to build the boot image variants for the
-// android.BuildOs OsType, i.e. the type of OS on which the build is being running.
+// config.BuildOS OsType, i.e. the type of OS on which the build is being running.
 //
 // The files need to be generated into their predefined location because they are used from there
 // both within Soong and outside, e.g. for ART based host side testing and also for use by some
 // cloud based tools. However, they are not needed by callers of this function and so the paths do
 // not need to be returned from this func, unlike the buildBootImageVariantsForAndroidOs func.
 func buildBootImageVariantsForBuildOs(ctx android.ModuleContext, image *bootImageConfig, profile android.WritablePath) {
-	buildBootImageForOsType(ctx, image, profile, android.BuildOs)
+	buildBootImageForOsType(ctx, image, profile, ctx.Config().BuildOS)
 }
 
 // buildBootImageForOsType takes a bootImageConfig, a profile file and an android.OsType
diff --git a/java/dexpreopt_config.go b/java/dexpreopt_config.go
index b13955f..1507aaf 100644
--- a/java/dexpreopt_config.go
+++ b/java/dexpreopt_config.go
@@ -32,7 +32,7 @@
 		}
 	}
 	// We may also need the images on host in order to run host-based tests.
-	for _, target := range ctx.Config().Targets[android.BuildOs] {
+	for _, target := range ctx.Config().Targets[ctx.Config().BuildOS] {
 		targets = append(targets, target)
 	}
 
diff --git a/java/dexpreopt_test.go b/java/dexpreopt_test.go
index b25dece..8dc7b79 100644
--- a/java/dexpreopt_test.go
+++ b/java/dexpreopt_test.go
@@ -16,6 +16,7 @@
 
 import (
 	"fmt"
+	"runtime"
 	"testing"
 
 	"android/soong/android"
@@ -173,9 +174,9 @@
 }
 
 func TestDex2oatToolDeps(t *testing.T) {
-	if android.BuildOs != android.Linux {
+	if runtime.GOOS != "linux" {
 		// The host binary paths checked below are build OS dependent.
-		t.Skipf("Unsupported build OS %s", android.BuildOs)
+		t.Skipf("Unsupported build OS %s", runtime.GOOS)
 	}
 
 	preparers := android.GroupFixturePreparers(
diff --git a/java/hiddenapi_modular.go b/java/hiddenapi_modular.go
index c4832d2..86ab825 100644
--- a/java/hiddenapi_modular.go
+++ b/java/hiddenapi_modular.go
@@ -20,7 +20,6 @@
 
 	"android/soong/android"
 	"github.com/google/blueprint"
-	"github.com/google/blueprint/proptools"
 )
 
 // Contains support for processing hiddenAPI in a modular fashion.
@@ -511,14 +510,6 @@
 	}
 }
 
-// dedup removes duplicates in the flag files, while maintaining the order in which they were
-// appended.
-func (s FlagFilesByCategory) dedup() {
-	for category, paths := range s {
-		s[category] = android.FirstUniquePaths(paths)
-	}
-}
-
 // HiddenAPIInfo contains information provided by the hidden API processing.
 //
 // That includes paths resolved from HiddenAPIFlagFileProperties and also generated by hidden API
@@ -712,42 +703,6 @@
 	return input
 }
 
-// canPerformHiddenAPIProcessing determines whether hidden API processing should be performed.
-//
-// A temporary workaround to avoid existing bootclasspath_fragments that do not provide the
-// appropriate information needed for hidden API processing breaking the build.
-// TODO(b/179354495): Remove this workaround.
-func (i *HiddenAPIFlagInput) canPerformHiddenAPIProcessing(ctx android.ModuleContext, properties bootclasspathFragmentProperties) bool {
-	// Performing hidden API processing without stubs is not supported and it is unlikely to ever be
-	// required as the whole point of adding something to the bootclasspath fragment is to add it to
-	// the bootclasspath in order to be used by something else in the system. Without any stubs it
-	// cannot do that.
-	if len(i.StubDexJarsByScope) == 0 {
-		return false
-	}
-
-	// Hidden API processing is always enabled in tests.
-	if ctx.Config().TestProductVariables != nil {
-		return true
-	}
-
-	// A module that has fragments should have access to the information it needs in order to perform
-	// hidden API processing.
-	if len(properties.Fragments) != 0 {
-		return true
-	}
-
-	// The art bootclasspath fragment does not depend on any other fragments but already supports
-	// hidden API processing.
-	imageName := proptools.String(properties.Image_name)
-	if imageName == "art" {
-		return true
-	}
-
-	// Disable it for everything else.
-	return false
-}
-
 // gatherStubLibInfo gathers information from the stub libs needed by hidden API processing from the
 // dependencies added in hiddenAPIAddStubLibDependencies.
 //
diff --git a/java/hiddenapi_monolithic.go b/java/hiddenapi_monolithic.go
index 52f0770..404b4c1 100644
--- a/java/hiddenapi_monolithic.go
+++ b/java/hiddenapi_monolithic.go
@@ -58,68 +58,33 @@
 	// Merge all the information from the classpathElements. The fragments form a DAG so it is possible that
 	// this will introduce duplicates so they will be resolved after processing all the classpathElements.
 	for _, element := range classpathElements {
-		var classesJars android.Paths
 		switch e := element.(type) {
 		case *ClasspathLibraryElement:
-			classesJars = retrieveClassesJarsFromModule(e.Module())
+			classesJars := retrieveClassesJarsFromModule(e.Module())
+			monolithicInfo.ClassesJars = append(monolithicInfo.ClassesJars, classesJars...)
 
 		case *ClasspathFragmentElement:
 			fragment := e.Module()
 			if ctx.OtherModuleHasProvider(fragment, HiddenAPIInfoProvider) {
 				info := ctx.OtherModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo)
 				monolithicInfo.append(&info)
-
-				// If the bootclasspath fragment actually perform hidden API processing itself then use the
-				// CSV files it provides and do not bother processing the classesJars files. This ensures
-				// consistent behavior between source and prebuilt as prebuilt modules do not provide
-				// classesJars.
-				if info.AllFlagsPath != nil {
-					continue
-				}
+			} else {
+				ctx.ModuleErrorf("%s does not provide hidden API information", fragment)
 			}
-
-			classesJars = extractClassesJarsFromModules(e.Contents)
 		}
-
-		monolithicInfo.ClassesJars = append(monolithicInfo.ClassesJars, classesJars...)
 	}
 
-	// Dedup paths.
-	monolithicInfo.dedup()
-
 	return monolithicInfo
 }
 
 // append appends all the files from the supplied info to the corresponding files in this struct.
 func (i *MonolithicHiddenAPIInfo) append(other *HiddenAPIInfo) {
 	i.FlagsFilesByCategory.append(other.FlagFilesByCategory)
-
-	// The output may not be set if the bootclasspath_fragment has not yet been updated to support
-	// hidden API processing.
-	// TODO(b/179354495): Switch back to append once all bootclasspath_fragment modules have been
-	//  updated to support hidden API processing properly.
-	appendIfNotNil := func(paths android.Paths, path android.Path) android.Paths {
-		if path == nil {
-			return paths
-		}
-		return append(paths, path)
-	}
-	i.StubFlagsPaths = appendIfNotNil(i.StubFlagsPaths, other.StubFlagsPath)
-	i.AnnotationFlagsPaths = appendIfNotNil(i.AnnotationFlagsPaths, other.AnnotationFlagsPath)
-	i.MetadataPaths = appendIfNotNil(i.MetadataPaths, other.MetadataPath)
-	i.IndexPaths = appendIfNotNil(i.IndexPaths, other.IndexPath)
-	i.AllFlagsPaths = appendIfNotNil(i.AllFlagsPaths, other.AllFlagsPath)
-}
-
-// dedup removes duplicates in all the paths, while maintaining the order in which they were
-// appended.
-func (i *MonolithicHiddenAPIInfo) dedup() {
-	i.FlagsFilesByCategory.dedup()
-	i.StubFlagsPaths = android.FirstUniquePaths(i.StubFlagsPaths)
-	i.AnnotationFlagsPaths = android.FirstUniquePaths(i.AnnotationFlagsPaths)
-	i.MetadataPaths = android.FirstUniquePaths(i.MetadataPaths)
-	i.IndexPaths = android.FirstUniquePaths(i.IndexPaths)
-	i.AllFlagsPaths = android.FirstUniquePaths(i.AllFlagsPaths)
+	i.StubFlagsPaths = append(i.StubFlagsPaths, other.StubFlagsPath)
+	i.AnnotationFlagsPaths = append(i.AnnotationFlagsPaths, other.AnnotationFlagsPath)
+	i.MetadataPaths = append(i.MetadataPaths, other.MetadataPath)
+	i.IndexPaths = append(i.IndexPaths, other.IndexPath)
+	i.AllFlagsPaths = append(i.AllFlagsPaths, other.AllFlagsPath)
 }
 
 var MonolithicHiddenAPIInfoProvider = blueprint.NewProvider(MonolithicHiddenAPIInfo{})
diff --git a/java/java.go b/java/java.go
index 83e9fe1..e38a714 100644
--- a/java/java.go
+++ b/java/java.go
@@ -130,11 +130,19 @@
 			PropertyName: "java_boot_libs",
 			SupportsSdk:  true,
 		},
-		// Temporarily export implementation classes jar for java_boot_libs as it is required for the
-		// hiddenapi processing.
-		// TODO(b/179354495): Revert once hiddenapi processing has been modularized.
-		exportImplementationClassesJar,
-		sdkSnapshotFilePathForJar,
+		func(ctx android.SdkMemberContext, j *Library) android.Path {
+			// Java boot libs are only provided in the SDK to provide access to their dex implementation
+			// jar for use by dexpreopting and boot jars package check. They do not need to provide an
+			// actual implementation jar but the java_import will need a file that exists so just copy an
+			// empty file. Any attempt to use that file as a jar will cause a build error.
+			return ctx.SnapshotBuilder().EmptyFile()
+		},
+		func(osPrefix, name string) string {
+			// Create a special name for the implementation jar to try and provide some useful information
+			// to a developer that attempts to compile against this.
+			// TODO(b/175714559): Provide a proper error message in Soong not ninja.
+			return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
+		},
 		onlyCopyJarToSnapshot,
 	}
 
diff --git a/java/java_test.go b/java/java_test.go
index 0f9965d..b6780c2 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -441,7 +441,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 
 	bar := ctx.ModuleForTests("bar", buildOS+"_common")
 	barJar := bar.Output("bar.jar").Output.String()
@@ -478,7 +478,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 
 	foo := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
 
@@ -523,7 +523,7 @@
 	}
 
 	// check that -g is not overridden for host modules
-	buildOS := android.BuildOs.String()
+	buildOS := result.Config.BuildOS.String()
 	hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
 	hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
 	if strings.Contains(hostJavaFlags, "-g:source,lines") {
@@ -1371,7 +1371,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 
 	test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
 	entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
@@ -1387,7 +1387,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 	module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
 	assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
 		module.properties.Installable)
diff --git a/java/kotlin_test.go b/java/kotlin_test.go
index fd2f3ca..db30696 100644
--- a/java/kotlin_test.go
+++ b/java/kotlin_test.go
@@ -15,10 +15,11 @@
 package java
 
 import (
-	"android/soong/android"
 	"strconv"
 	"strings"
 	"testing"
+
+	"android/soong/android"
 )
 
 func TestKotlin(t *testing.T) {
@@ -114,7 +115,7 @@
 	t.Run("", func(t *testing.T) {
 		ctx, _ := testJava(t, bp)
 
-		buildOS := android.BuildOs.String()
+		buildOS := ctx.Config().BuildOS.String()
 
 		kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt")
 		kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
@@ -182,7 +183,7 @@
 			android.FixtureMergeEnv(env),
 		).RunTestWithBp(t, bp)
 
-		buildOS := android.BuildOs.String()
+		buildOS := result.Config.BuildOS.String()
 
 		kapt := result.ModuleForTests("foo", "android_common").Rule("kapt")
 		javac := result.ModuleForTests("foo", "android_common").Description("javac")
diff --git a/java/plugin_test.go b/java/plugin_test.go
index c7913d3..dc29b1c 100644
--- a/java/plugin_test.go
+++ b/java/plugin_test.go
@@ -15,7 +15,6 @@
 package java
 
 import (
-	"android/soong/android"
 	"testing"
 )
 
@@ -58,7 +57,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 
 	javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
 	turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine")
@@ -98,7 +97,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 
 	javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
 	turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine")
diff --git a/java/robolectric.go b/java/robolectric.go
index a37a118..a0c9c7f 100644
--- a/java/robolectric.go
+++ b/java/robolectric.go
@@ -83,6 +83,9 @@
 
 	testConfig android.Path
 	data       android.Paths
+
+	forceOSType   android.OsType
+	forceArchType android.ArchType
 }
 
 func (r *robolectricTest) TestSuites() []string {
@@ -115,6 +118,9 @@
 }
 
 func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	r.forceOSType = ctx.Config().BuildOS
+	r.forceArchType = ctx.Config().BuildArch
+
 	r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
 		r.testProperties.Test_config_template, r.testProperties.Test_suites,
 		r.testProperties.Auto_gen_config)
@@ -345,7 +351,7 @@
 func (r *robolectricTest) InstallBypassMake() bool  { return true }
 func (r *robolectricTest) InstallInTestcases() bool { return true }
 func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
-	return &android.BuildOs, &android.BuildArch
+	return &r.forceOSType, &r.forceArchType
 }
 
 func robolectricRuntimesFactory() android.Module {
@@ -366,6 +372,9 @@
 	props robolectricRuntimesProperties
 
 	runtimes []android.InstallPath
+
+	forceOSType   android.OsType
+	forceArchType android.ArchType
 }
 
 func (r *robolectricRuntimes) TestSuites() []string {
@@ -385,6 +394,9 @@
 		return
 	}
 
+	r.forceOSType = ctx.Config().BuildOS
+	r.forceArchType = ctx.Config().BuildArch
+
 	files := android.PathsForModuleSrc(ctx, r.props.Jars)
 
 	androidAllDir := android.PathForModuleInstall(ctx, "android-all")
@@ -417,5 +429,5 @@
 func (r *robolectricRuntimes) InstallBypassMake() bool  { return true }
 func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
 func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
-	return &android.BuildOs, &android.BuildArch
+	return &r.forceOSType, &r.forceArchType
 }
diff --git a/java/sdk_library.go b/java/sdk_library.go
index d1b4a47..268e797 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -981,13 +981,15 @@
 }
 
 // to satisfy SdkLibraryComponentDependency
-func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() *string {
-	return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
-}
-
-// to satisfy SdkLibraryComponentDependency
 func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
-	// Currently implementation library name is the same as the SDK library name.
+	// For shared libraries, this is the same as the SDK library name. If a Java library or app
+	// depends on a component library (e.g. a stub library) it still needs to know the name of the
+	// run-time library and the corresponding module that provides the implementation. This name is
+	// passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
+	// in dexpreopt).
+	//
+	// For non-shared SDK (component or not) libraries this returns `nil`, as they are not
+	// <uses-library> and should not be added to the manifest or to CLC.
 	return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
 }
 
@@ -999,12 +1001,6 @@
 	// SdkLibraryName returns the name of the java_sdk_library/_import module.
 	SdkLibraryName() *string
 
-	// The optional name of the sdk library that should be implicitly added to the
-	// AndroidManifest of an app that contains code which references the sdk library.
-	//
-	// Returns the name of the optional implicit SDK library or nil, if there isn't one.
-	OptionalImplicitSdkLibrary() *string
-
 	// The name of the implementation library for the optional SDK library or nil, if there isn't one.
 	OptionalSdkLibraryImplementation() *string
 }
diff --git a/java/sdk_test.go b/java/sdk_test.go
index bb595a5..6d62130 100644
--- a/java/sdk_test.go
+++ b/java/sdk_test.go
@@ -255,9 +255,11 @@
 				` + testcase.properties + `
 			}`
 
-			variant := "android_common"
-			if testcase.host == android.Host {
-				variant = android.BuildOs.String() + "_common"
+			variant := func(result *android.TestResult) string {
+				if testcase.host == android.Host {
+					return result.Config.BuildOS.String() + "_common"
+				}
+				return "android_common"
 			}
 
 			convertModulesToPaths := func(cp []string) []string {
@@ -312,7 +314,7 @@
 			}
 
 			checkClasspath := func(t *testing.T, result *android.TestResult, isJava8 bool) {
-				foo := result.ModuleForTests("foo", variant)
+				foo := result.ModuleForTests("foo", variant(result))
 				javac := foo.Rule("javac")
 				var deps []string
 
@@ -376,7 +378,7 @@
 				checkClasspath(t, result, true /* isJava8 */)
 
 				if testcase.host != android.Host {
-					aidl := result.ModuleForTests("foo", variant).Rule("aidl")
+					aidl := result.ModuleForTests("foo", variant(result)).Rule("aidl")
 
 					android.AssertStringDoesContain(t, "aidl command", aidl.RuleParams.Command, testcase.aidl+" -I.")
 				}
@@ -389,7 +391,7 @@
 				checkClasspath(t, result, false /* isJava8 */)
 
 				if testcase.host != android.Host {
-					aidl := result.ModuleForTests("foo", variant).Rule("aidl")
+					aidl := result.ModuleForTests("foo", variant(result)).Rule("aidl")
 
 					android.AssertStringDoesContain(t, "aidl command", aidl.RuleParams.Command, testcase.aidl+" -I.")
 				}
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..aa01e3b
--- /dev/null
+++ b/mk2rbc/cmd/mk2rbc.go
@@ -0,0 +1,498 @@
+// 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)
+	}
+	if len(flag.Args()) == 0 {
+		flag.Usage()
+	}
+	// 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 = &notExpr{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..55a35e9
--- /dev/null
+++ b/mk2rbc/mk2rbc.go
@@ -0,0 +1,1344 @@
+// 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},
+	"filter":                              {baseName + ".filter", starlarkTypeList},
+	"filter-out":                          {baseName + ".filter_out", starlarkTypeList},
+	"info":                                {baseName + ".mkinfo", starlarkTypeVoid},
+	"is-board-platform":                   {"!is-board-platform", starlarkTypeBool},
+	"is-board-platform-in-list":           {"!is-board-platform-in-list", starlarkTypeBool},
+	"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},
+	"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},
+	"subst":    {baseName + ".subst", 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 {
+			if ogv, ok := lhs.(*otherGlobalVariable); ok {
+				ogv.typ = inferred_type
+			} else if pcv, ok := lhs.(*productConfigVariable); ok {
+				pcv.typ = inferred_type
+			} else {
+				panic(fmt.Errorf("cannot assign new type to a variable %s, its flavor is %T", lhs.name(), lhs))
+			}
+		}
+	}
+	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) {
+	tokens := strings.Fields(directive.Args.Strings[0])
+	ctx.errorf(directive, "define is not supported: %s", tokens[0])
+}
+
+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 = &notExpr{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 = &notExpr{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(nil, "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":
+		return ctx.parseSubstFunc(node, 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, args *mkparser.MakeString) starlarkExpr {
+	words := args.Split(",")
+	if len(words) != 3 {
+		return ctx.newBadExpr(node, "subst function should have 3 arguments")
+	}
+	if !words[0].Const() || !words[1].Const() {
+		return ctx.newBadExpr(node, "subst function's from and to arguments should be constant")
+	}
+	from := words[0].Strings[0]
+	to := words[1].Strings[0]
+	words[2].TrimLeftSpaces()
+	words[2].TrimRightSpaces()
+	obj := ctx.parseMakeString(node, words[2])
+	return &callExpr{
+		object:     obj,
+		name:       "replace",
+		args:       []starlarkExpr{&stringLiteralExpr{from}, &stringLiteralExpr{to}},
+		returnType: starlarkTypeString,
+	}
+}
+
+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..54263b8
--- /dev/null
+++ b/mk2rbc/mk2rbc_test.go
@@ -0,0 +1,857 @@
+// 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)))
+
+`,
+		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]
+`,
+	},
+	{
+		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..56db192
--- /dev/null
+++ b/mk2rbc/variable.go
@@ -0,0 +1,300 @@
+// 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
+	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) 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: starlarkTypeString}}
+		} 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/Android.bp b/rust/Android.bp
index 11069d1..221014e 100644
--- a/rust/Android.bp
+++ b/rust/Android.bp
@@ -11,6 +11,7 @@
         "soong-bloaty",
         "soong-cc",
         "soong-rust-config",
+        "soong-snapshot",
     ],
     srcs: [
         "androidmk.go",
diff --git a/rust/androidmk.go b/rust/androidmk.go
index ea45ebd..630805a 100644
--- a/rust/androidmk.go
+++ b/rust/androidmk.go
@@ -205,24 +205,24 @@
 	ctx.SubAndroidMk(entries, fuzz.binaryDecorator)
 
 	var fuzzFiles []string
-	for _, d := range fuzz.corpus {
+	for _, d := range fuzz.fuzzPackagedModule.Corpus {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.corpusIntermediateDir.String())+":corpus/"+d.Base())
+			filepath.Dir(fuzz.fuzzPackagedModule.CorpusIntermediateDir.String())+":corpus/"+d.Base())
 	}
 
-	for _, d := range fuzz.data {
+	for _, d := range fuzz.fuzzPackagedModule.Data {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.dataIntermediateDir.String())+":data/"+d.Rel())
+			filepath.Dir(fuzz.fuzzPackagedModule.DataIntermediateDir.String())+":data/"+d.Rel())
 	}
 
-	if fuzz.dictionary != nil {
+	if fuzz.fuzzPackagedModule.Dictionary != nil {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.dictionary.String())+":"+fuzz.dictionary.Base())
+			filepath.Dir(fuzz.fuzzPackagedModule.Dictionary.String())+":"+fuzz.fuzzPackagedModule.Dictionary.Base())
 	}
 
-	if fuzz.config != nil {
+	if fuzz.fuzzPackagedModule.Config != nil {
 		fuzzFiles = append(fuzzFiles,
-			filepath.Dir(fuzz.config.String())+":config.json")
+			filepath.Dir(fuzz.fuzzPackagedModule.Config.String())+":config.json")
 	}
 
 	entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext,
diff --git a/rust/binary_test.go b/rust/binary_test.go
index 86f50d3..968c0c1 100644
--- a/rust/binary_test.go
+++ b/rust/binary_test.go
@@ -114,6 +114,23 @@
 	}
 }
 
+// Test that the bootstrap property sets the appropriate linker
+func TestBootstrap(t *testing.T) {
+	ctx := testRust(t, `
+		rust_binary {
+			name: "foo",
+			srcs: ["foo.rs"],
+			bootstrap: true,
+		}`)
+
+	foo := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Rule("rustc")
+
+	flag := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker64"
+	if !strings.Contains(foo.Args["linkFlags"], flag) {
+		t.Errorf("missing link flag to use bootstrap linker, expecting %#v, linkFlags: %#v", flag, foo.Args["linkFlags"])
+	}
+}
+
 func TestStaticBinaryFlags(t *testing.T) {
 	ctx := testRust(t, `
 		rust_binary {
diff --git a/rust/builder.go b/rust/builder.go
index 6db508d..523428d 100644
--- a/rust/builder.go
+++ b/rust/builder.go
@@ -220,6 +220,15 @@
 	linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
 	linkFlags = append(linkFlags, flags.LinkFlags...)
 
+	// Check if this module needs to use the bootstrap linker
+	if ctx.RustModule().Bootstrap() && !ctx.RustModule().InRecovery() && !ctx.RustModule().InRamdisk() && !ctx.RustModule().InVendorRamdisk() {
+		dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
+		if ctx.toolchain().Is64Bit() {
+			dynamicLinker += "64"
+		}
+		linkFlags = append(linkFlags, dynamicLinker)
+	}
+
 	libFlags := makeLibFlags(deps)
 
 	// Collect dependencies
diff --git a/rust/compiler.go b/rust/compiler.go
index 0b28135..df77759 100644
--- a/rust/compiler.go
+++ b/rust/compiler.go
@@ -305,7 +305,7 @@
 	if !Bool(compiler.Properties.No_stdlibs) {
 		for _, stdlib := range config.Stdlibs {
 			// If we're building for the primary arch of the build host, use the compiler's stdlibs
-			if ctx.Target().Os == android.BuildOs {
+			if ctx.Target().Os == ctx.Config().BuildOS {
 				stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
 			}
 			deps.Stdlibs = append(deps.Stdlibs, stdlib)
diff --git a/rust/config/allowed_list.go b/rust/config/allowed_list.go
index 31ec3f1..f568f27 100644
--- a/rust/config/allowed_list.go
+++ b/rust/config/allowed_list.go
@@ -6,6 +6,7 @@
 	// for an example.
 	// TODO(b/160223496): enable rustfmt globally.
 	RustAllowedPaths = []string{
+		"bionic/libc",
 		"device/google/cuttlefish",
 		"external/adhd",
 		"external/crosvm",
diff --git a/rust/config/global.go b/rust/config/global.go
index 1b56237..39ed992 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.52.1"
 	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/fuzz.go b/rust/fuzz.go
index 7e1c55a..18b2513 100644
--- a/rust/fuzz.go
+++ b/rust/fuzz.go
@@ -32,13 +32,7 @@
 type fuzzDecorator struct {
 	*binaryDecorator
 
-	Properties            cc.FuzzProperties
-	dictionary            android.Path
-	corpus                android.Paths
-	corpusIntermediateDir android.Path
-	config                android.Path
-	data                  android.Paths
-	dataIntermediateDir   android.Path
+	fuzzPackagedModule cc.FuzzPackagedModule
 }
 
 var _ compiler = (*binaryDecorator)(nil)
@@ -88,7 +82,7 @@
 
 func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
 	return append(fuzzer.binaryDecorator.compilerProps(),
-		&fuzzer.Properties)
+		&fuzzer.fuzzPackagedModule.FuzzProperties)
 }
 
 func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
@@ -102,32 +96,19 @@
 // Responsible for generating GNU Make rules that package fuzz targets into
 // their architecture & target/host specific zip file.
 type rustFuzzPackager struct {
-	packages    android.Paths
-	fuzzTargets map[string]bool
+	cc.FuzzPackager
 }
 
 func rustFuzzPackagingFactory() android.Singleton {
 	return &rustFuzzPackager{}
 }
 
-type fileToZip struct {
-	SourceFilePath        android.Path
-	DestinationPathPrefix string
-}
-
-type archOs struct {
-	hostOrTarget string
-	arch         string
-	dir          string
-}
-
 func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
-
 	// Map between each architecture + host/device combination.
-	archDirs := make(map[archOs][]fileToZip)
+	archDirs := make(map[cc.ArchOs][]cc.FileToZip)
 
 	// List of individual fuzz targets.
-	s.fuzzTargets = make(map[string]bool)
+	s.FuzzTargets = make(map[string]bool)
 
 	ctx.VisitAllModules(func(module android.Module) {
 		// Discard non-fuzz targets.
@@ -136,23 +117,15 @@
 			return
 		}
 
+		if ok := cc.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall {
+			return
+		}
+
 		fuzzModule, ok := rustModule.compiler.(*fuzzDecorator)
 		if !ok {
 			return
 		}
 
-		// Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of
-		// fuzz targets we're going to package anyway.
-		if !rustModule.Enabled() || rustModule.Properties.PreventInstall ||
-			rustModule.InRamdisk() || rustModule.InVendorRamdisk() || rustModule.InRecovery() {
-			return
-		}
-
-		// Discard modules that are in an unavailable namespace.
-		if !rustModule.ExportedToMake() {
-			return
-		}
-
 		hostOrTargetString := "target"
 		if rustModule.Host() {
 			hostOrTargetString = "host"
@@ -160,126 +133,34 @@
 
 		archString := rustModule.Arch().ArchType.String()
 		archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
-		archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()}
+		archOs := cc.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
 
-		var files []fileToZip
+		var files []cc.FileToZip
 		builder := android.NewRuleBuilder(pctx, ctx)
 
-		// Package the corpora into a zipfile.
-		if fuzzModule.corpus != nil {
-			corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip")
-			command := builder.Command().BuiltTool("soong_zip").
-				Flag("-j").
-				FlagWithOutput("-o ", corpusZip)
-			rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
-			command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.corpus)
-			files = append(files, fileToZip{corpusZip, ""})
-		}
-
-		// Package the data into a zipfile.
-		if fuzzModule.data != nil {
-			dataZip := archDir.Join(ctx, module.Name()+"_data.zip")
-			command := builder.Command().BuiltTool("soong_zip").
-				FlagWithOutput("-o ", dataZip)
-			for _, f := range fuzzModule.data {
-				intermediateDir := strings.TrimSuffix(f.String(), f.Rel())
-				command.FlagWithArg("-C ", intermediateDir)
-				command.FlagWithInput("-f ", f)
-			}
-			files = append(files, fileToZip{dataZip, ""})
-		}
+		// Package the artifacts (data, corpus, config and dictionary into a zipfile.
+		files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
 
 		// The executable.
-		files = append(files, fileToZip{rustModule.unstrippedOutputFile.Path(), ""})
+		files = append(files, cc.FileToZip{rustModule.unstrippedOutputFile.Path(), ""})
 
-		// The dictionary.
-		if fuzzModule.dictionary != nil {
-			files = append(files, fileToZip{fuzzModule.dictionary, ""})
+		archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
+		if !ok {
+			return
 		}
 
-		// Additional fuzz config.
-		if fuzzModule.config != nil {
-			files = append(files, fileToZip{fuzzModule.config, ""})
-		}
-
-		fuzzZip := archDir.Join(ctx, module.Name()+".zip")
-
-		command := builder.Command().BuiltTool("soong_zip").
-			Flag("-j").
-			FlagWithOutput("-o ", fuzzZip)
-
-		for _, file := range files {
-			if file.DestinationPathPrefix != "" {
-				command.FlagWithArg("-P ", file.DestinationPathPrefix)
-			} else {
-				command.Flag("-P ''")
-			}
-			command.FlagWithInput("-f ", file.SourceFilePath)
-		}
-
-		builder.Build("create-"+fuzzZip.String(),
-			"Package "+module.Name()+" for "+archString+"-"+hostOrTargetString)
-
-		// Don't add modules to 'make haiku-rust' that are set to not be
-		// exported to the fuzzing infrastructure.
-		if config := fuzzModule.Properties.Fuzz_config; config != nil {
-			if rustModule.Host() && !BoolDefault(config.Fuzz_on_haiku_host, true) {
-				return
-			} else if !BoolDefault(config.Fuzz_on_haiku_device, true) {
-				return
-			}
-		}
-
-		s.fuzzTargets[module.Name()] = true
-		archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""})
 	})
-
-	var archOsList []archOs
-	for archOs := range archDirs {
-		archOsList = append(archOsList, archOs)
-	}
-	sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].dir < archOsList[j].dir })
-
-	for _, archOs := range archOsList {
-		filesToZip := archDirs[archOs]
-		arch := archOs.arch
-		hostOrTarget := archOs.hostOrTarget
-		builder := android.NewRuleBuilder(pctx, ctx)
-		outputFile := android.PathForOutput(ctx, "fuzz-rust-"+hostOrTarget+"-"+arch+".zip")
-		s.packages = append(s.packages, outputFile)
-
-		command := builder.Command().BuiltTool("soong_zip").
-			Flag("-j").
-			FlagWithOutput("-o ", outputFile).
-			Flag("-L 0") // No need to try and re-compress the zipfiles.
-
-		for _, fileToZip := range filesToZip {
-			if fileToZip.DestinationPathPrefix != "" {
-				command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix)
-			} else {
-				command.Flag("-P ''")
-			}
-			command.FlagWithInput("-f ", fileToZip.SourceFilePath)
-		}
-		builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget,
-			"Create fuzz target packages for "+arch+"-"+hostOrTarget)
-	}
-
+	s.CreateFuzzPackage(ctx, archDirs, cc.Rust)
 }
 
 func (s *rustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
-	packages := s.packages.Strings()
+	packages := s.Packages.Strings()
 	sort.Strings(packages)
 
 	ctx.Strict("SOONG_RUST_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
 
-	// Preallocate the slice of fuzz targets to minimise memory allocations.
-	fuzzTargets := make([]string, 0, len(s.fuzzTargets))
-	for target, _ := range s.fuzzTargets {
-		fuzzTargets = append(fuzzTargets, target)
-	}
-	sort.Strings(fuzzTargets)
-	ctx.Strict("ALL_RUST_FUZZ_TARGETS", strings.Join(fuzzTargets, " "))
+	// Preallocate the slice of fuzz targets to minimize memory allocations.
+	s.PreallocateSlice(ctx, "ALL_RUST_FUZZ_TARGETS")
 }
 
 func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
@@ -289,13 +170,13 @@
 		"fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
 	fuzz.binaryDecorator.baseCompiler.install(ctx)
 
-	if fuzz.Properties.Corpus != nil {
-		fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus)
+	if fuzz.fuzzPackagedModule.FuzzProperties.Corpus != nil {
+		fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
 	}
-	if fuzz.Properties.Data != nil {
-		fuzz.data = android.PathsForModuleSrc(ctx, fuzz.Properties.Data)
+	if fuzz.fuzzPackagedModule.FuzzProperties.Data != nil {
+		fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
 	}
-	if fuzz.Properties.Dictionary != nil {
-		fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary)
+	if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
+		fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
 	}
 }
diff --git a/rust/library.go b/rust/library.go
index 747a29d..8c10e29 100644
--- a/rust/library.go
+++ b/rust/library.go
@@ -21,6 +21,7 @@
 
 	"android/soong/android"
 	"android/soong/cc"
+	"android/soong/snapshot"
 )
 
 var (
@@ -431,6 +432,12 @@
 
 func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
 	flags.RustFlags = append(flags.RustFlags, "-C metadata="+ctx.ModuleName())
+	if library.dylib() {
+		// We need to add a dependency on std in order to link crates as dylibs.
+		// The hack to add this dependency is guarded by the following cfg so
+		// that we don't force a dependency when it isn't needed.
+		library.baseCompiler.Properties.Cfgs = append(library.baseCompiler.Properties.Cfgs, "android_dylib")
+	}
 	flags = library.baseCompiler.compilerFlags(ctx, flags)
 	if library.shared() || library.static() {
 		library.includeDirs = append(library.includeDirs, android.PathsForModuleSrc(ctx, library.Properties.Include_dirs)...)
@@ -639,7 +646,7 @@
 			variation := v.(*Module).ModuleBase.ImageVariation().Variation
 			if strings.HasPrefix(variation, cc.VendorVariationPrefix) &&
 				m.HasVendorVariant() &&
-				!cc.IsVendorProprietaryModule(mctx) &&
+				!snapshot.IsVendorProprietaryModule(mctx) &&
 				strings.TrimPrefix(variation, cc.VendorVariationPrefix) == mctx.DeviceConfig().VndkVersion() {
 
 				// cc.MutateImage runs before LibraryMutator, so vendor variations which are meant for rlibs only are
diff --git a/rust/library_test.go b/rust/library_test.go
index 54cd2a5..cb4ef7e 100644
--- a/rust/library_test.go
+++ b/rust/library_test.go
@@ -85,6 +85,22 @@
 	}
 }
 
+// Check that we are passing the android_dylib config flag
+func TestAndroidDylib(t *testing.T) {
+	ctx := testRust(t, `
+		rust_library_host_dylib {
+			name: "libfoo",
+			srcs: ["foo.rs"],
+			crate_name: "foo",
+		}`)
+
+	libfooDylib := ctx.ModuleForTests("libfoo", "linux_glibc_x86_64_dylib").Output("libfoo.dylib.so")
+
+	if !strings.Contains(libfooDylib.Args["rustcFlags"], "--cfg 'android_dylib'") {
+		t.Errorf("missing android_dylib cfg flag for libfoo dylib, rustcFlags: %#v", libfooDylib.Args["rustcFlags"])
+	}
+}
+
 func TestValidateLibraryStem(t *testing.T) {
 	testRustError(t, "crate_name must be defined.", `
 			rust_library_host {
diff --git a/rust/project_json_test.go b/rust/project_json_test.go
index 09d30db..bdd54c5 100644
--- a/rust/project_json_test.go
+++ b/rust/project_json_test.go
@@ -176,6 +176,8 @@
 }
 
 func TestProjectJsonBindGen(t *testing.T) {
+	buildOS := android.TestConfig(t.TempDir(), nil, "", nil).BuildOS
+
 	bp := `
 	rust_library {
 		name: "libd",
@@ -214,9 +216,9 @@
 		if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") {
 			t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule)
 		}
-		if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, android.BuildOs.String()) {
+		if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, buildOS.String()) {
 			t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v",
-				rootModule, android.BuildOs.String())
+				rootModule, buildOS.String())
 		}
 		// Check that libbindings1 does not depend on itself.
 		if strings.Contains(rootModule, "libbindings1") {
diff --git a/rust/rust.go b/rust/rust.go
index 38f1742..80be496 100644
--- a/rust/rust.go
+++ b/rust/rust.go
@@ -85,6 +85,10 @@
 	VendorRamdiskVariantNeeded bool     `blueprint:"mutated"`
 	ExtraVariants              []string `blueprint:"mutated"`
 
+	// Allows this module to use non-APEX version of libraries. Useful
+	// for building binaries that are started before APEXes are activated.
+	Bootstrap *bool
+
 	// Used by vendor snapshot to record dependencies from snapshot modules.
 	SnapshotSharedLibs []string `blueprint:"mutated"`
 	SnapshotStaticLibs []string `blueprint:"mutated"`
@@ -119,9 +123,7 @@
 }
 
 type Module struct {
-	android.ModuleBase
-	android.DefaultableModuleBase
-	android.ApexModuleBase
+	cc.FuzzModule
 
 	VendorProperties cc.VendorProperties
 
@@ -290,7 +292,7 @@
 }
 
 func (mod *Module) Bootstrap() bool {
-	return false
+	return Bool(mod.Properties.Bootstrap)
 }
 
 func (mod *Module) MustUseVendorVariant() bool {
@@ -859,6 +861,8 @@
 		if mod.installable(apexInfo) {
 			mod.compiler.install(ctx)
 		}
+
+		ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
 	}
 }
 
diff --git a/rust/snapshot_prebuilt.go b/rust/snapshot_prebuilt.go
index 2f54973..79eaab3 100644
--- a/rust/snapshot_prebuilt.go
+++ b/rust/snapshot_prebuilt.go
@@ -17,6 +17,7 @@
 import (
 	"android/soong/android"
 	"android/soong/cc"
+
 	"github.com/google/blueprint/proptools"
 )
 
diff --git a/scripts/hiddenapi/Android.bp b/scripts/hiddenapi/Android.bp
index 7472f52..c50dc24 100644
--- a/scripts/hiddenapi/Android.bp
+++ b/scripts/hiddenapi/Android.bp
@@ -48,6 +48,27 @@
     },
 }
 
+python_test_host {
+    name: "generate_hiddenapi_lists_test",
+    main: "generate_hiddenapi_lists_test.py",
+    srcs: [
+        "generate_hiddenapi_lists.py",
+        "generate_hiddenapi_lists_test.py",
+    ],
+    version: {
+        py2: {
+            enabled: false,
+        },
+        py3: {
+            enabled: true,
+            embedded_launcher: true,
+        },
+    },
+    test_options: {
+        unit_test: true,
+    },
+}
+
 python_binary_host {
     name: "verify_overlaps",
     main: "verify_overlaps.py",
diff --git a/scripts/hiddenapi/generate_hiddenapi_lists_test.py b/scripts/hiddenapi/generate_hiddenapi_lists_test.py
index ff3d708..b81424b 100755
--- a/scripts/hiddenapi/generate_hiddenapi_lists_test.py
+++ b/scripts/hiddenapi/generate_hiddenapi_lists_test.py
@@ -101,4 +101,4 @@
         self.assertEqual(extract_package(signature), expected_package)
 
 if __name__ == '__main__':
-    unittest.main()
+    unittest.main(verbosity=2)
diff --git a/scripts/manifest_check.py b/scripts/manifest_check.py
index 8168fbf..4ef4399 100755
--- a/scripts/manifest_check.py
+++ b/scripts/manifest_check.py
@@ -90,6 +90,15 @@
   else:
     manifest_required, manifest_optional, tags = extract_uses_libs_xml(manifest)
 
+  # Trim namespace component. Normally Soong does that automatically when it
+  # handles module names specified in Android.bp properties. However not all
+  # <uses-library> entries in the manifest correspond to real modules: some of
+  # the optional libraries may be missing at build time. Therefor this script
+  # accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the
+  # optional namespace part manually.
+  required = trim_namespace_parts(required)
+  optional = trim_namespace_parts(optional)
+
   if manifest_required == required and manifest_optional == optional:
     return None
 
@@ -118,6 +127,17 @@
   return errmsg
 
 
+MODULE_NAMESPACE = re.compile("^//[^:]+:")
+
+def trim_namespace_parts(modules):
+  """Trim the namespace part of each module, if present. Leave only the name."""
+
+  trimmed = []
+  for module in modules:
+    trimmed.append(MODULE_NAMESPACE.sub('', module))
+  return trimmed
+
+
 def extract_uses_libs_apk(badging):
   """Extract <uses-library> tags from the manifest of an APK."""
 
diff --git a/scripts/manifest_check_test.py b/scripts/manifest_check_test.py
index 7159bdd..e3e8ac4 100755
--- a/scripts/manifest_check_test.py
+++ b/scripts/manifest_check_test.py
@@ -183,6 +183,15 @@
                             optional_uses_libraries=['bar'])
     self.assertTrue(matches)
 
+  def test_mixed_with_namespace(self):
+    xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
+                                      uses_library_xml('bar', required_xml(False))]))
+    apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
+                                      uses_library_apk('bar', required_apk(False))]))
+    matches = self.run_test(xml, apk, uses_libraries=['//x/y/z:foo'],
+                            optional_uses_libraries=['//x/y/z:bar'])
+    self.assertTrue(matches)
+
 
 class ExtractTargetSdkVersionTest(unittest.TestCase):
   def run_test(self, xml, apk, version):
diff --git a/sdk/bootclasspath_fragment_sdk_test.go b/sdk/bootclasspath_fragment_sdk_test.go
index 1d2ab42..efd2b5b 100644
--- a/sdk/bootclasspath_fragment_sdk_test.go
+++ b/sdk/bootclasspath_fragment_sdk_test.go
@@ -133,6 +133,13 @@
     apex_available: ["com.android.art"],
     image_name: "art",
     contents: ["mybootlib"],
+    hidden_api: {
+        stub_flags: "hiddenapi/stub-flags.csv",
+        annotation_flags: "hiddenapi/annotation-flags.csv",
+        metadata: "hiddenapi/metadata.csv",
+        index: "hiddenapi/index.csv",
+        all_flags: "hiddenapi/all-flags.csv",
+    },
 }
 
 java_import {
@@ -140,7 +147,7 @@
     prefer: false,
     visibility: ["//visibility:public"],
     apex_available: ["com.android.art"],
-    jars: ["java/mybootlib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
 }
 `),
 		checkVersionedAndroidBpContents(`
@@ -153,6 +160,13 @@
     apex_available: ["com.android.art"],
     image_name: "art",
     contents: ["mysdk_mybootlib@current"],
+    hidden_api: {
+        stub_flags: "hiddenapi/stub-flags.csv",
+        annotation_flags: "hiddenapi/annotation-flags.csv",
+        metadata: "hiddenapi/metadata.csv",
+        index: "hiddenapi/index.csv",
+        all_flags: "hiddenapi/all-flags.csv",
+    },
 }
 
 java_import {
@@ -160,7 +174,7 @@
     sdk_member_name: "mybootlib",
     visibility: ["//visibility:public"],
     apex_available: ["com.android.art"],
-    jars: ["java/mybootlib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
 }
 
 sdk_snapshot {
@@ -171,8 +185,13 @@
 }
 `),
 		checkAllCopyRules(`
-.intermediates/mybootlib/android_common/javac/mybootlib.jar -> java/mybootlib.jar
-`),
+.intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/stub-flags.csv -> hiddenapi/stub-flags.csv
+.intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/annotation-flags.csv -> hiddenapi/annotation-flags.csv
+.intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/metadata.csv -> hiddenapi/metadata.csv
+.intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/index.csv -> hiddenapi/index.csv
+.intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/all-flags.csv -> hiddenapi/all-flags.csv
+.intermediates/mysdk/common_os/empty -> java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar
+		`),
 		snapshotTestPreparer(checkSnapshotWithoutSource, preparerForSnapshot),
 
 		// Check the behavior of the snapshot without the source.
@@ -326,7 +345,7 @@
     prefer: false,
     visibility: ["//visibility:public"],
     apex_available: ["myapex"],
-    jars: ["java/mybootlib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
     permitted_packages: ["mybootlib"],
 }
 
@@ -410,7 +429,7 @@
     sdk_member_name: "mybootlib",
     visibility: ["//visibility:public"],
     apex_available: ["myapex"],
-    jars: ["java/mybootlib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
     permitted_packages: ["mybootlib"],
 }
 
@@ -480,7 +499,7 @@
 .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/metadata.csv -> hiddenapi/metadata.csv
 .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/index.csv -> hiddenapi/index.csv
 .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/all-flags.csv -> hiddenapi/all-flags.csv
-.intermediates/mybootlib/android_common/javac/mybootlib.jar -> java/mybootlib.jar
+.intermediates/mysdk/common_os/empty -> java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar
 .intermediates/myothersdklibrary.stubs/android_common/javac/myothersdklibrary.stubs.jar -> sdk_library/public/myothersdklibrary-stubs.jar
 .intermediates/myothersdklibrary.stubs.source/android_common/metalava/myothersdklibrary.stubs.source_api.txt -> sdk_library/public/myothersdklibrary.txt
 .intermediates/myothersdklibrary.stubs.source/android_common/metalava/myothersdklibrary.stubs.source_removed.txt -> sdk_library/public/myothersdklibrary-removed.txt
@@ -832,7 +851,7 @@
     prefer: false,
     visibility: ["//visibility:public"],
     apex_available: ["myapex"],
-    jars: ["java/mybootlib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
     permitted_packages: ["mybootlib"],
 }
 
@@ -867,7 +886,7 @@
 .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/metadata.csv -> hiddenapi/metadata.csv
 .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/index.csv -> hiddenapi/index.csv
 .intermediates/mybootclasspathfragment/android_common/modular-hiddenapi/all-flags.csv -> hiddenapi/all-flags.csv
-.intermediates/mybootlib/android_common/javac/mybootlib.jar -> java/mybootlib.jar
+.intermediates/mysdk/common_os/empty -> java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar
 .intermediates/mysdklibrary.stubs/android_common/javac/mysdklibrary.stubs.jar -> sdk_library/public/mysdklibrary-stubs.jar
 .intermediates/mysdklibrary.stubs.source/android_common/metalava/mysdklibrary.stubs.source_api.txt -> sdk_library/public/mysdklibrary.txt
 .intermediates/mysdklibrary.stubs.source/android_common/metalava/mysdklibrary.stubs.source_removed.txt -> sdk_library/public/mysdklibrary-removed.txt
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/sdk/java_sdk_test.go b/sdk/java_sdk_test.go
index 813dcfd..9efb3a4 100644
--- a/sdk/java_sdk_test.go
+++ b/sdk/java_sdk_test.go
@@ -453,7 +453,7 @@
     prefer: false,
     visibility: ["//visibility:public"],
     apex_available: ["//apex_available:platform"],
-    jars: ["java/myjavalib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/myjavalib.jar"],
     permitted_packages: ["pkg.myjavalib"],
 }
 `),
@@ -465,7 +465,7 @@
     sdk_member_name: "myjavalib",
     visibility: ["//visibility:public"],
     apex_available: ["//apex_available:platform"],
-    jars: ["java/myjavalib.jar"],
+    jars: ["java_boot_libs/snapshot/jars/are/invalid/myjavalib.jar"],
     permitted_packages: ["pkg.myjavalib"],
 }
 
@@ -474,9 +474,10 @@
     visibility: ["//visibility:public"],
     java_boot_libs: ["myexports_myjavalib@current"],
 }
+
 `),
 		checkAllCopyRules(`
-.intermediates/myjavalib/android_common/withres/myjavalib.jar -> java/myjavalib.jar
+.intermediates/myexports/common_os/empty -> java_boot_libs/snapshot/jars/are/invalid/myjavalib.jar
 `),
 	)
 }
diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go
index 97fb248..85e3d87 100644
--- a/sdk/sdk_test.go
+++ b/sdk/sdk_test.go
@@ -15,19 +15,21 @@
 package sdk
 
 import (
-	"android/soong/android"
 	"log"
 	"os"
+	"runtime"
 	"testing"
 
+	"android/soong/android"
+
 	"github.com/google/blueprint/proptools"
 )
 
 // Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE.
 func TestMain(m *testing.M) {
-	if android.BuildOs != android.Linux {
+	if runtime.GOOS != "linux" {
 		// b/145598135 - Generating host snapshots for anything other than linux is not supported.
-		log.Printf("Skipping as sdk snapshot generation is only supported on %s not %s", android.Linux, android.BuildOs)
+		log.Printf("Skipping as sdk snapshot generation is only supported on linux not %s", runtime.GOOS)
 		os.Exit(0)
 	}
 
diff --git a/sdk/update.go b/sdk/update.go
index 6da3756..1cd8f13 100644
--- a/sdk/update.go
+++ b/sdk/update.go
@@ -22,6 +22,7 @@
 
 	"android/soong/apex"
 	"android/soong/cc"
+
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
 
@@ -1029,6 +1030,9 @@
 	filesToZip  android.Paths
 	zipsToMerge android.Paths
 
+	// The path to an empty file.
+	emptyFile android.WritablePath
+
 	prebuiltModules map[string]*bpModule
 	prebuiltOrder   []*bpModule
 
@@ -1079,6 +1083,19 @@
 	s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
 }
 
+func (s *snapshotBuilder) EmptyFile() android.Path {
+	if s.emptyFile == nil {
+		ctx := s.ctx
+		s.emptyFile = android.PathForModuleOut(ctx, "empty")
+		s.ctx.Build(pctx, android.BuildParams{
+			Rule:   android.Touch,
+			Output: s.emptyFile,
+		})
+	}
+
+	return s.emptyFile
+}
+
 func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
 	name := member.Name()
 	if s.prebuiltModules[name] != nil {
@@ -1758,7 +1775,7 @@
 	var osTypes []android.OsType
 	for _, osType := range android.OsTypeList() {
 		if s.DeviceSupported() {
-			if osType.Class == android.Device && osType != android.Fuchsia {
+			if osType.Class == android.Device {
 				osTypes = append(osTypes, osType)
 			}
 		}
diff --git a/sh/sh_binary_test.go b/sh/sh_binary_test.go
index 20317d8..865d5f3 100644
--- a/sh/sh_binary_test.go
+++ b/sh/sh_binary_test.go
@@ -115,7 +115,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := config.BuildOS.String()
 	arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
 	for _, arch := range arches {
 		variant := ctx.ModuleForTests("foo", arch)
@@ -155,7 +155,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := ctx.Config().BuildOS.String()
 	mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
 	if !mod.Host() {
 		t.Errorf("host bit is not set for a sh_test_host module.")
@@ -192,7 +192,7 @@
 		}
 	`)
 
-	buildOS := android.BuildOs.String()
+	buildOS := config.BuildOS.String()
 	variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
 
 	relocated := variant.Output("relocated/lib64/libbar.so")
diff --git a/snapshot/Android.bp b/snapshot/Android.bp
new file mode 100644
index 0000000..f17ac53
--- /dev/null
+++ b/snapshot/Android.bp
@@ -0,0 +1,22 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+bootstrap_go_package {
+    name: "soong-snapshot",
+    pkgPath: "android/soong/snapshot",
+    deps: [
+        "blueprint",
+        "blueprint-pathtools",
+        "soong",
+        "soong-android",
+    ],
+    srcs: [
+        "recovery_snapshot.go",
+        "snapshot.go",
+        "snapshot_base.go",
+        "util.go",
+        "vendor_snapshot.go",
+    ],
+    pluginFor: ["soong_build"],
+}
diff --git a/snapshot/recovery_snapshot.go b/snapshot/recovery_snapshot.go
new file mode 100644
index 0000000..9b3919c
--- /dev/null
+++ b/snapshot/recovery_snapshot.go
@@ -0,0 +1,130 @@
+// 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 snapshot
+
+import "android/soong/android"
+
+// Interface for modules which can be captured in the recovery snapshot.
+type RecoverySnapshotModuleInterface interface {
+	SnapshotModuleInterfaceBase
+	InRecovery() bool
+	ExcludeFromRecoverySnapshot() bool
+}
+
+var recoverySnapshotSingleton = SnapshotSingleton{
+	"recovery",                     // name
+	"SOONG_RECOVERY_SNAPSHOT_ZIP",  // makeVar
+	android.OptionalPath{},         // snapshotZipFile
+	RecoverySnapshotImageSingleton, // Image
+	false,                          // Fake
+}
+
+func RecoverySnapshotSingleton() android.Singleton {
+	return &recoverySnapshotSingleton
+}
+
+// Determine if a dir under source tree is an SoC-owned proprietary directory based
+// on recovery snapshot configuration
+// Examples: device/, vendor/
+func isRecoveryProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
+	return RecoverySnapshotSingleton().(*SnapshotSingleton).Image.IsProprietaryPath(dir, deviceConfig)
+}
+
+func IsRecoveryProprietaryModule(ctx android.BaseModuleContext) bool {
+
+	// Any module in a recovery proprietary path is a recovery proprietary
+	// module.
+	if isRecoveryProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) {
+		return true
+	}
+
+	// However if the module is not in a recovery proprietary path, it may
+	// still be a recovery proprietary module. This happens for cc modules
+	// that are excluded from the recovery snapshot, and it means that the
+	// vendor has assumed control of the framework-provided module.
+
+	if c, ok := ctx.Module().(RecoverySnapshotModuleInterface); ok {
+		if c.ExcludeFromRecoverySnapshot() {
+			return true
+		}
+	}
+
+	return false
+}
+
+var RecoverySnapshotImageName = "recovery"
+
+type RecoverySnapshotImage struct{}
+
+func (RecoverySnapshotImage) Init(ctx android.RegistrationContext) {
+	ctx.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton)
+}
+
+func (RecoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
+	// RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a
+	// snapshot.
+	return ctx.DeviceConfig().RecoverySnapshotVersion() == "current"
+}
+
+func (RecoverySnapshotImage) InImage(m SnapshotModuleInterfaceBase) func() bool {
+	r, ok := m.(RecoverySnapshotModuleInterface)
+
+	if !ok {
+		// This module does not support recovery snapshot
+		return func() bool { return false }
+	}
+	return r.InRecovery
+}
+
+func (RecoverySnapshotImage) IsProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
+	return isDirectoryExcluded(dir, deviceConfig.RecoverySnapshotDirsExcludedMap(), deviceConfig.RecoverySnapshotDirsIncludedMap())
+}
+
+func (RecoverySnapshotImage) ExcludeFromSnapshot(m SnapshotModuleInterfaceBase) bool {
+	r, ok := m.(RecoverySnapshotModuleInterface)
+
+	if !ok {
+		// This module does not support recovery snapshot
+		return true
+	}
+	return r.ExcludeFromRecoverySnapshot()
+}
+
+func (RecoverySnapshotImage) IsUsingSnapshot(cfg android.DeviceConfig) bool {
+	recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
+	return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
+}
+
+func (RecoverySnapshotImage) TargetSnapshotVersion(cfg android.DeviceConfig) string {
+	return cfg.RecoverySnapshotVersion()
+}
+
+func (RecoverySnapshotImage) ExcludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
+	// If we're using full snapshot, not directed snapshot, capture every module
+	if !cfg.DirectedRecoverySnapshot() {
+		return false
+	}
+	// Else, checks if name is in RECOVERY_SNAPSHOT_MODULES.
+	return !cfg.RecoverySnapshotModules()[name]
+}
+
+func (RecoverySnapshotImage) ImageName() string {
+	return RecoverySnapshotImageName
+}
+
+var RecoverySnapshotImageSingleton RecoverySnapshotImage
+
+func init() {
+	RecoverySnapshotImageSingleton.Init(android.InitRegistrationContext)
+}
diff --git a/snapshot/snapshot.go b/snapshot/snapshot.go
new file mode 100644
index 0000000..294f8b6
--- /dev/null
+++ b/snapshot/snapshot.go
@@ -0,0 +1,122 @@
+// 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 snapshot
+
+import (
+	"path/filepath"
+	"sort"
+
+	"android/soong/android"
+)
+
+// This file contains singletons to capture snapshots. This singleton will generate snapshot of each target
+// image, and capturing snapshot module will be delegated to each module which implements GenerateSnapshotAction
+// function and register with RegisterSnapshotAction.
+
+var pctx = android.NewPackageContext("android/soong/snapshot")
+
+type SnapshotSingleton struct {
+	// Name, e.g., "vendor", "recovery", "ramdisk".
+	name string
+
+	// Make variable that points to the snapshot file, e.g.,
+	// "SOONG_RECOVERY_SNAPSHOT_ZIP".
+	makeVar string
+
+	// Path to the snapshot zip file.
+	snapshotZipFile android.OptionalPath
+
+	// Implementation of the image interface specific to the image
+	// associated with this snapshot (e.g., specific to the vendor image,
+	// recovery image, etc.).
+	Image SnapshotImage
+
+	// Whether this singleton is for fake snapshot or not.
+	// Fake snapshot is a snapshot whose prebuilt binaries and headers are empty.
+	// It is much faster to generate, and can be used to inspect dependencies.
+	Fake bool
+}
+
+// Interface of function to capture snapshot from each module
+type GenerateSnapshotAction func(snapshot SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths
+
+var snapshotActionList []GenerateSnapshotAction
+
+// Register GenerateSnapshotAction function so it can be called while generating snapshot
+func RegisterSnapshotAction(x GenerateSnapshotAction) {
+	snapshotActionList = append(snapshotActionList, x)
+}
+
+func (c *SnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
+	if !c.Image.shouldGenerateSnapshot(ctx) {
+		return
+	}
+
+	var snapshotOutputs android.Paths
+
+	// Snapshot zipped artifacts will be captured under {SNAPSHOT_ARCH} directory
+
+	snapshotDir := c.name + "-snapshot"
+	if c.Fake {
+		// If this is a fake snapshot singleton, place all files under fake/ subdirectory to avoid
+		// collision with real snapshot files
+		snapshotDir = filepath.Join("fake", snapshotDir)
+	}
+	snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
+
+	for _, f := range snapshotActionList {
+		snapshotOutputs = append(snapshotOutputs, f(*c, ctx, snapshotArchDir)...)
+	}
+
+	// All artifacts are ready. Sort them to normalize ninja and then zip.
+	sort.Slice(snapshotOutputs, func(i, j int) bool {
+		return snapshotOutputs[i].String() < snapshotOutputs[j].String()
+	})
+
+	zipPath := android.PathForOutput(
+		ctx,
+		snapshotDir,
+		c.name+"-"+ctx.Config().DeviceName()+".zip")
+	zipRule := android.NewRuleBuilder(pctx, ctx)
+
+	// filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
+	snapshotOutputList := android.PathForOutput(
+		ctx,
+		snapshotDir,
+		c.name+"-"+ctx.Config().DeviceName()+"_list")
+	rspFile := snapshotOutputList.ReplaceExtension(ctx, "rsp")
+	zipRule.Command().
+		Text("tr").
+		FlagWithArg("-d ", "\\'").
+		FlagWithRspFileInputList("< ", rspFile, snapshotOutputs).
+		FlagWithOutput("> ", snapshotOutputList)
+
+	zipRule.Temporary(snapshotOutputList)
+
+	zipRule.Command().
+		BuiltTool("soong_zip").
+		FlagWithOutput("-o ", zipPath).
+		FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
+		FlagWithInput("-l ", snapshotOutputList)
+
+	zipRule.Build(zipPath.String(), c.name+" snapshot "+zipPath.String())
+	zipRule.DeleteTemporaryFiles()
+	c.snapshotZipFile = android.OptionalPathForPath(zipPath)
+}
+
+func (c *SnapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
+	ctx.Strict(
+		c.makeVar,
+		c.snapshotZipFile.String())
+}
diff --git a/snapshot/snapshot_base.go b/snapshot/snapshot_base.go
new file mode 100644
index 0000000..de93f3e
--- /dev/null
+++ b/snapshot/snapshot_base.go
@@ -0,0 +1,104 @@
+// 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 snapshot
+
+import (
+	"android/soong/android"
+	"path/filepath"
+)
+
+// Interface for modules which can be captured in the snapshot.
+type SnapshotModuleInterfaceBase interface{}
+
+// Defines the specifics of different images to which the snapshot process is applicable, e.g.,
+// vendor, recovery, ramdisk.
+type SnapshotImage interface {
+	// Returns true if a snapshot should be generated for this image.
+	shouldGenerateSnapshot(ctx android.SingletonContext) bool
+
+	// Function that returns true if the module is included in this image.
+	// Using a function return instead of a value to prevent early
+	// evalution of a function that may be not be defined.
+	InImage(m SnapshotModuleInterfaceBase) func() bool
+
+	// Returns true if a dir under source tree is an SoC-owned proprietary
+	// directory, such as device/, vendor/, etc.
+	//
+	// For a given snapshot (e.g., vendor, recovery, etc.) if
+	// isProprietaryPath(dir, deviceConfig) returns true, then the module in dir
+	// will be built from sources.
+	IsProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool
+
+	// Whether a given module has been explicitly excluded from the
+	// snapshot, e.g., using the exclude_from_vendor_snapshot or
+	// exclude_from_recovery_snapshot properties.
+	ExcludeFromSnapshot(m SnapshotModuleInterfaceBase) bool
+
+	// Returns true if the build is using a snapshot for this image.
+	IsUsingSnapshot(cfg android.DeviceConfig) bool
+
+	// Returns a version of which the snapshot should be used in this target.
+	// This will only be meaningful when isUsingSnapshot is true.
+	TargetSnapshotVersion(cfg android.DeviceConfig) string
+
+	// Whether to exclude a given module from the directed snapshot or not.
+	// If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
+	// and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
+	ExcludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
+
+	// Returns target image name
+	ImageName() string
+}
+
+type directoryMap map[string]bool
+
+var (
+	// Modules under following directories are ignored. They are OEM's and vendor's
+	// proprietary modules(device/, kernel/, vendor/, and hardware/).
+	defaultDirectoryExcludedMap = directoryMap{
+		"device":   true,
+		"hardware": true,
+		"kernel":   true,
+		"vendor":   true,
+	}
+
+	// Modules under following directories are included as they are in AOSP,
+	// although hardware/ and kernel/ are normally for vendor's own.
+	defaultDirectoryIncludedMap = directoryMap{
+		"kernel/configs":              true,
+		"kernel/prebuilts":            true,
+		"kernel/tests":                true,
+		"hardware/interfaces":         true,
+		"hardware/libhardware":        true,
+		"hardware/libhardware_legacy": true,
+		"hardware/ril":                true,
+	}
+)
+
+func isDirectoryExcluded(dir string, excludedMap directoryMap, includedMap directoryMap) bool {
+	if dir == "." || dir == "/" {
+		return false
+	}
+	if includedMap[dir] {
+		return false
+	} else if excludedMap[dir] {
+		return true
+	} else if defaultDirectoryIncludedMap[dir] {
+		return false
+	} else if defaultDirectoryExcludedMap[dir] {
+		return true
+	} else {
+		return isDirectoryExcluded(filepath.Dir(dir), excludedMap, includedMap)
+	}
+}
diff --git a/snapshot/util.go b/snapshot/util.go
new file mode 100644
index 0000000..2297dfc
--- /dev/null
+++ b/snapshot/util.go
@@ -0,0 +1,36 @@
+// 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 snapshot
+
+import "android/soong/android"
+
+func WriteStringToFileRule(ctx android.SingletonContext, content, out string) android.OutputPath {
+	outPath := android.PathForOutput(ctx, out)
+	android.WriteFileRule(ctx, outPath, content)
+	return outPath
+}
+
+func CopyFileRule(pctx android.PackageContext, ctx android.SingletonContext, path android.Path, out string) android.OutputPath {
+	outPath := android.PathForOutput(ctx, out)
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        android.Cp,
+		Input:       path,
+		Output:      outPath,
+		Description: "copy " + path.String() + " -> " + out,
+		Args: map[string]string{
+			"cpFlags": "-f -L",
+		},
+	})
+	return outPath
+}
diff --git a/snapshot/vendor_snapshot.go b/snapshot/vendor_snapshot.go
new file mode 100644
index 0000000..9bd26c2
--- /dev/null
+++ b/snapshot/vendor_snapshot.go
@@ -0,0 +1,147 @@
+// 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 snapshot
+
+import "android/soong/android"
+
+// Interface for modules which can be captured in the vendor snapshot.
+type VendorSnapshotModuleInterface interface {
+	SnapshotModuleInterfaceBase
+	InVendor() bool
+	ExcludeFromVendorSnapshot() bool
+}
+
+var vendorSnapshotSingleton = SnapshotSingleton{
+	"vendor",                     // name
+	"SOONG_VENDOR_SNAPSHOT_ZIP",  // makeVar
+	android.OptionalPath{},       // snapshotZipFile
+	VendorSnapshotImageSingleton, // Image
+	false,                        // Fake
+}
+
+var vendorFakeSnapshotSingleton = SnapshotSingleton{
+	"vendor",                         // name
+	"SOONG_VENDOR_FAKE_SNAPSHOT_ZIP", // makeVar
+	android.OptionalPath{},           // snapshotZipFile
+	VendorSnapshotImageSingleton,     // Image
+	true,                             // Fake
+}
+
+func VendorSnapshotSingleton() android.Singleton {
+	return &vendorSnapshotSingleton
+}
+
+func VendorFakeSnapshotSingleton() android.Singleton {
+	return &vendorFakeSnapshotSingleton
+}
+
+// Determine if a dir under source tree is an SoC-owned proprietary directory based
+// on vendor snapshot configuration
+// Examples: device/, vendor/
+func isVendorProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
+	return VendorSnapshotSingleton().(*SnapshotSingleton).Image.IsProprietaryPath(dir, deviceConfig)
+}
+
+func IsVendorProprietaryModule(ctx android.BaseModuleContext) bool {
+	// Any module in a vendor proprietary path is a vendor proprietary
+	// module.
+	if isVendorProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) {
+		return true
+	}
+
+	// However if the module is not in a vendor proprietary path, it may
+	// still be a vendor proprietary module. This happens for cc modules
+	// that are excluded from the vendor snapshot, and it means that the
+	// vendor has assumed control of the framework-provided module.
+	if c, ok := ctx.Module().(VendorSnapshotModuleInterface); ok {
+		if c.ExcludeFromVendorSnapshot() {
+			return true
+		}
+	}
+
+	return false
+}
+
+var VendorSnapshotImageName = "vendor"
+
+type VendorSnapshotImage struct{}
+
+func (VendorSnapshotImage) Init(ctx android.RegistrationContext) {
+	ctx.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
+	ctx.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton)
+}
+
+func (VendorSnapshotImage) RegisterAdditionalModule(ctx android.RegistrationContext, name string, factory android.ModuleFactory) {
+	ctx.RegisterModuleType(name, factory)
+}
+
+func (VendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool {
+	// BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot.
+	return ctx.DeviceConfig().VndkVersion() == "current"
+}
+
+func (VendorSnapshotImage) InImage(m SnapshotModuleInterfaceBase) func() bool {
+	v, ok := m.(VendorSnapshotModuleInterface)
+
+	if !ok {
+		// This module does not support Vendor snapshot
+		return func() bool { return false }
+	}
+
+	return v.InVendor
+}
+
+func (VendorSnapshotImage) IsProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
+	return isDirectoryExcluded(dir, deviceConfig.VendorSnapshotDirsExcludedMap(), deviceConfig.VendorSnapshotDirsIncludedMap())
+}
+
+func (VendorSnapshotImage) ExcludeFromSnapshot(m SnapshotModuleInterfaceBase) bool {
+	v, ok := m.(VendorSnapshotModuleInterface)
+
+	if !ok {
+		// This module does not support Vendor snapshot
+		return true
+	}
+
+	return v.ExcludeFromVendorSnapshot()
+}
+
+func (VendorSnapshotImage) IsUsingSnapshot(cfg android.DeviceConfig) bool {
+	vndkVersion := cfg.VndkVersion()
+	return vndkVersion != "current" && vndkVersion != ""
+}
+
+func (VendorSnapshotImage) TargetSnapshotVersion(cfg android.DeviceConfig) string {
+	return cfg.VndkVersion()
+}
+
+// returns true iff a given module SHOULD BE EXCLUDED, false if included
+func (VendorSnapshotImage) ExcludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool {
+	// If we're using full snapshot, not directed snapshot, capture every module
+	if !cfg.DirectedVendorSnapshot() {
+		return false
+	}
+	// Else, checks if name is in VENDOR_SNAPSHOT_MODULES.
+	return !cfg.VendorSnapshotModules()[name]
+}
+
+func (VendorSnapshotImage) ImageName() string {
+	return VendorSnapshotImageName
+}
+
+var VendorSnapshotImageSingleton VendorSnapshotImage
+
+func init() {
+	VendorSnapshotImageSingleton.Init(android.InitRegistrationContext)
+}