| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 java | 
 | 16 |  | 
 | 17 | import ( | 
| Nicolas Geoffray | 47cbfcd | 2020-02-17 14:55:06 +0000 | [diff] [blame] | 18 | 	"fmt" | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 19 | 	"path/filepath" | 
 | 20 | 	"strings" | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 21 |  | 
 | 22 | 	"android/soong/android" | 
 | 23 | 	"android/soong/dexpreopt" | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 24 | ) | 
 | 25 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 26 | // systemServerClasspath returns the on-device locations of the modules in the system server classpath.  It is computed | 
 | 27 | // once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same | 
 | 28 | // ctx.Config(). | 
| Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 29 | func systemServerClasspath(ctx android.MakeVarsContext) []string { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 30 | 	return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 31 | 		global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 32 | 		var systemServerClasspathLocations []string | 
| Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 33 | 		nonUpdatable := dexpreopt.NonUpdatableSystemServerJars(ctx, global) | 
 | 34 | 		// 1) Non-updatable jars. | 
 | 35 | 		for _, m := range nonUpdatable { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 36 | 			systemServerClasspathLocations = append(systemServerClasspathLocations, | 
 | 37 | 				filepath.Join("/system/framework", m+".jar")) | 
 | 38 | 		} | 
| Nicolas Geoffray | 47cbfcd | 2020-02-17 14:55:06 +0000 | [diff] [blame] | 39 | 		// 2) The jars that are from an updatable apex. | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 40 | 		systemServerClasspathLocations = append(systemServerClasspathLocations, | 
 | 41 | 			global.UpdatableSystemServerJars.DevicePaths(ctx.Config(), android.Android)...) | 
 | 42 | 		if len(systemServerClasspathLocations) != len(global.SystemServerJars)+global.UpdatableSystemServerJars.Len() { | 
| Nicolas Geoffray | 47cbfcd | 2020-02-17 14:55:06 +0000 | [diff] [blame] | 43 | 			panic(fmt.Errorf("Wrong number of system server jars, got %d, expected %d", | 
 | 44 | 				len(systemServerClasspathLocations), | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 45 | 				len(global.SystemServerJars)+global.UpdatableSystemServerJars.Len())) | 
| Nicolas Geoffray | 47cbfcd | 2020-02-17 14:55:06 +0000 | [diff] [blame] | 46 | 		} | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 47 | 		return systemServerClasspathLocations | 
 | 48 | 	}) | 
 | 49 | } | 
 | 50 |  | 
 | 51 | var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath") | 
 | 52 |  | 
| Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 53 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures | 
 | 54 | // supported through native bridge. | 
 | 55 | func dexpreoptTargets(ctx android.PathContext) []android.Target { | 
 | 56 | 	var targets []android.Target | 
| Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 57 | 	for _, target := range ctx.Config().Targets[android.Android] { | 
| Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 58 | 		if target.NativeBridge == android.NativeBridgeDisabled { | 
 | 59 | 			targets = append(targets, target) | 
 | 60 | 		} | 
 | 61 | 	} | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 62 | 	// We may also need the images on host in order to run host-based tests. | 
 | 63 | 	for _, target := range ctx.Config().Targets[android.BuildOs] { | 
 | 64 | 		targets = append(targets, target) | 
 | 65 | 	} | 
| Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 66 |  | 
 | 67 | 	return targets | 
 | 68 | } | 
 | 69 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 70 | var ( | 
| Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 71 | 	bootImageConfigKey     = android.NewOnceKey("bootImageConfig") | 
 | 72 | 	artBootImageName       = "art" | 
 | 73 | 	frameworkBootImageName = "boot" | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 74 | ) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 75 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 76 | // Construct the global boot image configs. | 
 | 77 | func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig { | 
 | 78 | 	return ctx.Config().Once(bootImageConfigKey, func() interface{} { | 
 | 79 |  | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 80 | 		global := dexpreopt.GetGlobalConfig(ctx) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 81 | 		targets := dexpreoptTargets(ctx) | 
 | 82 | 		deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName()) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 83 |  | 
| Paul Duffin | 7d584e9 | 2020-10-23 18:26:03 +0100 | [diff] [blame] | 84 | 		artModules := global.ArtApexJars | 
| Paul Duffin | 7d584e9 | 2020-10-23 18:26:03 +0100 | [diff] [blame] | 85 | 		frameworkModules := global.BootJars.RemoveList(artModules) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 86 |  | 
| Martin Stjernholm | ea581fc | 2020-10-14 23:29:49 +0100 | [diff] [blame] | 87 | 		artSubdir := "apex/art_boot_images/javalib" | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 88 | 		frameworkSubdir := "system/framework" | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 89 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 90 | 		// ART config for the primary boot image in the ART apex. | 
 | 91 | 		// It includes the Core Libraries. | 
 | 92 | 		artCfg := bootImageConfig{ | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 93 | 			name:          artBootImageName, | 
 | 94 | 			stem:          "boot", | 
 | 95 | 			installSubdir: artSubdir, | 
 | 96 | 			modules:       artModules, | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 97 | 		} | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 98 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 99 | 		// Framework config for the boot image extension. | 
| Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 100 | 		// It includes framework libraries and depends on the ART config. | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 101 | 		frameworkCfg := bootImageConfig{ | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 102 | 			extends:       &artCfg, | 
 | 103 | 			name:          frameworkBootImageName, | 
 | 104 | 			stem:          "boot", | 
 | 105 | 			installSubdir: frameworkSubdir, | 
 | 106 | 			modules:       frameworkModules, | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 107 | 		} | 
 | 108 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 109 | 		configs := map[string]*bootImageConfig{ | 
| Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 110 | 			artBootImageName:       &artCfg, | 
 | 111 | 			frameworkBootImageName: &frameworkCfg, | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 112 | 		} | 
 | 113 |  | 
 | 114 | 		// common to all configs | 
 | 115 | 		for _, c := range configs { | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 116 | 			c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars") | 
 | 117 | 			c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped") | 
 | 118 |  | 
 | 119 | 			// expands to <stem>.art for primary image and <stem>-<1st module>.art for extension | 
| Ulya Trafimovich | 8640ab9 | 2020-05-11 18:06:15 +0100 | [diff] [blame] | 120 | 			imageName := c.firstModuleNameOrStem(ctx) + ".art" | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 121 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 122 | 			// The path to bootclasspath dex files needs to be known at module | 
 | 123 | 			// GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled. | 
 | 124 | 			// Set up known paths for them, the singleton rules will copy them there. | 
 | 125 | 			// TODO(b/143682396): use module dependencies instead | 
 | 126 | 			inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input") | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 127 | 			c.dexPaths = c.modules.BuildPaths(ctx, inputDir) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 128 | 			c.dexPathsDeps = c.dexPaths | 
 | 129 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 130 | 			// Create target-specific variants. | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 131 | 			for _, target := range targets { | 
 | 132 | 				arch := target.Arch.ArchType | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 133 | 				imageDir := c.dir.Join(ctx, target.Os.String(), c.installSubdir, arch.String()) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 134 | 				variant := &bootImageVariant{ | 
 | 135 | 					bootImageConfig: c, | 
 | 136 | 					target:          target, | 
 | 137 | 					images:          imageDir.Join(ctx, imageName), | 
 | 138 | 					imagesDeps:      c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"), | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 139 | 					dexLocations:    c.modules.DevicePaths(ctx.Config(), target.Os), | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 140 | 				} | 
 | 141 | 				variant.dexLocationsDeps = variant.dexLocations | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 142 | 				c.variants = append(c.variants, variant) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 143 | 			} | 
| Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 144 |  | 
 | 145 | 			c.zip = c.dir.Join(ctx, c.name+".zip") | 
| Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 146 | 		} | 
 | 147 |  | 
| Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 148 | 		// specific to the framework config | 
 | 149 | 		frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 150 | 		for i := range targets { | 
 | 151 | 			frameworkCfg.variants[i].primaryImages = artCfg.variants[i].images | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 152 | 			frameworkCfg.variants[i].dexLocationsDeps = append(artCfg.variants[i].dexLocations, frameworkCfg.variants[i].dexLocationsDeps...) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 153 | 		} | 
| Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 154 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 155 | 		return configs | 
 | 156 | 	}).(map[string]*bootImageConfig) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 157 | } | 
 | 158 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 159 | func artBootImageConfig(ctx android.PathContext) *bootImageConfig { | 
 | 160 | 	return genBootImageConfigs(ctx)[artBootImageName] | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 161 | } | 
 | 162 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 163 | func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig { | 
 | 164 | 	return genBootImageConfigs(ctx)[frameworkBootImageName] | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 165 | } | 
 | 166 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 167 | func defaultBootclasspath(ctx android.PathContext) []string { | 
 | 168 | 	return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 169 | 		global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 170 | 		image := defaultBootImageConfig(ctx) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 171 |  | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 172 | 		updatableBootclasspath := global.UpdatableBootJars.DevicePaths(ctx.Config(), android.Android) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 173 |  | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 174 | 		bootclasspath := append(copyOf(image.getAnyAndroidVariant().dexLocationsDeps), updatableBootclasspath...) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 175 | 		return bootclasspath | 
 | 176 | 	}) | 
 | 177 | } | 
 | 178 |  | 
 | 179 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") | 
 | 180 |  | 
 | 181 | var copyOf = android.CopyOf | 
 | 182 |  | 
 | 183 | func init() { | 
 | 184 | 	android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) | 
 | 185 | } | 
 | 186 |  | 
 | 187 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { | 
 | 188 | 	ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":")) | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 189 | 	ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).getAnyAndroidVariant().dexLocationsDeps, ":")) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 190 | 	ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":")) | 
| Colin Cross | 9be4152 | 2019-02-20 10:40:13 -0800 | [diff] [blame] | 191 |  | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 192 | 	ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":")) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 193 | } |