Merge "Convert system modules tests to use fixtures"
diff --git a/android/config.go b/android/config.go
index ae4df1c..bc1aa3a 100644
--- a/android/config.go
+++ b/android/config.go
@@ -287,24 +287,21 @@
 	return testConfig
 }
 
-// TestArchConfigFuchsia returns a Config object suitable for using for
-// tests that need to run the arch mutator for the Fuchsia arch.
-func TestArchConfigFuchsia(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
-	testConfig := TestConfig(buildDir, env, bp, fs)
-	config := testConfig.config
-
-	config.Targets = map[OsType][]Target{
-		Fuchsia: []Target{
+func fuchsiaTargets() map[OsType][]Target {
+	return map[OsType][]Target{
+		Fuchsia: {
 			{Fuchsia, Arch{ArchType: Arm64, ArchVariant: "", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
 		},
-		BuildOs: []Target{
+		BuildOs: {
 			{BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
 		},
 	}
-
-	return testConfig
 }
 
+var PrepareForTestSetDeviceToFuchsia = FixtureModifyConfig(func(config Config) {
+	config.Targets = fuchsiaTargets()
+})
+
 func modifyTestConfigToSupportArchMutator(testConfig Config) {
 	config := testConfig.config
 
@@ -527,25 +524,6 @@
 	return PathForOutput(ctx, "host", c.PrebuiltOS(), "framework", path)
 }
 
-// NonHermeticHostSystemTool looks for non-hermetic tools from the system we're
-// running on. These tools are not checked-in to AOSP, and therefore could lead
-// to reproducibility problems. Should not be used for other than finding the
-// XCode SDK (xcrun, sw_vers), etc. See ui/build/paths/config.go for the
-// allowlist of host system tools.
-func (c *config) NonHermeticHostSystemTool(name string) string {
-	for _, dir := range filepath.SplitList(c.Getenv("PATH")) {
-		path := filepath.Join(dir, name)
-		if s, err := os.Stat(path); err != nil {
-			continue
-		} else if m := s.Mode(); !s.IsDir() && m&0111 != 0 {
-			return path
-		}
-	}
-	panic(fmt.Errorf(
-		"Cannot find non-hermetic system tool '%s' on path '%s'",
-		name, c.Getenv("PATH")))
-}
-
 // PrebuiltOS returns the name of the host OS used in prebuilts directories.
 func (c *config) PrebuiltOS() string {
 	switch runtime.GOOS {
diff --git a/android/fixture.go b/android/fixture.go
index 1def9c0..25dee27 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -581,6 +581,16 @@
 	}
 }
 
+// AssertStringListContains checks if the list of strings contains the expected string. If it does
+// not then it reports an error prefixed with the supplied message and including a reason for why it
+// failed.
+func (h *TestHelper) AssertStringListContains(message string, list []string, expected string) {
+	h.Helper()
+	if !InList(expected, list) {
+		h.Errorf("%s: could not find %q within %q", message, expected, list)
+	}
+}
+
 // AssertArrayString checks if the expected and actual values are equal and if they are not then it
 // reports an error prefixed with the supplied message and including a reason for why it failed.
 func (h *TestHelper) AssertArrayString(message string, expected, actual []string) {
diff --git a/android/variable.go b/android/variable.go
index dd000ad..be12a0a 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -24,11 +24,17 @@
 )
 
 func init() {
-	PreDepsMutators(func(ctx RegisterMutatorsContext) {
+	registerVariableBuildComponents(InitRegistrationContext)
+}
+
+func registerVariableBuildComponents(ctx RegistrationContext) {
+	ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
 		ctx.BottomUp("variable", VariableMutator).Parallel()
 	})
 }
 
+var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
+
 type variableProperties struct {
 	Product_variables struct {
 		Platform_sdk_version struct {
diff --git a/android/writedocs.go b/android/writedocs.go
index 6cb2f10..67b9aa3 100644
--- a/android/writedocs.go
+++ b/android/writedocs.go
@@ -35,10 +35,11 @@
 
 func primaryBuilderPath(ctx SingletonContext) Path {
 	buildDir := absolutePath(ctx.Config().BuildDir())
-	primaryBuilder, err := filepath.Rel(buildDir, os.Args[0])
+	binary := absolutePath(os.Args[0])
+	primaryBuilder, err := filepath.Rel(buildDir, binary)
 	if err != nil {
-		ctx.Errorf("path to primary builder %q is not in build dir %q",
-			os.Args[0], ctx.Config().BuildDir())
+		ctx.Errorf("path to primary builder %q is not in build dir %q (%q)",
+			os.Args[0], ctx.Config().BuildDir(), err)
 	}
 
 	return PathForOutput(ctx, primaryBuilder)
diff --git a/cc/cc_test.go b/cc/cc_test.go
index 4f28eaf..cc1f7d0 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -181,13 +181,12 @@
 			},
 		}`
 
-	config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
-	ctx := testCcWithConfig(t, config)
+	result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
 
 	rt := false
 	fb := false
 
-	ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
+	ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
 	implicits := ld.Implicits
 	for _, lib := range implicits {
 		if strings.Contains(lib.Rel(), "libcompiler_rt") {
@@ -218,16 +217,13 @@
 			},
 		}`
 
-	config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
-	ctx := testCcWithConfig(t, config)
-	ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
+	result := ccFixtureFactory.Extend(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())
 	}
-	if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
-		t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
-	}
+	result.AssertArrayString("libTest inputs", []string{"foo.o", "bar.o"}, objs)
 }
 
 func TestVendorSrc(t *testing.T) {
diff --git a/cc/config/x86_darwin_host.go b/cc/config/x86_darwin_host.go
index 1035df3..b0344af 100644
--- a/cc/config/x86_darwin_host.go
+++ b/cc/config/x86_darwin_host.go
@@ -136,7 +136,7 @@
 
 func getMacTools(ctx android.PackageVarContext) *macPlatformTools {
 	macTools.once.Do(func() {
-		xcrunTool := ctx.Config().NonHermeticHostSystemTool("xcrun")
+		xcrunTool := "/usr/bin/xcrun"
 
 		xcrun := func(args ...string) string {
 			if macTools.err != nil {
diff --git a/cc/testing.go b/cc/testing.go
index fcd124e..6840ef4 100644
--- a/cc/testing.go
+++ b/cc/testing.go
@@ -653,6 +653,14 @@
 	android.FixtureAddTextFile(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(
@@ -685,7 +693,7 @@
 
 	var config android.Config
 	if os == android.Fuchsia {
-		config = android.TestArchConfigFuchsia(buildDir, env, bp, mockFS)
+		panic("Fuchsia not supported use test fixture instead")
 	} else {
 		config = android.TestArchConfig(buildDir, env, bp, mockFS)
 	}
diff --git a/java/prebuilt_apis.go b/java/prebuilt_apis.go
index 59b8b8c..8a442b5 100644
--- a/java/prebuilt_apis.go
+++ b/java/prebuilt_apis.go
@@ -248,9 +248,9 @@
 	}
 
 	// Create incompatibilities tracking files for all modules, if we have a "next" api.
+	incompatibilities := make(map[string]bool)
 	if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
 		files := getPrebuiltFilesInSubdir(mctx, nextApiDir, "api/*incompatibilities.txt")
-		incompatibilities := make(map[string]bool)
 		for _, f := range files {
 			localPath := strings.TrimPrefix(f, mydir)
 			module, _, scope := parseApiFilePath(mctx, localPath)
@@ -266,11 +266,11 @@
 
 			incompatibilities[referencedModule+"."+scope] = true
 		}
-		// Create empty incompatibilities files for remaining modules
-		for _, k := range android.SortedStringKeys(m) {
-			if _, ok := incompatibilities[k]; !ok {
-				createEmptyFile(mctx, apiModuleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
-			}
+	}
+	// Create empty incompatibilities files for remaining modules
+	for _, k := range android.SortedStringKeys(m) {
+		if _, ok := incompatibilities[k]; !ok {
+			createEmptyFile(mctx, apiModuleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
 		}
 	}
 }
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 30d120d..b03f90c 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -452,6 +452,7 @@
 	// that references the latest released API and remove API specification files.
 	// * API specification filegroup -> <dist-stem>.api.<scope>.latest
 	// * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
+	// * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
 	Dist_stem *string
 
 	// A compatibility mode that allows historical API-tracking files to not exist.
@@ -1059,6 +1060,9 @@
 		if m := android.SrcIsModule(module.latestRemovedApiFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
 			missingApiModules = append(missingApiModules, m)
 		}
+		if m := android.SrcIsModule(module.latestIncompatibilitiesFilegroupName(apiScope)); !ctx.OtherModuleExists(m) {
+			missingApiModules = append(missingApiModules, m)
+		}
 	}
 	if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
 		m := module.Name() + " is missing tracking files for previously released library versions.\n"
@@ -1165,6 +1169,10 @@
 	return ":" + module.distStem() + "-removed.api." + apiScope.name + ".latest"
 }
 
+func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
+	return ":" + module.distStem() + "-incompatibilities.api." + apiScope.name + ".latest"
+}
+
 func childModuleVisibility(childVisibility []string) []string {
 	if childVisibility == nil {
 		// No child visibility set. The child will use the visibility of the sdk_library.
@@ -1389,6 +1397,8 @@
 		props.Check_api.Last_released.Api_file = latestApiFilegroupName
 		props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
 			module.latestRemovedApiFilegroupName(apiScope))
+		props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
+			module.latestIncompatibilitiesFilegroupName(apiScope))
 
 		if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
 			// Enable api lint.
diff --git a/ui/build/soong.go b/ui/build/soong.go
index 500abda..884e957 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -177,14 +177,6 @@
 		ninjaEnv.Set("TOP", os.Getenv("TOP"))
 		ninjaEnv.Set("SOONG_OUTDIR", config.SoongOutDir())
 
-		// Needed for NonHermeticHostSystemTool() and that, only in tests. We should
-		// probably find a better way of running tests other than making $PATH
-		// available also to production builds. Note that this is not get same as
-		// os.Getenv("PATH"): config.Environment() contains the $PATH that redirects
-		// every binary through the path interposer.
-		configPath, _ := config.Environment().Get("PATH")
-		ninjaEnv.Set("PATH", configPath)
-
 		// For debugging
 		if os.Getenv("SOONG_DELVE") != "" {
 			ninjaEnv.Set("SOONG_DELVE", os.Getenv("SOONG_DELVE"))