blob: 4eb03c25eb48d763a50bc498ff90244e92771342 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -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 (
22 "path/filepath"
23 "strings"
24
25 "android/soong/common"
26
27 "github.com/google/blueprint"
28 "github.com/google/blueprint/bootstrap"
29)
30
31var (
32 pctx = blueprint.NewPackageContext("android/soong/java")
33
34 // Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
35 // requirement leads to unpredictable generated source file names, and a single .java file
36 // will get compiled into multiple .class files if it contains inner classes. To work around
37 // this, all java rules write into separate directories and then a post-processing step lists
38 // the files in the the directory into a list file that later rules depend on (and sometimes
39 // read from directly using @<listfile>)
40 cc = pctx.StaticRule("javac",
41 blueprint.RuleParams{
42 Command: `$javacCmd -encoding UTF-8 $javacFlags $bootClasspath $classpath ` +
43 `-extdirs "" -d $outDir @$out.rsp || ( rm -rf $outDir; exit 41 ) && ` +
44 `find $outDir -name "*.class" > $out`,
45 Rspfile: "$out.rsp",
46 RspfileContent: "$in",
47 Description: "javac $outDir",
48 },
49 "javacCmd", "javacFlags", "bootClasspath", "classpath", "outDir")
50
51 jar = pctx.StaticRule("jar",
52 blueprint.RuleParams{
53 Command: `$jarCmd -o $out $jarArgs`,
54 Description: "jar $out",
55 },
56 "jarCmd", "jarArgs")
57
58 dx = pctx.StaticRule("dx",
59 blueprint.RuleParams{
60 Command: "$dxCmd --dex --output=$out $dxFlags $in",
61 Description: "dex $out",
62 },
63 "outDir", "dxFlags")
Colin Crosse1d62a82015-04-03 16:53:05 -070064
Colin Cross65bf4f22015-04-03 16:54:17 -070065 jarjar = pctx.StaticRule("jarjar",
66 blueprint.RuleParams{
67 Command: "java -jar $jarjarCmd process $rulesFile $in $out",
68 Description: "jarjar $out",
69 },
70 "rulesFile")
71
Colin Crosse1d62a82015-04-03 16:53:05 -070072 extractPrebuilt = pctx.StaticRule("extractPrebuilt",
73 blueprint.RuleParams{
74 Command: `rm -rf $outDir && unzip -qo $in -d $outDir && ` +
75 `find $outDir -name "*.class" > $classFile && ` +
76 `find $outDir -type f -a \! -name "*.class" -a \! -name "MANIFEST.MF" > $resourceFile || ` +
77 `(rm -rf $outDir; exit 42)`,
78 Description: "extract java prebuilt $outDir",
79 },
80 "outDir", "classFile", "resourceFile")
Colin Cross2fe66872015-03-30 17:20:39 -070081)
82
83func init() {
84 pctx.StaticVariable("commonJdkFlags", "-source 1.7 -target 1.7 -Xmaxerrs 9999999")
85 pctx.StaticVariable("javacCmd", "javac -J-Xmx1024M $commonJdkFlags")
86 pctx.StaticVariable("jarCmd", filepath.Join(bootstrap.BinDir, "soong_jar"))
87 pctx.VariableFunc("dxCmd", func(c interface{}) (string, error) {
Colin Cross1332b002015-04-07 17:11:30 -070088 return c.(common.Config).HostBinTool("dx")
Colin Cross2fe66872015-03-30 17:20:39 -070089 })
Colin Cross65bf4f22015-04-03 16:54:17 -070090 pctx.VariableFunc("jarjarCmd", func(c interface{}) (string, error) {
Colin Cross1332b002015-04-07 17:11:30 -070091 return c.(common.Config).HostJavaTool("jarjar.jar")
Colin Cross65bf4f22015-04-03 16:54:17 -070092 })
Colin Cross2fe66872015-03-30 17:20:39 -070093}
94
95type javaBuilderFlags struct {
96 javacFlags string
97 dxFlags string
98 bootClasspath string
99 classpath string
100}
101
102type jarSpec struct {
103 fileList, dir string
104}
105
106func (j jarSpec) soongJarArgs() string {
107 return "-C " + j.dir + " -l " + j.fileList
108}
109
110func TransformJavaToClasses(ctx common.AndroidModuleContext, srcFiles []string,
111 flags javaBuilderFlags, deps []string) jarSpec {
112
113 classDir := filepath.Join(common.ModuleOutDir(ctx), "classes")
114 classFileList := filepath.Join(classDir, "classes.list")
115
116 ctx.Build(pctx, blueprint.BuildParams{
117 Rule: cc,
118 Outputs: []string{classFileList},
119 Inputs: srcFiles,
120 Implicits: deps,
121 Args: map[string]string{
122 "javacFlags": flags.javacFlags,
123 "bootClasspath": flags.bootClasspath,
124 "classpath": flags.classpath,
125 "outDir": classDir,
126 },
127 })
128
129 return jarSpec{classFileList, classDir}
130}
131
132func TransformClassesToJar(ctx common.AndroidModuleContext, classes []jarSpec,
133 manifest string) string {
134
135 outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes-full-debug.jar")
136
137 deps := []string{}
138 jarArgs := []string{}
139
140 for _, j := range classes {
141 deps = append(deps, j.fileList)
142 jarArgs = append(jarArgs, j.soongJarArgs())
143 }
144
145 if manifest != "" {
146 deps = append(deps, manifest)
147 jarArgs = append(jarArgs, "-m "+manifest)
148 }
149
150 deps = append(deps, "$jarCmd")
151
152 ctx.Build(pctx, blueprint.BuildParams{
153 Rule: jar,
154 Outputs: []string{outputFile},
155 Implicits: deps,
156 Args: map[string]string{
157 "jarArgs": strings.Join(jarArgs, " "),
158 },
159 })
160
161 return outputFile
162}
163
164func TransformClassesJarToDex(ctx common.AndroidModuleContext, classesJar string,
165 flags javaBuilderFlags) string {
166
167 outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes.dex")
168
169 ctx.Build(pctx, blueprint.BuildParams{
170 Rule: dx,
171 Outputs: []string{outputFile},
172 Inputs: []string{classesJar},
173 Implicits: []string{"$dxCmd"},
174 Args: map[string]string{
175 "dxFlags": flags.dxFlags,
176 },
177 })
178
179 return outputFile
180}
181
182func TransformDexToJavaLib(ctx common.AndroidModuleContext, resources []jarSpec,
183 dexFile string) string {
184
185 outputFile := filepath.Join(common.ModuleOutDir(ctx), "javalib.jar")
186 var deps []string
187 var jarArgs []string
188
189 for _, j := range resources {
190 deps = append(deps, j.fileList)
191 jarArgs = append(jarArgs, j.soongJarArgs())
192 }
193
194 dexDir, _ := filepath.Split(dexFile)
195 jarArgs = append(jarArgs, "-C "+dexDir+" -f "+dexFile)
196
197 deps = append(deps, "$jarCmd", dexFile)
198
199 ctx.Build(pctx, blueprint.BuildParams{
200 Rule: jar,
201 Outputs: []string{outputFile},
202 Implicits: deps,
203 Args: map[string]string{
204 "jarArgs": strings.Join(jarArgs, " "),
205 },
206 })
207
208 return outputFile
209}
Colin Crosse1d62a82015-04-03 16:53:05 -0700210
Colin Cross65bf4f22015-04-03 16:54:17 -0700211func TransformJarJar(ctx common.AndroidModuleContext, classesJar string, rulesFile string) string {
212 outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes-jarjar.jar")
213 ctx.Build(pctx, blueprint.BuildParams{
214 Rule: jarjar,
215 Outputs: []string{outputFile},
216 Inputs: []string{classesJar},
217 Implicits: []string{"$jarjarCmd"},
218 Args: map[string]string{
219 "rulesFile": rulesFile,
220 },
221 })
222
223 return outputFile
224}
225
Colin Crosse1d62a82015-04-03 16:53:05 -0700226func TransformPrebuiltJarToClasses(ctx common.AndroidModuleContext,
227 prebuilt string) (classJarSpec, resourceJarSpec jarSpec) {
228
229 classDir := filepath.Join(common.ModuleOutDir(ctx), "classes")
230 classFileList := filepath.Join(classDir, "classes.list")
231 resourceFileList := filepath.Join(classDir, "resources.list")
232
233 ctx.Build(pctx, blueprint.BuildParams{
234 Rule: extractPrebuilt,
235 Outputs: []string{classFileList, resourceFileList},
236 Inputs: []string{prebuilt},
237 Args: map[string]string{
238 "outDir": classDir,
239 "classFile": classFileList,
240 "resourceFile": resourceFileList,
241 },
242 })
243
244 return jarSpec{classFileList, classDir}, jarSpec{resourceFileList, classDir}
245}