Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -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 ( |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 18 | "github.com/google/blueprint" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{ |
Andrei Onea | 23fea04 | 2020-08-12 16:48:23 +0100 | [diff] [blame] | 24 | Command: "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out", |
| 25 | CommandDeps: []string{"${config.Class2NonSdkList}"}, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 26 | }, "outFlag", "stubAPIFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 27 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 28 | type hiddenAPI struct { |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 29 | // The name of the module as it would be used in the boot jars configuration, e.g. without any |
Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 30 | // prebuilt_ prefix (if it is a prebuilt) and without any ".impl" suffix if it is a |
| 31 | // java_sdk_library implementation library. |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 32 | configurationName string |
| 33 | |
| 34 | // True if the module containing this structure contributes to the hiddenapi information or has |
| 35 | // that information encoded within it. |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 36 | active bool |
| 37 | |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 38 | // Identifies the active module variant which will be used as the source of hiddenapi information. |
| 39 | // |
| 40 | // A class may be compiled into a number of different module variants each of which will need the |
| 41 | // hiddenapi information encoded into it and so will be marked as active. However, only one of |
| 42 | // them must be used as a source of information by hiddenapi otherwise it will end up with |
| 43 | // duplicate entries. That module will have primary=true. |
| 44 | // |
| 45 | // Note, that modules <x>-hiddenapi that provide additional annotation information for module <x> |
| 46 | // that is on the bootclasspath are marked as primary=true as they are the primary source of that |
| 47 | // annotation information. |
| 48 | primary bool |
| 49 | |
Paul Duffin | ff774a0 | 2021-01-29 12:53:15 +0000 | [diff] [blame] | 50 | // The path to the dex jar that is in the boot class path. If this is nil then the associated |
| 51 | // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional |
| 52 | // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar |
| 53 | // themselves. |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 54 | // |
| 55 | // This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on |
| 56 | // this file so using the encoded dex jar here would result in a cycle in the ninja rules. |
Paul Duffin | ff774a0 | 2021-01-29 12:53:15 +0000 | [diff] [blame] | 57 | bootDexJarPath android.Path |
| 58 | |
Paul Duffin | 36187b2 | 2021-04-22 16:43:06 +0100 | [diff] [blame] | 59 | // The paths to the classes jars that contain classes and class members annotated with |
| 60 | // the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API |
| 61 | // processing. |
| 62 | classesJarPaths android.Paths |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 65 | func (h *hiddenAPI) bootDexJar() android.Path { |
| 66 | return h.bootDexJarPath |
| 67 | } |
| 68 | |
Paul Duffin | 36187b2 | 2021-04-22 16:43:06 +0100 | [diff] [blame] | 69 | func (h *hiddenAPI) classesJars() android.Paths { |
| 70 | return h.classesJarPaths |
| 71 | } |
| 72 | |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 73 | // hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement. |
| 74 | type hiddenAPIModule interface { |
| 75 | android.Module |
| 76 | hiddenAPIIntf |
| 77 | } |
| 78 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 79 | type hiddenAPIIntf interface { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 80 | bootDexJar() android.Path |
Paul Duffin | 36187b2 | 2021-04-22 16:43:06 +0100 | [diff] [blame] | 81 | classesJars() android.Paths |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | var _ hiddenAPIIntf = (*hiddenAPI)(nil) |
| 85 | |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 86 | // Initialize the hiddenapi structure |
Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 87 | func (h *hiddenAPI) initHiddenAPI(ctx android.BaseModuleContext, configurationName string) { |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 88 | // If hiddenapi processing is disabled treat this as inactive. |
| 89 | if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") { |
| 90 | return |
| 91 | } |
| 92 | |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 93 | h.configurationName = configurationName |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 94 | |
| 95 | // It is important that hiddenapi information is only gathered for/from modules that are actually |
| 96 | // on the boot jars list because the runtime only enforces access to the hidden API for the |
| 97 | // bootclassloader. If information is gathered for modules not on the list then that will cause |
| 98 | // failures in the CtsHiddenApiBlocklist... tests. |
Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame] | 99 | module := ctx.Module() |
| 100 | h.active = isModuleInBootClassPath(ctx, module) |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 101 | if !h.active { |
| 102 | // The rest of the properties will be ignored if active is false. |
| 103 | return |
| 104 | } |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 105 | |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 106 | // Determine whether this module is the primary module or not. |
| 107 | primary := true |
| 108 | |
| 109 | // A prebuilt module is only primary if it is preferred and conversely a source module is only |
| 110 | // primary if it has not been replaced by a prebuilt module. |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 111 | if pi, ok := module.(android.PrebuiltInterface); ok { |
| 112 | if p := pi.Prebuilt(); p != nil { |
| 113 | primary = p.UsePrebuilt() |
| 114 | } |
| 115 | } else { |
Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 116 | // The only module that will pass a different configurationName to its module name to this |
| 117 | // method is the implementation library of a java_sdk_library. It has a configuration name of |
| 118 | // <x> the same as its parent java_sdk_library but a module name of <x>.impl. It is not the |
| 119 | // primary module, the java_sdk_library with the name of <x> is. |
| 120 | primary = configurationName == ctx.ModuleName() |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 121 | |
| 122 | // A source module that has been replaced by a prebuilt can never be the primary module. |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 123 | if module.IsReplacedByPrebuilt() { |
Paul Duffin | 894546d | 2021-04-21 01:03:30 +0100 | [diff] [blame] | 124 | if ctx.HasProvider(android.ApexInfoProvider) { |
| 125 | // The source module is in an APEX but the prebuilt module on which it depends is not in an |
| 126 | // APEX and so is not the one that will actually be used for hidden API processing. That |
| 127 | // means it is not possible to check to see if it is a suitable replacement so just assume |
| 128 | // that it is. |
| 129 | primary = false |
| 130 | } else { |
| 131 | ctx.VisitDirectDepsWithTag(android.PrebuiltDepTag, func(prebuilt android.Module) { |
| 132 | if h, ok := prebuilt.(hiddenAPIIntf); ok && h.bootDexJar() != nil { |
| 133 | primary = false |
| 134 | } else { |
| 135 | ctx.ModuleErrorf( |
| 136 | "hiddenapi has determined that the source module %q should be ignored as it has been"+ |
| 137 | " replaced by the prebuilt module %q but unfortunately it does not provide a"+ |
| 138 | " suitable boot dex jar", ctx.ModuleName(), ctx.OtherModuleName(prebuilt)) |
| 139 | } |
| 140 | }) |
| 141 | } |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 142 | } |
Paul Duffin | f75e527 | 2021-02-09 14:34:25 +0000 | [diff] [blame] | 143 | } |
| 144 | h.primary = primary |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Paul Duffin | 82b3fcf | 2021-02-12 15:42:46 +0000 | [diff] [blame] | 147 | func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool { |
| 148 | // Get the configured non-updatable and updatable boot jars. |
| 149 | nonUpdatableBootJars := ctx.Config().NonUpdatableBootJars() |
| 150 | updatableBootJars := ctx.Config().UpdatableBootJars() |
| 151 | active := isModuleInConfiguredList(ctx, module, nonUpdatableBootJars) || |
| 152 | isModuleInConfiguredList(ctx, module, updatableBootJars) |
| 153 | return active |
| 154 | } |
| 155 | |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 156 | // hiddenAPIEncodeDex is called by any module that needs to encode dex files. |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 157 | // |
| 158 | // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 159 | // jar list. In that case it simply returns the supplied dex jar path. |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 160 | // |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 161 | // Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi |
| 162 | // flags and returns this instead of the supplied dex jar. |
| 163 | func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath, uncompressDex bool) android.OutputPath { |
Paul Duffin | 001e606 | 2021-05-14 01:13:55 +0100 | [diff] [blame] | 164 | |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 165 | if !h.active { |
| 166 | return dexJar |
| 167 | } |
Paul Duffin | d2aceca | 2019-02-28 16:13:20 +0000 | [diff] [blame] | 168 | |
Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 169 | hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", h.configurationName+".jar").OutputPath |
Paul Duffin | a2058f8 | 2020-06-24 16:22:38 +0100 | [diff] [blame] | 170 | |
Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 171 | // Create a copy of the dex jar which has been encoded with hiddenapi flags. |
| 172 | hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex) |
Paul Duffin | 4103e92 | 2021-02-01 19:01:34 +0000 | [diff] [blame] | 173 | |
Paul Duffin | f8f4af8 | 2021-02-12 15:42:20 +0000 | [diff] [blame] | 174 | // Use the encoded dex jar from here onwards. |
| 175 | dexJar = hiddenAPIJar |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 176 | |
| 177 | return dexJar |
| 178 | } |
| 179 | |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 180 | // hiddenAPIUpdatePaths generates ninja rules to extract the information from the classes |
Paul Duffin | 4fd997b | 2021-02-03 20:06:33 +0000 | [diff] [blame] | 181 | // jar, and outputs it to the appropriate module specific CSV file. |
| 182 | // |
| 183 | // It also makes the dex jar available for use when generating the |
| 184 | // hiddenAPISingletonPathsStruct.stubFlags. |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 185 | func (h *hiddenAPI) hiddenAPIUpdatePaths(ctx android.ModuleContext, dexJar, classesJar android.Path) { |
Paul Duffin | 001e606 | 2021-05-14 01:13:55 +0100 | [diff] [blame] | 186 | |
| 187 | // Save the classes jars even if this is not active as they may be used by modular hidden API |
| 188 | // processing. |
| 189 | classesJars := android.Paths{classesJar} |
| 190 | ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) { |
| 191 | javaInfo := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) |
| 192 | classesJars = append(classesJars, javaInfo.ImplementationJars...) |
| 193 | }) |
| 194 | h.classesJarPaths = classesJars |
| 195 | |
| 196 | // Save the unencoded dex jar so it can be used when generating the |
| 197 | // hiddenAPISingletonPathsStruct.stubFlags file. |
| 198 | h.bootDexJarPath = dexJar |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file |
| 202 | // from the classes jars and stub-flags.csv files. |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 203 | // |
| 204 | // The annotation-flags.csv file contains mappings from Java signature to various flags derived from |
| 205 | // annotations in the source, e.g. whether it is public or the sdk version above which it can no |
| 206 | // longer be used. |
| 207 | // |
| 208 | // It is created by the Class2NonSdkList tool which processes the .class files in the class |
| 209 | // implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The |
| 210 | // tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform |
| 211 | // consistency checks on the information in the annotations and to filter out bridge methods |
| 212 | // that are already part of the public API. |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 213 | func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) { |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 214 | ctx.Build(pctx, android.BuildParams{ |
| 215 | Rule: hiddenAPIGenerateCSVRule, |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 216 | Description: desc, |
Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 217 | Inputs: classesJars, |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 218 | Output: outputPath, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 219 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 220 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 221 | "outFlag": "--write-flags-csv", |
| 222 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 223 | }, |
| 224 | }) |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 225 | } |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 226 | |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 227 | // buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from |
| 228 | // the classes jars and stub-flags.csv files. |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 229 | // |
| 230 | // The metadata.csv file contains mappings from Java signature to the value of properties specified |
| 231 | // on UnsupportedAppUsage annotations in the source. |
| 232 | // |
| 233 | // Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way. |
| 234 | // Although the two files could potentially be created in a single invocation of the |
| 235 | // Class2NonSdkList at the moment they are created using their own invocation, with the behavior |
| 236 | // being determined by the property that is used. |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 237 | func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) { |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 238 | ctx.Build(pctx, android.BuildParams{ |
| 239 | Rule: hiddenAPIGenerateCSVRule, |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 240 | Description: desc, |
Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 241 | Inputs: classesJars, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 242 | Output: metadataCSV, |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 243 | Implicit: stubFlagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 244 | Args: map[string]string{ |
David Brazdil | 0f670a2 | 2019-01-18 16:30:03 +0000 | [diff] [blame] | 245 | "outFlag": "--write-metadata-csv", |
| 246 | "stubAPIFlags": stubFlagsCSV.String(), |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 247 | }, |
| 248 | }) |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 249 | } |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 250 | |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 251 | // buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 252 | // jars. |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame^] | 253 | // |
| 254 | // The index.csv file contains mappings from Java signature to source location information. |
| 255 | // |
| 256 | // It is created by the merge_csv tool which processes the class implementation jar, extracting |
| 257 | // all the files ending in .uau (which are CSV files) and merges them together. The .uau files are |
| 258 | // created by the unsupported app usage annotation processor during compilation of the class |
| 259 | // implementation jar. |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 260 | func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 261 | rule := android.NewRuleBuilder(pctx, ctx) |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 262 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 263 | BuiltTool("merge_csv"). |
Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 264 | Flag("--zip_input"). |
Paul Duffin | 2c36f24 | 2021-02-16 16:57:06 +0000 | [diff] [blame] | 265 | Flag("--key_field signature"). |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 266 | FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties"). |
Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 267 | FlagWithOutput("--output=", indexCSV). |
| 268 | Inputs(classesJars) |
Paul Duffin | 850e61f | 2021-05-14 09:58:48 +0100 | [diff] [blame] | 269 | rule.Build(desc, desc) |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{ |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 273 | Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output && |
Colin Cross | d783bbb | 2020-07-11 22:30:45 -0700 | [diff] [blame] | 274 | unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input && |
Artur Satayev | b5df8a0 | 2020-02-19 16:39:59 +0000 | [diff] [blame] | 275 | for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do |
| 276 | echo "--input-dex=$${INPUT_DEX}"; |
| 277 | echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})"; |
| 278 | done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags && |
| 279 | ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" && |
| 280 | ${config.MergeZipsCmd} -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 281 | CommandDeps: []string{ |
| 282 | "${config.HiddenAPI}", |
| 283 | "${config.SoongZipCmd}", |
| 284 | "${config.MergeZipsCmd}", |
| 285 | }, |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 286 | }, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags") |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 287 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 288 | func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path, |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 289 | uncompressDex bool) { |
| 290 | |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 291 | flagsCSV := hiddenAPISingletonPaths(ctx).flags |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 292 | |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 293 | // The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed |
| 294 | // in the input it stays uncompressed in the output. |
| 295 | soongZipFlags := "" |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 296 | hiddenapiFlags := "" |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 297 | tmpOutput := output |
| 298 | tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex") |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 299 | if uncompressDex { |
| 300 | soongZipFlags = "-L 0" |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 301 | tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar") |
| 302 | tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned") |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 303 | } |
Jiyong Park | 93e57a0 | 2020-02-21 16:04:53 +0900 | [diff] [blame] | 304 | |
| 305 | enforceHiddenApiFlagsToAllMembers := true |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 306 | // If frameworks/base doesn't exist we must be building with the 'master-art' manifest. |
| 307 | // Disable assertion that all methods/fields have hidden API flags assigned. |
| 308 | if !ctx.Config().FrameworksBaseDirExists(ctx) { |
Jiyong Park | 93e57a0 | 2020-02-21 16:04:53 +0900 | [diff] [blame] | 309 | enforceHiddenApiFlagsToAllMembers = false |
| 310 | } |
| 311 | // b/149353192: when a module is instrumented, jacoco adds synthetic members |
| 312 | // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags, |
| 313 | // don't complain when we don't find hidden API flags for the synthetic members. |
Paul Duffin | c495d2b | 2020-05-19 21:07:52 +0100 | [diff] [blame] | 314 | if j, ok := ctx.Module().(interface { |
| 315 | shouldInstrument(android.BaseModuleContext) bool |
| 316 | }); ok && j.shouldInstrument(ctx) { |
Jiyong Park | 93e57a0 | 2020-02-21 16:04:53 +0900 | [diff] [blame] | 317 | enforceHiddenApiFlagsToAllMembers = false |
| 318 | } |
| 319 | |
| 320 | if !enforceHiddenApiFlagsToAllMembers { |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 321 | hiddenapiFlags = "--no-force-assign-all" |
| 322 | } |
Colin Cross | cd964b3 | 2019-01-18 22:03:02 -0800 | [diff] [blame] | 323 | |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 324 | ctx.Build(pctx, android.BuildParams{ |
| 325 | Rule: hiddenAPIEncodeDexRule, |
| 326 | Description: "hiddenapi encode dex", |
| 327 | Input: dexInput, |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 328 | Output: tmpOutput, |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 329 | Implicit: flagsCSV, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 330 | Args: map[string]string{ |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 331 | "flagsCsv": flagsCSV.String(), |
David Brazdil | 91b4e3e | 2019-01-23 21:04:05 +0000 | [diff] [blame] | 332 | "tmpDir": tmpDir.String(), |
| 333 | "soongZipFlags": soongZipFlags, |
| 334 | "hiddenapiFlags": hiddenapiFlags, |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 335 | }, |
| 336 | }) |
| 337 | |
Nicolas Geoffray | 65fd8ba | 2019-01-21 23:20:23 +0000 | [diff] [blame] | 338 | if uncompressDex { |
| 339 | TransformZipAlign(ctx, output, tmpOutput) |
| 340 | } |
Colin Cross | 8faf8fc | 2019-01-16 15:15:52 -0800 | [diff] [blame] | 341 | } |
Paul Duffin | 031d869 | 2021-02-12 11:46:42 +0000 | [diff] [blame] | 342 | |
| 343 | type hiddenApiAnnotationsDependencyTag struct { |
| 344 | blueprint.BaseDependencyTag |
| 345 | } |
| 346 | |
| 347 | // Tag used to mark dependencies on java_library instances that contains Java source files whose |
| 348 | // sole purpose is to provide additional hiddenapi annotations. |
| 349 | var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag |
| 350 | |
| 351 | // Mark this tag so dependencies that use it are excluded from APEX contents. |
| 352 | func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {} |
| 353 | |
| 354 | var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag |