blob: b9b5f43a787ff683331c557eccf6dde3a05afa73 [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"
Colin Cross30e076a2015-04-13 13:58:27 -070029)
30
31var (
Colin Cross9d45bb72016-08-29 16:14:13 -070032 signapk = pctx.AndroidStaticRule("signapk",
Colin Cross30e076a2015-04-13 13:58:27 -070033 blueprint.RuleParams{
Colin Cross5ab4e6d2017-11-22 16:20:45 -080034 Command: `${config.JavaCmd} -Djava.library.path=$$(dirname $signapkJniLibrary) ` +
35 `-jar $signapkCmd $certificates $in $out`,
36 CommandDeps: []string{"$signapkCmd", "$signapkJniLibrary"},
Colin Cross30e076a2015-04-13 13:58:27 -070037 },
38 "certificates")
39
Colin Cross9d45bb72016-08-29 16:14:13 -070040 androidManifestMerger = pctx.AndroidStaticRule("androidManifestMerger",
Colin Cross30e076a2015-04-13 13:58:27 -070041 blueprint.RuleParams{
42 Command: "java -classpath $androidManifestMergerCmd com.android.manifmerger.Main merge " +
43 "--main $in --libs $libsManifests --out $out",
Dan Willemsenc94a7682015-11-17 15:27:28 -080044 CommandDeps: []string{"$androidManifestMergerCmd"},
Colin Cross67a5c132017-05-09 13:45:28 -070045 Description: "merge manifest files",
Colin Cross30e076a2015-04-13 13:58:27 -070046 },
47 "libsManifests")
48)
49
50func init() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070051 pctx.SourcePathVariable("androidManifestMergerCmd", "prebuilts/devtools/tools/lib/manifest-merger.jar")
52 pctx.HostBinToolVariable("aaptCmd", "aapt")
53 pctx.HostJavaToolVariable("signapkCmd", "signapk.jar")
Colin Cross5ab4e6d2017-11-22 16:20:45 -080054 // TODO(ccross): this should come from the signapk dependencies, but we don't have any way
55 // to express host JNI dependencies yet.
56 pctx.HostJNIToolVariable("signapkJniLibrary", "libconscrypt_openjdk_jni")
Colin Cross30e076a2015-04-13 13:58:27 -070057}
58
Colin Cross3bc7ffa2017-11-22 16:19:37 -080059var combineApk = pctx.AndroidStaticRule("combineApk",
60 blueprint.RuleParams{
61 Command: `${config.MergeZipsCmd} $out $in`,
62 CommandDeps: []string{"${config.MergeZipsCmd}"},
Colin Cross30e076a2015-04-13 13:58:27 -070063 })
64
Colin Cross3bc7ffa2017-11-22 16:19:37 -080065func CreateAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
Colin Crossa4f08812018-10-02 22:03:40 -070066 resJarFile, jniJarFile, dexJarFile android.Path, certificates []certificate) {
Colin Cross3bc7ffa2017-11-22 16:19:37 -080067
68 unsignedApk := android.PathForModuleOut(ctx, "unsigned.apk")
69
Colin Crossa4f08812018-10-02 22:03:40 -070070 var inputs android.Paths
Colin Cross3bc7ffa2017-11-22 16:19:37 -080071 if dexJarFile != nil {
72 inputs = append(inputs, dexJarFile)
73 }
Colin Crossa4f08812018-10-02 22:03:40 -070074 inputs = append(inputs, resJarFile)
75 if jniJarFile != nil {
76 inputs = append(inputs, jniJarFile)
77 }
Colin Cross30e076a2015-04-13 13:58:27 -070078
Colin Crossae887032017-10-23 17:16:14 -070079 ctx.Build(pctx, android.BuildParams{
Colin Cross3bc7ffa2017-11-22 16:19:37 -080080 Rule: combineApk,
81 Inputs: inputs,
82 Output: unsignedApk,
Colin Cross30e076a2015-04-13 13:58:27 -070083 })
84
Colin Cross30e076a2015-04-13 13:58:27 -070085 var certificateArgs []string
86 for _, c := range certificates {
Colin Crosse1731a52017-12-14 11:22:55 -080087 certificateArgs = append(certificateArgs, c.pem.String(), c.key.String())
Colin Cross30e076a2015-04-13 13:58:27 -070088 }
89
Colin Cross5ab4e6d2017-11-22 16:20:45 -080090 // TODO(ccross): sometimes uncompress dex
91 // TODO(ccross): sometimes strip dex
92
Colin Crossae887032017-10-23 17:16:14 -070093 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070094 Rule: signapk,
95 Description: "signapk",
96 Output: outputFile,
Colin Cross3bc7ffa2017-11-22 16:19:37 -080097 Input: unsignedApk,
Colin Cross30e076a2015-04-13 13:58:27 -070098 Args: map[string]string{
99 "certificates": strings.Join(certificateArgs, " "),
100 },
101 })
Colin Cross30e076a2015-04-13 13:58:27 -0700102}
Colin Crossa97c5d32018-03-28 14:58:31 -0700103
104var buildAAR = pctx.AndroidStaticRule("buildAAR",
105 blueprint.RuleParams{
106 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
107 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
108 `cp ${classesJar} ${outDir}/classes.jar && ` +
109 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700110 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700111 CommandDeps: []string{"${config.SoongZipCmd}"},
112 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700113 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700114
115func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
116 classesJar, manifest, rTxt android.Path, res android.Paths) {
117
118 // TODO(ccross): uniquify and copy resources with dependencies
119
120 deps := android.Paths{manifest, rTxt}
121 classesJarPath := ""
122 if classesJar != nil {
123 deps = append(deps, classesJar)
124 classesJarPath = classesJar.String()
125 }
126
127 ctx.Build(pctx, android.BuildParams{
128 Rule: buildAAR,
129 Implicits: deps,
130 Output: outputFile,
131 Args: map[string]string{
132 "manifest": manifest.String(),
133 "classesJar": classesJarPath,
134 "rTxt": rTxt.String(),
135 "outDir": android.PathForModuleOut(ctx, "aar").String(),
136 },
137 })
138}
Colin Crossa4f08812018-10-02 22:03:40 -0700139
140func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
141 jniLibs []jniLib) {
142
143 var deps android.Paths
144 jarArgs := []string{
145 "-j", // junk paths, they will be added back with -P arguments
146 }
147
148 if !ctx.Config().UnbundledBuild() {
149 jarArgs = append(jarArgs, "-L 0")
150 }
151
152 for _, j := range jniLibs {
153 deps = append(deps, j.path)
154 jarArgs = append(jarArgs,
155 "-P "+targetToJniDir(j.target),
156 "-f "+j.path.String())
157 }
158
159 ctx.Build(pctx, android.BuildParams{
160 Rule: zip,
161 Description: "zip jni libs",
162 Output: outputFile,
163 Implicits: deps,
164 Args: map[string]string{
165 "jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "),
166 },
167 })
168}
169
170func targetToJniDir(target android.Target) string {
171 return filepath.Join("lib", target.Arch.Abi[0])
172}