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(), |
| 84 | "cFlags": "--pseudo-localize", |
| 85 | }, |
| 86 | }) |
| 87 | } |
| 88 | |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame^] | 89 | sort.Slice(ret, func(i, j int) bool { |
| 90 | return ret[i].String() < ret[j].String() |
| 91 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 92 | return ret |
| 93 | } |
| 94 | |
| 95 | var aapt2LinkRule = pctx.AndroidStaticRule("aapt2Link", |
| 96 | blueprint.RuleParams{ |
Colin Cross | 44f0668 | 2017-11-29 00:17:36 -0800 | [diff] [blame] | 97 | Command: `${config.Aapt2Cmd} link -o $out $flags --java $genDir --proguard $proguardOptions $inFlags && ` + |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 98 | `${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir`, |
| 99 | CommandDeps: []string{ |
Colin Cross | 44f0668 | 2017-11-29 00:17:36 -0800 | [diff] [blame] | 100 | "${config.Aapt2Cmd}", |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 101 | "${config.SoongZipCmd}", |
| 102 | }, |
| 103 | Restat: true, |
| 104 | }, |
| 105 | "flags", "inFlags", "proguardOptions", "genDir", "genJar") |
| 106 | |
| 107 | var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile", |
| 108 | blueprint.RuleParams{ |
| 109 | Command: `cp $out.rsp $out`, |
| 110 | Rspfile: "$out.rsp", |
| 111 | RspfileContent: "$in", |
| 112 | }) |
| 113 | |
| 114 | func aapt2Link(ctx android.ModuleContext, |
| 115 | packageRes, genJar, proguardOptions android.WritablePath, |
| 116 | flags []string, deps android.Paths, |
| 117 | compiledRes, compiledOverlay android.Paths) { |
| 118 | |
| 119 | genDir := android.PathForModuleGen(ctx, "aapt2", "R") |
| 120 | |
| 121 | var inFlags []string |
| 122 | |
| 123 | if len(compiledRes) > 0 { |
| 124 | resFileList := android.PathForModuleOut(ctx, "aapt2", "res.list") |
| 125 | // Write out file lists to files |
| 126 | ctx.Build(pctx, android.BuildParams{ |
| 127 | Rule: fileListToFileRule, |
| 128 | Description: "resource file list", |
| 129 | Inputs: compiledRes, |
| 130 | Output: resFileList, |
| 131 | }) |
| 132 | |
| 133 | deps = append(deps, compiledRes...) |
| 134 | deps = append(deps, resFileList) |
| 135 | inFlags = append(inFlags, "@"+resFileList.String()) |
| 136 | } |
| 137 | |
| 138 | if len(compiledOverlay) > 0 { |
| 139 | overlayFileList := android.PathForModuleOut(ctx, "aapt2", "overlay.list") |
| 140 | ctx.Build(pctx, android.BuildParams{ |
| 141 | Rule: fileListToFileRule, |
| 142 | Description: "overlay resource file list", |
| 143 | Inputs: compiledOverlay, |
| 144 | Output: overlayFileList, |
| 145 | }) |
| 146 | |
| 147 | deps = append(deps, compiledOverlay...) |
| 148 | deps = append(deps, overlayFileList) |
| 149 | inFlags = append(inFlags, "-R", "@"+overlayFileList.String()) |
| 150 | } |
| 151 | |
| 152 | ctx.Build(pctx, android.BuildParams{ |
| 153 | Rule: aapt2LinkRule, |
| 154 | Description: "aapt2 link", |
| 155 | Implicits: deps, |
| 156 | Output: packageRes, |
| 157 | ImplicitOutputs: android.WritablePaths{proguardOptions, genJar}, |
| 158 | Args: map[string]string{ |
| 159 | "flags": strings.Join(flags, " "), |
| 160 | "inFlags": strings.Join(inFlags, " "), |
| 161 | "proguardOptions": proguardOptions.String(), |
| 162 | "genDir": genDir.String(), |
| 163 | "genJar": genJar.String(), |
| 164 | }, |
| 165 | }) |
| 166 | } |