@@ -114,7 +114,7 @@ template("java_library") {
114
114
_data_deps = invoker .data_deps
115
115
}
116
116
117
- # Generates a .java file containing all sources to be compiled
117
+ # Generates a .sources file containing all sources to be compiled
118
118
_java_sources_file = " $target_gen_dir /$target_name .sources"
119
119
if (defined (invoker .java_sources_file )) {
120
120
_java_sources_file = invoker .java_sources_file
@@ -176,6 +176,162 @@ template("java_library") {
176
176
}
177
177
}
178
178
179
+ # Declare a Java executable target
180
+ #
181
+ # Manifext.txt: contains the given class as the entrypoint about the files packaged in a Java executable target.
182
+ #
183
+ # sources: List of .java files included in this binary. Mutually exclusive with jar_path.
184
+ #
185
+ # jar_path: A path to an existing JAR. Mutually exclusive with sources.
186
+ #
187
+ # output_name: File name for the output binary under root_build_dir/bin.
188
+ #
189
+ # javac_flags: additional flags to pass to the javac compiler
190
+ #
191
+ template (" java_binary" ) {
192
+ # Figure out the output name
193
+ _jar_name = target_name
194
+ if (defined (invoker .output_name )) {
195
+ _jar_name = invoker .output_name
196
+ } else {
197
+ _jar_name += " .jar"
198
+ }
199
+
200
+ _deps = []
201
+ if (defined (invoker .deps )) {
202
+ _deps = invoker .deps
203
+ }
204
+
205
+ # What files will be compiled
206
+ _java_files = []
207
+ if (defined (invoker .sources )) {
208
+ _java_files = invoker .sources
209
+ }
210
+
211
+ _is_prebuilt = defined (invoker .jar_path )
212
+
213
+ _jar_output = " "
214
+ _target_dir_name = get_label_info (" :$target_name " , " dir" )
215
+ if (_is_prebuilt ) {
216
+ assert (_java_files == [])
217
+ _jar_output = " $root_out_dir /bin/$_target_dir_name /" +
218
+ get_path_info (invoker .jar_path , " name" ) + " .jar"
219
+ } else {
220
+ _jar_output = " $root_out_dir /bin/$_target_dir_name /$_jar_name "
221
+ }
222
+
223
+ # Generate a list containing the expected build_config filepath of every dependency.
224
+ _deps_configs = []
225
+ foreach (_dep , _deps ) {
226
+ _dep_gen_dir = get_label_info (_dep , " target_gen_dir" )
227
+ _dep_name = get_label_info (_dep , " name" )
228
+ _dep_config = " $_dep_gen_dir /$_dep_name .json"
229
+ _deps_configs += [ _dep_config ]
230
+ }
231
+ _rebased_deps_configs = rebase_path (_deps_configs , root_build_dir )
232
+
233
+ # Create the name for this target's build_config.
234
+ _library_target_name = target_name
235
+ _build_config = " $target_gen_dir /$_library_target_name .json"
236
+ _rebased_build_config = rebase_path (_build_config , root_build_dir )
237
+
238
+ # Write the build_config file for this target.
239
+ _config_target_name = target_name + " _config"
240
+ action (_config_target_name ) {
241
+ script = write_build_config
242
+
243
+ deps = _deps
244
+
245
+ outputs = [ _build_config ]
246
+ args = [
247
+ " --jar-path" ,
248
+ rebase_path (_jar_output , root_build_dir ),
249
+ " --build-config" ,
250
+ _rebased_build_config ,
251
+ " --deps-configs=$_rebased_deps_configs " ,
252
+ ]
253
+ }
254
+
255
+ # Building from sources - perform Java compilation and JAR creation.
256
+ if (! _is_prebuilt ) {
257
+ # Additional flags
258
+ _javac_flags = [
259
+ " -Werror" ,
260
+ " -Xlint:all" ,
261
+ ]
262
+ if (defined (invoker .javac_flags )) {
263
+ _javac_flags += invoker .javac_flags
264
+ }
265
+
266
+ # Data deps
267
+ _data_deps = []
268
+ if (defined (invoker .data_deps )) {
269
+ _data_deps = invoker .data_deps
270
+ }
271
+
272
+ # Generates a .sources file containing all sources to be compiled
273
+ _java_sources_file = " $target_gen_dir /$target_name .sources"
274
+ if (defined (invoker .java_sources_file )) {
275
+ _java_sources_file = invoker .java_sources_file
276
+ }
277
+ write_file (_java_sources_file , rebase_path (_java_files , root_build_dir ))
278
+
279
+ # Compiles the given files into a directory and generates a 'class list'
280
+ _javac_target_name = target_name + " __javac"
281
+ _class_dir = rebase_path (target_out_dir , root_build_dir ) + " /classes"
282
+ _class_list_file = " $target_gen_dir /$target_name .classlist"
283
+ action (_javac_target_name ) {
284
+ sources = _java_files
285
+ deps = [ " :$_config_target_name " ]
286
+
287
+ outputs = [ _class_list_file ]
288
+
289
+ script = javac_runner
290
+
291
+ args = [
292
+ " --classdir" ,
293
+ _class_dir ,
294
+ " --outfile" ,
295
+ rebase_path (_class_list_file , root_build_dir ),
296
+ " --build-config" ,
297
+ _rebased_build_config ,
298
+ " --" ,
299
+ " -d" ,
300
+ _class_dir ,
301
+ " @" + rebase_path (_java_sources_file , root_build_dir ),
302
+ ] + _javac_flags
303
+ }
304
+
305
+ # Bundles all files within the 'class directory' into a jar file
306
+ action (target_name ) {
307
+ deps = [ " :$_javac_target_name " ] + _deps
308
+
309
+ data_deps = _data_deps
310
+
311
+ outputs = [ _jar_output ]
312
+
313
+ script = jar_runner
314
+
315
+ args = [
316
+ " cfm" ,
317
+ rebase_path (_jar_output , root_build_dir ),
318
+ " Manifest.txt" ,
319
+ " -C" ,
320
+ _class_dir ,
321
+ " ." ,
322
+ ]
323
+ }
324
+ } else {
325
+ # Using pre-specified JAR instead of building from sources - simply copy the JAR to the output directory.
326
+ _original_jar_path = invoker .jar_path
327
+ copy (target_name ) {
328
+ sources = [ _original_jar_path ]
329
+ outputs = [ _jar_output ]
330
+ deps = [ " :$_config_target_name " ] + _deps
331
+ }
332
+ }
333
+ }
334
+
179
335
template (" android_library" ) {
180
336
java_library (target_name ) {
181
337
forward_variables_from (invoker , " *" )
@@ -194,6 +350,24 @@ template("android_library") {
194
350
}
195
351
}
196
352
353
+ template (" android_binary" ) {
354
+ java_binary (target_name ) {
355
+ forward_variables_from (invoker , " *" )
356
+
357
+ if (! defined (javac_flags )) {
358
+ javac_flags = []
359
+ }
360
+
361
+ javac_flags += [
362
+ " -Xlint:-options" ,
363
+ " -source" ,
364
+ " 8" ,
365
+ " -target" ,
366
+ " 8" ,
367
+ ]
368
+ }
369
+ }
370
+
197
371
template (" java_prebuilt" ) {
198
372
java_library (target_name ) {
199
373
forward_variables_from (invoker , " *" )
0 commit comments