1// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dexpreopt
16
17import (
18	"fmt"
19
20	"android/soong/android"
21)
22
23type fakeToolBinary struct {
24	android.ModuleBase
25}
26
27func (m *fakeToolBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
28
29func (m *fakeToolBinary) HostToolPath() android.OptionalPath {
30	return android.OptionalPathForPath(android.PathForTesting("dex2oat"))
31}
32
33func fakeToolBinaryFactory() android.Module {
34	module := &fakeToolBinary{}
35	android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
36	return module
37}
38
39func RegisterToolModulesForTest(ctx android.RegistrationContext) {
40	ctx.RegisterModuleType("fake_tool_binary", fakeToolBinaryFactory)
41}
42
43func BpToolModulesForTest() string {
44	return `
45		fake_tool_binary {
46			name: "dex2oatd",
47		}
48	`
49}
50
51func CompatLibDefinitionsForTest() string {
52	bp := ""
53
54	// For class loader context and <uses-library> tests.
55	dexpreoptModules := []string{"android.test.runner"}
56	dexpreoptModules = append(dexpreoptModules, CompatUsesLibs...)
57	dexpreoptModules = append(dexpreoptModules, OptionalCompatUsesLibs...)
58
59	for _, extra := range dexpreoptModules {
60		bp += fmt.Sprintf(`
61			java_library {
62				name: "%s",
63				srcs: ["a.java"],
64				sdk_version: "none",
65				system_modules: "stable-core-platform-api-stubs-system-modules",
66				compile_dex: true,
67				installable: true,
68			}
69		`, extra)
70	}
71
72	return bp
73}
74
75var PrepareForTestWithDexpreoptCompatLibs = android.GroupFixturePreparers(
76	android.FixtureAddFile("defaults/dexpreopt/compat/a.java", nil),
77	android.FixtureAddTextFile("defaults/dexpreopt/compat/Android.bp", CompatLibDefinitionsForTest()),
78)
79
80var PrepareForTestWithFakeDex2oatd = android.GroupFixturePreparers(
81	android.FixtureRegisterWithContext(RegisterToolModulesForTest),
82	android.FixtureAddTextFile("defaults/dexpreopt/Android.bp", BpToolModulesForTest()),
83)
84
85// Prepares a test fixture by enabling dexpreopt, registering the fake_tool_binary module type and
86// using that to define the `dex2oatd` module.
87var PrepareForTestByEnablingDexpreopt = android.GroupFixturePreparers(
88	FixtureModifyGlobalConfig(func(android.PathContext, *GlobalConfig) {}),
89)
90
91var PrepareForTestWithDexpreoptConfig = android.GroupFixturePreparers(
92	android.PrepareForTestWithAndroidBuildComponents,
93	android.FixtureModifyContext(func(ctx *android.TestContext) {
94		ctx.RegisterParallelSingletonType("dexpreopt-soong-config", func() android.Singleton {
95			return &globalSoongConfigSingleton{}
96		})
97	}),
98)
99
100// FixtureModifyGlobalConfig enables dexpreopt (unless modified by the mutator) and modifies the
101// configuration.
102func FixtureModifyGlobalConfig(configModifier func(ctx android.PathContext, dexpreoptConfig *GlobalConfig)) android.FixturePreparer {
103	return android.FixtureModifyConfig(func(config android.Config) {
104		// Initialize the dexpreopt GlobalConfig to an empty structure. This has no effect if it has
105		// already been set.
106		pathCtx := android.PathContextForTesting(config)
107		dexpreoptConfig := GlobalConfigForTests(pathCtx)
108		SetTestGlobalConfig(config, dexpreoptConfig)
109
110		// Retrieve the existing configuration and modify it.
111		dexpreoptConfig = GetGlobalConfig(pathCtx)
112		configModifier(pathCtx, dexpreoptConfig)
113	})
114}
115
116// FixtureSetArtBootJars enables dexpreopt and sets the ArtApexJars property.
117func FixtureSetArtBootJars(bootJars ...string) android.FixturePreparer {
118	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
119		dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
120	})
121}
122
123// FixtureSetTestOnlyArtBootImageJars enables dexpreopt and sets the TestOnlyArtBootImageJars property.
124func FixtureSetTestOnlyArtBootImageJars(bootJars ...string) android.FixturePreparer {
125	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
126		dexpreoptConfig.TestOnlyArtBootImageJars = android.CreateTestConfiguredJarList(bootJars)
127	})
128}
129
130// FixtureSetBootJars enables dexpreopt and sets the BootJars property.
131func FixtureSetBootJars(bootJars ...string) android.FixturePreparer {
132	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
133		dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
134	})
135}
136
137// FixtureSetApexBootJars sets the ApexBootJars property in the global config.
138func FixtureSetApexBootJars(bootJars ...string) android.FixturePreparer {
139	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
140		dexpreoptConfig.ApexBootJars = android.CreateTestConfiguredJarList(bootJars)
141	})
142}
143
144// FixtureSetStandaloneSystemServerJars sets the StandaloneSystemServerJars property.
145func FixtureSetStandaloneSystemServerJars(jars ...string) android.FixturePreparer {
146	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
147		dexpreoptConfig.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars)
148	})
149}
150
151// FixtureSetSystemServerJars sets the SystemServerJars property.
152func FixtureSetSystemServerJars(jars ...string) android.FixturePreparer {
153	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
154		dexpreoptConfig.SystemServerJars = android.CreateTestConfiguredJarList(jars)
155	})
156}
157
158// FixtureSetApexSystemServerJars sets the ApexSystemServerJars property in the global config.
159func FixtureSetApexSystemServerJars(jars ...string) android.FixturePreparer {
160	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
161		dexpreoptConfig.ApexSystemServerJars = android.CreateTestConfiguredJarList(jars)
162	})
163}
164
165// FixtureSetApexStandaloneSystemServerJars sets the ApexStandaloneSystemServerJars property in the
166// global config.
167func FixtureSetApexStandaloneSystemServerJars(jars ...string) android.FixturePreparer {
168	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
169		dexpreoptConfig.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars)
170	})
171}
172
173// FixtureSetPreoptWithUpdatableBcp sets the PreoptWithUpdatableBcp property in the global config.
174func FixtureSetPreoptWithUpdatableBcp(value bool) android.FixturePreparer {
175	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
176		dexpreoptConfig.PreoptWithUpdatableBcp = value
177	})
178}
179
180// FixtureSetBootImageProfiles sets the BootImageProfiles property in the global config.
181func FixtureSetBootImageProfiles(profiles ...string) android.FixturePreparer {
182	return FixtureModifyGlobalConfig(func(ctx android.PathContext, dexpreoptConfig *GlobalConfig) {
183		dexpreoptConfig.BootImageProfiles = android.PathsForSource(ctx, profiles)
184	})
185}
186
187// FixtureDisableGenerateProfile sets the DisableGenerateProfile property in the global config.
188func FixtureDisableGenerateProfile(disable bool) android.FixturePreparer {
189	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
190		dexpreoptConfig.DisableGenerateProfile = disable
191	})
192}
193
194// FixtureDisableDexpreoptBootImages sets the DisablePreoptBootImages property in the global config.
195func FixtureDisableDexpreoptBootImages(disable bool) android.FixturePreparer {
196	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
197		dexpreoptConfig.DisablePreoptBootImages = disable
198	})
199}
200
201// FixtureDisableDexpreopt sets the DisablePreopt property in the global config.
202func FixtureDisableDexpreopt(disable bool) android.FixturePreparer {
203	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
204		dexpreoptConfig.DisablePreopt = disable
205	})
206}
207
208// FixtureSetEnableUffdGc sets the EnableUffdGc property in the global config.
209func FixtureSetEnableUffdGc(value string) android.FixturePreparer {
210	return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
211		dexpreoptConfig.EnableUffdGc = value
212	})
213}
214