xref: /aosp_15_r20/build/soong/tradefed_modules/test_module_config.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Workerpackage tradefed_modules
2*333d2b36SAndroid Build Coastguard Worker
3*333d2b36SAndroid Build Coastguard Workerimport (
4*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
5*333d2b36SAndroid Build Coastguard Worker	"android/soong/tradefed"
6*333d2b36SAndroid Build Coastguard Worker	"encoding/json"
7*333d2b36SAndroid Build Coastguard Worker	"fmt"
8*333d2b36SAndroid Build Coastguard Worker	"io"
9*333d2b36SAndroid Build Coastguard Worker	"slices"
10*333d2b36SAndroid Build Coastguard Worker	"strings"
11*333d2b36SAndroid Build Coastguard Worker
12*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
13*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
14*333d2b36SAndroid Build Coastguard Worker)
15*333d2b36SAndroid Build Coastguard Worker
16*333d2b36SAndroid Build Coastguard Workerfunc init() {
17*333d2b36SAndroid Build Coastguard Worker	RegisterTestModuleConfigBuildComponents(android.InitRegistrationContext)
18*333d2b36SAndroid Build Coastguard Worker}
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Worker// Register the license_kind module type.
21*333d2b36SAndroid Build Coastguard Workerfunc RegisterTestModuleConfigBuildComponents(ctx android.RegistrationContext) {
22*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("test_module_config", TestModuleConfigFactory)
23*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("test_module_config_host", TestModuleConfigHostFactory)
24*333d2b36SAndroid Build Coastguard Worker}
25*333d2b36SAndroid Build Coastguard Worker
26*333d2b36SAndroid Build Coastguard Workertype testModuleConfigModule struct {
27*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
28*333d2b36SAndroid Build Coastguard Worker	android.DefaultableModuleBase
29*333d2b36SAndroid Build Coastguard Worker
30*333d2b36SAndroid Build Coastguard Worker	tradefedProperties
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Worker	// Our updated testConfig.
33*333d2b36SAndroid Build Coastguard Worker	testConfig android.OutputPath
34*333d2b36SAndroid Build Coastguard Worker	manifest   android.OutputPath
35*333d2b36SAndroid Build Coastguard Worker	provider   tradefed.BaseTestProviderData
36*333d2b36SAndroid Build Coastguard Worker
37*333d2b36SAndroid Build Coastguard Worker	supportFiles android.InstallPaths
38*333d2b36SAndroid Build Coastguard Worker
39*333d2b36SAndroid Build Coastguard Worker	isHost bool
40*333d2b36SAndroid Build Coastguard Worker}
41*333d2b36SAndroid Build Coastguard Worker
42*333d2b36SAndroid Build Coastguard Worker// Host is mostly the same as non-host, just some diffs for AddDependency and
43*333d2b36SAndroid Build Coastguard Worker// AndroidMkEntries, but the properties are the same.
44*333d2b36SAndroid Build Coastguard Workertype testModuleConfigHostModule struct {
45*333d2b36SAndroid Build Coastguard Worker	testModuleConfigModule
46*333d2b36SAndroid Build Coastguard Worker}
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Worker// Properties to list in Android.bp for this module.
49*333d2b36SAndroid Build Coastguard Workertype tradefedProperties struct {
50*333d2b36SAndroid Build Coastguard Worker	// Module name of the base test that we will run.
51*333d2b36SAndroid Build Coastguard Worker	Base *string `android:"path,arch_variant"`
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Worker	// Tradefed Options to add to tradefed xml when not one of the include or exclude filter or property.
54*333d2b36SAndroid Build Coastguard Worker	// Sample: [{name: "TestRunnerOptionName", value: "OptionValue" }]
55*333d2b36SAndroid Build Coastguard Worker	Options []tradefed.Option
56*333d2b36SAndroid Build Coastguard Worker
57*333d2b36SAndroid Build Coastguard Worker	// List of tradefed include annotations to add to tradefed xml, like "android.platform.test.annotations.Presubmit".
58*333d2b36SAndroid Build Coastguard Worker	// Tests will be restricted to those matching an include_annotation or include_filter.
59*333d2b36SAndroid Build Coastguard Worker	Include_annotations []string
60*333d2b36SAndroid Build Coastguard Worker
61*333d2b36SAndroid Build Coastguard Worker	// List of tradefed include annotations to add to tradefed xml, like "android.support.test.filters.FlakyTest".
62*333d2b36SAndroid Build Coastguard Worker	// Tests matching an exclude annotation or filter will be skipped.
63*333d2b36SAndroid Build Coastguard Worker	Exclude_annotations []string
64*333d2b36SAndroid Build Coastguard Worker
65*333d2b36SAndroid Build Coastguard Worker	// List of tradefed include filters to add to tradefed xml, like "fully.qualified.class#method".
66*333d2b36SAndroid Build Coastguard Worker	// Tests will be restricted to those matching an include_annotation or include_filter.
67*333d2b36SAndroid Build Coastguard Worker	Include_filters []string
68*333d2b36SAndroid Build Coastguard Worker
69*333d2b36SAndroid Build Coastguard Worker	// List of tradefed exclude filters to add to tradefed xml, like "fully.qualified.class#method".
70*333d2b36SAndroid Build Coastguard Worker	// Tests matching an exclude annotation or filter will be skipped.
71*333d2b36SAndroid Build Coastguard Worker	Exclude_filters []string
72*333d2b36SAndroid Build Coastguard Worker
73*333d2b36SAndroid Build Coastguard Worker	// List of compatibility suites (for example "cts", "vts") that the module should be
74*333d2b36SAndroid Build Coastguard Worker	// installed into.
75*333d2b36SAndroid Build Coastguard Worker	Test_suites []string
76*333d2b36SAndroid Build Coastguard Worker}
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Workertype dependencyTag struct {
79*333d2b36SAndroid Build Coastguard Worker	blueprint.BaseDependencyTag
80*333d2b36SAndroid Build Coastguard Worker	name string
81*333d2b36SAndroid Build Coastguard Worker}
82*333d2b36SAndroid Build Coastguard Worker
83*333d2b36SAndroid Build Coastguard Workervar (
84*333d2b36SAndroid Build Coastguard Worker	testModuleConfigTag     = dependencyTag{name: "TestModuleConfigBase"}
85*333d2b36SAndroid Build Coastguard Worker	testModuleConfigHostTag = dependencyTag{name: "TestModuleConfigHostBase"}
86*333d2b36SAndroid Build Coastguard Worker	pctx                    = android.NewPackageContext("android/soong/tradefed_modules")
87*333d2b36SAndroid Build Coastguard Worker)
88*333d2b36SAndroid Build Coastguard Worker
89*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) InstallInTestcases() bool {
90*333d2b36SAndroid Build Coastguard Worker	return true
91*333d2b36SAndroid Build Coastguard Worker}
92*333d2b36SAndroid Build Coastguard Worker
93*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) DepsMutator(ctx android.BottomUpMutatorContext) {
94*333d2b36SAndroid Build Coastguard Worker	if m.Base == nil {
95*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("'base' field must be set to a 'android_test' module.")
96*333d2b36SAndroid Build Coastguard Worker		return
97*333d2b36SAndroid Build Coastguard Worker	}
98*333d2b36SAndroid Build Coastguard Worker	ctx.AddDependency(ctx.Module(), testModuleConfigTag, *m.Base)
99*333d2b36SAndroid Build Coastguard Worker}
100*333d2b36SAndroid Build Coastguard Worker
101*333d2b36SAndroid Build Coastguard Worker// Takes base's Tradefed Config xml file and generates a new one with the test properties
102*333d2b36SAndroid Build Coastguard Worker// appeneded from this module.
103*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) fixTestConfig(ctx android.ModuleContext, baseTestConfig android.Path) android.OutputPath {
104*333d2b36SAndroid Build Coastguard Worker	// Test safe to do when no test_runner_options, but check for that earlier?
105*333d2b36SAndroid Build Coastguard Worker	fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", ctx.ModuleName()+".config")
106*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
107*333d2b36SAndroid Build Coastguard Worker	command := rule.Command().BuiltTool("test_config_fixer").Input(baseTestConfig).Output(fixedConfig)
108*333d2b36SAndroid Build Coastguard Worker	options := m.composeOptions()
109*333d2b36SAndroid Build Coastguard Worker	if len(options) == 0 {
110*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("Test options must be given when using test_module_config. Set include/exclude filter or annotation.")
111*333d2b36SAndroid Build Coastguard Worker	}
112*333d2b36SAndroid Build Coastguard Worker	xmlTestModuleConfigSnippet, _ := json.Marshal(options)
113*333d2b36SAndroid Build Coastguard Worker	escaped := proptools.NinjaAndShellEscape(string(xmlTestModuleConfigSnippet))
114*333d2b36SAndroid Build Coastguard Worker	command.FlagWithArg("--test-runner-options=", escaped)
115*333d2b36SAndroid Build Coastguard Worker
116*333d2b36SAndroid Build Coastguard Worker	rule.Build("fix_test_config", "fix test config")
117*333d2b36SAndroid Build Coastguard Worker	return fixedConfig.OutputPath
118*333d2b36SAndroid Build Coastguard Worker}
119*333d2b36SAndroid Build Coastguard Worker
120*333d2b36SAndroid Build Coastguard Worker// Convert --exclude_filters: ["filter1", "filter2"] ->
121*333d2b36SAndroid Build Coastguard Worker// [ Option{Name: "exclude-filters", Value: "filter1"}, Option{Name: "exclude-filters", Value: "filter2"},
122*333d2b36SAndroid Build Coastguard Worker// ... + include + annotations ]
123*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) composeOptions() []tradefed.Option {
124*333d2b36SAndroid Build Coastguard Worker	options := m.Options
125*333d2b36SAndroid Build Coastguard Worker	for _, e := range m.Exclude_filters {
126*333d2b36SAndroid Build Coastguard Worker		options = append(options, tradefed.Option{Name: "exclude-filter", Value: e})
127*333d2b36SAndroid Build Coastguard Worker	}
128*333d2b36SAndroid Build Coastguard Worker	for _, i := range m.Include_filters {
129*333d2b36SAndroid Build Coastguard Worker		options = append(options, tradefed.Option{Name: "include-filter", Value: i})
130*333d2b36SAndroid Build Coastguard Worker	}
131*333d2b36SAndroid Build Coastguard Worker	for _, e := range m.Exclude_annotations {
132*333d2b36SAndroid Build Coastguard Worker		options = append(options, tradefed.Option{Name: "exclude-annotation", Value: e})
133*333d2b36SAndroid Build Coastguard Worker	}
134*333d2b36SAndroid Build Coastguard Worker	for _, i := range m.Include_annotations {
135*333d2b36SAndroid Build Coastguard Worker		options = append(options, tradefed.Option{Name: "include-annotation", Value: i})
136*333d2b36SAndroid Build Coastguard Worker	}
137*333d2b36SAndroid Build Coastguard Worker	return options
138*333d2b36SAndroid Build Coastguard Worker}
139*333d2b36SAndroid Build Coastguard Worker
140*333d2b36SAndroid Build Coastguard Worker// Files to write and where they come from:
141*333d2b36SAndroid Build Coastguard Worker// 1) test_module_config.manifest
142*333d2b36SAndroid Build Coastguard Worker//   - Leave a trail of where we got files from in case other tools need it.
143*333d2b36SAndroid Build Coastguard Worker//
144*333d2b36SAndroid Build Coastguard Worker// 2) $Module.config
145*333d2b36SAndroid Build Coastguard Worker//   - comes from base's module.config (AndroidTest.xml), and then we add our test_options.
146*333d2b36SAndroid Build Coastguard Worker//     provider.TestConfig
147*333d2b36SAndroid Build Coastguard Worker//     [rules via soong_app_prebuilt]
148*333d2b36SAndroid Build Coastguard Worker//
149*333d2b36SAndroid Build Coastguard Worker// 3) $ARCH/$Module.apk
150*333d2b36SAndroid Build Coastguard Worker//   - comes from base
151*333d2b36SAndroid Build Coastguard Worker//     provider.OutputFile
152*333d2b36SAndroid Build Coastguard Worker//     [rules via soong_app_prebuilt]
153*333d2b36SAndroid Build Coastguard Worker//
154*333d2b36SAndroid Build Coastguard Worker// 4) [bases data]
155*333d2b36SAndroid Build Coastguard Worker//   - We copy all of bases data (like helper apks) to our install directory too.
156*333d2b36SAndroid Build Coastguard Worker//     Since we call AndroidMkEntries on base, it will write out LOCAL_COMPATIBILITY_SUPPORT_FILES
157*333d2b36SAndroid Build Coastguard Worker//     with this data and app_prebuilt.mk will generate the rules to copy it from base.
158*333d2b36SAndroid Build Coastguard Worker//     We have no direct rules here to add to ninja.
159*333d2b36SAndroid Build Coastguard Worker//
160*333d2b36SAndroid Build Coastguard Worker// If we change to symlinks, this all needs to change.
161*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
162*333d2b36SAndroid Build Coastguard Worker	m.validateBase(ctx, &testModuleConfigTag, "android_test", false)
163*333d2b36SAndroid Build Coastguard Worker	m.generateManifestAndConfig(ctx)
164*333d2b36SAndroid Build Coastguard Worker
165*333d2b36SAndroid Build Coastguard Worker}
166*333d2b36SAndroid Build Coastguard Worker
167*333d2b36SAndroid Build Coastguard Worker// Ensure at least one test_suite is listed.  Ideally it should be general-tests
168*333d2b36SAndroid Build Coastguard Worker// or device-tests, whichever is listed in base and prefer general-tests if both are listed.
169*333d2b36SAndroid Build Coastguard Worker// However this is not enforced yet.
170*333d2b36SAndroid Build Coastguard Worker//
171*333d2b36SAndroid Build Coastguard Worker// Returns true if okay and reports errors via ModuleErrorf.
172*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) validateTestSuites(ctx android.ModuleContext) bool {
173*333d2b36SAndroid Build Coastguard Worker	if len(m.tradefedProperties.Test_suites) == 0 {
174*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("At least one test-suite must be set or this won't run. Use \"general-tests\" or \"device-tests\"")
175*333d2b36SAndroid Build Coastguard Worker		return false
176*333d2b36SAndroid Build Coastguard Worker	}
177*333d2b36SAndroid Build Coastguard Worker
178*333d2b36SAndroid Build Coastguard Worker	var extra_derived_suites []string
179*333d2b36SAndroid Build Coastguard Worker	// Ensure all suites listed are also in base.
180*333d2b36SAndroid Build Coastguard Worker	for _, s := range m.tradefedProperties.Test_suites {
181*333d2b36SAndroid Build Coastguard Worker		if !slices.Contains(m.provider.TestSuites, s) {
182*333d2b36SAndroid Build Coastguard Worker			extra_derived_suites = append(extra_derived_suites, s)
183*333d2b36SAndroid Build Coastguard Worker		}
184*333d2b36SAndroid Build Coastguard Worker	}
185*333d2b36SAndroid Build Coastguard Worker	if len(extra_derived_suites) != 0 {
186*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("Suites: [%s] listed but do not exist in base module: %s",
187*333d2b36SAndroid Build Coastguard Worker			strings.Join(extra_derived_suites, ", "),
188*333d2b36SAndroid Build Coastguard Worker			*m.tradefedProperties.Base)
189*333d2b36SAndroid Build Coastguard Worker		return false
190*333d2b36SAndroid Build Coastguard Worker	}
191*333d2b36SAndroid Build Coastguard Worker
192*333d2b36SAndroid Build Coastguard Worker	return true
193*333d2b36SAndroid Build Coastguard Worker}
194*333d2b36SAndroid Build Coastguard Worker
195*333d2b36SAndroid Build Coastguard Workerfunc TestModuleConfigFactory() android.Module {
196*333d2b36SAndroid Build Coastguard Worker	module := &testModuleConfigModule{}
197*333d2b36SAndroid Build Coastguard Worker
198*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&module.tradefedProperties)
199*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
200*333d2b36SAndroid Build Coastguard Worker	android.InitDefaultableModule(module)
201*333d2b36SAndroid Build Coastguard Worker
202*333d2b36SAndroid Build Coastguard Worker	return module
203*333d2b36SAndroid Build Coastguard Worker}
204*333d2b36SAndroid Build Coastguard Worker
205*333d2b36SAndroid Build Coastguard Workerfunc TestModuleConfigHostFactory() android.Module {
206*333d2b36SAndroid Build Coastguard Worker	module := &testModuleConfigHostModule{}
207*333d2b36SAndroid Build Coastguard Worker
208*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&module.tradefedProperties)
209*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon)
210*333d2b36SAndroid Build Coastguard Worker	android.InitDefaultableModule(module)
211*333d2b36SAndroid Build Coastguard Worker	module.isHost = true
212*333d2b36SAndroid Build Coastguard Worker
213*333d2b36SAndroid Build Coastguard Worker	return module
214*333d2b36SAndroid Build Coastguard Worker}
215*333d2b36SAndroid Build Coastguard Worker
216*333d2b36SAndroid Build Coastguard Worker// Implements android.AndroidMkEntriesProvider
217*333d2b36SAndroid Build Coastguard Workervar _ android.AndroidMkEntriesProvider = (*testModuleConfigModule)(nil)
218*333d2b36SAndroid Build Coastguard Worker
219*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) nativeExtraEntries(entries *android.AndroidMkEntries) {
220*333d2b36SAndroid Build Coastguard Worker	// TODO(ron) provider for suffix and STEM?
221*333d2b36SAndroid Build Coastguard Worker	entries.SetString("LOCAL_MODULE_SUFFIX", "")
222*333d2b36SAndroid Build Coastguard Worker	// Should the stem and path use the base name or our module name?
223*333d2b36SAndroid Build Coastguard Worker	entries.SetString("LOCAL_MODULE_STEM", m.provider.OutputFile.Rel())
224*333d2b36SAndroid Build Coastguard Worker	entries.SetPath("LOCAL_MODULE_PATH", m.provider.InstallDir)
225*333d2b36SAndroid Build Coastguard Worker}
226*333d2b36SAndroid Build Coastguard Worker
227*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) javaExtraEntries(entries *android.AndroidMkEntries) {
228*333d2b36SAndroid Build Coastguard Worker	// The app_prebuilt_internal.mk files try create a copy of the OutputFile as an .apk.
229*333d2b36SAndroid Build Coastguard Worker	// Normally, this copies the "package.apk" from the intermediate directory here.
230*333d2b36SAndroid Build Coastguard Worker	// To prevent the copy of the large apk and to prevent confusion with the real .apk we
231*333d2b36SAndroid Build Coastguard Worker	// link to, we set the STEM here to a bogus name and we set OutputFile to a small file (our manifest).
232*333d2b36SAndroid Build Coastguard Worker	// We do this so we don't have to add more conditionals to base_rules.mk
233*333d2b36SAndroid Build Coastguard Worker	// soong_java_prebult has the same issue for .jars so use this in both module types.
234*333d2b36SAndroid Build Coastguard Worker	entries.SetString("LOCAL_MODULE_STEM", fmt.Sprintf("UNUSED-%s", *m.Base))
235*333d2b36SAndroid Build Coastguard Worker	entries.SetString("LOCAL_MODULE_TAGS", "tests")
236*333d2b36SAndroid Build Coastguard Worker}
237*333d2b36SAndroid Build Coastguard Worker
238*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) AndroidMkEntries() []android.AndroidMkEntries {
239*333d2b36SAndroid Build Coastguard Worker	appClass := m.provider.MkAppClass
240*333d2b36SAndroid Build Coastguard Worker	include := m.provider.MkInclude
241*333d2b36SAndroid Build Coastguard Worker	return []android.AndroidMkEntries{{
242*333d2b36SAndroid Build Coastguard Worker		Class:      appClass,
243*333d2b36SAndroid Build Coastguard Worker		OutputFile: android.OptionalPathForPath(m.manifest),
244*333d2b36SAndroid Build Coastguard Worker		Include:    include,
245*333d2b36SAndroid Build Coastguard Worker		Required:   []string{*m.Base},
246*333d2b36SAndroid Build Coastguard Worker		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
247*333d2b36SAndroid Build Coastguard Worker			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
248*333d2b36SAndroid Build Coastguard Worker				entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
249*333d2b36SAndroid Build Coastguard Worker				entries.SetString("LOCAL_TEST_MODULE_CONFIG_BASE", *m.Base)
250*333d2b36SAndroid Build Coastguard Worker				if m.provider.LocalSdkVersion != "" {
251*333d2b36SAndroid Build Coastguard Worker					entries.SetString("LOCAL_SDK_VERSION", m.provider.LocalSdkVersion)
252*333d2b36SAndroid Build Coastguard Worker				}
253*333d2b36SAndroid Build Coastguard Worker				if m.provider.LocalCertificate != "" {
254*333d2b36SAndroid Build Coastguard Worker					entries.SetString("LOCAL_CERTIFICATE", m.provider.LocalCertificate)
255*333d2b36SAndroid Build Coastguard Worker				}
256*333d2b36SAndroid Build Coastguard Worker
257*333d2b36SAndroid Build Coastguard Worker				entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", m.provider.IsUnitTest)
258*333d2b36SAndroid Build Coastguard Worker				entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
259*333d2b36SAndroid Build Coastguard Worker				entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", m.provider.HostRequiredModuleNames...)
260*333d2b36SAndroid Build Coastguard Worker
261*333d2b36SAndroid Build Coastguard Worker				if m.provider.MkAppClass == "NATIVE_TESTS" {
262*333d2b36SAndroid Build Coastguard Worker					m.nativeExtraEntries(entries)
263*333d2b36SAndroid Build Coastguard Worker				} else {
264*333d2b36SAndroid Build Coastguard Worker					m.javaExtraEntries(entries)
265*333d2b36SAndroid Build Coastguard Worker				}
266*333d2b36SAndroid Build Coastguard Worker
267*333d2b36SAndroid Build Coastguard Worker				// In normal java/app modules, the module writes LOCAL_COMPATIBILITY_SUPPORT_FILES
268*333d2b36SAndroid Build Coastguard Worker				// and then base_rules.mk ends up copying each of those dependencies from .intermediates to the install directory.
269*333d2b36SAndroid Build Coastguard Worker				// tasks/general-tests.mk, tasks/devices-tests.mk also use these to figure out
270*333d2b36SAndroid Build Coastguard Worker				// which testcase files to put in a zip for running tests on another machine.
271*333d2b36SAndroid Build Coastguard Worker				//
272*333d2b36SAndroid Build Coastguard Worker				// We need our files to end up in the zip, but we don't want \.mk files to
273*333d2b36SAndroid Build Coastguard Worker				// `install` files for us.
274*333d2b36SAndroid Build Coastguard Worker				// So we create a new make variable to indicate these should be in the zip
275*333d2b36SAndroid Build Coastguard Worker				// but not installed.
276*333d2b36SAndroid Build Coastguard Worker				entries.AddStrings("LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES", m.supportFiles.Strings()...)
277*333d2b36SAndroid Build Coastguard Worker			},
278*333d2b36SAndroid Build Coastguard Worker		},
279*333d2b36SAndroid Build Coastguard Worker		// Ensure each of our supportFiles depends on the installed file in base so that our symlinks will always
280*333d2b36SAndroid Build Coastguard Worker		// resolve.  The provider gives us the .intermediate path for the support file in base, we change it to
281*333d2b36SAndroid Build Coastguard Worker		// the installed path with a string substitution.
282*333d2b36SAndroid Build Coastguard Worker		ExtraFooters: []android.AndroidMkExtraFootersFunc{
283*333d2b36SAndroid Build Coastguard Worker			func(w io.Writer, name, prefix, moduleDir string) {
284*333d2b36SAndroid Build Coastguard Worker				for _, f := range m.supportFiles.Strings() {
285*333d2b36SAndroid Build Coastguard Worker					// convert out/.../testcases/FrameworksServicesTests_contentprotection/file1.apk
286*333d2b36SAndroid Build Coastguard Worker					// to      out/.../testcases/FrameworksServicesTests/file1.apk
287*333d2b36SAndroid Build Coastguard Worker					basePath := strings.Replace(f, "/"+m.Name()+"/", "/"+*m.Base+"/", 1)
288*333d2b36SAndroid Build Coastguard Worker					fmt.Fprintf(w, "%s: %s\n", f, basePath)
289*333d2b36SAndroid Build Coastguard Worker				}
290*333d2b36SAndroid Build Coastguard Worker			},
291*333d2b36SAndroid Build Coastguard Worker		},
292*333d2b36SAndroid Build Coastguard Worker	}}
293*333d2b36SAndroid Build Coastguard Worker}
294*333d2b36SAndroid Build Coastguard Worker
295*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigHostModule) DepsMutator(ctx android.BottomUpMutatorContext) {
296*333d2b36SAndroid Build Coastguard Worker	if m.Base == nil {
297*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("'base' field must be set to a 'java_test_host' module")
298*333d2b36SAndroid Build Coastguard Worker		return
299*333d2b36SAndroid Build Coastguard Worker	}
300*333d2b36SAndroid Build Coastguard Worker	ctx.AddVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), testModuleConfigHostTag, *m.Base)
301*333d2b36SAndroid Build Coastguard Worker}
302*333d2b36SAndroid Build Coastguard Worker
303*333d2b36SAndroid Build Coastguard Worker// File to write:
304*333d2b36SAndroid Build Coastguard Worker// 1) out/host/linux-x86/testcases/derived-module/test_module_config.manifest # contains base's name.
305*333d2b36SAndroid Build Coastguard Worker// 2) out/host/linux-x86/testcases/derived-module/derived-module.config  # Update AnroidTest.xml
306*333d2b36SAndroid Build Coastguard Worker// 3) out/host/linux-x86/testcases/derived-module/base.jar
307*333d2b36SAndroid Build Coastguard Worker//   - written via soong_java_prebuilt.mk
308*333d2b36SAndroid Build Coastguard Worker//
309*333d2b36SAndroid Build Coastguard Worker// 4) out/host/linux-x86/testcases/derived-module/* # data dependencies from base.
310*333d2b36SAndroid Build Coastguard Worker//   - written via our InstallSymlink
311*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigHostModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
312*333d2b36SAndroid Build Coastguard Worker	m.validateBase(ctx, &testModuleConfigHostTag, "java_test_host", true)
313*333d2b36SAndroid Build Coastguard Worker	m.generateManifestAndConfig(ctx)
314*333d2b36SAndroid Build Coastguard Worker}
315*333d2b36SAndroid Build Coastguard Worker
316*333d2b36SAndroid Build Coastguard Worker// Ensure the base listed is the right type by checking that we get the expected provider data.
317*333d2b36SAndroid Build Coastguard Worker// Returns false on errors and the context is updated with an error indicating the baseType expected.
318*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) validateBase(ctx android.ModuleContext, depTag *dependencyTag, baseType string, baseShouldBeHost bool) {
319*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDepsWithTag(*depTag, func(dep android.Module) {
320*333d2b36SAndroid Build Coastguard Worker		if provider, ok := android.OtherModuleProvider(ctx, dep, tradefed.BaseTestProviderKey); ok {
321*333d2b36SAndroid Build Coastguard Worker			if baseShouldBeHost == provider.IsHost {
322*333d2b36SAndroid Build Coastguard Worker				m.provider = provider
323*333d2b36SAndroid Build Coastguard Worker			} else {
324*333d2b36SAndroid Build Coastguard Worker				if baseShouldBeHost {
325*333d2b36SAndroid Build Coastguard Worker					ctx.ModuleErrorf("'android_test' module used as base, but 'java_test_host' expected.")
326*333d2b36SAndroid Build Coastguard Worker				} else {
327*333d2b36SAndroid Build Coastguard Worker					ctx.ModuleErrorf("'java_test_host' module used as base, but 'android_test' expected.")
328*333d2b36SAndroid Build Coastguard Worker				}
329*333d2b36SAndroid Build Coastguard Worker			}
330*333d2b36SAndroid Build Coastguard Worker		} else {
331*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("'%s' module used as base but it is not a '%s' module.", *m.Base, baseType)
332*333d2b36SAndroid Build Coastguard Worker		}
333*333d2b36SAndroid Build Coastguard Worker	})
334*333d2b36SAndroid Build Coastguard Worker}
335*333d2b36SAndroid Build Coastguard Worker
336*333d2b36SAndroid Build Coastguard Worker// Actions to write:
337*333d2b36SAndroid Build Coastguard Worker//  1. manifest file to testcases dir
338*333d2b36SAndroid Build Coastguard Worker//  2. Symlink to base.apk under base's arch dir
339*333d2b36SAndroid Build Coastguard Worker//  3. Symlink to all data dependencies
340*333d2b36SAndroid Build Coastguard Worker//  4. New Module.config / AndroidTest.xml file with our options.
341*333d2b36SAndroid Build Coastguard Workerfunc (m *testModuleConfigModule) generateManifestAndConfig(ctx android.ModuleContext) {
342*333d2b36SAndroid Build Coastguard Worker	// Keep before early returns.
343*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
344*333d2b36SAndroid Build Coastguard Worker		TestOnly:       true,
345*333d2b36SAndroid Build Coastguard Worker		TopLevelTarget: true,
346*333d2b36SAndroid Build Coastguard Worker	})
347*333d2b36SAndroid Build Coastguard Worker
348*333d2b36SAndroid Build Coastguard Worker	if !m.validateTestSuites(ctx) {
349*333d2b36SAndroid Build Coastguard Worker		return
350*333d2b36SAndroid Build Coastguard Worker	}
351*333d2b36SAndroid Build Coastguard Worker	// Ensure the base provider is accurate
352*333d2b36SAndroid Build Coastguard Worker	if m.provider.TestConfig == nil {
353*333d2b36SAndroid Build Coastguard Worker		return
354*333d2b36SAndroid Build Coastguard Worker	}
355*333d2b36SAndroid Build Coastguard Worker	// 1) A manifest file listing the base, write text to a tiny file.
356*333d2b36SAndroid Build Coastguard Worker	installDir := android.PathForModuleInstall(ctx, ctx.ModuleName())
357*333d2b36SAndroid Build Coastguard Worker	manifest := android.PathForModuleOut(ctx, "test_module_config.manifest")
358*333d2b36SAndroid Build Coastguard Worker	android.WriteFileRule(ctx, manifest, fmt.Sprintf("{%q: %q}", "base", *m.tradefedProperties.Base))
359*333d2b36SAndroid Build Coastguard Worker	// build/soong/android/androidmk.go has this comment:
360*333d2b36SAndroid Build Coastguard Worker	//    Assume the primary install file is last
361*333d2b36SAndroid Build Coastguard Worker	// so we need to Install our file last.
362*333d2b36SAndroid Build Coastguard Worker	ctx.InstallFile(installDir, manifest.Base(), manifest)
363*333d2b36SAndroid Build Coastguard Worker	m.manifest = manifest.OutputPath
364*333d2b36SAndroid Build Coastguard Worker
365*333d2b36SAndroid Build Coastguard Worker	// 2) Symlink to base.apk
366*333d2b36SAndroid Build Coastguard Worker	baseApk := m.provider.OutputFile
367*333d2b36SAndroid Build Coastguard Worker
368*333d2b36SAndroid Build Coastguard Worker	// Typically looks like this for baseApk
369*333d2b36SAndroid Build Coastguard Worker	// FrameworksServicesTests
370*333d2b36SAndroid Build Coastguard Worker	// └── x86_64
371*333d2b36SAndroid Build Coastguard Worker	//    └── FrameworksServicesTests.apk
372*333d2b36SAndroid Build Coastguard Worker	if m.provider.MkAppClass != "NATIVE_TESTS" {
373*333d2b36SAndroid Build Coastguard Worker		symlinkName := fmt.Sprintf("%s/%s", ctx.DeviceConfig().DeviceArch(), baseApk.Base())
374*333d2b36SAndroid Build Coastguard Worker		// Only android_test, not java_host_test puts the output in the DeviceArch dir.
375*333d2b36SAndroid Build Coastguard Worker		if m.provider.IsHost || ctx.DeviceConfig().DeviceArch() == "" {
376*333d2b36SAndroid Build Coastguard Worker			// testcases/CtsDevicePolicyManagerTestCases
377*333d2b36SAndroid Build Coastguard Worker			// ├── CtsDevicePolicyManagerTestCases.jar
378*333d2b36SAndroid Build Coastguard Worker			symlinkName = baseApk.Base()
379*333d2b36SAndroid Build Coastguard Worker		}
380*333d2b36SAndroid Build Coastguard Worker
381*333d2b36SAndroid Build Coastguard Worker		target := installedBaseRelativeToHere(symlinkName, *m.tradefedProperties.Base)
382*333d2b36SAndroid Build Coastguard Worker		installedApk := ctx.InstallAbsoluteSymlink(installDir, symlinkName, target)
383*333d2b36SAndroid Build Coastguard Worker		m.supportFiles = append(m.supportFiles, installedApk)
384*333d2b36SAndroid Build Coastguard Worker	}
385*333d2b36SAndroid Build Coastguard Worker
386*333d2b36SAndroid Build Coastguard Worker	// 3) Symlink for all data deps
387*333d2b36SAndroid Build Coastguard Worker	// And like this for data files and required modules
388*333d2b36SAndroid Build Coastguard Worker	// FrameworksServicesTests
389*333d2b36SAndroid Build Coastguard Worker	// ├── data
390*333d2b36SAndroid Build Coastguard Worker	// │   └── broken_shortcut.xml
391*333d2b36SAndroid Build Coastguard Worker	// ├── JobTestApp.apk
392*333d2b36SAndroid Build Coastguard Worker	for _, symlinkName := range m.provider.TestcaseRelDataFiles {
393*333d2b36SAndroid Build Coastguard Worker		target := installedBaseRelativeToHere(symlinkName, *m.tradefedProperties.Base)
394*333d2b36SAndroid Build Coastguard Worker		installedPath := ctx.InstallAbsoluteSymlink(installDir, symlinkName, target)
395*333d2b36SAndroid Build Coastguard Worker		m.supportFiles = append(m.supportFiles, installedPath)
396*333d2b36SAndroid Build Coastguard Worker	}
397*333d2b36SAndroid Build Coastguard Worker
398*333d2b36SAndroid Build Coastguard Worker	// 4) Module.config / AndroidTest.xml
399*333d2b36SAndroid Build Coastguard Worker	m.testConfig = m.fixTestConfig(ctx, m.provider.TestConfig)
400*333d2b36SAndroid Build Coastguard Worker
401*333d2b36SAndroid Build Coastguard Worker	// 5) We provide so we can be listed in test_suites.
402*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, tradefed.BaseTestProviderKey, tradefed.BaseTestProviderData{
403*333d2b36SAndroid Build Coastguard Worker		TestcaseRelDataFiles:    testcaseRel(m.supportFiles.Paths()),
404*333d2b36SAndroid Build Coastguard Worker		OutputFile:              baseApk,
405*333d2b36SAndroid Build Coastguard Worker		TestConfig:              m.testConfig,
406*333d2b36SAndroid Build Coastguard Worker		HostRequiredModuleNames: m.provider.HostRequiredModuleNames,
407*333d2b36SAndroid Build Coastguard Worker		RequiredModuleNames:     m.provider.RequiredModuleNames,
408*333d2b36SAndroid Build Coastguard Worker		TestSuites:              m.tradefedProperties.Test_suites,
409*333d2b36SAndroid Build Coastguard Worker		IsHost:                  m.provider.IsHost,
410*333d2b36SAndroid Build Coastguard Worker		LocalCertificate:        m.provider.LocalCertificate,
411*333d2b36SAndroid Build Coastguard Worker		IsUnitTest:              m.provider.IsUnitTest,
412*333d2b36SAndroid Build Coastguard Worker	})
413*333d2b36SAndroid Build Coastguard Worker}
414*333d2b36SAndroid Build Coastguard Worker
415*333d2b36SAndroid Build Coastguard Workervar _ android.AndroidMkEntriesProvider = (*testModuleConfigHostModule)(nil)
416*333d2b36SAndroid Build Coastguard Worker
417*333d2b36SAndroid Build Coastguard Workerfunc testcaseRel(paths android.Paths) []string {
418*333d2b36SAndroid Build Coastguard Worker	relPaths := []string{}
419*333d2b36SAndroid Build Coastguard Worker	for _, p := range paths {
420*333d2b36SAndroid Build Coastguard Worker		relPaths = append(relPaths, p.Rel())
421*333d2b36SAndroid Build Coastguard Worker	}
422*333d2b36SAndroid Build Coastguard Worker	return relPaths
423*333d2b36SAndroid Build Coastguard Worker}
424*333d2b36SAndroid Build Coastguard Worker
425*333d2b36SAndroid Build Coastguard Worker// Given a relative path to a file in the current directory or a subdirectory,
426*333d2b36SAndroid Build Coastguard Worker// return a relative path under our sibling directory named `base`.
427*333d2b36SAndroid Build Coastguard Worker// There should be one "../" for each subdir we descend plus one to backup to "base".
428*333d2b36SAndroid Build Coastguard Worker//
429*333d2b36SAndroid Build Coastguard Worker//	 ThisDir/file1
430*333d2b36SAndroid Build Coastguard Worker//	 ThisDir/subdir/file2
431*333d2b36SAndroid Build Coastguard Worker//	would return "../base/file1" or "../../subdir/file2"
432*333d2b36SAndroid Build Coastguard Workerfunc installedBaseRelativeToHere(targetFileName string, base string) string {
433*333d2b36SAndroid Build Coastguard Worker	backup := strings.Repeat("../", strings.Count(targetFileName, "/")+1)
434*333d2b36SAndroid Build Coastguard Worker	return fmt.Sprintf("%s%s/%s", backup, base, targetFileName)
435*333d2b36SAndroid Build Coastguard Worker}
436