Add dexing for libraries and apks
[chromium-blink-merge.git] / build / config / android / internal_rules.gni
blob002320c5305946ba0f0ab94528a4accaba543dbe
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("//build/config/android/config.gni")
7 assert(is_android)
10 rebased_android_sdk = rebase_path(android_sdk, root_build_dir)
11 rebased_android_sdk_root = rebase_path(android_sdk_root, root_build_dir)
12 rebased_android_sdk_build_tools = rebase_path(android_sdk_build_tools, root_build_dir)
15 # Write the target's .build_config file. This is a json file that contains a
16 # dictionary of information about how to build this target (things that
17 # require knowledge about this target's dependencies and cannot be calculated
18 # at gn-time). There is a special syntax to add a value in that dictionary to
19 # an action/action_foreachs args:
20 #   --python-arg=@FileArg($rebased_build_config_path:key0:key1)
21 # At runtime, such an arg will be replaced by the value in the build_config.
22 # See build/android/gyp/write_build_config.py and
23 # build/android/gyp/util/build_utils.py:ExpandFileArgs
24 template("write_build_config") {
25   assert(defined(invoker.type))
26   assert(defined(invoker.build_config))
28   type = invoker.type
29   build_config = invoker.build_config
31   assert(type == "android_apk" || type == "android_library" || type == "android_resources")
33   action(target_name) {
34     script = "//build/android/gyp/write_build_config.py"
35     depfile = "$target_gen_dir/$target_name.d"
37     deps = []
38     if (defined(invoker.deps)) {
39       deps += invoker.deps
40     }
42     outputs = [
43       depfile,
44       build_config
45     ]
47     possible_deps_configs = []
48     foreach(d, deps) {
49       dep_gen_dir = get_label_info(d, "target_gen_dir")
50       dep_name = get_label_info(d, "name")
51       possible_deps_configs += [ "$dep_gen_dir/$dep_name.build_config" ]
52     }
53     rebase_possible_deps_configs = rebase_path(possible_deps_configs)
55     args = [
56       "--type", type,
57       "--depfile", rebase_path(depfile, root_build_dir),
58       "--possible-deps-configs=$rebase_possible_deps_configs",
59       "--build-config", rebase_path(build_config, root_build_dir),
60     ]
62     if (type == "android_library" || type == "android_apk") {
63       args += [
64         "--jar-path", rebase_path(invoker.jar_path, root_build_dir),
65         "--dex-path", rebase_path(invoker.dex_path, root_build_dir),
66       ]
67     }
69     if (type == "android_resources" || type == "android_apk") {
70       assert(defined(invoker.resources_zip))
71       args += [
72         "--resources-zip", rebase_path(invoker.resources_zip, root_build_dir),
73       ]
74     }
76     if (defined(invoker.srcjar)) {
77       args += [
78         "--srcjar", rebase_path(invoker.srcjar, root_build_dir)
79       ]
80     }
81   }
85 # Creates a zip archive of the inputs.
86 # If base_dir is provided, the archive paths will be relative to it.
87 template("zip") {
88   assert(defined(invoker.inputs))
89   assert(defined(invoker.output))
91   rebase_inputs = rebase_path(invoker.inputs, root_build_dir)
92   rebase_output = rebase_path(invoker.output, root_build_dir)
93   action(target_name) {
94     script = "//build/android/gn/zip.py"
95     depfile = "$target_gen_dir/$target_name.d"
96     inputs = invoker.inputs
97     outputs = [
98       depfile,
99       invoker.output
100     ]
101     args = [
102       "--depfile", rebase_path(depfile, root_build_dir),
103       "--inputs=$rebase_inputs",
104       "--output=$rebase_output",
105     ]
106     if (defined(invoker.base_dir)) {
107       args += [
108         "--base-dir", rebase_path(invoker.base_dir, root_build_dir)
109       ]
110     }
111   }
114 template("dex") {
115   assert(defined(invoker.sources))
116   assert(defined(invoker.output))
117   action(target_name) {
118     script = "//build/android/gyp/dex.py"
119     depfile = "$target_gen_dir/$target_name.d"
120     sources = invoker.sources
121     outputs = [depfile, invoker.output]
122     if (defined(invoker.inputs)) {
123       inputs = invoker.inputs
124     }
126     rebased_output = rebase_path(invoker.output, root_build_dir)
128     args = [
129       "--depfile", rebase_path(depfile, root_build_dir),
130       "--android-sdk-tools", rebased_android_sdk_build_tools,
131       "--dex-path", rebased_output,
132     ]
134     if (defined(invoker.no_locals) && invoker.no_locals) {
135       args += [
136         "--no-locals=1"
137       ]
138     }
140     if (defined(invoker.args)) {
141       args += invoker.args
142     }
144     args += rebase_path(invoker.sources, root_build_dir)
145   }
148 # Compiles and jars a set of java files.
150 # Outputs:
151 #  $jar_path.jar
152 #  $jar_path.jar.TOC
154 # Variables
155 #   java_files: List of .java files to compile.
156 #   java_deps: List of java dependencies. These should all have a .jar output
157 #     at "${target_gen_dir}/${target_name}.jar.
158 #   chromium_code: If 1, enable extra warnings.
159 #   srcjar_deps: List of srcjar dependencies. The .java files contained in the
160 #     dependencies srcjar outputs will be compiled and added to the output jar.
161 #   jar_path: Use this to explicitly set the output jar path. Defaults to
162 #     "${target_gen_dir}/${target_name}.jar.
163 template("java_library") {
164   assert(defined(invoker.java_files))
165   assert(defined(invoker.build_config))
166   assert(defined(invoker.jar_path))
168   java_files = invoker.java_files
169   jar_path = invoker.jar_path
170   jar_toc_path = jar_path + ".TOC"
172   build_config = invoker.build_config
174   jar_excluded_patterns = []
175   if (defined(invoker.jar_excluded_patterns)) {
176     jar_excluded_patterns += invoker.jar_excluded_patterns
177   }
179   chromium_code = false
180   if (defined(invoker.chromium_code)) {
181     chromium_code = chromium_code || invoker.chromium_code
182   }
184   srcjar_deps = []
185   if (defined(invoker.srcjar_deps)) {
186     srcjar_deps += invoker.srcjar_deps
187   }
189   java_srcjars = []
190   foreach(dep, srcjar_deps) {
191     dep_gen_dir = get_label_info(dep, "target_gen_dir")
192     dep_name = get_label_info(dep, "name")
193     java_srcjars += [ "$dep_gen_dir/$dep_name.srcjar" ]
194   }
195   # Mark srcjar_deps as used.
196   assert(srcjar_deps == [] || srcjar_deps != [])
198   rebase_jar_path = rebase_path(jar_path, root_build_dir)
200   system_jars = [ "${android_sdk}/android.jar" ]
201   action("${target_name}__javac") {
202     script = "//build/android/gyp/javac.py"
203     depfile = "$target_gen_dir/$target_name.d"
204     outputs = [
205       depfile,
206       jar_path,
207       jar_path + ".md5.stamp"
208     ]
209     sources = java_files + java_srcjars
210     inputs = system_jars + [ build_config ]
212     rebase_system_jars = rebase_path(system_jars, root_build_dir)
213     rebase_java_srcjars = rebase_path(java_srcjars, root_build_dir)
214     rebase_build_config = rebase_path(build_config, root_build_dir)
215     rebase_depfile = rebase_path(depfile, root_build_dir)
216     args = [
217       "--depfile=$rebase_depfile",
218       "--classpath=$rebase_system_jars",
219       "--classpath=@FileArg($rebase_build_config:javac:classpath)",
220       "--jar-path=$rebase_jar_path",
221       "--java-srcjars=$rebase_java_srcjars",
222       "--java-srcjars=@FileArg($rebase_build_config:javac:srcjars)",
223       "--jar-excluded-classes=$jar_excluded_patterns",
224     ]
225     if (chromium_code) {
226       args += [ "--chromium-code" ]
227     }
229     args += rebase_path(java_files, root_build_dir)
230   }
232   # TODO(cjhopman): proguard
234   rebase_jar_toc_path = rebase_path(jar_toc_path, root_build_dir)
235   action("${target_name}__jar_toc") {
236     script = "//build/android/gyp/jar_toc.py"
237     depfile = "$target_gen_dir/$target_name.d"
238     outputs = [
239       depfile,
240       jar_toc_path,
241       jar_toc_path + ".md5.stamp"
242     ]
243     inputs = [ jar_path ]
244     args = [
245       "--depfile", rebase_path(depfile, root_build_dir),
246       "--jar-path=${rebase_jar_path}",
247       "--toc-path=${rebase_jar_toc_path}",
248     ]
249   }
251   group(target_name) {
252     deps = [
253       ":${target_name}__javac",
254       ":${target_name}__jar_toc",
255     ]
256   }
260 # This adds Android-specific parts to the java_library template.
262 # Runs Android lint against the compiled java files.
263 # Dexes the output jar for inclusion in an APK.
264 template("android_java_library") {
265   assert(defined(invoker.java_files))
266   assert(defined(invoker.build_config))
267   assert(defined(invoker.jar_path))
268   assert(defined(invoker.dex_path))
270   _jar_path = invoker.jar_path
271   _dex_path = invoker.dex_path
273   java_library("${target_name}__java_library") {
274     jar_path = _jar_path
275     if (defined(invoker.jar_excluded_patterns)) {
276       jar_excluded_patterns = invoker.jar_excluded_patterns
277     }
278     build_config = invoker.build_config
279     java_files = invoker.java_files
281     if (defined(invoker.srcjar_deps)) {
282       srcjar_deps = invoker.srcjar_deps
283     }
284   }
286   # TODO(cjhopman): lint
289   dex("${target_name}__dex") {
290     sources = [_jar_path]
291     output = _dex_path
292   }
294   group(target_name) {
295     deps = [
296       ":${target_name}__java_library",
297       ":${target_name}__dex",
298     ]
299   }
303 # Runs process_resources.py
304 template("process_resources") {
305   zip_path = invoker.zip_path
306   srcjar_path = invoker.srcjar_path
307   build_config = invoker.build_config
308   resource_dirs = invoker.resource_dirs
309   android_manifest = invoker.android_manifest
311   non_constant_id = true
312   if (defined(invoker.generate_constant_ids) && invoker.generate_constant_ids) {
313     non_constant_id = false
314   }
316   action(target_name) {
317     script = "//build/android/gyp/process_resources.py"
319     depfile = "$target_gen_dir/$target_name.d"
320     outputs = [
321       depfile,
322       zip_path,
323       srcjar_path,
324     ]
326     sources_build_rel = exec_script(
327         "//build/android/gyp/find.py",
328         rebase_path(resource_dirs, root_build_dir),
329         "list lines"
330         )
331     sources = rebase_path(sources_build_rel, ".", root_build_dir)
333     source_prereqs = [
334       build_config,
335       android_manifest,
336     ]
338     rebase_resource_dirs = rebase_path(resource_dirs, root_build_dir)
339     rebase_build_config = rebase_path(build_config, root_build_dir)
340     args = [
341       "--depfile", rebase_path(depfile, root_build_dir),
342       "--android-sdk", rebase_path(android_sdk, root_build_dir),
343       "--android-sdk-tools", rebase_path(android_sdk_build_tools, root_build_dir),
344       "--android-manifest", rebase_path(android_manifest, root_build_dir),
346       "--resource-dirs=$rebase_resource_dirs",
347       "--srcjar-out", rebase_path(srcjar_path, root_build_dir),
348       "--resource-zip-out", rebase_path(zip_path, root_build_dir),
350       "--dependencies-res-zips=@FileArg($rebase_build_config:resources:dependency_zips)",
351     ]
353     if (non_constant_id) {
354       args += [ "--non-constant-id" ]
355     }
357     if (defined(invoker.custom_package)) {
358       args += [
359         "--custom-package", invoker.custom_package,
360       ]
361     }
363     if (defined(invoker.v14_verify_only) && invoker.v14_verify_only) {
364       args += ["--v14-verify-only"]
365     }
367     if (defined(invoker.all_resources_zip_path)) {
368       all_resources_zip = invoker.all_resources_zip_path
369       outputs += [ all_resources_zip ]
370       args += [
371         "--all-resources-zip-out", rebase_path(all_resources_zip, root_build_dir)
372       ]
373     }
375     if (defined(invoker.args)) {
376       args += invoker.args
377     }
378   }