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 16 matching lines...) Expand all Loading... |
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 _FILE_DIR = os.path.dirname(__file__) | 32 _FILE_DIR = os.path.dirname(__file__) |
33 _JAVA_SUBDIR = 'symlinked-java' | 33 _JAVA_SUBDIR = 'symlinked-java' |
34 _SRCJARS_SUBDIR = 'extracted-srcjars' | 34 _SRCJARS_SUBDIR = 'extracted-srcjars' |
35 _JNI_LIBS_SUBDIR = 'symlinked-libs' | 35 _JNI_LIBS_SUBDIR = 'symlinked-libs' |
36 _ARMEABI_SUBDIR = 'armeabi' | 36 _ARMEABI_SUBDIR = 'armeabi' |
| 37 _RES_SUBDIR = 'extracted-res' |
37 | 38 |
38 _DEFAULT_TARGETS = [ | 39 _DEFAULT_TARGETS = [ |
39 # TODO(agrieve): Requires alternate android.jar to compile. | 40 # TODO(agrieve): Requires alternate android.jar to compile. |
40 # '//android_webview:system_webview_apk', | 41 # '//android_webview:system_webview_apk', |
41 '//android_webview/test:android_webview_apk', | 42 '//android_webview/test:android_webview_apk', |
42 '//android_webview/test:android_webview_test_apk', | 43 '//android_webview/test:android_webview_test_apk', |
43 '//base:base_junit_tests', | 44 '//base:base_junit_tests', |
44 '//chrome/android:chrome_junit_tests', | 45 '//chrome/android:chrome_junit_tests', |
45 '//chrome/android:chrome_public_apk', | 46 '//chrome/android:chrome_public_apk', |
46 '//chrome/android:chrome_public_test_apk', | 47 '//chrome/android:chrome_public_test_apk', |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 206 |
206 def _GenJavaDirs(self, entry): | 207 def _GenJavaDirs(self, entry): |
207 java_dirs = _CreateJavaSourceDir( | 208 java_dirs = _CreateJavaSourceDir( |
208 constants.GetOutDirectory(), self.EntryOutputDir(entry), | 209 constants.GetOutDirectory(), self.EntryOutputDir(entry), |
209 entry.JavaFiles()) | 210 entry.JavaFiles()) |
210 if self.Srcjars(entry): | 211 if self.Srcjars(entry): |
211 java_dirs.append( | 212 java_dirs.append( |
212 os.path.join(self.EntryOutputDir(entry), _SRCJARS_SUBDIR)) | 213 os.path.join(self.EntryOutputDir(entry), _SRCJARS_SUBDIR)) |
213 return java_dirs | 214 return java_dirs |
214 | 215 |
| 216 def _GenResDirs(self, entry): |
| 217 res_dirs = list(entry.DepsInfo().get('owned_resources_dirs', [])) |
| 218 zips = entry.DepsInfo().get('owned_resources_zips') |
| 219 if zips: |
| 220 # These are generated resources that should not be modified. |
| 221 res_dir = os.path.join(self.EntryOutputDir(entry), _RES_SUBDIR) |
| 222 for zip_path in _RebasePath(zips): |
| 223 _ExtractFile(zip_path, res_dir) |
| 224 res_dirs.append(res_dir) |
| 225 return res_dirs |
| 226 |
215 def _Relativize(self, entry, paths): | 227 def _Relativize(self, entry, paths): |
216 return _RebasePath(paths, self.EntryOutputDir(entry)) | 228 return _RebasePath(paths, self.EntryOutputDir(entry)) |
217 | 229 |
218 def EntryOutputDir(self, entry): | 230 def EntryOutputDir(self, entry): |
219 return os.path.join(self.project_dir, entry.GradleSubdir()) | 231 return os.path.join(self.project_dir, entry.GradleSubdir()) |
220 | 232 |
221 def Srcjars(self, entry): | 233 def Srcjars(self, entry): |
222 srcjars = _RebasePath(entry.Gradle().get('bundled_srcjars', [])) | 234 srcjars = _RebasePath(entry.Gradle().get('bundled_srcjars', [])) |
223 if not self.use_gradle_process_resources: | 235 if not self.use_gradle_process_resources: |
224 srcjars += _RebasePath(entry.BuildConfig()['javac']['srcjars']) | 236 srcjars += _RebasePath(entry.BuildConfig()['javac']['srcjars']) |
225 return srcjars | 237 return srcjars |
226 | 238 |
227 def GeneratedInputs(self, entry): | 239 def GeneratedInputs(self, entry): |
228 generated_inputs = [] | 240 generated_inputs = [] |
229 generated_inputs.extend(self.Srcjars(entry)) | 241 generated_inputs.extend(self.Srcjars(entry)) |
230 generated_inputs.extend( | 242 generated_inputs.extend( |
231 p for p in entry.JavaFiles() if not p.startswith('..')) | 243 p for p in entry.JavaFiles() if not p.startswith('..')) |
232 generated_inputs.extend(entry.Gradle()['dependent_prebuilt_jars']) | 244 generated_inputs.extend(entry.Gradle()['dependent_prebuilt_jars']) |
233 return generated_inputs | 245 return generated_inputs |
234 | 246 |
235 def Generate(self, entry): | 247 def Generate(self, entry): |
236 variables = {} | 248 variables = {} |
237 android_test_manifest = entry.Gradle().get( | 249 android_test_manifest = entry.Gradle().get( |
238 'android_manifest', _DEFAULT_ANDROID_MANIFEST_PATH) | 250 'android_manifest', _DEFAULT_ANDROID_MANIFEST_PATH) |
239 variables['android_manifest'] = self._Relativize( | 251 variables['android_manifest'] = self._Relativize( |
240 entry, android_test_manifest) | 252 entry, android_test_manifest) |
241 variables['java_dirs'] = self._Relativize(entry, self._GenJavaDirs(entry)) | 253 variables['java_dirs'] = self._Relativize(entry, self._GenJavaDirs(entry)) |
242 variables['jni_libs'] = self._Relativize(entry, self._GenJniLibs(entry)) | 254 variables['jni_libs'] = self._Relativize(entry, self._GenJniLibs(entry)) |
| 255 variables['res_dirs'] = self._Relativize(entry, self._GenResDirs(entry)) |
243 deps = [_ProjectEntry.FromBuildConfigPath(p) | 256 deps = [_ProjectEntry.FromBuildConfigPath(p) |
244 for p in entry.Gradle()['dependent_android_projects']] | 257 for p in entry.Gradle()['dependent_android_projects']] |
245 variables['android_project_deps'] = [d.ProjectName() for d in deps] | 258 variables['android_project_deps'] = [d.ProjectName() for d in deps] |
246 # TODO(agrieve): Add an option to use interface jars and see if that speeds | 259 # TODO(agrieve): Add an option to use interface jars and see if that speeds |
247 # things up at all. | 260 # things up at all. |
248 variables['prebuilts'] = self._Relativize( | 261 variables['prebuilts'] = self._Relativize( |
249 entry, entry.Gradle()['dependent_prebuilt_jars']) | 262 entry, entry.Gradle()['dependent_prebuilt_jars']) |
250 deps = [_ProjectEntry.FromBuildConfigPath(p) | 263 deps = [_ProjectEntry.FromBuildConfigPath(p) |
251 for p in entry.Gradle()['dependent_java_projects']] | 264 for p in entry.Gradle()['dependent_java_projects']] |
252 variables['java_project_deps'] = [d.ProjectName() for d in deps] | 265 variables['java_project_deps'] = [d.ProjectName() for d in deps] |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 lines.append('') | 435 lines.append('') |
423 | 436 |
424 for entry in project_entries: | 437 for entry in project_entries: |
425 # Example target: android_webview:android_webview_java__build_config | 438 # Example target: android_webview:android_webview_java__build_config |
426 lines.append('include ":%s"' % entry.ProjectName()) | 439 lines.append('include ":%s"' % entry.ProjectName()) |
427 lines.append('project(":%s").projectDir = new File(settingsDir, "%s")' % | 440 lines.append('project(":%s").projectDir = new File(settingsDir, "%s")' % |
428 (entry.ProjectName(), entry.GradleSubdir())) | 441 (entry.ProjectName(), entry.GradleSubdir())) |
429 return '\n'.join(lines) | 442 return '\n'.join(lines) |
430 | 443 |
431 | 444 |
| 445 def _ExtractFile(zip_path, extracted_path): |
| 446 logging.info('Extracting %s to %s', zip_path, extracted_path) |
| 447 with zipfile.ZipFile(zip_path) as z: |
| 448 z.extractall(extracted_path) |
| 449 |
| 450 |
432 def _ExtractSrcjars(entry_output_dir, srcjar_tuples): | 451 def _ExtractSrcjars(entry_output_dir, srcjar_tuples): |
433 """Extracts all srcjars to the directory given by the tuples.""" | 452 """Extracts all srcjars to the directory given by the tuples.""" |
434 extracted_paths = set(s[1] for s in srcjar_tuples) | 453 extracted_paths = set(s[1] for s in srcjar_tuples) |
435 for extracted_path in extracted_paths: | 454 for extracted_path in extracted_paths: |
436 assert _IsSubpathOf(extracted_path, entry_output_dir) | 455 assert _IsSubpathOf(extracted_path, entry_output_dir) |
437 shutil.rmtree(extracted_path, True) | 456 shutil.rmtree(extracted_path, True) |
438 | 457 |
439 for srcjar_path, extracted_path in srcjar_tuples: | 458 for srcjar_path, extracted_path in srcjar_tuples: |
440 logging.info('Extracting %s to %s', srcjar_path, extracted_path) | 459 _ExtractFile(srcjar_path, extracted_path) |
441 with zipfile.ZipFile(srcjar_path) as z: | |
442 z.extractall(extracted_path) | |
443 | 460 |
444 | 461 |
445 def _FindAllProjectEntries(main_entries): | 462 def _FindAllProjectEntries(main_entries): |
446 """Returns the list of all _ProjectEntry instances given the root project.""" | 463 """Returns the list of all _ProjectEntry instances given the root project.""" |
447 found = set() | 464 found = set() |
448 to_scan = list(main_entries) | 465 to_scan = list(main_entries) |
449 while to_scan: | 466 while to_scan: |
450 cur_entry = to_scan.pop() | 467 cur_entry = to_scan.pop() |
451 if cur_entry in found: | 468 if cur_entry in found: |
452 continue | 469 continue |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 _ExtractSrcjars(generator.project_dir, srcjar_tuples) | 607 _ExtractSrcjars(generator.project_dir, srcjar_tuples) |
591 | 608 |
592 logging.warning('Project created! (%d subprojects)', len(project_entries)) | 609 logging.warning('Project created! (%d subprojects)', len(project_entries)) |
593 logging.warning('Generated projects work best with Android Studio 2.2') | 610 logging.warning('Generated projects work best with Android Studio 2.2') |
594 logging.warning('For more tips: https://chromium.googlesource.com/chromium' | 611 logging.warning('For more tips: https://chromium.googlesource.com/chromium' |
595 '/src.git/+/master/docs/android_studio.md') | 612 '/src.git/+/master/docs/android_studio.md') |
596 | 613 |
597 | 614 |
598 if __name__ == '__main__': | 615 if __name__ == '__main__': |
599 main() | 616 main() |
OLD | NEW |