blob: 47ae494e33d8702db9aa41aa1aa9ad1c5d254f66 [file] [log] [blame]
Martin Stjernholmd90676f2020-01-11 00:37:30 +00001// 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 (
Paul Duffin9fc9f532021-03-23 15:41:11 +000018 "fmt"
19
Martin Stjernholmd90676f2020-01-11 00:37:30 +000020 "android/soong/android"
21)
22
Paul Duffinf8e3d832021-03-23 14:47:18 +000023type fakeToolBinary struct {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000024 android.ModuleBase
25}
26
Paul Duffinf8e3d832021-03-23 14:47:18 +000027func (m *fakeToolBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
Martin Stjernholmd90676f2020-01-11 00:37:30 +000028
Paul Duffinf8e3d832021-03-23 14:47:18 +000029func (m *fakeToolBinary) HostToolPath() android.OptionalPath {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000030 return android.OptionalPathForPath(android.PathForTesting("dex2oat"))
31}
32
Paul Duffinf8e3d832021-03-23 14:47:18 +000033func fakeToolBinaryFactory() android.Module {
34 module := &fakeToolBinary{}
Martin Stjernholmd90676f2020-01-11 00:37:30 +000035 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
36 return module
37}
38
Paul Duffin635aa082021-01-25 19:11:24 +000039func RegisterToolModulesForTest(ctx android.RegistrationContext) {
Paul Duffinf8e3d832021-03-23 14:47:18 +000040 ctx.RegisterModuleType("fake_tool_binary", fakeToolBinaryFactory)
Martin Stjernholmd90676f2020-01-11 00:37:30 +000041}
42
43func BpToolModulesForTest() string {
44 return `
Paul Duffinf8e3d832021-03-23 14:47:18 +000045 fake_tool_binary {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000046 name: "dex2oatd",
47 }
48 `
49}
Paul Duffin4fced552021-03-09 16:28:16 +000050
Paul Duffin9fc9f532021-03-23 15:41:11 +000051func 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(
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000088 FixtureModifyGlobalConfig(func(android.PathContext, *GlobalConfig) {}),
Paul Duffin9fc9f532021-03-23 15:41:11 +000089)
Paul Duffin4fced552021-03-09 16:28:16 +000090
91// FixtureModifyGlobalConfig enables dexpreopt (unless modified by the mutator) and modifies the
92// configuration.
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000093func FixtureModifyGlobalConfig(configModifier func(ctx android.PathContext, dexpreoptConfig *GlobalConfig)) android.FixturePreparer {
Paul Duffin4fced552021-03-09 16:28:16 +000094 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)
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000103 configModifier(pathCtx, dexpreoptConfig)
Paul Duffin4fced552021-03-09 16:28:16 +0000104 })
105}
106
107// FixtureSetArtBootJars enables dexpreopt and sets the ArtApexJars property.
108func FixtureSetArtBootJars(bootJars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000109 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
Paul Duffin4fced552021-03-09 16:28:16 +0000110 dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
111 })
112}
113
114// FixtureSetBootJars enables dexpreopt and sets the BootJars property.
115func FixtureSetBootJars(bootJars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000116 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
Paul Duffin4fced552021-03-09 16:28:16 +0000117 dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
118 })
119}
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000120
satayevd604b212021-07-21 14:23:52 +0100121// FixtureSetApexBootJars sets the ApexBootJars property in the global config.
122func FixtureSetApexBootJars(bootJars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000123 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
satayevd604b212021-07-21 14:23:52 +0100124 dexpreoptConfig.ApexBootJars = android.CreateTestConfiguredJarList(bootJars)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000125 })
126}
127
Jiakai Zhangcee9e192021-10-29 19:46:45 +0000128// FixtureSetStandaloneSystemServerJars sets the StandaloneSystemServerJars property.
129func FixtureSetStandaloneSystemServerJars(jars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000130 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
Jiakai Zhangcee9e192021-10-29 19:46:45 +0000131 dexpreoptConfig.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars)
132 })
133}
134
satayevabcd5972021-08-06 17:49:46 +0100135// FixtureSetSystemServerJars sets the SystemServerJars property.
136func FixtureSetSystemServerJars(jars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000137 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
satayevabcd5972021-08-06 17:49:46 +0100138 dexpreoptConfig.SystemServerJars = android.CreateTestConfiguredJarList(jars)
139 })
140}
141
142// FixtureSetApexSystemServerJars sets the ApexSystemServerJars property in the global config.
143func FixtureSetApexSystemServerJars(jars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000144 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
satayevabcd5972021-08-06 17:49:46 +0100145 dexpreoptConfig.ApexSystemServerJars = android.CreateTestConfiguredJarList(jars)
146 })
147}
148
Jiakai Zhangcee9e192021-10-29 19:46:45 +0000149// FixtureSetApexStandaloneSystemServerJars sets the ApexStandaloneSystemServerJars property in the
150// global config.
151func FixtureSetApexStandaloneSystemServerJars(jars ...string) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000152 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
Jiakai Zhangcee9e192021-10-29 19:46:45 +0000153 dexpreoptConfig.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars)
154 })
155}
156
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000157// FixtureSetPreoptWithUpdatableBcp sets the PreoptWithUpdatableBcp property in the global config.
158func FixtureSetPreoptWithUpdatableBcp(value bool) android.FixturePreparer {
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000159 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000160 dexpreoptConfig.PreoptWithUpdatableBcp = value
161 })
162}
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000163
164// FixtureSetBootImageProfiles sets the BootImageProfiles property in the global config.
165func FixtureSetBootImageProfiles(profiles ...string) android.FixturePreparer {
166 return FixtureModifyGlobalConfig(func(ctx android.PathContext, dexpreoptConfig *GlobalConfig) {
167 dexpreoptConfig.BootImageProfiles = android.PathsForSource(ctx, profiles)
168 })
169}
Jiakai Zhang7d3c9e02021-12-13 17:19:05 +0000170
171// FixtureDisableGenerateProfile sets the DisableGenerateProfile property in the global config.
172func FixtureDisableGenerateProfile(disable bool) android.FixturePreparer {
173 return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
174 dexpreoptConfig.DisableGenerateProfile = disable
175 })
176}