[Sync] Componentize HistoryModelWorker.
[chromium-blink-merge.git] / build / config / android / rules.gni
blob1c12e87a992996d4b40eb551431e9628074f4098
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     public_deps = []
88     forward_variables_from(invoker,
89                            [
90                              "deps",
91                              "public_deps",
92                              "visibility",
93                            ])
94     public_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     public_deps = []
187     forward_variables_from(invoker,
188                            [
189                              "deps",
190                              "public_deps",
191                              "visibility",
192                            ])
193     public_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     public_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     public_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     public_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     public_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 #   }
615 #   android_resources("foo_resources_overrides") {
616 #     deps = [":foo_resources"]
617 #     resource_dirs = ["res_overrides"]
618 #   }
619 template("android_resources") {
620   set_sources_assignment_filter([])
621   forward_variables_from(invoker, [ "testonly" ])
623   assert(defined(invoker.resource_dirs))
625   base_path = "$target_gen_dir/$target_name"
626   zip_path = base_path + ".resources.zip"
627   srcjar_path = base_path + ".srcjar"
628   r_text_path = base_path + "_R.txt"
629   build_config = base_path + ".build_config"
631   build_config_target_name = "${target_name}__build_config"
632   process_resources_target_name = "${target_name}__process_resources"
633   final_target_name = target_name
635   write_build_config(build_config_target_name) {
636     forward_variables_from(invoker,
637                            [
638                              "android_manifest",
639                              "custom_package",
640                              "deps",
641                            ])
643     # No package means resources override their deps.
644     if (defined(custom_package) || defined(android_manifest)) {
645       r_text = r_text_path
646     } else {
647       assert(defined(invoker.deps),
648              "Must specify deps when custom_package is omitted.")
649     }
650     visibility = [ ":$process_resources_target_name" ]
652     type = "android_resources"
653     resources_zip = zip_path
654     srcjar = srcjar_path
655   }
657   process_resources(process_resources_target_name) {
658     visibility = [ ":$final_target_name" ]
659     deps = []
660     forward_variables_from(invoker,
661                            [
662                              "android_manifest",
663                              "custom_package",
664                              "deps",
665                              "resource_dirs",
666                              "shared_resources",
667                              "v14_skip",
668                            ])
669     deps += [ ":$build_config_target_name" ]
670     if (!defined(android_manifest)) {
671       android_manifest = "//build/android/AndroidManifest.xml"
672     }
673   }
675   group(final_target_name) {
676     forward_variables_from(invoker, [ "visibility" ])
677     public_deps = [
678       ":${target_name}__process_resources",
679     ]
680   }
683 # Declare a target that generates localized strings.xml from a .grd file.
685 # If this target is included in the deps of an android resources/library/apk,
686 # the strings.xml will be included with that target.
688 # Variables
689 #   deps: Specifies the dependencies of this target.
690 #   grd_file: Path to the .grd file to generate strings.xml from.
691 #   outputs: Expected grit outputs (see grit rule).
693 # Example
694 #  java_strings_grd("foo_strings_grd") {
695 #    grd_file = "foo_strings.grd"
696 #  }
697 template("java_strings_grd") {
698   set_sources_assignment_filter([])
699   forward_variables_from(invoker, [ "testonly" ])
701   base_path = "$target_gen_dir/$target_name"
702   resources_zip = base_path + ".resources.zip"
703   build_config = base_path + ".build_config"
705   write_build_config("${target_name}__build_config") {
706     forward_variables_from(invoker, [ "deps" ])
707     type = "android_resources"
708   }
710   # Put grit files into this subdirectory of target_gen_dir.
711   extra_output_path = target_name + "_grit_output"
713   grit_target_name = "${target_name}__grit"
714   grit_output_dir = "$target_gen_dir/$extra_output_path"
715   grit(grit_target_name) {
716     grit_flags = [
717       "-E",
718       "ANDROID_JAVA_TAGGED_ONLY=false",
719     ]
720     output_dir = grit_output_dir
721     resource_ids = ""
722     source = invoker.grd_file
723     outputs = invoker.outputs
724   }
726   # This needs to get outputs from grit's internal target, not the final
727   # source_set.
728   generate_strings_outputs = get_target_outputs(":${grit_target_name}_grit")
730   zip("${target_name}__zip") {
731     base_dir = grit_output_dir
732     inputs = generate_strings_outputs
733     output = resources_zip
734     deps = [
735       ":$grit_target_name",
736     ]
737   }
739   group(target_name) {
740     public_deps = [
741       ":${target_name}__build_config",
742       ":${target_name}__zip",
743     ]
744   }
747 # Declare a target that packages strings.xml generated from a grd file.
749 # If this target is included in the deps of an android resources/library/apk,
750 # the strings.xml will be included with that target.
752 # Variables
753 #  grit_output_dir: directory containing grit-generated files.
754 #  generated_files: list of android resource files to package.
756 # Example
757 #  java_strings_grd_prebuilt("foo_strings_grd") {
758 #    grit_output_dir = "$root_gen_dir/foo/grit"
759 #    generated_files = [
760 #      "values/strings.xml"
761 #    ]
762 #  }
763 template("java_strings_grd_prebuilt") {
764   set_sources_assignment_filter([])
765   forward_variables_from(invoker, [ "testonly" ])
767   base_path = "$target_gen_dir/$target_name"
768   resources_zip = base_path + ".resources.zip"
769   build_config = base_path + ".build_config"
771   build_config_target_name = "${target_name}__build_config"
772   zip_target_name = "${target_name}__zip"
773   final_target_name = target_name
775   write_build_config(build_config_target_name) {
776     visibility = [ ":$zip_target_name" ]
777     type = "android_resources"
778   }
780   zip(zip_target_name) {
781     visibility = [ ":$final_target_name" ]
783     base_dir = invoker.grit_output_dir
784     inputs = rebase_path(invoker.generated_files, ".", base_dir)
785     output = resources_zip
786     deps = [
787       ":$build_config_target_name",
788     ]
789     if (defined(invoker.deps)) {
790       deps += invoker.deps
791     }
792   }
794   group(final_target_name) {
795     forward_variables_from(invoker, [ "visibility" ])
796     public_deps = [
797       ":$zip_target_name",
798     ]
799   }
802 # Declare a Java executable target
804 # This target creates an executable from java code and libraries. The executable
805 # will be in the output folder's /bin/ directory.
807 # Variables
808 #   deps: Specifies the dependencies of this target. Java targets in this list
809 #     will be included in the executable (and the javac classpath).
810 #   java_files: List of .java files included in this library.
811 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
812 #     will be added to java_files and be included in this library.
813 #   srcjars: List of srcjars to be included in this library, together with the
814 #     ones obtained from srcjar_deps.
815 #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
816 #     dependencies for this target. This will allow depending on an
817 #     android_library target, for example.
818 #   chromium_code: If true, extra analysis warning/errors will be enabled.
819 #   enable_errorprone: If true, enables the errorprone compiler.
820 #   main_class: When specified, a wrapper script is created within
821 #     $target_out_dir/bin to launch the binary with the given class as the
822 #     entrypoint.
823 #   wrapper_script_args: List of additional arguments for the wrapper script.
825 #   data_deps, testonly
827 # Example
828 #   java_binary("foo") {
829 #     java_files = [ "org/chromium/foo/FooMain.java" ]
830 #     deps = [ ":bar_java" ]
831 #     main_class = "org.chromium.foo.FooMain"
832 #   }
833 template("java_binary") {
834   set_sources_assignment_filter([])
836   # TODO(cjhopman): This should not act like a java_library for dependents (i.e.
837   # dependents shouldn't get the jar in their classpath, etc.).
838   java_library_impl(target_name) {
839     forward_variables_from(invoker, "*")
840     supports_android = false
841     main_class = invoker.main_class
842   }
845 # Declare a Junit executable target
847 # This target creates an executable from java code for running as a junit test
848 # suite. The executable will be in the output folder's /bin/ directory.
850 # Variables
851 #   deps: Specifies the dependencies of this target. Java targets in this list
852 #     will be included in the executable (and the javac classpath).
854 #   java_files: List of .java files included in this library.
855 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
856 #     will be added to java_files and be included in this library.
857 #   srcjars: List of srcjars to be included in this library, together with the
858 #     ones obtained from srcjar_deps.
860 #   chromium_code: If true, extra analysis warning/errors will be enabled.
862 # Example
863 #   junit_binary("foo") {
864 #     java_files = [ "org/chromium/foo/FooTest.java" ]
865 #     deps = [ ":bar_java" ]
866 #   }
867 template("junit_binary") {
868   set_sources_assignment_filter([])
870   java_binary(target_name) {
871     deps = []
872     forward_variables_from(invoker, "*")
873     bypass_platform_checks = true
874     main_class = "org.chromium.testing.local.JunitTestMain"
875     wrapper_script_args = [
876       "-test-jars",
877       "$target_name.jar",
878     ]
879     testonly = true
881     deps += [
882       "//testing/android/junit:junit_test_support",
883       "//third_party/junit",
884       "//third_party/mockito:mockito_java",
885       "//third_party/robolectric:robolectric_java",
886       "//third_party/robolectric:android-all-4.3_r2-robolectric-0",
887     ]
888   }
891 # Declare a java library target
893 # Variables
894 #   deps: Specifies the dependencies of this target. Java targets in this list
895 #     will be added to the javac classpath.
897 #   java_files: List of .java files included in this library.
898 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
899 #     will be added to java_files and be included in this library.
900 #   srcjars: List of srcjars to be included in this library, together with the
901 #     ones obtained from srcjar_deps.
902 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
903 #     this directory will be included in the library. This is only supported to
904 #     ease the gyp->gn conversion and will be removed in the future.
906 #   chromium_code: If true, extra analysis warning/errors will be enabled.
907 #   enable_errorprone: If true, enables the errorprone compiler.
909 #   jar_excluded_patterns: List of patterns of .class files to exclude from the
910 #     final jar.
912 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
913 #     be used to remove unwanted parts of the library.
914 #   proguard_config: Path to the proguard config for preprocessing.
916 #   supports_android: If true, Android targets (android_library, android_apk)
917 #     may depend on this target. Note: if true, this target must only use the
918 #     subset of Java available on Android.
919 #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
920 #     dependencies for this target. This will allow depending on an
921 #     android_library target, for example.
923 #   data_deps, testonly
925 # Example
926 #   java_library("foo_java") {
927 #     java_files = [
928 #       "org/chromium/foo/Foo.java",
929 #       "org/chromium/foo/FooInterface.java",
930 #       "org/chromium/foo/FooService.java",
931 #     ]
932 #     deps = [
933 #       ":bar_java"
934 #     ]
935 #     srcjar_deps = [
936 #       ":foo_generated_enum"
937 #     ]
938 #     jar_excluded_patterns = [
939 #       "*/FooService.class", "*/FooService##*.class"
940 #     ]
941 #   }
942 template("java_library") {
943   set_sources_assignment_filter([])
944   java_library_impl(target_name) {
945     forward_variables_from(invoker, "*")
946   }
949 # Declare a java library target for a prebuilt jar
951 # Variables
952 #   deps: Specifies the dependencies of this target. Java targets in this list
953 #     will be added to the javac classpath.
954 #   jar_path: Path to the prebuilt jar.
955 #   jar_dep: Target that builds jar_path (optional).
956 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
957 #     be used to remove unwanted parts of the library.
958 #   proguard_config: Path to the proguard config for preprocessing.
959 #   supports_android: If true, Android targets (android_library, android_apk)
960 #     may depend on this target. Note: if true, this target must only use the
961 #     subset of Java available on Android.
963 # Example
964 #   java_prebuilt("foo_java") {
965 #     jar_path = "foo.jar"
966 #     deps = [
967 #       ":foo_resources",
968 #       ":bar_java"
969 #     ]
970 #   }
971 template("java_prebuilt") {
972   set_sources_assignment_filter([])
973   java_prebuilt_impl(target_name) {
974     forward_variables_from(invoker, "*")
975   }
978 # Declare an Android library target
980 # This target creates an Android library containing java code and Android
981 # resources.
983 # Variables
984 #   deps: Specifies the dependencies of this target. Java targets in this list
985 #     will be added to the javac classpath. Android resources in dependencies
986 #     will be used when building this library.
988 #   java_files: List of .java files included in this library.
989 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
990 #     will be added to java_files and be included in this library.
991 #   srcjars: List of srcjars to be included in this library, together with the
992 #     ones obtained from srcjar_deps.
993 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
994 #     this directory will be included in the library. This is only supported to
995 #     ease the gyp->gn conversion and will be removed in the future.
997 #   chromium_code: If true, extra analysis warning/errors will be enabled.
998 #   enable_errorprone: If true, enables the errorprone compiler.
1000 #   jar_excluded_patterns: List of patterns of .class files to exclude from the
1001 #     final jar.
1003 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
1004 #     be used to remove unwanted parts of the library.
1005 #   proguard_config: Path to the proguard config for preprocessing.
1007 #   dex_path: If set, the resulting .dex.jar file will be placed under this
1008 #     path.
1011 # Example
1012 #   android_library("foo_java") {
1013 #     java_files = [
1014 #       "android/org/chromium/foo/Foo.java",
1015 #       "android/org/chromium/foo/FooInterface.java",
1016 #       "android/org/chromium/foo/FooService.java",
1017 #     ]
1018 #     deps = [
1019 #       ":bar_java"
1020 #     ]
1021 #     srcjar_deps = [
1022 #       ":foo_generated_enum"
1023 #     ]
1024 #     jar_excluded_patterns = [
1025 #       "*/FooService.class", "*/FooService##*.class"
1026 #     ]
1027 #   }
1028 template("android_library") {
1029   set_sources_assignment_filter([])
1030   assert(!defined(invoker.jar_path),
1031          "android_library does not support a custom jar path")
1032   java_library_impl(target_name) {
1033     forward_variables_from(invoker, "*")
1035     supports_android = true
1036     requires_android = true
1038     if (!defined(jar_excluded_patterns)) {
1039       jar_excluded_patterns = []
1040     }
1041     jar_excluded_patterns += [
1042       "*/R.class",
1043       "*/R##*.class",
1044       "*/Manifest.class",
1045       "*/Manifest##*.class",
1046     ]
1047   }
1050 # Declare a target that packages a set of Java dependencies into a standalone
1051 # .dex.jar.
1053 # Variables
1054 #   deps: specifies the dependencies of this target. Android libraries in deps
1055 #     will be packaged into the resulting .dex.jar file.
1056 #   dex_path: location at which the output file will be put
1057 template("android_standalone_library") {
1058   set_sources_assignment_filter([])
1059   deps_dex(target_name) {
1060     forward_variables_from(invoker,
1061                            [
1062                              "deps",
1063                              "dex_path",
1064                              "excluded_jars",
1065                            ])
1066   }
1069 # Declare an Android library target for a prebuilt jar
1071 # This target creates an Android library containing java code and Android
1072 # resources.
1074 # Variables
1075 #   deps: Specifies the dependencies of this target. Java targets in this list
1076 #     will be added to the javac classpath. Android resources in dependencies
1077 #     will be used when building this library.
1078 #   jar_path: Path to the prebuilt jar.
1079 #   proguard_preprocess: If true, proguard preprocessing will be run. This can
1080 #     be used to remove unwanted parts of the library.
1081 #   proguard_config: Path to the proguard config for preprocessing.
1083 # Example
1084 #   android_java_prebuilt("foo_java") {
1085 #     jar_path = "foo.jar"
1086 #     deps = [
1087 #       ":foo_resources",
1088 #       ":bar_java"
1089 #     ]
1090 #   }
1091 template("android_java_prebuilt") {
1092   set_sources_assignment_filter([])
1093   java_prebuilt_impl(target_name) {
1094     forward_variables_from(invoker, "*")
1095     supports_android = true
1096     requires_android = true
1097   }
1100 # Declare an Android apk target
1102 # This target creates an Android APK containing java code, resources, assets,
1103 # and (possibly) native libraries.
1105 # Variables
1106 #   android_manifest: Path to AndroidManifest.xml.
1107 #   android_manifest_dep: Target that generates AndroidManifest (if applicable)
1108 #   data_deps: List of dependencies needed at runtime. These will be built but
1109 #     won't change the generated .apk in any way (in fact they may be built
1110 #     after the .apk is).
1111 #   deps: List of dependencies. All Android java resources and libraries in the
1112 #     "transitive closure" of these dependencies will be included in the apk.
1113 #     Note: this "transitive closure" actually only includes such targets if
1114 #     they are depended on through android_library or android_resources targets
1115 #     (and so not through builtin targets like 'action', 'group', etc).
1116 #   java_files: List of .java files to include in the apk.
1117 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1118 #      will be added to java_files and be included in this apk.
1119 #   apk_name: Name for final apk.
1120 #   final_apk_path: Path to final built apk. Default is
1121 #     $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
1122 #   native_libs: List paths of native libraries to include in this apk. If these
1123 #     libraries depend on other shared_library targets, those dependencies will
1124 #     also be included in the apk.
1125 #   apk_under_test: For an instrumentation test apk, this is the target of the
1126 #     tested apk.
1127 #   include_all_resources - If true include all resource IDs in all generated
1128 #     R.java files.
1129 #   testonly: Marks this target as "test-only".
1131 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1132 #     this directory will be included in the library. This is only supported to
1133 #     ease the gyp->gn conversion and will be removed in the future.
1135 # Example
1136 #   android_apk("foo_apk") {
1137 #     android_manifest = "AndroidManifest.xml"
1138 #     java_files = [
1139 #       "android/org/chromium/foo/FooApplication.java",
1140 #       "android/org/chromium/foo/FooActivity.java",
1141 #     ]
1142 #     deps = [
1143 #       ":foo_support_java"
1144 #       ":foo_resources"
1145 #     ]
1146 #     srcjar_deps = [
1147 #       ":foo_generated_enum"
1148 #     ]
1149 #     native_libs = [
1150 #       native_lib_path
1151 #     ]
1152 #   }
1153 template("android_apk") {
1154   set_sources_assignment_filter([])
1155   forward_variables_from(invoker, [ "testonly" ])
1157   assert(defined(invoker.final_apk_path) || defined(invoker.apk_name))
1158   assert(defined(invoker.android_manifest))
1159   gen_dir = "$target_gen_dir/$target_name"
1160   base_path = "$gen_dir/$target_name"
1161   _build_config = "$target_gen_dir/$target_name.build_config"
1162   resources_zip_path = "$base_path.resources.zip"
1163   _all_resources_zip_path = "$base_path.resources.all.zip"
1164   _jar_path = "$base_path.jar"
1165   _lib_dex_path = "$base_path.dex.jar"
1166   _rebased_lib_dex_path = rebase_path(_lib_dex_path, root_build_dir)
1167   _template_name = target_name
1169   final_dex_path = "$gen_dir/classes.dex"
1170   final_dex_target_name = "${_template_name}__final_dex"
1172   _final_apk_path = ""
1173   if (defined(invoker.final_apk_path)) {
1174     _final_apk_path = invoker.final_apk_path
1175   } else if (defined(invoker.apk_name)) {
1176     _final_apk_path = "$root_build_dir/apks/" + invoker.apk_name + ".apk"
1177   }
1178   _dist_jar_path_list =
1179       process_file_template(
1180           [ _final_apk_path ],
1181           "$root_build_dir/test.lib.java/{{source_name_part}}.jar")
1182   _dist_jar_path = _dist_jar_path_list[0]
1183   _final_apk_path_no_ext_list =
1184       process_file_template([ _final_apk_path ],
1185                             "{{source_dir}}/{{source_name_part}}")
1186   _final_apk_path_no_ext = _final_apk_path_no_ext_list[0]
1187   assert(_final_apk_path_no_ext != "")  # Mark as used.
1189   _native_libs = []
1191   _version_code = "1"
1192   if (defined(invoker.version_code)) {
1193     _version_code = invoker.version_code
1194   }
1196   _version_name = "Developer Build"
1197   if (defined(invoker.version_name)) {
1198     _version_name = invoker.version_name
1199   }
1200   _keystore_path = android_keystore_path
1201   _keystore_name = android_keystore_name
1202   _keystore_password = android_keystore_password
1204   if (defined(invoker.keystore_path)) {
1205     _keystore_path = invoker.keystore_path
1206     _keystore_name = invoker.keystore_name
1207     _keystore_password = invoker.keystore_password
1208   }
1210   _srcjar_deps = []
1211   if (defined(invoker.srcjar_deps)) {
1212     _srcjar_deps += invoker.srcjar_deps
1213   }
1215   _load_library_from_apk = false
1217   # The dependency that makes the chromium linker, if any is needed.
1218   _chromium_linker_dep = []
1220   if (defined(invoker.native_libs)) {
1221     _use_chromium_linker = false
1222     if (defined(invoker.use_chromium_linker)) {
1223       _use_chromium_linker =
1224           invoker.use_chromium_linker && chromium_linker_supported
1225       _chromium_linker_dep = [ "//base/android/linker:chromium_android_linker" ]
1226     }
1228     if (defined(invoker.load_library_from_apk) &&
1229         invoker.load_library_from_apk) {
1230       _load_library_from_apk = true
1231       assert(_use_chromium_linker,
1232              "Loading library from the apk requires use" +
1233                  " of the Chromium linker.")
1234     }
1236     _enable_relocation_packing = false
1237     if (defined(invoker.enable_relocation_packing) &&
1238         invoker.enable_relocation_packing) {
1239       _enable_relocation_packing = relocation_packing_supported
1240       assert(_use_chromium_linker,
1241              "Relocation packing requires use of the" + " Chromium linker.")
1242     }
1244     if (is_component_build) {
1245       _native_libs += [ "$root_shlib_dir/libc++_shared.so" ]
1246       _chromium_linker_dep += [ "//build/android:cpplib_stripped" ]
1247     }
1249     # Allow native_libs to be in the form "foo.so" or "foo.cr.so"
1250     _first_ext_removed =
1251         process_file_template(invoker.native_libs, "{{source_name_part}}")
1252     _native_libs += process_file_template(
1253             _first_ext_removed,
1254             "$root_shlib_dir/{{source_name_part}}$shlib_extension")
1256     # Add in target_cpu so that other architectures are not accidentally
1257     # included when switching target_cpu without doing a clean build.
1258     _native_libs_dir = gen_dir + "/lib-$target_cpu"
1260     if (_use_chromium_linker) {
1261       _native_libs +=
1262           [ "$root_shlib_dir/libchromium_android_linker$shlib_extension" ]
1263     }
1265     _enable_relocation_packing = false
1266     if (_use_chromium_linker && defined(invoker.enable_relocation_packing) &&
1267         invoker.enable_relocation_packing) {
1268       _enable_relocation_packing = true
1269     }
1271     _native_lib_version_rule = ""
1272     if (defined(invoker.native_lib_version_rule)) {
1273       _native_lib_version_rule = invoker.native_lib_version_rule
1274     }
1275     _native_lib_version_arg = "\"\""
1276     if (defined(invoker.native_lib_version_arg)) {
1277       _native_lib_version_arg = invoker.native_lib_version_arg
1278     }
1279   }
1281   _android_manifest_deps = []
1282   if (defined(invoker.android_manifest_dep)) {
1283     _android_manifest_deps = [ invoker.android_manifest_dep ]
1284   }
1285   _android_manifest = invoker.android_manifest
1287   _rebased_build_config = rebase_path(_build_config, root_build_dir)
1288   _create_abi_split =
1289       defined(invoker.create_abi_split) && invoker.create_abi_split
1290   _create_density_splits =
1291       defined(invoker.create_density_splits) && invoker.create_density_splits
1292   _create_language_splits =
1293       defined(invoker.language_splits) && invoker.language_splits != []
1295   # Help GN understand that _create_abi_split is not unused (bug in GN).
1296   assert(_create_abi_split || true)
1298   build_config_target = "${_template_name}__build_config"
1299   write_build_config(build_config_target) {
1300     forward_variables_from(invoker, [ "apk_under_test" ])
1301     type = "android_apk"
1302     jar_path = _jar_path
1303     dex_path = final_dex_path
1304     resources_zip = resources_zip_path
1305     build_config = _build_config
1306     android_manifest = _android_manifest
1308     deps = _chromium_linker_dep + _android_manifest_deps
1309     if (defined(invoker.deps)) {
1310       deps += invoker.deps
1311     }
1313     native_libs = _native_libs
1314   }
1316   _final_deps = []
1317   _incremental_final_deps = []
1319   process_resources_target = "${_template_name}__process_resources"
1320   process_resources(process_resources_target) {
1321     forward_variables_from(invoker, [ "include_all_resources" ])
1322     srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
1323     r_text_path = "${target_gen_dir}/${target_name}_R.txt"
1324     android_manifest = _android_manifest
1325     resource_dirs = [ "//build/android/ant/empty/res" ]
1326     zip_path = resources_zip_path
1327     all_resources_zip_path = _all_resources_zip_path
1328     generate_constant_ids = true
1330     build_config = _build_config
1331     deps = _android_manifest_deps + [ ":$build_config_target" ]
1332     if (defined(invoker.deps)) {
1333       deps += invoker.deps
1334     }
1335   }
1336   _srcjar_deps += [ ":$process_resources_target" ]
1338   if (_native_libs != []) {
1339     _enable_chromium_linker_tests = false
1340     if (defined(invoker.enable_chromium_linker_tests)) {
1341       _enable_chromium_linker_tests = invoker.enable_chromium_linker_tests
1342     }
1344     java_cpp_template("${_template_name}__native_libraries_java") {
1345       package_name = "org/chromium/base/library_loader"
1346       sources = [
1347         "//base/android/java/templates/NativeLibraries.template",
1348       ]
1349       inputs = [
1350         _build_config,
1351       ]
1352       deps = [
1353         ":$build_config_target",
1354       ]
1355       if (_native_lib_version_rule != "") {
1356         deps += [ _native_lib_version_rule ]
1357       }
1359       defines = [
1360         "NATIVE_LIBRARIES_LIST=" +
1361             "@FileArg($_rebased_build_config:native:java_libraries_list)",
1362         "NATIVE_LIBRARIES_VERSION_NUMBER=$_native_lib_version_arg",
1363       ]
1364       if (_use_chromium_linker) {
1365         defines += [ "ENABLE_CHROMIUM_LINKER" ]
1366       }
1367       if (_load_library_from_apk) {
1368         defines += [ "ENABLE_CHROMIUM_LINKER_LIBRARY_IN_ZIP_FILE" ]
1369       }
1370       if (_enable_chromium_linker_tests) {
1371         defines += [ "ENABLE_CHROMIUM_LINKER_TESTS" ]
1372       }
1373     }
1374     _srcjar_deps += [ ":${_template_name}__native_libraries_java" ]
1375   }
1377   java_target = "${_template_name}__java"
1378   java_library_impl(java_target) {
1379     forward_variables_from(invoker, [ "run_findbugs" ])
1380     supports_android = true
1381     requires_android = true
1382     override_build_config = _build_config
1383     deps = _android_manifest_deps + [ ":$build_config_target" ]
1385     android_manifest = _android_manifest
1386     chromium_code = true
1387     if (defined(invoker.java_files)) {
1388       java_files = invoker.java_files
1389     } else if (defined(invoker.DEPRECATED_java_in_dir)) {
1390       DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
1391     } else {
1392       java_files = []
1393     }
1394     srcjar_deps = _srcjar_deps
1395     jar_path = _jar_path
1396     dex_path = _lib_dex_path
1398     if (defined(invoker.deps)) {
1399       deps += invoker.deps
1400     }
1401   }
1403   if (_dist_jar_path != "") {
1404     create_dist_target = "${_template_name}__create_dist_jar"
1405     _final_deps += [ ":$create_dist_target" ]
1406     _incremental_final_deps += [ ":$create_dist_target" ]
1408     # TODO(cjhopman): This is only ever needed to calculate the list of tests to
1409     # run. See build/android/pylib/instrumentation/test_jar.py. We should be
1410     # able to just do that calculation at build time instead.
1411     action(create_dist_target) {
1412       script = "//build/android/gyp/create_dist_jar.py"
1413       depfile = "$target_gen_dir/$target_name.d"
1414       inputs = [
1415         _build_config,
1416       ]
1417       outputs = [
1418         depfile,
1419         _dist_jar_path,
1420       ]
1421       args = [
1422         "--depfile",
1423         rebase_path(depfile, root_build_dir),
1424         "--output",
1425         rebase_path(_dist_jar_path, root_build_dir),
1426         "--inputs=@FileArg($_rebased_build_config:dist_jar:dependency_jars)",
1427       ]
1428       inputs += [ _jar_path ]
1429       _rebased_jar_path = rebase_path([ _jar_path ], root_build_dir)
1430       args += [ "--inputs=$_rebased_jar_path" ]
1431       deps = [
1432         ":$build_config_target",  # Generates the build config file.
1433         ":$java_target",  # Generates the jar file.
1434       ]
1435     }
1436   }
1438   dex("$final_dex_target_name") {
1439     deps = [
1440       ":$build_config_target",
1441       ":$java_target",
1442     ]
1443     inputs = [
1444       _build_config,
1445     ]
1446     output = final_dex_path
1447     _dex_arg_key = "${_rebased_build_config}:final_dex:dependency_dex_files"
1448     args = [
1449       "--inputs=@FileArg($_dex_arg_key)",
1450       _rebased_lib_dex_path,
1451     ]
1452   }
1454   if (_native_libs != []) {
1455     action("${_template_name}__prepare_native") {
1456       forward_variables_from(invoker,
1457                              [
1458                                "data_deps",
1459                                "public_deps",
1460                              ])
1461       script = "//build/android/gyp/pack_relocations.py"
1462       packed_libraries_dir = "$_native_libs_dir/$android_app_abi"
1463       depfile = "$target_gen_dir/$target_name.d"
1464       outputs = [
1465         depfile,
1466       ]
1468       inputs = _native_libs
1469       deps = _chromium_linker_dep
1471       inputs += [ _build_config ]
1472       deps += [ ":$build_config_target" ]
1474       skip_packing_list = [
1475         "gdbserver",
1476         "libchromium_android_linker$shlib_extension",
1477       ]
1479       enable_packing_arg = 0
1480       if (_enable_relocation_packing) {
1481         enable_packing_arg = 1
1482         deps += [ relocation_packer_target ]
1483       }
1485       args = [
1486         "--depfile",
1487         rebase_path(depfile, root_build_dir),
1488         "--enable-packing=$enable_packing_arg",
1489         "--exclude-packing-list=$skip_packing_list",
1490         "--android-pack-relocations",
1491         rebase_path(relocation_packer_exe, root_build_dir),
1492         "--stripped-libraries-dir",
1493         rebase_path(root_build_dir, root_build_dir),
1494         "--packed-libraries-dir",
1495         rebase_path(packed_libraries_dir, root_build_dir),
1496         "--libraries=@FileArg(${_rebased_build_config}:native:libraries)",
1497         "--clear-dir",
1498       ]
1500       if (defined(invoker.deps)) {
1501         deps += invoker.deps
1502       }
1504       if (is_debug) {
1505         rebased_gdbserver = rebase_path([ android_gdbserver ], root_build_dir)
1506         inputs += [ android_gdbserver ]
1507         args += [ "--libraries=$rebased_gdbserver" ]
1508       }
1509     }
1510   }
1512   _final_deps += [ ":${_template_name}__create" ]
1513   _incremental_final_deps += [ ":${_template_name}__create_incremental" ]
1514   create_apk("${_template_name}__create") {
1515     forward_variables_from(invoker, [ "language_splits" ])
1516     apk_path = _final_apk_path
1517     android_manifest = _android_manifest
1518     resources_zip = _all_resources_zip_path
1519     dex_path = final_dex_path
1520     load_library_from_apk = _load_library_from_apk
1521     create_density_splits = _create_density_splits
1522     if (defined(invoker.extensions_to_not_compress)) {
1523       extensions_to_not_compress = invoker.extensions_to_not_compress
1524     } else {
1525       # Allow icu data, v8 snapshots, and pak files to be loaded directly from
1526       # the .apk.
1527       # Note: These are actually suffix matches, not necessarily extensions.
1528       extensions_to_not_compress = ".dat,.bin,.pak"
1529     }
1531     version_code = _version_code
1532     version_name = _version_name
1534     keystore_name = _keystore_name
1535     keystore_path = _keystore_path
1536     keystore_password = _keystore_password
1538     # This target generates the input file _all_resources_zip_path.
1539     deps = _android_manifest_deps + [
1540              ":$process_resources_target",
1541              ":$final_dex_target_name",
1542            ]
1543     if (defined(invoker.deps)) {
1544       deps += invoker.deps
1545     }
1547     if (defined(invoker.asset_location)) {
1548       asset_location = invoker.asset_location
1550       # We don't know the exact dependencies that create the assets in
1551       # |asset_location|; we depend on all caller deps until a better solution
1552       # is figured out (http://crbug.com/433330).
1553       if (defined(invoker.deps)) {
1554         deps += invoker.deps
1555       }
1556     }
1558     if (_native_libs != [] && !_create_abi_split) {
1559       native_libs_dir = _native_libs_dir
1560       deps += [ ":${_template_name}__prepare_native" ]
1561     }
1562   }
1564   if (_native_libs != [] && _create_abi_split) {
1565     _manifest_rule = "${_template_name}__split_manifest_abi_${android_app_abi}"
1566     generate_split_manifest(_manifest_rule) {
1567       main_manifest = _android_manifest
1568       out_manifest =
1569           "$gen_dir/split-manifests/${android_app_abi}/AndroidManifest.xml"
1570       split_name = "abi_${android_app_abi}"
1571       deps = _android_manifest_deps
1572     }
1574     _apk_rule = "${_template_name}__split_apk_abi_${android_app_abi}"
1575     _final_deps += [ ":$_apk_rule" ]
1577     # Don't add this to _incremental_final_deps since for incremental installs
1578     # we skip abi splits altogether.
1579     create_apk(_apk_rule) {
1580       apk_path = "${_final_apk_path_no_ext}-abi-${android_app_abi}.apk"
1581       base_path = "$gen_dir/$_apk_rule"
1583       manifest_outputs = get_target_outputs(":${_manifest_rule}")
1584       android_manifest = manifest_outputs[1]
1585       load_library_from_apk = _load_library_from_apk
1587       version_code = _version_code
1588       version_name = _version_name
1590       keystore_name = _keystore_name
1591       keystore_path = _keystore_path
1592       keystore_password = _keystore_password
1594       native_libs_dir = _native_libs_dir
1595       deps = [
1596         ":${_template_name}__prepare_native",
1597         ":${_manifest_rule}",
1598       ]
1599     }
1600   }
1602   _create_incremental_script_rule_name = "${_template_name}__incremental_script"
1603   _incremental_final_deps += [ ":${_create_incremental_script_rule_name}" ]
1604   action(_create_incremental_script_rule_name) {
1605     script = "//build/android/incremental_install/create_install_script.py"
1606     depfile = "$target_gen_dir/$target_name.d"
1607     deps = [
1608       ":$build_config_target",
1609     ]
1611     _generated_script_path =
1612         "${root_out_dir}/bin/install_incremental_${_template_name}"
1613     outputs = [
1614       depfile,
1615       _generated_script_path,
1616     ]
1618     _rebased_apk_path_no_ext =
1619         rebase_path(_final_apk_path_no_ext, root_build_dir)
1620     _rebased_generated_script_path =
1621         rebase_path(_generated_script_path, root_build_dir)
1622     _rebased_depfile = rebase_path(depfile, root_build_dir)
1623     _dex_arg_key = "${_rebased_build_config}:final_dex:dependency_dex_files"
1624     args = [
1625       "--apk-path=${_rebased_apk_path_no_ext}_incremental.apk",
1626       "--script-output-path=$_rebased_generated_script_path",
1627       "--dex-file=$_rebased_lib_dex_path",
1628       "--dex-file-list=@FileArg($_dex_arg_key)",
1629       "--depfile=$_rebased_depfile",
1630     ]
1631     if (defined(_native_libs_dir)) {
1632       _rebased_native_libs_dir = rebase_path(_native_libs_dir, root_build_dir)
1633       args += [ "--lib-dir=$_rebased_native_libs_dir/$android_app_abi" ]
1634     }
1635     if (_create_density_splits) {
1636       args += [ "--split=${_rebased_apk_path_no_ext}-density-*.apk" ]
1637     }
1638     if (_create_language_splits) {
1639       args += [ "--split=${_rebased_apk_path_no_ext}-language-*.apk" ]
1640     }
1641   }
1643   group(target_name) {
1644     forward_variables_from(invoker, [ "data_deps" ])
1645     public_deps = _final_deps
1646   }
1647   group("${target_name}_incremental") {
1648     data_deps = []
1649     forward_variables_from(invoker, [ "data_deps" ])
1650     data_deps += [ "//build/android/pylib/device/commands" ]
1651     public_deps = _incremental_final_deps
1652   }
1655 # Declare an Android instrumentation test apk
1657 # This target creates an Android instrumentation test apk.
1659 # Variables
1660 #   android_manifest: Path to AndroidManifest.xml.
1661 #   data_deps: List of dependencies needed at runtime. These will be built but
1662 #     won't change the generated .apk in any way (in fact they may be built
1663 #     after the .apk is).
1664 #   deps: List of dependencies. All Android java resources and libraries in the
1665 #     "transitive closure" of these dependencies will be included in the apk.
1666 #     Note: this "transitive closure" actually only includes such targets if
1667 #     they are depended on through android_library or android_resources targets
1668 #     (and so not through builtin targets like 'action', 'group', etc).
1669 #   java_files: List of .java files to include in the apk.
1670 #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1671 #      will be added to java_files and be included in this apk.
1672 #   apk_name: Name for final apk.
1673 #   final_apk_path: Path to final built apk. Default is
1674 #     $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
1675 #   native_libs: List paths of native libraries to include in this apk. If these
1676 #     libraries depend on other shared_library targets, those dependencies will
1677 #     also be included in the apk.
1678 #   apk_under_test: The apk being tested.
1679 #   isolate_file: Isolate file containing the list of test data dependencies.
1681 #   DEPRECATED_java_in_dir: Directory containing java files. All .java files in
1682 #     this directory will be included in the library. This is only supported to
1683 #     ease the gyp->gn conversion and will be removed in the future.
1685 # Example
1686 #   instrumentation_test_apk("foo_test_apk") {
1687 #     android_manifest = "AndroidManifest.xml"
1688 #     apk_name = "FooTest"
1689 #     apk_under_test = "Foo"
1690 #     java_files = [
1691 #       "android/org/chromium/foo/FooTestCase.java",
1692 #       "android/org/chromium/foo/FooExampleTest.java",
1693 #     ]
1694 #     deps = [
1695 #       ":foo_test_support_java"
1696 #     ]
1697 #   }
1698 template("instrumentation_test_apk") {
1699   set_sources_assignment_filter([])
1700   testonly = true
1701   _template_name = target_name
1703   if (defined(invoker.apk_name)) {
1704     test_runner_data_dep = [ ":${_template_name}__test_runner_script" ]
1705     test_runner_script("${_template_name}__test_runner_script") {
1706       forward_variables_from(invoker, [ "isolate_file" ])
1707       test_name = invoker.target_name
1708       test_type = "instrumentation"
1709       test_apk = invoker.apk_name
1710     }
1711   }
1713   android_apk(target_name) {
1714     deps = []
1715     data_deps = []
1716     forward_variables_from(invoker, "*")
1717     data_deps += [
1718       "//testing/android/driver:driver_apk",
1719       "//tools/android/forwarder2",
1720       "//tools/android/md5sum",
1721     ]
1722     if (defined(test_runner_data_dep)) {
1723       data_deps += test_runner_data_dep
1724     }
1725     deps += [ "//testing/android/broker:broker_java" ]
1726     run_findbugs = false
1727   }
1730 # Declare an Android gtest apk
1732 # This target creates an Android apk for running gtest-based unittests.
1734 # Variables
1735 #   deps: Specifies the dependencies of this target. These will be passed to
1736 #     the underlying android_apk invocation and should include the java and
1737 #     resource dependencies of the apk.
1738 #   unittests_dep: This should be the label of the gtest native target. This
1739 #     target must be defined previously in the same file.
1740 #   unittests_binary: The basename of the library produced by the unittests_dep
1741 #     target. If unspecified, it assumes the name of the unittests_dep target
1742 #     (which will be correct unless that target specifies an "output_name".
1743 #     TODO(brettw) make this automatic by allowing get_target_outputs to
1744 #     support executables.
1745 #   apk_name: The name of the produced apk. If unspecified, it uses the name
1746 #             of the unittests_dep target postfixed with "_apk"
1748 # Example
1749 #   unittest_apk("foo_unittests_apk") {
1750 #     deps = [ ":foo_java", ":foo_resources" ]
1751 #     unittests_dep = ":foo_unittests"
1752 #   }
1753 template("unittest_apk") {
1754   set_sources_assignment_filter([])
1755   testonly = true
1757   assert(defined(invoker.unittests_dep), "Need unittests_dep for $target_name")
1759   test_suite_name = get_label_info(invoker.unittests_dep, "name")
1761   # This trivial assert is needed in case both unittests_binary and apk_name
1762   # are defined, as otherwise test_suite_name would not be used.
1763   assert(test_suite_name != "")
1765   if (defined(invoker.unittests_binary)) {
1766     unittests_binary = invoker.unittests_binary
1767   } else {
1768     unittests_binary = "lib${test_suite_name}${shlib_extension}"
1769   }
1771   if (defined(invoker.apk_name)) {
1772     apk_name = invoker.apk_name
1773   } else {
1774     apk_name = test_suite_name
1775   }
1777   android_apk(target_name) {
1778     forward_variables_from(invoker, [ "asset_location" ])
1779     final_apk_path = "$root_build_dir/${apk_name}_apk/${apk_name}-debug.apk"
1780     java_files = [
1781       "//testing/android/native_test/java/src/org/chromium/native_test/NativeBrowserTestActivity.java",
1782       "//testing/android/native_test/java/src/org/chromium/native_test/NativeTestActivity.java",
1783       "//testing/android/native_test/java/src/org/chromium/native_test/NativeUnitTestActivity.java",
1784       "//testing/android/native_test/java/src/org/chromium/native_test/NativeTestInstrumentationTestRunner.java",
1785     ]
1786     android_manifest = "//testing/android/native_test/java/AndroidManifest.xml"
1787     native_libs = [ unittests_binary ]
1788     deps = [
1789       "//base:base_java",
1790       "//build/android/pylib/remote/device/dummy:remote_device_dummy_apk",
1791       "//testing/android/appurify_support:appurify_support_java",
1792       "//testing/android/reporter:reporter_java",
1793     ]
1794     if (defined(invoker.deps)) {
1795       deps += invoker.deps
1796     }
1797     data_deps = [ "//tools/android/md5sum" ]
1798     if (host_os == "linux") {
1799       data_deps += [ "//tools/android/forwarder2" ]
1800     }
1801     if (defined(invoker.data_deps)) {
1802       data_deps += invoker.data_deps
1803     }
1804   }
1807 # Generate .java files from .aidl files.
1809 # This target will store the .java files in a srcjar and should be included in
1810 # an android_library or android_apk's srcjar_deps.
1812 # Variables
1813 #   sources: Paths to .aidl files to compile.
1814 #   import_include: Path to directory containing .java files imported by the
1815 #     .aidl files.
1816 #   interface_file: Preprocessed aidl file to import.
1818 # Example
1819 #   android_aidl("foo_aidl") {
1820 #     import_include = "java/src"
1821 #     sources = [
1822 #       "java/src/com/foo/bar/FooBarService.aidl",
1823 #       "java/src/com/foo/bar/FooBarServiceCallback.aidl",
1824 #     ]
1825 #   }
1826 template("android_aidl") {
1827   set_sources_assignment_filter([])
1828   forward_variables_from(invoker, [ "testonly" ])
1830   srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
1831   aidl_path = "${android_sdk_build_tools}/aidl"
1832   framework_aidl = "$android_sdk/framework.aidl"
1834   action(target_name) {
1835     script = "//build/android/gyp/aidl.py"
1836     sources = invoker.sources
1838     imports = [ framework_aidl ]
1839     if (defined(invoker.interface_file)) {
1840       assert(invoker.interface_file != "")
1841       imports += [ invoker.interface_file ]
1842     }
1844     inputs = [ aidl_path ] + imports
1846     depfile = "${target_gen_dir}/${target_name}.d"
1847     outputs = [
1848       depfile,
1849       srcjar_path,
1850     ]
1851     rebased_imports = rebase_path(imports, root_build_dir)
1852     args = [
1853       "--depfile",
1854       rebase_path(depfile, root_build_dir),
1855       "--aidl-path",
1856       rebase_path(aidl_path, root_build_dir),
1857       "--imports=$rebased_imports",
1858       "--srcjar",
1859       rebase_path(srcjar_path, root_build_dir),
1860     ]
1861     if (defined(invoker.import_include) && invoker.import_include != "") {
1862       # TODO(cjhopman): aidl supports creating a depfile. We should be able to
1863       # switch to constructing a depfile for the overall action from that
1864       # instead of having all the .java files in the include paths as inputs.
1865       rebased_import_includes =
1866           rebase_path([ invoker.import_include ], root_build_dir)
1867       args += [ "--includes=$rebased_import_includes" ]
1869       _java_files_build_rel =
1870           exec_script("//build/android/gyp/find.py",
1871                       rebase_path([ invoker.import_include ], root_build_dir),
1872                       "list lines")
1873       _java_files = rebase_path(_java_files_build_rel, ".", root_build_dir)
1874       inputs += _java_files
1875     }
1876     args += rebase_path(sources, root_build_dir)
1877   }
1880 # Creates a dist directory for a native executable.
1882 # Running a native executable on a device requires all the shared library
1883 # dependencies of that executable. To make it easier to install and run such an
1884 # executable, this will create a directory containing the native exe and all
1885 # it's library dependencies.
1887 # Note: It's usually better to package things as an APK than as a native
1888 # executable.
1890 # Variables
1891 #   dist_dir: Directory for the exe and libraries. Everything in this directory
1892 #     will be deleted before copying in the exe and libraries.
1893 #   binary: Path to (stripped) executable.
1895 # Example
1896 #   create_native_executable_dist("foo_dist") {
1897 #     dist_dir = "$root_build_dir/foo_dist"
1898 #     binary = "$root_build_dir/foo"
1899 #     deps = [ ":the_thing_that_makes_foo" ]
1900 #   }
1901 template("create_native_executable_dist") {
1902   set_sources_assignment_filter([])
1903   forward_variables_from(invoker, [ "testonly" ])
1905   dist_dir = invoker.dist_dir
1906   binary = invoker.binary
1907   template_name = target_name
1909   libraries_list =
1910       "${target_gen_dir}/${template_name}_library_dependencies.list"
1912   find_deps_target_name = "${template_name}__find_library_dependencies"
1913   copy_target_name = "${template_name}__copy_libraries_and_exe"
1915   action(find_deps_target_name) {
1916     forward_variables_from(invoker, [ "deps" ])
1917     visibility = [ ":$copy_target_name" ]
1919     script = "//build/android/gyp/write_ordered_libraries.py"
1920     depfile = "$target_gen_dir/$target_name.d"
1921     inputs = [
1922       binary,
1923       android_readelf,
1924     ]
1925     outputs = [
1926       depfile,
1927       libraries_list,
1928     ]
1929     rebased_binaries = rebase_path([ binary ], root_build_dir)
1930     args = [
1931       "--depfile",
1932       rebase_path(depfile, root_build_dir),
1933       "--input-libraries=$rebased_binaries",
1934       "--libraries-dir",
1935       rebase_path(root_shlib_dir, root_build_dir),
1936       "--output",
1937       rebase_path(libraries_list, root_build_dir),
1938       "--readelf",
1939       rebase_path(android_readelf, root_build_dir),
1940     ]
1941   }
1943   copy_ex(copy_target_name) {
1944     visibility = [ ":$template_name" ]
1946     clear_dir = true
1947     inputs = [
1948       binary,
1949       libraries_list,
1950     ]
1951     dest = dist_dir
1952     rebased_binaries_list = rebase_path([ binary ], root_build_dir)
1953     rebased_libraries_list = rebase_path(libraries_list, root_build_dir)
1954     args = [
1955       "--files=$rebased_binaries_list",
1956       "--files=@FileArg($rebased_libraries_list:lib_paths)",
1957     ]
1959     deps = [
1960       ":$find_deps_target_name",
1961     ]
1962     if (defined(invoker.deps)) {
1963       deps += invoker.deps
1964     }
1965   }
1967   group(template_name) {
1968     forward_variables_from(invoker, [ "visibility" ])
1969     public_deps = [
1970       ":$copy_target_name",
1971     ]
1972   }
1975 # Compile a protocol buffer to java.
1977 # This generates java files from protocol buffers and creates an Android library
1978 # containing the classes.
1980 # Variables
1981 #   sources: Paths to .proto files to compile.
1982 #   proto_path: Root directory of .proto files.
1984 # Example:
1985 #  proto_java_library("foo_proto_java") {
1986 #    proto_path = [ "src/foo" ]
1987 #    sources = [ "$proto_path/foo.proto" ]
1988 #  }
1989 template("proto_java_library") {
1990   set_sources_assignment_filter([])
1991   forward_variables_from(invoker, [ "testonly" ])
1992   _protoc_dep = "//third_party/android_protobuf:android_protoc($host_toolchain)"
1993   _protoc_out_dir = get_label_info(_protoc_dep, "root_out_dir")
1994   _protoc_bin = "$_protoc_out_dir/android_protoc"
1995   _proto_path = invoker.proto_path
1997   _template_name = target_name
1999   action("${_template_name}__protoc_java") {
2000     srcjar_path = "$target_gen_dir/$target_name.srcjar"
2001     script = "//build/protoc_java.py"
2002     deps = [
2003       _protoc_dep,
2004     ]
2005     sources = invoker.sources
2006     depfile = "$target_gen_dir/$target_name.d"
2007     outputs = [
2008       depfile,
2009       srcjar_path,
2010     ]
2011     args = [
2012              "--depfile",
2013              rebase_path(depfile, root_build_dir),
2014              "--protoc",
2015              rebase_path(_protoc_bin, root_build_dir),
2016              "--proto-path",
2017              rebase_path(_proto_path, root_build_dir),
2018              "--srcjar",
2019              rebase_path(srcjar_path, root_build_dir),
2020            ] + rebase_path(sources, root_build_dir)
2021   }
2023   android_library(target_name) {
2024     java_files = []
2025     srcjar_deps = [ ":${_template_name}__protoc_java" ]
2026     deps = [
2027       "//third_party/android_protobuf:protobuf_nano_javalib",
2028     ]
2029   }
2032 # TODO(GYP): implement this.
2033 template("uiautomator_test") {
2034   set_sources_assignment_filter([])
2035   forward_variables_from(invoker, [ "testonly" ])
2036   assert(target_name != "")
2037   assert(invoker.deps != [] || true)
2038   group(target_name) {
2039   }