| 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 _JNI_LIBS_SUBDIR = 'symlinked-libs' | 35 _JNI_LIBS_SUBDIR = 'symlinked-libs' |
| 38 _ARMEABI_SUBDIR = 'armeabi' | 36 _ARMEABI_SUBDIR = 'armeabi' |
| 39 | 37 |
| 40 _DEFAULT_TARGETS = [ | 38 _DEFAULT_TARGETS = [ |
| 41 # TODO(agrieve): Requires alternate android.jar to compile. | 39 # TODO(agrieve): Requires alternate android.jar to compile. |
| 42 # '//android_webview:system_webview_apk', | 40 # '//android_webview:system_webview_apk', |
| 43 '//android_webview/test:android_webview_apk', | 41 '//android_webview/test:android_webview_apk', |
| 44 '//android_webview/test:android_webview_test_apk', | 42 '//android_webview/test:android_webview_test_apk', |
| 45 '//base:base_junit_tests', | 43 '//base:base_junit_tests', |
| 46 '//chrome/android:chrome_junit_tests', | 44 '//chrome/android:chrome_junit_tests', |
| 47 '//chrome/android:chrome_public_apk', | 45 '//chrome/android:chrome_public_apk', |
| 48 '//chrome/android:chrome_public_test_apk', | 46 '//chrome/android:chrome_public_test_apk', |
| 49 '//chrome/android:chrome_sync_shell_apk', | 47 '//chrome/android:chrome_sync_shell_apk', |
| 50 '//chrome/android:chrome_sync_shell_test_apk', | 48 '//chrome/android:chrome_sync_shell_test_apk', |
| 51 '//content/public/android:content_junit_tests', | 49 '//content/public/android:content_junit_tests', |
| 52 '//content/shell/android:content_shell_apk', | 50 '//content/shell/android:content_shell_apk', |
| 53 ] | 51 ] |
| 54 | 52 |
| 53 |
| 54 def _TemplatePath(name): |
| 55 return os.path.join(_FILE_DIR, '{}.jinja'.format(name)) |
| 56 |
| 57 |
| 55 def _RebasePath(path_or_list, new_cwd=None, old_cwd=None): | 58 def _RebasePath(path_or_list, new_cwd=None, old_cwd=None): |
| 56 """Makes the given path(s) relative to new_cwd, or absolute if not specified. | 59 """Makes the given path(s) relative to new_cwd, or absolute if not specified. |
| 57 | 60 |
| 58 If new_cwd is not specified, absolute paths are returned. | 61 If new_cwd is not specified, absolute paths are returned. |
| 59 If old_cwd is not specified, constants.GetOutDirectory() is assumed. | 62 If old_cwd is not specified, constants.GetOutDirectory() is assumed. |
| 60 """ | 63 """ |
| 61 if not isinstance(path_or_list, basestring): | 64 if not isinstance(path_or_list, basestring): |
| 62 return [_RebasePath(p, new_cwd, old_cwd) for p in path_or_list] | 65 return [_RebasePath(p, new_cwd, old_cwd) for p in path_or_list] |
| 63 if old_cwd is None: | 66 if old_cwd is None: |
| 64 old_cwd = constants.GetOutDirectory() | 67 old_cwd = constants.GetOutDirectory() |
| (...skipping 18 matching lines...) Expand all Loading... |
| 83 with codecs.open(path, 'w', 'utf-8') as output_file: | 86 with codecs.open(path, 'w', 'utf-8') as output_file: |
| 84 output_file.write(data) | 87 output_file.write(data) |
| 85 | 88 |
| 86 | 89 |
| 87 def _ReadBuildVars(output_dir): | 90 def _ReadBuildVars(output_dir): |
| 88 with open(os.path.join(output_dir, 'build_vars.txt')) as f: | 91 with open(os.path.join(output_dir, 'build_vars.txt')) as f: |
| 89 return dict(l.rstrip().split('=', 1) for l in f) | 92 return dict(l.rstrip().split('=', 1) for l in f) |
| 90 | 93 |
| 91 | 94 |
| 92 def _RunNinja(output_dir, args): | 95 def _RunNinja(output_dir, args): |
| 93 cmd = ['ninja', '-C', output_dir, '-j50'] | 96 cmd = ['ninja', '-C', output_dir, '-j1000'] |
| 94 cmd.extend(args) | 97 cmd.extend(args) |
| 95 logging.info('Running: %r', cmd) | 98 logging.info('Running: %r', cmd) |
| 96 subprocess.check_call(cmd) | 99 subprocess.check_call(cmd) |
| 97 | 100 |
| 98 | 101 |
| 99 def _QueryForAllGnTargets(output_dir): | 102 def _QueryForAllGnTargets(output_dir): |
| 100 # Query ninja rather than GN since it's faster. | 103 # Query ninja rather than GN since it's faster. |
| 101 cmd = ['ninja', '-C', output_dir, '-t', 'targets'] | 104 cmd = ['ninja', '-C', output_dir, '-t', 'targets'] |
| 102 logging.info('Running: %r', cmd) | 105 logging.info('Running: %r', cmd) |
| 103 ninja_output = build_utils.CheckOutput(cmd) | 106 ninja_output = build_utils.CheckOutput(cmd) |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 # things up at all. | 325 # things up at all. |
| 323 variables['prebuilts'] = relativize(gradle['dependent_prebuilt_jars']) | 326 variables['prebuilts'] = relativize(gradle['dependent_prebuilt_jars']) |
| 324 deps = [_ProjectEntry.FromBuildConfigPath(p) | 327 deps = [_ProjectEntry.FromBuildConfigPath(p) |
| 325 for p in gradle['dependent_android_projects']] | 328 for p in gradle['dependent_android_projects']] |
| 326 | 329 |
| 327 variables['android_project_deps'] = [d.ProjectName() for d in deps] | 330 variables['android_project_deps'] = [d.ProjectName() for d in deps] |
| 328 deps = [_ProjectEntry.FromBuildConfigPath(p) | 331 deps = [_ProjectEntry.FromBuildConfigPath(p) |
| 329 for p in gradle['dependent_java_projects']] | 332 for p in gradle['dependent_java_projects']] |
| 330 variables['java_project_deps'] = [d.ProjectName() for d in deps] | 333 variables['java_project_deps'] = [d.ProjectName() for d in deps] |
| 331 | 334 |
| 332 return jinja_processor.Render(_JINJA_TEMPLATE_PATH, variables) | 335 return jinja_processor.Render( |
| 336 _TemplatePath(target_type.split('_')[0]), variables) |
| 333 | 337 |
| 334 | 338 |
| 335 def _GenerateRootGradle(jinja_processor): | 339 def _GenerateRootGradle(jinja_processor): |
| 336 """Returns the data for the root project's build.gradle.""" | 340 """Returns the data for the root project's build.gradle.""" |
| 337 variables = {'template_type': 'root'} | 341 return jinja_processor.Render(_TemplatePath('root')) |
| 338 return jinja_processor.Render(_JINJA_TEMPLATE_PATH, variables) | |
| 339 | 342 |
| 340 | 343 |
| 341 def _GenerateSettingsGradle(project_entries): | 344 def _GenerateSettingsGradle(project_entries): |
| 342 """Returns the data for settings.gradle.""" | 345 """Returns the data for settings.gradle.""" |
| 343 project_name = os.path.basename(os.path.dirname(host_paths.DIR_SOURCE_ROOT)) | 346 project_name = os.path.basename(os.path.dirname(host_paths.DIR_SOURCE_ROOT)) |
| 344 lines = [] | 347 lines = [] |
| 345 lines.append('// Generated by //build/android/gradle/generate_gradle.py') | 348 lines.append('// Generated by //build/android/gradle/generate_gradle.py') |
| 346 lines.append('rootProject.name = "%s"' % project_name) | 349 lines.append('rootProject.name = "%s"' % project_name) |
| 347 lines.append('rootProject.projectDir = settingsDir') | 350 lines.append('rootProject.projectDir = settingsDir') |
| 348 lines.append('') | 351 lines.append('') |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 | 443 |
| 441 # There are many unused libraries, so restrict to those that are actually used | 444 # There are many unused libraries, so restrict to those that are actually used |
| 442 # when using --all. | 445 # when using --all. |
| 443 if args.all: | 446 if args.all: |
| 444 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] | 447 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] |
| 445 | 448 |
| 446 all_entries = _FindAllProjectEntries(main_entries) | 449 all_entries = _FindAllProjectEntries(main_entries) |
| 447 logging.info('Found %d dependent build_config targets.', len(all_entries)) | 450 logging.info('Found %d dependent build_config targets.', len(all_entries)) |
| 448 | 451 |
| 449 logging.warning('Writing .gradle files...') | 452 logging.warning('Writing .gradle files...') |
| 450 jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) | 453 jinja_processor = jinja_template.JinjaProcessor(_FILE_DIR) |
| 451 build_vars = _ReadBuildVars(output_dir) | 454 build_vars = _ReadBuildVars(output_dir) |
| 452 project_entries = [] | 455 project_entries = [] |
| 453 srcjar_tuples = [] | 456 srcjar_tuples = [] |
| 454 generated_inputs = [] | 457 generated_inputs = [] |
| 455 for entry in all_entries: | 458 for entry in all_entries: |
| 456 if entry.GetType() not in ('android_apk', 'java_library', 'java_binary'): | 459 if entry.GetType() not in ('android_apk', 'java_library', 'java_binary'): |
| 457 continue | 460 continue |
| 458 | 461 |
| 459 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) | 462 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) |
| 460 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) | 463 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) | 517 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) |
| 515 | 518 |
| 516 logging.warning('Project created! (%d subprojects)', len(project_entries)) | 519 logging.warning('Project created! (%d subprojects)', len(project_entries)) |
| 517 logging.warning('Generated projects work best with Android Studio 2.2') | 520 logging.warning('Generated projects work best with Android Studio 2.2') |
| 518 logging.warning('For more tips: https://chromium.googlesource.com/chromium' | 521 logging.warning('For more tips: https://chromium.googlesource.com/chromium' |
| 519 '/src.git/+/master/docs/android_studio.md') | 522 '/src.git/+/master/docs/android_studio.md') |
| 520 | 523 |
| 521 | 524 |
| 522 if __name__ == '__main__': | 525 if __name__ == '__main__': |
| 523 main() | 526 main() |
| OLD | NEW |