Reland "Roll buildtools fa660d47..ecc8e253"
[chromium-blink-merge.git] / build / config / android / rules.gni
blob57af185bf57bc78c2f06370682fa8b144f931c21
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import("//base/android/linker/config.gni")
6 import("//build/config/android/config.gni")
7 import("//build/config/android/internal_rules.gni")
8 import("//third_party/android_platform/config.gni")
9 import("//tools/grit/grit_rule.gni")
11 assert(is_android)
13 # Declare a jni target
15 # This target generates the native jni bindings for a set of .java files.
17 # See base/android/jni_generator/jni_generator.py for more info about the
18 # format of generating JNI bindings.
20 # Variables
21 #   sources: list of .java files to generate jni for
22 #   jni_package: subdirectory path for generated bindings
24 # Example
25 #   generate_jni("foo_jni") {
26 #     sources = [
27 #       "android/java/src/org/chromium/foo/Foo.java",
28 #       "android/java/src/org/chromium/foo/FooUtil.java",
29 #     ]
30 #     jni_package = "foo"
31 #   }
32 template("generate_jni") {
33   set_sources_assignment_filter([])
34   if (defined(invoker.testonly)) {
35     testonly = invoker.testonly
36   }
38   assert(defined(invoker.sources))
39   assert(defined(invoker.jni_package))
40   jni_package = invoker.jni_package
41   base_output_dir = "${target_gen_dir}/${target_name}"
42   package_output_dir = "${base_output_dir}/${jni_package}"
43   jni_output_dir = "${package_output_dir}/jni"
45   jni_generator_include = "//base/android/jni_generator/jni_generator_helper.h"
47   foreach_target_name = "${target_name}__jni_gen"
48   action_foreach(foreach_target_name) {
49     script = "//base/android/jni_generator/jni_generator.py"
50     depfile = "$target_gen_dir/$target_name.{{source_name_part}}.d"
51     sources = invoker.sources
52     outputs = [
53       depfile,
54       "${jni_output_dir}/{{source_name_part}}_jni.h",
55     ]
57     args = [
58       "--depfile",
59       rebase_path(depfile, root_build_dir),
60       "--input_file={{source}}",
61       "--optimize_generation=1",
62       "--ptr_type=long",
63       "--output_dir",
64       rebase_path(jni_output_dir, root_build_dir),
65       "--includes",
66       rebase_path(jni_generator_include, jni_output_dir),
67       "--native_exports_optional",
68     ]
69     if (defined(invoker.jni_generator_jarjar_file)) {
70       args += [
71         "--jarjar",
72         rebase_path(jni_generator_jarjar_file, root_build_dir),
73       ]
74     }
75   }
77   config("jni_includes_${target_name}") {
78     # TODO(cjhopman): #includes should probably all be relative to
79     # base_output_dir. Remove that from this config once the includes are
80     # updated.
81     include_dirs = [
82       base_output_dir,
83       package_output_dir,
84     ]
85   }
87   group(target_name) {
88     deps = [
89       ":$foreach_target_name",
90     ]
91     public_configs = [ ":jni_includes_${target_name}" ]
93     if (defined(invoker.deps)) {
94       deps += invoker.deps
95     }
96     if (defined(invoker.public_deps)) {
97       public_deps = invoker.public_deps
98     }
100     if (defined(invoker.visibility)) {
101       visibility = invoker.visibility
102     }
103   }
106 # Declare a jni target for a prebuilt jar
108 # This target generates the native jni bindings for a set of classes in a .jar.
110 # See base/android/jni_generator/jni_generator.py for more info about the
111 # format of generating JNI bindings.
113 # Variables
114 #   classes: list of .class files in the jar to generate jni for. These should
115 #     include the full path to the .class file.
116 #   jni_package: subdirectory path for generated bindings
117 #   jar_file: the path to the .jar. If not provided, will default to the sdk's
118 #     android.jar
120 #   deps, public_deps: As normal
122 # Example
123 #   generate_jar_jni("foo_jni") {
124 #     classes = [
125 #       "android/view/Foo.class",
126 #     ]
127 #     jni_package = "foo"
128 #   }
129 template("generate_jar_jni") {
130   set_sources_assignment_filter([])
131   if (defined(invoker.testonly)) {
132     testonly = invoker.testonly
133   }
135   assert(defined(invoker.classes))
136   assert(defined(invoker.jni_package))
138   if (defined(invoker.jar_file)) {
139     jar_file = invoker.jar_file
140   } else {
141     jar_file = android_sdk_jar
142   }
144   jni_package = invoker.jni_package
145   base_output_dir = "${root_gen_dir}/${target_name}/${jni_package}"
146   jni_output_dir = "${base_output_dir}/jni"
148   jni_generator_include = "//base/android/jni_generator/jni_generator_helper.h"
150   # TODO(cjhopman): make jni_generator.py support generating jni for multiple
151   # .class files from a .jar.
152   jni_actions = []
153   foreach(class, invoker.classes) {
154     _classname_list = []
155     _classname_list = process_file_template([ class ], "{{source_name_part}}")
156     classname = _classname_list[0]
157     jni_target_name = "${target_name}__jni_${classname}"
158     jni_actions += [ ":$jni_target_name" ]
159     action(jni_target_name) {
160       # The sources aren't compiled so don't check their dependencies.
161       check_includes = false
162       depfile = "$target_gen_dir/$target_name.d"
163       script = "//base/android/jni_generator/jni_generator.py"
164       sources = [
165         jar_file,
166       ]
167       outputs = [
168         depfile,
169         "${jni_output_dir}/${classname}_jni.h",
170       ]
172       args = [
173         "--depfile",
174         rebase_path(depfile, root_build_dir),
175         "--jar_file",
176         rebase_path(jar_file, root_build_dir),
177         "--input_file",
178         class,
179         "--optimize_generation=1",
180         "--ptr_type=long",
181         "--output_dir",
182         rebase_path(jni_output_dir, root_build_dir),
183         "--includes",
184         rebase_path(jni_generator_include, jni_output_dir),
185         "--native_exports_optional",
186       ]
187     }
188   }
190   config("jni_includes_${target_name}") {
191     include_dirs = [ base_output_dir ]
192   }
194   group(target_name) {
195     deps = jni_actions
196     if (defined(invoker.deps)) {
197       deps += invoker.deps
198     }
199     if (defined(invoker.public_deps)) {
200       public_deps = invoker.public_deps
201     }
202     public_configs = [ ":jni_includes_${target_name}" ]
203   }
206 # Declare a target for c-preprocessor-generated java files
208 # NOTE: For generating Java conterparts to enums prefer using the java_cpp_enum
209 #       rule instead.
211 # This target generates java files using the host C pre-processor. Each file in
212 # sources will be compiled using the C pre-processor. If include_path is
213 # specified, it will be passed (with --I) to the pre-processor.
215 # This target will create a single .srcjar. Adding this target to an
216 # android_library target's srcjar_deps will make the generated java files be
217 # included in that library's final outputs.
219 # Variables
220 #   sources: list of files to be processed by the C pre-processor. For each
221 #     file in sources, there will be one .java file in the final .srcjar. For a
222 #     file named FooBar.template, a java file will be created with name
223 #     FooBar.java.
224 #   inputs: additional compile-time dependencies. Any files
225 #     `#include`-ed in the templates should be listed here.
226 #   package_name: this will be the subdirectory for each .java file in the
227 #     .srcjar.
229 # Example
230 #   java_cpp_template("foo_generated_enum") {
231 #     sources = [
232 #       "android/java/templates/Foo.template",
233 #     ]
234 #     inputs = [
235 #       "android/java/templates/native_foo_header.h",
236 #     ]
238 #     package_name = "org/chromium/base/library_loader"
239 #     include_path = "android/java/templates"
240 #   }
241 template("java_cpp_template") {
242   set_sources_assignment_filter([])
243   if (defined(invoker.testonly)) {
244     testonly = invoker.testonly
245   }
247   assert(defined(invoker.sources))
248   package_name = invoker.package_name + ""
250   if (defined(invoker.include_path)) {
251     include_path = invoker.include_path + ""
252   } else {
253     include_path = "//"
254   }
256   apply_gcc_target_name = "${target_name}__apply_gcc"
257   zip_srcjar_target_name = "${target_name}__zip_srcjar"
258   final_target_name = target_name
260   action_foreach(apply_gcc_target_name) {
261     visibility = [ ":$zip_srcjar_target_name" ]
262     script = "//build/android/gyp/gcc_preprocess.py"
263     if (defined(invoker.inputs)) {
264       inputs = invoker.inputs + []
265     }
266     depfile = "${target_gen_dir}/${target_name}_{{source_name_part}}.d"
268     sources = invoker.sources
270     if (defined(invoker.deps)) {
271       deps = invoker.deps
272     }
273     if (defined(invoker.public_deps)) {
274       public_deps = invoker.public_deps
275     }
276     if (defined(invoker.data_deps)) {
277       data_deps = invoker.data_deps
278     }
280     gen_dir =
281         "${target_gen_dir}/${target_name}/java_cpp_template/${package_name}"
282     gcc_template_output_pattern = "${gen_dir}/{{source_name_part}}.java"
284     outputs = [
285       depfile,
286       gcc_template_output_pattern,
287     ]
289     args = [
290       "--depfile",
291       rebase_path(depfile, root_build_dir),
292       "--include-path",
293       rebase_path(include_path, root_build_dir),
294       "--output",
295       rebase_path(gen_dir, root_build_dir) + "/{{source_name_part}}.java",
296       "--template={{source}}",
297     ]
299     if (defined(invoker.defines)) {
300       foreach(def, invoker.defines) {
301         args += [
302           "--defines",
303           def,
304         ]
305       }
306     }
307   }
309   apply_gcc_outputs = get_target_outputs(":$apply_gcc_target_name")
310   base_gen_dir = get_label_info(":$apply_gcc_target_name", "target_gen_dir")
312   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
313   zip(zip_srcjar_target_name) {
314     visibility = [ ":$final_target_name" ]
315     inputs = apply_gcc_outputs
316     output = srcjar_path
317     base_dir = base_gen_dir
318     deps = [
319       ":$apply_gcc_target_name",
320     ]
321   }
323   group(final_target_name) {
324     if (defined(invoker.visibility)) {
325       visibility = invoker.visibility
326     }
327     deps = [
328       ":$zip_srcjar_target_name",
329     ]
330   }
333 # Declare a target for generating Java classes from C++ enums.
335 # This target generates Java files from C++ enums using a script.
337 # This target will create a single .srcjar. Adding this target to an
338 # android_library target's srcjar_deps will make the generated java files be
339 # included in that library's final outputs.
341 # Variables
342 #   sources: list of files to be processed by the script. For each annotated
343 #     enum contained in the sources files the script will generate a .java
344 #     file with the same name as the name of the enum.
346 #   outputs: list of outputs, relative to the output_dir. These paths are
347 #     verified at build time by the script. To get the list programatically run:
348 #       python build/android/gyp/java_cpp_enum.py \
349 #         --print_output_only . path/to/header/file.h
351 # Example
352 #   java_cpp_enum("foo_generated_enum") {
353 #     sources = [
354 #       "src/native_foo_header.h",
355 #     ]
356 #     outputs = [
357 #       "org/chromium/FooEnum.java",
358 #     ]
359 #   }
360 template("java_cpp_enum") {
361   set_sources_assignment_filter([])
362   if (defined(invoker.testonly)) {
363     testonly = invoker.testonly
364   }
366   assert(defined(invoker.sources))
367   assert(defined(invoker.outputs))
369   generate_enum_target_name = "${target_name}__generate_enum"
370   zip_srcjar_target_name = "${target_name}__zip_srcjar"
371   final_target_name = target_name
373   action(generate_enum_target_name) {
374     visibility = [ ":$zip_srcjar_target_name" ]
376     # The sources aren't compiled so don't check their dependencies.
377     check_includes = false
379     sources = invoker.sources
380     script = "//build/android/gyp/java_cpp_enum.py"
381     gen_dir = "${target_gen_dir}/${target_name}/enums"
382     outputs =
383         get_path_info(rebase_path(invoker.outputs, ".", gen_dir), "abspath")
385     args = []
386     foreach(output, rebase_path(outputs, root_build_dir)) {
387       args += [
388         "--assert_file",
389         output,
390       ]
391     }
392     args += [ rebase_path(gen_dir, root_build_dir) ]
393     args += rebase_path(invoker.sources, root_build_dir)
394   }
396   generate_enum_outputs = get_target_outputs(":$generate_enum_target_name")
397   base_gen_dir = get_label_info(":$generate_enum_target_name", "target_gen_dir")
399   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
400   zip(zip_srcjar_target_name) {
401     visibility = [ ":$final_target_name" ]
402     inputs = generate_enum_outputs
403     output = srcjar_path
404     base_dir = base_gen_dir
405     deps = [
406       ":$generate_enum_target_name",
407     ]
408   }
410   group(final_target_name) {
411     if (defined(invoker.visibility)) {
412       visibility = invoker.visibility
413     }
414     deps = [
415       ":$zip_srcjar_target_name",
416     ]
417   }
420 # Declare a target for processing a Jinja template.
422 # Variables
423 #   input: The template file to be processed.
424 #   output: Where to save the result.
425 #   variables: (Optional) A list of variables to make available to the template
426 #     processing environment, e.g. ["name=foo", "color=red"].
428 # Example
429 #   jinja_template("chrome_shell_manifest") {
430 #     input = "shell/java/AndroidManifest.xml"
431 #     output = "$target_gen_dir/AndroidManifest.xml"
432 #   }
433 template("jinja_template") {
434   set_sources_assignment_filter([])
435   if (defined(invoker.testonly)) {
436     testonly = invoker.testonly
437   }
439   assert(defined(invoker.input))
440   assert(defined(invoker.output))
442   action(target_name) {
443     if (defined(invoker.visibility)) {
444       visibility = invoker.visibility
445     }
447     sources = [
448       invoker.input,
449     ]
450     script = "//build/android/gyp/jinja_template.py"
451     depfile = "$target_gen_dir/$target_name.d"
453     outputs = [
454       depfile,
455       invoker.output,
456     ]
458     args = [
459       "--inputs",
460       rebase_path(invoker.input, root_build_dir),
461       "--output",
462       rebase_path(invoker.output, root_build_dir),
463       "--depfile",
464       rebase_path(depfile, root_build_dir),
465     ]
466     if (defined(invoker.variables)) {
467       variables = invoker.variables
468       args += [ "--variables=${variables}" ]
469     }
470   }
473 # Declare a target for processing Android resources as Jinja templates.
475 # This takes an Android resource directory where each resource is a Jinja
476 # template, processes each template, then packages the results in a zip file
477 # which can be consumed by an android resources, library, or apk target.
479 # If this target is included in the deps of an android resources/library/apk,
480 # the resources will be included with that target.
482 # Variables
483 #   resources: The list of resources files to process.
484 #   res_dir: The resource directory containing the resources.
485 #   variables: (Optional) A list of variables to make available to the template
486 #     processing environment, e.g. ["name=foo", "color=red"].
488 # Example
489 #   jinja_template_resources("chrome_shell_template_resources") {
490 #     res_dir = "shell/res_template"
491 #     resources = ["shell/res_template/xml/syncable.xml"]
492 #     variables = ["color=red"]
493 #   }
494 template("jinja_template_resources") {
495   set_sources_assignment_filter([])
496   if (defined(invoker.testonly)) {
497     testonly = invoker.testonly
498   }
500   assert(defined(invoker.resources))
501   assert(defined(invoker.res_dir))
503   _base_path = "$target_gen_dir/$target_name"
504   _resources_zip = _base_path + ".resources.zip"
505   _build_config = _base_path + ".build_config"
507   write_build_config("${target_name}__build_config") {
508     build_config = _build_config
509     resources_zip = _resources_zip
510     type = "android_resources"
511   }
513   action("${target_name}__template") {
514     sources = invoker.resources
515     script = "//build/android/gyp/jinja_template.py"
516     depfile = "$target_gen_dir/$target_name.d"
518     outputs = [
519       depfile,
520       _resources_zip,
521     ]
523     rebased_resources = rebase_path(invoker.resources, root_build_dir)
524     args = [
525       "--inputs=${rebased_resources}",
526       "--inputs-base-dir",
527       rebase_path(invoker.res_dir, root_build_dir),
528       "--outputs-zip",
529       rebase_path(_resources_zip, root_build_dir),
530       "--depfile",
531       rebase_path(depfile, root_build_dir),
532     ]
533     if (defined(invoker.variables)) {
534       variables = invoker.variables
535       args += [ "--variables=${variables}" ]
536     }
537   }
539   group(target_name) {
540     deps = [
541       ":${target_name}__build_config",
542       ":${target_name}__template",
543     ]
544   }
547 # Creates a resources.zip with locale.pak files placed into appropriate
548 # resource configs (e.g. en-GB.pak -> res/raw-en/en_gb.pak). Also generates
549 # a locale_paks TypedArray so that resource files can be enumerated at runtime.
551 # If this target is included in the deps of an android resources/library/apk,
552 # the resources will be included with that target.
554 # Variables:
555 #   sources: List of .pak files. Names must be of the form "en.pak" or
556 #       "en-US.pak".
557 #   deps: (optional) List of dependencies that might be needed to generate
558 #       the .pak files.
560 # Example
561 #   locale_pak_resources("locale_paks") {
562 #     sources = [ "path/en-US.pak", "path/fr.pak", ... ]
563 #   }
564 template("locale_pak_resources") {
565   set_sources_assignment_filter([])
566   assert(defined(invoker.sources))
568   _base_path = "$target_gen_dir/$target_name"
569   _resources_zip = _base_path + ".resources.zip"
570   _build_config = _base_path + ".build_config"
572   write_build_config("${target_name}__build_config") {
573     build_config = _build_config
574     resources_zip = _resources_zip
575     type = "android_resources"
576   }
578   action("${target_name}__create_resources_zip") {
579     sources = invoker.sources
580     script = "//build/android/gyp/locale_pak_resources.py"
581     depfile = "$target_gen_dir/$target_name.d"
583     outputs = [
584       depfile,
585       _resources_zip,
586     ]
588     _rebased_sources = rebase_path(invoker.sources, root_build_dir)
589     args = [
590       "--locale-paks=${_rebased_sources}",
591       "--resources-zip",
592       rebase_path(_resources_zip, root_build_dir),
593       "--depfile",
594       rebase_path(depfile, root_build_dir),
595     ]
597     if (defined(invoker.deps)) {
598       deps = invoker.deps
599     }
600   }
602   group(target_name) {
603     deps = [
604       ":${target_name}__build_config",
605       ":${target_name}__create_resources_zip",
606     ]
607   }
610 # Declare an Android resources target
612 # This creates a resources zip file that will be used when building an Android
613 # library or apk and included into a final apk.
615 # To include these resources in a library/apk, this target should be listed in
616 # the library's deps. A library/apk will also include any resources used by its
617 # own dependencies.
619 # Variables
620 #   deps: Specifies the dependencies of this target. Any Android resources
621 #     listed in deps will be included by libraries/apks that depend on this
622 #     target.
623 #   resource_dirs: List of directories containing resources for this target.
624 #   android_manifest: AndroidManifest.xml for this target. Defaults to
625 #     //build/android/AndroidManifest.xml.
626 #   custom_package: java package for generated .java files.
627 #   v14_skip: If true, don't run v14 resource generator on this. Defaults to
628 #     false. (see build/android/gyp/generate_v14_compatible_resources.py)
630 #   shared_resources: If true make a resource package that can be loaded by a
631 #     different application at runtime to access the package's resources.
634 # Example
635 #   android_resources("foo_resources") {
636 #     deps = [":foo_strings_grd"]
637 #     resource_dirs = ["res"]
638 #     custom_package = "org.chromium.foo"
639 #   }
640 template("android_resources") {
641   set_sources_assignment_filter([])
642   if (defined(invoker.testonly)) {
643     testonly = invoker.testonly
644   }
646   assert(defined(invoker.resource_dirs))
647   assert(defined(invoker.android_manifest) || defined(invoker.custom_package))
649   base_path = "$target_gen_dir/$target_name"
650   zip_path = base_path + ".resources.zip"
651   srcjar_path = base_path + ".srcjar"
652   r_text_path = base_path + "_R.txt"
653   build_config = base_path + ".build_config"
655   build_config_target_name = "${target_name}__build_config"
656   process_resources_target_name = "${target_name}__process_resources"
657   final_target_name = target_name
659   write_build_config(build_config_target_name) {
660     visibility = [ ":$process_resources_target_name" ]
662     type = "android_resources"
663     resources_zip = zip_path
664     srcjar = srcjar_path
665     r_text = r_text_path
666     if (defined(invoker.deps)) {
667       deps = invoker.deps
668     }
669     if (defined(invoker.android_manifest)) {
670       android_manifest = invoker.android_manifest
671     }
672     if (defined(invoker.custom_package)) {
673       custom_package = invoker.custom_package
674     }
675   }
677   android_manifest = "//build/android/AndroidManifest.xml"
678   if (defined(invoker.android_manifest)) {
679     android_manifest = invoker.android_manifest
680   }
682   process_resources(process_resources_target_name) {
683     visibility = [ ":$final_target_name" ]
685     resource_dirs = invoker.resource_dirs
686     if (defined(invoker.custom_package)) {
687       custom_package = invoker.custom_package
688     }
690     if (defined(invoker.v14_skip)) {
691       v14_skip = invoker.v14_skip
692     }
694     if (defined(invoker.shared_resources)) {
695       shared_resources = invoker.shared_resources
696     }
698     deps = [
699       ":$build_config_target_name",
700     ]
701     if (defined(invoker.deps)) {
702       # Invoker may have added deps that generate the input resources.
703       deps += invoker.deps
704     }
705   }
707   group(final_target_name) {
708     if (defined(invoker.visibility)) {
709       visibility = invoker.visibility
710     }
711     deps = [
712       ":${target_name}__process_resources",
713     ]
714   }
717 # Declare a target that generates localized strings.xml from a .grd file.
719 # If this target is included in the deps of an android resources/library/apk,
720 # the strings.xml will be included with that target.
722 # Variables
723 #   deps: Specifies the dependencies of this target.
724 #   grd_file: Path to the .grd file to generate strings.xml from.
725 #   outputs: Expected grit outputs (see grit rule).
727 # Example
728 #  java_strings_grd("foo_strings_grd") {
729 #    grd_file = "foo_strings.grd"
730 #  }
731 template("java_strings_grd") {
732   set_sources_assignment_filter([])
733   if (defined(invoker.testonly)) {
734     testonly = invoker.testonly
735   }
737   base_path = "$target_gen_dir/$target_name"
738   resources_zip = base_path + ".resources.zip"
739   build_config = base_path + ".build_config"
741   write_build_config("${target_name}__build_config") {
742     type = "android_resources"
743     if (defined(invoker.deps)) {
744       deps = invoker.deps
745     }
746   }
748   # Put grit files into this subdirectory of target_gen_dir.
749   extra_output_path = target_name + "_grit_output"
751   grit_target_name = "${target_name}__grit"
752   grit_output_dir = "$target_gen_dir/$extra_output_path"
753   grit(grit_target_name) {
754     grit_flags = [
755       "-E",
756       "ANDROID_JAVA_TAGGED_ONLY=false",
757     ]
758     output_dir = grit_output_dir
759     resource_ids = ""
760     source = invoker.grd_file
761     outputs = invoker.outputs
762   }
764   # This needs to get outputs from grit's internal target, not the final
765   # source_set.
766   generate_strings_outputs = get_target_outputs(":${grit_target_name}_grit")
768   zip("${target_name}__zip") {
769     base_dir = grit_output_dir
770     inputs = generate_strings_outputs
771     output = resources_zip
772     deps = [
773       ":$grit_target_name",
774     ]
775   }
777   group(target_name) {
778     deps = [
779       ":${target_name}__build_config",
780       ":${target_name}__zip",
781     ]
782   }
785 # Declare a target that packages strings.xml generated from a grd file.
787 # If this target is included in the deps of an android resources/library/apk,
788 # the strings.xml will be included with that target.
790 # Variables
791 #  grit_output_dir: directory containing grit-generated files.
792 #  generated_files: list of android resource files to package.
794 # Example
795 #  java_strings_grd_prebuilt("foo_strings_grd") {
796 #    grit_output_dir = "$root_gen_dir/foo/grit"
797 #    generated_files = [
798 #      "values/strings.xml"
799 #    ]
800 #  }
801 template("java_strings_grd_prebuilt") {
802   set_sources_assignment_filter([])
803   if (defined(invoker.testonly)) {
804     testonly = invoker.testonly
805   }
807   base_path = "$target_gen_dir/$target_name"
808   resources_zip = base_path + ".resources.zip"
809   build_config = base_path + ".build_config"
811   build_config_target_name = "${target_name}__build_config"
812   zip_target_name = "${target_name}__zip"
813   final_target_name = target_name
815   write_build_config(build_config_target_name) {
816     visibility = [ ":$zip_target_name" ]
817     type = "android_resources"
818   }
820   zip(zip_target_name) {
821     visibility = [ ":$final_target_name" ]
823     base_dir = invoker.grit_output_dir
824     inputs = rebase_path(invoker.generated_files, ".", base_dir)
825     output = resources_zip
826     deps = [
827       ":$build_config_target_name",
828     ]
829     if (defined(invoker.deps)) {
830       deps += invoker.deps
831     }
832   }
834   group(final_target_name) {
835     if (defined(invoker.visibility)) {
836       visibility = invoker.visibility
837     }
838     deps = [
839       ":$zip_target_name",
840     ]
841   }
844 # Declare a Java executable target
846 # This target creates an executable from java code and libraries. The executable
847 # will be in the output folder's /bin/ directory.
849 # Variables
850 #   deps: Specifies the dependencies of this target. Java targets in this list
851 #     will be included in the executable (and the javac classpath).
853 #   java_files: List of .java files included in this library.
854 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
855 #     will be added to java_files and be included in this library.
856 #   srcjars: List of srcjars to be included in this library, together with the
857 #     ones obtained from srcjar_deps.
859 #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
860 #     dependencies for this target. This will allow depending on an
861 #     android_library target, for example.
863 #   chromium_code: If true, extra analysis warning/errors will be enabled.
865 #   data_deps, testonly
867 # Example
868 #   java_binary("foo") {
869 #     java_files = [ "org/chromium/foo/FooMain.java" ]
870 #     deps = [ ":bar_java" ]
871 #     main_class = "org.chromium.foo.FooMain"
872 #   }
873 template("java_binary") {
874   set_sources_assignment_filter([])
876   # TODO(cjhopman): This should not act like a java_library for dependents (i.e.
877   # dependents shouldn't get the jar in their classpath, etc.).
878   java_library_impl(target_name) {
879     if (defined(invoker.DEPRECATED_java_in_dir)) {
880       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
881     }
882     if (defined(invoker.chromium_code)) {
883       chromium_code = invoker.chromium_code
884     }
885     if (defined(invoker.data_deps)) {
886       deps = invoker.data_deps
887     }
888     if (defined(invoker.deps)) {
889       deps = invoker.deps
890     }
891     if (defined(invoker.java_files)) {
892       java_files = invoker.java_files
893     }
894     if (defined(invoker.srcjar_deps)) {
895       srcjar_deps = invoker.srcjar_deps
896     }
897     if (defined(invoker.srcjars)) {
898       srcjars = invoker.srcjars
899     }
900     if (defined(invoker.bypass_platform_checks)) {
901       bypass_platform_checks = invoker.bypass_platform_checks
902     }
903     if (defined(invoker.testonly)) {
904       testonly = invoker.testonly
905     }
907     main_class = invoker.main_class
908   }
911 # Declare a Junit executable target
913 # This target creates an executable from java code for running as a junit test
914 # suite. The executable will be in the output folder's /bin/ directory.
916 # Variables
917 #   deps: Specifies the dependencies of this target. Java targets in this list
918 #     will be included in the executable (and the javac classpath).
920 #   java_files: List of .java files included in this library.
921 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
922 #     will be added to java_files and be included in this library.
923 #   srcjars: List of srcjars to be included in this library, together with the
924 #     ones obtained from srcjar_deps.
926 #   chromium_code: If true, extra analysis warning/errors will be enabled.
928 # Example
929 #   junit_binary("foo") {
930 #     java_files = [ "org/chromium/foo/FooTest.java" ]
931 #     deps = [ ":bar_java" ]
932 #   }
933 template("junit_binary") {
934   set_sources_assignment_filter([])
936   java_binary(target_name) {
937     bypass_platform_checks = true
938     main_class = "org.chromium.testing.local.JunitTestMain"
939     testonly = true
941     if (defined(invoker.DEPRECATED_java_in_dir)) {
942       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
943     }
944     if (defined(invoker.chromium_code)) {
945       chromium_code = invoker.chromium_code
946     }
947     deps = [
948       "//testing/android/junit:junit_test_support",
949       "//third_party/junit",
950       "//third_party/mockito:mockito_java",
951       "//third_party/robolectric:robolectric_java",
952       "//third_party/robolectric:android-all-4.3_r2-robolectric-0",
953     ]
954     if (defined(invoker.deps)) {
955       deps += invoker.deps
956     }
957     if (defined(invoker.java_files)) {
958       java_files = invoker.java_files
959     }
960     if (defined(invoker.srcjar_deps)) {
961       srcjar_deps = invoker.srcjar_deps
962     }
963     if (defined(invoker.srcjars)) {
964       srcjars = invoker.srcjars
965     }
966   }
969 # Declare a java library target
971 # Variables
972 #   deps: Specifies the dependencies of this target. Java targets in this list
973 #     will be added to the javac classpath.
975 #   java_files: List of .java files included in this library.
976 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
977 #     will be added to java_files and be included in this library.
978 #   srcjars: List of srcjars to be included in this library, together with the
979 #     ones obtained from srcjar_deps.
980 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
981 #     this directory will be included in the library. This is only supported to
982 #     ease the gyp->gn conversion and will be removed in the future.
984 #   chromium_code: If true, extra analysis warning/errors will be enabled.
985 #   jar_excluded_patterns: List of patterns of .class files to exclude from the
986 #     final jar.
988 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
989 #     be used to remove unwanted parts of the library.
990 #   proguard_config: Path to the proguard config for preprocessing.
992 #   supports_android: If true, Android targets (android_library, android_apk)
993 #     may depend on this target. Note: if true, this target must only use the
994 #     subset of Java available on Android.
995 #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
996 #     dependencies for this target. This will allow depending on an
997 #     android_library target, for example.
999 #   data_deps, testonly
1001 # Example
1002 #   java_library("foo_java") {
1003 #     java_files = [
1004 #       "org/chromium/foo/Foo.java",
1005 #       "org/chromium/foo/FooInterface.java",
1006 #       "org/chromium/foo/FooService.java",
1007 #     ]
1008 #     deps = [
1009 #       ":bar_java"
1010 #     ]
1011 #     srcjar_deps = [
1012 #       ":foo_generated_enum"
1013 #     ]
1014 #     jar_excluded_patterns = [
1015 #       "*/FooService.class", "*/FooService##*.class"
1016 #     ]
1017 #   }
1018 template("java_library") {
1019   set_sources_assignment_filter([])
1020   java_library_impl(target_name) {
1021     if (defined(invoker.DEPRECATED_java_in_dir)) {
1022       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
1023     }
1024     if (defined(invoker.chromium_code)) {
1025       chromium_code = invoker.chromium_code
1026     }
1027     if (defined(invoker.data_deps)) {
1028       deps = invoker.data_deps
1029     }
1030     if (defined(invoker.deps)) {
1031       deps = invoker.deps
1032     }
1033     if (defined(invoker.jar_excluded_patterns)) {
1034       jar_excluded_patterns = invoker.jar_excluded_patterns
1035     }
1036     if (defined(invoker.java_files)) {
1037       java_files = invoker.java_files
1038     }
1039     if (defined(invoker.proguard_config)) {
1040       proguard_config = invoker.proguard_config
1041     }
1042     if (defined(invoker.proguard_preprocess)) {
1043       proguard_preprocess = invoker.proguard_preprocess
1044     }
1045     if (defined(invoker.srcjar_deps)) {
1046       srcjar_deps = invoker.srcjar_deps
1047     }
1048     if (defined(invoker.srcjars)) {
1049       srcjars = invoker.srcjars
1050     }
1051     if (defined(invoker.bypass_platform_checks)) {
1052       bypass_platform_checks = invoker.bypass_platform_checks
1053     }
1054     if (defined(invoker.testonly)) {
1055       testonly = invoker.testonly
1056     }
1057     if (defined(invoker.jar_path)) {
1058       jar_path = invoker.jar_path
1059     }
1061     if (defined(invoker.supports_android) && invoker.supports_android) {
1062       supports_android = true
1063     }
1064   }
1067 # Declare a java library target for a prebuilt jar
1069 # Variables
1070 #   deps: Specifies the dependencies of this target. Java targets in this list
1071 #     will be added to the javac classpath.
1072 #   jar_path: Path to the prebuilt jar.
1073 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
1074 #     be used to remove unwanted parts of the library.
1075 #   proguard_config: Path to the proguard config for preprocessing.
1077 # Example
1078 #   java_prebuilt("foo_java") {
1079 #     jar_path = "foo.jar"
1080 #     deps = [
1081 #       ":foo_resources",
1082 #       ":bar_java"
1083 #     ]
1084 #   }
1085 template("java_prebuilt") {
1086   set_sources_assignment_filter([])
1087   java_prebuilt_impl(target_name) {
1088     jar_path = invoker.jar_path
1089     if (defined(invoker.testonly)) {
1090       testonly = invoker.testonly
1091     }
1092     if (defined(invoker.deps)) {
1093       deps = invoker.deps
1094     }
1095     if (defined(invoker.proguard_config)) {
1096       proguard_config = invoker.proguard_config
1097     }
1098     if (defined(invoker.proguard_preprocess)) {
1099       proguard_preprocess = invoker.proguard_preprocess
1100     }
1101   }
1104 # Declare an Android library target
1106 # This target creates an Android library containing java code and Android
1107 # resources.
1109 # Variables
1110 #   deps: Specifies the dependencies of this target. Java targets in this list
1111 #     will be added to the javac classpath. Android resources in dependencies
1112 #     will be used when building this library.
1114 #   java_files: List of .java files included in this library.
1115 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1116 #     will be added to java_files and be included in this library.
1117 #   srcjars: List of srcjars to be included in this library, together with the
1118 #     ones obtained from srcjar_deps.
1119 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1120 #     this directory will be included in the library. This is only supported to
1121 #     ease the gyp->gn conversion and will be removed in the future.
1123 #   chromium_code: If true, extra analysis warning/errors will be enabled.
1124 #   jar_excluded_patterns: List of patterns of .class files to exclude from the
1125 #     final jar.
1127 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
1128 #     be used to remove unwanted parts of the library.
1129 #   proguard_config: Path to the proguard config for preprocessing.
1131 #   dex_path: If set, the resulting .dex.jar file will be placed under this
1132 #     path.
1135 # Example
1136 #   android_library("foo_java") {
1137 #     java_files = [
1138 #       "android/org/chromium/foo/Foo.java",
1139 #       "android/org/chromium/foo/FooInterface.java",
1140 #       "android/org/chromium/foo/FooService.java",
1141 #     ]
1142 #     deps = [
1143 #       ":bar_java"
1144 #     ]
1145 #     srcjar_deps = [
1146 #       ":foo_generated_enum"
1147 #     ]
1148 #     jar_excluded_patterns = [
1149 #       "*/FooService.class", "*/FooService##*.class"
1150 #     ]
1151 #   }
1152 template("android_library") {
1153   set_sources_assignment_filter([])
1154   assert(!defined(invoker.jar_path),
1155          "android_library does not support a custom jar path")
1156   java_library_impl(target_name) {
1157     if (defined(invoker.DEPRECATED_java_in_dir)) {
1158       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
1159     }
1160     if (defined(invoker.chromium_code)) {
1161       chromium_code = invoker.chromium_code
1162     }
1163     if (defined(invoker.data_deps)) {
1164       deps = invoker.data_deps
1165     }
1166     if (defined(invoker.deps)) {
1167       deps = invoker.deps
1168     }
1169     if (defined(invoker.jar_excluded_patterns)) {
1170       jar_excluded_patterns = invoker.jar_excluded_patterns
1171     }
1172     if (defined(invoker.java_files)) {
1173       java_files = invoker.java_files
1174     }
1175     if (defined(invoker.proguard_config)) {
1176       proguard_config = invoker.proguard_config
1177     }
1178     if (defined(invoker.proguard_preprocess)) {
1179       proguard_preprocess = invoker.proguard_preprocess
1180     }
1181     if (defined(invoker.srcjar_deps)) {
1182       srcjar_deps = invoker.srcjar_deps
1183     }
1184     if (defined(invoker.srcjars)) {
1185       srcjars = invoker.srcjars
1186     }
1187     if (defined(invoker.testonly)) {
1188       testonly = invoker.testonly
1189     }
1190     if (defined(invoker.visibility)) {
1191       visibility = invoker.visibility
1192     }
1193     if (defined(invoker.dex_path)) {
1194       dex_path = invoker.dex_path
1195     }
1196     if (defined(invoker.manifest_entries)) {
1197       manifest_entries = invoker.manifest_entries
1198     }
1200     supports_android = true
1201     requires_android = true
1203     if (!defined(jar_excluded_patterns)) {
1204       jar_excluded_patterns = []
1205     }
1206     jar_excluded_patterns += [
1207       "*/R.class",
1208       "*/R##*.class",
1209       "*/Manifest.class",
1210       "*/Manifest##*.class",
1211     ]
1212   }
1215 # Declare a target that packages a set of Java dependencies into a standalone
1216 # .dex.jar.
1218 # Variables
1219 #   deps: specifies the dependencies of this target. Android libraries in deps
1220 #     will be packaged into the resulting .dex.jar file.
1221 #   dex_path: location at which the output file will be put
1222 template("android_standalone_library") {
1223   set_sources_assignment_filter([])
1224   deps_dex(target_name) {
1225     deps = invoker.deps
1226     dex_path = invoker.dex_path
1227     if (defined(invoker.excluded_jars)) {
1228       excluded_jars = invoker.excluded_jars
1229     }
1230   }
1233 # Declare an Android library target for a prebuilt jar
1235 # This target creates an Android library containing java code and Android
1236 # resources.
1238 # Variables
1239 #   deps: Specifies the dependencies of this target. Java targets in this list
1240 #     will be added to the javac classpath. Android resources in dependencies
1241 #     will be used when building this library.
1242 #   jar_path: Path to the prebuilt jar.
1243 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
1244 #     be used to remove unwanted parts of the library.
1245 #   proguard_config: Path to the proguard config for preprocessing.
1247 # Example
1248 #   android_java_prebuilt("foo_java") {
1249 #     jar_path = "foo.jar"
1250 #     deps = [
1251 #       ":foo_resources",
1252 #       ":bar_java"
1253 #     ]
1254 #   }
1255 template("android_java_prebuilt") {
1256   set_sources_assignment_filter([])
1257   java_prebuilt_impl(target_name) {
1258     jar_path = invoker.jar_path
1259     supports_android = true
1260     requires_android = true
1261     if (defined(invoker.testonly)) {
1262       testonly = invoker.testonly
1263     }
1264     if (defined(invoker.deps)) {
1265       deps = invoker.deps
1266     }
1267     if (defined(invoker.proguard_config)) {
1268       proguard_config = invoker.proguard_config
1269     }
1270     if (defined(invoker.proguard_preprocess)) {
1271       proguard_preprocess = invoker.proguard_preprocess
1272     }
1273   }
1276 # Declare an Android apk target
1278 # This target creates an Android APK containing java code, resources, assets,
1279 # and (possibly) native libraries.
1281 # Variables
1282 #   android_manifest: Path to AndroidManifest.xml.
1283 #   data_deps: List of dependencies needed at runtime. These will be built but
1284 #     won't change the generated .apk in any way (in fact they may be built
1285 #     after the .apk is).
1286 #   deps: List of dependencies. All Android java resources and libraries in the
1287 #     "transitive closure" of these dependencies will be included in the apk.
1288 #     Note: this "transitive closure" actually only includes such targets if
1289 #     they are depended on through android_library or android_resources targets
1290 #     (and so not through builtin targets like 'action', 'group', etc).
1291 #   java_files: List of .java files to include in the apk.
1292 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1293 #      will be added to java_files and be included in this apk.
1294 #   apk_name: Name for final apk.
1295 #   final_apk_path: Path to final built apk. Default is
1296 #     $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
1297 #   native_libs: List paths of native libraries to include in this apk. If these
1298 #     libraries depend on other shared_library targets, those dependencies will
1299 #     also be included in the apk.
1300 #   apk_under_test: For an instrumentation test apk, this is the target of the
1301 #     tested apk.
1302 #   include_all_resources - If true include all resource IDs in all generated
1303 #     R.java files.
1304 #   testonly: Marks this target as "test-only".
1306 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1307 #     this directory will be included in the library. This is only supported to
1308 #     ease the gyp->gn conversion and will be removed in the future.
1310 # Example
1311 #   android_apk("foo_apk") {
1312 #     android_manifest = "AndroidManifest.xml"
1313 #     java_files = [
1314 #       "android/org/chromium/foo/FooApplication.java",
1315 #       "android/org/chromium/foo/FooActivity.java",
1316 #     ]
1317 #     deps = [
1318 #       ":foo_support_java"
1319 #       ":foo_resources"
1320 #     ]
1321 #     srcjar_deps = [
1322 #       ":foo_generated_enum"
1323 #     ]
1324 #     native_libs = [
1325 #       native_lib_path
1326 #     ]
1327 #   }
1328 template("android_apk") {
1329   set_sources_assignment_filter([])
1330   if (defined(invoker.testonly)) {
1331     testonly = invoker.testonly
1332   }
1334   assert(defined(invoker.final_apk_path) || defined(invoker.apk_name))
1335   assert(defined(invoker.android_manifest))
1336   gen_dir = "$target_gen_dir/$target_name"
1337   base_path = "$gen_dir/$target_name"
1338   _build_config = "$target_gen_dir/$target_name.build_config"
1339   resources_zip_path = "$base_path.resources.zip"
1340   _all_resources_zip_path = "$base_path.resources.all.zip"
1341   jar_path = "$base_path.jar"
1342   _template_name = target_name
1344   final_dex_path = "$gen_dir/classes.dex"
1345   final_dex_target_name = "${_template_name}__final_dex"
1347   _final_apk_path = ""
1348   if (defined(invoker.final_apk_path)) {
1349     _final_apk_path = invoker.final_apk_path
1350   } else if (defined(invoker.apk_name)) {
1351     _final_apk_path = "$root_build_dir/apks/" + invoker.apk_name + ".apk"
1352   }
1353   _dist_jar_path_list =
1354       process_file_template(
1355           [ _final_apk_path ],
1356           "$root_build_dir/test.lib.java/{{source_name_part}}.jar")
1357   _dist_jar_path = _dist_jar_path_list[0]
1359   _native_libs = []
1361   _version_code = "1"
1362   if (defined(invoker.version_code)) {
1363     _version_code = invoker.version_code
1364   }
1366   _version_name = "Developer Build"
1367   if (defined(invoker.version_name)) {
1368     _version_name = invoker.version_name
1369   }
1370   _keystore_path = android_default_keystore_path
1371   _keystore_name = android_default_keystore_name
1372   _keystore_password = android_default_keystore_password
1374   if (defined(invoker.keystore_path)) {
1375     _keystore_path = invoker.keystore_path
1376     _keystore_name = invoker.keystore_name
1377     _keystore_password = invoker.keystore_password
1378   }
1380   _srcjar_deps = []
1381   if (defined(invoker.srcjar_deps)) {
1382     _srcjar_deps += invoker.srcjar_deps
1383   }
1385   _load_library_from_apk = false
1387   # The dependency that makes the chromium linker, if any is needed.
1388   _chromium_linker_dep = []
1390   if (defined(invoker.native_libs)) {
1391     _use_chromium_linker = false
1392     if (defined(invoker.use_chromium_linker)) {
1393       _use_chromium_linker =
1394           invoker.use_chromium_linker && chromium_linker_supported
1395       _chromium_linker_dep = [ "//base/android/linker:chromium_android_linker" ]
1396     }
1398     if (defined(invoker.load_library_from_apk) &&
1399         invoker.load_library_from_apk) {
1400       _load_library_from_apk = true
1401       assert(_use_chromium_linker,
1402              "Loading library from the apk requires use" +
1403                  " of the Chromium linker.")
1404     }
1406     _enable_relocation_packing = false
1407     if (defined(invoker.enable_relocation_packing) &&
1408         invoker.enable_relocation_packing) {
1409       _enable_relocation_packing = relocation_packing_supported
1410       assert(_use_chromium_linker,
1411              "Relocation packing requires use of the" + " Chromium linker.")
1412     }
1414     _native_libs = process_file_template(
1415             invoker.native_libs,
1416             "$root_build_dir/lib.stripped/{{source_file_part}}")
1418     _native_libs_dir = base_path + "/libs"
1420     if (_use_chromium_linker) {
1421       _native_libs +=
1422           [ "$root_build_dir/lib.stripped/libchromium_android_linker.so" ]
1423     }
1425     _enable_relocation_packing = false
1426     if (_use_chromium_linker && defined(invoker.enable_relocation_packing) &&
1427         invoker.enable_relocation_packing) {
1428       _enable_relocation_packing = true
1429     }
1431     _native_lib_version_name = ""
1432     if (defined(invoker.native_lib_version_name)) {
1433       _native_lib_version_name = invoker.native_lib_version_name
1434     }
1435   }
1437   _android_manifest = invoker.android_manifest
1438   _rebased_build_config = rebase_path(_build_config, root_build_dir)
1439   _create_abi_split =
1440       defined(invoker.create_abi_split) && invoker.create_abi_split
1441   _create_density_splits =
1442       defined(invoker.create_density_splits) && invoker.create_density_splits
1444   # Help GN understand that _create_abi_split is not unused (bug in GN).
1445   assert(_create_abi_split || true)
1447   build_config_target = "${_template_name}__build_config"
1448   write_build_config(build_config_target) {
1449     type = "android_apk"
1450     dex_path = final_dex_path
1451     resources_zip = resources_zip_path
1452     build_config = _build_config
1453     android_manifest = _android_manifest
1455     deps = _chromium_linker_dep
1456     if (defined(invoker.deps)) {
1457       deps += invoker.deps
1458     }
1460     if (defined(invoker.apk_under_test)) {
1461       apk_under_test = invoker.apk_under_test
1462     }
1464     native_libs = _native_libs
1465   }
1467   final_deps = []
1469   process_resources_target = "${_template_name}__process_resources"
1470   final_deps += [ ":$process_resources_target" ]
1471   process_resources(process_resources_target) {
1472     srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
1473     r_text_path = "${target_gen_dir}/${target_name}_R.txt"
1474     android_manifest = _android_manifest
1475     resource_dirs = [ "//build/android/ant/empty/res" ]
1476     zip_path = resources_zip_path
1477     all_resources_zip_path = _all_resources_zip_path
1478     generate_constant_ids = true
1480     if (defined(invoker.include_all_resources)) {
1481       include_all_resources = invoker.include_all_resources
1482     }
1484     build_config = _build_config
1485     deps = [
1486       ":$build_config_target",
1487     ]
1488     if (defined(invoker.deps)) {
1489       deps += invoker.deps
1490     }
1491   }
1492   _srcjar_deps += [ ":$process_resources_target" ]
1494   if (_native_libs != []) {
1495     _enable_chromium_linker_tests = false
1496     if (defined(invoker.enable_chromium_linker_tests)) {
1497       _enable_chromium_linker_tests = invoker.enable_chromium_linker_tests
1498     }
1500     java_cpp_template("${_template_name}__native_libraries_java") {
1501       package_name = "org/chromium/base/library_loader"
1502       sources = [
1503         "//base/android/java/templates/NativeLibraries.template",
1504       ]
1505       inputs = [
1506         _build_config,
1507       ]
1508       deps = [
1509         ":$build_config_target",
1510       ]
1512       defines = [
1513         "NATIVE_LIBRARIES_LIST=" +
1514             "@FileArg($_rebased_build_config:native:java_libraries_list)",
1515         "NATIVE_LIBRARIES_VERSION_NUMBER=\"$_native_lib_version_name\"",
1516       ]
1517       if (_use_chromium_linker) {
1518         defines += [ "ENABLE_CHROMIUM_LINKER" ]
1519       }
1520       if (_load_library_from_apk) {
1521         defines += [ "ENABLE_CHROMIUM_LINKER_LIBRARY_IN_ZIP_FILE" ]
1522       }
1523       if (_enable_chromium_linker_tests) {
1524         defines += [ "ENABLE_CHROMIUM_LINKER_TESTS" ]
1525       }
1526     }
1527     _srcjar_deps += [ ":${_template_name}__native_libraries_java" ]
1528   }
1530   java_target = "${_template_name}__java"
1531   final_deps += [ ":$java_target" ]
1532   java_library_impl(java_target) {
1533     supports_android = true
1534     requires_android = true
1535     override_build_config = _build_config
1536     deps = [
1537       ":$build_config_target",
1538     ]
1540     android_manifest = _android_manifest
1541     chromium_code = true
1542     if (defined(invoker.java_files)) {
1543       java_files = invoker.java_files
1544     } else if (defined(invoker.DEPRECATED_java_in_dir)) {
1545       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
1546     } else {
1547       java_files = []
1548     }
1549     srcjar_deps = _srcjar_deps
1550     dex_path = base_path + ".dex.jar"
1552     if (defined(invoker.deps)) {
1553       deps += invoker.deps
1554     }
1555   }
1557   if (_dist_jar_path != "") {
1558     create_dist_target = "${_template_name}__create_dist_jar"
1559     final_deps += [ ":$create_dist_target" ]
1561     # TODO(cjhopman): This is only ever needed to calculate the list of tests to
1562     # run. See build/android/pylib/instrumentation/test_jar.py. We should be
1563     # able to just do that calculation at build time instead.
1564     action(create_dist_target) {
1565       script = "//build/android/gyp/create_dist_jar.py"
1566       depfile = "$target_gen_dir/$target_name.d"
1567       inputs = [
1568         _build_config,
1569       ]
1570       outputs = [
1571         depfile,
1572         _dist_jar_path,
1573       ]
1574       args = [
1575         "--depfile",
1576         rebase_path(depfile, root_build_dir),
1577         "--output",
1578         rebase_path(_dist_jar_path, root_build_dir),
1579         "--inputs=@FileArg($_rebased_build_config:dist_jar:dependency_jars)",
1580       ]
1581       inputs += [ jar_path ]
1582       _rebased_jar_path = rebase_path([ jar_path ], root_build_dir)
1583       args += [ "--inputs=$_rebased_jar_path" ]
1584       deps = [
1585         ":$build_config_target",  # Generates the build config file.
1586         ":$java_target",  # Generates the jar file.
1587       ]
1588     }
1589   }
1591   final_deps += [ ":$final_dex_target_name" ]
1592   dex("$final_dex_target_name") {
1593     deps = [
1594       ":$build_config_target",
1595       ":$java_target",
1596     ]
1597     sources = [
1598       jar_path,
1599     ]
1600     inputs = [
1601       _build_config,
1602     ]
1603     output = final_dex_path
1604     dex_arg_key = "${_rebased_build_config}:final_dex:dependency_dex_files"
1605     args = [ "--inputs=@FileArg($dex_arg_key)" ]
1606   }
1608   if (_native_libs != []) {
1609     action("${_template_name}__prepare_native") {
1610       script = "//build/android/gyp/pack_arm_relocations.py"
1611       packed_libraries_dir = "$_native_libs_dir/$android_app_abi"
1612       depfile = "$target_gen_dir/$target_name.d"
1613       outputs = [
1614         depfile,
1615       ]
1617       inputs = _native_libs
1618       deps = _chromium_linker_dep
1620       inputs += [ _build_config ]
1621       deps += [ ":$build_config_target" ]
1623       skip_packing_list = [
1624         "gdbserver",
1625         "libchromium_android_linker.so",
1626       ]
1628       enable_packing_arg = 0
1629       if (_enable_relocation_packing) {
1630         enable_packing_arg = 1
1631         deps += [ relocation_packer_target ]
1632       }
1634       args = [
1635         "--depfile",
1636         rebase_path(depfile, root_build_dir),
1637         "--enable-packing=$enable_packing_arg",
1638         "--exclude-packing-list=$skip_packing_list",
1639         "--android-pack-relocations",
1640         rebase_path(relocation_packer_exe, root_build_dir),
1641         "--stripped-libraries-dir",
1642         rebase_path(root_build_dir, root_build_dir),
1643         "--packed-libraries-dir",
1644         rebase_path(packed_libraries_dir, root_build_dir),
1645         "--libraries=@FileArg(${_rebased_build_config}:native:libraries)",
1646         "--clear-dir",
1647       ]
1649       if (defined(invoker.deps)) {
1650         deps += invoker.deps
1651       }
1652       if (defined(invoker.public_deps)) {
1653         public_deps = invoker.public_deps
1654       }
1655       if (defined(invoker.data_deps)) {
1656         data_deps = invoker.data_deps
1657       }
1659       if (is_debug) {
1660         rebased_gdbserver = rebase_path([ android_gdbserver ], root_build_dir)
1661         inputs += [ android_gdbserver ]
1662         args += [ "--libraries=$rebased_gdbserver" ]
1663       }
1664     }
1665   }
1667   final_deps += [ ":${_template_name}__create" ]
1668   create_apk("${_template_name}__create") {
1669     apk_path = _final_apk_path
1670     android_manifest = _android_manifest
1671     resources_zip = _all_resources_zip_path
1672     dex_path = final_dex_path
1673     load_library_from_apk = _load_library_from_apk
1674     create_density_splits = _create_density_splits
1675     if (defined(invoker.extensions_to_not_compress)) {
1676       extensions_to_not_compress = invoker.extensions_to_not_compress
1677     } else {
1678       # Allow icu data and v8 snapshots to be loaded directly from the .apk.
1679       extensions_to_not_compress = "dat,bin"
1680     }
1682     version_code = _version_code
1683     version_name = _version_name
1685     keystore_name = _keystore_name
1686     keystore_path = _keystore_path
1687     keystore_password = _keystore_password
1689     # This target generates the input file _all_resources_zip_path.
1690     deps = [
1691       ":$process_resources_target",
1692       ":$final_dex_target_name",
1693     ]
1694     if (defined(invoker.deps)) {
1695       deps += invoker.deps
1696     }
1698     if (defined(invoker.asset_location)) {
1699       asset_location = invoker.asset_location
1701       # We don't know the exact dependencies that create the assets in
1702       # |asset_location|; we depend on all caller deps until a better solution
1703       # is figured out (http://crbug.com/433330).
1704       if (defined(invoker.deps)) {
1705         deps += invoker.deps
1706       }
1707     }
1709     if (_native_libs != [] && !_create_abi_split) {
1710       native_libs_dir = _native_libs_dir
1711       deps += [ ":${_template_name}__prepare_native" ]
1712     }
1713   }
1715   if (_native_libs != [] && _create_abi_split) {
1716     _manifest_rule = "${_template_name}__split_manifest_abi_${android_app_abi}"
1717     generate_split_manifest(_manifest_rule) {
1718       main_manifest = _android_manifest
1719       out_manifest =
1720           "$gen_dir/split-manifests/${android_app_abi}/AndroidManifest.xml"
1721       split_name = "abi_${android_app_abi}"
1722     }
1724     _apk_rule = "${_template_name}__split_apk_abi_${android_app_abi}"
1725     final_deps += [ ":$_apk_rule" ]
1726     create_apk(_apk_rule) {
1727       _split_paths = process_file_template(
1728               [ _final_apk_path ],
1729               "{{source_dir}}/{{source_name_part}}-abi-${android_app_abi}.apk")
1730       apk_path = _split_paths[0]
1731       base_path = "$gen_dir/$_apk_rule"
1733       manifest_outputs = get_target_outputs(":${_manifest_rule}")
1734       android_manifest = manifest_outputs[1]
1735       load_library_from_apk = _load_library_from_apk
1737       version_code = _version_code
1738       version_name = _version_name
1740       keystore_name = _keystore_name
1741       keystore_path = _keystore_path
1742       keystore_password = _keystore_password
1744       native_libs_dir = _native_libs_dir
1745       deps = [
1746         ":${_template_name}__prepare_native",
1747       ]
1748     }
1749   }
1751   group(target_name) {
1752     deps = final_deps
1753     if (defined(invoker.data_deps)) {
1754       data_deps = invoker.data_deps
1755     }
1756   }
1759 # Declare an Android instrumentation test apk
1761 # This target creates an Android instrumentation test apk.
1763 # Variables
1764 #   android_manifest: Path to AndroidManifest.xml.
1765 #   data_deps: List of dependencies needed at runtime. These will be built but
1766 #     won't change the generated .apk in any way (in fact they may be built
1767 #     after the .apk is).
1768 #   deps: List of dependencies. All Android java resources and libraries in the
1769 #     "transitive closure" of these dependencies will be included in the apk.
1770 #     Note: this "transitive closure" actually only includes such targets if
1771 #     they are depended on through android_library or android_resources targets
1772 #     (and so not through builtin targets like 'action', 'group', etc).
1773 #   java_files: List of .java files to include in the apk.
1774 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1775 #      will be added to java_files and be included in this apk.
1776 #   apk_name: Name for final apk.
1777 #   final_apk_path: Path to final built apk. Default is
1778 #     $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
1779 #   native_libs: List paths of native libraries to include in this apk. If these
1780 #     libraries depend on other shared_library targets, those dependencies will
1781 #     also be included in the apk.
1782 #   apk_under_test: The apk being tested.
1783 #   isolate_file: Isolate file containing the list of test data dependencies.
1785 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1786 #     this directory will be included in the library. This is only supported to
1787 #     ease the gyp->gn conversion and will be removed in the future.
1789 # Example
1790 #   instrumentation_test_apk("foo_test_apk") {
1791 #     android_manifest = "AndroidManifest.xml"
1792 #     apk_name = "FooTest"
1793 #     apk_under_test = "Foo"
1794 #     java_files = [
1795 #       "android/org/chromium/foo/FooTestCase.java",
1796 #       "android/org/chromium/foo/FooExampleTest.java",
1797 #     ]
1798 #     deps = [
1799 #       ":foo_test_support_java"
1800 #     ]
1801 #   }
1802 template("instrumentation_test_apk") {
1803   set_sources_assignment_filter([])
1804   testonly = true
1805   _template_name = target_name
1807   if (defined(invoker.apk_name)) {
1808     test_runner_data_dep = [ ":${_template_name}__test_runner_script" ]
1809     test_runner_script("${_template_name}__test_runner_script") {
1810       test_name = invoker.target_name
1811       test_type = "instrumentation"
1812       test_apk = invoker.apk_name
1813       if (defined(invoker.isolate_file)) {
1814         isolate_file = invoker.isolate_file
1815       }
1816     }
1817   }
1819   android_apk(target_name) {
1820     if (defined(invoker.android_manifest)) {
1821       android_manifest = invoker.android_manifest
1822     }
1823     data_deps = [
1824       "//testing/android/driver:driver_apk",
1825       "//tools/android/forwarder2",
1826       "//tools/android/md5sum",
1827     ]
1828     if (defined(test_runner_data_dep)) {
1829       data_deps += test_runner_data_dep
1830     }
1831     if (defined(invoker.data_deps)) {
1832       data_deps += invoker.data_deps
1833     }
1834     deps = [
1835       "//testing/android/broker:broker_java",
1836     ]
1837     if (defined(invoker.deps)) {
1838       deps += invoker.deps
1839     }
1840     if (defined(invoker.java_files)) {
1841       java_files = invoker.java_files
1842     }
1843     if (defined(invoker.srcjar_deps)) {
1844       srcjar_deps = invoker.srcjar_deps
1845     }
1846     if (defined(invoker.apk_name)) {
1847       apk_name = invoker.apk_name
1848     }
1849     if (defined(invoker.final_apk_path)) {
1850       final_apk_path = invoker.final_apk_path
1851     }
1852     if (defined(invoker.native_libs)) {
1853       native_libs = invoker.native_libs
1854     }
1855     if (defined(invoker.apk_under_test)) {
1856       apk_under_test = invoker.apk_under_test
1857     }
1858     if (defined(invoker.DEPRECATED_java_in_dir)) {
1859       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
1860     }
1861   }
1864 # Declare an Android gtest apk
1866 # This target creates an Android apk for running gtest-based unittests.
1868 # Variables
1869 #   deps: Specifies the dependencies of this target. These will be passed to
1870 #     the underlying android_apk invocation and should include the java and
1871 #     resource dependencies of the apk.
1872 #   unittests_dep: This should be the label of the gtest native target. This
1873 #     target must be defined previously in the same file.
1874 #   unittests_binary: The basename of the library produced by the unittests_dep
1875 #     target. If unspecified, it assumes the name of the unittests_dep target
1876 #     (which will be correct unless that target specifies an "output_name".
1877 #     TODO(brettw) make this automatic by allowing get_target_outputs to
1878 #     support executables.
1879 #   apk_name: The name of the produced apk. If unspecified, it uses the name
1880 #             of the unittests_dep target postfixed with "_apk"
1882 # Example
1883 #   unittest_apk("foo_unittests_apk") {
1884 #     deps = [ ":foo_java", ":foo_resources" ]
1885 #     unittests_dep = ":foo_unittests"
1886 #   }
1887 template("unittest_apk") {
1888   set_sources_assignment_filter([])
1889   testonly = true
1891   assert(defined(invoker.unittests_dep), "Need unittests_dep for $target_name")
1893   test_suite_name = get_label_info(invoker.unittests_dep, "name")
1895   # This trivial assert is needed in case both unittests_binary and apk_name
1896   # are defined, as otherwise test_suite_name would not be used.
1897   assert(test_suite_name != "")
1899   if (defined(invoker.unittests_binary)) {
1900     unittests_binary = invoker.unittests_binary
1901   } else {
1902     unittests_binary = "lib" + test_suite_name + ".so"
1903   }
1905   if (defined(invoker.apk_name)) {
1906     apk_name = invoker.apk_name
1907   } else {
1908     apk_name = test_suite_name
1909   }
1911   android_apk(target_name) {
1912     final_apk_path = "$root_build_dir/${apk_name}_apk/${apk_name}-debug.apk"
1913     java_files = [
1914       "//testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java",
1915       "//testing/android/native_test/java/src/org/chromium/native_test/NativeTestActivity.java",
1916       "//testing/android/native_test/java/src/org/chromium/native_test/NativeUnitTestActivity.java",
1917       "//testing/android/native_test/java/src/org/chromium/native_test/NativeTestInstrumentationTestRunner.java",
1918     ]
1919     android_manifest = "//testing/android/native_test/java/AndroidManifest.xml"
1920     native_libs = [ unittests_binary ]
1921     if (defined(invoker.asset_location)) {
1922       asset_location = invoker.asset_location
1923     }
1924     deps = [
1925       "//base:base_java",
1926       "//build/android/pylib/remote/device/dummy:remote_device_dummy_apk",
1927       "//testing/android/appurify_support:appurify_support_java",
1928       "//testing/android/reporter:reporter_java",
1929     ]
1930     if (defined(invoker.deps)) {
1931       deps += invoker.deps
1932     }
1933     data_deps = [ "//tools/android/md5sum" ]
1934     if (host_os == "linux") {
1935       data_deps += [ "//tools/android/forwarder2" ]
1936     }
1937     if (defined(invoker.data_deps)) {
1938       data_deps += invoker.data_deps
1939     }
1940   }
1943 # Generate .java files from .aidl files.
1945 # This target will store the .java files in a srcjar and should be included in
1946 # an android_library or android_apk's srcjar_deps.
1948 # Variables
1949 #   sources: Paths to .aidl files to compile.
1950 #   import_include: Path to directory containing .java files imported by the
1951 #     .aidl files.
1952 #   interface_file: Preprocessed aidl file to import.
1954 # Example
1955 #   android_aidl("foo_aidl") {
1956 #     import_include = "java/src"
1957 #     sources = [
1958 #       "java/src/com/foo/bar/FooBarService.aidl",
1959 #       "java/src/com/foo/bar/FooBarServiceCallback.aidl",
1960 #     ]
1961 #   }
1962 template("android_aidl") {
1963   set_sources_assignment_filter([])
1964   if (defined(invoker.testonly)) {
1965     testonly = invoker.testonly
1966   }
1968   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
1969   aidl_path = "${android_sdk_build_tools}/aidl"
1970   framework_aidl = "$android_sdk/framework.aidl"
1972   action(target_name) {
1973     script = "//build/android/gyp/aidl.py"
1974     sources = invoker.sources
1976     imports = [ framework_aidl ]
1977     if (defined(invoker.interface_file)) {
1978       assert(invoker.interface_file != "")
1979       imports += [ invoker.interface_file ]
1980     }
1982     inputs = [ aidl_path ] + imports
1984     depfile = "${target_gen_dir}/${target_name}.d"
1985     outputs = [
1986       depfile,
1987       srcjar_path,
1988     ]
1989     rebased_imports = rebase_path(imports, root_build_dir)
1990     args = [
1991       "--depfile",
1992       rebase_path(depfile, root_build_dir),
1993       "--aidl-path",
1994       rebase_path(aidl_path, root_build_dir),
1995       "--imports=$rebased_imports",
1996       "--srcjar",
1997       rebase_path(srcjar_path, root_build_dir),
1998     ]
1999     if (defined(invoker.import_include) && invoker.import_include != "") {
2000       # TODO(cjhopman): aidl supports creating a depfile. We should be able to
2001       # switch to constructing a depfile for the overall action from that
2002       # instead of having all the .java files in the include paths as inputs.
2003       rebased_import_includes =
2004           rebase_path([ invoker.import_include ], root_build_dir)
2005       args += [ "--includes=$rebased_import_includes" ]
2007       _java_files_build_rel =
2008           exec_script("//build/android/gyp/find.py",
2009                       rebase_path([ invoker.import_include ], root_build_dir),
2010                       "list lines")
2011       _java_files = rebase_path(_java_files_build_rel, ".", root_build_dir)
2012       inputs += _java_files
2013     }
2014     args += rebase_path(sources, root_build_dir)
2015   }
2018 # Creates a dist directory for a native executable.
2020 # Running a native executable on a device requires all the shared library
2021 # dependencies of that executable. To make it easier to install and run such an
2022 # executable, this will create a directory containing the native exe and all
2023 # it's library dependencies.
2025 # Note: It's usually better to package things as an APK than as a native
2026 # executable.
2028 # Variables
2029 #   dist_dir: Directory for the exe and libraries. Everything in this directory
2030 #     will be deleted before copying in the exe and libraries.
2031 #   binary: Path to (stripped) executable.
2033 # Example
2034 #   create_native_executable_dist("foo_dist") {
2035 #     dist_dir = "$root_build_dir/foo_dist"
2036 #     binary = "$root_build_dir/exe.stripped/foo"
2037 #     deps = [ ":the_thing_that_makes_foo" ]
2038 #   }
2039 template("create_native_executable_dist") {
2040   set_sources_assignment_filter([])
2041   if (defined(invoker.testonly)) {
2042     testonly = invoker.testonly
2043   }
2045   dist_dir = invoker.dist_dir
2046   binary = invoker.binary
2047   template_name = target_name
2049   libraries_list =
2050       "${target_gen_dir}/${template_name}_library_dependencies.list"
2052   # TODO(gyp)
2053   #'dependencies': [
2054   #'<(DEPTH)/build/android/setup.gyp:copy_system_libraries',
2055   #],
2057   find_deps_target_name = "${template_name}__find_library_dependencies"
2058   copy_target_name = "${template_name}__copy_libraries_and_exe"
2060   stripped_libraries_dir = "$root_build_dir/lib.stripped"
2061   action(find_deps_target_name) {
2062     visibility = [ ":$copy_target_name" ]
2064     script = "//build/android/gyp/write_ordered_libraries.py"
2065     depfile = "$target_gen_dir/$target_name.d"
2066     inputs = [
2067       binary,
2068       android_readelf,
2069     ]
2070     outputs = [
2071       depfile,
2072       libraries_list,
2073     ]
2074     rebased_binaries = rebase_path([ binary ], root_build_dir)
2075     args = [
2076       "--depfile",
2077       rebase_path(depfile, root_build_dir),
2078       "--input-libraries=$rebased_binaries",
2079       "--libraries-dir",
2080       rebase_path(stripped_libraries_dir, root_build_dir),
2081       "--output",
2082       rebase_path(libraries_list, root_build_dir),
2083       "--readelf",
2084       rebase_path(android_readelf, root_build_dir),
2085     ]
2086     if (defined(invoker.deps)) {
2087       deps = invoker.deps
2088     }
2089   }
2091   copy_ex(copy_target_name) {
2092     visibility = [ ":$template_name" ]
2094     clear_dir = true
2095     inputs = [
2096       binary,
2097       libraries_list,
2098     ]
2099     dest = dist_dir
2100     rebased_binaries_list = rebase_path([ binary ], root_build_dir)
2101     rebased_libraries_list = rebase_path(libraries_list, root_build_dir)
2102     args = [
2103       "--files=$rebased_binaries_list",
2104       "--files=@FileArg($rebased_libraries_list:libraries)",
2105     ]
2107     deps = [
2108       ":$find_deps_target_name",
2109     ]
2110     if (defined(invoker.deps)) {
2111       deps += invoker.deps
2112     }
2113   }
2115   group(template_name) {
2116     if (defined(invoker.visibility)) {
2117       visibility = invoker.visibility
2118     }
2119     deps = [
2120       ":$copy_target_name",
2121     ]
2122   }
2125 # Compile a protocol buffer to java.
2127 # This generates java files from protocol buffers and creates an Android library
2128 # containing the classes.
2130 # Variables
2131 #   sources: Paths to .proto files to compile.
2132 #   proto_path: Root directory of .proto files.
2134 # Example:
2135 #  proto_java_library("foo_proto_java") {
2136 #    proto_path = [ "src/foo" ]
2137 #    sources = [ "$proto_path/foo.proto" ]
2138 #  }
2139 template("proto_java_library") {
2140   set_sources_assignment_filter([])
2141   _protoc_dep = "//third_party/android_protobuf:android_protoc($host_toolchain)"
2142   _protoc_out_dir = get_label_info(_protoc_dep, "root_out_dir")
2143   _protoc_bin = "$_protoc_out_dir/android_protoc"
2144   _proto_path = invoker.proto_path
2146   _template_name = target_name
2148   action("${_template_name}__protoc_java") {
2149     srcjar_path = "$target_gen_dir/$target_name.srcjar"
2150     script = "//build/protoc_java.py"
2151     deps = [
2152       _protoc_dep,
2153     ]
2154     sources = invoker.sources
2155     depfile = "$target_gen_dir/$target_name.d"
2156     outputs = [
2157       depfile,
2158       srcjar_path,
2159     ]
2160     args = [
2161              "--depfile",
2162              rebase_path(depfile, root_build_dir),
2163              "--protoc",
2164              rebase_path(_protoc_bin, root_build_dir),
2165              "--proto-path",
2166              rebase_path(_proto_path, root_build_dir),
2167              "--srcjar",
2168              rebase_path(srcjar_path, root_build_dir),
2169            ] + rebase_path(sources, root_build_dir)
2170   }
2172   android_library(target_name) {
2173     java_files = []
2174     srcjar_deps = [ ":${_template_name}__protoc_java" ]
2175     deps = [
2176       "//third_party/android_protobuf:protobuf_nano_javalib",
2177     ]
2178   }
2181 # TODO(GYP): implement this.
2182 template("uiautomator_test") {
2183   set_sources_assignment_filter([])
2184   if (defined(invoker.testonly)) {
2185     testonly = invoker.testonly
2186   }
2187   assert(target_name != "")
2188   assert(invoker.deps != [] || true)
2189   group(target_name) {
2190   }