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