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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 subpath = os.path.relpath(target_path, prefix) | 191 subpath = os.path.relpath(target_path, prefix) |
192 symlinked_path = os.path.join(symlink_dir, subpath) | 192 symlinked_path = os.path.join(symlink_dir, subpath) |
193 symlinked_dir = os.path.dirname(symlinked_path) | 193 symlinked_dir = os.path.dirname(symlinked_path) |
194 if not os.path.exists(symlinked_dir): | 194 if not os.path.exists(symlinked_dir): |
195 os.makedirs(symlinked_dir) | 195 os.makedirs(symlinked_dir) |
196 relpath = os.path.relpath(target_path, symlinked_dir) | 196 relpath = os.path.relpath(target_path, symlinked_dir) |
197 logging.debug('Creating symlink %s -> %s', symlinked_path, relpath) | 197 logging.debug('Creating symlink %s -> %s', symlinked_path, relpath) |
198 os.symlink(relpath, symlinked_path) | 198 os.symlink(relpath, symlinked_path) |
199 | 199 |
200 | 200 |
201 def _CreateJavaSourceDir(output_dir, entry_output_dir, java_sources_file): | 201 def _CreateJavaSourceDir(output_dir, entry_output_dir, java_files): |
202 """Computes and constructs when necessary the list of java source directories. | 202 """Computes and constructs when necessary the list of java source directories. |
203 | 203 |
204 1. Computes the root java source directories from the list of files. | 204 1. Computes the root java source directories from the list of files. |
205 2. Determines whether there are any .java files in them that are not included | 205 2. Determines whether there are any .java files in them that are not included |
206 in |java_sources_file|. | 206 in |java_files|. |
207 3. If not, returns the list of java source directories. If so, constructs a | 207 3. If not, returns the list of java source directories. If so, constructs a |
208 tree of symlinks within |entry_output_dir| of all files in | 208 tree of symlinks within |entry_output_dir| of all files in |java_files|. |
209 |java_sources_file|. | |
210 """ | 209 """ |
211 java_dirs = [] | 210 java_dirs = [] |
212 if java_sources_file: | 211 if java_files: |
213 java_files = _RebasePath(build_utils.ReadSourcesList(java_sources_file)) | 212 java_files = _RebasePath(java_files) |
214 java_dirs = _ComputeJavaSourceDirs(java_files) | 213 java_dirs = _ComputeJavaSourceDirs(java_files) |
215 | 214 |
216 found_java_files = build_utils.FindInDirectories(java_dirs, '*.java') | 215 found_java_files = build_utils.FindInDirectories(java_dirs, '*.java') |
217 unwanted_java_files = set(found_java_files) - set(java_files) | 216 unwanted_java_files = set(found_java_files) - set(java_files) |
218 missing_java_files = set(java_files) - set(found_java_files) | 217 missing_java_files = set(java_files) - set(found_java_files) |
219 # Warn only about non-generated files that are missing. | 218 # Warn only about non-generated files that are missing. |
220 missing_java_files = [p for p in missing_java_files | 219 missing_java_files = [p for p in missing_java_files |
221 if not p.startswith(output_dir)] | 220 if not p.startswith(output_dir)] |
222 if unwanted_java_files: | 221 if unwanted_java_files: |
223 logging.debug('Target requires .java symlinks: %s', entry_output_dir) | 222 logging.debug('Target requires .java symlinks: %s', entry_output_dir) |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] | 387 main_entries = [e for e in main_entries if e.GetType() == 'android_apk'] |
389 | 388 |
390 all_entries = _FindAllProjectEntries(main_entries) | 389 all_entries = _FindAllProjectEntries(main_entries) |
391 logging.info('Found %d dependent build_config targets.', len(all_entries)) | 390 logging.info('Found %d dependent build_config targets.', len(all_entries)) |
392 | 391 |
393 logging.warning('Writing .gradle files...') | 392 logging.warning('Writing .gradle files...') |
394 jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) | 393 jinja_processor = jinja_template.JinjaProcessor(host_paths.DIR_SOURCE_ROOT) |
395 build_vars = _ReadBuildVars(output_dir) | 394 build_vars = _ReadBuildVars(output_dir) |
396 project_entries = [] | 395 project_entries = [] |
397 srcjar_tuples = [] | 396 srcjar_tuples = [] |
| 397 generated_inputs = [] |
398 for entry in all_entries: | 398 for entry in all_entries: |
399 if entry.GetType() not in ('android_apk', 'java_library'): | 399 if entry.GetType() not in ('android_apk', 'java_library'): |
400 continue | 400 continue |
401 | 401 |
402 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) | 402 entry_output_dir = os.path.join(gradle_output_dir, entry.GradleSubdir()) |
403 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) | 403 relativize = lambda x, d=entry_output_dir: _RebasePath(x, d) |
404 build_config = entry.BuildConfig() | 404 build_config = entry.BuildConfig() |
405 | 405 |
406 srcjars = _RebasePath(build_config['gradle'].get('bundled_srcjars', [])) | 406 srcjars = _RebasePath(build_config['gradle'].get('bundled_srcjars', [])) |
407 if not args.use_gradle_process_resources: | 407 if not args.use_gradle_process_resources: |
408 srcjars += _RebasePath(build_config['javac']['srcjars']) | 408 srcjars += _RebasePath(build_config['javac']['srcjars']) |
409 | 409 |
410 java_sources_file = build_config['gradle'].get('java_sources_file') | 410 java_sources_file = build_config['gradle'].get('java_sources_file') |
| 411 java_files = [] |
411 if java_sources_file: | 412 if java_sources_file: |
412 java_sources_file = _RebasePath(java_sources_file) | 413 java_sources_file = _RebasePath(java_sources_file) |
| 414 java_files = build_utils.ReadSourcesList(java_sources_file) |
413 | 415 |
414 java_dirs = _CreateJavaSourceDir(output_dir, entry_output_dir, | 416 java_dirs = _CreateJavaSourceDir(output_dir, entry_output_dir, java_files) |
415 java_sources_file) | |
416 if srcjars: | 417 if srcjars: |
417 java_dirs.append(os.path.join(entry_output_dir, _SRCJARS_SUBDIR)) | 418 java_dirs.append(os.path.join(entry_output_dir, _SRCJARS_SUBDIR)) |
418 | 419 |
419 data = _GenerateGradleFile(build_config, build_vars, java_dirs, relativize, | 420 data = _GenerateGradleFile(build_config, build_vars, java_dirs, relativize, |
420 args.use_gradle_process_resources, | 421 args.use_gradle_process_resources, |
421 jinja_processor) | 422 jinja_processor) |
422 if data: | 423 if data: |
423 project_entries.append(entry) | 424 project_entries.append(entry) |
| 425 # Build all paths references by .gradle that exist within output_dir. |
| 426 generated_inputs.extend(srcjars) |
| 427 generated_inputs.extend(p for p in java_files if not p.startswith('..')) |
| 428 generated_inputs.extend(build_config['gradle']['dependent_prebuilt_jars']) |
| 429 |
424 srcjar_tuples.extend( | 430 srcjar_tuples.extend( |
425 (s, os.path.join(entry_output_dir, _SRCJARS_SUBDIR)) for s in srcjars) | 431 (s, os.path.join(entry_output_dir, _SRCJARS_SUBDIR)) for s in srcjars) |
426 _WriteFile(os.path.join(entry_output_dir, 'build.gradle'), data) | 432 _WriteFile(os.path.join(entry_output_dir, 'build.gradle'), data) |
427 | 433 |
428 _WriteFile(os.path.join(gradle_output_dir, 'build.gradle'), | 434 _WriteFile(os.path.join(gradle_output_dir, 'build.gradle'), |
429 _GenerateRootGradle(jinja_processor)) | 435 _GenerateRootGradle(jinja_processor)) |
430 | 436 |
431 _WriteFile(os.path.join(gradle_output_dir, 'settings.gradle'), | 437 _WriteFile(os.path.join(gradle_output_dir, 'settings.gradle'), |
432 _GenerateSettingsGradle(project_entries)) | 438 _GenerateSettingsGradle(project_entries)) |
433 | 439 |
434 sdk_path = _RebasePath(build_vars['android_sdk_root']) | 440 sdk_path = _RebasePath(build_vars['android_sdk_root']) |
435 _WriteFile(os.path.join(gradle_output_dir, 'local.properties'), | 441 _WriteFile(os.path.join(gradle_output_dir, 'local.properties'), |
436 _GenerateLocalProperties(sdk_path)) | 442 _GenerateLocalProperties(sdk_path)) |
437 | 443 |
| 444 if generated_inputs: |
| 445 logging.warning('Building generated source files...') |
| 446 targets = _RebasePath(generated_inputs, output_dir) |
| 447 _RunNinja(output_dir, targets) |
| 448 |
438 if srcjar_tuples: | 449 if srcjar_tuples: |
439 logging.warning('Building all .srcjar files...') | |
440 targets = _RebasePath([s[0] for s in srcjar_tuples], output_dir) | |
441 _RunNinja(output_dir, targets) | |
442 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) | 450 _ExtractSrcjars(gradle_output_dir, srcjar_tuples) |
| 451 |
443 logging.warning('Project created! (%d subprojects)', len(project_entries)) | 452 logging.warning('Project created! (%d subprojects)', len(project_entries)) |
444 logging.warning('Generated projects work best with Android Studio 2.2') | 453 logging.warning('Generated projects work best with Android Studio 2.2') |
445 logging.warning('For more tips: https://chromium.googlesource.com/chromium' | 454 logging.warning('For more tips: https://chromium.googlesource.com/chromium' |
446 '/src.git/+/master/docs/android_studio.md') | 455 '/src.git/+/master/docs/android_studio.md') |
447 | 456 |
448 | 457 |
449 if __name__ == '__main__': | 458 if __name__ == '__main__': |
450 main() | 459 main() |
OLD | NEW |