Merge "Add ConvertApexAvailableToTags"
diff --git a/android/bazel_handler.go b/android/bazel_handler.go
index 9ff6b52..d5886dc 100644
--- a/android/bazel_handler.go
+++ b/android/bazel_handler.go
@@ -45,11 +45,14 @@
 		CommandDeps: []string{"${bazelBuildRunfilesTool}"},
 	}, "outDir")
 	allowedBazelEnvironmentVars = []string{
+		// clang-tidy
 		"ALLOW_LOCAL_TIDY_TRUE",
 		"DEFAULT_TIDY_HEADER_DIRS",
 		"TIDY_TIMEOUT",
 		"WITH_TIDY",
 		"WITH_TIDY_FLAGS",
+		"TIDY_EXTERNAL_VENDOR",
+
 		"SKIP_ABI_CHECKS",
 		"UNSAFE_DISABLE_APEX_ALLOWED_DEPS_CHECK",
 		"AUTO_ZERO_INITIALIZE",
diff --git a/android/config.go b/android/config.go
index 600fda0..bb3cc97 100644
--- a/android/config.go
+++ b/android/config.go
@@ -397,11 +397,13 @@
 arch_variant_product_var_constraints = _arch_variant_product_var_constraints
 `,
 	}
-	err = os.WriteFile(filepath.Join(dir, "product_variables.bzl"), []byte(strings.Join(bzl, "\n")), 0644)
+	err = pathtools.WriteFileIfChanged(filepath.Join(dir, "product_variables.bzl"),
+		[]byte(strings.Join(bzl, "\n")), 0644)
 	if err != nil {
 		return fmt.Errorf("Could not write .bzl config file %s", err)
 	}
-	err = os.WriteFile(filepath.Join(dir, "BUILD"), []byte(bazel.GeneratedBazelFileWarning), 0644)
+	err = pathtools.WriteFileIfChanged(filepath.Join(dir, "BUILD"),
+		[]byte(bazel.GeneratedBazelFileWarning), 0644)
 	if err != nil {
 		return fmt.Errorf("Could not write BUILD config file %s", err)
 	}
diff --git a/android/packaging.go b/android/packaging.go
index ecd84a2..c6b20ea 100644
--- a/android/packaging.go
+++ b/android/packaging.go
@@ -73,6 +73,10 @@
 	return p.partition
 }
 
+func (p *PackagingSpec) SrcPath() Path {
+	return p.srcPath
+}
+
 type PackageModule interface {
 	Module
 	packagingBase() *PackagingBase
diff --git a/android/paths.go b/android/paths.go
index 2c50104..6c3009f 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -16,7 +16,6 @@
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"reflect"
@@ -2099,13 +2098,16 @@
 
 // Writes a file to the output directory.  Attempting to write directly to the output directory
 // will fail due to the sandbox of the soong_build process.
+// Only writes the file if the file doesn't exist or if it has different contents, to prevent
+// updating the timestamp if no changes would be made. (This is better for incremental
+// performance.)
 func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
 	absPath := absolutePath(path.String())
 	err := os.MkdirAll(filepath.Dir(absPath), 0777)
 	if err != nil {
 		return err
 	}
-	return ioutil.WriteFile(absPath, data, perm)
+	return pathtools.WriteFileIfChanged(absPath, data, perm)
 }
 
 func RemoveAllOutputDir(path WritablePath) error {
diff --git a/bp2build/symlink_forest.go b/bp2build/symlink_forest.go
index 183eb12..aac5e7d 100644
--- a/bp2build/symlink_forest.go
+++ b/bp2build/symlink_forest.go
@@ -25,6 +25,7 @@
 	"sync/atomic"
 
 	"android/soong/shared"
+	"github.com/google/blueprint/pathtools"
 )
 
 // A tree structure that describes what to do at each directory in the created
@@ -116,25 +117,13 @@
 		generatedBuildFileContent = packageDefaultVisibilityRegex.ReplaceAll(generatedBuildFileContent, []byte{})
 	}
 
-	outFile, err := os.Create(output)
-	if err != nil {
-		return err
+	newContents := generatedBuildFileContent
+	if newContents[len(newContents)-1] != '\n' {
+		newContents = append(newContents, '\n')
 	}
+	newContents = append(newContents, srcBuildFileContent...)
 
-	_, err = outFile.Write(generatedBuildFileContent)
-	if err != nil {
-		return err
-	}
-
-	if generatedBuildFileContent[len(generatedBuildFileContent)-1] != '\n' {
-		_, err = outFile.WriteString("\n")
-		if err != nil {
-			return err
-		}
-	}
-
-	_, err = outFile.Write(srcBuildFileContent)
-	return err
+	return pathtools.WriteFileIfChanged(output, newContents, 0666)
 }
 
 // Calls readdir() and returns it as a map from the basename of the files in dir
diff --git a/cc/config/riscv64_device.go b/cc/config/riscv64_device.go
index 67208b2..b3619c8 100644
--- a/cc/config/riscv64_device.go
+++ b/cc/config/riscv64_device.go
@@ -35,7 +35,9 @@
 	}
 
 	riscv64Lldflags = append(riscv64Ldflags,
-		"-Wl,-z,max-page-size=4096")
+		"-Wl,-z,max-page-size=4096",
+		"-Wl,-plugin-opt,-emulated-tls=0",
+	)
 
 	riscv64Cppflags = []string{}
 
diff --git a/python/binary.go b/python/binary.go
index 75135f3..95c751a 100644
--- a/python/binary.go
+++ b/python/binary.go
@@ -125,6 +125,25 @@
 				launcherPath = provider.IntermPathForModuleOut()
 			}
 		})
+
+		// TODO: get the list of shared libraries directly from the launcher module somehow
+		var sharedLibs []string
+		sharedLibs = append(sharedLibs, "libsqlite")
+		if ctx.Target().Os.Bionic() {
+			sharedLibs = append(sharedLibs, "libc", "libdl", "libm")
+		}
+		if ctx.Target().Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
+			sharedLibs = append(sharedLibs, "libc_musl")
+		}
+		switch p.properties.Actual_version {
+		case pyVersion2:
+			sharedLibs = append(sharedLibs, "libc++")
+		case pyVersion3:
+			if ctx.Device() {
+				sharedLibs = append(sharedLibs, "liblog")
+			}
+		}
+		p.androidMkSharedLibs = sharedLibs
 	}
 	srcsZips := make(android.Paths, 0, len(depsSrcsZips)+1)
 	if embeddedLauncher {
@@ -136,14 +155,6 @@
 	p.installSource = registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
 		p.getHostInterpreterName(ctx, p.properties.Actual_version),
 		main, p.getStem(ctx), srcsZips)
-
-	var sharedLibs []string
-	// if embedded launcher is enabled, we need to collect the shared library dependencies of the
-	// launcher
-	for _, dep := range ctx.GetDirectDepsWithTag(launcherSharedLibTag) {
-		sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
-	}
-	p.androidMkSharedLibs = sharedLibs
 }
 
 func (p *PythonBinaryModule) AndroidMkEntries() []android.AndroidMkEntries {
@@ -176,7 +187,7 @@
 	p.PythonLibraryModule.DepsMutator(ctx)
 
 	if p.isEmbeddedLauncherEnabled() {
-		p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, launcherSharedLibTag, p.autorun(), ctx.Target())
+		p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, p.autorun(), ctx.Target())
 	}
 }
 
diff --git a/python/python.go b/python/python.go
index 18e5b68..c23be8d 100644
--- a/python/python.go
+++ b/python/python.go
@@ -226,20 +226,18 @@
 	pythonLibTag = dependencyTag{name: "pythonLib"}
 	javaDataTag  = dependencyTag{name: "javaData"}
 	// The python interpreter, with soong module name "py3-launcher" or "py3-launcher-autorun".
-	launcherTag          = dependencyTag{name: "launcher"}
-	launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"}
+	launcherTag = dependencyTag{name: "launcher"}
 	// The python interpreter built for host so that we can precompile python sources.
 	// This only works because the precompiled sources don't vary by architecture.
 	// The soong module name is "py3-launcher".
-	hostLauncherTag          = dependencyTag{name: "hostLauncher"}
-	hostlauncherSharedLibTag = dependencyTag{name: "hostlauncherSharedLib"}
-	hostStdLibTag            = dependencyTag{name: "hostStdLib"}
-	pathComponentRegexp      = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
-	pyExt                    = ".py"
-	protoExt                 = ".proto"
-	pyVersion2               = "PY2"
-	pyVersion3               = "PY3"
-	internalPath             = "internal"
+	hostLauncherTag     = dependencyTag{name: "hostLauncher"}
+	hostStdLibTag       = dependencyTag{name: "hostStdLib"}
+	pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`)
+	pyExt               = ".py"
+	protoExt            = ".proto"
+	pyVersion2          = "PY2"
+	pyVersion3          = "PY3"
+	internalPath        = "internal"
 )
 
 type basePropertiesProvider interface {
@@ -323,35 +321,21 @@
 	javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
 	ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...)
 
-	p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, hostlauncherSharedLibTag, false, ctx.Config().BuildOSTarget)
+	p.AddDepsOnPythonLauncherAndStdlib(ctx, hostStdLibTag, hostLauncherTag, false, ctx.Config().BuildOSTarget)
 }
 
-// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib,
-// launcher (interpreter), and the launcher's shared libraries. If autorun is true, it will use
-// the autorun launcher instead of the regular one. This function acceps a targetForDeps argument
-// as the target to use for these dependencies. For embedded launcher python binaries, the launcher
-// that will be embedded will be under the same target as the python module itself. But when
-// precompiling python code, we need to get the python launcher built for host, even if we're
-// compiling the python module for device, so we pass a different target to this function.
+// AddDepsOnPythonLauncherAndStdlib will make the current module depend on the python stdlib
+// and launcher (interpreter). If autorun is true, it will use the autorun launcher instead of the
+// regular one. This function accepts a targetForDeps argument as the target to use for these
+// dependencies. For embedded launcher python binaries, the launcher that will be embedded will be
+// under the same target as the python module itself. But when precompiling python code, we need to
+// get the python launcher built for host, even if we're compiling the python module for device, so
+// we pass a different target to this function.
 func (p *PythonLibraryModule) AddDepsOnPythonLauncherAndStdlib(ctx android.BottomUpMutatorContext,
-	stdLibTag, launcherTag, launcherSharedLibTag blueprint.DependencyTag,
+	stdLibTag, launcherTag blueprint.DependencyTag,
 	autorun bool, targetForDeps android.Target) {
 	var stdLib string
 	var launcherModule string
-	// Add launcher shared lib dependencies. Ideally, these should be
-	// derived from the `shared_libs` property of the launcher. TODO: read these from
-	// the python launcher itself using ctx.OtherModuleProvider() or similar on the result
-	// of ctx.AddFarVariationDependencies()
-	launcherSharedLibDeps := []string{
-		"libsqlite",
-	}
-	// Add launcher-specific dependencies for bionic
-	if targetForDeps.Os.Bionic() {
-		launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm")
-	}
-	if targetForDeps.Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() {
-		launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl")
-	}
 
 	switch p.properties.Actual_version {
 	case pyVersion2:
@@ -362,7 +346,6 @@
 			launcherModule = "py2-launcher-autorun"
 		}
 
-		launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++")
 	case pyVersion3:
 		stdLib = "py3-stdlib"
 
@@ -373,9 +356,6 @@
 		if ctx.Config().HostStaticBinaries() && targetForDeps.Os == android.LinuxMusl {
 			launcherModule += "-static"
 		}
-		if ctx.Device() {
-			launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog")
-		}
 	default:
 		panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.",
 			p.properties.Actual_version, ctx.ModuleName()))
@@ -391,7 +371,6 @@
 		ctx.AddFarVariationDependencies(stdLibVariations, stdLibTag, stdLib)
 	}
 	ctx.AddFarVariationDependencies(targetVariations, launcherTag, launcherModule)
-	ctx.AddFarVariationDependencies(targetVariations, launcherSharedLibTag, launcherSharedLibDeps...)
 }
 
 // GenerateAndroidBuildActions performs build actions common to all Python modules
@@ -595,22 +574,19 @@
 			}
 		})
 	}
+	var launcherSharedLibs android.Paths
+	var ldLibraryPath []string
 	ctx.VisitDirectDepsWithTag(hostLauncherTag, func(module android.Module) {
 		if dep, ok := module.(IntermPathProvider); ok {
 			optionalLauncher := dep.IntermPathForModuleOut()
 			if optionalLauncher.Valid() {
 				launcher = optionalLauncher.Path()
 			}
-		}
-	})
-	var launcherSharedLibs android.Paths
-	var ldLibraryPath []string
-	ctx.VisitDirectDepsWithTag(hostlauncherSharedLibTag, func(module android.Module) {
-		if dep, ok := module.(IntermPathProvider); ok {
-			optionalPath := dep.IntermPathForModuleOut()
-			if optionalPath.Valid() {
-				launcherSharedLibs = append(launcherSharedLibs, optionalPath.Path())
-				ldLibraryPath = append(ldLibraryPath, filepath.Dir(optionalPath.Path().String()))
+			for _, spec := range module.TransitivePackagingSpecs() {
+				if strings.HasSuffix(spec.SrcPath().String(), ".so") {
+					launcherSharedLibs = append(launcherSharedLibs, spec.SrcPath())
+					ldLibraryPath = append(ldLibraryPath, filepath.Dir(spec.SrcPath().String()))
+				}
 			}
 		}
 	})
diff --git a/rust/config/global.go b/rust/config/global.go
index 50ac1f7..ef428b8 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.65.0.p1"
+	RustDefaultVersion = "1.66.1"
 	RustDefaultBase    = "prebuilts/rust/"
 	DefaultEdition     = "2021"
 	Stdlibs            = []string{