blob: 75774443f2b2d9bc302b36ae52b375f3ed667b06 [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 Crossae887032017-10-23 17:16:14 -070090 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070091 Rule: signapk,
92 Description: "signapk",
93 Output: outputFile,
Colin Cross3bc7ffa2017-11-22 16:19:37 -080094 Input: unsignedApk,
Colin Cross30e076a2015-04-13 13:58:27 -070095 Args: map[string]string{
96 "certificates": strings.Join(certificateArgs, " "),
97 },
98 })
Colin Cross30e076a2015-04-13 13:58:27 -070099}
Colin Crossa97c5d32018-03-28 14:58:31 -0700100
101var buildAAR = pctx.AndroidStaticRule("buildAAR",
102 blueprint.RuleParams{
103 Command: `rm -rf ${outDir} && mkdir -p ${outDir} && ` +
104 `cp ${manifest} ${outDir}/AndroidManifest.xml && ` +
105 `cp ${classesJar} ${outDir}/classes.jar && ` +
106 `cp ${rTxt} ${outDir}/R.txt && ` +
Colin Cross1d98ee22018-09-18 17:05:15 -0700107 `${config.SoongZipCmd} -jar -o $out -C ${outDir} -D ${outDir}`,
Colin Crossa97c5d32018-03-28 14:58:31 -0700108 CommandDeps: []string{"${config.SoongZipCmd}"},
109 },
Colin Cross1d98ee22018-09-18 17:05:15 -0700110 "manifest", "classesJar", "rTxt", "outDir")
Colin Crossa97c5d32018-03-28 14:58:31 -0700111
112func BuildAAR(ctx android.ModuleContext, outputFile android.WritablePath,
113 classesJar, manifest, rTxt android.Path, res android.Paths) {
114
115 // TODO(ccross): uniquify and copy resources with dependencies
116
117 deps := android.Paths{manifest, rTxt}
118 classesJarPath := ""
119 if classesJar != nil {
120 deps = append(deps, classesJar)
121 classesJarPath = classesJar.String()
122 }
123
124 ctx.Build(pctx, android.BuildParams{
125 Rule: buildAAR,
126 Implicits: deps,
127 Output: outputFile,
128 Args: map[string]string{
129 "manifest": manifest.String(),
130 "classesJar": classesJarPath,
131 "rTxt": rTxt.String(),
132 "outDir": android.PathForModuleOut(ctx, "aar").String(),
133 },
134 })
135}
Colin Crossa4f08812018-10-02 22:03:40 -0700136
137func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
138 jniLibs []jniLib) {
139
140 var deps android.Paths
141 jarArgs := []string{
142 "-j", // junk paths, they will be added back with -P arguments
143 }
144
145 if !ctx.Config().UnbundledBuild() {
146 jarArgs = append(jarArgs, "-L 0")
147 }
148
149 for _, j := range jniLibs {
150 deps = append(deps, j.path)
151 jarArgs = append(jarArgs,
152 "-P "+targetToJniDir(j.target),
153 "-f "+j.path.String())
154 }
155
156 ctx.Build(pctx, android.BuildParams{
157 Rule: zip,
158 Description: "zip jni libs",
159 Output: outputFile,
160 Implicits: deps,
161 Args: map[string]string{
162 "jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "),
163 },
164 })
165}
166
167func targetToJniDir(target android.Target) string {
168 return filepath.Join("lib", target.Arch.Abi[0])
169}