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(*GlobalConfig) {}),
89)
90
91// FixtureModifyGlobalConfig enables dexpreopt (unless modified by the mutator) and modifies the
92// configuration.
93func FixtureModifyGlobalConfig(configModifier func(dexpreoptConfig *GlobalConfig)) android.FixturePreparer {
94	return android.FixtureModifyConfig(func(config android.Config) {
95		// Initialize the dexpreopt GlobalConfig to an empty structure. This has no effect if it has
96		// already been set.
97		pathCtx := android.PathContextForTesting(config)
98		dexpreoptConfig := GlobalConfigForTests(pathCtx)
99		SetTestGlobalConfig(config, dexpreoptConfig)
100
101		// Retrieve the existing configuration and modify it.
102		dexpreoptConfig = GetGlobalConfig(pathCtx)
103		configModifier(dexpreoptConfig)
104	})
105}
106
107// FixtureSetArtBootJars enables dexpreopt and sets the ArtApexJars property.
108func FixtureSetArtBootJars(bootJars ...string) android.FixturePreparer {
109	return FixtureModifyGlobalConfig(func(dexpreoptConfig *GlobalConfig) {
110		dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
111	})
112}
113
114// FixtureSetBootJars enables dexpreopt and sets the BootJars property.
115func FixtureSetBootJars(bootJars ...string) android.FixturePreparer {
116	return FixtureModifyGlobalConfig(func(dexpreoptConfig *GlobalConfig) {
117		dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
118	})
119}
120
121// FixtureSetUpdatableBootJars sets the UpdatableBootJars property in the global config.
122func FixtureSetUpdatableBootJars(bootJars ...string) android.FixturePreparer {
123	return FixtureModifyGlobalConfig(func(dexpreoptConfig *GlobalConfig) {
124		dexpreoptConfig.UpdatableBootJars = android.CreateTestConfiguredJarList(bootJars)
125	})
126}
127
128// FixtureSetPreoptWithUpdatableBcp sets the PreoptWithUpdatableBcp property in the global config.
129func FixtureSetPreoptWithUpdatableBcp(value bool) android.FixturePreparer {
130	return FixtureModifyGlobalConfig(func(dexpreoptConfig *GlobalConfig) {
131		dexpreoptConfig.PreoptWithUpdatableBcp = value
132	})
133}
134