Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [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 | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 18 | "fmt" |
Spandan Das | c8cb0c3 | 2021-12-08 17:00:38 +0000 | [diff] [blame] | 19 | "strconv" |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 20 | "strings" |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | |
| 24 | "android/soong/android" |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 25 | "android/soong/dexpreopt" |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer", |
| 29 | blueprint.RuleParams{ |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 30 | Command: `${config.ManifestFixerCmd} ` + |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 31 | `$args $in $out`, |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 32 | CommandDeps: []string{"${config.ManifestFixerCmd}"}, |
| 33 | }, |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 34 | "args") |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 35 | |
| 36 | var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger", |
| 37 | blueprint.RuleParams{ |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 38 | Command: `${config.ManifestMergerCmd} $args --main $in $libs --out $out`, |
Colin Cross | 87ecbfe | 2019-02-26 22:14:04 -0800 | [diff] [blame] | 39 | CommandDeps: []string{"${config.ManifestMergerCmd}"}, |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 40 | }, |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 41 | "args", "libs") |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 42 | |
Spandan Das | c8cb0c3 | 2021-12-08 17:00:38 +0000 | [diff] [blame] | 43 | // targetSdkVersion for manifest_fixer |
| 44 | // When TARGET_BUILD_APPS is not empty, this method returns 10000 for modules targeting an unreleased SDK |
| 45 | // This enables release builds (that run with TARGET_BUILD_APPS=[val...]) to target APIs that have not yet been finalized as part of an SDK |
| 46 | func targetSdkVersionForManifestFixer(ctx android.ModuleContext, sdkContext android.SdkContext) string { |
| 47 | targetSdkVersionSpec := sdkContext.TargetSdkVersion(ctx) |
| 48 | if ctx.Config().UnbundledBuildApps() && targetSdkVersionSpec.ApiLevel.IsPreview() { |
| 49 | return strconv.Itoa(android.FutureApiLevel.FinalOrFutureInt()) |
| 50 | } |
| 51 | targetSdkVersion, err := targetSdkVersionSpec.EffectiveVersionString(ctx) |
| 52 | if err != nil { |
| 53 | ctx.ModuleErrorf("invalid targetSdkVersion: %s", err) |
| 54 | } |
| 55 | return targetSdkVersion |
| 56 | } |
| 57 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 58 | type ManifestFixerParams struct { |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 59 | SdkContext android.SdkContext |
| 60 | ClassLoaderContexts dexpreopt.ClassLoaderContextMap |
| 61 | IsLibrary bool |
| 62 | UseEmbeddedNativeLibs bool |
| 63 | UsesNonSdkApis bool |
| 64 | UseEmbeddedDex bool |
| 65 | HasNoCode bool |
| 66 | TestOnly bool |
| 67 | LoggingParent string |
| 68 | } |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 69 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 70 | // Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 71 | func ManifestFixer(ctx android.ModuleContext, manifest android.Path, |
| 72 | params ManifestFixerParams) android.Path { |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 73 | var args []string |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 74 | |
| 75 | if params.IsLibrary { |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 76 | args = append(args, "--library") |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 77 | } else if params.SdkContext != nil { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 78 | minSdkVersion, err := params.SdkContext.MinSdkVersion(ctx).EffectiveVersion(ctx) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 79 | if err != nil { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 80 | ctx.ModuleErrorf("invalid minSdkVersion: %s", err) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 81 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 82 | if minSdkVersion.FinalOrFutureInt() >= 23 { |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 83 | args = append(args, fmt.Sprintf("--extract-native-libs=%v", !params.UseEmbeddedNativeLibs)) |
| 84 | } else if params.UseEmbeddedNativeLibs { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 85 | ctx.ModuleErrorf("module attempted to store uncompressed native libraries, but minSdkVersion=%d doesn't support it", |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 86 | minSdkVersion) |
| 87 | } |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 88 | } |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 89 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 90 | if params.UsesNonSdkApis { |
David Brazdil | d25060a | 2019-02-18 18:24:16 +0000 | [diff] [blame] | 91 | args = append(args, "--uses-non-sdk-api") |
| 92 | } |
| 93 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 94 | if params.UseEmbeddedDex { |
Sasha Smundak | d2e5361 | 2019-05-14 17:03:24 -0700 | [diff] [blame] | 95 | args = append(args, "--use-embedded-dex") |
Colin Cross | 46abdad | 2019-02-07 13:07:08 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 98 | if params.ClassLoaderContexts != nil { |
| 99 | // manifest_fixer should add only the implicit SDK libraries inferred by Soong, not those added |
| 100 | // explicitly via `uses_libs`/`optional_uses_libs`. |
| 101 | requiredUsesLibs, optionalUsesLibs := params.ClassLoaderContexts.ImplicitUsesLibs() |
| 102 | |
| 103 | for _, usesLib := range requiredUsesLibs { |
| 104 | args = append(args, "--uses-library", usesLib) |
| 105 | } |
| 106 | for _, usesLib := range optionalUsesLibs { |
| 107 | args = append(args, "--optional-uses-library", usesLib) |
| 108 | } |
Colin Cross | 5446e88 | 2019-05-22 10:46:27 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 111 | if params.HasNoCode { |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 112 | args = append(args, "--has-no-code") |
| 113 | } |
| 114 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 115 | if params.TestOnly { |
| 116 | args = append(args, "--test-only") |
| 117 | } |
| 118 | |
| 119 | if params.LoggingParent != "" { |
| 120 | args = append(args, "--logging-parent", params.LoggingParent) |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 121 | } |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 122 | var deps android.Paths |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 123 | var argsMapper = make(map[string]string) |
Spandan Das | c8cb0c3 | 2021-12-08 17:00:38 +0000 | [diff] [blame] | 124 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 125 | if params.SdkContext != nil { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 126 | targetSdkVersion := targetSdkVersionForManifestFixer(ctx, params.SdkContext) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 127 | args = append(args, "--targetSdkVersion ", targetSdkVersion) |
| 128 | |
Rashed Abdel-Tawab | 8782f15 | 2018-08-09 14:08:53 -0700 | [diff] [blame^] | 129 | if UseApiFingerprint(ctx) && ctx.ModuleName() != "framework-res" && ctx.ModuleName() != "org.lineageos.platform-res" { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 130 | targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", ApiFingerprintPath(ctx).String()) |
| 131 | deps = append(deps, ApiFingerprintPath(ctx)) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 134 | minSdkVersion, err := params.SdkContext.MinSdkVersion(ctx).EffectiveVersionString(ctx) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 135 | if err != nil { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 136 | ctx.ModuleErrorf("invalid minSdkVersion: %s", err) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Rashed Abdel-Tawab | 8782f15 | 2018-08-09 14:08:53 -0700 | [diff] [blame^] | 139 | if UseApiFingerprint(ctx) && ctx.ModuleName() != "framework-res" && ctx.ModuleName() != "org.lineageos.platform-res" { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 140 | minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", ApiFingerprintPath(ctx).String()) |
| 141 | deps = append(deps, ApiFingerprintPath(ctx)) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if err != nil { |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 145 | ctx.ModuleErrorf("invalid minSdkVersion: %s", err) |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 146 | } |
| 147 | args = append(args, "--minSdkVersion ", minSdkVersion) |
| 148 | args = append(args, "--raise-min-sdk-version") |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 149 | } |
| 150 | |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 151 | fixedManifest := android.PathForModuleOut(ctx, "manifest_fixer", "AndroidManifest.xml") |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 152 | argsMapper["args"] = strings.Join(args, " ") |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 153 | |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 154 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 155 | Rule: manifestFixerRule, |
| 156 | Description: "fix manifest", |
Gurpreet Singh | 7deabfa | 2022-02-10 13:28:35 +0000 | [diff] [blame] | 157 | Input: manifest, |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 158 | Implicits: deps, |
| 159 | Output: fixedManifest, |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 160 | Args: argsMapper, |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 161 | }) |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 162 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 163 | return fixedManifest.WithoutRel() |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 164 | } |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 165 | |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 166 | func manifestMerger(ctx android.ModuleContext, manifest android.Path, staticLibManifests android.Paths, |
| 167 | isLibrary bool) android.Path { |
| 168 | |
| 169 | var args string |
| 170 | if !isLibrary { |
| 171 | // Follow Gradle's behavior, only pass --remove-tools-declarations when merging app manifests. |
| 172 | args = "--remove-tools-declarations" |
| 173 | } |
| 174 | |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 175 | mergedManifest := android.PathForModuleOut(ctx, "manifest_merger", "AndroidManifest.xml") |
| 176 | ctx.Build(pctx, android.BuildParams{ |
| 177 | Rule: manifestMergerRule, |
| 178 | Description: "merge manifest", |
| 179 | Input: manifest, |
| 180 | Implicits: staticLibManifests, |
| 181 | Output: mergedManifest, |
| 182 | Args: map[string]string{ |
| 183 | "libs": android.JoinWithPrefix(staticLibManifests.Strings(), "--libs "), |
Colin Cross | aa1c6f1 | 2019-04-19 16:25:38 -0700 | [diff] [blame] | 184 | "args": args, |
Colin Cross | 90c25c6 | 2019-04-19 16:22:57 -0700 | [diff] [blame] | 185 | }, |
| 186 | }) |
| 187 | |
Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 188 | return mergedManifest.WithoutRel() |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 189 | } |