blob: 2f9d7018525926e6394c484036f8520fe3e8daf0 [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")
64)
65
66func init() {
67 pctx.StaticVariable("commonJdkFlags", "-source 1.7 -target 1.7 -Xmaxerrs 9999999")
68 pctx.StaticVariable("javacCmd", "javac -J-Xmx1024M $commonJdkFlags")
69 pctx.StaticVariable("jarCmd", filepath.Join(bootstrap.BinDir, "soong_jar"))
70 pctx.VariableFunc("dxCmd", func(c interface{}) (string, error) {
71 return c.(Config).HostBinTool("dx")
72 })
73}
74
75type javaBuilderFlags struct {
76 javacFlags string
77 dxFlags string
78 bootClasspath string
79 classpath string
80}
81
82type jarSpec struct {
83 fileList, dir string
84}
85
86func (j jarSpec) soongJarArgs() string {
87 return "-C " + j.dir + " -l " + j.fileList
88}
89
90func TransformJavaToClasses(ctx common.AndroidModuleContext, srcFiles []string,
91 flags javaBuilderFlags, deps []string) jarSpec {
92
93 classDir := filepath.Join(common.ModuleOutDir(ctx), "classes")
94 classFileList := filepath.Join(classDir, "classes.list")
95
96 ctx.Build(pctx, blueprint.BuildParams{
97 Rule: cc,
98 Outputs: []string{classFileList},
99 Inputs: srcFiles,
100 Implicits: deps,
101 Args: map[string]string{
102 "javacFlags": flags.javacFlags,
103 "bootClasspath": flags.bootClasspath,
104 "classpath": flags.classpath,
105 "outDir": classDir,
106 },
107 })
108
109 return jarSpec{classFileList, classDir}
110}
111
112func TransformClassesToJar(ctx common.AndroidModuleContext, classes []jarSpec,
113 manifest string) string {
114
115 outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes-full-debug.jar")
116
117 deps := []string{}
118 jarArgs := []string{}
119
120 for _, j := range classes {
121 deps = append(deps, j.fileList)
122 jarArgs = append(jarArgs, j.soongJarArgs())
123 }
124
125 if manifest != "" {
126 deps = append(deps, manifest)
127 jarArgs = append(jarArgs, "-m "+manifest)
128 }
129
130 deps = append(deps, "$jarCmd")
131
132 ctx.Build(pctx, blueprint.BuildParams{
133 Rule: jar,
134 Outputs: []string{outputFile},
135 Implicits: deps,
136 Args: map[string]string{
137 "jarArgs": strings.Join(jarArgs, " "),
138 },
139 })
140
141 return outputFile
142}
143
144func TransformClassesJarToDex(ctx common.AndroidModuleContext, classesJar string,
145 flags javaBuilderFlags) string {
146
147 outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes.dex")
148
149 ctx.Build(pctx, blueprint.BuildParams{
150 Rule: dx,
151 Outputs: []string{outputFile},
152 Inputs: []string{classesJar},
153 Implicits: []string{"$dxCmd"},
154 Args: map[string]string{
155 "dxFlags": flags.dxFlags,
156 },
157 })
158
159 return outputFile
160}
161
162func TransformDexToJavaLib(ctx common.AndroidModuleContext, resources []jarSpec,
163 dexFile string) string {
164
165 outputFile := filepath.Join(common.ModuleOutDir(ctx), "javalib.jar")
166 var deps []string
167 var jarArgs []string
168
169 for _, j := range resources {
170 deps = append(deps, j.fileList)
171 jarArgs = append(jarArgs, j.soongJarArgs())
172 }
173
174 dexDir, _ := filepath.Split(dexFile)
175 jarArgs = append(jarArgs, "-C "+dexDir+" -f "+dexFile)
176
177 deps = append(deps, "$jarCmd", dexFile)
178
179 ctx.Build(pctx, blueprint.BuildParams{
180 Rule: jar,
181 Outputs: []string{outputFile},
182 Implicits: deps,
183 Args: map[string]string{
184 "jarArgs": strings.Join(jarArgs, " "),
185 },
186 })
187
188 return outputFile
189}