Chromium Code Reviews| Index: build/android/gradle/generate_gradle.py |
| diff --git a/build/android/gradle/generate_gradle.py b/build/android/gradle/generate_gradle.py |
| index fbe0966ac3b40b2e4ce3ea8a5fb3b613eb9f5c18..90ae3db79234b78a419ed45dd10fb5183f87a386 100755 |
| --- a/build/android/gradle/generate_gradle.py |
| +++ b/build/android/gradle/generate_gradle.py |
| @@ -7,6 +7,7 @@ |
| import argparse |
| import codecs |
| +import glob |
| import logging |
| import os |
| import re |
| @@ -30,7 +31,6 @@ from util import build_utils |
| _DEFAULT_ANDROID_MANIFEST_PATH = os.path.join( |
| host_paths.DIR_SOURCE_ROOT, 'build', 'android', 'AndroidManifest.xml') |
| _FILE_DIR = os.path.dirname(__file__) |
| -_JAVA_SUBDIR = 'symlinked-java' |
| _SRCJARS_SUBDIR = 'extracted-srcjars' |
| _JNI_LIBS_SUBDIR = 'symlinked-libs' |
| _ARMEABI_SUBDIR = 'armeabi' |
| @@ -204,13 +204,12 @@ class _ProjectContextGenerator(object): |
| return jni_libs |
| def _GenJavaDirs(self, entry): |
| - java_dirs = _CreateJavaSourceDir( |
| - constants.GetOutDirectory(), self.EntryOutputDir(entry), |
| - entry.JavaFiles()) |
| + java_dirs, excludes = _CreateJavaSourceDir( |
| + constants.GetOutDirectory(), entry.JavaFiles()) |
| if self.Srcjars(entry): |
| java_dirs.append( |
| os.path.join(self.EntryOutputDir(entry), _SRCJARS_SUBDIR)) |
| - return java_dirs |
| + return java_dirs, excludes |
| def _Relativize(self, entry, paths): |
| return _RebasePath(paths, self.EntryOutputDir(entry)) |
| @@ -238,7 +237,9 @@ class _ProjectContextGenerator(object): |
| 'android_manifest', _DEFAULT_ANDROID_MANIFEST_PATH) |
| variables['android_manifest'] = self._Relativize( |
| entry, android_test_manifest) |
| - variables['java_dirs'] = self._Relativize(entry, self._GenJavaDirs(entry)) |
| + java_dirs, excludes = self._GenJavaDirs(entry) |
| + variables['java_dirs'] = self._Relativize(entry, java_dirs) |
| + variables['java_excludes'] = excludes |
| variables['jni_libs'] = self._Relativize(entry, self._GenJniLibs(entry)) |
| deps = [_ProjectEntry.FromBuildConfigPath(p) |
| for p in entry.Gradle()['dependent_android_projects']] |
| @@ -254,8 +255,8 @@ class _ProjectContextGenerator(object): |
| def _ComputeJavaSourceDirs(java_files): |
| - """Returns the list of source directories for the given files.""" |
| - found_roots = set() |
| + """Returns a dictionary of source dirs with each given files in one.""" |
| + found_roots = {} |
| for path in java_files: |
| path_root = path |
| # Recognize these tokens as top-level. |
| @@ -268,8 +269,10 @@ def _ComputeJavaSourceDirs(java_files): |
| if basename in ('javax', 'org', 'com'): |
| path_root = os.path.dirname(path_root) |
| break |
| - found_roots.add(path_root) |
| - return list(found_roots) |
| + if path_root not in found_roots: |
| + found_roots[path_root] = [] |
| + found_roots[path_root].append(path) |
| + return found_roots |
| def _CreateRelativeSymlink(target_path, link_path): |
| @@ -298,39 +301,51 @@ def _CreateSymlinkTree(entry_output_dir, symlink_dir, desired_files, |
| _CreateRelativeSymlink(target_path, symlinked_path) |
| -def _CreateJavaSourceDir(output_dir, entry_output_dir, java_files): |
| - """Computes and constructs when necessary the list of java source directories. |
| +def _CreateJavaSourceDir(output_dir, java_files): |
| + """Computes the list of java source directories and exclude patterns. |
| 1. Computes the root java source directories from the list of files. |
| - 2. Determines whether there are any .java files in them that are not included |
| - in |java_files|. |
| - 3. If not, returns the list of java source directories. If so, constructs a |
| - tree of symlinks within |entry_output_dir| of all files in |java_files|. |
| + 2. Compute exclude patterns that exclude all extra files only. |
| + 3. Returns the list of java source directories and exclude patterns. |
| """ |
| java_dirs = [] |
| + excludes = [] |
| if java_files: |
| java_files = _RebasePath(java_files) |
| - java_dirs = _ComputeJavaSourceDirs(java_files) |
| - |
| - found_java_files = build_utils.FindInDirectories(java_dirs, '*.java') |
| - unwanted_java_files = set(found_java_files) - set(java_files) |
| - missing_java_files = set(java_files) - set(found_java_files) |
| + computed_dirs = _ComputeJavaSourceDirs(java_files) |
| + java_dirs = computed_dirs.keys() |
| + all_found_java_files = set() |
| + |
| + for directory, files in computed_dirs.iteritems(): |
| + found_java_files = build_utils.FindInDirectory(directory, '*.java') |
| + all_found_java_files.update(found_java_files) |
| + unwanted_java_files = set(found_java_files) - set(files) |
| + |
| + if unwanted_java_files: |
| + logging.debug('Target requires .java symlinks: %s, %s', |
|
agrieve
2017/02/09 10:00:38
nit: update log message to not say symlinks
Peter Wen
2017/02/09 14:38:28
Done.
|
| + directory, files) |
| + # Shorten exclude list by globbing if possible. |
|
agrieve
2017/02/09 10:00:38
Might be worth pulling this out into a helper func
Peter Wen
2017/02/09 14:38:28
Done.
|
| + while unwanted_java_files: |
| + unwanted_file = unwanted_java_files.pop() |
| + target_exclude = os.path.join( |
| + os.path.dirname(unwanted_file), '*.java') |
| + found_files = glob.glob(target_exclude) |
| + valid_files = set(found_files) & set(files) |
| + if valid_files: |
| + excludes.append(os.path.relpath(unwanted_file, directory)) |
| + else: |
| + excludes.append(os.path.relpath(target_exclude, directory)) |
| + unwanted_java_files -= set(found_files) |
|
agrieve
2017/02/09 10:00:38
you convert found_files to a set twice. Maybe just
Peter Wen
2017/02/09 14:38:28
Done.
|
| + |
| + missing_java_files = set(java_files) - set(all_found_java_files) |
| # Warn only about non-generated files that are missing. |
| missing_java_files = [p for p in missing_java_files |
| if not p.startswith(output_dir)] |
| - |
| - symlink_dir = os.path.join(entry_output_dir, _JAVA_SUBDIR) |
| - shutil.rmtree(symlink_dir, True) |
| - |
| - if unwanted_java_files: |
| - logging.debug('Target requires .java symlinks: %s', entry_output_dir) |
| - _CreateSymlinkTree(entry_output_dir, symlink_dir, java_files, java_dirs) |
| - java_dirs = [symlink_dir] |
| - |
| if missing_java_files: |
| - logging.warning('Some java files were not found: %s', missing_java_files) |
| + logging.warning( |
| + 'Some java files were not found: %s', missing_java_files) |
| - return java_dirs |
| + return java_dirs, excludes |
| def _CreateJniLibsDir(output_dir, entry_output_dir, so_files): |