OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Generates an Android Studio project from a GN target.""" | 6 """Generates an Android Studio project from a GN target.""" |
7 | 7 |
8 import argparse | 8 import argparse |
9 import codecs | 9 import codecs |
10 import logging | 10 import logging |
(...skipping 11 matching lines...) Expand all Loading... | |
22 from pylib import constants | 22 from pylib import constants |
23 from pylib.constants import host_paths | 23 from pylib.constants import host_paths |
24 | 24 |
25 sys.path.append(os.path.join(_BUILD_ANDROID, 'gyp')) | 25 sys.path.append(os.path.join(_BUILD_ANDROID, 'gyp')) |
26 import jinja_template | 26 import jinja_template |
27 from util import build_utils | 27 from util import build_utils |
28 | 28 |
29 | 29 |
30 _DEFAULT_ANDROID_MANIFEST_PATH = os.path.join( | 30 _DEFAULT_ANDROID_MANIFEST_PATH = os.path.join( |
31 host_paths.DIR_SOURCE_ROOT, 'build', 'android', 'AndroidManifest.xml') | 31 host_paths.DIR_SOURCE_ROOT, 'build', 'android', 'AndroidManifest.xml') |
32 _JINJA_TEMPLATE_PATH = os.path.join( | 32 _FILE_DIR = os.path.dirname(__file__) |
33 os.path.dirname(__file__), 'build.gradle.jinja') | |
34 | |
35 _JAVA_SUBDIR = 'symlinked-java' | 33 _JAVA_SUBDIR = 'symlinked-java' |
36 _SRCJARS_SUBDIR = 'extracted-srcjars' | 34 _SRCJARS_SUBDIR = 'extracted-srcjars' |
37 | 35 |
38 _DEFAULT_TARGETS = [ | 36 _DEFAULT_TARGETS = [ |
39 # TODO(agrieve): Requires alternate android.jar to compile. | 37 # TODO(agrieve): Requires alternate android.jar to compile. |
40 # '//android_webview:system_webview_apk', | 38 # '//android_webview:system_webview_apk', |
41 '//android_webview/test:android_webview_apk', | 39 '//android_webview/test:android_webview_apk', |
42 '//android_webview/test:android_webview_test_apk', | 40 '//android_webview/test:android_webview_test_apk', |
43 '//base:base_junit_tests', | 41 '//base:base_junit_tests', |
44 '//chrome/android:chrome_junit_tests', | 42 '//chrome/android:chrome_junit_tests', |
45 '//chrome/android:chrome_public_apk', | 43 '//chrome/android:chrome_public_apk', |
46 '//chrome/android:chrome_public_test_apk', | 44 '//chrome/android:chrome_public_test_apk', |
47 '//chrome/android:chrome_sync_shell_apk', | 45 '//chrome/android:chrome_sync_shell_apk', |
48 '//chrome/android:chrome_sync_shell_test_apk', | 46 '//chrome/android:chrome_sync_shell_test_apk', |
49 '//content/public/android:content_junit_tests', | 47 '//content/public/android:content_junit_tests', |
50 '//content/shell/android:content_shell_apk', | 48 '//content/shell/android:content_shell_apk', |
51 ] | 49 ] |
52 | 50 |
51 def _TemplatePath(name): | |
52 return os.path.join(_FILE_DIR, '{}.jinja'.format(name)) | |
53 | |
agrieve
2017/01/11 19:51:44
nit: 2 blank lines between to-level functions
Peter Wen
2017/01/11 20:00:27
Done.
| |
53 def _RebasePath(path_or_list, new_cwd=None, old_cwd=None): | 54 def _RebasePath(path_or_list, new_cwd=None, old_cwd=None): |
54 """Makes the given path(s) relative to new_cwd, or absolute if not specified. | 55 """Makes the given path(s) relative to new_cwd, or absolute if not specified. |
55 | 56 |
56 If new_cwd is not specified, absolute paths are returned. | 57 If new_cwd is not specified, absolute paths are returned. |
57 If old_cwd is not specified, constants.GetOutDirectory() is assumed. | 58 If old_cwd is not specified, constants.GetOutDirectory() is assumed. |
58 """ | 59 """ |
59 if not isinstance(path_or_list, basestring): | 60 if not isinstance(path_or_list, basestring): |
60 return [_RebasePath(p, new_cwd, old_cwd) for p in path_or_list] | 61 return [_RebasePath(p, new_cwd, old_cwd) for p in path_or_list] |
61 if old_cwd is None: | 62 if old_cwd is None: |
62 old_cwd = constants.GetOutDirectory() | 63 old_cwd = constants.GetOutDirectory() |
(...skipping 18 matching lines...) Expand all Loading... | |
81 with codecs.open(path, 'w', 'utf-8') as output_file: | 82 with codecs.open(path, 'w', 'utf-8') as output_file: |
82 output_file.write(data) | 83 output_file.write(data) |
83 | 84 |
84 | 85 |
85 def _ReadBuildVars(output_dir): | 86 def _ReadBuildVars(output_dir): |
86 with open(os.path.join(output_dir, 'build_vars.txt')) as f: | 87 with open(os.path.join(output_dir, 'build_vars.txt')) as f: |
87 return dict(l.rstrip().split('=', 1) for l in f) | 88 return dict(l.rstrip().split('=', 1) for l in f) |
88 | 89 |
89 | 90 |
90 def _RunNinja(output_dir, args): | 91 def _RunNinja(output_dir, args): |
91 cmd = ['ninja', '-C', output_dir, '-j50'] | 92 cmd = ['ninja', '-C', output_dir, '-j1000'] |
92 cmd.extend(args) | 93 cmd.extend(args) |
93 logging.info('Running: %r', cmd) | 94 logging.info('Running: %r', cmd) |
94 subprocess.check_call(cmd) | 95 subprocess.check_call(cmd) |
95 | 96 |
96 | 97 |
97 def _QueryForAllGnTargets(output_dir): | 98 def _QueryForAllGnTargets(output_dir): |
98 # Query ninja rather than GN since it's faster. | 99 # Query ninja rather than GN since it's faster. |
99 cmd = ['ninja', '-C', output_dir, '-t', 'targets'] | 100 cmd = ['ninja', '-C', output_dir, '-t', 'targets'] |
100 logging.info('Running: %r', cmd) | 101 logging.info('Running: %r', cmd) |
101 ninja_output = build_utils.CheckOutput(cmd) | 102 ninja_output = build_utils.CheckOutput(cmd) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 # things up at all. | 293 # things up at all. |
293 variables['prebuilts'] = relativize(gradle['dependent_prebuilt_jars']) | 294 variables['prebuilts'] = relativize(gradle['dependent_prebuilt_jars']) |
294 deps = [_ProjectEntry.FromBuildConfigPath(p) | 295 deps = [_ProjectEntry.FromBuildConfigPath(p) |
295 for p in gradle['dependent_android_projects']] | 296 for p in gradle['dependent_android_projects']] |
296 | 297 |
297 variables['android_project_deps'] = [d.ProjectName() for d in deps] | 298 variables['android_project_deps'] = [d.ProjectName() for d in deps] |
298 deps = [_ProjectEntry.FromBuildConfigPath(p) | 299 deps = [_ProjectEntry.FromBuildConfigPath(p) |
299 for p in gradle['dependent_java_projects']] | 300 for p in gradle['dependent_java_projects']] |
300 variables['java_project_deps'] = [d.ProjectName() for d in deps] | 301 variables['java_project_deps'] = [d.ProjectName() for d in deps] |
301 | 302 |
302 return jinja_processor.Render(_JINJA_TEMPLATE_PATH, variables) | 303 return jinja_processor.Render( |
304 _TemplatePath(target_type.split('_')[0]), variables) | |
303 | 305 |
304 | 306 |
305 def _GenerateRootGradle(jinja_processor): | 307 def _GenerateRootGradle(jinja_processor): |
306 """Returns the data for the root project's build.gradle.""" | 308 """Returns the data for the root project's build.gradle.""" |
307 variables = {'template_type': 'root'} | 309 return jinja_processor.Render(_TemplatePath('root')) |
308 return jinja_processor.Render(_JINJA_TEMPLATE_PATH, variables) | |
309 | 310 |
310 | 311 |
311 def _GenerateSettingsGradle(project_entries): | 312 def _GenerateSettingsGradle(project_entries): |
312 """Returns the data for settings.gradle.""" | 313 """Returns the data for settings.gradle.""" |
313 project_name = os.path.basename(os.path.dirname(host_paths.DIR_SOURCE_ROOT)) | 314 project_name = os.path.basename(os.path.dirname(host_paths.DIR_SOURCE_ROOT)) |
314 lines = [] | 315 lines = [] |
315 lines.append('// Generated by //build/android/gradle/generate_gradle.py') | 316 lines.append('// Generated by //build/android/gradle/generate_gradle.py') |
316 lines.append('rootProject.name = "%s"' % project_name) | 317 lines.append('rootProject.name = "%s"' % project_name) |
317 lines.append('rootProject.projectDir = settingsDir') | 318 lines.append('rootProject.projectDir = settingsDir') |
318 lines.append('') | 319 lines.append('') |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 | 411 |
411 # There are many unused libraries, so restrict to those that are actually used | 412 # There are many unused libraries, so restrict to those that are actually used |
412 # when using --all. | 413 # when using --all. |
413 if args.all: | 414 if args.all: |
414 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] | 415 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] |
415 | 416 |
416 all_entries = _FindAllProjectEntries(main_entries) | 417 all_entries = _FindAllProjectEntries(main_entries) |
417 logging.info('Found %d dependent build_config targets.', len(all_entries)) | 418 logging.info('Found %d dependent build_config targets.', len(all_entries)) |
418 | 419 |
419 logging.warning('Writing .gradle files...') | 420 logging.warning('Writing .gradle files...') |
420 jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) | 421 jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) |
421 build_vars = _ReadBuildVars(output_dir) | 422 build_vars = _ReadBuildVars(output_dir) |
422 project_entries = [] | 423 project_entries = [] |
423 srcjar_tuples = [] | 424 srcjar_tuples = [] |
424 generated_inputs = [] | 425 generated_inputs = [] |
425 for entry in all_entries: | 426 for entry in all_entries: |
426 if entry.GetType() not in ('android_apk', 'java_library', 'java_binary'): | 427 if entry.GetType() not in ('android_apk', 'java_library', 'java_binary'): |
427 continue | 428 continue |
428 | 429 |
429 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) | 430 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) |
430 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) | 431 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) | 478 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) |
478 | 479 |
479 logging.warning('Project created! (%d subprojects)', len(project_entries)) | 480 logging.warning('Project created! (%d subprojects)', len(project_entries)) |
480 logging.warning('Generated projects work best with Android Studio 2.2') | 481 logging.warning('Generated projects work best with Android Studio 2.2') |
481 logging.warning('For more tips: https://chromium.googlesource.com/chromium' | 482 logging.warning('For more tips: https://chromium.googlesource.com/chromium' |
482 '/src.git/+/master/docs/android_studio.md') | 483 '/src.git/+/master/docs/android_studio.md') |
483 | 484 |
484 | 485 |
485 if __name__ == '__main__': | 486 if __name__ == '__main__': |
486 main() | 487 main() |
OLD | NEW |