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