1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package java
// This file generates the final rules for compiling all Java. All properties related to
// compiling should have been translated into javaBuilderFlags or another argument to the Transform*
// functions.
import (
"path/filepath"
"strings"
"android/soong/common"
"github.com/google/blueprint"
"github.com/google/blueprint/bootstrap"
)
var (
pctx = blueprint.NewPackageContext("android/soong/java")
// Compiling java is not conducive to proper dependency tracking. The path-matches-class-name
// requirement leads to unpredictable generated source file names, and a single .java file
// will get compiled into multiple .class files if it contains inner classes. To work around
// this, all java rules write into separate directories and then a post-processing step lists
// the files in the the directory into a list file that later rules depend on (and sometimes
// read from directly using @<listfile>)
javac = pctx.StaticRule("javac",
blueprint.RuleParams{
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`$javacCmd -encoding UTF-8 $javacFlags $bootClasspath $classpath ` +
`-extdirs "" -d $outDir @$out.rsp || ( rm -rf "$outDir"; exit 41 ) && ` +
`find $outDir -name "*.class" > $out`,
Rspfile: "$out.rsp",
RspfileContent: "$in",
Description: "javac $outDir",
},
"javacCmd", "javacFlags", "bootClasspath", "classpath", "outDir")
jar = pctx.StaticRule("jar",
blueprint.RuleParams{
Command: `$jarCmd -o $out $jarArgs`,
Description: "jar $out",
},
"jarCmd", "jarArgs")
dx = pctx.StaticRule("dx",
blueprint.RuleParams{
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`$dxCmd --dex --output=$outDir $dxFlags $in || ( rm -rf "$outDir"; exit 41 ) && ` +
`find "$outDir" -name "classes*.dex" > $out`,
Description: "dex $out",
},
"outDir", "dxFlags")
jarjar = pctx.StaticRule("jarjar",
blueprint.RuleParams{
Command: "java -jar $jarjarCmd process $rulesFile $in $out",
Description: "jarjar $out",
},
"rulesFile")
extractPrebuilt = pctx.StaticRule("extractPrebuilt",
blueprint.RuleParams{
Command: `rm -rf $outDir && unzip -qo $in -d $outDir && ` +
`find $outDir -name "*.class" > $classFile && ` +
`find $outDir -type f -a \! -name "*.class" -a \! -name "MANIFEST.MF" > $resourceFile || ` +
`(rm -rf $outDir; exit 42)`,
Description: "extract java prebuilt $outDir",
},
"outDir", "classFile", "resourceFile")
)
func init() {
pctx.StaticVariable("commonJdkFlags", "-source 1.7 -target 1.7 -Xmaxerrs 9999999")
pctx.StaticVariable("javacCmd", "javac -J-Xmx1024M $commonJdkFlags")
pctx.StaticVariable("jarCmd", filepath.Join(bootstrap.BinDir, "soong_jar"))
pctx.VariableFunc("dxCmd", func(c interface{}) (string, error) {
return c.(common.Config).HostBinTool("dx")
})
pctx.VariableFunc("jarjarCmd", func(c interface{}) (string, error) {
return c.(common.Config).HostJavaTool("jarjar.jar")
})
}
type javaBuilderFlags struct {
javacFlags string
dxFlags string
bootClasspath string
classpath string
aidlFlags string
}
type jarSpec struct {
fileList, dir string
}
func (j jarSpec) soongJarArgs() string {
return "-C " + j.dir + " -l " + j.fileList
}
func TransformJavaToClasses(ctx common.AndroidModuleContext, srcFiles []string,
flags javaBuilderFlags, deps []string) jarSpec {
classDir := filepath.Join(common.ModuleOutDir(ctx), "classes")
classFileList := filepath.Join(common.ModuleOutDir(ctx), "classes.list")
ctx.Build(pctx, blueprint.BuildParams{
Rule: javac,
Outputs: []string{classFileList},
Inputs: srcFiles,
Implicits: deps,
Args: map[string]string{
"javacFlags": flags.javacFlags,
"bootClasspath": flags.bootClasspath,
"classpath": flags.classpath,
"outDir": classDir,
},
})
return jarSpec{classFileList, classDir}
}
func TransformClassesToJar(ctx common.AndroidModuleContext, classes []jarSpec,
manifest string) string {
outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes-full-debug.jar")
deps := []string{}
jarArgs := []string{}
for _, j := range classes {
deps = append(deps, j.fileList)
jarArgs = append(jarArgs, j.soongJarArgs())
}
if manifest != "" {
deps = append(deps, manifest)
jarArgs = append(jarArgs, "-m "+manifest)
}
deps = append(deps, "$jarCmd")
ctx.Build(pctx, blueprint.BuildParams{
Rule: jar,
Outputs: []string{outputFile},
Implicits: deps,
Args: map[string]string{
"jarArgs": strings.Join(jarArgs, " "),
},
})
return outputFile
}
func TransformClassesJarToDex(ctx common.AndroidModuleContext, classesJar string,
flags javaBuilderFlags) jarSpec {
outDir := filepath.Join(common.ModuleOutDir(ctx), "dex")
outputFile := filepath.Join(common.ModuleOutDir(ctx), "dex.filelist")
ctx.Build(pctx, blueprint.BuildParams{
Rule: dx,
Outputs: []string{outputFile},
Inputs: []string{classesJar},
Implicits: []string{"$dxCmd"},
Args: map[string]string{
"dxFlags": flags.dxFlags,
"outDir": outDir,
},
})
return jarSpec{outputFile, outDir}
}
func TransformDexToJavaLib(ctx common.AndroidModuleContext, resources []jarSpec,
dexJarSpec jarSpec) string {
outputFile := filepath.Join(common.ModuleOutDir(ctx), "javalib.jar")
var deps []string
var jarArgs []string
for _, j := range resources {
deps = append(deps, j.fileList)
jarArgs = append(jarArgs, j.soongJarArgs())
}
deps = append(deps, dexJarSpec.fileList)
jarArgs = append(jarArgs, dexJarSpec.soongJarArgs())
ctx.Build(pctx, blueprint.BuildParams{
Rule: jar,
Outputs: []string{outputFile},
Implicits: deps,
Args: map[string]string{
"jarArgs": strings.Join(jarArgs, " "),
},
})
return outputFile
}
func TransformJarJar(ctx common.AndroidModuleContext, classesJar string, rulesFile string) string {
outputFile := filepath.Join(common.ModuleOutDir(ctx), "classes-jarjar.jar")
ctx.Build(pctx, blueprint.BuildParams{
Rule: jarjar,
Outputs: []string{outputFile},
Inputs: []string{classesJar},
Implicits: []string{"$jarjarCmd"},
Args: map[string]string{
"rulesFile": rulesFile,
},
})
return outputFile
}
func TransformPrebuiltJarToClasses(ctx common.AndroidModuleContext,
prebuilt string) (classJarSpec, resourceJarSpec jarSpec) {
classDir := filepath.Join(common.ModuleOutDir(ctx), "classes")
classFileList := filepath.Join(classDir, "classes.list")
resourceFileList := filepath.Join(classDir, "resources.list")
ctx.Build(pctx, blueprint.BuildParams{
Rule: extractPrebuilt,
Outputs: []string{classFileList, resourceFileList},
Inputs: []string{prebuilt},
Args: map[string]string{
"outDir": classDir,
"classFile": classFileList,
"resourceFile": resourceFileList,
},
})
return jarSpec{classFileList, classDir}, jarSpec{resourceFileList, classDir}
}
|