Merge "Stop sdk_library generated api_library to depend on full surface api_library" into main
diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go
index 8a22886..71c8d0e 100644
--- a/android/allowlists/allowlists.go
+++ b/android/allowlists/allowlists.go
@@ -1095,6 +1095,7 @@
 		"ion-unit-tests",
 		"jemalloc5_integrationtests",
 		"jemalloc5_unittests",
+		"jemalloc5_stresstests", // run by run_jemalloc_tests.sh and will be deleted after V
 		"ld_config_test_helper",
 		"ld_preload_test_helper",
 		"libBionicCtsGtestMain", // depends on unconverted modules: libgtest_isolated
diff --git a/android/bazel.go b/android/bazel.go
index df30ff2..e4fada0 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -21,6 +21,7 @@
 	"strings"
 
 	"android/soong/ui/metrics/bp2build_metrics_proto"
+
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/bootstrap"
 	"github.com/google/blueprint/proptools"
@@ -153,8 +154,8 @@
 	HasHandcraftedLabel() bool
 	HandcraftedLabel() string
 	GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
-	ShouldConvertWithBp2build(ctx BazelConversionContext) bool
-	shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool
+	ShouldConvertWithBp2build(ctx ShouldConvertWithBazelContext) bool
+	shouldConvertWithBp2build(shouldConvertModuleContext, shouldConvertParams) bool
 
 	// ConvertWithBp2build either converts the module to a Bazel build target or
 	// declares the module as unconvertible (for logging and metrics).
@@ -448,12 +449,32 @@
 	if !ok {
 		return false
 	}
-	return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
+
+	return b.HasHandcraftedLabel() || b.shouldConvertWithBp2build(ctx, shouldConvertParams{
+		module:     module,
+		moduleDir:  ctx.OtherModuleDir(module),
+		moduleName: ctx.OtherModuleName(module),
+		moduleType: ctx.OtherModuleType(module),
+	})
+}
+
+type ShouldConvertWithBazelContext interface {
+	ModuleErrorf(format string, args ...interface{})
+	Module() Module
+	Config() Config
+	ModuleType() string
+	ModuleName() string
+	ModuleDir() string
 }
 
 // ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build
-func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool {
-	return b.shouldConvertWithBp2build(ctx, ctx.Module())
+func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx ShouldConvertWithBazelContext) bool {
+	return b.shouldConvertWithBp2build(ctx, shouldConvertParams{
+		module:     ctx.Module(),
+		moduleDir:  ctx.ModuleDir(),
+		moduleName: ctx.ModuleName(),
+		moduleType: ctx.ModuleType(),
+	})
 }
 
 type bazelOtherModuleContext interface {
@@ -471,11 +492,24 @@
 		arch == Riscv64 // TODO(b/262192655) Riscv64 toolchains are not currently supported.
 }
 
-func (b *BazelModuleBase) shouldConvertWithBp2build(ctx bazelOtherModuleContext, module blueprint.Module) bool {
+type shouldConvertModuleContext interface {
+	ModuleErrorf(format string, args ...interface{})
+	Config() Config
+}
+
+type shouldConvertParams struct {
+	module     blueprint.Module
+	moduleType string
+	moduleDir  string
+	moduleName string
+}
+
+func (b *BazelModuleBase) shouldConvertWithBp2build(ctx shouldConvertModuleContext, p shouldConvertParams) bool {
 	if !b.bazelProps().Bazel_module.CanConvertToBazel {
 		return false
 	}
 
+	module := p.module
 	// In api_bp2build mode, all soong modules that can provide API contributions should be converted
 	// This is irrespective of its presence/absence in bp2build allowlists
 	if ctx.Config().BuildMode == ApiBp2build {
@@ -484,7 +518,7 @@
 	}
 
 	propValue := b.bazelProperties.Bazel_module.Bp2build_available
-	packagePath := moduleDirWithPossibleOverride(ctx, module)
+	packagePath := moduleDirWithPossibleOverride(ctx, module, p.moduleDir)
 
 	// Modules in unit tests which are enabled in the allowlist by type or name
 	// trigger this conditional because unit tests run under the "." package path
@@ -493,10 +527,10 @@
 		return true
 	}
 
-	moduleName := moduleNameWithPossibleOverride(ctx, module)
+	moduleName := moduleNameWithPossibleOverride(ctx, module, p.moduleName)
 	allowlist := ctx.Config().Bp2buildPackageConfig
 	moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName]
-	moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[ctx.OtherModuleType(module)]
+	moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[p.moduleType]
 	allowlistConvert := moduleNameAllowed || moduleTypeAllowed
 	if moduleNameAllowed && moduleTypeAllowed {
 		ctx.ModuleErrorf("A module cannot be in moduleAlwaysConvert and also be in moduleTypeAlwaysConvert")
@@ -588,8 +622,21 @@
 		ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
 		return
 	}
+	// There may be cases where the target is created by a macro rather than in a BUILD file, those
+	// should be captured as well.
+	if bModule.HasHandcraftedLabel() {
+		// Defer to the BUILD target. Generating an additional target would
+		// cause a BUILD file conflict.
+		ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, "")
+		return
+	}
 	// TODO: b/285631638 - Differentiate between denylisted modules and missing bp2build capabilities.
-	if !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
+	if !bModule.shouldConvertWithBp2build(ctx, shouldConvertParams{
+		module:     ctx.Module(),
+		moduleDir:  ctx.ModuleDir(),
+		moduleName: ctx.ModuleName(),
+		moduleType: ctx.ModuleType(),
+	}) {
 		ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
 		return
 	}
diff --git a/android/bazel_paths.go b/android/bazel_paths.go
index b08a4ca..7992564 100644
--- a/android/bazel_paths.go
+++ b/android/bazel_paths.go
@@ -88,6 +88,8 @@
 	EarlyModulePathContext
 	BazelConversionContext
 
+	ModuleName() string
+	ModuleType() string
 	ModuleErrorf(fmt string, args ...interface{})
 	PropertyErrorf(property, fmt string, args ...interface{})
 	GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
@@ -459,8 +461,8 @@
 }
 
 func bp2buildModuleLabel(ctx BazelConversionContext, module blueprint.Module) string {
-	moduleName := moduleNameWithPossibleOverride(ctx, module)
-	moduleDir := moduleDirWithPossibleOverride(ctx, module)
+	moduleName := moduleNameWithPossibleOverride(ctx, module, ctx.OtherModuleName(module))
+	moduleDir := moduleDirWithPossibleOverride(ctx, module, ctx.OtherModuleDir(module))
 	if moduleDir == Bp2BuildTopLevel {
 		moduleDir = ""
 	}
diff --git a/android/bazel_paths_test.go b/android/bazel_paths_test.go
index 60c0a14..75b77a3 100644
--- a/android/bazel_paths_test.go
+++ b/android/bazel_paths_test.go
@@ -19,6 +19,7 @@
 	"testing"
 
 	"android/soong/bazel"
+
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/pathtools"
 )
@@ -157,6 +158,14 @@
 	return ctx.moduleDir
 }
 
+func (ctx *TestBazelConversionPathContext) ModuleName() string {
+	panic("Unimplemented")
+}
+
+func (ctx *TestBazelConversionPathContext) ModuleType() string {
+	panic("Unimplemented")
+}
+
 func TestTransformSubpackagePath(t *testing.T) {
 	cfg := NullConfig("out", "out/soong")
 	cfg.fs = pathtools.MockFs(map[string][]byte{
diff --git a/android/bazel_test.go b/android/bazel_test.go
index 13fd408..194a6b3 100644
--- a/android/bazel_test.go
+++ b/android/bazel_test.go
@@ -373,7 +373,14 @@
 				allowlist: test.allowlist,
 			}
 
-			shouldConvert := test.module.shouldConvertWithBp2build(bcc, test.module.TestModuleInfo)
+			shouldConvert := test.module.shouldConvertWithBp2build(bcc,
+				shouldConvertParams{
+					module:     test.module.TestModuleInfo,
+					moduleDir:  test.module.TestModuleInfo.Dir,
+					moduleType: test.module.TestModuleInfo.Typ,
+					moduleName: test.module.TestModuleInfo.ModuleName,
+				},
+			)
 			if test.shouldConvert != shouldConvert {
 				t.Errorf("Module shouldConvert expected to be: %v, but was: %v", test.shouldConvert, shouldConvert)
 			}
diff --git a/android/override_module.go b/android/override_module.go
index a4b7431..9e0de6f 100644
--- a/android/override_module.go
+++ b/android/override_module.go
@@ -353,26 +353,26 @@
 // variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule
 // or if this variant is not overridden.
 func ModuleNameWithPossibleOverride(ctx BazelConversionContext) string {
-	return moduleNameWithPossibleOverride(ctx, ctx.Module())
+	return moduleNameWithPossibleOverride(ctx, ctx.Module(), ctx.OtherModuleName(ctx.Module()))
 }
 
-func moduleNameWithPossibleOverride(ctx bazelOtherModuleContext, module blueprint.Module) string {
+func moduleNameWithPossibleOverride(ctx shouldConvertModuleContext, module blueprint.Module, name string) string {
 	if overridable, ok := module.(OverridableModule); ok {
 		if o := overridable.GetOverriddenBy(); o != "" {
 			return o
 		}
 	}
-	return ctx.OtherModuleName(module)
+	return name
 }
 
 // moduleDirWithPossibleOverride returns the dir of the OverrideModule that overrides the current
 // variant of the given OverridableModule, or ctx.OtherModuleName() if the module is not an
 // OverridableModule or if the variant is not overridden.
-func moduleDirWithPossibleOverride(ctx bazelOtherModuleContext, module blueprint.Module) string {
+func moduleDirWithPossibleOverride(ctx shouldConvertModuleContext, module blueprint.Module, dir string) string {
 	if overridable, ok := module.(OverridableModule); ok {
 		if o := overridable.GetOverriddenByModuleDir(); o != "" {
 			return o
 		}
 	}
-	return ctx.OtherModuleDir(module)
+	return dir
 }
diff --git a/apex/apex_test.go b/apex/apex_test.go
index bd19cb5..8368db1 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -390,7 +390,7 @@
 			name: "foo.rust",
 			srcs: ["foo.rs"],
 			rlibs: ["libfoo.rlib.rust"],
-			dylibs: ["libfoo.dylib.rust"],
+			rustlibs: ["libfoo.dylib.rust"],
 			apex_available: ["myapex"],
 		}
 
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 6ca4bb4..cd1bc7f 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -686,6 +686,9 @@
 			//
 			// bp2build converters are used for the majority of modules.
 			if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
+				if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
+					panic(fmt.Errorf("module %q [%s] [%s] was both converted with bp2build and has a handcrafted label", bpCtx.ModuleName(m), moduleType, dir))
+				}
 				// Handle modules converted to handcrafted targets.
 				//
 				// Since these modules are associated with some handcrafted
diff --git a/cc/lto.go b/cc/lto.go
index df9ca0a..820c1f0 100644
--- a/cc/lto.go
+++ b/cc/lto.go
@@ -162,7 +162,7 @@
 }
 
 func GlobalThinLTO(ctx android.BaseModuleContext) bool {
-	return !ctx.Config().IsEnvFalse("GLOBAL_THINLTO")
+	return ctx.Config().IsEnvTrue("GLOBAL_THINLTO")
 }
 
 // Propagate lto requirements down from binaries
diff --git a/rust/bindgen.go b/rust/bindgen.go
index c2bf6af..407f275 100644
--- a/rust/bindgen.go
+++ b/rust/bindgen.go
@@ -294,7 +294,7 @@
 // rust_bindgen generates Rust FFI bindings to C libraries using bindgen given a wrapper header as the primary input.
 // Bindgen has a number of flags to control the generated source, and additional flags can be passed to clang to ensure
 // the header and generated source is appropriately handled. It is recommended to add it as a dependency in the
-// rlibs, dylibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
+// rlibs or rustlibs property. It may also be added in the srcs property for external crates, using the ":"
 // prefix.
 func RustBindgenFactory() android.Module {
 	module, _ := NewRustBindgen(android.HostAndDeviceSupported)
diff --git a/rust/compiler.go b/rust/compiler.go
index 06ae12f..84c1fce 100644
--- a/rust/compiler.go
+++ b/rust/compiler.go
@@ -91,10 +91,8 @@
 	// list of rust rlib crate dependencies
 	Rlibs []string `android:"arch_variant"`
 
-	// list of rust dylib crate dependencies
-	Dylibs []string `android:"arch_variant"`
-
-	// list of rust automatic crate dependencies
+	// list of rust automatic crate dependencies.
+	// Rustlibs linkage is rlib for host targets and dylib for device targets.
 	Rustlibs []string `android:"arch_variant"`
 
 	// list of rust proc_macro crate dependencies
@@ -359,7 +357,6 @@
 
 func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
 	deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
-	deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
 	deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
 	deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
 	deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
diff --git a/rust/protobuf.go b/rust/protobuf.go
index 0cf6e8c..a14ebea 100644
--- a/rust/protobuf.go
+++ b/rust/protobuf.go
@@ -243,7 +243,7 @@
 
 // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for
 // protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will
-// create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs
+// create library variants that can be used as a crate dependency by adding it to the rlibs and rustlibs
 // properties of other modules.
 func RustProtobufFactory() android.Module {
 	module, _ := NewRustProtobuf(android.HostAndDeviceSupported)
diff --git a/rust/rust_test.go b/rust/rust_test.go
index 3f4e296..704bfe7 100644
--- a/rust/rust_test.go
+++ b/rust/rust_test.go
@@ -232,11 +232,6 @@
 			srcs: ["foo.rs"],
 			crate_name: "shared",
 		}
-		rust_library_host_dylib {
-			name: "libdylib",
-			srcs: ["foo.rs"],
-			crate_name: "dylib",
-		}
 		rust_library_host_rlib {
 			name: "librlib",
 			srcs: ["foo.rs"],
@@ -252,7 +247,6 @@
 		}
 		rust_binary_host {
 			name: "fizz-buzz",
-			dylibs: ["libdylib"],
 			rlibs: ["librlib"],
 			proc_macros: ["libpm"],
 			static_libs: ["libstatic"],
@@ -265,10 +259,6 @@
 	rustLink := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Rule("rustLink")
 
 	// Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up.
-	if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
-		t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
-	}
-
 	if !android.InList("librlib.rlib-std", module.Properties.AndroidMkRlibs) {
 		t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
 	}
diff --git a/sysprop/sysprop_library.go b/sysprop/sysprop_library.go
index 0edbb7c..a2c0fb7 100644
--- a/sysprop/sysprop_library.go
+++ b/sysprop/sysprop_library.go
@@ -24,6 +24,7 @@
 	"sync"
 
 	"android/soong/bazel"
+
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
 
@@ -410,7 +411,7 @@
 	Apex_available     []string
 	Min_sdk_version    *string
 	Bazel_module       struct {
-		Bp2build_available *bool
+		Label *string
 	}
 }
 
@@ -428,6 +429,9 @@
 	SyspropPublicStub string
 	Apex_available    []string
 	Min_sdk_version   *string
+	Bazel_module      struct {
+		Bp2build_available *bool
+	}
 }
 
 func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
@@ -473,6 +477,14 @@
 			"Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
 	}
 
+	var label *string
+	if b, ok := ctx.Module().(android.Bazelable); ok && b.ShouldConvertWithBp2build(ctx) {
+		// TODO: b/295566168 - this will need to change once build files are checked in to account for
+		// checked in modules in mixed builds
+		label = proptools.StringPtr(
+			fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.CcImplementationModuleName()))
+	}
+
 	// Generate a C++ implementation library.
 	// cc_library can receive *.sysprop files as their srcs, generating sources itself.
 	ccProps := ccLibraryProperties{}
@@ -492,11 +504,7 @@
 	ccProps.Host_supported = m.properties.Host_supported
 	ccProps.Apex_available = m.ApexProperties.Apex_available
 	ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
-	// A Bazel macro handles this, so this module does not need to be handled
-	// in bp2build
-	// TODO(b/237810289) perhaps do something different here so that we aren't
-	//                   also disabling these modules in mixed builds
-	ccProps.Bazel_module.Bp2build_available = proptools.BoolPtr(false)
+	ccProps.Bazel_module.Label = label
 	ctx.CreateModule(cc.LibraryFactory, &ccProps)
 
 	scope := "internal"
@@ -541,6 +549,11 @@
 		SyspropPublicStub: publicStub,
 		Apex_available:    m.ApexProperties.Apex_available,
 		Min_sdk_version:   m.properties.Java.Min_sdk_version,
+		Bazel_module: struct {
+			Bp2build_available *bool
+		}{
+			Bp2build_available: proptools.BoolPtr(false),
+		},
 	})
 
 	if publicStub != "" {
@@ -558,6 +571,11 @@
 			Sdk_version: proptools.StringPtr("core_current"),
 			Libs:        []string{javaSyspropStub},
 			Stem:        proptools.StringPtr(m.BaseModuleName()),
+			Bazel_module: struct {
+				Bp2build_available *bool
+			}{
+				Bp2build_available: proptools.BoolPtr(false),
+			},
 		})
 	}
 
diff --git a/tests/sbom_test.sh b/tests/sbom_test.sh
index c41f28d..9801a8e 100755
--- a/tests/sbom_test.sh
+++ b/tests/sbom_test.sh
@@ -86,16 +86,17 @@
 
   declare -A diff_excludes
   diff_excludes[vendor]="\
-    -I /vendor/lib64/libkeystore2_crypto.so"
+    -I /vendor/lib64/libkeystore2_crypto.so \
+    -I /vendor/lib64/libvsock_utils.so"
   diff_excludes[system]="\
-    -I /system/bin/assemble_cvd
-    -I /system/bin/console_forwarder
-    -I /system/bin/kernel_log_monitor
-    -I /system/bin/logcat_receiver
-    -I /system/bin/mkenvimage_slim
-    -I /system/bin/run_cvd
-    -I /system/bin/simg2img
-    -I /system/bin/log_tee
+    -I /system/bin/assemble_cvd \
+    -I /system/bin/console_forwarder \
+    -I /system/bin/kernel_log_monitor \
+    -I /system/bin/logcat_receiver \
+    -I /system/bin/mkenvimage_slim \
+    -I /system/bin/run_cvd \
+    -I /system/bin/simg2img \
+    -I /system/bin/log_tee \
     -I /system/lib64/android.hardware.confirmationui@1.0.so \
     -I /system/lib64/android.hardware.confirmationui-V1-ndk.so \
     -I /system/lib64/android.hardware.keymaster@4.1.so \