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.
6 import("internal_rules.gni")
10 # This target generates the native jni bindings for a set of .java files.
12 # See base/android/jni_generator/jni_generator.py for more info about the
13 # format of generating JNI bindings.
16 # sources: list of .java files to generate jni for
17 # jni_package: subdirectory path for generated bindings
20 # generate_jni("foo_jni") {
22 # "android/java/src/org/chromium/foo/Foo.java",
23 # "android/java/src/org/chromium/foo/FooUtil.java",
27 template("generate_jni") {
28 assert(defined(invoker.sources))
29 assert(defined(invoker.jni_package))
30 jni_package = invoker.jni_package
31 base_output_dir = "${root_gen_dir}/${target_name}/${jni_package}"
32 jni_output_dir = "${base_output_dir}/jni"
34 jni_generator_include = "//base/android/jni_generator/jni_generator_helper.h"
36 foreach_target_name = "${target_name}__jni_gen"
37 action_foreach(foreach_target_name) {
38 script = "//base/android/jni_generator/jni_generator.py"
39 depfile = "$target_gen_dir/$target_name.{{source_name_part}}.d"
40 sources = invoker.sources
41 source_prereqs = [ jni_generator_include ]
44 "${jni_output_dir}/{{source_name_part}}_jni.h"
48 "--depfile", rebase_path(depfile, root_build_dir),
49 "--input_file={{source}}",
50 "--optimize_generation=1",
52 "--output_dir", rebase_path(jni_output_dir, root_build_dir),
53 "--includes", rebase_path(jni_generator_include, "//"),
55 if (defined(invoker.jni_generator_jarjar_file)) {
57 "--jarjar", rebase_path(jni_generator_jarjar_file, root_build_dir),
62 config("jni_includes_${target_name}") {
63 include_dirs = [ base_output_dir ]
67 deps = [ ":$foreach_target_name" ]
68 direct_dependent_configs = [ ":jni_includes_${target_name}" ]
70 if (defined(invoker.deps)) {
73 if (defined(invoker.forward_dependent_configs_from)) {
74 forward_dependent_configs_from = invoker.forward_dependent_configs_from
80 # Declare a jni target for a prebuilt jar
82 # This target generates the native jni bindings for a set of classes in a .jar.
84 # See base/android/jni_generator/jni_generator.py for more info about the
85 # format of generating JNI bindings.
88 # classes: list of .class files in the jar to generate jni for. These should
89 # include the full path to the .class file.
90 # jni_package: subdirectory path for generated bindings
91 # jar_file: the path to the .jar. If not provided, will default to the sdk's
95 # generate_jar_jni("foo_jni") {
97 # "android/view/Foo.class",
101 template("generate_jar_jni") {
102 assert(defined(invoker.classes))
103 assert(defined(invoker.jni_package))
105 if (defined(invoker.jar_file)) {
106 jar_file = invoker.jar_file
108 jar_file = android_sdk_jar
111 jni_package = invoker.jni_package
112 base_output_dir = "${root_gen_dir}/${target_name}/${jni_package}"
113 jni_output_dir = "${base_output_dir}/jni"
115 jni_generator_include =
116 rebase_path("//base/android/jni_generator/jni_generator_helper.h",
119 # TODO(cjhopman): make jni_generator.py support generating jni for multiple
120 # .class files from a .jar.
122 foreach(class, invoker.classes) {
123 classname_list = process_file_template(
124 [class], "{{source_name_part}}")
125 classname = classname_list[0]
126 jni_target_name = "${target_name}__jni_${classname}"
127 jni_actions += [ ":$jni_target_name" ]
128 action(jni_target_name) {
129 depfile = "$target_gen_dir/$target_name.d"
130 script = "//base/android/jni_generator/jni_generator.py"
132 jni_generator_include,
137 "${jni_output_dir}/${classname}_jni.h"
141 "--depfile", rebase_path(depfile, root_build_dir),
142 "--jar_file", rebase_path(jar_file, root_build_dir),
143 "--input_file", class,
144 "--optimize_generation=1",
146 "--output_dir", rebase_path(jni_output_dir, root_build_dir),
147 "--includes", rebase_path(jni_generator_include, "//"),
152 config("jni_includes_${target_name}") {
153 include_dirs = [ base_output_dir ]
158 if (defined(invoker.deps)) {
161 if (defined(invoker.forward_dependent_configs_from)) {
162 forward_dependent_configs_from = invoker.forward_dependent_configs_from
164 direct_dependent_configs = [ ":jni_includes_${target_name}" ]
168 # Declare a target for c-preprocessor-generated java files
170 # This target generates java files using the host C pre-processor. Each file in
171 # sources will be compiled using the C pre-processor. If include_path is
172 # specified, it will be passed (with --I) to the pre-processor.
174 # This target will create a single .srcjar. Adding this target to a library
175 # target's srcjar_deps will make the generated java files be included in that
176 # library's final outputs.
179 # sources: list of files to be processed by the C pre-processor. For each
180 # file in sources, there will be one .java file in the final .srcjar. For a
181 # file named FooBar.template, a java file will be created with name
183 # source_prereqs: additional compile-time dependencies. Any files
184 # `#include`-ed in the templates should be listed here.
185 # package_name: this will be the subdirectory for each .java file in the .srcjar.
188 # java_cpp_template("foo_generated_enum") {
190 # "android/java/templates/Foo.template",
193 # "android/java/templates/native_foo_header.h",
196 # package_name = "org/chromium/base/library_loader"
197 # include_path = "android/java/templates"
199 template("java_cpp_template") {
200 assert(defined(invoker.sources))
201 package_name = invoker.package_name + ""
203 if (defined(invoker.include_path)) {
204 include_path = invoker.include_path + ""
209 action_foreach("${target_name}__apply_gcc") {
210 script = "//build/android/gyp/gcc_preprocess.py"
211 if (defined(invoker.source_prereqs)) {
212 source_prereqs = invoker.source_prereqs + []
214 depfile = "${target_gen_dir}/${target_name}.d"
216 sources = invoker.sources
218 gen_dir = "${target_gen_dir}/${package_name}"
219 gcc_template_output_pattern = "${gen_dir}/{{source_name_part}}.java"
223 gcc_template_output_pattern
227 "--depfile", rebase_path(depfile, root_build_dir),
228 "--include-path", rebase_path(include_path, root_build_dir),
229 "--output", rebase_path(gen_dir, root_build_dir) + "/{{source_name_part}}.java",
230 "--template={{source}}",
234 apply_gcc_outputs = get_target_outputs(":${target_name}__apply_gcc")
235 base_gen_dir = get_label_info(":${target_name}__apply_gcc", "target_gen_dir")
237 srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
238 zip("${target_name}__zip_srcjar") {
239 inputs = apply_gcc_outputs
241 base_dir = base_gen_dir
246 ":${target_name}__zip_srcjar"