Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 ( |
| 18 | "path/filepath" |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 19 | "sort" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 28 | // Convert input resource file path to output file path. |
| 29 | // values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat; |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 30 | // For other resource file, just replace the last "/" with "_" and add .flat extension. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 31 | func pathToAapt2Path(ctx android.ModuleContext, res android.Path) android.WritablePath { |
| 32 | |
| 33 | name := res.Base() |
| 34 | subDir := filepath.Dir(res.String()) |
| 35 | subDir, lastDir := filepath.Split(subDir) |
| 36 | if strings.HasPrefix(lastDir, "values") { |
| 37 | name = strings.TrimSuffix(name, ".xml") + ".arsc" |
| 38 | } |
| 39 | name = lastDir + "_" + name + ".flat" |
| 40 | return android.PathForModuleOut(ctx, "aapt2", subDir, name) |
| 41 | } |
| 42 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 43 | // pathsToAapt2Paths Calls pathToAapt2Path on each entry of the given Paths, i.e. []Path. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 44 | func pathsToAapt2Paths(ctx android.ModuleContext, resPaths android.Paths) android.WritablePaths { |
| 45 | outPaths := make(android.WritablePaths, len(resPaths)) |
| 46 | |
| 47 | for i, res := range resPaths { |
| 48 | outPaths[i] = pathToAapt2Path(ctx, res) |
| 49 | } |
| 50 | |
| 51 | return outPaths |
| 52 | } |
| 53 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 54 | // Shard resource files for efficiency. See aapt2Compile for details. |
| 55 | const AAPT2_SHARD_SIZE = 100 |
| 56 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 57 | var aapt2CompileRule = pctx.AndroidStaticRule("aapt2Compile", |
| 58 | blueprint.RuleParams{ |
Colin Cross | 4215cfd | 2019-06-20 16:53:30 -0700 | [diff] [blame] | 59 | Command: `${config.Aapt2Cmd} compile -o $outDir $cFlags $in`, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 60 | CommandDeps: []string{"${config.Aapt2Cmd}"}, |
| 61 | }, |
| 62 | "outDir", "cFlags") |
| 63 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 64 | // aapt2Compile compiles resources and puts the results in the requested directory. |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 65 | func aapt2Compile(ctx android.ModuleContext, dir android.Path, paths android.Paths, |
| 66 | flags []string) android.WritablePaths { |
| 67 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 68 | // Shard the input paths so that they can be processed in parallel. If we shard them into too |
| 69 | // small chunks, the additional cost of spinning up aapt2 outweighs the performance gain. The |
| 70 | // current shard size, 100, seems to be a good balance between the added cost and the gain. |
| 71 | // The aapt2 compile actions are trivially short, but each action in ninja takes on the order of |
| 72 | // ~10 ms to run. frameworks/base/core/res/res has >10k resource files, so compiling each one |
| 73 | // with an individual action could take 100 CPU seconds. Sharding them reduces the overhead of |
| 74 | // starting actions by a factor of 100, at the expense of recompiling more files when one |
| 75 | // changes. Since the individual compiles are trivial it's a good tradeoff. |
Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 76 | shards := android.ShardPaths(paths, AAPT2_SHARD_SIZE) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 77 | |
| 78 | ret := make(android.WritablePaths, 0, len(paths)) |
| 79 | |
| 80 | for i, shard := range shards { |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 81 | // This should be kept in sync with pathToAapt2Path. The aapt2 compile command takes an |
| 82 | // output directory path, but not output file paths. So, outPaths is just where we expect |
| 83 | // the output files will be located. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 84 | outPaths := pathsToAapt2Paths(ctx, shard) |
| 85 | ret = append(ret, outPaths...) |
| 86 | |
| 87 | shardDesc := "" |
| 88 | if i != 0 { |
| 89 | shardDesc = " " + strconv.Itoa(i+1) |
| 90 | } |
| 91 | |
| 92 | ctx.Build(pctx, android.BuildParams{ |
| 93 | Rule: aapt2CompileRule, |
| 94 | Description: "aapt2 compile " + dir.String() + shardDesc, |
| 95 | Inputs: shard, |
| 96 | Outputs: outPaths, |
| 97 | Args: map[string]string{ |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 98 | // The aapt2 compile command takes an output directory path, but not output file paths. |
| 99 | // outPaths specified above is only used for dependency management purposes. In order for |
| 100 | // the outPaths values to match the actual outputs from aapt2, the dir parameter value |
| 101 | // must be a common prefix path of the paths values, and the top-level path segment used |
| 102 | // below, "aapt2", must always be kept in sync with the one in pathToAapt2Path. |
| 103 | // TODO(b/174505750): Make this easier and robust to use. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 104 | "outDir": android.PathForModuleOut(ctx, "aapt2", dir.String()).String(), |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 105 | "cFlags": strings.Join(flags, " "), |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 106 | }, |
| 107 | }) |
| 108 | } |
| 109 | |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 110 | sort.Slice(ret, func(i, j int) bool { |
| 111 | return ret[i].String() < ret[j].String() |
| 112 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 113 | return ret |
| 114 | } |
| 115 | |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 116 | var aapt2CompileZipRule = pctx.AndroidStaticRule("aapt2CompileZip", |
| 117 | blueprint.RuleParams{ |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 118 | Command: `${config.ZipSyncCmd} -d $resZipDir $zipSyncFlags $in && ` + |
Colin Cross | 4215cfd | 2019-06-20 16:53:30 -0700 | [diff] [blame] | 119 | `${config.Aapt2Cmd} compile -o $out $cFlags --dir $resZipDir`, |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 120 | CommandDeps: []string{ |
| 121 | "${config.Aapt2Cmd}", |
| 122 | "${config.ZipSyncCmd}", |
| 123 | }, |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 124 | }, "cFlags", "resZipDir", "zipSyncFlags") |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 125 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 126 | // Unzips the given compressed file and compiles the resource source files in it. The zipPrefix |
| 127 | // parameter points to the subdirectory in the zip file where the resource files are located. |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 128 | func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip android.Path, zipPrefix string, |
| 129 | flags []string) { |
| 130 | |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 131 | if zipPrefix != "" { |
| 132 | zipPrefix = "--zip-prefix " + zipPrefix |
| 133 | } |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 134 | ctx.Build(pctx, android.BuildParams{ |
| 135 | Rule: aapt2CompileZipRule, |
| 136 | Description: "aapt2 compile zip", |
| 137 | Input: zip, |
| 138 | Output: flata, |
| 139 | Args: map[string]string{ |
Colin Cross | a0ba2f5 | 2019-06-22 12:59:27 -0700 | [diff] [blame] | 140 | "cFlags": strings.Join(flags, " "), |
Dan Willemsen | 304cfec | 2019-05-28 14:49:06 -0700 | [diff] [blame] | 141 | "resZipDir": android.PathForModuleOut(ctx, "aapt2", "reszip", flata.Base()).String(), |
| 142 | "zipSyncFlags": zipPrefix, |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame] | 143 | }, |
| 144 | }) |
| 145 | } |
| 146 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 147 | var aapt2LinkRule = pctx.AndroidStaticRule("aapt2Link", |
| 148 | blueprint.RuleParams{ |
Colin Cross | 78e3cb0 | 2018-10-17 15:42:59 -0700 | [diff] [blame] | 149 | Command: `rm -rf $genDir && ` + |
| 150 | `${config.Aapt2Cmd} link -o $out $flags --java $genDir --proguard $proguardOptions ` + |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 151 | `--output-text-symbols ${rTxt} $inFlags && ` + |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 152 | `${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir &&` + |
| 153 | `${config.ExtractJarPackagesCmd} -i $genJar -o $extraPackages --prefix '--extra-packages '`, |
| 154 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 155 | CommandDeps: []string{ |
Colin Cross | 44f0668 | 2017-11-29 00:17:36 -0800 | [diff] [blame] | 156 | "${config.Aapt2Cmd}", |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 157 | "${config.SoongZipCmd}", |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 158 | "${config.ExtractJarPackagesCmd}", |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 159 | }, |
| 160 | Restat: true, |
| 161 | }, |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 162 | "flags", "inFlags", "proguardOptions", "genDir", "genJar", "rTxt", "extraPackages") |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 163 | |
| 164 | var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile", |
| 165 | blueprint.RuleParams{ |
| 166 | Command: `cp $out.rsp $out`, |
| 167 | Rspfile: "$out.rsp", |
| 168 | RspfileContent: "$in", |
| 169 | }) |
| 170 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 171 | var mergeAssetsRule = pctx.AndroidStaticRule("mergeAssets", |
| 172 | blueprint.RuleParams{ |
| 173 | Command: `${config.MergeZipsCmd} ${out} ${in}`, |
| 174 | CommandDeps: []string{"${config.MergeZipsCmd}"}, |
| 175 | }) |
| 176 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 177 | func aapt2Link(ctx android.ModuleContext, |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 178 | packageRes, genJar, proguardOptions, rTxt, extraPackages android.WritablePath, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 179 | flags []string, deps android.Paths, |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 180 | compiledRes, compiledOverlay, assetPackages android.Paths, splitPackages android.WritablePaths) { |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 181 | |
| 182 | genDir := android.PathForModuleGen(ctx, "aapt2", "R") |
| 183 | |
| 184 | var inFlags []string |
| 185 | |
| 186 | if len(compiledRes) > 0 { |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 187 | // Create a file that contains the list of all compiled resource file paths. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 188 | resFileList := android.PathForModuleOut(ctx, "aapt2", "res.list") |
| 189 | // Write out file lists to files |
| 190 | ctx.Build(pctx, android.BuildParams{ |
| 191 | Rule: fileListToFileRule, |
| 192 | Description: "resource file list", |
| 193 | Inputs: compiledRes, |
| 194 | Output: resFileList, |
| 195 | }) |
| 196 | |
| 197 | deps = append(deps, compiledRes...) |
| 198 | deps = append(deps, resFileList) |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 199 | // aapt2 filepath arguments that start with "@" mean file-list files. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 200 | inFlags = append(inFlags, "@"+resFileList.String()) |
| 201 | } |
| 202 | |
| 203 | if len(compiledOverlay) > 0 { |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 204 | // Compiled overlay files are processed the same way as compiled resources. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 205 | overlayFileList := android.PathForModuleOut(ctx, "aapt2", "overlay.list") |
| 206 | ctx.Build(pctx, android.BuildParams{ |
| 207 | Rule: fileListToFileRule, |
| 208 | Description: "overlay resource file list", |
| 209 | Inputs: compiledOverlay, |
| 210 | Output: overlayFileList, |
| 211 | }) |
| 212 | |
| 213 | deps = append(deps, compiledOverlay...) |
| 214 | deps = append(deps, overlayFileList) |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 215 | // Compiled overlay files are passed over to aapt2 using -R option. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 216 | inFlags = append(inFlags, "-R", "@"+overlayFileList.String()) |
| 217 | } |
| 218 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 219 | // Set auxiliary outputs as implicit outputs to establish correct dependency chains. |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 220 | implicitOutputs := append(splitPackages, proguardOptions, genJar, rTxt, extraPackages) |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 221 | linkOutput := packageRes |
| 222 | |
| 223 | // AAPT2 ignores assets in overlays. Merge them after linking. |
| 224 | if len(assetPackages) > 0 { |
| 225 | linkOutput = android.PathForModuleOut(ctx, "aapt2", "package-res.apk") |
| 226 | inputZips := append(android.Paths{linkOutput}, assetPackages...) |
| 227 | ctx.Build(pctx, android.BuildParams{ |
| 228 | Rule: mergeAssetsRule, |
| 229 | Inputs: inputZips, |
| 230 | Output: packageRes, |
| 231 | Description: "merge assets from dependencies", |
| 232 | }) |
| 233 | } |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 234 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 235 | ctx.Build(pctx, android.BuildParams{ |
| 236 | Rule: aapt2LinkRule, |
| 237 | Description: "aapt2 link", |
| 238 | Implicits: deps, |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 239 | Output: linkOutput, |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 240 | ImplicitOutputs: implicitOutputs, |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 241 | // Note the absence of splitPackages. The caller is supposed to compose and provide --split flag |
| 242 | // values via the flags parameter when it wants to split outputs. |
| 243 | // TODO(b/174509108): Perhaps we can process it in this func while keeping the code reasonably |
| 244 | // tidy. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 245 | Args: map[string]string{ |
| 246 | "flags": strings.Join(flags, " "), |
| 247 | "inFlags": strings.Join(inFlags, " "), |
| 248 | "proguardOptions": proguardOptions.String(), |
| 249 | "genDir": genDir.String(), |
| 250 | "genJar": genJar.String(), |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 251 | "rTxt": rTxt.String(), |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 252 | "extraPackages": extraPackages.String(), |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 253 | }, |
| 254 | }) |
| 255 | } |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 256 | |
| 257 | var aapt2ConvertRule = pctx.AndroidStaticRule("aapt2Convert", |
| 258 | blueprint.RuleParams{ |
| 259 | Command: `${config.Aapt2Cmd} convert --output-format proto $in -o $out`, |
| 260 | CommandDeps: []string{"${config.Aapt2Cmd}"}, |
| 261 | }) |
| 262 | |
Jaewoong Jung | 60d6d57 | 2020-11-20 17:58:27 -0800 | [diff] [blame] | 263 | // Converts xml files and resource tables (resources.arsc) in the given jar/apk file to a proto |
| 264 | // format. The proto definition is available at frameworks/base/tools/aapt2/Resources.proto. |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 265 | func aapt2Convert(ctx android.ModuleContext, out android.WritablePath, in android.Path) { |
| 266 | ctx.Build(pctx, android.BuildParams{ |
| 267 | Rule: aapt2ConvertRule, |
| 268 | Input: in, |
| 269 | Output: out, |
| 270 | Description: "convert to proto", |
| 271 | }) |
| 272 | } |