summaryrefslogtreecommitdiffstats
path: root/build/config/android/rules.gni
blob: f18c86a4bc6e85289ba53c96bcad012ea5f0bdd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/android/config.gni")
import("//build/config/android/internal_rules.gni")
import("//tools/grit/grit_rule.gni")

assert(is_android)


# Declare a jni target
#
# This target generates the native jni bindings for a set of .java files.
#
# See base/android/jni_generator/jni_generator.py for more info about the
# format of generating JNI bindings.
#
# Variables
#   sources: list of .java files to generate jni for
#   jni_package: subdirectory path for generated bindings
#
# Example
#   generate_jni("foo_jni") {
#     sources = [
#       "android/java/src/org/chromium/foo/Foo.java",
#       "android/java/src/org/chromium/foo/FooUtil.java",
#     ]
#     jni_package = "foo"
#   }
template("generate_jni") {
  assert(defined(invoker.sources))
  assert(defined(invoker.jni_package))
  jni_package = invoker.jni_package
  base_output_dir = "${root_gen_dir}/${target_name}/${jni_package}"
  jni_output_dir = "${base_output_dir}/jni"

  jni_generator_include = "//base/android/jni_generator/jni_generator_helper.h"

  foreach_target_name = "${target_name}__jni_gen"
  action_foreach(foreach_target_name) {
    script = "//base/android/jni_generator/jni_generator.py"
    depfile = "$target_gen_dir/$target_name.{{source_name_part}}.d"
    sources = invoker.sources
    inputs = [ jni_generator_include ]
    outputs = [
      depfile,
      "${jni_output_dir}/{{source_name_part}}_jni.h"
    ]

    args = [
      "--depfile", rebase_path(depfile, root_build_dir),
      "--input_file={{source}}",
      "--optimize_generation=1",
      "--ptr_type=long",
      "--output_dir", rebase_path(jni_output_dir, root_build_dir),
      "--includes", rebase_path(jni_generator_include, "//"),
    ]
    if (defined(invoker.jni_generator_jarjar_file)) {
      args += [
        "--jarjar", rebase_path(jni_generator_jarjar_file, root_build_dir),
      ]
    }
  }

  config("jni_includes_${target_name}") {
    include_dirs = [ base_output_dir ]
  }

  group(target_name) {
    deps = [ ":$foreach_target_name" ]
    direct_dependent_configs = [ ":jni_includes_${target_name}" ]

    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (defined(invoker.forward_dependent_configs_from)) {
      forward_dependent_configs_from = invoker.forward_dependent_configs_from
    }
  }
}


# Declare a jni target for a prebuilt jar
#
# This target generates the native jni bindings for a set of classes in a .jar.
#
# See base/android/jni_generator/jni_generator.py for more info about the
# format of generating JNI bindings.
#
# Variables
#   classes: list of .class files in the jar to generate jni for. These should
#     include the full path to the .class file.
#   jni_package: subdirectory path for generated bindings
#   jar_file: the path to the .jar. If not provided, will default to the sdk's
#     android.jar
#
#   deps, forward_dependent_configs_from: As normal
#
# Example
#   generate_jar_jni("foo_jni") {
#     classes = [
#       "android/view/Foo.class",
#     ]
#     jni_package = "foo"
#   }
template("generate_jar_jni") {
  assert(defined(invoker.classes))
  assert(defined(invoker.jni_package))

  if (defined(invoker.jar_file)) {
    jar_file = invoker.jar_file
  } else {
    jar_file = android_sdk_jar
  }

  jni_package = invoker.jni_package
  base_output_dir = "${root_gen_dir}/${target_name}/${jni_package}"
  jni_output_dir = "${base_output_dir}/jni"

  jni_generator_include =
      rebase_path("//base/android/jni_generator/jni_generator_helper.h",
          root_build_dir)

  # TODO(cjhopman): make jni_generator.py support generating jni for multiple
  # .class files from a .jar.
  jni_actions = []
  foreach(class, invoker.classes) {
    classname_list = process_file_template(
        [class], "{{source_name_part}}")
    classname = classname_list[0]
    jni_target_name = "${target_name}__jni_${classname}"
    jni_actions += [ ":$jni_target_name" ]
    action(jni_target_name) {
      depfile = "$target_gen_dir/$target_name.d"
      script = "//base/android/jni_generator/jni_generator.py"
      sources = [
        jni_generator_include,
        jar_file,
      ]
      outputs = [
        depfile,
        "${jni_output_dir}/${classname}_jni.h"
      ]

      args = [
        "--depfile", rebase_path(depfile, root_build_dir),
        "--jar_file", rebase_path(jar_file, root_build_dir),
        "--input_file", class,
        "--optimize_generation=1",
        "--ptr_type=long",
        "--output_dir", rebase_path(jni_output_dir, root_build_dir),
        "--includes", rebase_path(jni_generator_include, "//"),
      ]
    }
  }

  config("jni_includes_${target_name}") {
    include_dirs = [ base_output_dir ]
  }

  group(target_name) {
    deps = jni_actions
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    if (defined(invoker.forward_dependent_configs_from)) {
      forward_dependent_configs_from = invoker.forward_dependent_configs_from
    }
    direct_dependent_configs = [ ":jni_includes_${target_name}" ]
  }
}


# Declare a target for c-preprocessor-generated java files
#
# This target generates java files using the host C pre-processor. Each file in
# sources will be compiled using the C pre-processor. If include_path is
# specified, it will be passed (with --I) to the pre-processor.
#
# This target will create a single .srcjar. Adding this target to an
# android_library target's srcjar_deps will make the generated java files be
# included in that library's final outputs.
#
# Variables
#   sources: list of files to be processed by the C pre-processor. For each
#     file in sources, there will be one .java file in the final .srcjar. For a
#     file named FooBar.template, a java file will be created with name
#     FooBar.java.
#   inputs: additional compile-time dependencies. Any files
#     `#include`-ed in the templates should be listed here.
#   package_name: this will be the subdirectory for each .java file in the
#     .srcjar.
#
# Example
#   java_cpp_template("foo_generated_enum") {
#     sources = [
#       "android/java/templates/Foo.template",
#     ]
#     inputs = [
#       "android/java/templates/native_foo_header.h",
#     ]
#
#     package_name = "org/chromium/base/library_loader"
#     include_path = "android/java/templates"
#   }
template("java_cpp_template") {
  assert(defined(invoker.sources))
  package_name = invoker.package_name + ""

  if (defined(invoker.include_path)) {
    include_path = invoker.include_path + ""
  } else {
    include_path = "//"
  }

  action_foreach("${target_name}__apply_gcc") {
    script = "//build/android/gyp/gcc_preprocess.py"
    if (defined(invoker.inputs)) {
      inputs = invoker.inputs + []
    }
    depfile = "${target_gen_dir}/${target_name}.d"

    sources = invoker.sources

    gen_dir = "${target_gen_dir}/${target_name}/java_cpp_template/${package_name}"
    gcc_template_output_pattern = "${gen_dir}/{{source_name_part}}.java"

    outputs = [
      depfile,
      gcc_template_output_pattern
    ]

    args = [
      "--depfile", rebase_path(depfile, root_build_dir),
      "--include-path", rebase_path(include_path, root_build_dir),
      "--output", rebase_path(gen_dir, root_build_dir) + "/{{source_name_part}}.java",
      "--template={{source}}",
    ]
  }

  apply_gcc_outputs = get_target_outputs(":${target_name}__apply_gcc")
  base_gen_dir = get_label_info(":${target_name}__apply_gcc", "target_gen_dir")

  srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
  zip("${target_name}__zip_srcjar") {
    inputs = apply_gcc_outputs
    output = srcjar_path
    base_dir = base_gen_dir
  }

  group(target_name) {
    deps = [
      ":${target_name}__zip_srcjar"
    ]
  }
}


# Declare an Android resources target
#
# This creates a resources zip file that will be used when building an Android
# library or apk and included into a final apk.
#
# To include these resources in a library/apk, this target should be listed in
# the library's deps. A library/apk will also include any resources used by its
# own dependencies.
#
# Variables
#   deps: Specifies the dependencies of this target. Any Android resources
#     listed in deps will be included by libraries/apks that depend on this
#     target.
#   resource_dirs: List of directories containing resources for this target.
#   android_manifest: AndroidManifest.xml for this target. Defaults to
#     //build/android/AndroidManifest.xml.
#   custom_package: java package for generated .java files.
#   v14_verify_only: If true, don't generate v14/v17 resources and just verify
#     that the resources are v14-compliant (see
#     build/android/gyp/generate_v14_compatible_resources.py). Defaults to
#     false.
#
# Example
#   android_resources("foo_resources") {
#     deps = [":foo_strings_grd"]
#     resource_dirs = ["res"]
#     custom_package = "org.chromium.foo"
#   }
template("android_resources") {
  assert(defined(invoker.resource_dirs))

  base_path = "$target_gen_dir/$target_name"
  zip_path = base_path + ".resources.zip"
  srcjar_path = base_path + ".srcjar"
  build_config = base_path + ".build_config"

  write_build_config("${target_name}__build_config") {
    type = "android_resources"
    resources_zip = zip_path
    srcjar = srcjar_path
    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
  }

  android_manifest = "//build/android/AndroidManifest.xml"
  if (defined(invoker.android_manifest)) {
    android_manifest = invoker.android_manifest
  }

  process_resources("${target_name}__process_resources") {
    resource_dirs = invoker.resource_dirs
    if (defined(invoker.custom_package)) {
      custom_package = invoker.custom_package
    }

    if (defined(invoker.v14_verify_only)) {
      v14_verify_only = invoker.v14_verify_only
    }
  }

  group(target_name) {
    deps = [
      ":${target_name}__build_config",
      ":${target_name}__process_resources",
    ]
  }
}


# Declare a target that generates localized strings.xml from a .grd file.
#
# If this target is included in the deps of an android resources/library/apk,
# the strings.xml will be included with that target.
#
# Variables
#   deps: Specifies the dependencies of this target.
#   grd_file: Path to the .grd file to generate strings.xml from.
#   outputs: Expected grit outputs (see grit rule).
#
# Example
#  java_strings_grd("foo_strings_grd") {
#    grd_file = "foo_strings.grd"
#  }
template("java_strings_grd") {
  base_path = "$target_gen_dir/$target_name"
  resources_zip = base_path + ".resources.zip"

  write_build_config("${target_name}__build_config") {
    type = "android_resources"
    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
  }

  # Put grit files into this subdirectory of target_gen_dir.
  extra_output_path = target_name + "_grit_output"

  grit_target_name = "${target_name}__grit"
  grit_output_dir = "$target_gen_dir/$extra_output_path"
  grit(grit_target_name) {
    grit_flags = [
      "-E", "ANDROID_JAVA_TAGGED_ONLY=false",
    ]
    output_dir = grit_output_dir
    resource_ids = ""
    source = invoker.grd_file
    # The grit rule expects files relative to target_gen_dir, but this rule
    # appends a suffix to that. This rebase will prepend extra_output_path to
    # each filename.
    outputs = rebase_path(invoker.outputs, ".", extra_output_path)
  }

  # This needs to get outputs from grit's internal target, not the final
  # source_set.
  generate_strings_outputs = get_target_outputs(":${grit_target_name}_grit")

  zip("${target_name}__zip") {
    base_dir = grit_output_dir
    inputs = generate_strings_outputs
    output = resources_zip
  }

  group(target_name) {
    deps = [
      ":${target_name}__build_config",
      ":${target_name}__zip",
    ]
  }
}


# Declare an Android library target
#
# This target creates an Android library containing java code and Android
# resources.
#
# Variables
#   deps: Specifies the dependencies of this target. Java targets in this list
#     will be added to the javac classpath. Android resources in dependencies
#     will be used when building this library.
#   java_files: List of .java files included in this library.
#   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
#     will be added to java_files and be included in this library.
#
#   jar_excluded_patterns: List of patterns of .class files to exclude from the
#     final jar.
#
# Example
#   android_library("foo_java") {
#     java_files = [
#       "android/org/chromium/foo/Foo.java",
#       "android/org/chromium/foo/FooInterface.java",
#       "android/org/chromium/foo/FooService.java",
#     ]
#     deps = [
#       ":bar_java"
#     ]
#     srcjar_deps = [
#       ":foo_generated_enum"
#     ]
#     jar_excluded_patterns = [
#       "*/FooService.class", "*/FooService##*.class"
#     ]
#   }
template("android_library") {
  assert(defined(invoker.java_files))

  base_path = "$target_gen_dir/$target_name"
  build_config = base_path + ".build_config"

  write_build_config("${target_name}__build_config") {
    type = "android_library"

    deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    # base_path
  }

  jar_path = base_path + ".jar"
  android_java_library(target_name) {
    java_files = invoker.java_files
    build_config = build_config

    if (defined(invoker.jar_excluded_patterns)) {
      jar_excluded_patterns = invoker.jar_excluded_patterns
    }

    if (defined(invoker.srcjar_deps)) {
      srcjar_deps = invoker.srcjar_deps
    }
  }
}