blob: 29764eec2e54fe1dab3a2b4ea73380ae65933881 [file] [log] [blame] [view]
David 'Digit' Turner40560ef72018-03-07 09:44:281# Introduction
2
3This document describes the `.build_config` files that are used by the
4Chromium build system for Android-specific targets like APK, resources,
5and more.
6
7[TOC]
8
9# I. Overview of .build_config files:
10
11The Android build requires performing computations about dependencies in
12various targets, which are not possible with the GN build language. To address
13this, `.build_config` files are written during the build to store the needed
14per-target information as JSON files.
15
16They are always written to `$target_gen_dir/${target_name}.build_config`.
17
18Many scripts under [`build/android/gyp/`](build/android_gyp/), which are used
19during the build, can also accept parameter arguments using
20`@FileArg references`, which look like:
21
22 --some-param=@FileArg(<filename>:<key1>:<key2>:..<keyN>)
23
24This placeholder will ensure that `<filename>` is read as a JSON file, then
25return the value at `[key1][key2]...[keyN]` for the `--some-param` option.
26
27Apart from that, the scripts do not need to know anything about the structure
28of `.build_config` files (but the GN rules that invoke them do and select
29which `@FileArg()` references to use).
30
31For a concrete example, consider the following GN fragment:
32
33```gn
34# From //ui/android/BUILD.gn:
35android_resources("ui_java_resources") {
36 custom_package = "org.chromium.ui"
37 resource_dirs = [ "java/res" ]
38 deps = [
39 ":ui_strings_grd",
40 ]
41}
42```
43
44This will end up generating the following JSON file under
45`$CHROMIUM_OUTPUT_DIR/gen/ui/android/ui_java_resources.build_config`:
46
47```json
48{
49 "deps_info": {
50 "deps_configs": [
51 "gen/ui/android/ui_strings_grd.build_config"
52 ],
53 "name": "ui_java_resources.build_config",
54 "package_name": "org.chromium.ui",
55 "path": "gen/ui/android/ui_java_resources.build_config",
56 "r_text": "gen/ui/android/ui_java_resources_R.txt",
57 "resources_dirs": [
58 "../../ui/android/java/res"
59 ],
60 "resources_zip": "resource_zips/ui/android/ui_java_resources.resources.zip",
61 "srcjar": "gen/ui/android/ui_java_resources.srcjar",
62 "type": "android_resources"
63 },
64 "gradle": {},
65 "resources": {
66 "dependency_zips": [
67 "resource_zips/ui/android/ui_strings_grd.resources.zip"
68 ],
69 "extra_package_names": [],
70 "extra_r_text_files": []
71 }
72}
73```
74
75NOTE: All path values in `.build_config` files are relative to your
76`$CHROMIUM_OUTPUT_DIR`.
77
78# II. Generation of .build_config files:
79
80They are generated by the GN [`write_build_config()`](gn_write_build_config)
81internal template, which ends up invoking
82[`write_build_config.py`](write_build_config_py). For our example above, this
83is with the following parameters:
84
85```
86python ../../build/android/gyp/write_build_config.py \
87 --type=android_resources \
88 --depfile gen/ui/android/ui_java_resources__build_config.d \
89 --deps-configs=\[\"gen/ui/android/ui_strings_grd.build_config\"\] \
90 --build-config gen/ui/android/ui_java_resources.build_config \
91 --resources-zip resource_zips/ui/android/ui_java_resources.resources.zip \
92 --package-name org.chromium.ui \
93 --r-text gen/ui/android/ui_java_resources_R.txt \
94 --resource-dirs=\[\"../../ui/android/java/res\"\] \
95 --srcjar gen/ui/android/ui_java_resources.srcjar
96```
97
98Note that *most* of the content of the JSON file comes from command-line
99parameters, but not all of it.
100
101In particular, the `resources['dependency_zips']` entry was computed by
102inspecting the content of all dependencies (here, only
103`ui_string_grd.build_config`), and collecting their
104`deps_configs['resources_zip']` values.
105
106Because a target's `.build_config` file will always be generated after
107that of all of its dependencies,
108[`write_build_config.py`](write_build_config_py) can traverse the
109whole (transitive) set of direct *and* indirect dependencies for a given target
110and extract useful information out of it.
111
112This is the kind of processing that cannot be done at the GN language level,
113and is very powerful for Android builds.
114
115
116# III. Usage of .build_config files:
117
118In addition to being parsed by `write_build_config.py`, when they are listed
119in the `--deps-configs` of a given target, the `.build_config` files are used
120by other scripts under [build/android/gyp/] to build stuff.
121
122For example, the GN `android_resources` template uses it to invoke the
123[`process_resources.py`] script with the following command, in order to
124generate various related files (e.g. `ui_java_resources_R.txt`):
125
126```sh
127python ../../build/android/gyp/process_resources.py \
128 --depfile gen/ui/android/ui_java_resources_1.d \
129 --android-sdk-jar ../../third_party/android_tools/sdk/platforms/android-27/android.jar \
130 --aapt-path ../../third_party/android_tools/sdk/build-tools/27.0.3/aapt \
131 --dependencies-res-zips=@FileArg\(gen/ui/android/ui_java_resources.build_config:resources:dependency_zips\) \
132 --extra-res-packages=@FileArg\(gen/ui/android/ui_java_resources.build_config:resources:extra_package_names\) \
133 --extra-r-text-files=@FileArg\(gen/ui/android/ui_java_resources.build_config:resources:extra_r_text_files\) \
134 --resource-dirs=\[\"../../ui/android/java/res\"\] \
135 --debuggable \
136 --resource-zip-out resource_zips/ui/android/ui_java_resources.resources.zip \
137 --r-text-out gen/ui/android/ui_java_resources_R.txt \
138 --srcjar-out gen/ui/android/ui_java_resources.srcjar \
139 --non-constant-id \
140 --custom-package org.chromium.ui \
141 --shared-resources
142```
143
144Note the use of `@FileArg()` references here, to tell the script where to find
145the information it needs.
146
147
148# IV. Format of .build_config files:
149
150Thanks to `@FileArg()` references, Python build scripts under
151[`build/android/gyp/`](build/android/gyp/) do not need to know anything
152about the internal format of `.build_config` files.
153
154This format is decided between internal GN build rules and
155[`write_build_config.py`][write_build_config_py]. Since these changes rather
156often, the format documentation is kept inside the Python script itself, but
157can be extracted as a Markdown file and visualized with the following commands:
158
159```sh
160# Extract .build_config format documentation
161build/android/gyp/write_build_config.py \
162 --generate-markdown-format-doc > /tmp/format.md
163
164# Launch a browser to visualize the format documentation.
165python tools/md_browser/md_browser.py -d /tmp /tmp/format.md
166```
167
168[build/android/gyp/]: https://chromium.googlesource.com/chromium/src/build/+/master/android/gyp/
169[gn_write_build_config]: https://cs.chromium.org/chromium/src/build/config/android/internal_rules.gni?q=write_build_config&sq=package:chromium
170[write_build_config_py]: https://chromium.googlesource.com/chromium/src/build/+/master/android/gyp/write_build_config.py