blob: 4348644845fce6a6be87b8ff4cb6fc4f1af2ad4c [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 (
Colin Cross77cdcfd2021-03-12 11:28:25 -080033 Signapk, SignapkRE = pctx.RemoteStaticRules("signapk",
Colin Cross30e076a2015-04-13 13:58:27 -070034 blueprint.RuleParams{
Ulf Adams7e7ef562020-11-25 23:07:23 +010035 Command: `rm -f $out && $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 Kammer70dd74d2020-05-07 13:24:05 -070055 packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate, deps android.Paths, v4SignatureFile android.WritablePath, 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 Kammer70dd74d2020-05-07 13:24:05 -070076 SignAppPackage(ctx, outputFile, unsignedApk, certificates, v4SignatureFile, lineageFile)
Jaewoong Jungccbb3932019-04-15 09:48:31 -070077}
78
Liz Kammer70dd74d2020-05-07 13:24:05 -070079func SignAppPackage(ctx android.ModuleContext, signedApk android.WritablePath, unsignedApk android.Path, certificates []Certificate, v4SignatureFile android.WritablePath, 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
Songchun Fan688de9a2020-03-24 20:32:24 -070088 outputFiles := android.WritablePaths{signedApk}
Liz Kammer70dd74d2020-05-07 13:24:05 -070089 var flags []string
Songchun Fan688de9a2020-03-24 20:32:24 -070090 if v4SignatureFile != nil {
91 outputFiles = append(outputFiles, v4SignatureFile)
Liz Kammer70dd74d2020-05-07 13:24:05 -070092 flags = append(flags, "--enable-v4")
93 }
94
95 if lineageFile != nil {
96 flags = append(flags, "--lineage", lineageFile.String())
Liz Kammer9cd4f812020-05-08 17:19:26 -070097 deps = append(deps, lineageFile)
Songchun Fan688de9a2020-03-24 20:32:24 -070098 }
99
Kousik Kumar309b1c02020-05-28 06:13:33 -0700100 rule := Signapk
101 args := map[string]string{
102 "certificates": strings.Join(certificateArgs, " "),
103 "flags": strings.Join(flags, " "),
104 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400105 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_SIGNAPK") {
Kousik Kumar309b1c02020-05-28 06:13:33 -0700106 rule = SignapkRE
107 args["implicits"] = strings.Join(deps.Strings(), ",")
108 args["outCommaList"] = strings.Join(outputFiles.Strings(), ",")
109 }
Colin Crossae887032017-10-23 17:16:14 -0700110 ctx.Build(pctx, android.BuildParams{
Kousik Kumar309b1c02020-05-28 06:13:33 -0700111 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700112 Description: "signapk",
Songchun Fan688de9a2020-03-24 20:32:24 -0700113 Outputs: outputFiles,
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800114 Input: unsignedApk,
Dan Willemsenc4bd8f82019-04-09 21:26:14 -0700115 Implicits: deps,
Kousik Kumar309b1c02020-05-28 06:13:33 -0700116 Args: args,
Colin Cross30e076a2015-04-13 13:58:27 -0700117 })
Colin Cross30e076a2015-04-13 13:58:27 -0700118}
Colin Crossa97c5d32018-03-28 14:58:31 -0700119
120var buildAAR = pctx.AndroidStaticRule("buildAAR",
121 blueprint.RuleParams{
122 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
123 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
124 `cp ${classesJar} ${outDir}/classes.jar && ` +
125 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700126 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700127 CommandDeps: []string{"${config.SoongZipCmd}"},
128 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700129 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700130
131func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
132 classesJar, manifest, rTxt android.Path, res android.Paths) {
133
134 // TODO(ccross): uniquify and copy resources with dependencies
135
136 deps := android.Paths{manifest, rTxt}
137 classesJarPath := ""
138 if classesJar != nil {
139 deps = append(deps, classesJar)
140 classesJarPath = classesJar.String()
141 }
142
143 ctx.Build(pctx, android.BuildParams{
Colin Crossf57c5782019-01-25 13:20:38 -0800144 Rule: buildAAR,
145 Description: "aar",
146 Implicits: deps,
147 Output: outputFile,
Colin Crossa97c5d32018-03-28 14:58:31 -0700148 Args: map[string]string{
149 "manifest": manifest.String(),
150 "classesJar": classesJarPath,
151 "rTxt": rTxt.String(),
152 "outDir": android.PathForModuleOut(ctx, "aar").String(),
153 },
154 })
155}
Colin Crossa4f08812018-10-02 22:03:40 -0700156
Colin Crossf6237212018-10-29 23:14:58 -0700157var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
158 blueprint.RuleParams{
Colin Crossfd94c402018-11-01 14:50:55 -0700159 Command: `${config.MergeZipsCmd} ${out} ${in}`,
160 CommandDeps: []string{"${config.MergeZipsCmd}"},
161 })
162
163var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
164 blueprint.RuleParams{
165 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
166 CommandDeps: []string{"${config.Zip2ZipCmd}"},
167 })
168
169var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
170 blueprint.RuleParams{
171 Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
172 `${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
173 CommandDeps: []string{"${config.Zip2ZipCmd}"},
174 }, "resJar")
Colin Crossf6237212018-10-29 23:14:58 -0700175
176// Builds an app into a module suitable for input to bundletool
177func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossfd94c402018-11-01 14:50:55 -0700178 packageFile, jniJarFile, dexJarFile android.Path) {
Colin Crossf6237212018-10-29 23:14:58 -0700179
180 protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
Colin Crossfd94c402018-11-01 14:50:55 -0700181 aapt2Convert(ctx, protoResJarFile, packageFile)
Colin Crossf6237212018-10-29 23:14:58 -0700182
Colin Crossfd94c402018-11-01 14:50:55 -0700183 var zips android.Paths
184
185 mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
186 ctx.Build(pctx, android.BuildParams{
187 Rule: bundleMungePackage,
188 Input: protoResJarFile,
189 Output: mungedPackage,
190 Description: "bundle apk",
191 })
192 zips = append(zips, mungedPackage)
193
Colin Crossf6237212018-10-29 23:14:58 -0700194 if dexJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700195 mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
196 mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
197 ctx.Build(pctx, android.BuildParams{
198 Rule: bundleMungeDexJar,
199 Input: dexJarFile,
200 Output: mungedDexJar,
201 ImplicitOutput: mungedResJar,
202 Description: "bundle dex",
203 Args: map[string]string{
204 "resJar": mungedResJar.String(),
205 },
206 })
207 zips = append(zips, mungedDexJar, mungedResJar)
Colin Crossf6237212018-10-29 23:14:58 -0700208 }
209 if jniJarFile != nil {
Colin Crossfd94c402018-11-01 14:50:55 -0700210 zips = append(zips, jniJarFile)
Colin Crossf6237212018-10-29 23:14:58 -0700211 }
212
213 ctx.Build(pctx, android.BuildParams{
214 Rule: buildBundleModule,
Colin Crossfd94c402018-11-01 14:50:55 -0700215 Inputs: zips,
Colin Crossf6237212018-10-29 23:14:58 -0700216 Output: outputFile,
217 Description: "bundle",
Colin Crossf6237212018-10-29 23:14:58 -0700218 })
219}
220
Sam Delmerico82602492022-06-10 17:05:42 +0000221const jniJarOutputPathString = "jniJarOutput.zip"
222
223func TransformJniLibsToJar(
224 ctx android.ModuleContext,
225 outputFile android.WritablePath,
226 jniLibs []jniLib,
227 prebuiltJniPackages android.Paths,
228 uncompressJNI bool) {
Colin Crossa4f08812018-10-02 22:03:40 -0700229
230 var deps android.Paths
231 jarArgs := []string{
232 "-j", // junk paths, they will be added back with -P arguments
233 }
234
Colin Crosse4246ab2019-02-05 21:55:21 -0800235 if uncompressJNI {
Peter Collingbournead84f972019-12-17 16:46:18 -0800236 jarArgs = append(jarArgs, "-L", "0")
Colin Crossa4f08812018-10-02 22:03:40 -0700237 }
238
239 for _, j := range jniLibs {
240 deps = append(deps, j.path)
241 jarArgs = append(jarArgs,
Peter Collingbournead84f972019-12-17 16:46:18 -0800242 "-P", targetToJniDir(j.target),
243 "-f", j.path.String())
Colin Crossa4f08812018-10-02 22:03:40 -0700244 }
245
Kousik Kumar366afc52020-05-20 11:27:16 -0700246 rule := zip
247 args := map[string]string{
248 "jarArgs": strings.Join(proptools.NinjaAndShellEscapeList(jarArgs), " "),
249 }
Ramy Medhat16f23a42020-09-03 01:29:49 -0400250 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
Kousik Kumar366afc52020-05-20 11:27:16 -0700251 rule = zipRE
252 args["implicits"] = strings.Join(deps.Strings(), ",")
253 }
Sam Delmerico82602492022-06-10 17:05:42 +0000254 jniJarPath := android.PathForModuleOut(ctx, jniJarOutputPathString)
Colin Crossa4f08812018-10-02 22:03:40 -0700255 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -0700256 Rule: rule,
Colin Crossa4f08812018-10-02 22:03:40 -0700257 Description: "zip jni libs",
Sam Delmerico82602492022-06-10 17:05:42 +0000258 Output: jniJarPath,
Colin Crossa4f08812018-10-02 22:03:40 -0700259 Implicits: deps,
Kousik Kumar366afc52020-05-20 11:27:16 -0700260 Args: args,
Colin Crossa4f08812018-10-02 22:03:40 -0700261 })
Sam Delmerico82602492022-06-10 17:05:42 +0000262 ctx.Build(pctx, android.BuildParams{
263 Rule: mergeAssetsRule,
264 Description: "merge prebuilt JNI packages",
265 Inputs: append(prebuiltJniPackages, jniJarPath),
266 Output: outputFile,
267 })
Colin Crossa4f08812018-10-02 22:03:40 -0700268}
269
270func targetToJniDir(target android.Target) string {
271 return filepath.Join("lib", target.Arch.Abi[0])
272}