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 | |
| 28 | const AAPT2_SHARD_SIZE = 100 |
| 29 | |
| 30 | // Convert input resource file path to output file path. |
| 31 | // values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat; |
| 32 | // For other resource file, just replace the last "/" with "_" and |
| 33 | // add .flat extension. |
| 34 | func pathToAapt2Path(ctx android.ModuleContext, res android.Path) android.WritablePath { |
| 35 | |
| 36 | name := res.Base() |
| 37 | subDir := filepath.Dir(res.String()) |
| 38 | subDir, lastDir := filepath.Split(subDir) |
| 39 | if strings.HasPrefix(lastDir, "values") { |
| 40 | name = strings.TrimSuffix(name, ".xml") + ".arsc" |
| 41 | } |
| 42 | name = lastDir + "_" + name + ".flat" |
| 43 | return android.PathForModuleOut(ctx, "aapt2", subDir, name) |
| 44 | } |
| 45 | |
| 46 | func pathsToAapt2Paths(ctx android.ModuleContext, resPaths android.Paths) android.WritablePaths { |
| 47 | outPaths := make(android.WritablePaths, len(resPaths)) |
| 48 | |
| 49 | for i, res := range resPaths { |
| 50 | outPaths[i] = pathToAapt2Path(ctx, res) |
| 51 | } |
| 52 | |
| 53 | return outPaths |
| 54 | } |
| 55 | |
| 56 | var aapt2CompileRule = pctx.AndroidStaticRule("aapt2Compile", |
| 57 | blueprint.RuleParams{ |
Colin Cross | 44f0668 | 2017-11-29 00:17:36 -0800 | [diff] [blame] | 58 | Command: `${config.Aapt2Cmd} compile -o $outDir $cFlags --legacy $in`, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 59 | CommandDeps: []string{"${config.Aapt2Cmd}"}, |
| 60 | }, |
| 61 | "outDir", "cFlags") |
| 62 | |
| 63 | func aapt2Compile(ctx android.ModuleContext, dir android.Path, paths android.Paths) android.WritablePaths { |
| 64 | shards := shardPaths(paths, AAPT2_SHARD_SIZE) |
| 65 | |
| 66 | ret := make(android.WritablePaths, 0, len(paths)) |
| 67 | |
| 68 | for i, shard := range shards { |
| 69 | outPaths := pathsToAapt2Paths(ctx, shard) |
| 70 | ret = append(ret, outPaths...) |
| 71 | |
| 72 | shardDesc := "" |
| 73 | if i != 0 { |
| 74 | shardDesc = " " + strconv.Itoa(i+1) |
| 75 | } |
| 76 | |
| 77 | ctx.Build(pctx, android.BuildParams{ |
| 78 | Rule: aapt2CompileRule, |
| 79 | Description: "aapt2 compile " + dir.String() + shardDesc, |
| 80 | Inputs: shard, |
| 81 | Outputs: outPaths, |
| 82 | Args: map[string]string{ |
| 83 | "outDir": android.PathForModuleOut(ctx, "aapt2", dir.String()).String(), |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 84 | // Always set --pseudo-localize, it will be stripped out later for release |
| 85 | // builds that don't want it. |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 86 | "cFlags": "--pseudo-localize", |
| 87 | }, |
| 88 | }) |
| 89 | } |
| 90 | |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 91 | sort.Slice(ret, func(i, j int) bool { |
| 92 | return ret[i].String() < ret[j].String() |
| 93 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 94 | return ret |
| 95 | } |
| 96 | |
Colin Cross | fabb608 | 2018-02-20 17:22:23 -0800 | [diff] [blame] | 97 | func aapt2CompileDirs(ctx android.ModuleContext, flata android.WritablePath, dirs android.Paths, deps android.Paths) { |
| 98 | ctx.Build(pctx, android.BuildParams{ |
| 99 | Rule: aapt2CompileRule, |
| 100 | Description: "aapt2 compile dirs", |
| 101 | Implicits: deps, |
| 102 | Output: flata, |
| 103 | Args: map[string]string{ |
| 104 | "outDir": flata.String(), |
| 105 | // Always set --pseudo-localize, it will be stripped out later for release |
| 106 | // builds that don't want it. |
| 107 | "cFlags": "--pseudo-localize " + android.JoinWithPrefix(dirs.Strings(), "--dir "), |
| 108 | }, |
| 109 | }) |
| 110 | } |
| 111 | |
Colin Cross | a592e3e | 2019-02-19 16:59:53 -0800 | [diff] [blame^] | 112 | var aapt2CompileZipRule = pctx.AndroidStaticRule("aapt2CompileZip", |
| 113 | blueprint.RuleParams{ |
| 114 | Command: `${config.ZipSyncCmd} -d $resZipDir $in && ` + |
| 115 | `${config.Aapt2Cmd} compile -o $out $cFlags --legacy --dir $resZipDir`, |
| 116 | CommandDeps: []string{ |
| 117 | "${config.Aapt2Cmd}", |
| 118 | "${config.ZipSyncCmd}", |
| 119 | }, |
| 120 | }, "cFlags", "resZipDir") |
| 121 | |
| 122 | func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip android.Path) { |
| 123 | ctx.Build(pctx, android.BuildParams{ |
| 124 | Rule: aapt2CompileZipRule, |
| 125 | Description: "aapt2 compile zip", |
| 126 | Input: zip, |
| 127 | Output: flata, |
| 128 | Args: map[string]string{ |
| 129 | // Always set --pseudo-localize, it will be stripped out later for release |
| 130 | // builds that don't want it. |
| 131 | "cFlags": "--pseudo-localize", |
| 132 | "resZipDir": android.PathForModuleOut(ctx, "aapt2", "reszip", flata.Base()).String(), |
| 133 | }, |
| 134 | }) |
| 135 | } |
| 136 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 137 | var aapt2LinkRule = pctx.AndroidStaticRule("aapt2Link", |
| 138 | blueprint.RuleParams{ |
Colin Cross | 78e3cb0 | 2018-10-17 15:42:59 -0700 | [diff] [blame] | 139 | Command: `rm -rf $genDir && ` + |
| 140 | `${config.Aapt2Cmd} link -o $out $flags --java $genDir --proguard $proguardOptions ` + |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 141 | `--output-text-symbols ${rTxt} $inFlags && ` + |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 142 | `${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir &&` + |
| 143 | `${config.ExtractJarPackagesCmd} -i $genJar -o $extraPackages --prefix '--extra-packages '`, |
| 144 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 145 | CommandDeps: []string{ |
Colin Cross | 44f0668 | 2017-11-29 00:17:36 -0800 | [diff] [blame] | 146 | "${config.Aapt2Cmd}", |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 147 | "${config.SoongZipCmd}", |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 148 | "${config.ExtractJarPackagesCmd}", |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 149 | }, |
| 150 | Restat: true, |
| 151 | }, |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 152 | "flags", "inFlags", "proguardOptions", "genDir", "genJar", "rTxt", "extraPackages") |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 153 | |
| 154 | var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile", |
| 155 | blueprint.RuleParams{ |
| 156 | Command: `cp $out.rsp $out`, |
| 157 | Rspfile: "$out.rsp", |
| 158 | RspfileContent: "$in", |
| 159 | }) |
| 160 | |
| 161 | func aapt2Link(ctx android.ModuleContext, |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 162 | packageRes, genJar, proguardOptions, rTxt, extraPackages android.WritablePath, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 163 | flags []string, deps android.Paths, |
| 164 | compiledRes, compiledOverlay android.Paths) { |
| 165 | |
| 166 | genDir := android.PathForModuleGen(ctx, "aapt2", "R") |
| 167 | |
| 168 | var inFlags []string |
| 169 | |
| 170 | if len(compiledRes) > 0 { |
| 171 | resFileList := android.PathForModuleOut(ctx, "aapt2", "res.list") |
| 172 | // Write out file lists to files |
| 173 | ctx.Build(pctx, android.BuildParams{ |
| 174 | Rule: fileListToFileRule, |
| 175 | Description: "resource file list", |
| 176 | Inputs: compiledRes, |
| 177 | Output: resFileList, |
| 178 | }) |
| 179 | |
| 180 | deps = append(deps, compiledRes...) |
| 181 | deps = append(deps, resFileList) |
| 182 | inFlags = append(inFlags, "@"+resFileList.String()) |
| 183 | } |
| 184 | |
| 185 | if len(compiledOverlay) > 0 { |
| 186 | overlayFileList := android.PathForModuleOut(ctx, "aapt2", "overlay.list") |
| 187 | ctx.Build(pctx, android.BuildParams{ |
| 188 | Rule: fileListToFileRule, |
| 189 | Description: "overlay resource file list", |
| 190 | Inputs: compiledOverlay, |
| 191 | Output: overlayFileList, |
| 192 | }) |
| 193 | |
| 194 | deps = append(deps, compiledOverlay...) |
| 195 | deps = append(deps, overlayFileList) |
| 196 | inFlags = append(inFlags, "-R", "@"+overlayFileList.String()) |
| 197 | } |
| 198 | |
| 199 | ctx.Build(pctx, android.BuildParams{ |
| 200 | Rule: aapt2LinkRule, |
| 201 | Description: "aapt2 link", |
| 202 | Implicits: deps, |
| 203 | Output: packageRes, |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 204 | ImplicitOutputs: android.WritablePaths{proguardOptions, genJar, rTxt, extraPackages}, |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 205 | Args: map[string]string{ |
| 206 | "flags": strings.Join(flags, " "), |
| 207 | "inFlags": strings.Join(inFlags, " "), |
| 208 | "proguardOptions": proguardOptions.String(), |
| 209 | "genDir": genDir.String(), |
| 210 | "genJar": genJar.String(), |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 211 | "rTxt": rTxt.String(), |
Colin Cross | 66f7882 | 2018-05-02 12:58:28 -0700 | [diff] [blame] | 212 | "extraPackages": extraPackages.String(), |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 213 | }, |
| 214 | }) |
| 215 | } |
Colin Cross | f623721 | 2018-10-29 23:14:58 -0700 | [diff] [blame] | 216 | |
| 217 | var aapt2ConvertRule = pctx.AndroidStaticRule("aapt2Convert", |
| 218 | blueprint.RuleParams{ |
| 219 | Command: `${config.Aapt2Cmd} convert --output-format proto $in -o $out`, |
| 220 | CommandDeps: []string{"${config.Aapt2Cmd}"}, |
| 221 | }) |
| 222 | |
| 223 | func aapt2Convert(ctx android.ModuleContext, out android.WritablePath, in android.Path) { |
| 224 | ctx.Build(pctx, android.BuildParams{ |
| 225 | Rule: aapt2ConvertRule, |
| 226 | Input: in, |
| 227 | Output: out, |
| 228 | Description: "convert to proto", |
| 229 | }) |
| 230 | } |