Revert "Change test_module_config from copying files to symlink ..."

Revert submission 3060229-ron-tmc-symlinks

Reason for revert: Droidmonitor created revert due to b/344045516

Reverted changes: /q/submissionid:3060229-ron-tmc-symlinks

Change-Id: If5045366677163560cdae95c0ab74256b4b49b9a
diff --git a/tradefed_modules/test_module_config.go b/tradefed_modules/test_module_config.go
index f8518f7..b2d5631 100644
--- a/tradefed_modules/test_module_config.go
+++ b/tradefed_modules/test_module_config.go
@@ -5,8 +5,6 @@
 	"android/soong/tradefed"
 	"encoding/json"
 	"fmt"
-	"io"
-	"strings"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -25,17 +23,14 @@
 type testModuleConfigModule struct {
 	android.ModuleBase
 	android.DefaultableModuleBase
+	base android.Module
 
 	tradefedProperties
 
 	// Our updated testConfig.
 	testConfig android.OutputPath
-	manifest   android.OutputPath
+	manifest   android.InstallPath
 	provider   tradefed.BaseTestProviderData
-
-	supportFiles android.InstallPaths
-
-	isHost bool
 }
 
 // Host is mostly the same as non-host, just some diffs for AddDependency and
@@ -99,6 +94,7 @@
 
 // Takes base's Tradefed Config xml file and generates a new one with the test properties
 // appeneded from this module.
+// Rewrite the name of the apk in "test-file-name" to be our module's name, rather than the original one.
 func (m *testModuleConfigModule) fixTestConfig(ctx android.ModuleContext, baseTestConfig android.Path) android.OutputPath {
 	// Test safe to do when no test_runner_options, but check for that earlier?
 	fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", ctx.ModuleName()+".config")
@@ -110,8 +106,9 @@
 	}
 	xmlTestModuleConfigSnippet, _ := json.Marshal(options)
 	escaped := proptools.NinjaAndShellEscape(string(xmlTestModuleConfigSnippet))
-	command.FlagWithArg("--test-runner-options=", escaped)
-
+	command.FlagWithArg("--test-file-name=", ctx.ModuleName()+".apk").
+		FlagWithArg("--orig-test-file-name=", *m.tradefedProperties.Base+".apk").
+		FlagWithArg("--test-runner-options=", escaped)
 	rule.Build("fix_test_config", "fix test config")
 	return fixedConfig.OutputPath
 }
@@ -193,7 +190,6 @@
 	module.AddProperties(&module.tradefedProperties)
 	android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
-	module.isHost = true
 
 	return module
 }
@@ -202,66 +198,39 @@
 var _ android.AndroidMkEntriesProvider = (*testModuleConfigModule)(nil)
 
 func (m *testModuleConfigModule) AndroidMkEntries() []android.AndroidMkEntries {
-	appClass := "APPS"
-	include := "$(BUILD_SYSTEM)/soong_app_prebuilt.mk"
-	if m.isHost {
-		appClass = "JAVA_LIBRARIES"
-		include = "$(BUILD_SYSTEM)/soong_java_prebuilt.mk"
-	}
-	return []android.AndroidMkEntries{{
-		Class:      appClass,
-		OutputFile: android.OptionalPathForPath(m.manifest),
-		Include:    include,
-		Required:   []string{*m.Base},
-		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
-			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
-				entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
-				entries.SetString("LOCAL_MODULE_TAGS", "tests")
-				entries.SetString("LOCAL_TEST_MODULE_CONFIG_BASE", *m.Base)
-				if m.provider.LocalSdkVersion != "" {
-					entries.SetString("LOCAL_SDK_VERSION", m.provider.LocalSdkVersion)
-				}
-				if m.provider.LocalCertificate != "" {
-					entries.SetString("LOCAL_CERTIFICATE", m.provider.LocalCertificate)
-				}
+	// We rely on base writing LOCAL_COMPATIBILITY_SUPPORT_FILES for its data files
+	entriesList := m.base.(android.AndroidMkEntriesProvider).AndroidMkEntries()
+	entries := &entriesList[0]
+	entries.OutputFile = android.OptionalPathForPath(m.provider.OutputFile)
+	entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
+		entries.SetString("LOCAL_MODULE", m.Name()) //  out module name, not base's
 
-				entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", m.provider.IsUnitTest)
-				entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
+		// Out update config file with extra options.
+		entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
+		entries.SetString("LOCAL_MODULE_TAGS", "tests")
 
-				// The app_prebuilt_internal.mk files try create a copy of the OutputFile as an .apk.
-				// Normally, this copies the "package.apk" from the intermediate directory here.
-				// To prevent the copy of the large apk and to prevent confusion with the real .apk we
-				// link to, we set the STEM here to a bogus name and we set OutputFile to a small file (our manifest).
-				// We do this so we don't have to add more conditionals to base_rules.mk
-				// soong_java_prebult has the same issue for .jars so use this in both module types.
-				entries.SetString("LOCAL_MODULE_STEM", fmt.Sprintf("UNUSED-%s", *m.Base))
+		// Don't append to base's test-suites, only use the ones we define, so clear it before
+		// appending to it.
+		entries.SetString("LOCAL_COMPATIBILITY_SUITE", "")
+		entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
 
-				// In normal java/app modules, the module writes LOCAL_COMPATIBILITY_SUPPORT_FILES
-				// and then base_rules.mk ends up copying each of those dependencies from .intermediates to the install directory.
-				// tasks/general-tests.mk, tasks/devices-tests.mk also use these to figure out
-				// which testcase files to put in a zip for running tests on another machine.
-				//
-				// We need our files to end up in the zip, but we don't want \.mk files to
-				// `install` files for us.
-				// So we create a new make variable to indicate these should be in the zip
-				// but not installed.
-				entries.AddStrings("LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES", m.supportFiles.Strings()...)
-			},
-		},
-		// Ensure each of our supportFiles depends on the installed file in base so that our symlinks will always
-		// resolve.  The provider gives us the .intermediate path for the support file in base, we change it to
-		// the installed path with a string substitution.
-		ExtraFooters: []android.AndroidMkExtraFootersFunc{
-			func(w io.Writer, name, prefix, moduleDir string) {
-				for _, f := range m.supportFiles.Strings() {
-					// convert out/.../testcases/FrameworksServicesTests_contentprotection/file1.apk
-					// to      out/.../testcases/FrameworksServicesTests/file1.apk
-					basePath := strings.Replace(f, "/"+m.Name()+"/", "/"+*m.Base+"/", 1)
-					fmt.Fprintf(w, "%s: %s\n", f, basePath)
-				}
-			},
-		},
-	}}
+		if len(m.provider.HostRequiredModuleNames) > 0 {
+			entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", m.provider.HostRequiredModuleNames...)
+		}
+		if len(m.provider.RequiredModuleNames) > 0 {
+			entries.AddStrings("LOCAL_REQUIRED_MODULES", m.provider.RequiredModuleNames...)
+		}
+
+		if m.provider.IsHost == false {
+			// Not needed for jar_host_test
+			//
+			// Clear the JNI symbols because they belong to base not us. Either transform the names in the string
+			// or clear the variable because we don't need it, we are copying bases libraries not generating
+			// new ones.
+			entries.SetString("LOCAL_SOONG_JNI_LIBS_SYMBOLS", "")
+		}
+	})
+	return entriesList
 }
 
 func (m *testModuleConfigHostModule) DepsMutator(ctx android.BottomUpMutatorContext) {
@@ -279,7 +248,7 @@
 //   - written via soong_java_prebuilt.mk
 //
 // 4) out/host/linux-x86/testcases/derived-module/* # data dependencies from base.
-//   - written via our InstallSymlink
+//   - written via soong_java_prebuilt.mk
 func (m *testModuleConfigHostModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	m.validateBase(ctx, &testModuleConfigHostTag, "java_test_host", true)
 	m.generateManifestAndConfig(ctx)
@@ -291,6 +260,7 @@
 	ctx.VisitDirectDepsWithTag(*depTag, func(dep android.Module) {
 		if provider, ok := android.OtherModuleProvider(ctx, dep, tradefed.BaseTestProviderKey); ok {
 			if baseShouldBeHost == provider.IsHost {
+				m.base = dep
 				m.provider = provider
 			} else {
 				if baseShouldBeHost {
@@ -307,9 +277,7 @@
 
 // Actions to write:
 //  1. manifest file to testcases dir
-//  2. Symlink to base.apk under base's arch dir
-//  3. Symlink to all data dependencies
-//  4. New Module.config / AndroidTest.xml file with our options.
+//  2. New Module.config / AndroidTest.xml file with our options.
 func (m *testModuleConfigModule) generateManifestAndConfig(ctx android.ModuleContext) {
 	// Keep before early returns.
 	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
@@ -324,6 +292,7 @@
 	if m.provider.TestConfig == nil {
 		return
 	}
+
 	// 1) A manifest file listing the base, write text to a tiny file.
 	installDir := android.PathForModuleInstall(ctx, ctx.ModuleName())
 	manifest := android.PathForModuleOut(ctx, "test_module_config.manifest")
@@ -332,53 +301,9 @@
 	//    Assume the primary install file is last
 	// so we need to Install our file last.
 	ctx.InstallFile(installDir, manifest.Base(), manifest)
-	m.manifest = manifest.OutputPath
 
-	// 2) Symlink to base.apk
-	baseApk := m.provider.OutputFile
-
-	// Typically looks like this for baseApk
-	// FrameworksServicesTests
-	// └── x86_64
-	//    └── FrameworksServicesTests.apk
-	symlinkName := fmt.Sprintf("%s/%s", ctx.DeviceConfig().DeviceArch(), baseApk.Base())
-	// Only android_test, not java_host_test puts the output in the DeviceArch dir.
-	if m.provider.IsHost {
-		// testcases/CtsDevicePolicyManagerTestCases
-		// ├── CtsDevicePolicyManagerTestCases.jar
-		symlinkName = baseApk.Base()
-	}
-	target := installedBaseRelativeToHere(symlinkName, *m.tradefedProperties.Base)
-	installedApk := ctx.InstallAbsoluteSymlink(installDir, symlinkName, target)
-	m.supportFiles = append(m.supportFiles, installedApk)
-
-	// 3) Symlink for all data deps
-	// And like this for data files and required modules
-	// FrameworksServicesTests
-	// ├── data
-	// │   └── broken_shortcut.xml
-	// ├── JobTestApp.apk
-	for _, f := range m.provider.InstalledFiles {
-		symlinkName := f.Rel()
-		target := installedBaseRelativeToHere(symlinkName, *m.tradefedProperties.Base)
-		installedPath := ctx.InstallAbsoluteSymlink(installDir, symlinkName, target)
-		m.supportFiles = append(m.supportFiles, installedPath)
-	}
-
-	// 4) Module.config / AndroidTest.xml
+	// 2) Module.config / AndroidTest.xml
 	m.testConfig = m.fixTestConfig(ctx, m.provider.TestConfig)
 }
 
 var _ android.AndroidMkEntriesProvider = (*testModuleConfigHostModule)(nil)
-
-// Given a relative path to a file in the current directory or a subdirectory,
-// return a relative path under our sibling directory named `base`.
-// There should be one "../" for each subdir we descend plus one to backup to "base".
-//
-//	 ThisDir/file1
-//	 ThisDir/subdir/file2
-//	would return "../base/file1" or "../../subdir/file2"
-func installedBaseRelativeToHere(targetFileName string, base string) string {
-	backup := strings.Repeat("../", strings.Count(targetFileName, "/")+1)
-	return fmt.Sprintf("%s%s/%s", backup, base, targetFileName)
-}