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