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