Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 18 | "fmt" |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 19 | "path/filepath" |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 20 | "slices" |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 21 | "strconv" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 22 | "strings" |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 23 | |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 24 | "android/soong/android" |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 25 | "android/soong/bazel" |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 26 | "android/soong/dexpreopt" |
Romain Jobredeaux | 7a71e07 | 2023-08-16 17:39:12 -0400 | [diff] [blame] | 27 | "android/soong/ui/metrics/bp2build_metrics_proto" |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 28 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 29 | "github.com/google/blueprint" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 30 | "github.com/google/blueprint/proptools" |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 33 | type AndroidLibraryDependency interface { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 34 | ExportPackage() android.Path |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 35 | ResourcesNodeDepSet() *android.DepSet[*resourcesNode] |
| 36 | RRODirsDepSet() *android.DepSet[rroDir] |
| 37 | ManifestsDepSet() *android.DepSet[android.Path] |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 38 | SetRROEnforcedForDependent(enforce bool) |
| 39 | IsRROEnforced(ctx android.BaseModuleContext) bool |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | func init() { |
Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 43 | RegisterAARBuildComponents(android.InitRegistrationContext) |
| 44 | } |
| 45 | |
| 46 | func RegisterAARBuildComponents(ctx android.RegistrationContext) { |
| 47 | ctx.RegisterModuleType("android_library_import", AARImportFactory) |
| 48 | ctx.RegisterModuleType("android_library", AndroidLibraryFactory) |
Paul Duffin | 04ba70d | 2021-03-22 13:56:43 +0000 | [diff] [blame] | 49 | ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 50 | ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel() |
| 51 | }) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // |
| 55 | // AAR (android library) |
| 56 | // |
| 57 | |
| 58 | type androidLibraryProperties struct { |
| 59 | BuildAAR bool `blueprint:"mutated"` |
| 60 | } |
| 61 | |
| 62 | type aaptProperties struct { |
| 63 | // flags passed to aapt when creating the apk |
| 64 | Aaptflags []string |
| 65 | |
Dan Willemsen | 72be590 | 2018-10-24 20:24:57 -0700 | [diff] [blame] | 66 | // include all resource configurations, not just the product-configured |
| 67 | // ones. |
| 68 | Aapt_include_all_resources *bool |
| 69 | |
Jiakai Zhang | ba82e28 | 2023-10-13 18:08:59 +0100 | [diff] [blame] | 70 | // list of files to use as assets. |
| 71 | Assets []string `android:"path"` |
| 72 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 73 | // list of directories relative to the Blueprints file containing assets. |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 74 | // Defaults to ["assets"] if a directory called assets exists. Set to [] |
| 75 | // to disable the default. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 76 | Asset_dirs []string |
| 77 | |
| 78 | // list of directories relative to the Blueprints file containing |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 79 | // Android resources. Defaults to ["res"] if a directory called res exists. |
| 80 | // Set to [] to disable the default. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 81 | Resource_dirs []string |
| 82 | |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 83 | // list of zip files containing Android resources. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 84 | Resource_zips []string `android:"path"` |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 85 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 86 | // path to AndroidManifest.xml. If unset, defaults to "AndroidManifest.xml". |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 87 | Manifest *string `android:"path"` |
changho.shin | b5432b7 | 2019-08-08 18:37:17 +0900 | [diff] [blame] | 88 | |
| 89 | // paths to additional manifest files to merge with main manifest. |
| 90 | Additional_manifests []string `android:"path"` |
Sasha Smundak | 541056c | 2019-10-28 15:50:06 -0700 | [diff] [blame] | 91 | |
| 92 | // do not include AndroidManifest from dependent libraries |
| 93 | Dont_merge_manifests *bool |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 94 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 95 | // If use_resource_processor is set, use Bazel's resource processor instead of aapt2 to generate R.class files. |
| 96 | // The resource processor produces more optimal R.class files that only list resources in the package of the |
| 97 | // library that provided them, as opposed to aapt2 which produces R.java files for every package containing |
| 98 | // every resource. Using the resource processor can provide significant build time speedups, but requires |
| 99 | // fixing the module to use the correct package to reference each resource, and to avoid having any other |
| 100 | // libraries in the tree that use the same package name. Defaults to false, but will default to true in the |
| 101 | // future. |
| 102 | Use_resource_processor *bool |
| 103 | |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 104 | // true if RRO is enforced for any of the dependent modules |
| 105 | RROEnforcedForDependent bool `blueprint:"mutated"` |
Inseob Kim | 34dc4cd | 2023-11-07 13:37:14 +0900 | [diff] [blame] | 106 | |
| 107 | // Filter only specified product and ignore other products |
| 108 | Filter_product *string `blueprint:"mutated"` |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | type aapt struct { |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 112 | aaptSrcJar android.Path |
| 113 | transitiveAaptRJars android.Paths |
| 114 | transitiveAaptResourcePackagesFile android.Path |
| 115 | exportPackage android.Path |
| 116 | manifestPath android.Path |
| 117 | proguardOptionsFile android.Path |
| 118 | rTxt android.Path |
| 119 | rJar android.Path |
| 120 | extraAaptPackagesFile android.Path |
| 121 | mergedManifestFile android.Path |
| 122 | noticeFile android.OptionalPath |
| 123 | assetPackage android.OptionalPath |
| 124 | isLibrary bool |
| 125 | defaultManifestVersion string |
| 126 | useEmbeddedNativeLibs bool |
| 127 | useEmbeddedDex bool |
| 128 | usesNonSdkApis bool |
| 129 | hasNoCode bool |
| 130 | LoggingParent string |
| 131 | resourceFiles android.Paths |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 132 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 133 | splitNames []string |
| 134 | splits []split |
| 135 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 136 | aaptProperties aaptProperties |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 137 | |
| 138 | resourcesNodesDepSet *android.DepSet[*resourcesNode] |
| 139 | rroDirsDepSet *android.DepSet[rroDir] |
| 140 | manifestsDepSet *android.DepSet[android.Path] |
Alix | 96ea8845 | 2023-08-31 15:48:23 +0000 | [diff] [blame] | 141 | |
| 142 | manifestValues struct { |
| 143 | applicationId string |
| 144 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 147 | type split struct { |
| 148 | name string |
| 149 | suffix string |
| 150 | path android.Path |
| 151 | } |
| 152 | |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 153 | // Propagate RRO enforcement flag to static lib dependencies transitively. |
| 154 | func propagateRROEnforcementMutator(ctx android.TopDownMutatorContext) { |
| 155 | m := ctx.Module() |
| 156 | if d, ok := m.(AndroidLibraryDependency); ok && d.IsRROEnforced(ctx) { |
| 157 | ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) { |
| 158 | if a, ok := d.(AndroidLibraryDependency); ok { |
| 159 | a.SetRROEnforcedForDependent(true) |
| 160 | } |
| 161 | }) |
| 162 | } |
| 163 | } |
| 164 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 165 | func (a *aapt) useResourceProcessorBusyBox() bool { |
| 166 | return BoolDefault(a.aaptProperties.Use_resource_processor, false) |
| 167 | } |
| 168 | |
Inseob Kim | 34dc4cd | 2023-11-07 13:37:14 +0900 | [diff] [blame] | 169 | func (a *aapt) filterProduct() string { |
| 170 | return String(a.aaptProperties.Filter_product) |
| 171 | } |
| 172 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 173 | func (a *aapt) ExportPackage() android.Path { |
| 174 | return a.exportPackage |
| 175 | } |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 176 | func (a *aapt) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] { |
| 177 | return a.resourcesNodesDepSet |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 180 | func (a *aapt) RRODirsDepSet() *android.DepSet[rroDir] { |
| 181 | return a.rroDirsDepSet |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 184 | func (a *aapt) ManifestsDepSet() *android.DepSet[android.Path] { |
| 185 | return a.manifestsDepSet |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 188 | func (a *aapt) SetRROEnforcedForDependent(enforce bool) { |
| 189 | a.aaptProperties.RROEnforcedForDependent = enforce |
| 190 | } |
| 191 | |
| 192 | func (a *aapt) IsRROEnforced(ctx android.BaseModuleContext) bool { |
| 193 | // True if RRO is enforced for this module or... |
| 194 | return ctx.Config().EnforceRROForModule(ctx.ModuleName()) || |
Jeongik Cha | cee5ba9 | 2021-02-19 12:11:51 +0900 | [diff] [blame] | 195 | // if RRO is enforced for any of its dependents. |
| 196 | a.aaptProperties.RROEnforcedForDependent |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 199 | func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkContext, |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 200 | manifestPath android.Path) (compileFlags, linkFlags []string, linkDeps android.Paths, |
| 201 | resDirs, overlayDirs []globbedResourceDir, rroDirs []rroDir, resZips android.Paths) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 202 | |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 203 | hasVersionCode := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-code") |
| 204 | hasVersionName := android.PrefixInList(a.aaptProperties.Aaptflags, "--version-name") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 205 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 206 | // Flags specified in Android.bp |
| 207 | linkFlags = append(linkFlags, a.aaptProperties.Aaptflags...) |
| 208 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 209 | // Find implicit or explicit asset and resource dirs |
Jiakai Zhang | ba82e28 | 2023-10-13 18:08:59 +0100 | [diff] [blame] | 210 | assets := android.PathsRelativeToModuleSourceDir(android.SourceInput{ |
| 211 | Context: ctx, |
| 212 | Paths: a.aaptProperties.Assets, |
| 213 | IncludeDirs: false, |
| 214 | }) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 215 | assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets") |
| 216 | resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res") |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 217 | resourceZips := android.PathsForModuleSrc(ctx, a.aaptProperties.Resource_zips) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 218 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 219 | // Glob directories into lists of paths |
| 220 | for _, dir := range resourceDirs { |
| 221 | resDirs = append(resDirs, globbedResourceDir{ |
| 222 | dir: dir, |
| 223 | files: androidResourceGlob(ctx, dir), |
| 224 | }) |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 225 | resOverlayDirs, resRRODirs := overlayResourceGlob(ctx, a, dir) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 226 | overlayDirs = append(overlayDirs, resOverlayDirs...) |
| 227 | rroDirs = append(rroDirs, resRRODirs...) |
| 228 | } |
| 229 | |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 230 | var assetDeps android.Paths |
| 231 | for i, dir := range assetDirs { |
| 232 | // Add a dependency on every file in the asset directory. This ensures the aapt2 |
| 233 | // rule will be rerun if one of the files in the asset directory is modified. |
| 234 | assetDeps = append(assetDeps, androidResourceGlob(ctx, dir)...) |
| 235 | |
| 236 | // Add a dependency on a file that contains a list of all the files in the asset directory. |
| 237 | // This ensures the aapt2 rule will be run if a file is removed from the asset directory, |
| 238 | // or a file is added whose timestamp is older than the output of aapt2. |
| 239 | assetFileListFile := android.PathForModuleOut(ctx, "asset_dir_globs", strconv.Itoa(i)+".glob") |
| 240 | androidResourceGlobList(ctx, dir, assetFileListFile) |
| 241 | assetDeps = append(assetDeps, assetFileListFile) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 244 | assetDirStrings := assetDirs.Strings() |
| 245 | if a.noticeFile.Valid() { |
| 246 | assetDirStrings = append(assetDirStrings, filepath.Dir(a.noticeFile.Path().String())) |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 247 | assetDeps = append(assetDeps, a.noticeFile.Path()) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 248 | } |
Jiakai Zhang | ba82e28 | 2023-10-13 18:08:59 +0100 | [diff] [blame] | 249 | if len(assets) > 0 { |
| 250 | // aapt2 doesn't support adding individual asset files. Create a temp directory to hold asset |
| 251 | // files and pass it to aapt2. |
| 252 | tmpAssetDir := android.PathForModuleOut(ctx, "tmp_asset_dir") |
| 253 | |
| 254 | rule := android.NewRuleBuilder(pctx, ctx) |
| 255 | rule.Command(). |
| 256 | Text("rm -rf").Text(tmpAssetDir.String()). |
| 257 | Text("&&"). |
| 258 | Text("mkdir -p").Text(tmpAssetDir.String()) |
| 259 | |
| 260 | for _, asset := range assets { |
| 261 | output := tmpAssetDir.Join(ctx, asset.Rel()) |
| 262 | assetDeps = append(assetDeps, output) |
| 263 | rule.Command().Text("mkdir -p").Text(filepath.Dir(output.String())) |
| 264 | rule.Command().Text("cp").Input(asset).Output(output) |
| 265 | } |
| 266 | |
| 267 | rule.Build("tmp_asset_dir", "tmp_asset_dir") |
| 268 | |
| 269 | assetDirStrings = append(assetDirStrings, tmpAssetDir.String()) |
| 270 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 271 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 272 | linkFlags = append(linkFlags, "--manifest "+manifestPath.String()) |
| 273 | linkDeps = append(linkDeps, manifestPath) |
| 274 | |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 275 | linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirStrings, "-A ")) |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame] | 276 | linkDeps = append(linkDeps, assetDeps...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 277 | |
Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 278 | // Returns the effective version for {min|target}_sdk_version |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 279 | effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string { |
Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 280 | // If {min|target}_sdk_version is current, use sdk_version to determine the effective level |
| 281 | // This is necessary for vendor modules. |
| 282 | // The effective version does not _only_ depend on {min|target}_sdk_version(level), |
| 283 | // but also on the sdk_version (kind+level) |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 284 | if minSdkVersion.IsCurrent() { |
Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 285 | ret, err := sdkVersion.EffectiveVersionString(ctx) |
| 286 | if err != nil { |
| 287 | ctx.ModuleErrorf("invalid sdk_version: %s", err) |
| 288 | } |
| 289 | return ret |
| 290 | } |
| 291 | ret, err := minSdkVersion.EffectiveVersionString(ctx) |
| 292 | if err != nil { |
| 293 | ctx.ModuleErrorf("invalid min_sdk_version: %s", err) |
| 294 | } |
| 295 | return ret |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 296 | } |
Spandan Das | 50885c0 | 2023-02-23 21:31:33 +0000 | [diff] [blame] | 297 | // SDK version flags |
| 298 | sdkVersion := sdkContext.SdkVersion(ctx) |
| 299 | minSdkVersion := effectiveVersionString(sdkVersion, sdkContext.MinSdkVersion(ctx)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 300 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 301 | linkFlags = append(linkFlags, "--min-sdk-version "+minSdkVersion) |
Spandan Das | 6450b55 | 2023-02-23 19:27:07 +0000 | [diff] [blame] | 302 | // Use minSdkVersion for target-sdk-version, even if `target_sdk_version` is set |
| 303 | // This behavior has been copied from Make. |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 304 | linkFlags = append(linkFlags, "--target-sdk-version "+minSdkVersion) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 305 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 306 | // Version code |
| 307 | if !hasVersionCode { |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 308 | linkFlags = append(linkFlags, "--version-code", ctx.Config().PlatformSdkVersion().String()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if !hasVersionName { |
Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 312 | var versionName string |
| 313 | if ctx.ModuleName() == "framework-res" { |
| 314 | // Some builds set AppsDefaultVersionName() to include the build number ("O-123456"). aapt2 copies the |
| 315 | // version name of framework-res into app manifests as compileSdkVersionCodename, which confuses things |
Colin Cross | bfd347d | 2018-05-09 11:11:35 -0700 | [diff] [blame] | 316 | // if it contains the build number. Use the PlatformVersionName instead. |
| 317 | versionName = ctx.Config().PlatformVersionName() |
Colin Cross | 402d5e0 | 2018-04-25 14:54:06 -0700 | [diff] [blame] | 318 | } else { |
| 319 | versionName = ctx.Config().AppsDefaultVersionName() |
| 320 | } |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 321 | versionName = proptools.NinjaEscape(versionName) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 322 | linkFlags = append(linkFlags, "--version-name ", versionName) |
| 323 | } |
| 324 | |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 325 | linkFlags, compileFlags = android.FilterList(linkFlags, []string{"--legacy"}) |
| 326 | |
| 327 | // Always set --pseudo-localize, it will be stripped out later for release |
| 328 | // builds that don't want it. |
| 329 | compileFlags = append(compileFlags, "--pseudo-localize") |
| 330 | |
| 331 | return compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resourceZips |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 334 | func (a *aapt) deps(ctx android.BottomUpMutatorContext, sdkDep sdkDep) { |
Colin Cross | 42308aa | 2018-11-14 21:44:17 -0800 | [diff] [blame] | 335 | if sdkDep.frameworkResModule != "" { |
| 336 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 340 | var extractAssetsRule = pctx.AndroidStaticRule("extractAssets", |
| 341 | blueprint.RuleParams{ |
| 342 | Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "assets/**/*"`, |
| 343 | CommandDeps: []string{"${config.Zip2ZipCmd}"}, |
| 344 | }) |
| 345 | |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 346 | type aaptBuildActionOptions struct { |
| 347 | sdkContext android.SdkContext |
| 348 | classLoaderContexts dexpreopt.ClassLoaderContextMap |
| 349 | excludedLibs []string |
| 350 | enforceDefaultTargetSdkVersion bool |
| 351 | extraLinkFlags []string |
| 352 | } |
| 353 | |
| 354 | func (a *aapt) buildActions(ctx android.ModuleContext, opts aaptBuildActionOptions) { |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 355 | |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 356 | staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedExportPackages, libFlags := |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 357 | aaptLibs(ctx, opts.sdkContext, opts.classLoaderContexts) |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 358 | |
Paul Duffin | 0653057 | 2022-02-03 17:54:15 +0000 | [diff] [blame] | 359 | // Exclude any libraries from the supplied list. |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 360 | opts.classLoaderContexts = opts.classLoaderContexts.ExcludeLibs(opts.excludedLibs) |
Paul Duffin | 0653057 | 2022-02-03 17:54:15 +0000 | [diff] [blame] | 361 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 362 | // App manifest file |
| 363 | manifestFile := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml") |
| 364 | manifestSrcPath := android.PathForModuleSrc(ctx, manifestFile) |
| 365 | |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 366 | manifestPath := ManifestFixer(ctx, manifestSrcPath, ManifestFixerParams{ |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 367 | SdkContext: opts.sdkContext, |
| 368 | ClassLoaderContexts: opts.classLoaderContexts, |
Harshit Mahajan | 5b8b730 | 2022-06-10 11:24:05 +0000 | [diff] [blame] | 369 | IsLibrary: a.isLibrary, |
| 370 | DefaultManifestVersion: a.defaultManifestVersion, |
| 371 | UseEmbeddedNativeLibs: a.useEmbeddedNativeLibs, |
| 372 | UsesNonSdkApis: a.usesNonSdkApis, |
| 373 | UseEmbeddedDex: a.useEmbeddedDex, |
| 374 | HasNoCode: a.hasNoCode, |
| 375 | LoggingParent: a.LoggingParent, |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 376 | EnforceDefaultTargetSdkVersion: opts.enforceDefaultTargetSdkVersion, |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 377 | }) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 378 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 379 | staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList()) |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 380 | sharedDeps := transitiveAarDeps(sharedResourcesNodesDepSet.ToList()) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 381 | |
Luca Stefani | fd89882 | 2019-09-10 22:13:31 +0200 | [diff] [blame] | 382 | // Add additional manifest files to transitive manifests. |
| 383 | additionalManifests := android.PathsForModuleSrc(ctx, a.aaptProperties.Additional_manifests) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 384 | transitiveManifestPaths := append(android.Paths{manifestPath}, additionalManifests...) |
| 385 | // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import |
| 386 | // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies |
| 387 | // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of |
| 388 | // android_library_import modules. If this is fixed, staticManifestsDepSet can be dropped completely in favor of |
| 389 | // staticResourcesNodesDepSet.manifests() |
| 390 | transitiveManifestPaths = append(transitiveManifestPaths, staticManifestsDepSet.ToList()...) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 391 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 392 | if len(transitiveManifestPaths) > 1 && !Bool(a.aaptProperties.Dont_merge_manifests) { |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 393 | manifestMergerParams := ManifestMergerParams{ |
| 394 | staticLibManifests: transitiveManifestPaths[1:], |
Alix | 96ea8845 | 2023-08-31 15:48:23 +0000 | [diff] [blame] | 395 | isLibrary: a.isLibrary, |
| 396 | packageName: a.manifestValues.applicationId, |
| 397 | } |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 398 | a.mergedManifestFile = manifestMerger(ctx, transitiveManifestPaths[0], manifestMergerParams) |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 399 | if !a.isLibrary { |
| 400 | // Only use the merged manifest for applications. For libraries, the transitive closure of manifests |
| 401 | // will be propagated to the final application and merged there. The merged manifest for libraries is |
| 402 | // only passed to Make, which can't handle transitive dependencies. |
| 403 | manifestPath = a.mergedManifestFile |
| 404 | } |
| 405 | } else { |
| 406 | a.mergedManifestFile = manifestPath |
| 407 | } |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 408 | |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 409 | compileFlags, linkFlags, linkDeps, resDirs, overlayDirs, rroDirs, resZips := a.aapt2Flags(ctx, opts.sdkContext, manifestPath) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 410 | |
| 411 | linkFlags = append(linkFlags, libFlags...) |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 412 | linkDeps = append(linkDeps, sharedExportPackages...) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 413 | linkDeps = append(linkDeps, staticDeps.resPackages()...) |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 414 | linkFlags = append(linkFlags, opts.extraLinkFlags...) |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 415 | if a.isLibrary { |
| 416 | linkFlags = append(linkFlags, "--static-lib") |
| 417 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 418 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 419 | if a.isLibrary && a.useResourceProcessorBusyBox() { |
| 420 | // When building an android_library using ResourceProcessorBusyBox the resources are merged into |
| 421 | // package-res.apk with --merge-only, but --no-static-lib-packages is not used so that R.txt only |
| 422 | // contains resources from this library. |
| 423 | linkFlags = append(linkFlags, "--merge-only") |
| 424 | } else { |
| 425 | // When building and app or when building an android_library without ResourceProcessorBusyBox |
| 426 | // --no-static-lib-packages is used to put all the resources into the app. If ResourceProcessorBusyBox |
| 427 | // is used then the app's R.txt will be post-processed along with the R.txt files from dependencies to |
| 428 | // sort resources into the right packages in R.class. |
| 429 | linkFlags = append(linkFlags, "--no-static-lib-packages") |
| 430 | } |
| 431 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 432 | packageRes := android.PathForModuleOut(ctx, "package-res.apk") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 433 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") |
| 434 | rTxt := android.PathForModuleOut(ctx, "R.txt") |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 435 | // This file isn't used by Soong, but is generated for exporting |
| 436 | extraPackages := android.PathForModuleOut(ctx, "extra_packages") |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 437 | var transitiveRJars android.Paths |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 438 | var srcJar android.WritablePath |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 439 | |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 440 | var compiledResDirs []android.Paths |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 441 | for _, dir := range resDirs { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 442 | a.resourceFiles = append(a.resourceFiles, dir.files...) |
Inseob Kim | 34dc4cd | 2023-11-07 13:37:14 +0900 | [diff] [blame] | 443 | compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 444 | } |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 445 | |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 446 | for i, zip := range resZips { |
| 447 | flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i)) |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 448 | aapt2CompileZip(ctx, flata, zip, "", compileFlags) |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 449 | compiledResDirs = append(compiledResDirs, android.Paths{flata}) |
| 450 | } |
| 451 | |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 452 | var compiledRes, compiledOverlay android.Paths |
| 453 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 454 | // AAPT2 overlays are in lowest to highest priority order, reverse the topological order |
| 455 | // of transitiveStaticLibs. |
| 456 | transitiveStaticLibs := android.ReversePaths(staticDeps.resPackages()) |
| 457 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 458 | if a.isLibrary && a.useResourceProcessorBusyBox() { |
| 459 | // When building an android_library with ResourceProcessorBusyBox enabled treat static library dependencies |
| 460 | // as imports. The resources from dependencies will not be merged into this module's package-res.apk, and |
| 461 | // instead modules depending on this module will reference package-res.apk from all transitive static |
| 462 | // dependencies. |
| 463 | for _, staticDep := range staticDeps { |
| 464 | linkDeps = append(linkDeps, staticDep.resPackage) |
| 465 | linkFlags = append(linkFlags, "-I "+staticDep.resPackage.String()) |
| 466 | if staticDep.usedResourceProcessor { |
| 467 | transitiveRJars = append(transitiveRJars, staticDep.rJar) |
| 468 | } |
| 469 | } |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 470 | for _, sharedDep := range sharedDeps { |
| 471 | if sharedDep.usedResourceProcessor { |
| 472 | transitiveRJars = append(transitiveRJars, sharedDep.rJar) |
| 473 | } |
| 474 | } |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 475 | } else { |
| 476 | // When building an app or building a library without ResourceProcessorBusyBox enabled all static |
| 477 | // dependencies are compiled into this module's package-res.apk as overlays. |
| 478 | compiledOverlay = append(compiledOverlay, transitiveStaticLibs...) |
| 479 | } |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 480 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 481 | if len(transitiveStaticLibs) > 0 { |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 482 | // If we are using static android libraries, every source file becomes an overlay. |
| 483 | // This is to emulate old AAPT behavior which simulated library support. |
| 484 | for _, compiledResDir := range compiledResDirs { |
| 485 | compiledOverlay = append(compiledOverlay, compiledResDir...) |
| 486 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 487 | } else if a.isLibrary { |
| 488 | // Otherwise, for a static library we treat all the resources equally with no overlay. |
| 489 | for _, compiledResDir := range compiledResDirs { |
| 490 | compiledRes = append(compiledRes, compiledResDir...) |
| 491 | } |
Colin Cross | 4aaa84a | 2018-08-21 15:14:37 -0700 | [diff] [blame] | 492 | } else if len(compiledResDirs) > 0 { |
| 493 | // Without static libraries, the first directory is our directory, which can then be |
| 494 | // overlaid by the rest. |
| 495 | compiledRes = append(compiledRes, compiledResDirs[0]...) |
| 496 | for _, compiledResDir := range compiledResDirs[1:] { |
| 497 | compiledOverlay = append(compiledOverlay, compiledResDir...) |
| 498 | } |
| 499 | } |
| 500 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 501 | for _, dir := range overlayDirs { |
Inseob Kim | 34dc4cd | 2023-11-07 13:37:14 +0900 | [diff] [blame] | 502 | compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files, compileFlags, a.filterProduct()).Paths()...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 505 | var splitPackages android.WritablePaths |
| 506 | var splits []split |
| 507 | |
| 508 | for _, s := range a.splitNames { |
| 509 | suffix := strings.Replace(s, ",", "_", -1) |
| 510 | path := android.PathForModuleOut(ctx, "package_"+suffix+".apk") |
| 511 | linkFlags = append(linkFlags, "--split", path.String()+":"+s) |
| 512 | splitPackages = append(splitPackages, path) |
| 513 | splits = append(splits, split{ |
| 514 | name: s, |
| 515 | suffix: suffix, |
| 516 | path: path, |
| 517 | }) |
| 518 | } |
| 519 | |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 520 | if !a.useResourceProcessorBusyBox() { |
| 521 | // the subdir "android" is required to be filtered by package names |
| 522 | srcJar = android.PathForModuleGen(ctx, "android", "R.srcjar") |
| 523 | } |
| 524 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 525 | // No need to specify assets from dependencies to aapt2Link for libraries, all transitive assets will be |
| 526 | // provided to the final app aapt2Link step. |
| 527 | var transitiveAssets android.Paths |
| 528 | if !a.isLibrary { |
| 529 | transitiveAssets = android.ReverseSliceInPlace(staticDeps.assets()) |
| 530 | } |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 531 | aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 532 | linkFlags, linkDeps, compiledRes, compiledOverlay, transitiveAssets, splitPackages) |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 533 | // Extract assets from the resource package output so that they can be used later in aapt2link |
| 534 | // for modules that depend on this one. |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 535 | if android.PrefixInList(linkFlags, "-A ") { |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 536 | assets := android.PathForModuleOut(ctx, "assets.zip") |
| 537 | ctx.Build(pctx, android.BuildParams{ |
| 538 | Rule: extractAssetsRule, |
| 539 | Input: packageRes, |
| 540 | Output: assets, |
| 541 | Description: "extract assets from built resource file", |
| 542 | }) |
| 543 | a.assetPackage = android.OptionalPathForPath(assets) |
| 544 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 545 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 546 | if a.useResourceProcessorBusyBox() { |
| 547 | rJar := android.PathForModuleOut(ctx, "busybox/R.jar") |
| 548 | resourceProcessorBusyBoxGenerateBinaryR(ctx, rTxt, a.mergedManifestFile, rJar, staticDeps, a.isLibrary) |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 549 | aapt2ExtractExtraPackages(ctx, extraPackages, rJar) |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 550 | transitiveRJars = append(transitiveRJars, rJar) |
| 551 | a.rJar = rJar |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 552 | } else { |
| 553 | aapt2ExtractExtraPackages(ctx, extraPackages, srcJar) |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 556 | transitiveAaptResourcePackages := staticDeps.resPackages().Strings() |
| 557 | transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool { |
| 558 | return p == packageRes.String() |
| 559 | }) |
| 560 | transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages") |
| 561 | android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n")) |
| 562 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 563 | a.aaptSrcJar = srcJar |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 564 | a.transitiveAaptRJars = transitiveRJars |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 565 | a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 566 | a.exportPackage = packageRes |
| 567 | a.manifestPath = manifestPath |
| 568 | a.proguardOptionsFile = proguardOptionsFile |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 569 | a.extraAaptPackagesFile = extraPackages |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 570 | a.rTxt = rTxt |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 571 | a.splits = splits |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 572 | a.resourcesNodesDepSet = android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL). |
| 573 | Direct(&resourcesNode{ |
| 574 | resPackage: a.exportPackage, |
| 575 | manifest: a.manifestPath, |
| 576 | additionalManifests: additionalManifests, |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 577 | rTxt: a.rTxt, |
| 578 | rJar: a.rJar, |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 579 | assets: a.assetPackage, |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 580 | |
| 581 | usedResourceProcessor: a.useResourceProcessorBusyBox(), |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 582 | }). |
| 583 | Transitive(staticResourcesNodesDepSet).Build() |
| 584 | a.rroDirsDepSet = android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL). |
| 585 | Direct(rroDirs...). |
| 586 | Transitive(staticRRODirsDepSet).Build() |
| 587 | a.manifestsDepSet = android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL). |
| 588 | Direct(a.manifestPath). |
| 589 | DirectSlice(additionalManifests). |
| 590 | Transitive(staticManifestsDepSet).Build() |
| 591 | } |
| 592 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 593 | var resourceProcessorBusyBox = pctx.AndroidStaticRule("resourceProcessorBusyBox", |
| 594 | blueprint.RuleParams{ |
| 595 | Command: "${config.JavaCmd} -cp ${config.ResourceProcessorBusyBox} " + |
| 596 | "com.google.devtools.build.android.ResourceProcessorBusyBox --tool=GENERATE_BINARY_R -- @${out}.args && " + |
| 597 | "if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out}; fi", |
| 598 | CommandDeps: []string{"${config.ResourceProcessorBusyBox}"}, |
| 599 | Rspfile: "${out}.args", |
| 600 | RspfileContent: "--primaryRTxt ${rTxt} --primaryManifest ${manifest} --classJarOutput ${out}.tmp ${args}", |
| 601 | Restat: true, |
| 602 | }, "rTxt", "manifest", "args") |
| 603 | |
| 604 | // resourceProcessorBusyBoxGenerateBinaryR converts the R.txt file produced by aapt2 into R.class files |
| 605 | // using Bazel's ResourceProcessorBusyBox tool, which is faster than compiling the R.java files and |
| 606 | // supports producing classes for static dependencies that only include resources from that dependency. |
| 607 | func resourceProcessorBusyBoxGenerateBinaryR(ctx android.ModuleContext, rTxt, manifest android.Path, |
| 608 | rJar android.WritablePath, transitiveDeps transitiveAarDeps, isLibrary bool) { |
| 609 | |
| 610 | var args []string |
| 611 | var deps android.Paths |
| 612 | |
| 613 | if !isLibrary { |
| 614 | // When compiling an app, pass all R.txt and AndroidManifest.xml from transitive static library dependencies |
| 615 | // to ResourceProcessorBusyBox so that it can regenerate R.class files with the final resource IDs for each |
| 616 | // package. |
| 617 | args, deps = transitiveDeps.resourceProcessorDeps() |
| 618 | } else { |
| 619 | // When compiling a library don't pass any dependencies as it only needs to generate an R.class file for this |
| 620 | // library. Pass --finalFields=false so that the R.class file contains non-final fields so they don't get |
| 621 | // inlined into the library before the final IDs are assigned during app compilation. |
| 622 | args = append(args, "--finalFields=false") |
| 623 | } |
| 624 | |
| 625 | deps = append(deps, rTxt, manifest) |
| 626 | |
| 627 | ctx.Build(pctx, android.BuildParams{ |
| 628 | Rule: resourceProcessorBusyBox, |
| 629 | Output: rJar, |
| 630 | Implicits: deps, |
| 631 | Description: "ResourceProcessorBusyBox", |
| 632 | Args: map[string]string{ |
| 633 | "rTxt": rTxt.String(), |
| 634 | "manifest": manifest.String(), |
| 635 | "args": strings.Join(args, " "), |
| 636 | }, |
| 637 | }) |
| 638 | } |
| 639 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 640 | type resourcesNode struct { |
| 641 | resPackage android.Path |
| 642 | manifest android.Path |
| 643 | additionalManifests android.Paths |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 644 | rTxt android.Path |
| 645 | rJar android.Path |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 646 | assets android.OptionalPath |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 647 | |
| 648 | usedResourceProcessor bool |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | type transitiveAarDeps []*resourcesNode |
| 652 | |
| 653 | func (t transitiveAarDeps) resPackages() android.Paths { |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 654 | paths := make(android.Paths, 0, len(t)) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 655 | for _, dep := range t { |
| 656 | paths = append(paths, dep.resPackage) |
| 657 | } |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 658 | return paths |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | func (t transitiveAarDeps) manifests() android.Paths { |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 662 | paths := make(android.Paths, 0, len(t)) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 663 | for _, dep := range t { |
| 664 | paths = append(paths, dep.manifest) |
| 665 | paths = append(paths, dep.additionalManifests...) |
| 666 | } |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 667 | return paths |
| 668 | } |
| 669 | |
| 670 | func (t transitiveAarDeps) resourceProcessorDeps() (args []string, deps android.Paths) { |
| 671 | for _, dep := range t { |
| 672 | args = append(args, "--library="+dep.rTxt.String()+","+dep.manifest.String()) |
| 673 | deps = append(deps, dep.rTxt, dep.manifest) |
| 674 | } |
| 675 | return args, deps |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | func (t transitiveAarDeps) assets() android.Paths { |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 679 | paths := make(android.Paths, 0, len(t)) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 680 | for _, dep := range t { |
| 681 | if dep.assets.Valid() { |
| 682 | paths = append(paths, dep.assets.Path()) |
| 683 | } |
| 684 | } |
| 685 | return paths |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 689 | func aaptLibs(ctx android.ModuleContext, sdkContext android.SdkContext, classLoaderContexts dexpreopt.ClassLoaderContextMap) ( |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 690 | staticResourcesNodes, sharedResourcesNodes *android.DepSet[*resourcesNode], staticRRODirs *android.DepSet[rroDir], |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 691 | staticManifests *android.DepSet[android.Path], sharedLibs android.Paths, flags []string) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 692 | |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 693 | if classLoaderContexts == nil { |
Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 694 | // Not all callers need to compute class loader context, those who don't just pass nil. |
| 695 | // Create a temporary class loader context here (it will be computed, but not used). |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 696 | classLoaderContexts = make(dexpreopt.ClassLoaderContextMap) |
Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 699 | sdkDep := decodeSdkDep(ctx, sdkContext) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 700 | if sdkDep.useFiles { |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 701 | sharedLibs = append(sharedLibs, sdkDep.jars...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 704 | var staticResourcesNodeDepSets []*android.DepSet[*resourcesNode] |
| 705 | var sharedResourcesNodeDepSets []*android.DepSet[*resourcesNode] |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 706 | rroDirsDepSetBuilder := android.NewDepSetBuilder[rroDir](android.TOPOLOGICAL) |
| 707 | manifestsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL) |
| 708 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 709 | ctx.VisitDirectDeps(func(module android.Module) { |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 710 | depTag := ctx.OtherModuleDependencyTag(module) |
Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 711 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 712 | var exportPackage android.Path |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 713 | aarDep, _ := module.(AndroidLibraryDependency) |
| 714 | if aarDep != nil { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 715 | exportPackage = aarDep.ExportPackage() |
| 716 | } |
| 717 | |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 718 | switch depTag { |
Colin Cross | 4b964c0 | 2018-10-15 16:18:06 -0700 | [diff] [blame] | 719 | case instrumentationForTag: |
| 720 | // Nothing, instrumentationForTag is treated as libTag for javac but not for aapt2. |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 721 | case sdkLibTag, libTag: |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 722 | if exportPackage != nil { |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 723 | sharedResourcesNodeDepSets = append(sharedResourcesNodeDepSets, aarDep.ResourcesNodeDepSet()) |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 724 | sharedLibs = append(sharedLibs, exportPackage) |
| 725 | } |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 726 | case frameworkResTag: |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 727 | if exportPackage != nil { |
| 728 | sharedLibs = append(sharedLibs, exportPackage) |
| 729 | } |
| 730 | case staticLibTag: |
| 731 | if exportPackage != nil { |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 732 | staticResourcesNodeDepSets = append(staticResourcesNodeDepSets, aarDep.ResourcesNodeDepSet()) |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 733 | rroDirsDepSetBuilder.Transitive(aarDep.RRODirsDepSet()) |
| 734 | manifestsDepSetBuilder.Transitive(aarDep.ManifestsDepSet()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
Ulya Trafimovich | 1855424 | 2020-11-03 15:55:11 +0000 | [diff] [blame] | 737 | |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 738 | addCLCFromDep(ctx, module, classLoaderContexts) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 739 | }) |
| 740 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 741 | // AAPT2 overlays are in lowest to highest priority order, the topological order will be reversed later. |
| 742 | // Reverse the dependency order now going into the depset so that it comes out in order after the second |
| 743 | // reverse later. |
| 744 | // NOTE: this is legacy and probably incorrect behavior, for most other cases (e.g. conflicting classes in |
| 745 | // dependencies) the highest priority dependency is listed first, but for resources the highest priority |
| 746 | // dependency has to be listed last. |
| 747 | staticResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil, |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 748 | android.ReverseSliceInPlace(staticResourcesNodeDepSets)) |
| 749 | sharedResourcesNodes = android.NewDepSet(android.TOPOLOGICAL, nil, |
| 750 | android.ReverseSliceInPlace(sharedResourcesNodeDepSets)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 751 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 752 | staticRRODirs = rroDirsDepSetBuilder.Build() |
| 753 | staticManifests = manifestsDepSetBuilder.Build() |
| 754 | |
| 755 | if len(staticResourcesNodes.ToList()) > 0 { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 756 | flags = append(flags, "--auto-add-overlay") |
| 757 | } |
| 758 | |
| 759 | for _, sharedLib := range sharedLibs { |
| 760 | flags = append(flags, "-I "+sharedLib.String()) |
| 761 | } |
| 762 | |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 763 | return staticResourcesNodes, sharedResourcesNodes, staticRRODirs, staticManifests, sharedLibs, flags |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | type AndroidLibrary struct { |
| 767 | Library |
| 768 | aapt |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 769 | android.BazelModuleBase |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 770 | |
| 771 | androidLibraryProperties androidLibraryProperties |
| 772 | |
| 773 | aarFile android.WritablePath |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Saeid Farivar Asanjan | 1fca301 | 2021-09-14 18:40:19 +0000 | [diff] [blame] | 776 | var _ android.OutputFileProducer = (*AndroidLibrary)(nil) |
| 777 | |
| 778 | // For OutputFileProducer interface |
| 779 | func (a *AndroidLibrary) OutputFiles(tag string) (android.Paths, error) { |
| 780 | switch tag { |
| 781 | case ".aar": |
| 782 | return []android.Path{a.aarFile}, nil |
| 783 | default: |
| 784 | return a.Library.OutputFiles(tag) |
| 785 | } |
| 786 | } |
| 787 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 788 | var _ AndroidLibraryDependency = (*AndroidLibrary)(nil) |
| 789 | |
| 790 | func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 791 | a.Module.deps(ctx) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 792 | sdkDep := decodeSdkDep(ctx, android.SdkContext(a)) |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 793 | if sdkDep.hasFrameworkLibs() { |
| 794 | a.aapt.deps(ctx, sdkDep) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 795 | } |
Colin Cross | 4a80a15 | 2022-12-21 21:51:52 -0800 | [diff] [blame] | 796 | a.usesLibrary.deps(ctx, false) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 800 | a.aapt.isLibrary = true |
Ulya Trafimovich | 42c7f0d | 2021-08-17 16:20:29 +0100 | [diff] [blame] | 801 | a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx) |
Alix | f7a1027 | 2023-09-27 16:47:56 +0000 | [diff] [blame] | 802 | a.aapt.buildActions(ctx, |
| 803 | aaptBuildActionOptions{ |
| 804 | sdkContext: android.SdkContext(a), |
| 805 | classLoaderContexts: a.classLoaderContexts, |
| 806 | enforceDefaultTargetSdkVersion: false, |
| 807 | }, |
| 808 | ) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 809 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 810 | a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() |
| 811 | |
Jihoon Kang | 1bfb6f2 | 2023-07-01 00:13:47 +0000 | [diff] [blame] | 812 | a.stem = proptools.StringDefault(a.overridableDeviceProperties.Stem, ctx.ModuleName()) |
| 813 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 814 | ctx.CheckbuildFile(a.aapt.proguardOptionsFile) |
| 815 | ctx.CheckbuildFile(a.aapt.exportPackage) |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 816 | if a.useResourceProcessorBusyBox() { |
| 817 | ctx.CheckbuildFile(a.aapt.rJar) |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 818 | } else { |
| 819 | ctx.CheckbuildFile(a.aapt.aaptSrcJar) |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 820 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 821 | |
| 822 | // apps manifests are handled by aapt, don't let Module see them |
| 823 | a.properties.Manifest = nil |
| 824 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 825 | a.linter.mergedManifest = a.aapt.mergedManifestFile |
| 826 | a.linter.manifest = a.aapt.manifestPath |
| 827 | a.linter.resources = a.aapt.resourceFiles |
| 828 | |
Sam Delmerico | c8e040c | 2023-10-31 17:27:02 +0000 | [diff] [blame] | 829 | proguardSpecInfo := a.collectProguardSpecInfo(ctx) |
| 830 | ctx.SetProvider(ProguardSpecInfoProvider, proguardSpecInfo) |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 831 | exportedProguardFlagsFiles := proguardSpecInfo.ProguardFlagsFiles.ToList() |
| 832 | a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, exportedProguardFlagsFiles...) |
| 833 | a.extraProguardFlagsFiles = append(a.extraProguardFlagsFiles, a.proguardOptionsFile) |
| 834 | |
| 835 | combinedExportedProguardFlagFile := android.PathForModuleOut(ctx, "export_proguard_flags") |
| 836 | writeCombinedProguardFlagsFile(ctx, combinedExportedProguardFlagFile, exportedProguardFlagsFiles) |
| 837 | a.combinedExportedProguardFlagsFile = combinedExportedProguardFlagFile |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 838 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 839 | var extraSrcJars android.Paths |
| 840 | var extraCombinedJars android.Paths |
| 841 | var extraClasspathJars android.Paths |
| 842 | if a.useResourceProcessorBusyBox() { |
| 843 | // When building a library with ResourceProcessorBusyBox enabled ResourceProcessorBusyBox for this |
| 844 | // library and each of the transitive static android_library dependencies has already created an |
| 845 | // R.class file for the appropriate package. Add all of those R.class files to the classpath. |
| 846 | extraClasspathJars = a.transitiveAaptRJars |
| 847 | } else { |
| 848 | // When building a library without ResourceProcessorBusyBox the aapt2 rule creates R.srcjar containing |
| 849 | // R.java files for the library's package and the packages from all transitive static android_library |
| 850 | // dependencies. Compile the srcjar alongside the rest of the sources. |
| 851 | extraSrcJars = android.Paths{a.aapt.aaptSrcJar} |
| 852 | } |
| 853 | |
| 854 | a.Module.compile(ctx, extraSrcJars, extraClasspathJars, extraCombinedJars) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 855 | |
Colin Cross | f57c578 | 2019-01-25 13:20:38 -0800 | [diff] [blame] | 856 | a.aarFile = android.PathForModuleOut(ctx, ctx.ModuleName()+".aar") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 857 | var res android.Paths |
| 858 | if a.androidLibraryProperties.BuildAAR { |
| 859 | BuildAAR(ctx, a.aarFile, a.outputFile, a.manifestPath, a.rTxt, res) |
| 860 | ctx.CheckbuildFile(a.aarFile) |
| 861 | } |
Colin Cross | 89c3158 | 2018-04-30 15:55:11 -0700 | [diff] [blame] | 862 | |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 863 | prebuiltJniPackages := android.Paths{} |
| 864 | ctx.VisitDirectDeps(func(module android.Module) { |
| 865 | if info, ok := ctx.OtherModuleProvider(module, JniPackageProvider).(JniPackageInfo); ok { |
| 866 | prebuiltJniPackages = append(prebuiltJniPackages, info.JniPackages...) |
| 867 | } |
| 868 | }) |
| 869 | if len(prebuiltJniPackages) > 0 { |
| 870 | ctx.SetProvider(JniPackageProvider, JniPackageInfo{ |
| 871 | JniPackages: prebuiltJniPackages, |
| 872 | }) |
| 873 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Colin Cross | 95b53b8 | 2023-10-17 13:21:02 -0700 | [diff] [blame] | 876 | func (a *AndroidLibrary) IDEInfo(dpInfo *android.IdeInfo) { |
| 877 | a.Library.IDEInfo(dpInfo) |
| 878 | a.aapt.IDEInfo(dpInfo) |
| 879 | } |
| 880 | |
| 881 | func (a *aapt) IDEInfo(dpInfo *android.IdeInfo) { |
| 882 | if a.useResourceProcessorBusyBox() { |
| 883 | dpInfo.Jars = append(dpInfo.Jars, a.rJar.String()) |
| 884 | } |
| 885 | } |
| 886 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 887 | // android_library builds and links sources into a `.jar` file for the device along with Android resources. |
| 888 | // |
| 889 | // An android_library has a single variant that produces a `.jar` file containing `.class` files that were |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 890 | // compiled against the device bootclasspath, along with a `package-res.apk` file containing Android resources compiled |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 891 | // with aapt2. This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of |
| 892 | // an android_app module. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 893 | func AndroidLibraryFactory() android.Module { |
| 894 | module := &AndroidLibrary{} |
| 895 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 896 | module.Module.addHostAndDeviceProperties() |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 897 | module.AddProperties( |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 898 | &module.aaptProperties, |
| 899 | &module.androidLibraryProperties) |
| 900 | |
| 901 | module.androidLibraryProperties.BuildAAR = true |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 902 | module.Module.linter.library = true |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 903 | |
Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 904 | android.InitApexModule(module) |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 905 | InitJavaModule(module, android.DeviceSupported) |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 906 | android.InitBazelModule(module) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 907 | return module |
| 908 | } |
| 909 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 910 | // |
| 911 | // AAR (android library) prebuilts |
| 912 | // |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 913 | |
Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 914 | // Properties for android_library_import |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 915 | type AARImportProperties struct { |
Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 916 | // ARR (android library prebuilt) filepath. Exactly one ARR is required. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 917 | Aars []string `android:"path"` |
Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 918 | // If not blank, set to the version of the sdk to compile against. |
| 919 | // Defaults to private. |
| 920 | // Values are of one of the following forms: |
| 921 | // 1) numerical API level, "current", "none", or "core_platform" |
| 922 | // 2) An SDK kind with an API level: "<sdk kind>_<API level>" |
| 923 | // See build/soong/android/sdk_version.go for the complete and up to date list of SDK kinds. |
| 924 | // If the SDK kind is empty, it will be set to public |
| 925 | Sdk_version *string |
| 926 | // If not blank, set the minimum version of the sdk that the compiled artifacts will run against. |
| 927 | // Defaults to sdk_version if not set. See sdk_version for possible values. |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 928 | Min_sdk_version *string |
Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 929 | // List of java static libraries that the included ARR (android library prebuilts) has dependencies to. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 930 | Static_libs []string |
Vinh Tran | ce0781f | 2022-04-13 01:30:44 +0000 | [diff] [blame] | 931 | // List of java libraries that the included ARR (android library prebuilts) has dependencies to. |
| 932 | Libs []string |
| 933 | // If set to true, run Jetifier against .aar file. Defaults to false. |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 934 | Jetifier *bool |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 935 | // If true, extract JNI libs from AAR archive. These libs will be accessible to android_app modules and |
| 936 | // will be passed transitively through android_libraries to an android_app. |
| 937 | //TODO(b/241138093) evaluate whether we can have this flag default to true for Bazel conversion |
| 938 | Extract_jni *bool |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | type AARImport struct { |
| 942 | android.ModuleBase |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 943 | android.DefaultableModuleBase |
Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 944 | android.ApexModuleBase |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 945 | android.BazelModuleBase |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 946 | prebuilt android.Prebuilt |
| 947 | |
Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 948 | // Functionality common to Module and Import. |
| 949 | embeddableInModuleAndImport |
| 950 | |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 951 | providesTransitiveHeaderJars |
| 952 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 953 | properties AARImportProperties |
| 954 | |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 955 | classpathFile android.WritablePath |
| 956 | proguardFlags android.WritablePath |
| 957 | exportPackage android.WritablePath |
| 958 | transitiveAaptResourcePackagesFile android.Path |
| 959 | extraAaptPackagesFile android.WritablePath |
| 960 | manifest android.WritablePath |
| 961 | assetsPackage android.WritablePath |
| 962 | rTxt android.WritablePath |
| 963 | rJar android.WritablePath |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 964 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 965 | resourcesNodesDepSet *android.DepSet[*resourcesNode] |
| 966 | manifestsDepSet *android.DepSet[android.Path] |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 967 | |
| 968 | hideApexVariantFromMake bool |
Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 969 | |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 970 | aarPath android.Path |
| 971 | jniPackages android.Paths |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 972 | |
| 973 | sdkVersion android.SdkSpec |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 974 | minSdkVersion android.ApiLevel |
Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | var _ android.OutputFileProducer = (*AARImport)(nil) |
| 978 | |
| 979 | // For OutputFileProducer interface |
| 980 | func (a *AARImport) OutputFiles(tag string) (android.Paths, error) { |
| 981 | switch tag { |
| 982 | case ".aar": |
| 983 | return []android.Path{a.aarPath}, nil |
| 984 | case "": |
| 985 | return []android.Path{a.classpathFile}, nil |
| 986 | default: |
| 987 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 988 | } |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 991 | func (a *AARImport) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { |
| 992 | return android.SdkSpecFrom(ctx, String(a.properties.Sdk_version)) |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 995 | func (a *AARImport) SystemModules() string { |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 996 | return "" |
| 997 | } |
| 998 | |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 999 | func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 1000 | if a.properties.Min_sdk_version != nil { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 1001 | return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version) |
Colin Cross | 479884c | 2018-07-10 13:39:30 -0700 | [diff] [blame] | 1002 | } |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 1003 | return a.SdkVersion(ctx).ApiLevel |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 1006 | func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel { |
| 1007 | return android.SdkSpecFrom(ctx, "").ApiLevel |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Spandan Das | ca70fc4 | 2023-03-01 23:38:49 +0000 | [diff] [blame] | 1010 | func (a *AARImport) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { |
| 1011 | return a.SdkVersion(ctx).ApiLevel |
Dan Willemsen | 419290a | 2018-10-31 15:28:47 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 1014 | func (a *AARImport) javaVersion() string { |
| 1015 | return "" |
| 1016 | } |
| 1017 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1018 | var _ AndroidLibraryDependency = (*AARImport)(nil) |
| 1019 | |
| 1020 | func (a *AARImport) ExportPackage() android.Path { |
| 1021 | return a.exportPackage |
| 1022 | } |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1023 | func (a *AARImport) ResourcesNodeDepSet() *android.DepSet[*resourcesNode] { |
| 1024 | return a.resourcesNodesDepSet |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1027 | func (a *AARImport) RRODirsDepSet() *android.DepSet[rroDir] { |
| 1028 | return android.NewDepSet[rroDir](android.TOPOLOGICAL, nil, nil) |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1031 | func (a *AARImport) ManifestsDepSet() *android.DepSet[android.Path] { |
| 1032 | return a.manifestsDepSet |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 1035 | // RRO enforcement is not available on aar_import since its RRO dirs are not |
| 1036 | // exported. |
| 1037 | func (a *AARImport) SetRROEnforcedForDependent(enforce bool) { |
| 1038 | } |
| 1039 | |
| 1040 | // RRO enforcement is not available on aar_import since its RRO dirs are not |
| 1041 | // exported. |
| 1042 | func (a *AARImport) IsRROEnforced(ctx android.BaseModuleContext) bool { |
| 1043 | return false |
| 1044 | } |
| 1045 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1046 | func (a *AARImport) Prebuilt() *android.Prebuilt { |
| 1047 | return &a.prebuilt |
| 1048 | } |
| 1049 | |
| 1050 | func (a *AARImport) Name() string { |
| 1051 | return a.prebuilt.Name(a.ModuleBase.Name()) |
| 1052 | } |
| 1053 | |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 1054 | func (a *AARImport) JacocoReportClassesFile() android.Path { |
| 1055 | return nil |
| 1056 | } |
| 1057 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1058 | func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 1059 | if !ctx.Config().AlwaysUsePrebuiltSdks() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1060 | sdkDep := decodeSdkDep(ctx, android.SdkContext(a)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1061 | if sdkDep.useModule && sdkDep.frameworkResModule != "" { |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 1062 | ctx.AddVariationDependencies(nil, frameworkResTag, sdkDep.frameworkResModule) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1065 | |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 1066 | ctx.AddVariationDependencies(nil, libTag, a.properties.Libs...) |
| 1067 | ctx.AddVariationDependencies(nil, staticLibTag, a.properties.Static_libs...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1070 | type JniPackageInfo struct { |
| 1071 | // List of zip files containing JNI libraries |
| 1072 | // Zip files should have directory structure jni/<arch>/*.so |
| 1073 | JniPackages android.Paths |
| 1074 | } |
| 1075 | |
| 1076 | var JniPackageProvider = blueprint.NewProvider(JniPackageInfo{}) |
| 1077 | |
| 1078 | // Unzip an AAR and extract the JNI libs for $archString. |
| 1079 | var extractJNI = pctx.AndroidStaticRule("extractJNI", |
| 1080 | blueprint.RuleParams{ |
| 1081 | Command: `rm -rf $out $outDir && touch $out && ` + |
| 1082 | `unzip -qoDD -d $outDir $in "jni/${archString}/*" && ` + |
| 1083 | `jni_files=$$(find $outDir/jni -type f) && ` + |
| 1084 | // print error message if there are no JNI libs for this arch |
| 1085 | `[ -n "$$jni_files" ] || (echo "ERROR: no JNI libs found for arch ${archString}" && exit 1) && ` + |
Sam Delmerico | 80ee45c | 2023-06-22 15:36:02 -0400 | [diff] [blame] | 1086 | `${config.SoongZipCmd} -o $out -L 0 -P 'lib/${archString}' ` + |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1087 | `-C $outDir/jni/${archString} $$(echo $$jni_files | xargs -n1 printf " -f %s")`, |
| 1088 | CommandDeps: []string{"${config.SoongZipCmd}"}, |
| 1089 | }, |
| 1090 | "outDir", "archString") |
| 1091 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1092 | // Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 1093 | // touched to create an empty file. The res directory is not extracted, as it will be extracted in its own rule. |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1094 | var unzipAAR = pctx.AndroidStaticRule("unzipAAR", |
| 1095 | blueprint.RuleParams{ |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 1096 | Command: `rm -rf $outDir && mkdir -p $outDir && ` + |
Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1097 | `unzip -qoDD -d $outDir $in && rm -rf $outDir/res && touch $out && ` + |
Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1098 | `${config.Zip2ZipCmd} -i $in -o $assetsPackage 'assets/**/*' && ` + |
Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1099 | `${config.MergeZipsCmd} $combinedClassesJar $$(ls $outDir/classes.jar 2> /dev/null) $$(ls $outDir/libs/*.jar 2> /dev/null)`, |
Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1100 | CommandDeps: []string{"${config.MergeZipsCmd}", "${config.Zip2ZipCmd}"}, |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1101 | }, |
Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1102 | "outDir", "combinedClassesJar", "assetsPackage") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1103 | |
| 1104 | func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1105 | if len(a.properties.Aars) != 1 { |
| 1106 | ctx.PropertyErrorf("aars", "exactly one aar is required") |
| 1107 | return |
| 1108 | } |
| 1109 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1110 | a.sdkVersion = a.SdkVersion(ctx) |
| 1111 | a.minSdkVersion = a.MinSdkVersion(ctx) |
| 1112 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1113 | a.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() |
| 1114 | |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1115 | aarName := ctx.ModuleName() + ".aar" |
Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1116 | a.aarPath = android.PathForModuleSrc(ctx, a.properties.Aars[0]) |
| 1117 | |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 1118 | if Bool(a.properties.Jetifier) { |
Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1119 | inputFile := a.aarPath |
| 1120 | a.aarPath = android.PathForModuleOut(ctx, "jetifier", aarName) |
| 1121 | TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile) |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1122 | } |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1123 | |
| 1124 | extractedAARDir := android.PathForModuleOut(ctx, "aar") |
Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1125 | a.classpathFile = extractedAARDir.Join(ctx, "classes-combined.jar") |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 1126 | a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml") |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1127 | aarRTxt := extractedAARDir.Join(ctx, "R.txt") |
Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1128 | a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip") |
Sam Delmerico | 95d7094 | 2023-08-02 18:00:35 -0400 | [diff] [blame] | 1129 | a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt") |
| 1130 | ctx.SetProvider(ProguardSpecInfoProvider, ProguardSpecInfo{ |
| 1131 | ProguardFlagsFiles: android.NewDepSet[android.Path]( |
| 1132 | android.POSTORDER, |
| 1133 | android.Paths{a.proguardFlags}, |
| 1134 | nil, |
| 1135 | ), |
| 1136 | }) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1137 | |
| 1138 | ctx.Build(pctx, android.BuildParams{ |
| 1139 | Rule: unzipAAR, |
Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1140 | Input: a.aarPath, |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1141 | Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest, a.assetsPackage, aarRTxt}, |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1142 | Description: "unzip AAR", |
| 1143 | Args: map[string]string{ |
Colin Cross | 205e911 | 2020-08-06 13:20:17 -0700 | [diff] [blame] | 1144 | "outDir": extractedAARDir.String(), |
| 1145 | "combinedClassesJar": a.classpathFile.String(), |
Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1146 | "assetsPackage": a.assetsPackage.String(), |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1147 | }, |
| 1148 | }) |
| 1149 | |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 1150 | // Always set --pseudo-localize, it will be stripped out later for release |
| 1151 | // builds that don't want it. |
| 1152 | compileFlags := []string{"--pseudo-localize"} |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1153 | compiledResDir := android.PathForModuleOut(ctx, "flat-res") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1154 | flata := compiledResDir.Join(ctx, "gen_res.flata") |
Saeid Farivar Asanjan | f043696 | 2020-10-05 19:09:09 +0000 | [diff] [blame] | 1155 | aapt2CompileZip(ctx, flata, a.aarPath, "res", compileFlags) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1156 | |
| 1157 | a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1158 | proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options") |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1159 | a.rTxt = android.PathForModuleOut(ctx, "R.txt") |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 1160 | a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages") |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1161 | |
| 1162 | var linkDeps android.Paths |
| 1163 | |
| 1164 | linkFlags := []string{ |
| 1165 | "--static-lib", |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1166 | "--merge-only", |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1167 | "--auto-add-overlay", |
| 1168 | } |
| 1169 | |
Colin Cross | 10f7c4a | 2018-05-23 10:59:28 -0700 | [diff] [blame] | 1170 | linkFlags = append(linkFlags, "--manifest "+a.manifest.String()) |
| 1171 | linkDeps = append(linkDeps, a.manifest) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1172 | |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 1173 | staticResourcesNodesDepSet, sharedResourcesNodesDepSet, staticRRODirsDepSet, staticManifestsDepSet, sharedLibs, libFlags := |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1174 | aaptLibs(ctx, android.SdkContext(a), nil) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 1175 | |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 1176 | _ = sharedResourcesNodesDepSet |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1177 | _ = staticRRODirsDepSet |
Colin Cross | 8676c8c | 2023-10-12 15:58:57 -0700 | [diff] [blame] | 1178 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1179 | staticDeps := transitiveAarDeps(staticResourcesNodesDepSet.ToList()) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1180 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1181 | linkDeps = append(linkDeps, sharedLibs...) |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1182 | linkDeps = append(linkDeps, staticDeps.resPackages()...) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1183 | linkFlags = append(linkFlags, libFlags...) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1184 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1185 | overlayRes := android.Paths{flata} |
| 1186 | |
| 1187 | // Treat static library dependencies of static libraries as imports. |
| 1188 | transitiveStaticLibs := staticDeps.resPackages() |
| 1189 | linkDeps = append(linkDeps, transitiveStaticLibs...) |
| 1190 | for _, staticLib := range transitiveStaticLibs { |
| 1191 | linkFlags = append(linkFlags, "-I "+staticLib.String()) |
| 1192 | } |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1193 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1194 | transitiveAssets := android.ReverseSliceInPlace(staticDeps.assets()) |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 1195 | aapt2Link(ctx, a.exportPackage, nil, proguardOptionsFile, a.rTxt, |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 1196 | linkFlags, linkDeps, nil, overlayRes, transitiveAssets, nil) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1197 | |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1198 | a.rJar = android.PathForModuleOut(ctx, "busybox/R.jar") |
| 1199 | resourceProcessorBusyBoxGenerateBinaryR(ctx, a.rTxt, a.manifest, a.rJar, nil, true) |
| 1200 | |
Colin Cross | f3b7bad | 2023-08-02 15:49:00 -0700 | [diff] [blame] | 1201 | aapt2ExtractExtraPackages(ctx, a.extraAaptPackagesFile, a.rJar) |
| 1202 | |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1203 | resourcesNodesDepSetBuilder := android.NewDepSetBuilder[*resourcesNode](android.TOPOLOGICAL) |
| 1204 | resourcesNodesDepSetBuilder.Direct(&resourcesNode{ |
| 1205 | resPackage: a.exportPackage, |
| 1206 | manifest: a.manifest, |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1207 | rTxt: a.rTxt, |
| 1208 | rJar: a.rJar, |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1209 | assets: android.OptionalPathForPath(a.assetsPackage), |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1210 | |
| 1211 | usedResourceProcessor: true, |
Colin Cross | ab8d138 | 2023-07-14 17:23:41 +0000 | [diff] [blame] | 1212 | }) |
| 1213 | resourcesNodesDepSetBuilder.Transitive(staticResourcesNodesDepSet) |
| 1214 | a.resourcesNodesDepSet = resourcesNodesDepSetBuilder.Build() |
| 1215 | |
| 1216 | manifestDepSetBuilder := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(a.manifest) |
| 1217 | // TODO(b/288358614): Soong has historically not merged manifests from dependencies of android_library_import |
| 1218 | // modules. Merging manifests from dependencies could remove the need for pom2bp to generate the "-nodeps" copies |
| 1219 | // of androidx libraries, but doing so triggers errors due to errors introduced by existing dependencies of |
| 1220 | // android_library_import modules. If this is fixed, AndroidLibraryDependency.ManifestsDepSet can be dropped |
| 1221 | // completely in favor of AndroidLibraryDependency.ResourceNodesDepSet.manifest |
| 1222 | //manifestDepSetBuilder.Transitive(transitiveStaticDeps.manifests) |
| 1223 | _ = staticManifestsDepSet |
| 1224 | a.manifestsDepSet = manifestDepSetBuilder.Build() |
Michael Rosenfeld | 5ad1557 | 2021-12-03 13:25:10 -0800 | [diff] [blame] | 1225 | |
Colin Cross | 312634e | 2023-11-21 15:13:56 -0800 | [diff] [blame^] | 1226 | transitiveAaptResourcePackages := staticDeps.resPackages().Strings() |
| 1227 | transitiveAaptResourcePackages = slices.DeleteFunc(transitiveAaptResourcePackages, func(p string) bool { |
| 1228 | return p == a.exportPackage.String() |
| 1229 | }) |
| 1230 | transitiveAaptResourcePackagesFile := android.PathForModuleOut(ctx, "transitive-res-packages") |
| 1231 | android.WriteFileRule(ctx, transitiveAaptResourcePackagesFile, strings.Join(transitiveAaptResourcePackages, "\n")) |
| 1232 | a.transitiveAaptResourcePackagesFile = transitiveAaptResourcePackagesFile |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 1233 | |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 1234 | a.collectTransitiveHeaderJars(ctx) |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1235 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
| 1236 | HeaderJars: android.PathsIfNonNil(a.classpathFile), |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 1237 | TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars, |
| 1238 | TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars, |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1239 | ImplementationAndResourcesJars: android.PathsIfNonNil(a.classpathFile), |
| 1240 | ImplementationJars: android.PathsIfNonNil(a.classpathFile), |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 1241 | // TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1242 | }) |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1243 | |
| 1244 | if proptools.Bool(a.properties.Extract_jni) { |
| 1245 | for _, t := range ctx.MultiTargets() { |
| 1246 | arch := t.Arch.Abi[0] |
| 1247 | path := android.PathForModuleOut(ctx, arch+"_jni.zip") |
| 1248 | a.jniPackages = append(a.jniPackages, path) |
| 1249 | |
| 1250 | outDir := android.PathForModuleOut(ctx, "aarForJni") |
| 1251 | aarPath := android.PathForModuleSrc(ctx, a.properties.Aars[0]) |
| 1252 | ctx.Build(pctx, android.BuildParams{ |
| 1253 | Rule: extractJNI, |
| 1254 | Input: aarPath, |
| 1255 | Outputs: android.WritablePaths{path}, |
| 1256 | Description: "extract JNI from AAR", |
| 1257 | Args: map[string]string{ |
| 1258 | "outDir": outDir.String(), |
| 1259 | "archString": arch, |
| 1260 | }, |
| 1261 | }) |
| 1262 | } |
| 1263 | |
| 1264 | ctx.SetProvider(JniPackageProvider, JniPackageInfo{ |
| 1265 | JniPackages: a.jniPackages, |
| 1266 | }) |
| 1267 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1268 | } |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1269 | |
| 1270 | func (a *AARImport) HeaderJars() android.Paths { |
| 1271 | return android.Paths{a.classpathFile} |
| 1272 | } |
| 1273 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 1274 | func (a *AARImport) ImplementationAndResourcesJars() android.Paths { |
| 1275 | return android.Paths{a.classpathFile} |
| 1276 | } |
| 1277 | |
Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 1278 | func (a *AARImport) DexJarBuildPath() android.Path { |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 1279 | return nil |
| 1280 | } |
| 1281 | |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 1282 | func (a *AARImport) DexJarInstallPath() android.Path { |
| 1283 | return nil |
| 1284 | } |
| 1285 | |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 1286 | func (a *AARImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1287 | return nil |
| 1288 | } |
| 1289 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 1290 | var _ android.ApexModule = (*AARImport)(nil) |
| 1291 | |
| 1292 | // Implements android.ApexModule |
Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1293 | func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { |
| 1294 | return a.depIsInSameApex(ctx, dep) |
| 1295 | } |
| 1296 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 1297 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 1298 | func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 1299 | sdkVersion android.ApiLevel) error { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 1300 | return nil |
| 1301 | } |
| 1302 | |
Sam Delmerico | af8bb70 | 2022-07-25 15:39:32 -0400 | [diff] [blame] | 1303 | var _ android.PrebuiltInterface = (*AARImport)(nil) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1304 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1305 | // android_library_import imports an `.aar` file into the build graph as if it was built with android_library. |
| 1306 | // |
| 1307 | // This module is not suitable for installing on a device, but can be used as a `static_libs` dependency of |
| 1308 | // an android_app module. |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1309 | func AARImportFactory() android.Module { |
| 1310 | module := &AARImport{} |
| 1311 | |
| 1312 | module.AddProperties(&module.properties) |
| 1313 | |
| 1314 | android.InitPrebuiltModule(module, &module.properties.Aars) |
Jooyung Han | acc7bbe | 2020-05-20 09:06:00 +0900 | [diff] [blame] | 1315 | android.InitApexModule(module) |
Sam Delmerico | 8260249 | 2022-06-10 17:05:42 +0000 | [diff] [blame] | 1316 | InitJavaModuleMultiTargets(module, android.DeviceSupported) |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1317 | android.InitBazelModule(module) |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 1318 | return module |
| 1319 | } |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1320 | |
| 1321 | type bazelAapt struct { |
| 1322 | Manifest bazel.Label |
| 1323 | Resource_files bazel.LabelListAttribute |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 1324 | Resource_zips bazel.LabelListAttribute |
Romain Jobredeaux | 7a71e07 | 2023-08-16 17:39:12 -0400 | [diff] [blame] | 1325 | Assets_dir bazel.StringAttribute |
| 1326 | Assets bazel.LabelListAttribute |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | type bazelAndroidLibrary struct { |
| 1330 | *javaLibraryAttributes |
| 1331 | *bazelAapt |
| 1332 | } |
| 1333 | |
| 1334 | type bazelAndroidLibraryImport struct { |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 1335 | Aar bazel.Label |
| 1336 | Deps bazel.LabelListAttribute |
| 1337 | Exports bazel.LabelListAttribute |
| 1338 | Sdk_version bazel.StringAttribute |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1339 | } |
| 1340 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 1341 | func (a *aapt) convertAaptAttrsWithBp2Build(ctx android.Bp2buildMutatorContext) (*bazelAapt, bool) { |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1342 | manifest := proptools.StringDefault(a.aaptProperties.Manifest, "AndroidManifest.xml") |
| 1343 | |
| 1344 | resourceFiles := bazel.LabelList{ |
| 1345 | Includes: []bazel.Label{}, |
| 1346 | } |
| 1347 | for _, dir := range android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Resource_dirs, "res") { |
| 1348 | files := android.RootToModuleRelativePaths(ctx, androidResourceGlob(ctx, dir)) |
| 1349 | resourceFiles.Includes = append(resourceFiles.Includes, files...) |
| 1350 | } |
Romain Jobredeaux | 7a71e07 | 2023-08-16 17:39:12 -0400 | [diff] [blame] | 1351 | |
| 1352 | assetsDir := bazel.StringAttribute{} |
| 1353 | var assets bazel.LabelList |
| 1354 | for i, dir := range android.PathsWithOptionalDefaultForModuleSrc(ctx, a.aaptProperties.Asset_dirs, "assets") { |
| 1355 | if i > 0 { |
| 1356 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "multiple asset_dirs") |
| 1357 | return &bazelAapt{}, false |
| 1358 | } |
| 1359 | // Assets_dirs are relative to the module dir when specified, but if the default in used in |
| 1360 | // PathsWithOptionalDefaultForModuleSrc, then dir is relative to the top. |
| 1361 | assetsRelDir, error := filepath.Rel(ctx.ModuleDir(), dir.Rel()) |
| 1362 | if error != nil { |
| 1363 | assetsRelDir = dir.Rel() |
| 1364 | } |
| 1365 | assetsDir.Value = proptools.StringPtr(assetsRelDir) |
| 1366 | assets = bazel.MakeLabelList(android.RootToModuleRelativePaths(ctx, androidResourceGlob(ctx, dir))) |
| 1367 | |
| 1368 | } |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 1369 | var resourceZips bazel.LabelList |
| 1370 | if len(a.aaptProperties.Resource_zips) > 0 { |
| 1371 | if ctx.ModuleName() == "framework-res" { |
| 1372 | resourceZips = android.BazelLabelForModuleSrc(ctx, a.aaptProperties.Resource_zips) |
| 1373 | } else { |
| 1374 | //TODO: b/301593550 - Implement support for this |
| 1375 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "resource_zips") |
| 1376 | return &bazelAapt{}, false |
| 1377 | } |
| 1378 | } |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1379 | return &bazelAapt{ |
| 1380 | android.BazelLabelForModuleSrcSingle(ctx, manifest), |
| 1381 | bazel.MakeLabelListAttribute(resourceFiles), |
Romain Jobredeaux | 9c06ef3 | 2023-08-21 18:05:29 -0400 | [diff] [blame] | 1382 | bazel.MakeLabelListAttribute(resourceZips), |
Romain Jobredeaux | 7a71e07 | 2023-08-16 17:39:12 -0400 | [diff] [blame] | 1383 | assetsDir, |
| 1384 | bazel.MakeLabelListAttribute(assets), |
| 1385 | }, true |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1386 | } |
| 1387 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 1388 | func (a *AARImport) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Romain Jobredeaux | afc5d27 | 2023-09-21 11:07:30 -0400 | [diff] [blame] | 1389 | if len(a.properties.Aars) == 0 { |
| 1390 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "aars can't be empty") |
| 1391 | return |
| 1392 | } |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1393 | aars := android.BazelLabelForModuleSrcExcludes(ctx, a.properties.Aars, []string{}) |
| 1394 | exportableStaticLibs := []string{} |
| 1395 | // TODO(b/240716882): investigate and handle static_libs deps that are not imports. They are not supported for export by Bazel. |
| 1396 | for _, depName := range a.properties.Static_libs { |
| 1397 | if dep, ok := ctx.ModuleFromName(depName); ok { |
| 1398 | switch dep.(type) { |
| 1399 | case *AARImport, *Import: |
| 1400 | exportableStaticLibs = append(exportableStaticLibs, depName) |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | name := android.RemoveOptionalPrebuiltPrefix(a.Name()) |
| 1405 | deps := android.BazelLabelForModuleDeps(ctx, android.LastUniqueStrings(android.CopyOf(append(a.properties.Static_libs, a.properties.Libs...)))) |
| 1406 | exports := android.BazelLabelForModuleDeps(ctx, android.LastUniqueStrings(exportableStaticLibs)) |
| 1407 | |
| 1408 | ctx.CreateBazelTargetModule( |
| 1409 | bazel.BazelTargetModuleProperties{ |
| 1410 | Rule_class: "aar_import", |
Alix | a381cd1 | 2023-05-10 14:49:38 +0000 | [diff] [blame] | 1411 | Bzl_load_location: "//build/bazel/rules/android:aar_import.bzl", |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1412 | }, |
| 1413 | android.CommonAttributes{Name: name}, |
| 1414 | &bazelAndroidLibraryImport{ |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 1415 | Aar: aars.Includes[0], |
| 1416 | Deps: bazel.MakeLabelListAttribute(deps), |
| 1417 | Exports: bazel.MakeLabelListAttribute(exports), |
| 1418 | Sdk_version: bazel.StringAttribute{Value: a.properties.Sdk_version}, |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1419 | }, |
| 1420 | ) |
| 1421 | |
Alix | 14101de | 2023-01-06 03:42:07 +0000 | [diff] [blame] | 1422 | neverlink := true |
| 1423 | ctx.CreateBazelTargetModule( |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 1424 | AndroidLibraryBazelTargetModuleProperties(), |
Alix | 14101de | 2023-01-06 03:42:07 +0000 | [diff] [blame] | 1425 | android.CommonAttributes{Name: name + "-neverlink"}, |
| 1426 | &bazelAndroidLibrary{ |
| 1427 | javaLibraryAttributes: &javaLibraryAttributes{ |
| 1428 | Neverlink: bazel.BoolAttribute{Value: &neverlink}, |
| 1429 | Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}), |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 1430 | javaCommonAttributes: &javaCommonAttributes{ |
| 1431 | Sdk_version: bazel.StringAttribute{Value: a.properties.Sdk_version}, |
| 1432 | }, |
Alix | 14101de | 2023-01-06 03:42:07 +0000 | [diff] [blame] | 1433 | }, |
| 1434 | }, |
| 1435 | ) |
| 1436 | |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1437 | } |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 1438 | func AndroidLibraryBazelTargetModuleProperties() bazel.BazelTargetModuleProperties { |
| 1439 | return bazel.BazelTargetModuleProperties{ |
| 1440 | Rule_class: "android_library", |
Alix | a381cd1 | 2023-05-10 14:49:38 +0000 | [diff] [blame] | 1441 | Bzl_load_location: "//build/bazel/rules/android:android_library.bzl", |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1444 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 1445 | func (a *AndroidLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 1446 | commonAttrs, bp2buildInfo, supported := a.convertLibraryAttrsBp2Build(ctx) |
| 1447 | if !supported { |
| 1448 | return |
| 1449 | } |
| 1450 | |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 1451 | depLabels := bp2buildInfo.DepLabels |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1452 | |
| 1453 | deps := depLabels.Deps |
| 1454 | if !commonAttrs.Srcs.IsEmpty() { |
| 1455 | deps.Append(depLabels.StaticDeps) // we should only append these if there are sources to use them |
| 1456 | } else if !depLabels.Deps.IsEmpty() { |
Zi Wang | 1066479 | 2023-09-27 12:29:34 -0700 | [diff] [blame] | 1457 | // android_library does not accept deps when there are no srcs because |
| 1458 | // there is no compilation happening, but it accepts exports. |
| 1459 | // The non-empty deps here are unnecessary as deps on the android_library |
| 1460 | // since they aren't being propagated to any dependencies. |
| 1461 | // So we can drop deps here. |
| 1462 | deps = bazel.LabelListAttribute{} |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1463 | } |
Alix | 82fb94e | 2022-10-26 20:40:18 +0000 | [diff] [blame] | 1464 | name := a.Name() |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 1465 | props := AndroidLibraryBazelTargetModuleProperties() |
Alix | 82fb94e | 2022-10-26 20:40:18 +0000 | [diff] [blame] | 1466 | |
Romain Jobredeaux | 7a71e07 | 2023-08-16 17:39:12 -0400 | [diff] [blame] | 1467 | aaptAttrs, supported := a.convertAaptAttrsWithBp2Build(ctx) |
| 1468 | if !supported { |
| 1469 | return |
| 1470 | } |
Alix | ee51bd6 | 2023-08-29 16:01:46 +0000 | [diff] [blame] | 1471 | if hasJavaResources := aaptAttrs.ConvertJavaResources(ctx, commonAttrs); hasJavaResources { |
| 1472 | return |
| 1473 | } |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1474 | ctx.CreateBazelTargetModule( |
Alix | 82fb94e | 2022-10-26 20:40:18 +0000 | [diff] [blame] | 1475 | props, |
| 1476 | android.CommonAttributes{Name: name}, |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1477 | &bazelAndroidLibrary{ |
| 1478 | &javaLibraryAttributes{ |
| 1479 | javaCommonAttributes: commonAttrs, |
| 1480 | Deps: deps, |
| 1481 | Exports: depLabels.StaticDeps, |
| 1482 | }, |
Romain Jobredeaux | 7a71e07 | 2023-08-16 17:39:12 -0400 | [diff] [blame] | 1483 | aaptAttrs, |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1484 | }, |
| 1485 | ) |
Alix | 82fb94e | 2022-10-26 20:40:18 +0000 | [diff] [blame] | 1486 | |
| 1487 | neverlink := true |
| 1488 | ctx.CreateBazelTargetModule( |
| 1489 | props, |
| 1490 | android.CommonAttributes{Name: name + "-neverlink"}, |
| 1491 | &bazelAndroidLibrary{ |
| 1492 | javaLibraryAttributes: &javaLibraryAttributes{ |
| 1493 | Neverlink: bazel.BoolAttribute{Value: &neverlink}, |
| 1494 | Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}), |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 1495 | javaCommonAttributes: &javaCommonAttributes{ |
| 1496 | Sdk_version: bazel.StringAttribute{Value: a.deviceProperties.Sdk_version}, |
| 1497 | Java_version: bazel.StringAttribute{Value: a.properties.Java_version}, |
| 1498 | }, |
Alix | 82fb94e | 2022-10-26 20:40:18 +0000 | [diff] [blame] | 1499 | }, |
| 1500 | }, |
| 1501 | ) |
Romain Jobredeaux | c9b2bba | 2022-02-15 09:35:07 -0500 | [diff] [blame] | 1502 | } |