Build chromium_commands when building any _incremental apk
[chromium-blink-merge.git] / build / config / android / rules.gni
blob190cd20414a9b13d681cebcf768508ebdacae617
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("//build/toolchain/toolchain.gni")
9 import("//third_party/android_platform/config.gni")
10 import("//tools/grit/grit_rule.gni")
12 assert(is_android)
14 # Declare a jni target
16 # This target generates the native jni bindings for a set of .java files.
18 # See base/android/jni_generator/jni_generator.py for more info about the
19 # format of generating JNI bindings.
21 # Variables
22 #   sources: list of .java files to generate jni for
23 #   jni_package: subdirectory path for generated bindings
25 # Example
26 #   generate_jni("foo_jni") {
27 #     sources = [
28 #       "android/java/src/org/chromium/foo/Foo.java",
29 #       "android/java/src/org/chromium/foo/FooUtil.java",
30 #     ]
31 #     jni_package = "foo"
32 #   }
33 template("generate_jni") {
34   set_sources_assignment_filter([])
35   forward_variables_from(invoker, [ "testonly" ])
37   assert(defined(invoker.sources))
38   assert(defined(invoker.jni_package))
39   jni_package = invoker.jni_package
40   base_output_dir = "${target_gen_dir}/${target_name}"
41   package_output_dir = "${base_output_dir}/${jni_package}"
42   jni_output_dir = "${package_output_dir}/jni"
44   jni_generator_include = "//base/android/jni_generator/jni_generator_helper.h"
46   foreach_target_name = "${target_name}__jni_gen"
47   action_foreach(foreach_target_name) {
48     script = "//base/android/jni_generator/jni_generator.py"
49     depfile = "$target_gen_dir/$target_name.{{source_name_part}}.d"
50     sources = invoker.sources
51     outputs = [
52       depfile,
53       "${jni_output_dir}/{{source_name_part}}_jni.h",
54     ]
56     args = [
57       "--depfile",
58       rebase_path(depfile, root_build_dir),
59       "--input_file={{source}}",
60       "--optimize_generation=1",
61       "--ptr_type=long",
62       "--output_dir",
63       rebase_path(jni_output_dir, root_build_dir),
64       "--includes",
65       rebase_path(jni_generator_include, jni_output_dir),
66       "--native_exports_optional",
67     ]
68     if (defined(invoker.jni_generator_jarjar_file)) {
69       args += [
70         "--jarjar",
71         rebase_path(jni_generator_jarjar_file, root_build_dir),
72       ]
73     }
74   }
76   config("jni_includes_${target_name}") {
77     # TODO(cjhopman): #includes should probably all be relative to
78     # base_output_dir. Remove that from this config once the includes are
79     # updated.
80     include_dirs = [
81       base_output_dir,
82       package_output_dir,
83     ]
84   }
86   group(target_name) {
87     deps = []
88     forward_variables_from(invoker,
89                            [
90                              "deps",
91                              "public_deps",
92                              "visibility",
93                            ])
94     deps += [ ":$foreach_target_name" ]
95     public_configs = [ ":jni_includes_${target_name}" ]
96   }
99 # Declare a jni target for a prebuilt jar
101 # This target generates the native jni bindings for a set of classes in a .jar.
103 # See base/android/jni_generator/jni_generator.py for more info about the
104 # format of generating JNI bindings.
106 # Variables
107 #   classes: list of .class files in the jar to generate jni for. These should
108 #     include the full path to the .class file.
109 #   jni_package: subdirectory path for generated bindings
110 #   jar_file: the path to the .jar. If not provided, will default to the sdk's
111 #     android.jar
113 #   deps, public_deps: As normal
115 # Example
116 #   generate_jar_jni("foo_jni") {
117 #     classes = [
118 #       "android/view/Foo.class",
119 #     ]
120 #     jni_package = "foo"
121 #   }
122 template("generate_jar_jni") {
123   set_sources_assignment_filter([])
124   forward_variables_from(invoker, [ "testonly" ])
126   assert(defined(invoker.classes))
127   assert(defined(invoker.jni_package))
129   if (defined(invoker.jar_file)) {
130     jar_file = invoker.jar_file
131   } else {
132     jar_file = android_sdk_jar
133   }
135   jni_package = invoker.jni_package
136   base_output_dir = "${root_gen_dir}/${target_name}/${jni_package}"
137   jni_output_dir = "${base_output_dir}/jni"
139   jni_generator_include = "//base/android/jni_generator/jni_generator_helper.h"
141   # TODO(cjhopman): make jni_generator.py support generating jni for multiple
142   # .class files from a .jar.
143   jni_actions = []
144   foreach(class, invoker.classes) {
145     _classname_list = []
146     _classname_list = process_file_template([ class ], "{{source_name_part}}")
147     classname = _classname_list[0]
148     jni_target_name = "${target_name}__jni_${classname}"
149     jni_actions += [ ":$jni_target_name" ]
150     action(jni_target_name) {
151       # The sources aren't compiled so don't check their dependencies.
152       check_includes = false
153       depfile = "$target_gen_dir/$target_name.d"
154       script = "//base/android/jni_generator/jni_generator.py"
155       sources = [
156         jar_file,
157       ]
158       outputs = [
159         depfile,
160         "${jni_output_dir}/${classname}_jni.h",
161       ]
163       args = [
164         "--depfile",
165         rebase_path(depfile, root_build_dir),
166         "--jar_file",
167         rebase_path(jar_file, root_build_dir),
168         "--input_file",
169         class,
170         "--optimize_generation=1",
171         "--ptr_type=long",
172         "--output_dir",
173         rebase_path(jni_output_dir, root_build_dir),
174         "--includes",
175         rebase_path(jni_generator_include, jni_output_dir),
176         "--native_exports_optional",
177       ]
178     }
179   }
181   config("jni_includes_${target_name}") {
182     include_dirs = [ base_output_dir ]
183   }
185   group(target_name) {
186     deps = []
187     forward_variables_from(invoker,
188                            [
189                              "deps",
190                              "public_deps",
191                              "visibility",
192                            ])
193     deps += jni_actions
194     public_configs = [ ":jni_includes_${target_name}" ]
195   }
198 # Declare a target for c-preprocessor-generated java files
200 # NOTE: For generating Java conterparts to enums prefer using the java_cpp_enum
201 #       rule instead.
203 # This target generates java files using the host C pre-processor. Each file in
204 # sources will be compiled using the C pre-processor. If include_path is
205 # specified, it will be passed (with --I) to the pre-processor.
207 # This target will create a single .srcjar. Adding this target to an
208 # android_library target's srcjar_deps will make the generated java files be
209 # included in that library's final outputs.
211 # Variables
212 #   sources: list of files to be processed by the C pre-processor. For each
213 #     file in sources, there will be one .java file in the final .srcjar. For a
214 #     file named FooBar.template, a java file will be created with name
215 #     FooBar.java.
216 #   inputs: additional compile-time dependencies. Any files
217 #     `#include`-ed in the templates should be listed here.
218 #   package_name: this will be the subdirectory for each .java file in the
219 #     .srcjar.
221 # Example
222 #   java_cpp_template("foo_generated_enum") {
223 #     sources = [
224 #       "android/java/templates/Foo.template",
225 #     ]
226 #     inputs = [
227 #       "android/java/templates/native_foo_header.h",
228 #     ]
230 #     package_name = "org/chromium/base/library_loader"
231 #     include_path = "android/java/templates"
232 #   }
233 template("java_cpp_template") {
234   set_sources_assignment_filter([])
235   forward_variables_from(invoker, [ "testonly" ])
237   assert(defined(invoker.sources))
238   package_name = invoker.package_name + ""
240   if (defined(invoker.include_path)) {
241     include_path = invoker.include_path + ""
242   } else {
243     include_path = "//"
244   }
246   apply_gcc_target_name = "${target_name}__apply_gcc"
247   zip_srcjar_target_name = "${target_name}__zip_srcjar"
248   final_target_name = target_name
250   action_foreach(apply_gcc_target_name) {
251     forward_variables_from(invoker,
252                            [
253                              "deps",
254                              "public_deps",
255                              "data_deps",
256                            ])
257     visibility = [ ":$zip_srcjar_target_name" ]
258     script = "//build/android/gyp/gcc_preprocess.py"
259     if (defined(invoker.inputs)) {
260       inputs = invoker.inputs + []
261     }
262     depfile = "${target_gen_dir}/${target_name}_{{source_name_part}}.d"
264     sources = invoker.sources
266     gen_dir =
267         "${target_gen_dir}/${target_name}/java_cpp_template/${package_name}"
268     gcc_template_output_pattern = "${gen_dir}/{{source_name_part}}.java"
270     outputs = [
271       depfile,
272       gcc_template_output_pattern,
273     ]
275     args = [
276       "--depfile",
277       rebase_path(depfile, root_build_dir),
278       "--include-path",
279       rebase_path(include_path, root_build_dir),
280       "--output",
281       rebase_path(gen_dir, root_build_dir) + "/{{source_name_part}}.java",
282       "--template={{source}}",
283     ]
285     if (defined(invoker.defines)) {
286       foreach(def, invoker.defines) {
287         args += [
288           "--defines",
289           def,
290         ]
291       }
292     }
293   }
295   apply_gcc_outputs = get_target_outputs(":$apply_gcc_target_name")
296   base_gen_dir = get_label_info(":$apply_gcc_target_name", "target_gen_dir")
298   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
299   zip(zip_srcjar_target_name) {
300     visibility = [ ":$final_target_name" ]
301     inputs = apply_gcc_outputs
302     output = srcjar_path
303     base_dir = base_gen_dir
304     deps = [
305       ":$apply_gcc_target_name",
306     ]
307   }
309   group(final_target_name) {
310     forward_variables_from(invoker, [ "visibility" ])
311     deps = [
312       ":$zip_srcjar_target_name",
313     ]
314   }
317 # Declare a target for generating Java classes from C++ enums.
319 # This target generates Java files from C++ enums using a script.
321 # This target will create a single .srcjar. Adding this target to an
322 # android_library target's srcjar_deps will make the generated java files be
323 # included in that library's final outputs.
325 # Variables
326 #   sources: list of files to be processed by the script. For each annotated
327 #     enum contained in the sources files the script will generate a .java
328 #     file with the same name as the name of the enum.
330 #   outputs: list of outputs, relative to the output_dir. These paths are
331 #     verified at build time by the script. To get the list programatically run:
332 #       python build/android/gyp/java_cpp_enum.py \
333 #         --print_output_only . path/to/header/file.h
335 # Example
336 #   java_cpp_enum("foo_generated_enum") {
337 #     sources = [
338 #       "src/native_foo_header.h",
339 #     ]
340 #     outputs = [
341 #       "org/chromium/FooEnum.java",
342 #     ]
343 #   }
344 template("java_cpp_enum") {
345   set_sources_assignment_filter([])
346   forward_variables_from(invoker, [ "testonly" ])
348   assert(defined(invoker.sources))
349   assert(defined(invoker.outputs))
351   generate_enum_target_name = "${target_name}__generate_enum"
352   zip_srcjar_target_name = "${target_name}__zip_srcjar"
353   final_target_name = target_name
355   action(generate_enum_target_name) {
356     visibility = [ ":$zip_srcjar_target_name" ]
358     # The sources aren't compiled so don't check their dependencies.
359     check_includes = false
361     sources = invoker.sources
362     script = "//build/android/gyp/java_cpp_enum.py"
363     gen_dir = "${target_gen_dir}/${target_name}/enums"
364     outputs =
365         get_path_info(rebase_path(invoker.outputs, ".", gen_dir), "abspath")
367     args = []
368     foreach(output, rebase_path(outputs, root_build_dir)) {
369       args += [
370         "--assert_file",
371         output,
372       ]
373     }
374     args += [ rebase_path(gen_dir, root_build_dir) ]
375     args += rebase_path(invoker.sources, root_build_dir)
376   }
378   generate_enum_outputs = get_target_outputs(":$generate_enum_target_name")
379   base_gen_dir = get_label_info(":$generate_enum_target_name", "target_gen_dir")
381   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
382   zip(zip_srcjar_target_name) {
383     visibility = [ ":$final_target_name" ]
384     inputs = generate_enum_outputs
385     output = srcjar_path
386     base_dir = base_gen_dir
387     deps = [
388       ":$generate_enum_target_name",
389     ]
390   }
392   group(final_target_name) {
393     forward_variables_from(invoker, [ "visibility" ])
394     deps = [
395       ":$zip_srcjar_target_name",
396     ]
397   }
400 # Declare a target for processing a Jinja template.
402 # Variables
403 #   input: The template file to be processed.
404 #   output: Where to save the result.
405 #   variables: (Optional) A list of variables to make available to the template
406 #     processing environment, e.g. ["name=foo", "color=red"].
408 # Example
409 #   jinja_template("chrome_public_manifest") {
410 #     input = "java/AndroidManifest.xml"
411 #     output = "$target_gen_dir/AndroidManifest.xml"
412 #   }
413 template("jinja_template") {
414   set_sources_assignment_filter([])
415   forward_variables_from(invoker, [ "testonly" ])
417   assert(defined(invoker.input))
418   assert(defined(invoker.output))
420   action(target_name) {
421     forward_variables_from(invoker, [ "visibility" ])
423     sources = [
424       invoker.input,
425     ]
426     script = "//build/android/gyp/jinja_template.py"
427     depfile = "$target_gen_dir/$target_name.d"
429     outputs = [
430       depfile,
431       invoker.output,
432     ]
434     args = [
435       "--inputs",
436       rebase_path(invoker.input, root_build_dir),
437       "--output",
438       rebase_path(invoker.output, root_build_dir),
439       "--depfile",
440       rebase_path(depfile, root_build_dir),
441     ]
442     if (defined(invoker.variables)) {
443       variables = invoker.variables
444       args += [ "--variables=${variables}" ]
445     }
446   }
449 # Declare a target for processing Android resources as Jinja templates.
451 # This takes an Android resource directory where each resource is a Jinja
452 # template, processes each template, then packages the results in a zip file
453 # which can be consumed by an android resources, library, or apk target.
455 # If this target is included in the deps of an android resources/library/apk,
456 # the resources will be included with that target.
458 # Variables
459 #   resources: The list of resources files to process.
460 #   res_dir: The resource directory containing the resources.
461 #   variables: (Optional) A list of variables to make available to the template
462 #     processing environment, e.g. ["name=foo", "color=red"].
464 # Example
465 #   jinja_template_resources("chrome_public_template_resources") {
466 #     res_dir = "res_template"
467 #     resources = ["res_template/xml/syncable.xml"]
468 #     variables = ["color=red"]
469 #   }
470 template("jinja_template_resources") {
471   set_sources_assignment_filter([])
472   forward_variables_from(invoker, [ "testonly" ])
474   assert(defined(invoker.resources))
475   assert(defined(invoker.res_dir))
477   _base_path = "$target_gen_dir/$target_name"
478   _resources_zip = _base_path + ".resources.zip"
479   _build_config = _base_path + ".build_config"
481   write_build_config("${target_name}__build_config") {
482     build_config = _build_config
483     resources_zip = _resources_zip
484     type = "android_resources"
485   }
487   action("${target_name}__template") {
488     sources = invoker.resources
489     script = "//build/android/gyp/jinja_template.py"
490     depfile = "$target_gen_dir/$target_name.d"
492     outputs = [
493       depfile,
494       _resources_zip,
495     ]
497     rebased_resources = rebase_path(invoker.resources, root_build_dir)
498     args = [
499       "--inputs=${rebased_resources}",
500       "--inputs-base-dir",
501       rebase_path(invoker.res_dir, root_build_dir),
502       "--outputs-zip",
503       rebase_path(_resources_zip, root_build_dir),
504       "--depfile",
505       rebase_path(depfile, root_build_dir),
506     ]
507     if (defined(invoker.variables)) {
508       variables = invoker.variables
509       args += [ "--variables=${variables}" ]
510     }
511   }
513   group(target_name) {
514     deps = [
515       ":${target_name}__build_config",
516       ":${target_name}__template",
517     ]
518   }
521 # Creates a resources.zip with locale.pak files placed into appropriate
522 # resource configs (e.g. en-GB.pak -> res/raw-en/en_gb.pak). Also generates
523 # a locale_paks TypedArray so that resource files can be enumerated at runtime.
525 # If this target is included in the deps of an android resources/library/apk,
526 # the resources will be included with that target.
528 # Variables:
529 #   sources: List of .pak files. Names must be of the form "en.pak" or
530 #       "en-US.pak".
531 #   deps: (optional) List of dependencies that might be needed to generate
532 #       the .pak files.
534 # Example
535 #   locale_pak_resources("locale_paks") {
536 #     sources = [ "path/en-US.pak", "path/fr.pak", ... ]
537 #   }
538 template("locale_pak_resources") {
539   set_sources_assignment_filter([])
540   assert(defined(invoker.sources))
542   _base_path = "$target_gen_dir/$target_name"
543   _resources_zip = _base_path + ".resources.zip"
544   _build_config = _base_path + ".build_config"
546   write_build_config("${target_name}__build_config") {
547     build_config = _build_config
548     resources_zip = _resources_zip
549     type = "android_resources"
550   }
552   action("${target_name}__create_resources_zip") {
553     forward_variables_from(invoker,
554                            [
555                              "deps",
556                              "sources",
557                            ])
558     script = "//build/android/gyp/locale_pak_resources.py"
559     depfile = "$target_gen_dir/$target_name.d"
561     outputs = [
562       depfile,
563       _resources_zip,
564     ]
566     _rebased_sources = rebase_path(sources, root_build_dir)
567     args = [
568       "--locale-paks=${_rebased_sources}",
569       "--resources-zip",
570       rebase_path(_resources_zip, root_build_dir),
571       "--depfile",
572       rebase_path(depfile, root_build_dir),
573     ]
574   }
576   group(target_name) {
577     deps = [
578       ":${target_name}__build_config",
579       ":${target_name}__create_resources_zip",
580     ]
581   }
584 # Declare an Android resources target
586 # This creates a resources zip file that will be used when building an Android
587 # library or apk and included into a final apk.
589 # To include these resources in a library/apk, this target should be listed in
590 # the library's deps. A library/apk will also include any resources used by its
591 # own dependencies.
593 # Variables
594 #   deps: Specifies the dependencies of this target. Any Android resources
595 #     listed in deps will be included by libraries/apks that depend on this
596 #     target.
597 #   resource_dirs: List of directories containing resources for this target.
598 #   android_manifest: AndroidManifest.xml for this target. Defaults to
599 #     //build/android/AndroidManifest.xml.
600 #   custom_package: java package for generated .java files.
601 #   v14_skip: If true, don't run v14 resource generator on this. Defaults to
602 #     false. (see build/android/gyp/generate_v14_compatible_resources.py)
604 #   shared_resources: If true make a resource package that can be loaded by a
605 #     different application at runtime to access the package's resources.
608 # Example
609 #   android_resources("foo_resources") {
610 #     deps = [":foo_strings_grd"]
611 #     resource_dirs = ["res"]
612 #     custom_package = "org.chromium.foo"
613 #   }
614 template("android_resources") {
615   set_sources_assignment_filter([])
616   forward_variables_from(invoker, [ "testonly" ])
618   assert(defined(invoker.resource_dirs))
619   assert(defined(invoker.android_manifest) || defined(invoker.custom_package))
621   base_path = "$target_gen_dir/$target_name"
622   zip_path = base_path + ".resources.zip"
623   srcjar_path = base_path + ".srcjar"
624   r_text_path = base_path + "_R.txt"
625   build_config = base_path + ".build_config"
627   build_config_target_name = "${target_name}__build_config"
628   process_resources_target_name = "${target_name}__process_resources"
629   final_target_name = target_name
631   write_build_config(build_config_target_name) {
632     forward_variables_from(invoker,
633                            [
634                              "android_manifest",
635                              "custom_package",
636                              "deps",
637                            ])
638     visibility = [ ":$process_resources_target_name" ]
640     type = "android_resources"
641     resources_zip = zip_path
642     srcjar = srcjar_path
643     r_text = r_text_path
644   }
646   process_resources(process_resources_target_name) {
647     visibility = [ ":$final_target_name" ]
648     deps = []
649     forward_variables_from(invoker,
650                            [
651                              "android_manifest",
652                              "custom_package",
653                              "deps",
654                              "resource_dirs",
655                              "shared_resources",
656                              "v14_skip",
657                            ])
658     deps += [ ":$build_config_target_name" ]
659     if (!defined(android_manifest)) {
660       android_manifest = "//build/android/AndroidManifest.xml"
661     }
662   }
664   group(final_target_name) {
665     forward_variables_from(invoker, [ "visibility" ])
666     deps = [
667       ":${target_name}__process_resources",
668     ]
669   }
672 # Declare a target that generates localized strings.xml from a .grd file.
674 # If this target is included in the deps of an android resources/library/apk,
675 # the strings.xml will be included with that target.
677 # Variables
678 #   deps: Specifies the dependencies of this target.
679 #   grd_file: Path to the .grd file to generate strings.xml from.
680 #   outputs: Expected grit outputs (see grit rule).
682 # Example
683 #  java_strings_grd("foo_strings_grd") {
684 #    grd_file = "foo_strings.grd"
685 #  }
686 template("java_strings_grd") {
687   set_sources_assignment_filter([])
688   forward_variables_from(invoker, [ "testonly" ])
690   base_path = "$target_gen_dir/$target_name"
691   resources_zip = base_path + ".resources.zip"
692   build_config = base_path + ".build_config"
694   write_build_config("${target_name}__build_config") {
695     forward_variables_from(invoker, [ "deps" ])
696     type = "android_resources"
697   }
699   # Put grit files into this subdirectory of target_gen_dir.
700   extra_output_path = target_name + "_grit_output"
702   grit_target_name = "${target_name}__grit"
703   grit_output_dir = "$target_gen_dir/$extra_output_path"
704   grit(grit_target_name) {
705     grit_flags = [
706       "-E",
707       "ANDROID_JAVA_TAGGED_ONLY=false",
708     ]
709     output_dir = grit_output_dir
710     resource_ids = ""
711     source = invoker.grd_file
712     outputs = invoker.outputs
713   }
715   # This needs to get outputs from grit's internal target, not the final
716   # source_set.
717   generate_strings_outputs = get_target_outputs(":${grit_target_name}_grit")
719   zip("${target_name}__zip") {
720     base_dir = grit_output_dir
721     inputs = generate_strings_outputs
722     output = resources_zip
723     deps = [
724       ":$grit_target_name",
725     ]
726   }
728   group(target_name) {
729     deps = [
730       ":${target_name}__build_config",
731       ":${target_name}__zip",
732     ]
733   }
736 # Declare a target that packages strings.xml generated from a grd file.
738 # If this target is included in the deps of an android resources/library/apk,
739 # the strings.xml will be included with that target.
741 # Variables
742 #  grit_output_dir: directory containing grit-generated files.
743 #  generated_files: list of android resource files to package.
745 # Example
746 #  java_strings_grd_prebuilt("foo_strings_grd") {
747 #    grit_output_dir = "$root_gen_dir/foo/grit"
748 #    generated_files = [
749 #      "values/strings.xml"
750 #    ]
751 #  }
752 template("java_strings_grd_prebuilt") {
753   set_sources_assignment_filter([])
754   forward_variables_from(invoker, [ "testonly" ])
756   base_path = "$target_gen_dir/$target_name"
757   resources_zip = base_path + ".resources.zip"
758   build_config = base_path + ".build_config"
760   build_config_target_name = "${target_name}__build_config"
761   zip_target_name = "${target_name}__zip"
762   final_target_name = target_name
764   write_build_config(build_config_target_name) {
765     visibility = [ ":$zip_target_name" ]
766     type = "android_resources"
767   }
769   zip(zip_target_name) {
770     visibility = [ ":$final_target_name" ]
772     base_dir = invoker.grit_output_dir
773     inputs = rebase_path(invoker.generated_files, ".", base_dir)
774     output = resources_zip
775     deps = [
776       ":$build_config_target_name",
777     ]
778     if (defined(invoker.deps)) {
779       deps += invoker.deps
780     }
781   }
783   group(final_target_name) {
784     forward_variables_from(invoker, [ "visibility" ])
785     deps = [
786       ":$zip_target_name",
787     ]
788   }
791 # Declare a Java executable target
793 # This target creates an executable from java code and libraries. The executable
794 # will be in the output folder's /bin/ directory.
796 # Variables
797 #   deps: Specifies the dependencies of this target. Java targets in this list
798 #     will be included in the executable (and the javac classpath).
800 #   java_files: List of .java files included in this library.
801 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
802 #     will be added to java_files and be included in this library.
803 #   srcjars: List of srcjars to be included in this library, together with the
804 #     ones obtained from srcjar_deps.
806 #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
807 #     dependencies for this target. This will allow depending on an
808 #     android_library target, for example.
810 #   chromium_code: If true, extra analysis warning/errors will be enabled.
811 #   enable_errorprone: If true, enables the errorprone compiler.
813 #   data_deps, testonly
815 # Example
816 #   java_binary("foo") {
817 #     java_files = [ "org/chromium/foo/FooMain.java" ]
818 #     deps = [ ":bar_java" ]
819 #     main_class = "org.chromium.foo.FooMain"
820 #   }
821 template("java_binary") {
822   set_sources_assignment_filter([])
824   # TODO(cjhopman): This should not act like a java_library for dependents (i.e.
825   # dependents shouldn't get the jar in their classpath, etc.).
826   java_library_impl(target_name) {
827     forward_variables_from(invoker, "*")
828     supports_android = false
829     main_class = invoker.main_class
830   }
833 # Declare a Junit executable target
835 # This target creates an executable from java code for running as a junit test
836 # suite. The executable will be in the output folder's /bin/ directory.
838 # Variables
839 #   deps: Specifies the dependencies of this target. Java targets in this list
840 #     will be included in the executable (and the javac classpath).
842 #   java_files: List of .java files included in this library.
843 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
844 #     will be added to java_files and be included in this library.
845 #   srcjars: List of srcjars to be included in this library, together with the
846 #     ones obtained from srcjar_deps.
848 #   chromium_code: If true, extra analysis warning/errors will be enabled.
850 # Example
851 #   junit_binary("foo") {
852 #     java_files = [ "org/chromium/foo/FooTest.java" ]
853 #     deps = [ ":bar_java" ]
854 #   }
855 template("junit_binary") {
856   set_sources_assignment_filter([])
858   java_binary(target_name) {
859     deps = []
860     forward_variables_from(invoker, "*")
861     bypass_platform_checks = true
862     main_class = "org.chromium.testing.local.JunitTestMain"
863     testonly = true
865     deps += [
866       "//testing/android/junit:junit_test_support",
867       "//third_party/junit",
868       "//third_party/mockito:mockito_java",
869       "//third_party/robolectric:robolectric_java",
870       "//third_party/robolectric:android-all-4.3_r2-robolectric-0",
871     ]
872   }
875 # Declare a java library target
877 # Variables
878 #   deps: Specifies the dependencies of this target. Java targets in this list
879 #     will be added to the javac classpath.
881 #   java_files: List of .java files included in this library.
882 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
883 #     will be added to java_files and be included in this library.
884 #   srcjars: List of srcjars to be included in this library, together with the
885 #     ones obtained from srcjar_deps.
886 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
887 #     this directory will be included in the library. This is only supported to
888 #     ease the gyp->gn conversion and will be removed in the future.
890 #   chromium_code: If true, extra analysis warning/errors will be enabled.
891 #   enable_errorprone: If true, enables the errorprone compiler.
893 #   jar_excluded_patterns: List of patterns of .class files to exclude from the
894 #     final jar.
896 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
897 #     be used to remove unwanted parts of the library.
898 #   proguard_config: Path to the proguard config for preprocessing.
900 #   supports_android: If true, Android targets (android_library, android_apk)
901 #     may depend on this target. Note: if true, this target must only use the
902 #     subset of Java available on Android.
903 #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
904 #     dependencies for this target. This will allow depending on an
905 #     android_library target, for example.
907 #   data_deps, testonly
909 # Example
910 #   java_library("foo_java") {
911 #     java_files = [
912 #       "org/chromium/foo/Foo.java",
913 #       "org/chromium/foo/FooInterface.java",
914 #       "org/chromium/foo/FooService.java",
915 #     ]
916 #     deps = [
917 #       ":bar_java"
918 #     ]
919 #     srcjar_deps = [
920 #       ":foo_generated_enum"
921 #     ]
922 #     jar_excluded_patterns = [
923 #       "*/FooService.class", "*/FooService##*.class"
924 #     ]
925 #   }
926 template("java_library") {
927   set_sources_assignment_filter([])
928   java_library_impl(target_name) {
929     forward_variables_from(invoker, "*")
930   }
933 # Declare a java library target for a prebuilt jar
935 # Variables
936 #   deps: Specifies the dependencies of this target. Java targets in this list
937 #     will be added to the javac classpath.
938 #   jar_path: Path to the prebuilt jar.
939 #   jar_dep: Target that builds jar_path (optional).
940 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
941 #     be used to remove unwanted parts of the library.
942 #   proguard_config: Path to the proguard config for preprocessing.
943 #   supports_android: If true, Android targets (android_library, android_apk)
944 #     may depend on this target. Note: if true, this target must only use the
945 #     subset of Java available on Android.
947 # Example
948 #   java_prebuilt("foo_java") {
949 #     jar_path = "foo.jar"
950 #     deps = [
951 #       ":foo_resources",
952 #       ":bar_java"
953 #     ]
954 #   }
955 template("java_prebuilt") {
956   set_sources_assignment_filter([])
957   java_prebuilt_impl(target_name) {
958     forward_variables_from(invoker, "*")
959   }
962 # Declare an Android library target
964 # This target creates an Android library containing java code and Android
965 # resources.
967 # Variables
968 #   deps: Specifies the dependencies of this target. Java targets in this list
969 #     will be added to the javac classpath. Android resources in dependencies
970 #     will be used when building this library.
972 #   java_files: List of .java files included in this library.
973 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
974 #     will be added to java_files and be included in this library.
975 #   srcjars: List of srcjars to be included in this library, together with the
976 #     ones obtained from srcjar_deps.
977 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
978 #     this directory will be included in the library. This is only supported to
979 #     ease the gyp->gn conversion and will be removed in the future.
981 #   chromium_code: If true, extra analysis warning/errors will be enabled.
982 #   enable_errorprone: If true, enables the errorprone compiler.
984 #   jar_excluded_patterns: List of patterns of .class files to exclude from the
985 #     final jar.
987 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
988 #     be used to remove unwanted parts of the library.
989 #   proguard_config: Path to the proguard config for preprocessing.
991 #   dex_path: If set, the resulting .dex.jar file will be placed under this
992 #     path.
995 # Example
996 #   android_library("foo_java") {
997 #     java_files = [
998 #       "android/org/chromium/foo/Foo.java",
999 #       "android/org/chromium/foo/FooInterface.java",
1000 #       "android/org/chromium/foo/FooService.java",
1001 #     ]
1002 #     deps = [
1003 #       ":bar_java"
1004 #     ]
1005 #     srcjar_deps = [
1006 #       ":foo_generated_enum"
1007 #     ]
1008 #     jar_excluded_patterns = [
1009 #       "*/FooService.class", "*/FooService##*.class"
1010 #     ]
1011 #   }
1012 template("android_library") {
1013   set_sources_assignment_filter([])
1014   assert(!defined(invoker.jar_path),
1015          "android_library does not support a custom jar path")
1016   java_library_impl(target_name) {
1017     forward_variables_from(invoker, "*")
1019     supports_android = true
1020     requires_android = true
1022     if (!defined(jar_excluded_patterns)) {
1023       jar_excluded_patterns = []
1024     }
1025     jar_excluded_patterns += [
1026       "*/R.class",
1027       "*/R##*.class",
1028       "*/Manifest.class",
1029       "*/Manifest##*.class",
1030     ]
1031   }
1034 # Declare a target that packages a set of Java dependencies into a standalone
1035 # .dex.jar.
1037 # Variables
1038 #   deps: specifies the dependencies of this target. Android libraries in deps
1039 #     will be packaged into the resulting .dex.jar file.
1040 #   dex_path: location at which the output file will be put
1041 template("android_standalone_library") {
1042   set_sources_assignment_filter([])
1043   deps_dex(target_name) {
1044     forward_variables_from(invoker,
1045                            [
1046                              "deps",
1047                              "dex_path",
1048                              "excluded_jars",
1049                            ])
1050   }
1053 # Declare an Android library target for a prebuilt jar
1055 # This target creates an Android library containing java code and Android
1056 # resources.
1058 # Variables
1059 #   deps: Specifies the dependencies of this target. Java targets in this list
1060 #     will be added to the javac classpath. Android resources in dependencies
1061 #     will be used when building this library.
1062 #   jar_path: Path to the prebuilt jar.
1063 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
1064 #     be used to remove unwanted parts of the library.
1065 #   proguard_config: Path to the proguard config for preprocessing.
1067 # Example
1068 #   android_java_prebuilt("foo_java") {
1069 #     jar_path = "foo.jar"
1070 #     deps = [
1071 #       ":foo_resources",
1072 #       ":bar_java"
1073 #     ]
1074 #   }
1075 template("android_java_prebuilt") {
1076   set_sources_assignment_filter([])
1077   java_prebuilt_impl(target_name) {
1078     forward_variables_from(invoker, "*")
1079     supports_android = true
1080     requires_android = true
1081   }
1084 # Declare an Android apk target
1086 # This target creates an Android APK containing java code, resources, assets,
1087 # and (possibly) native libraries.
1089 # Variables
1090 #   android_manifest: Path to AndroidManifest.xml.
1091 #   android_manifest_dep: Target that generates AndroidManifest (if applicable)
1092 #   data_deps: List of dependencies needed at runtime. These will be built but
1093 #     won't change the generated .apk in any way (in fact they may be built
1094 #     after the .apk is).
1095 #   deps: List of dependencies. All Android java resources and libraries in the
1096 #     "transitive closure" of these dependencies will be included in the apk.
1097 #     Note: this "transitive closure" actually only includes such targets if
1098 #     they are depended on through android_library or android_resources targets
1099 #     (and so not through builtin targets like 'action', 'group', etc).
1100 #   java_files: List of .java files to include in the apk.
1101 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1102 #      will be added to java_files and be included in this apk.
1103 #   apk_name: Name for final apk.
1104 #   final_apk_path: Path to final built apk. Default is
1105 #     $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
1106 #   native_libs: List paths of native libraries to include in this apk. If these
1107 #     libraries depend on other shared_library targets, those dependencies will
1108 #     also be included in the apk.
1109 #   apk_under_test: For an instrumentation test apk, this is the target of the
1110 #     tested apk.
1111 #   include_all_resources - If true include all resource IDs in all generated
1112 #     R.java files.
1113 #   testonly: Marks this target as "test-only".
1115 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1116 #     this directory will be included in the library. This is only supported to
1117 #     ease the gyp->gn conversion and will be removed in the future.
1119 # Example
1120 #   android_apk("foo_apk") {
1121 #     android_manifest = "AndroidManifest.xml"
1122 #     java_files = [
1123 #       "android/org/chromium/foo/FooApplication.java",
1124 #       "android/org/chromium/foo/FooActivity.java",
1125 #     ]
1126 #     deps = [
1127 #       ":foo_support_java"
1128 #       ":foo_resources"
1129 #     ]
1130 #     srcjar_deps = [
1131 #       ":foo_generated_enum"
1132 #     ]
1133 #     native_libs = [
1134 #       native_lib_path
1135 #     ]
1136 #   }
1137 template("android_apk") {
1138   set_sources_assignment_filter([])
1139   forward_variables_from(invoker, [ "testonly" ])
1141   assert(defined(invoker.final_apk_path) || defined(invoker.apk_name))
1142   assert(defined(invoker.android_manifest))
1143   gen_dir = "$target_gen_dir/$target_name"
1144   base_path = "$gen_dir/$target_name"
1145   _build_config = "$target_gen_dir/$target_name.build_config"
1146   resources_zip_path = "$base_path.resources.zip"
1147   _all_resources_zip_path = "$base_path.resources.all.zip"
1148   jar_path = "$base_path.jar"
1149   _template_name = target_name
1151   final_dex_path = "$gen_dir/classes.dex"
1152   final_dex_target_name = "${_template_name}__final_dex"
1154   _final_apk_path = ""
1155   if (defined(invoker.final_apk_path)) {
1156     _final_apk_path = invoker.final_apk_path
1157   } else if (defined(invoker.apk_name)) {
1158     _final_apk_path = "$root_build_dir/apks/" + invoker.apk_name + ".apk"
1159   }
1160   _dist_jar_path_list =
1161       process_file_template(
1162           [ _final_apk_path ],
1163           "$root_build_dir/test.lib.java/{{source_name_part}}.jar")
1164   _dist_jar_path = _dist_jar_path_list[0]
1165   _final_apk_path_no_ext_list =
1166       process_file_template([ _final_apk_path ],
1167                             "{{source_dir}}/{{source_name_part}}")
1168   _final_apk_path_no_ext = _final_apk_path_no_ext_list[0]
1169   assert(_final_apk_path_no_ext != "")  # Mark as used.
1171   _native_libs = []
1173   _version_code = "1"
1174   if (defined(invoker.version_code)) {
1175     _version_code = invoker.version_code
1176   }
1178   _version_name = "Developer Build"
1179   if (defined(invoker.version_name)) {
1180     _version_name = invoker.version_name
1181   }
1182   _keystore_path = android_default_keystore_path
1183   _keystore_name = android_default_keystore_name
1184   _keystore_password = android_default_keystore_password
1186   if (defined(invoker.keystore_path)) {
1187     _keystore_path = invoker.keystore_path
1188     _keystore_name = invoker.keystore_name
1189     _keystore_password = invoker.keystore_password
1190   }
1192   _srcjar_deps = []
1193   if (defined(invoker.srcjar_deps)) {
1194     _srcjar_deps += invoker.srcjar_deps
1195   }
1197   _load_library_from_apk = false
1199   # The dependency that makes the chromium linker, if any is needed.
1200   _chromium_linker_dep = []
1202   if (defined(invoker.native_libs)) {
1203     _use_chromium_linker = false
1204     if (defined(invoker.use_chromium_linker)) {
1205       _use_chromium_linker =
1206           invoker.use_chromium_linker && chromium_linker_supported
1207       _chromium_linker_dep = [ "//base/android/linker:chromium_android_linker" ]
1208     }
1210     if (defined(invoker.load_library_from_apk) &&
1211         invoker.load_library_from_apk) {
1212       _load_library_from_apk = true
1213       assert(_use_chromium_linker,
1214              "Loading library from the apk requires use" +
1215                  " of the Chromium linker.")
1216     }
1218     _enable_relocation_packing = false
1219     if (defined(invoker.enable_relocation_packing) &&
1220         invoker.enable_relocation_packing) {
1221       _enable_relocation_packing = relocation_packing_supported
1222       assert(_use_chromium_linker,
1223              "Relocation packing requires use of the" + " Chromium linker.")
1224     }
1226     if (is_component_build) {
1227       _native_libs += [ "$root_shlib_dir/libc++_shared.so" ]
1228       _chromium_linker_dep += [ "//build/android:cpplib_stripped" ]
1229     }
1231     # Allow native_libs to be in the form "foo.so" or "foo.cr.so"
1232     _first_ext_removed =
1233         process_file_template(invoker.native_libs, "{{source_name_part}}")
1234     _native_libs += process_file_template(
1235             _first_ext_removed,
1236             "$root_shlib_dir/{{source_name_part}}$shlib_extension")
1238     # Add in target_cpu so that other architectures are not accidentally
1239     # included when switching target_cpu without doing a clean build.
1240     _native_libs_dir = gen_dir + "/lib-$target_cpu"
1242     if (_use_chromium_linker) {
1243       _native_libs +=
1244           [ "$root_shlib_dir/libchromium_android_linker$shlib_extension" ]
1245     }
1247     _enable_relocation_packing = false
1248     if (_use_chromium_linker && defined(invoker.enable_relocation_packing) &&
1249         invoker.enable_relocation_packing) {
1250       _enable_relocation_packing = true
1251     }
1253     _native_lib_version_rule = ""
1254     if (defined(invoker.native_lib_version_rule)) {
1255       _native_lib_version_rule = invoker.native_lib_version_rule
1256     }
1257     _native_lib_version_arg = "\"\""
1258     if (defined(invoker.native_lib_version_arg)) {
1259       _native_lib_version_arg = invoker.native_lib_version_arg
1260     }
1261   }
1263   _android_manifest_deps = []
1264   if (defined(invoker.android_manifest_dep)) {
1265     _android_manifest_deps = [ invoker.android_manifest_dep ]
1266   }
1267   _android_manifest = invoker.android_manifest
1269   _rebased_build_config = rebase_path(_build_config, root_build_dir)
1270   _create_abi_split =
1271       defined(invoker.create_abi_split) && invoker.create_abi_split
1272   _create_density_splits =
1273       defined(invoker.create_density_splits) && invoker.create_density_splits
1274   _create_language_splits =
1275       defined(invoker.language_splits) && invoker.language_splits != []
1277   # Help GN understand that _create_abi_split is not unused (bug in GN).
1278   assert(_create_abi_split || true)
1280   build_config_target = "${_template_name}__build_config"
1281   write_build_config(build_config_target) {
1282     forward_variables_from(invoker, [ "apk_under_test" ])
1283     type = "android_apk"
1284     dex_path = final_dex_path
1285     resources_zip = resources_zip_path
1286     build_config = _build_config
1287     android_manifest = _android_manifest
1289     deps = _chromium_linker_dep + _android_manifest_deps
1290     if (defined(invoker.deps)) {
1291       deps += invoker.deps
1292     }
1294     native_libs = _native_libs
1295   }
1297   _final_deps = []
1298   _incremental_final_deps = []
1300   process_resources_target = "${_template_name}__process_resources"
1301   process_resources(process_resources_target) {
1302     forward_variables_from(invoker, [ "include_all_resources" ])
1303     srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
1304     r_text_path = "${target_gen_dir}/${target_name}_R.txt"
1305     android_manifest = _android_manifest
1306     resource_dirs = [ "//build/android/ant/empty/res" ]
1307     zip_path = resources_zip_path
1308     all_resources_zip_path = _all_resources_zip_path
1309     generate_constant_ids = true
1311     build_config = _build_config
1312     deps = _android_manifest_deps + [ ":$build_config_target" ]
1313     if (defined(invoker.deps)) {
1314       deps += invoker.deps
1315     }
1316   }
1317   _srcjar_deps += [ ":$process_resources_target" ]
1319   if (_native_libs != []) {
1320     _enable_chromium_linker_tests = false
1321     if (defined(invoker.enable_chromium_linker_tests)) {
1322       _enable_chromium_linker_tests = invoker.enable_chromium_linker_tests
1323     }
1325     java_cpp_template("${_template_name}__native_libraries_java") {
1326       package_name = "org/chromium/base/library_loader"
1327       sources = [
1328         "//base/android/java/templates/NativeLibraries.template",
1329       ]
1330       inputs = [
1331         _build_config,
1332       ]
1333       deps = [
1334         ":$build_config_target",
1335       ]
1336       if (_native_lib_version_rule != "") {
1337         deps += [ _native_lib_version_rule ]
1338       }
1340       defines = [
1341         "NATIVE_LIBRARIES_LIST=" +
1342             "@FileArg($_rebased_build_config:native:java_libraries_list)",
1343         "NATIVE_LIBRARIES_VERSION_NUMBER=$_native_lib_version_arg",
1344       ]
1345       if (_use_chromium_linker) {
1346         defines += [ "ENABLE_CHROMIUM_LINKER" ]
1347       }
1348       if (_load_library_from_apk) {
1349         defines += [ "ENABLE_CHROMIUM_LINKER_LIBRARY_IN_ZIP_FILE" ]
1350       }
1351       if (_enable_chromium_linker_tests) {
1352         defines += [ "ENABLE_CHROMIUM_LINKER_TESTS" ]
1353       }
1354     }
1355     _srcjar_deps += [ ":${_template_name}__native_libraries_java" ]
1356   }
1358   java_target = "${_template_name}__java"
1359   java_library_impl(java_target) {
1360     forward_variables_from(invoker, [ "run_findbugs" ])
1361     supports_android = true
1362     requires_android = true
1363     override_build_config = _build_config
1364     deps = _android_manifest_deps + [ ":$build_config_target" ]
1366     android_manifest = _android_manifest
1367     chromium_code = true
1368     if (defined(invoker.java_files)) {
1369       java_files = invoker.java_files
1370     } else if (defined(invoker.DEPRECATED_java_in_dir)) {
1371       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
1372     } else {
1373       java_files = []
1374     }
1375     srcjar_deps = _srcjar_deps
1376     dex_path = base_path + ".dex.jar"
1378     if (defined(invoker.deps)) {
1379       deps += invoker.deps
1380     }
1381   }
1383   if (_dist_jar_path != "") {
1384     create_dist_target = "${_template_name}__create_dist_jar"
1385     _final_deps += [ ":$create_dist_target" ]
1386     _incremental_final_deps += [ ":$create_dist_target" ]
1388     # TODO(cjhopman): This is only ever needed to calculate the list of tests to
1389     # run. See build/android/pylib/instrumentation/test_jar.py. We should be
1390     # able to just do that calculation at build time instead.
1391     action(create_dist_target) {
1392       script = "//build/android/gyp/create_dist_jar.py"
1393       depfile = "$target_gen_dir/$target_name.d"
1394       inputs = [
1395         _build_config,
1396       ]
1397       outputs = [
1398         depfile,
1399         _dist_jar_path,
1400       ]
1401       args = [
1402         "--depfile",
1403         rebase_path(depfile, root_build_dir),
1404         "--output",
1405         rebase_path(_dist_jar_path, root_build_dir),
1406         "--inputs=@FileArg($_rebased_build_config:dist_jar:dependency_jars)",
1407       ]
1408       inputs += [ jar_path ]
1409       _rebased_jar_path = rebase_path([ jar_path ], root_build_dir)
1410       args += [ "--inputs=$_rebased_jar_path" ]
1411       deps = [
1412         ":$build_config_target",  # Generates the build config file.
1413         ":$java_target",  # Generates the jar file.
1414       ]
1415     }
1416   }
1418   dex("$final_dex_target_name") {
1419     deps = [
1420       ":$build_config_target",
1421       ":$java_target",
1422     ]
1423     sources = [
1424       jar_path,
1425     ]
1426     inputs = [
1427       _build_config,
1428     ]
1429     output = final_dex_path
1430     dex_arg_key = "${_rebased_build_config}:final_dex:dependency_dex_files"
1431     args = [ "--inputs=@FileArg($dex_arg_key)" ]
1432   }
1434   if (_native_libs != []) {
1435     action("${_template_name}__prepare_native") {
1436       forward_variables_from(invoker,
1437                              [
1438                                "data_deps",
1439                                "public_deps",
1440                              ])
1441       script = "//build/android/gyp/pack_relocations.py"
1442       packed_libraries_dir = "$_native_libs_dir/$android_app_abi"
1443       depfile = "$target_gen_dir/$target_name.d"
1444       outputs = [
1445         depfile,
1446       ]
1448       inputs = _native_libs
1449       deps = _chromium_linker_dep
1451       inputs += [ _build_config ]
1452       deps += [ ":$build_config_target" ]
1454       skip_packing_list = [
1455         "gdbserver",
1456         "libchromium_android_linker$shlib_extension",
1457       ]
1459       enable_packing_arg = 0
1460       if (_enable_relocation_packing) {
1461         enable_packing_arg = 1
1462         deps += [ relocation_packer_target ]
1463       }
1465       args = [
1466         "--depfile",
1467         rebase_path(depfile, root_build_dir),
1468         "--enable-packing=$enable_packing_arg",
1469         "--exclude-packing-list=$skip_packing_list",
1470         "--android-pack-relocations",
1471         rebase_path(relocation_packer_exe, root_build_dir),
1472         "--stripped-libraries-dir",
1473         rebase_path(root_build_dir, root_build_dir),
1474         "--packed-libraries-dir",
1475         rebase_path(packed_libraries_dir, root_build_dir),
1476         "--libraries=@FileArg(${_rebased_build_config}:native:libraries)",
1477         "--clear-dir",
1478       ]
1480       if (defined(invoker.deps)) {
1481         deps += invoker.deps
1482       }
1484       if (is_debug) {
1485         rebased_gdbserver = rebase_path([ android_gdbserver ], root_build_dir)
1486         inputs += [ android_gdbserver ]
1487         args += [ "--libraries=$rebased_gdbserver" ]
1488       }
1489     }
1490   }
1492   _final_deps += [ ":${_template_name}__create" ]
1493   _incremental_final_deps += [ ":${_template_name}__create_incremental" ]
1494   create_apk("${_template_name}__create") {
1495     forward_variables_from(invoker, [ "language_splits" ])
1496     apk_path = _final_apk_path
1497     android_manifest = _android_manifest
1498     resources_zip = _all_resources_zip_path
1499     dex_path = final_dex_path
1500     load_library_from_apk = _load_library_from_apk
1501     create_density_splits = _create_density_splits
1502     if (defined(invoker.extensions_to_not_compress)) {
1503       extensions_to_not_compress = invoker.extensions_to_not_compress
1504     } else {
1505       # Allow icu data, v8 snapshots, and pak files to be loaded directly from
1506       # the .apk.
1507       # Note: These are actually suffix matches, not necessarily extensions.
1508       extensions_to_not_compress = ".dat,.bin,.pak"
1509     }
1511     version_code = _version_code
1512     version_name = _version_name
1514     keystore_name = _keystore_name
1515     keystore_path = _keystore_path
1516     keystore_password = _keystore_password
1518     # This target generates the input file _all_resources_zip_path.
1519     deps = _android_manifest_deps + [
1520              ":$process_resources_target",
1521              ":$final_dex_target_name",
1522            ]
1523     if (defined(invoker.deps)) {
1524       deps += invoker.deps
1525     }
1527     if (defined(invoker.asset_location)) {
1528       asset_location = invoker.asset_location
1530       # We don't know the exact dependencies that create the assets in
1531       # |asset_location|; we depend on all caller deps until a better solution
1532       # is figured out (http://crbug.com/433330).
1533       if (defined(invoker.deps)) {
1534         deps += invoker.deps
1535       }
1536     }
1538     if (_native_libs != [] && !_create_abi_split) {
1539       native_libs_dir = _native_libs_dir
1540       deps += [ ":${_template_name}__prepare_native" ]
1541     }
1542   }
1544   if (_native_libs != [] && _create_abi_split) {
1545     _manifest_rule = "${_template_name}__split_manifest_abi_${android_app_abi}"
1546     generate_split_manifest(_manifest_rule) {
1547       main_manifest = _android_manifest
1548       out_manifest =
1549           "$gen_dir/split-manifests/${android_app_abi}/AndroidManifest.xml"
1550       split_name = "abi_${android_app_abi}"
1551       deps = _android_manifest_deps
1552     }
1554     _apk_rule = "${_template_name}__split_apk_abi_${android_app_abi}"
1555     _final_deps += [ ":$_apk_rule" ]
1557     # Don't add this to _incremental_final_deps since for incremental installs
1558     # we skip abi splits altogether.
1559     create_apk(_apk_rule) {
1560       apk_path = "${_final_apk_path_no_ext}-abi-${android_app_abi}.apk"
1561       base_path = "$gen_dir/$_apk_rule"
1563       manifest_outputs = get_target_outputs(":${_manifest_rule}")
1564       android_manifest = manifest_outputs[1]
1565       load_library_from_apk = _load_library_from_apk
1567       version_code = _version_code
1568       version_name = _version_name
1570       keystore_name = _keystore_name
1571       keystore_path = _keystore_path
1572       keystore_password = _keystore_password
1574       native_libs_dir = _native_libs_dir
1575       deps = [
1576         ":${_template_name}__prepare_native",
1577         ":${_manifest_rule}",
1578       ]
1579     }
1580   }
1582   _create_incremental_script_rule_name = "${_template_name}__incremental_script"
1583   _incremental_final_deps += [ ":${_create_incremental_script_rule_name}" ]
1584   action(_create_incremental_script_rule_name) {
1585     script = "//build/android/gn/create_incremental_install_script.py"
1586     depfile = "$target_gen_dir/$target_name.d"
1588     _generated_script_path =
1589         "${root_out_dir}/bin/install_incremental_${_template_name}"
1590     outputs = [
1591       depfile,
1592       _generated_script_path,
1593     ]
1595     _rebased_apk_path_no_ext =
1596         rebase_path(_final_apk_path_no_ext, root_build_dir)
1597     _rebased_generated_script_path =
1598         rebase_path(_generated_script_path, root_build_dir)
1599     _rebased_depfile = rebase_path(depfile, root_build_dir)
1600     args = [
1601       "--apk-path=${_rebased_apk_path_no_ext}_incremental.apk",
1602       "--script-output-path=$_rebased_generated_script_path",
1603       "--depfile=$_rebased_depfile",
1604     ]
1605     if (defined(_native_libs_dir)) {
1606       _rebased_native_libs_dir = rebase_path(_native_libs_dir, root_build_dir)
1607       args += [ "--lib-dir=$_rebased_native_libs_dir/$android_app_abi" ]
1608     }
1609     if (_create_density_splits) {
1610       args += [ "--split=${_rebased_apk_path_no_ext}-density-*.apk" ]
1611     }
1612     if (_create_language_splits) {
1613       args += [ "--split=${_rebased_apk_path_no_ext}-language-*.apk" ]
1614     }
1615   }
1617   group(target_name) {
1618     forward_variables_from(invoker, [ "data_deps" ])
1619     deps = _final_deps
1620   }
1621   group("${target_name}_incremental") {
1622     data_deps = []
1623     forward_variables_from(invoker, [ "data_deps" ])
1624     data_deps += [ "//build/android/pylib/device/commands" ]
1625     deps = _incremental_final_deps
1626   }
1629 # Declare an Android instrumentation test apk
1631 # This target creates an Android instrumentation test apk.
1633 # Variables
1634 #   android_manifest: Path to AndroidManifest.xml.
1635 #   data_deps: List of dependencies needed at runtime. These will be built but
1636 #     won't change the generated .apk in any way (in fact they may be built
1637 #     after the .apk is).
1638 #   deps: List of dependencies. All Android java resources and libraries in the
1639 #     "transitive closure" of these dependencies will be included in the apk.
1640 #     Note: this "transitive closure" actually only includes such targets if
1641 #     they are depended on through android_library or android_resources targets
1642 #     (and so not through builtin targets like 'action', 'group', etc).
1643 #   java_files: List of .java files to include in the apk.
1644 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1645 #      will be added to java_files and be included in this apk.
1646 #   apk_name: Name for final apk.
1647 #   final_apk_path: Path to final built apk. Default is
1648 #     $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
1649 #   native_libs: List paths of native libraries to include in this apk. If these
1650 #     libraries depend on other shared_library targets, those dependencies will
1651 #     also be included in the apk.
1652 #   apk_under_test: The apk being tested.
1653 #   isolate_file: Isolate file containing the list of test data dependencies.
1655 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1656 #     this directory will be included in the library. This is only supported to
1657 #     ease the gyp->gn conversion and will be removed in the future.
1659 # Example
1660 #   instrumentation_test_apk("foo_test_apk") {
1661 #     android_manifest = "AndroidManifest.xml"
1662 #     apk_name = "FooTest"
1663 #     apk_under_test = "Foo"
1664 #     java_files = [
1665 #       "android/org/chromium/foo/FooTestCase.java",
1666 #       "android/org/chromium/foo/FooExampleTest.java",
1667 #     ]
1668 #     deps = [
1669 #       ":foo_test_support_java"
1670 #     ]
1671 #   }
1672 template("instrumentation_test_apk") {
1673   set_sources_assignment_filter([])
1674   testonly = true
1675   _template_name = target_name
1677   if (defined(invoker.apk_name)) {
1678     test_runner_data_dep = [ ":${_template_name}__test_runner_script" ]
1679     test_runner_script("${_template_name}__test_runner_script") {
1680       forward_variables_from(invoker, [ "isolate_file" ])
1681       test_name = invoker.target_name
1682       test_type = "instrumentation"
1683       test_apk = invoker.apk_name
1684     }
1685   }
1687   android_apk(target_name) {
1688     deps = []
1689     data_deps = []
1690     forward_variables_from(invoker, "*")
1691     data_deps += [
1692       "//testing/android/driver:driver_apk",
1693       "//tools/android/forwarder2",
1694       "//tools/android/md5sum",
1695     ]
1696     if (defined(test_runner_data_dep)) {
1697       data_deps += test_runner_data_dep
1698     }
1699     deps += [ "//testing/android/broker:broker_java" ]
1700     run_findbugs = false
1701   }
1704 # Declare an Android gtest apk
1706 # This target creates an Android apk for running gtest-based unittests.
1708 # Variables
1709 #   deps: Specifies the dependencies of this target. These will be passed to
1710 #     the underlying android_apk invocation and should include the java and
1711 #     resource dependencies of the apk.
1712 #   unittests_dep: This should be the label of the gtest native target. This
1713 #     target must be defined previously in the same file.
1714 #   unittests_binary: The basename of the library produced by the unittests_dep
1715 #     target. If unspecified, it assumes the name of the unittests_dep target
1716 #     (which will be correct unless that target specifies an "output_name".
1717 #     TODO(brettw) make this automatic by allowing get_target_outputs to
1718 #     support executables.
1719 #   apk_name: The name of the produced apk. If unspecified, it uses the name
1720 #             of the unittests_dep target postfixed with "_apk"
1722 # Example
1723 #   unittest_apk("foo_unittests_apk") {
1724 #     deps = [ ":foo_java", ":foo_resources" ]
1725 #     unittests_dep = ":foo_unittests"
1726 #   }
1727 template("unittest_apk") {
1728   set_sources_assignment_filter([])
1729   testonly = true
1731   assert(defined(invoker.unittests_dep), "Need unittests_dep for $target_name")
1733   test_suite_name = get_label_info(invoker.unittests_dep, "name")
1735   # This trivial assert is needed in case both unittests_binary and apk_name
1736   # are defined, as otherwise test_suite_name would not be used.
1737   assert(test_suite_name != "")
1739   if (defined(invoker.unittests_binary)) {
1740     unittests_binary = invoker.unittests_binary
1741   } else {
1742     unittests_binary = "lib${test_suite_name}${shlib_extension}"
1743   }
1745   if (defined(invoker.apk_name)) {
1746     apk_name = invoker.apk_name
1747   } else {
1748     apk_name = test_suite_name
1749   }
1751   android_apk(target_name) {
1752     forward_variables_from(invoker, [ "asset_location" ])
1753     final_apk_path = "$root_build_dir/${apk_name}_apk/${apk_name}-debug.apk"
1754     java_files = [
1755       "//testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java",
1756       "//testing/android/native_test/java/src/org/chromium/native_test/NativeTestActivity.java",
1757       "//testing/android/native_test/java/src/org/chromium/native_test/NativeUnitTestActivity.java",
1758       "//testing/android/native_test/java/src/org/chromium/native_test/NativeTestInstrumentationTestRunner.java",
1759     ]
1760     android_manifest = "//testing/android/native_test/java/AndroidManifest.xml"
1761     native_libs = [ unittests_binary ]
1762     deps = [
1763       "//base:base_java",
1764       "//build/android/pylib/remote/device/dummy:remote_device_dummy_apk",
1765       "//testing/android/appurify_support:appurify_support_java",
1766       "//testing/android/reporter:reporter_java",
1767     ]
1768     if (defined(invoker.deps)) {
1769       deps += invoker.deps
1770     }
1771     data_deps = [ "//tools/android/md5sum" ]
1772     if (host_os == "linux") {
1773       data_deps += [ "//tools/android/forwarder2" ]
1774     }
1775     if (defined(invoker.data_deps)) {
1776       data_deps += invoker.data_deps
1777     }
1778   }
1781 # Generate .java files from .aidl files.
1783 # This target will store the .java files in a srcjar and should be included in
1784 # an android_library or android_apk's srcjar_deps.
1786 # Variables
1787 #   sources: Paths to .aidl files to compile.
1788 #   import_include: Path to directory containing .java files imported by the
1789 #     .aidl files.
1790 #   interface_file: Preprocessed aidl file to import.
1792 # Example
1793 #   android_aidl("foo_aidl") {
1794 #     import_include = "java/src"
1795 #     sources = [
1796 #       "java/src/com/foo/bar/FooBarService.aidl",
1797 #       "java/src/com/foo/bar/FooBarServiceCallback.aidl",
1798 #     ]
1799 #   }
1800 template("android_aidl") {
1801   set_sources_assignment_filter([])
1802   forward_variables_from(invoker, [ "testonly" ])
1804   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
1805   aidl_path = "${android_sdk_build_tools}/aidl"
1806   framework_aidl = "$android_sdk/framework.aidl"
1808   action(target_name) {
1809     script = "//build/android/gyp/aidl.py"
1810     sources = invoker.sources
1812     imports = [ framework_aidl ]
1813     if (defined(invoker.interface_file)) {
1814       assert(invoker.interface_file != "")
1815       imports += [ invoker.interface_file ]
1816     }
1818     inputs = [ aidl_path ] + imports
1820     depfile = "${target_gen_dir}/${target_name}.d"
1821     outputs = [
1822       depfile,
1823       srcjar_path,
1824     ]
1825     rebased_imports = rebase_path(imports, root_build_dir)
1826     args = [
1827       "--depfile",
1828       rebase_path(depfile, root_build_dir),
1829       "--aidl-path",
1830       rebase_path(aidl_path, root_build_dir),
1831       "--imports=$rebased_imports",
1832       "--srcjar",
1833       rebase_path(srcjar_path, root_build_dir),
1834     ]
1835     if (defined(invoker.import_include) && invoker.import_include != "") {
1836       # TODO(cjhopman): aidl supports creating a depfile. We should be able to
1837       # switch to constructing a depfile for the overall action from that
1838       # instead of having all the .java files in the include paths as inputs.
1839       rebased_import_includes =
1840           rebase_path([ invoker.import_include ], root_build_dir)
1841       args += [ "--includes=$rebased_import_includes" ]
1843       _java_files_build_rel =
1844           exec_script("//build/android/gyp/find.py",
1845                       rebase_path([ invoker.import_include ], root_build_dir),
1846                       "list lines")
1847       _java_files = rebase_path(_java_files_build_rel, ".", root_build_dir)
1848       inputs += _java_files
1849     }
1850     args += rebase_path(sources, root_build_dir)
1851   }
1854 # Creates a dist directory for a native executable.
1856 # Running a native executable on a device requires all the shared library
1857 # dependencies of that executable. To make it easier to install and run such an
1858 # executable, this will create a directory containing the native exe and all
1859 # it's library dependencies.
1861 # Note: It's usually better to package things as an APK than as a native
1862 # executable.
1864 # Variables
1865 #   dist_dir: Directory for the exe and libraries. Everything in this directory
1866 #     will be deleted before copying in the exe and libraries.
1867 #   binary: Path to (stripped) executable.
1869 # Example
1870 #   create_native_executable_dist("foo_dist") {
1871 #     dist_dir = "$root_build_dir/foo_dist"
1872 #     binary = "$root_build_dir/foo"
1873 #     deps = [ ":the_thing_that_makes_foo" ]
1874 #   }
1875 template("create_native_executable_dist") {
1876   set_sources_assignment_filter([])
1877   forward_variables_from(invoker, [ "testonly" ])
1879   dist_dir = invoker.dist_dir
1880   binary = invoker.binary
1881   template_name = target_name
1883   libraries_list =
1884       "${target_gen_dir}/${template_name}_library_dependencies.list"
1886   find_deps_target_name = "${template_name}__find_library_dependencies"
1887   copy_target_name = "${template_name}__copy_libraries_and_exe"
1889   action(find_deps_target_name) {
1890     forward_variables_from(invoker, [ "deps" ])
1891     visibility = [ ":$copy_target_name" ]
1893     script = "//build/android/gyp/write_ordered_libraries.py"
1894     depfile = "$target_gen_dir/$target_name.d"
1895     inputs = [
1896       binary,
1897       android_readelf,
1898     ]
1899     outputs = [
1900       depfile,
1901       libraries_list,
1902     ]
1903     rebased_binaries = rebase_path([ binary ], root_build_dir)
1904     args = [
1905       "--depfile",
1906       rebase_path(depfile, root_build_dir),
1907       "--input-libraries=$rebased_binaries",
1908       "--libraries-dir",
1909       rebase_path(root_shlib_dir, root_build_dir),
1910       "--output",
1911       rebase_path(libraries_list, root_build_dir),
1912       "--readelf",
1913       rebase_path(android_readelf, root_build_dir),
1914     ]
1915   }
1917   copy_ex(copy_target_name) {
1918     visibility = [ ":$template_name" ]
1920     clear_dir = true
1921     inputs = [
1922       binary,
1923       libraries_list,
1924     ]
1925     dest = dist_dir
1926     rebased_binaries_list = rebase_path([ binary ], root_build_dir)
1927     rebased_libraries_list = rebase_path(libraries_list, root_build_dir)
1928     args = [
1929       "--files=$rebased_binaries_list",
1930       "--files=@FileArg($rebased_libraries_list:lib_paths)",
1931     ]
1933     deps = [
1934       ":$find_deps_target_name",
1935     ]
1936     if (defined(invoker.deps)) {
1937       deps += invoker.deps
1938     }
1939   }
1941   group(template_name) {
1942     forward_variables_from(invoker, [ "visibility" ])
1943     deps = [
1944       ":$copy_target_name",
1945     ]
1946   }
1949 # Compile a protocol buffer to java.
1951 # This generates java files from protocol buffers and creates an Android library
1952 # containing the classes.
1954 # Variables
1955 #   sources: Paths to .proto files to compile.
1956 #   proto_path: Root directory of .proto files.
1958 # Example:
1959 #  proto_java_library("foo_proto_java") {
1960 #    proto_path = [ "src/foo" ]
1961 #    sources = [ "$proto_path/foo.proto" ]
1962 #  }
1963 template("proto_java_library") {
1964   set_sources_assignment_filter([])
1965   forward_variables_from(invoker, [ "testonly" ])
1966   _protoc_dep = "//third_party/android_protobuf:android_protoc($host_toolchain)"
1967   _protoc_out_dir = get_label_info(_protoc_dep, "root_out_dir")
1968   _protoc_bin = "$_protoc_out_dir/android_protoc"
1969   _proto_path = invoker.proto_path
1971   _template_name = target_name
1973   action("${_template_name}__protoc_java") {
1974     srcjar_path = "$target_gen_dir/$target_name.srcjar"
1975     script = "//build/protoc_java.py"
1976     deps = [
1977       _protoc_dep,
1978     ]
1979     sources = invoker.sources
1980     depfile = "$target_gen_dir/$target_name.d"
1981     outputs = [
1982       depfile,
1983       srcjar_path,
1984     ]
1985     args = [
1986              "--depfile",
1987              rebase_path(depfile, root_build_dir),
1988              "--protoc",
1989              rebase_path(_protoc_bin, root_build_dir),
1990              "--proto-path",
1991              rebase_path(_proto_path, root_build_dir),
1992              "--srcjar",
1993              rebase_path(srcjar_path, root_build_dir),
1994            ] + rebase_path(sources, root_build_dir)
1995   }
1997   android_library(target_name) {
1998     java_files = []
1999     srcjar_deps = [ ":${_template_name}__protoc_java" ]
2000     deps = [
2001       "//third_party/android_protobuf:protobuf_nano_javalib",
2002     ]
2003   }
2006 # TODO(GYP): implement this.
2007 template("uiautomator_test") {
2008   set_sources_assignment_filter([])
2009   forward_variables_from(invoker, [ "testonly" ])
2010   assert(target_name != "")
2011   assert(invoker.deps != [] || true)
2012   group(target_name) {
2013   }