blob: a8151608f0e971030c9a0a3179214bc64c754327 [file] [log] [blame]
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001// 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
15package java
16
17import (
18 "path/filepath"
Colin Crossb69301e2017-12-01 10:48:26 -080019 "sort"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080020 "strconv"
21 "strings"
22
23 "github.com/google/blueprint"
24
25 "android/soong/android"
26)
27
28const 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.
34func 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
46func 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
56var aapt2CompileRule = pctx.AndroidStaticRule("aapt2Compile",
57 blueprint.RuleParams{
Colin Cross44f06682017-11-29 00:17:36 -080058 Command: `${config.Aapt2Cmd} compile -o $outDir $cFlags --legacy $in`,
Colin Cross3bc7ffa2017-11-22 16:19:37 -080059 CommandDeps: []string{"${config.Aapt2Cmd}"},
60 },
61 "outDir", "cFlags")
62
63func 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 Crossfabb6082018-02-20 17:22:23 -080084 // Always set --pseudo-localize, it will be stripped out later for release
85 // builds that don't want it.
Colin Cross3bc7ffa2017-11-22 16:19:37 -080086 "cFlags": "--pseudo-localize",
87 },
88 })
89 }
90
Colin Crossb69301e2017-12-01 10:48:26 -080091 sort.Slice(ret, func(i, j int) bool {
92 return ret[i].String() < ret[j].String()
93 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -080094 return ret
95}
96
Colin Crossa592e3e2019-02-19 16:59:53 -080097var aapt2CompileZipRule = pctx.AndroidStaticRule("aapt2CompileZip",
98 blueprint.RuleParams{
Dan Willemsen304cfec2019-05-28 14:49:06 -070099 Command: `${config.ZipSyncCmd} -d $resZipDir $zipSyncFlags $in && ` +
Colin Crossa592e3e2019-02-19 16:59:53 -0800100 `${config.Aapt2Cmd} compile -o $out $cFlags --legacy --dir $resZipDir`,
101 CommandDeps: []string{
102 "${config.Aapt2Cmd}",
103 "${config.ZipSyncCmd}",
104 },
Dan Willemsen304cfec2019-05-28 14:49:06 -0700105 }, "cFlags", "resZipDir", "zipSyncFlags")
Colin Crossa592e3e2019-02-19 16:59:53 -0800106
Dan Willemsen304cfec2019-05-28 14:49:06 -0700107func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip android.Path, zipPrefix string) {
108 if zipPrefix != "" {
109 zipPrefix = "--zip-prefix " + zipPrefix
110 }
Colin Crossa592e3e2019-02-19 16:59:53 -0800111 ctx.Build(pctx, android.BuildParams{
112 Rule: aapt2CompileZipRule,
113 Description: "aapt2 compile zip",
114 Input: zip,
115 Output: flata,
116 Args: map[string]string{
117 // Always set --pseudo-localize, it will be stripped out later for release
118 // builds that don't want it.
Dan Willemsen304cfec2019-05-28 14:49:06 -0700119 "cFlags": "--pseudo-localize",
120 "resZipDir": android.PathForModuleOut(ctx, "aapt2", "reszip", flata.Base()).String(),
121 "zipSyncFlags": zipPrefix,
Colin Crossa592e3e2019-02-19 16:59:53 -0800122 },
123 })
124}
125
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800126var aapt2LinkRule = pctx.AndroidStaticRule("aapt2Link",
127 blueprint.RuleParams{
Colin Cross78e3cb02018-10-17 15:42:59 -0700128 Command: `rm -rf $genDir && ` +
129 `${config.Aapt2Cmd} link -o $out $flags --java $genDir --proguard $proguardOptions ` +
Colin Crossa97c5d32018-03-28 14:58:31 -0700130 `--output-text-symbols ${rTxt} $inFlags && ` +
Colin Cross66f78822018-05-02 12:58:28 -0700131 `${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir &&` +
132 `${config.ExtractJarPackagesCmd} -i $genJar -o $extraPackages --prefix '--extra-packages '`,
133
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800134 CommandDeps: []string{
Colin Cross44f06682017-11-29 00:17:36 -0800135 "${config.Aapt2Cmd}",
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800136 "${config.SoongZipCmd}",
Colin Cross66f78822018-05-02 12:58:28 -0700137 "${config.ExtractJarPackagesCmd}",
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800138 },
139 Restat: true,
140 },
Colin Cross66f78822018-05-02 12:58:28 -0700141 "flags", "inFlags", "proguardOptions", "genDir", "genJar", "rTxt", "extraPackages")
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800142
143var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile",
144 blueprint.RuleParams{
145 Command: `cp $out.rsp $out`,
146 Rspfile: "$out.rsp",
147 RspfileContent: "$in",
148 })
149
150func aapt2Link(ctx android.ModuleContext,
Colin Cross66f78822018-05-02 12:58:28 -0700151 packageRes, genJar, proguardOptions, rTxt, extraPackages android.WritablePath,
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800152 flags []string, deps android.Paths,
Colin Crosse560c4a2019-03-19 16:03:11 -0700153 compiledRes, compiledOverlay android.Paths, splitPackages android.WritablePaths) {
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800154
155 genDir := android.PathForModuleGen(ctx, "aapt2", "R")
156
157 var inFlags []string
158
159 if len(compiledRes) > 0 {
160 resFileList := android.PathForModuleOut(ctx, "aapt2", "res.list")
161 // Write out file lists to files
162 ctx.Build(pctx, android.BuildParams{
163 Rule: fileListToFileRule,
164 Description: "resource file list",
165 Inputs: compiledRes,
166 Output: resFileList,
167 })
168
169 deps = append(deps, compiledRes...)
170 deps = append(deps, resFileList)
171 inFlags = append(inFlags, "@"+resFileList.String())
172 }
173
174 if len(compiledOverlay) > 0 {
175 overlayFileList := android.PathForModuleOut(ctx, "aapt2", "overlay.list")
176 ctx.Build(pctx, android.BuildParams{
177 Rule: fileListToFileRule,
178 Description: "overlay resource file list",
179 Inputs: compiledOverlay,
180 Output: overlayFileList,
181 })
182
183 deps = append(deps, compiledOverlay...)
184 deps = append(deps, overlayFileList)
185 inFlags = append(inFlags, "-R", "@"+overlayFileList.String())
186 }
187
Colin Crosse560c4a2019-03-19 16:03:11 -0700188 implicitOutputs := append(splitPackages, proguardOptions, genJar, rTxt, extraPackages)
189
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800190 ctx.Build(pctx, android.BuildParams{
191 Rule: aapt2LinkRule,
192 Description: "aapt2 link",
193 Implicits: deps,
194 Output: packageRes,
Colin Crosse560c4a2019-03-19 16:03:11 -0700195 ImplicitOutputs: implicitOutputs,
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800196 Args: map[string]string{
197 "flags": strings.Join(flags, " "),
198 "inFlags": strings.Join(inFlags, " "),
199 "proguardOptions": proguardOptions.String(),
200 "genDir": genDir.String(),
201 "genJar": genJar.String(),
Colin Crossa97c5d32018-03-28 14:58:31 -0700202 "rTxt": rTxt.String(),
Colin Cross66f78822018-05-02 12:58:28 -0700203 "extraPackages": extraPackages.String(),
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800204 },
205 })
206}
Colin Crossf6237212018-10-29 23:14:58 -0700207
208var aapt2ConvertRule = pctx.AndroidStaticRule("aapt2Convert",
209 blueprint.RuleParams{
210 Command: `${config.Aapt2Cmd} convert --output-format proto $in -o $out`,
211 CommandDeps: []string{"${config.Aapt2Cmd}"},
212 })
213
214func aapt2Convert(ctx android.ModuleContext, out android.WritablePath, in android.Path) {
215 ctx.Build(pctx, android.BuildParams{
216 Rule: aapt2ConvertRule,
217 Input: in,
218 Output: out,
219 Description: "convert to proto",
220 })
221}