| Colin Cross | f24a22a | 2019-01-31 14:12:44 -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 ( | 
| Paul Duffin | d3c1513 | 2021-04-21 22:12:35 +0100 | [diff] [blame] | 18 | 	"strings" | 
 | 19 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 20 | 	"android/soong/android" | 
 | 21 | ) | 
 | 22 |  | 
 | 23 | func init() { | 
| Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 24 | 	RegisterHiddenApiSingletonComponents(android.InitRegistrationContext) | 
 | 25 | } | 
 | 26 |  | 
 | 27 | func RegisterHiddenApiSingletonComponents(ctx android.RegistrationContext) { | 
| LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 28 | 	ctx.RegisterParallelSingletonType("hiddenapi", hiddenAPISingletonFactory) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 29 | } | 
 | 30 |  | 
| Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 31 | var PrepareForTestWithHiddenApiBuildComponents = android.FixtureRegisterWithContext(RegisterHiddenApiSingletonComponents) | 
 | 32 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 33 | type hiddenAPISingletonPathsStruct struct { | 
| Paul Duffin | ff774a0 | 2021-01-29 12:53:15 +0000 | [diff] [blame] | 34 | 	// The path to the CSV file that contains the flags that will be encoded into the dex boot jars. | 
 | 35 | 	// | 
 | 36 | 	// It is created by the generate_hiddenapi_lists.py tool that is passed the stubFlags along with | 
 | 37 | 	// a number of additional files that are used to augment the information in the stubFlags with | 
 | 38 | 	// manually curated data. | 
 | 39 | 	flags android.OutputPath | 
 | 40 |  | 
 | 41 | 	// The path to the CSV index file that contains mappings from Java signature to source location | 
 | 42 | 	// information for all Java elements annotated with the UnsupportedAppUsage annotation in the | 
 | 43 | 	// source of all the boot jars. | 
 | 44 | 	// | 
 | 45 | 	// It is created by the merge_csv tool which merges all the hiddenAPI.indexCSVPath files that have | 
 | 46 | 	// been created by the rest of the build. That includes the index files generated for | 
 | 47 | 	// <x>-hiddenapi modules. | 
 | 48 | 	index android.OutputPath | 
 | 49 |  | 
 | 50 | 	// The path to the CSV metadata file that contains mappings from Java signature to the value of | 
 | 51 | 	// properties specified on UnsupportedAppUsage annotations in the source of all the boot jars. | 
 | 52 | 	// | 
 | 53 | 	// It is created by the merge_csv tool which merges all the hiddenAPI.metadataCSVPath files that | 
 | 54 | 	// have been created by the rest of the build. That includes the metadata files generated for | 
 | 55 | 	// <x>-hiddenapi modules. | 
 | 56 | 	metadata android.OutputPath | 
 | 57 |  | 
 | 58 | 	// The path to the CSV metadata file that contains mappings from Java signature to flags obtained | 
 | 59 | 	// from the public, system and test API stubs. | 
 | 60 | 	// | 
 | 61 | 	// This is created by the hiddenapi tool which is given dex files for the public, system and test | 
 | 62 | 	// API stubs (including product specific stubs) along with dex boot jars, so does not include | 
 | 63 | 	// <x>-hiddenapi modules. For each API surface (i.e. public, system, test) it records which | 
 | 64 | 	// members in the dex boot jars match a member in the dex stub jars for that API surface and then | 
 | 65 | 	// outputs a file containing the signatures of all members in the dex boot jars along with the | 
 | 66 | 	// flags that indicate which API surface it belongs, if any. | 
 | 67 | 	// | 
 | 68 | 	// e.g. a dex member that matches a member in the public dex stubs would have flags | 
 | 69 | 	// "public-api,system-api,test-api" set (as system and test are both supersets of public). A dex | 
 | 70 | 	// member that didn't match a member in any of the dex stubs is still output it just has an empty | 
 | 71 | 	// set of flags. | 
 | 72 | 	// | 
 | 73 | 	// The notion of matching is quite complex, it is not restricted to just exact matching but also | 
 | 74 | 	// follows the Java inheritance rules. e.g. if a method is public then all overriding/implementing | 
 | 75 | 	// methods are also public. If an interface method is public and a class inherits an | 
 | 76 | 	// implementation of that method from a super class then that super class method is also public. | 
 | 77 | 	// That ensures that any method that can be called directly by an App through a public method is | 
 | 78 | 	// visible to that App. | 
 | 79 | 	// | 
 | 80 | 	// Propagating the visibility of members across the inheritance hierarchy at build time will cause | 
 | 81 | 	// problems when modularizing and unbundling as it that propagation can cross module boundaries. | 
 | 82 | 	// e.g. Say that a private framework class implements a public interface and inherits an | 
 | 83 | 	// implementation of one of its methods from a core platform ART class. In that case the ART | 
 | 84 | 	// implementation method needs to be marked as public which requires the build to have access to | 
 | 85 | 	// the framework implementation classes at build time. The work to rectify this is being tracked | 
 | 86 | 	// at http://b/178693149. | 
 | 87 | 	// | 
 | 88 | 	// This file (or at least those items marked as being in the public-api) is used by hiddenapi when | 
 | 89 | 	// creating the metadata and flags for the individual modules in order to perform consistency | 
 | 90 | 	// checks and filter out bridge methods that are part of the public API. The latter relies on the | 
 | 91 | 	// propagation of visibility across the inheritance hierarchy. | 
| Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 92 | 	stubFlags android.OutputPath | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 93 | } | 
 | 94 |  | 
 | 95 | var hiddenAPISingletonPathsKey = android.NewOnceKey("hiddenAPISingletonPathsKey") | 
 | 96 |  | 
 | 97 | // hiddenAPISingletonPaths creates all the paths for singleton files the first time it is called, which may be | 
 | 98 | // from a ModuleContext that needs to reference a file that will be created by a singleton rule that hasn't | 
 | 99 | // yet been created. | 
 | 100 | func hiddenAPISingletonPaths(ctx android.PathContext) hiddenAPISingletonPathsStruct { | 
 | 101 | 	return ctx.Config().Once(hiddenAPISingletonPathsKey, func() interface{} { | 
| Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 102 | 		// Make the paths relative to the out/soong/hiddenapi directory instead of to the out/soong/ | 
 | 103 | 		// directory. This ensures that if they are used as java_resources they do not end up in a | 
 | 104 | 		// hiddenapi directory in the resulting APK. | 
 | 105 | 		hiddenapiDir := android.PathForOutput(ctx, "hiddenapi") | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 106 | 		return hiddenAPISingletonPathsStruct{ | 
| Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 107 | 			flags:     hiddenapiDir.Join(ctx, "hiddenapi-flags.csv"), | 
 | 108 | 			index:     hiddenapiDir.Join(ctx, "hiddenapi-index.csv"), | 
 | 109 | 			metadata:  hiddenapiDir.Join(ctx, "hiddenapi-unsupported.csv"), | 
 | 110 | 			stubFlags: hiddenapiDir.Join(ctx, "hiddenapi-stub-flags.txt"), | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 111 | 		} | 
 | 112 | 	}).(hiddenAPISingletonPathsStruct) | 
 | 113 | } | 
 | 114 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 115 | func hiddenAPISingletonFactory() android.Singleton { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 116 | 	return &hiddenAPISingleton{} | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 117 | } | 
 | 118 |  | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 119 | type hiddenAPISingleton struct { | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 120 | } | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 121 |  | 
 | 122 | // hiddenAPI singleton rules | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 123 | func (h *hiddenAPISingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
| Pratyush | faec4db | 2023-07-20 11:19:04 +0000 | [diff] [blame] | 124 | 	// Don't run any hiddenapi rules if hiddenapi checks are disabled | 
 | 125 | 	if ctx.Config().DisableHiddenApiChecks() { | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 126 | 		return | 
 | 127 | 	} | 
 | 128 |  | 
| Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 129 | 	// If there is a prebuilt hiddenapi dir, generate rules to use the | 
 | 130 | 	// files within. Generally, we build the hiddenapi files from source | 
 | 131 | 	// during the build, ensuring consistency. It's possible, in a split | 
 | 132 | 	// build (framework and vendor) scenario, for the vendor build to use | 
 | 133 | 	// prebuilt hiddenapi files from the framework build. In this scenario, | 
 | 134 | 	// the framework and vendor builds must use the same source to ensure | 
 | 135 | 	// consistency. | 
 | 136 |  | 
 | 137 | 	if ctx.Config().PrebuiltHiddenApiDir(ctx) != "" { | 
| Paul Duffin | b6f53c0 | 2021-05-14 07:52:42 +0100 | [diff] [blame] | 138 | 		prebuiltFlagsRule(ctx) | 
| Paul Duffin | 22c7431 | 2021-04-12 16:39:39 +0100 | [diff] [blame] | 139 | 		prebuiltIndexRule(ctx) | 
| Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 140 | 		return | 
 | 141 | 	} | 
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 142 | } | 
 | 143 |  | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 144 | // Checks to see whether the supplied module variant is in the list of boot jars. | 
 | 145 | // | 
| Paul Duffin | 7ebebfd | 2021-04-27 19:36:57 +0100 | [diff] [blame] | 146 | // TODO(b/179354495): Avoid having to perform this type of check. | 
| Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame] | 147 | func isModuleInConfiguredList(ctx android.BaseModuleContext, module android.Module, configuredBootJars android.ConfiguredJarList) bool { | 
 | 148 | 	name := ctx.OtherModuleName(module) | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 149 |  | 
 | 150 | 	// Strip a prebuilt_ prefix so that this can match a prebuilt module that has not been renamed. | 
 | 151 | 	name = android.RemoveOptionalPrebuiltPrefix(name) | 
 | 152 |  | 
| Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 153 | 	// Strip the ".impl" suffix, so that the implementation library of the java_sdk_library is | 
 | 154 | 	// treated identical to the top level java_sdk_library. | 
 | 155 | 	name = strings.TrimSuffix(name, ".impl") | 
 | 156 |  | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 157 | 	// Ignore any module that is not listed in the boot image configuration. | 
 | 158 | 	index := configuredBootJars.IndexOfJar(name) | 
 | 159 | 	if index == -1 { | 
 | 160 | 		return false | 
 | 161 | 	} | 
 | 162 |  | 
 | 163 | 	// It is an error if the module is not an ApexModule. | 
 | 164 | 	if _, ok := module.(android.ApexModule); !ok { | 
| Paul Duffin | d6894ca | 2021-04-27 15:23:28 +0100 | [diff] [blame] | 165 | 		ctx.ModuleErrorf("%s is configured in boot jars but does not support being added to an apex", ctx.OtherModuleName(module)) | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 166 | 		return false | 
 | 167 | 	} | 
 | 168 |  | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 169 | 	apexInfo, _ := android.OtherModuleProvider(ctx, module, android.ApexInfoProvider) | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 170 |  | 
 | 171 | 	// Now match the apex part of the boot image configuration. | 
 | 172 | 	requiredApex := configuredBootJars.Apex(index) | 
| Jiakai Zhang | b1639db | 2023-07-11 15:03:13 +0100 | [diff] [blame] | 173 | 	if android.IsConfiguredJarForPlatform(requiredApex) { | 
| Jiyong Park | ab50b07 | 2021-05-12 17:13:56 +0900 | [diff] [blame] | 174 | 		if len(apexInfo.InApexVariants) != 0 { | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 175 | 			// A platform variant is required but this is for an apex so ignore it. | 
 | 176 | 			return false | 
 | 177 | 		} | 
| Martin Stjernholm | be10503 | 2021-05-26 16:57:39 +0100 | [diff] [blame] | 178 | 	} else if !apexInfo.InApexVariant(requiredApex) { | 
| Paul Duffin | dd63d6d | 2021-02-03 18:34:00 +0000 | [diff] [blame] | 179 | 		// An apex variant for a specific apex is required but this is the wrong apex. | 
 | 180 | 		return false | 
 | 181 | 	} | 
 | 182 |  | 
 | 183 | 	return true | 
 | 184 | } | 
 | 185 |  | 
| Paul Duffin | b6f53c0 | 2021-05-14 07:52:42 +0100 | [diff] [blame] | 186 | func prebuiltFlagsRule(ctx android.SingletonContext) { | 
| Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 187 | 	outputPath := hiddenAPISingletonPaths(ctx).flags | 
 | 188 | 	inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-flags.csv") | 
 | 189 |  | 
 | 190 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 191 | 		Rule:   android.Cp, | 
 | 192 | 		Output: outputPath, | 
 | 193 | 		Input:  inputPath, | 
 | 194 | 	}) | 
| Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 195 | } | 
 | 196 |  | 
| Paul Duffin | 22c7431 | 2021-04-12 16:39:39 +0100 | [diff] [blame] | 197 | func prebuiltIndexRule(ctx android.SingletonContext) { | 
 | 198 | 	outputPath := hiddenAPISingletonPaths(ctx).index | 
 | 199 | 	inputPath := android.PathForSource(ctx, ctx.Config().PrebuiltHiddenApiDir(ctx), "hiddenapi-index.csv") | 
 | 200 |  | 
 | 201 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 202 | 		Rule:   android.Cp, | 
 | 203 | 		Output: outputPath, | 
 | 204 | 		Input:  inputPath, | 
 | 205 | 	}) | 
 | 206 | } | 
 | 207 |  | 
| Paul Duffin | d3c1513 | 2021-04-21 22:12:35 +0100 | [diff] [blame] | 208 | // tempPathForRestat creates a path of the same type as the supplied type but with a name of | 
 | 209 | // <path>.tmp. | 
 | 210 | // | 
 | 211 | // e.g. If path is an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv then this will return | 
 | 212 | // an OutputPath for out/soong/hiddenapi/hiddenapi-flags.csv.tmp | 
 | 213 | func tempPathForRestat(ctx android.PathContext, path android.WritablePath) android.WritablePath { | 
 | 214 | 	extWithoutLeadingDot := strings.TrimPrefix(path.Ext(), ".") | 
 | 215 | 	return path.ReplaceExtension(ctx, extWithoutLeadingDot+".tmp") | 
 | 216 | } | 
 | 217 |  | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 218 | // commitChangeForRestat adds a command to a rule that updates outputPath from tempPath if they are different.  It | 
 | 219 | // also marks the rule as restat and marks the tempPath as a temporary file that should not be considered an output of | 
 | 220 | // the rule. | 
 | 221 | func commitChangeForRestat(rule *android.RuleBuilder, tempPath, outputPath android.WritablePath) { | 
 | 222 | 	rule.Restat() | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 223 | 	rule.Temporary(tempPath) | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 224 | 	rule.Command(). | 
 | 225 | 		Text("("). | 
 | 226 | 		Text("if"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 227 | 		Text("cmp -s").Input(tempPath).Output(outputPath).Text(";"). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 228 | 		Text("then"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 229 | 		Text("rm").Input(tempPath).Text(";"). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 230 | 		Text("else"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 231 | 		Text("mv").Input(tempPath).Output(outputPath).Text(";"). | 
| Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 232 | 		Text("fi"). | 
 | 233 | 		Text(")") | 
 | 234 | } |