2017-11-23 01:19:37 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2017-12-01 19:48:26 +01:00
|
|
|
"sort"
|
2017-11-23 01:19:37 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Convert input resource file path to output file path.
|
|
|
|
// values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat;
|
2020-11-21 02:58:27 +01:00
|
|
|
// For other resource file, just replace the last "/" with "_" and add .flat extension.
|
2017-11-23 01:19:37 +01:00
|
|
|
func pathToAapt2Path(ctx android.ModuleContext, res android.Path) android.WritablePath {
|
|
|
|
|
|
|
|
name := res.Base()
|
|
|
|
subDir := filepath.Dir(res.String())
|
|
|
|
subDir, lastDir := filepath.Split(subDir)
|
|
|
|
if strings.HasPrefix(lastDir, "values") {
|
|
|
|
name = strings.TrimSuffix(name, ".xml") + ".arsc"
|
|
|
|
}
|
|
|
|
name = lastDir + "_" + name + ".flat"
|
|
|
|
return android.PathForModuleOut(ctx, "aapt2", subDir, name)
|
|
|
|
}
|
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// pathsToAapt2Paths Calls pathToAapt2Path on each entry of the given Paths, i.e. []Path.
|
2017-11-23 01:19:37 +01:00
|
|
|
func pathsToAapt2Paths(ctx android.ModuleContext, resPaths android.Paths) android.WritablePaths {
|
|
|
|
outPaths := make(android.WritablePaths, len(resPaths))
|
|
|
|
|
|
|
|
for i, res := range resPaths {
|
|
|
|
outPaths[i] = pathToAapt2Path(ctx, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outPaths
|
|
|
|
}
|
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// Shard resource files for efficiency. See aapt2Compile for details.
|
|
|
|
const AAPT2_SHARD_SIZE = 100
|
|
|
|
|
2017-11-23 01:19:37 +01:00
|
|
|
var aapt2CompileRule = pctx.AndroidStaticRule("aapt2Compile",
|
|
|
|
blueprint.RuleParams{
|
2019-06-21 01:53:30 +02:00
|
|
|
Command: `${config.Aapt2Cmd} compile -o $outDir $cFlags $in`,
|
2017-11-23 01:19:37 +01:00
|
|
|
CommandDeps: []string{"${config.Aapt2Cmd}"},
|
|
|
|
},
|
|
|
|
"outDir", "cFlags")
|
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// aapt2Compile compiles resources and puts the results in the requested directory.
|
2019-06-22 21:59:27 +02:00
|
|
|
func aapt2Compile(ctx android.ModuleContext, dir android.Path, paths android.Paths,
|
|
|
|
flags []string) android.WritablePaths {
|
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// Shard the input paths so that they can be processed in parallel. If we shard them into too
|
|
|
|
// small chunks, the additional cost of spinning up aapt2 outweighs the performance gain. The
|
|
|
|
// current shard size, 100, seems to be a good balance between the added cost and the gain.
|
|
|
|
// The aapt2 compile actions are trivially short, but each action in ninja takes on the order of
|
|
|
|
// ~10 ms to run. frameworks/base/core/res/res has >10k resource files, so compiling each one
|
|
|
|
// with an individual action could take 100 CPU seconds. Sharding them reduces the overhead of
|
|
|
|
// starting actions by a factor of 100, at the expense of recompiling more files when one
|
|
|
|
// changes. Since the individual compiles are trivial it's a good tradeoff.
|
2019-09-23 23:33:09 +02:00
|
|
|
shards := android.ShardPaths(paths, AAPT2_SHARD_SIZE)
|
2017-11-23 01:19:37 +01:00
|
|
|
|
|
|
|
ret := make(android.WritablePaths, 0, len(paths))
|
|
|
|
|
|
|
|
for i, shard := range shards {
|
2020-11-21 02:58:27 +01:00
|
|
|
// This should be kept in sync with pathToAapt2Path. The aapt2 compile command takes an
|
|
|
|
// output directory path, but not output file paths. So, outPaths is just where we expect
|
|
|
|
// the output files will be located.
|
2017-11-23 01:19:37 +01:00
|
|
|
outPaths := pathsToAapt2Paths(ctx, shard)
|
|
|
|
ret = append(ret, outPaths...)
|
|
|
|
|
|
|
|
shardDesc := ""
|
|
|
|
if i != 0 {
|
|
|
|
shardDesc = " " + strconv.Itoa(i+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: aapt2CompileRule,
|
|
|
|
Description: "aapt2 compile " + dir.String() + shardDesc,
|
|
|
|
Inputs: shard,
|
|
|
|
Outputs: outPaths,
|
|
|
|
Args: map[string]string{
|
2020-11-21 02:58:27 +01:00
|
|
|
// The aapt2 compile command takes an output directory path, but not output file paths.
|
|
|
|
// outPaths specified above is only used for dependency management purposes. In order for
|
|
|
|
// the outPaths values to match the actual outputs from aapt2, the dir parameter value
|
|
|
|
// must be a common prefix path of the paths values, and the top-level path segment used
|
|
|
|
// below, "aapt2", must always be kept in sync with the one in pathToAapt2Path.
|
|
|
|
// TODO(b/174505750): Make this easier and robust to use.
|
2017-11-23 01:19:37 +01:00
|
|
|
"outDir": android.PathForModuleOut(ctx, "aapt2", dir.String()).String(),
|
2019-06-22 21:59:27 +02:00
|
|
|
"cFlags": strings.Join(flags, " "),
|
2017-11-23 01:19:37 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-01 19:48:26 +01:00
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
return ret[i].String() < ret[j].String()
|
|
|
|
})
|
2017-11-23 01:19:37 +01:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:59:53 +01:00
|
|
|
var aapt2CompileZipRule = pctx.AndroidStaticRule("aapt2CompileZip",
|
|
|
|
blueprint.RuleParams{
|
2019-05-28 23:49:06 +02:00
|
|
|
Command: `${config.ZipSyncCmd} -d $resZipDir $zipSyncFlags $in && ` +
|
2019-06-21 01:53:30 +02:00
|
|
|
`${config.Aapt2Cmd} compile -o $out $cFlags --dir $resZipDir`,
|
2019-02-20 01:59:53 +01:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.Aapt2Cmd}",
|
|
|
|
"${config.ZipSyncCmd}",
|
|
|
|
},
|
2019-05-28 23:49:06 +02:00
|
|
|
}, "cFlags", "resZipDir", "zipSyncFlags")
|
2019-02-20 01:59:53 +01:00
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// Unzips the given compressed file and compiles the resource source files in it. The zipPrefix
|
|
|
|
// parameter points to the subdirectory in the zip file where the resource files are located.
|
2019-06-22 21:59:27 +02:00
|
|
|
func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip android.Path, zipPrefix string,
|
|
|
|
flags []string) {
|
|
|
|
|
2019-05-28 23:49:06 +02:00
|
|
|
if zipPrefix != "" {
|
|
|
|
zipPrefix = "--zip-prefix " + zipPrefix
|
|
|
|
}
|
2019-02-20 01:59:53 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: aapt2CompileZipRule,
|
|
|
|
Description: "aapt2 compile zip",
|
|
|
|
Input: zip,
|
|
|
|
Output: flata,
|
|
|
|
Args: map[string]string{
|
2019-06-22 21:59:27 +02:00
|
|
|
"cFlags": strings.Join(flags, " "),
|
2019-05-28 23:49:06 +02:00
|
|
|
"resZipDir": android.PathForModuleOut(ctx, "aapt2", "reszip", flata.Base()).String(),
|
|
|
|
"zipSyncFlags": zipPrefix,
|
2019-02-20 01:59:53 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-23 01:19:37 +01:00
|
|
|
var aapt2LinkRule = pctx.AndroidStaticRule("aapt2Link",
|
|
|
|
blueprint.RuleParams{
|
2018-10-18 00:42:59 +02:00
|
|
|
Command: `rm -rf $genDir && ` +
|
|
|
|
`${config.Aapt2Cmd} link -o $out $flags --java $genDir --proguard $proguardOptions ` +
|
2018-03-28 23:58:31 +02:00
|
|
|
`--output-text-symbols ${rTxt} $inFlags && ` +
|
2018-05-02 21:58:28 +02:00
|
|
|
`${config.SoongZipCmd} -write_if_changed -jar -o $genJar -C $genDir -D $genDir &&` +
|
2023-08-03 02:53:03 +02:00
|
|
|
`${config.ExtractJarPackagesCmd} -i $genJar -o $extraPackages --prefix '--extra-packages ' && ` +
|
|
|
|
`rm -rf $genDir`,
|
2018-05-02 21:58:28 +02:00
|
|
|
|
2017-11-23 01:19:37 +01:00
|
|
|
CommandDeps: []string{
|
2017-11-29 09:17:36 +01:00
|
|
|
"${config.Aapt2Cmd}",
|
2017-11-23 01:19:37 +01:00
|
|
|
"${config.SoongZipCmd}",
|
2018-05-02 21:58:28 +02:00
|
|
|
"${config.ExtractJarPackagesCmd}",
|
2017-11-23 01:19:37 +01:00
|
|
|
},
|
|
|
|
Restat: true,
|
|
|
|
},
|
2018-05-02 21:58:28 +02:00
|
|
|
"flags", "inFlags", "proguardOptions", "genDir", "genJar", "rTxt", "extraPackages")
|
2017-11-23 01:19:37 +01:00
|
|
|
|
|
|
|
var fileListToFileRule = pctx.AndroidStaticRule("fileListToFile",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `cp $out.rsp $out`,
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
})
|
|
|
|
|
2020-01-15 23:15:10 +01:00
|
|
|
var mergeAssetsRule = pctx.AndroidStaticRule("mergeAssets",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `${config.MergeZipsCmd} ${out} ${in}`,
|
|
|
|
CommandDeps: []string{"${config.MergeZipsCmd}"},
|
|
|
|
})
|
|
|
|
|
2017-11-23 01:19:37 +01:00
|
|
|
func aapt2Link(ctx android.ModuleContext,
|
2018-05-02 21:58:28 +02:00
|
|
|
packageRes, genJar, proguardOptions, rTxt, extraPackages android.WritablePath,
|
2017-11-23 01:19:37 +01:00
|
|
|
flags []string, deps android.Paths,
|
2020-01-15 23:15:10 +01:00
|
|
|
compiledRes, compiledOverlay, assetPackages android.Paths, splitPackages android.WritablePaths) {
|
2017-11-23 01:19:37 +01:00
|
|
|
|
|
|
|
genDir := android.PathForModuleGen(ctx, "aapt2", "R")
|
|
|
|
|
|
|
|
var inFlags []string
|
|
|
|
|
|
|
|
if len(compiledRes) > 0 {
|
2020-11-21 02:58:27 +01:00
|
|
|
// Create a file that contains the list of all compiled resource file paths.
|
2017-11-23 01:19:37 +01:00
|
|
|
resFileList := android.PathForModuleOut(ctx, "aapt2", "res.list")
|
|
|
|
// Write out file lists to files
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: fileListToFileRule,
|
|
|
|
Description: "resource file list",
|
|
|
|
Inputs: compiledRes,
|
|
|
|
Output: resFileList,
|
|
|
|
})
|
|
|
|
|
|
|
|
deps = append(deps, compiledRes...)
|
|
|
|
deps = append(deps, resFileList)
|
2020-11-21 02:58:27 +01:00
|
|
|
// aapt2 filepath arguments that start with "@" mean file-list files.
|
2017-11-23 01:19:37 +01:00
|
|
|
inFlags = append(inFlags, "@"+resFileList.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(compiledOverlay) > 0 {
|
2020-11-21 02:58:27 +01:00
|
|
|
// Compiled overlay files are processed the same way as compiled resources.
|
2017-11-23 01:19:37 +01:00
|
|
|
overlayFileList := android.PathForModuleOut(ctx, "aapt2", "overlay.list")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: fileListToFileRule,
|
|
|
|
Description: "overlay resource file list",
|
|
|
|
Inputs: compiledOverlay,
|
|
|
|
Output: overlayFileList,
|
|
|
|
})
|
|
|
|
|
|
|
|
deps = append(deps, compiledOverlay...)
|
|
|
|
deps = append(deps, overlayFileList)
|
2020-11-21 02:58:27 +01:00
|
|
|
// Compiled overlay files are passed over to aapt2 using -R option.
|
2017-11-23 01:19:37 +01:00
|
|
|
inFlags = append(inFlags, "-R", "@"+overlayFileList.String())
|
|
|
|
}
|
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// Set auxiliary outputs as implicit outputs to establish correct dependency chains.
|
2019-03-20 00:03:11 +01:00
|
|
|
implicitOutputs := append(splitPackages, proguardOptions, genJar, rTxt, extraPackages)
|
2020-01-15 23:15:10 +01:00
|
|
|
linkOutput := packageRes
|
|
|
|
|
|
|
|
// AAPT2 ignores assets in overlays. Merge them after linking.
|
|
|
|
if len(assetPackages) > 0 {
|
|
|
|
linkOutput = android.PathForModuleOut(ctx, "aapt2", "package-res.apk")
|
|
|
|
inputZips := append(android.Paths{linkOutput}, assetPackages...)
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: mergeAssetsRule,
|
|
|
|
Inputs: inputZips,
|
|
|
|
Output: packageRes,
|
|
|
|
Description: "merge assets from dependencies",
|
|
|
|
})
|
|
|
|
}
|
2019-03-20 00:03:11 +01:00
|
|
|
|
2017-11-23 01:19:37 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: aapt2LinkRule,
|
|
|
|
Description: "aapt2 link",
|
|
|
|
Implicits: deps,
|
2020-01-15 23:15:10 +01:00
|
|
|
Output: linkOutput,
|
2019-03-20 00:03:11 +01:00
|
|
|
ImplicitOutputs: implicitOutputs,
|
2020-11-21 02:58:27 +01:00
|
|
|
// Note the absence of splitPackages. The caller is supposed to compose and provide --split flag
|
|
|
|
// values via the flags parameter when it wants to split outputs.
|
|
|
|
// TODO(b/174509108): Perhaps we can process it in this func while keeping the code reasonably
|
|
|
|
// tidy.
|
2017-11-23 01:19:37 +01:00
|
|
|
Args: map[string]string{
|
|
|
|
"flags": strings.Join(flags, " "),
|
|
|
|
"inFlags": strings.Join(inFlags, " "),
|
|
|
|
"proguardOptions": proguardOptions.String(),
|
|
|
|
"genDir": genDir.String(),
|
|
|
|
"genJar": genJar.String(),
|
2018-03-28 23:58:31 +02:00
|
|
|
"rTxt": rTxt.String(),
|
2018-05-02 21:58:28 +02:00
|
|
|
"extraPackages": extraPackages.String(),
|
2017-11-23 01:19:37 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-10-30 07:14:58 +01:00
|
|
|
|
|
|
|
var aapt2ConvertRule = pctx.AndroidStaticRule("aapt2Convert",
|
|
|
|
blueprint.RuleParams{
|
2023-08-01 14:38:36 +02:00
|
|
|
Command: `${config.Aapt2Cmd} convert --enable-compact-entries ` +
|
|
|
|
`--output-format $format $in -o $out`,
|
2018-10-30 07:14:58 +01:00
|
|
|
CommandDeps: []string{"${config.Aapt2Cmd}"},
|
2022-09-22 10:41:42 +02:00
|
|
|
}, "format",
|
|
|
|
)
|
2018-10-30 07:14:58 +01:00
|
|
|
|
2020-11-21 02:58:27 +01:00
|
|
|
// Converts xml files and resource tables (resources.arsc) in the given jar/apk file to a proto
|
|
|
|
// format. The proto definition is available at frameworks/base/tools/aapt2/Resources.proto.
|
2022-09-22 10:41:42 +02:00
|
|
|
func aapt2Convert(ctx android.ModuleContext, out android.WritablePath, in android.Path, format string) {
|
2018-10-30 07:14:58 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: aapt2ConvertRule,
|
|
|
|
Input: in,
|
|
|
|
Output: out,
|
2022-09-22 10:41:42 +02:00
|
|
|
Description: "convert to " + format,
|
|
|
|
Args: map[string]string{
|
|
|
|
"format": format,
|
|
|
|
},
|
2018-10-30 07:14:58 +01:00
|
|
|
})
|
|
|
|
}
|