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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 """Returns the Gradle project name.""" | 142 """Returns the Gradle project name.""" |
143 return self.GradleSubdir().replace(os.path.sep, '>') | 143 return self.GradleSubdir().replace(os.path.sep, '>') |
144 | 144 |
145 def BuildConfig(self): | 145 def BuildConfig(self): |
146 """Reads and returns the project's .build_config JSON.""" | 146 """Reads and returns the project's .build_config JSON.""" |
147 if not self._build_config: | 147 if not self._build_config: |
148 path = os.path.join('gen', self.GradleSubdir() + '.build_config') | 148 path = os.path.join('gen', self.GradleSubdir() + '.build_config') |
149 self._build_config = build_utils.ReadJson(_RebasePath(path)) | 149 self._build_config = build_utils.ReadJson(_RebasePath(path)) |
150 return self._build_config | 150 return self._build_config |
151 | 151 |
| 152 def GetType(self): |
| 153 """Returns the target type from its .build_config.""" |
| 154 return self.BuildConfig()['deps_info']['type'] |
| 155 |
152 | 156 |
153 def _ComputeJavaSourceDirs(java_files): | 157 def _ComputeJavaSourceDirs(java_files): |
154 """Returns the list of source directories for the given files.""" | 158 """Returns the list of source directories for the given files.""" |
155 found_roots = set() | 159 found_roots = set() |
156 for path in java_files: | 160 for path in java_files: |
157 path_root = path | 161 path_root = path |
158 # Recognize these tokens as top-level. | 162 # Recognize these tokens as top-level. |
159 while os.path.basename(path_root) not in ('javax', 'org', 'com', 'src'): | 163 while os.path.basename(path_root) not in ('javax', 'org', 'com', 'src'): |
160 assert path_root, 'Failed to find source dir for ' + path | 164 assert path_root, 'Failed to find source dir for ' + path |
161 path_root = os.path.dirname(path_root) | 165 path_root = os.path.dirname(path_root) |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 _RunNinja(output_dir, ['build.ninja']) | 365 _RunNinja(output_dir, ['build.ninja']) |
362 # Query ninja for all __build_config targets. | 366 # Query ninja for all __build_config targets. |
363 targets = _QueryForAllGnTargets(output_dir) | 367 targets = _QueryForAllGnTargets(output_dir) |
364 else: | 368 else: |
365 targets = args.targets or _DEFAULT_TARGETS | 369 targets = args.targets or _DEFAULT_TARGETS |
366 # TODO(agrieve): See if it makes sense to utilize Gradle's test constructs | 370 # TODO(agrieve): See if it makes sense to utilize Gradle's test constructs |
367 # for our instrumentation tests. | 371 # for our instrumentation tests. |
368 targets = [re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets] | 372 targets = [re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets] |
369 | 373 |
370 main_entries = [_ProjectEntry(t) for t in targets] | 374 main_entries = [_ProjectEntry(t) for t in targets] |
| 375 |
371 logging.warning('Building .build_config files...') | 376 logging.warning('Building .build_config files...') |
372 _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries]) | 377 _RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries]) |
373 | 378 |
| 379 # There are many unused libraries, so restrict to those that are actually used |
| 380 # when using --all. |
| 381 if args.all: |
| 382 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] |
| 383 |
374 all_entries = _FindAllProjectEntries(main_entries) | 384 all_entries = _FindAllProjectEntries(main_entries) |
375 logging.info('Found %d dependent build_config targets.', len(all_entries)) | 385 logging.info('Found %d dependent build_config targets.', len(all_entries)) |
376 | 386 |
377 logging.warning('Writing .gradle files...') | 387 logging.warning('Writing .gradle files...') |
378 jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) | 388 jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) |
379 config_json = build_utils.ReadJson( | 389 config_json = build_utils.ReadJson( |
380 os.path.join(output_dir, 'gradle', 'config.json')) | 390 os.path.join(output_dir, 'gradle', 'config.json')) |
381 project_entries = [] | 391 project_entries = [] |
382 srcjar_tuples = [] | 392 srcjar_tuples = [] |
383 for entry in all_entries: | 393 for entry in all_entries: |
384 build_config = entry.BuildConfig() | 394 if entry.GetType() not in ('android_apk', 'java_library'): |
385 if build_config['deps_info']['type'] not in ('android_apk', 'java_library'): | |
386 continue | 395 continue |
387 | 396 |
388 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) | 397 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) |
389 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) | 398 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) |
| 399 build_config = entry.BuildConfig() |
390 | 400 |
391 srcjars = _RebasePath(build_config['gradle'].get('bundled_srcjars', [])) | 401 srcjars = _RebasePath(build_config['gradle'].get('bundled_srcjars', [])) |
392 if not args.use_gradle_process_resources: | 402 if not args.use_gradle_process_resources: |
393 srcjars += _RebasePath(build_config['javac']['srcjars']) | 403 srcjars += _RebasePath(build_config['javac']['srcjars']) |
394 | 404 |
395 java_sources_file = build_config['gradle'].get('java_sources_file') | 405 java_sources_file = build_config['gradle'].get('java_sources_file') |
396 if java_sources_file: | 406 if java_sources_file: |
397 java_sources_file = _RebasePath(java_sources_file) | 407 java_sources_file = _RebasePath(java_sources_file) |
398 | 408 |
399 java_dirs = _CreateJavaSourceDir(output_dir, entry_output_dir, | 409 java_dirs = _CreateJavaSourceDir(output_dir, entry_output_dir, |
(...skipping 18 matching lines...) Expand all Loading... |
418 | 428 |
419 sdk_path = _RebasePath(config_json['android_sdk_root']) | 429 sdk_path = _RebasePath(config_json['android_sdk_root']) |
420 _WriteFile(os.path.join(gradle_output_dir, 'local.properties'), | 430 _WriteFile(os.path.join(gradle_output_dir, 'local.properties'), |
421 _GenerateLocalProperties(sdk_path)) | 431 _GenerateLocalProperties(sdk_path)) |
422 | 432 |
423 if srcjar_tuples: | 433 if srcjar_tuples: |
424 logging.warning('Building all .srcjar files...') | 434 logging.warning('Building all .srcjar files...') |
425 targets = _RebasePath([s[0] for s in srcjar_tuples], output_dir) | 435 targets = _RebasePath([s[0] for s in srcjar_tuples], output_dir) |
426 _RunNinja(output_dir, targets) | 436 _RunNinja(output_dir, targets) |
427 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) | 437 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) |
428 logging.warning('Project created successfully!') | 438 logging.warning('Project created! (%d subprojects)', len(project_entries)) |
429 logging.warning('Generated projects work best with Android Studio 2.2') | 439 logging.warning('Generated projects work best with Android Studio 2.2') |
430 logging.warning('For more tips: https://chromium.googlesource.com/chromium' | 440 logging.warning('For more tips: https://chromium.googlesource.com/chromium' |
431 '/src.git/+/master/docs/android_studio.md') | 441 '/src.git/+/master/docs/android_studio.md') |
432 | 442 |
433 | 443 |
434 if __name__ == '__main__': | 444 if __name__ == '__main__': |
435 main() | 445 main() |
OLD | NEW |