| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | package dexpreopt | 
|  | 16 |  | 
|  | 17 | import ( | 
| Paul Duffin | 9fc9f53 | 2021-03-23 15:41:11 +0000 | [diff] [blame] | 18 | "fmt" | 
|  | 19 |  | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 20 | "android/soong/android" | 
|  | 21 | ) | 
|  | 22 |  | 
| Paul Duffin | f8e3d83 | 2021-03-23 14:47:18 +0000 | [diff] [blame] | 23 | type fakeToolBinary struct { | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 24 | android.ModuleBase | 
|  | 25 | } | 
|  | 26 |  | 
| Paul Duffin | f8e3d83 | 2021-03-23 14:47:18 +0000 | [diff] [blame] | 27 | func (m *fakeToolBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {} | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 28 |  | 
| Paul Duffin | f8e3d83 | 2021-03-23 14:47:18 +0000 | [diff] [blame] | 29 | func (m *fakeToolBinary) HostToolPath() android.OptionalPath { | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 30 | return android.OptionalPathForPath(android.PathForTesting("dex2oat")) | 
|  | 31 | } | 
|  | 32 |  | 
| Paul Duffin | f8e3d83 | 2021-03-23 14:47:18 +0000 | [diff] [blame] | 33 | func fakeToolBinaryFactory() android.Module { | 
|  | 34 | module := &fakeToolBinary{} | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 35 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) | 
|  | 36 | return module | 
|  | 37 | } | 
|  | 38 |  | 
| Paul Duffin | 635aa08 | 2021-01-25 19:11:24 +0000 | [diff] [blame] | 39 | func RegisterToolModulesForTest(ctx android.RegistrationContext) { | 
| Paul Duffin | f8e3d83 | 2021-03-23 14:47:18 +0000 | [diff] [blame] | 40 | ctx.RegisterModuleType("fake_tool_binary", fakeToolBinaryFactory) | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
|  | 43 | func BpToolModulesForTest() string { | 
|  | 44 | return ` | 
| Paul Duffin | f8e3d83 | 2021-03-23 14:47:18 +0000 | [diff] [blame] | 45 | fake_tool_binary { | 
| Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 46 | name: "dex2oatd", | 
|  | 47 | } | 
|  | 48 | ` | 
|  | 49 | } | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 50 |  | 
| Paul Duffin | 9fc9f53 | 2021-03-23 15:41:11 +0000 | [diff] [blame] | 51 | func 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 |  | 
|  | 75 | var PrepareForTestWithDexpreoptCompatLibs = android.GroupFixturePreparers( | 
|  | 76 | android.FixtureAddFile("defaults/dexpreopt/compat/a.java", nil), | 
|  | 77 | android.FixtureAddTextFile("defaults/dexpreopt/compat/Android.bp", CompatLibDefinitionsForTest()), | 
|  | 78 | ) | 
|  | 79 |  | 
|  | 80 | var 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. | 
|  | 87 | var PrepareForTestByEnablingDexpreopt = android.GroupFixturePreparers( | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 88 | FixtureModifyGlobalConfig(func(android.PathContext, *GlobalConfig) {}), | 
| Paul Duffin | 9fc9f53 | 2021-03-23 15:41:11 +0000 | [diff] [blame] | 89 | ) | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 90 |  | 
|  | 91 | // FixtureModifyGlobalConfig enables dexpreopt (unless modified by the mutator) and modifies the | 
|  | 92 | // configuration. | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 93 | func FixtureModifyGlobalConfig(configModifier func(ctx android.PathContext, dexpreoptConfig *GlobalConfig)) android.FixturePreparer { | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 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) | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 103 | configModifier(pathCtx, dexpreoptConfig) | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 104 | }) | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | // FixtureSetArtBootJars enables dexpreopt and sets the ArtApexJars property. | 
|  | 108 | func FixtureSetArtBootJars(bootJars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 109 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 110 | dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars) | 
|  | 111 | }) | 
|  | 112 | } | 
|  | 113 |  | 
| Jiakai Zhang | 556bdf8 | 2023-07-12 16:51:57 +0100 | [diff] [blame] | 114 | // FixtureSetTestOnlyArtBootImageJars enables dexpreopt and sets the TestOnlyArtBootImageJars property. | 
|  | 115 | func FixtureSetTestOnlyArtBootImageJars(bootJars ...string) android.FixturePreparer { | 
|  | 116 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
|  | 117 | dexpreoptConfig.TestOnlyArtBootImageJars = android.CreateTestConfiguredJarList(bootJars) | 
|  | 118 | }) | 
|  | 119 | } | 
|  | 120 |  | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 121 | // FixtureSetBootJars enables dexpreopt and sets the BootJars property. | 
|  | 122 | func FixtureSetBootJars(bootJars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 123 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| Paul Duffin | 4fced55 | 2021-03-09 16:28:16 +0000 | [diff] [blame] | 124 | dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars) | 
|  | 125 | }) | 
|  | 126 | } | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 127 |  | 
| satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 128 | // FixtureSetApexBootJars sets the ApexBootJars property in the global config. | 
|  | 129 | func FixtureSetApexBootJars(bootJars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 130 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 131 | dexpreoptConfig.ApexBootJars = android.CreateTestConfiguredJarList(bootJars) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 132 | }) | 
|  | 133 | } | 
|  | 134 |  | 
| Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 135 | // FixtureSetStandaloneSystemServerJars sets the StandaloneSystemServerJars property. | 
|  | 136 | func FixtureSetStandaloneSystemServerJars(jars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 137 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 138 | dexpreoptConfig.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars) | 
|  | 139 | }) | 
|  | 140 | } | 
|  | 141 |  | 
| satayev | abcd597 | 2021-08-06 17:49:46 +0100 | [diff] [blame] | 142 | // FixtureSetSystemServerJars sets the SystemServerJars property. | 
|  | 143 | func FixtureSetSystemServerJars(jars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 144 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| satayev | abcd597 | 2021-08-06 17:49:46 +0100 | [diff] [blame] | 145 | dexpreoptConfig.SystemServerJars = android.CreateTestConfiguredJarList(jars) | 
|  | 146 | }) | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | // FixtureSetApexSystemServerJars sets the ApexSystemServerJars property in the global config. | 
|  | 150 | func FixtureSetApexSystemServerJars(jars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 151 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| satayev | abcd597 | 2021-08-06 17:49:46 +0100 | [diff] [blame] | 152 | dexpreoptConfig.ApexSystemServerJars = android.CreateTestConfiguredJarList(jars) | 
|  | 153 | }) | 
|  | 154 | } | 
|  | 155 |  | 
| Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 156 | // FixtureSetApexStandaloneSystemServerJars sets the ApexStandaloneSystemServerJars property in the | 
|  | 157 | // global config. | 
|  | 158 | func FixtureSetApexStandaloneSystemServerJars(jars ...string) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 159 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| Jiakai Zhang | cee9e19 | 2021-10-29 19:46:45 +0000 | [diff] [blame] | 160 | dexpreoptConfig.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars) | 
|  | 161 | }) | 
|  | 162 | } | 
|  | 163 |  | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 164 | // FixtureSetPreoptWithUpdatableBcp sets the PreoptWithUpdatableBcp property in the global config. | 
|  | 165 | func FixtureSetPreoptWithUpdatableBcp(value bool) android.FixturePreparer { | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 166 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 167 | dexpreoptConfig.PreoptWithUpdatableBcp = value | 
|  | 168 | }) | 
|  | 169 | } | 
| Jiakai Zhang | 49b1eb6 | 2021-11-26 18:09:27 +0000 | [diff] [blame] | 170 |  | 
|  | 171 | // FixtureSetBootImageProfiles sets the BootImageProfiles property in the global config. | 
|  | 172 | func FixtureSetBootImageProfiles(profiles ...string) android.FixturePreparer { | 
|  | 173 | return FixtureModifyGlobalConfig(func(ctx android.PathContext, dexpreoptConfig *GlobalConfig) { | 
|  | 174 | dexpreoptConfig.BootImageProfiles = android.PathsForSource(ctx, profiles) | 
|  | 175 | }) | 
|  | 176 | } | 
| Jiakai Zhang | 7d3c9e0 | 2021-12-13 17:19:05 +0000 | [diff] [blame] | 177 |  | 
|  | 178 | // FixtureDisableGenerateProfile sets the DisableGenerateProfile property in the global config. | 
|  | 179 | func FixtureDisableGenerateProfile(disable bool) android.FixturePreparer { | 
|  | 180 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
|  | 181 | dexpreoptConfig.DisableGenerateProfile = disable | 
|  | 182 | }) | 
|  | 183 | } | 
| Jiakai Zhang | bc698cd | 2023-05-08 16:28:38 +0000 | [diff] [blame] | 184 |  | 
|  | 185 | // FixtureDisableDexpreoptBootImages sets the DisablePreoptBootImages property in the global config. | 
|  | 186 | func FixtureDisableDexpreoptBootImages(disable bool) android.FixturePreparer { | 
|  | 187 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
|  | 188 | dexpreoptConfig.DisablePreoptBootImages = disable | 
|  | 189 | }) | 
|  | 190 | } | 
| Jiakai Zhang | cf61e3c | 2023-05-08 16:28:38 +0000 | [diff] [blame] | 191 |  | 
|  | 192 | // FixtureDisableDexpreopt sets the DisablePreopt property in the global config. | 
|  | 193 | func FixtureDisableDexpreopt(disable bool) android.FixturePreparer { | 
|  | 194 | return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) { | 
|  | 195 | dexpreoptConfig.DisablePreopt = disable | 
|  | 196 | }) | 
|  | 197 | } |