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