blob: 014bd54f118ce74c302c75a96e31abe9f1730636 [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 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
17// This file generates the final rules for compiling all Java. All properties related to
18// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
19// functions.
20
21import (
Colin Crossa4f08812018-10-02 22:03:40 -070022 "path/filepath"
Colin Cross30e076a2015-04-13 13:58:27 -070023 "strings"
24
25 "github.com/google/blueprint"
Colin Crossa4f08812018-10-02 22:03:40 -070026 "github.com/google/blueprint/proptools"
Colin Cross30e076a2015-04-13 13:58:27 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Kousik Kumar309b1c02020-05-28 06:13:33 -070029 "android/soong/remoteexec"
Colin Cross30e076a2015-04-13 13:58:27 -070030)
31
32var (
Kousik Kumar309b1c02020-05-28 06:13:33 -070033 Signapk, SignapkRE = remoteexec.StaticRules(pctx, "signapk",
Colin Cross30e076a2015-04-13 13:58:27 -070034 blueprint.RuleParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -070035 Command: `$reTemplate${config.JavaCmd} ${config.JavaVmFlags} -Djava.library.path=$$(dirname ${config.SignapkJniLibrary}) ` +
Colin Crossbdf95142019-08-08 12:56:34 -070036 `-jar ${config.SignapkCmd} $flags $certificates $in $out`,
37 CommandDeps: []string{"${config.SignapkCmd}", "${config.SignapkJniLibrary}"},
Colin Cross30e076a2015-04-13 13:58:27 -070038 },
Kousik Kumar309b1c02020-05-28 06:13:33 -070039 &remoteexec.REParams{Labels: map[string]string{"type": "tool", "name": "signapk"},
40 ExecStrategy: "${config.RESignApkExecStrategy}",
41 Inputs: []string{"${config.SignapkCmd}", "$in", "$$(dirname ${config.SignapkJniLibrary})", "$implicits"},
42 OutputFiles: []string{"$outCommaList"},
43 ToolchainInputs: []string{"${config.JavaCmd}"},
44 Platform: map[string]string{remoteexec.PoolKey: "${config.REJavaPool}"},
45 }, []string{"flags", "certificates"}, []string{"implicits", "outCommaList"})
Colin Cross30e076a2015-04-13 13:58:27 -070046)
47
Colin Cross3bc7ffa2017-11-22 16:19:37 -080048var combineApk = pctx.AndroidStaticRule("combineApk",
49 blueprint.RuleParams{
50 Command: `${config.MergeZipsCmd} $out $in`,
51 CommandDeps: []string{"${config.MergeZipsCmd}"},
Colin Cross30e076a2015-04-13 13:58:27 -070052 })
53
Jaewoong Jungccbb3932019-04-15 09:48:31 -070054func CreateAndSignAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
Liz Kammere2b27f42020-05-07 13:24:05 -070055 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, lineageFile android.Path) {
Colin Cross3bc7ffa2017-11-22 16:19:37 -080056
Colin Crosse560c4a2019-03-19 16:03:11 -070057 unsignedApkName := strings.TrimSuffix(outputFile.Base(), ".apk") + "-unsigned.apk"
58 unsignedApk := android.PathForModuleOut(ctx, unsignedApkName)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080059
Colin Crossa4f08812018-10-02 22:03:40 -070060 var inputs android.Paths
Colin Cross3bc7ffa2017-11-22 16:19:37 -080061 if dexJarFile != nil {
62 inputs = append(inputs, dexJarFile)
63 }
Colin Crossfd94c402018-11-01 14:50:55 -070064 inputs = append(inputs, packageFile)
Colin Crossa4f08812018-10-02 22:03:40 -070065 if jniJarFile != nil {
66 inputs = append(inputs, jniJarFile)
67 }
Colin Cross30e076a2015-04-13 13:58:27 -070068
Colin Crossae887032017-10-23 17:16:14 -070069 ctx.Build(pctx, android.BuildParams{
Colin Cross50ddcc42019-05-16 12:28:22 -070070 Rule: combineApk,
71 Inputs: inputs,
72 Output: unsignedApk,
73 Implicits: deps,
Colin Cross30e076a2015-04-13 13:58:27 -070074 })
75
Liz Kammere2b27f42020-05-07 13:24:05 -070076 SignAppPackage(ctx, outputFile, unsignedApk, certificates, lineageFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070077}
78
Liz Kammere2b27f42020-05-07 13:24:05 -070079func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, lineageFile android.Path) {
Jaewoong Jungccbb3932019-04-15 09:48:31 -070080
Colin Cross30e076a2015-04-13 13:58:27 -070081 var certificateArgs []string
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070082 var deps android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -070083 for _, c := range certificates {
Jiyong Parkc00cbd92018-10-30 21:20:05 +090084 certificateArgs = append(certificateArgs, c.Pem.String(), c.Key.String())
Dan Willemsenc4bd8f82019-04-09 21:26:14 -070085 deps = append(deps, c.Pem, c.Key)
Colin Cross30e076a2015-04-13 13:58:27 -070086 }
87
Kousik Kumar309b1c02020-05-28 06:13:33 -070088 outputFiles := android.WritablePaths{signedApk}
Liz Kammere2b27f42020-05-07 13:24:05 -070089 var flags []string
90 if lineageFile != nil {
91 flags = append(flags, "--lineage", lineageFile.String())
Liz Kammera7126552020-05-08 17:19:26 -070092 deps = append(deps, lineageFile)
Liz Kammere2b27f42020-05-07 13:24:05 -070093 }
94
Kousik Kumar309b1c02020-05-28 06:13:33 -070095 rule := Signapk
96 args := map[string]string{
97 "certificates": strings.Join(certificateArgs, " "),
98 "flags": strings.Join(flags, " "),
99 }
100 if ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
101 rule = SignapkRE
102 args["implicits"] = strings.Join(deps.Strings(), ",")
103 args["outCommaList"] = strings.Join(outputFiles.Strings(), ",")
104 }
Colin Crossae887032017-10-23 17:16:14 -0700105 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700106 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700107 Description: "signapk",
Kousik Kumar309b1c02020-05-28 06:13:33 -0700108 Outputs: outputFiles,
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800109 Input: unsignedApk,
Dan Willemsenc4bd8f82019-04-09 21:26:14 -0700110 Implicits: deps,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700111 Args: args,
Colin Cross30e076a2015-04-13 13:58:27 -0700112 })
Colin Cross30e076a2015-04-13 13:58:27 -0700113}
Colin Crossa97c5d32018-03-28 14:58:31 -0700114
115var buildAAR = pctx.AndroidStaticRule("buildAAR",
116 blueprint.RuleParams{
117 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
118 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
119 `cp ${classesJar} ${outDir}/classes.jar && ` +
120 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700121 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700122 CommandDeps: []string{"${config.SoongZipCmd}"},
123 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700124 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700125
126func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
127 classesJar, manifest, rTxt android.Path, res android.Paths) {
128
129 // TODO(ccross): uniquify and copy resources with dependencies
130
131 deps := android.Paths{manifest, rTxt}
132 classesJarPath := ""
133 if classesJar != nil {
134 deps = append(deps, classesJar)
135 classesJarPath = classesJar.String()
136 }
137
138 ctx.Build(pctx, android.BuildParams{
Colin Crossf57c5782019-01-25 13:20:38 -0800139 Rule: buildAAR,
140 Description: "aar",
141 Implicits: deps,
142 Output: outputFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700143 Args: map[string]string{
144 "manifest": manifest.String(),
145 "classesJar": classesJarPath,
146 "rTxt": rTxt.String(),
147 "outDir": android.PathForModuleOut(ctx, "aar").String(),
148 },
149 })
150}
Colin Crossa4f08812018-10-02 22:03:40 -0700151
Colin Crossf6237212018-10-29 23:14:58 -0700152var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
153 blueprint.RuleParams{
Colin Crossfd94c402018-11-01 14:50:55 -0700154 Command: `${config.MergeZipsCmd} ${out} ${in}`,
155 CommandDeps: []string{"${config.MergeZipsCmd}"},
156 })
157
158var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
159 blueprint.RuleParams{
160 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
161 CommandDeps: []string{"${config.Zip2ZipCmd}"},
162 })
163
164var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
165 blueprint.RuleParams{
166 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
167 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
168 CommandDeps: []string{"${config.Zip2ZipCmd}"},
169 }, "resJar")
Colin Crossf6237212018-10-29 23:14:58 -0700170
171// Builds an app into a module suitable for input to bundletool
172func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossfd94c402018-11-01 14:50:55 -0700173 packageFile, jniJarFile, dexJarFile android.Path) {
Colin Crossf6237212018-10-29 23:14:58 -0700174
175 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
Colin Crossfd94c402018-11-01 14:50:55 -0700176 aapt2Convert(ctx, protoResJarFile, packageFile)
Colin Crossf6237212018-10-29 23:14:58 -0700177
Colin Crossfd94c402018-11-01 14:50:55 -0700178 var zips android.Paths
179
180 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
181 ctx.Build(pctx, android.BuildParams{
182 Rule: bundleMungePackage,
183 Input: protoResJarFile,
184 Output: mungedPackage,
185 Description: "bundle apk",
186 })
187 zips = append(zips, mungedPackage)
188
Colin Crossf6237212018-10-29 23:14:58 -0700189 if dexJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700190 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
191 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
192 ctx.Build(pctx, android.BuildParams{
193 Rule: bundleMungeDexJar,
194 Input: dexJarFile,
195 Output: mungedDexJar,
196 ImplicitOutput: mungedResJar,
197 Description: "bundle dex",
198 Args: map[string]string{
199 "resJar": mungedResJar.String(),
200 },
201 })
202 zips = append(zips, mungedDexJar, mungedResJar)
Colin Crossf6237212018-10-29 23:14:58 -0700203 }
204 if jniJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700205 zips = append(zips, jniJarFile)
Colin Crossf6237212018-10-29 23:14:58 -0700206 }
207
208 ctx.Build(pctx, android.BuildParams{
209 Rule: buildBundleModule,
Colin Crossfd94c402018-11-01 14:50:55 -0700210 Inputs: zips,
Colin Crossf6237212018-10-29 23:14:58 -0700211 Output: outputFile,
212 Description: "bundle",
Colin Crossf6237212018-10-29 23:14:58 -0700213 })
214}
215
Colin Crossa4f08812018-10-02 22:03:40 -0700216func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crosse4246ab2019-02-05 21:55:21 -0800217 jniLibs []jniLib, uncompressJNI bool) {
Colin Crossa4f08812018-10-02 22:03:40 -0700218
219 var deps android.Paths
220 jarArgs := []string{
221 "-j", // junk paths, they will be added back with -P arguments
222 }
223
Colin Crosse4246ab2019-02-05 21:55:21 -0800224 if uncompressJNI {
Peter Collingbournead84f972019-12-17 16:46:18 -0800225 jarArgs = append(jarArgs, "-L", "0")
Colin Crossa4f08812018-10-02 22:03:40 -0700226 }
227
228 for _, j := range jniLibs {
229 deps = append(deps, j.path)
230 jarArgs = append(jarArgs,
Peter Collingbournead84f972019-12-17 16:46:18 -0800231 "-P", targetToJniDir(j.target),
232 "-f", j.path.String())
Colin Crossa4f08812018-10-02 22:03:40 -0700233 }
234
Kousik Kumar366afc52020-05-20 11:27:16 -0700235 rule := zip
236 args := map[string]string{
237 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
238 }
239 if ctx.Config().IsEnvTrue("RBE_ZIP") {
240 rule = zipRE
241 args["implicits"] = strings.Join(deps.Strings(), ",")
242 }
Colin Crossa4f08812018-10-02 22:03:40 -0700243 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700244 Rule: rule,
Colin Crossa4f08812018-10-02 22:03:40 -0700245 Description: "zip jni libs",
246 Output: outputFile,
247 Implicits: deps,
Kousik Kumar366afc52020-05-20 11:27:16 -0700248 Args: args,
Colin Crossa4f08812018-10-02 22:03:40 -0700249 })
250}
251
252func targetToJniDir(target android.Target) string {
253 return filepath.Join("lib", target.Arch.Abi[0])
254}