| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 import optparse | 7 import optparse |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import tempfile | 10 import tempfile |
| 11 | 11 |
| 12 REPOSITORY_ROOT = os.path.abspath(os.path.join( | 12 REPOSITORY_ROOT = os.path.abspath(os.path.join( |
| 13 os.path.dirname(__file__), '..', '..', '..')) | 13 os.path.dirname(__file__), '..', '..', '..')) |
| 14 | 14 |
| 15 sys.path.append(os.path.join(REPOSITORY_ROOT, 'build/android/gyp/util')) | 15 sys.path.append(os.path.join(REPOSITORY_ROOT, 'build/android/gyp/util')) |
| 16 import build_utils | 16 import build_utils |
| 17 | 17 |
| 18 JAVA_PACKAGE_PREFIX = 'org/chromium/' |
| 18 | 19 |
| 19 def JarSources(src_dir, jar_path): | 20 def JarSources(src_dir, src_files, jar_path): |
| 20 # The paths of the files in the jar will be the same as they are passed in to | 21 # The paths of the files in the jar will be the same as they are passed in to |
| 21 # the command. Because of this, the command should be run in | 22 # the command. Because of this, the command should be run in |
| 22 # options.src_dir so the .java file paths in the jar are correct. | 23 # options.src_dir so the .java file paths in the jar are correct. |
| 23 jar_cwd = src_dir | 24 jar_cwd = src_dir |
| 24 jar_path = os.path.abspath(jar_path) | 25 jar_path = os.path.abspath(jar_path) |
| 25 if os.path.exists(jar_path): | 26 if os.path.exists(jar_path): |
| 26 jar_cmd = ['jar', 'uf', jar_path, '.'] | 27 jar_cmd = ['jar', 'uf', jar_path] |
| 27 else: | 28 else: |
| 28 jar_cmd = ['jar', 'cf', jar_path, '.'] | 29 jar_cmd = ['jar', 'cf', jar_path] |
| 29 | 30 |
| 31 jar_cmd.extend(src_files) |
| 30 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) | 32 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) |
| 31 | 33 |
| 32 # Uncompress source jars so that they can be combined with other sources | 34 # Uncompress source jars so that they can be combined with other sources |
| 33 def UnzipSourceJar(jar, unzipped_jar_path): | 35 def UnzipSourceJar(jar, unzipped_jar_path): |
| 34 if os.path.exists(jar): | 36 if os.path.exists(jar): |
| 35 jar_cmd = ['jar', 'xf', os.path.abspath(jar)] | 37 jar_cmd = ['jar', 'xf', os.path.abspath(jar)] |
| 36 build_utils.CheckOutput(jar_cmd, cwd=unzipped_jar_path) | 38 build_utils.CheckOutput(jar_cmd, cwd=unzipped_jar_path) |
| 37 else: | 39 else: |
| 38 raise Exception('Jar file does not exist %s' % jar) | 40 raise Exception('Jar file does not exist %s' % jar) |
| 39 | 41 |
| 40 | 42 |
| 41 def main(): | 43 def main(): |
| 42 parser = optparse.OptionParser() | 44 parser = optparse.OptionParser() |
| 43 build_utils.AddDepfileOption(parser) | 45 build_utils.AddDepfileOption(parser) |
| 44 parser.add_option('--src-dir', action="append", | 46 parser.add_option('--src-search-dirs', action="append", |
| 45 help='Directory containing .java files.') | 47 help='A list of directories that should be searched' |
| 48 ' for the source files.') |
| 49 parser.add_option('--src-files', action="append", |
| 50 help='A list of source files to jar.') |
| 46 parser.add_option('--src-jars', action="append", | 51 parser.add_option('--src-jars', action="append", |
| 47 help='A list of source jars to include in addition to source files.') | 52 help='A list of source jars to include in addition to source files.') |
| 53 parser.add_option('--src-list-files', action="append", |
| 54 help='A list of files that contain a list of sources,' |
| 55 ' e.g. a list of \'.sources\' files generated by GN.') |
| 48 parser.add_option('--jar-path', help='Jar output path.') | 56 parser.add_option('--jar-path', help='Jar output path.') |
| 49 parser.add_option('--stamp', help='Path to touch on success.') | 57 parser.add_option('--stamp', help='Path to touch on success.') |
| 50 | 58 |
| 51 options, _ = parser.parse_args() | 59 options, _ = parser.parse_args() |
| 52 | 60 |
| 53 # A temporary directory to put the output of jar files. | 61 # A temporary directory to put the output of jar files. |
| 54 unzipped_jar_path = None | 62 unzipped_jar_path = None |
| 55 generated_src_dirs = [] | |
| 56 if options.src_jars: | 63 if options.src_jars: |
| 57 unzipped_jar_path = tempfile.mkdtemp(dir=os.path.dirname(options.jar_path)) | 64 unzipped_jar_path = tempfile.mkdtemp(dir=os.path.dirname(options.jar_path)) |
| 58 generated_src_dirs.append(unzipped_jar_path) | |
| 59 jar_list = [] | 65 jar_list = [] |
| 60 for gn_list in options.src_jars: | 66 for gn_list in options.src_jars: |
| 61 jar_list.extend(build_utils.ParseGnList(gn_list)) | 67 jar_list.extend(build_utils.ParseGnList(gn_list)) |
| 62 | 68 |
| 63 for jar in jar_list: | 69 for jar in jar_list: |
| 64 UnzipSourceJar(jar, unzipped_jar_path) | 70 UnzipSourceJar(jar, unzipped_jar_path) |
| 65 | 71 |
| 66 src_dirs = [] | 72 src_search_dirs = [] |
| 67 for src_dir in options.src_dir: | 73 for gn_src_search_dirs in options.src_search_dirs: |
| 68 src_dirs.extend(build_utils.ParseGnList(src_dir)) | 74 src_search_dirs.extend(build_utils.ParseGnList(gn_src_search_dirs)) |
| 69 | 75 |
| 70 for src_dir in src_dirs + generated_src_dirs: | 76 src_list_files = [] |
| 71 JarSources(src_dir, options.jar_path) | 77 if options.src_list_files: |
| 78 for gn_src_list_file in options.src_list_files: |
| 79 src_list_files.extend(build_utils.ParseGnList(gn_src_list_file)) |
| 80 |
| 81 src_files = [] |
| 82 for gn_src_files in options.src_files: |
| 83 src_files.extend(build_utils.ParseGnList(gn_src_files)) |
| 84 |
| 85 # Add files from --source_list_files |
| 86 for src_list_file in src_list_files: |
| 87 with open(src_list_file, 'r') as f: |
| 88 src_files.extend(f.read().splitlines()) |
| 89 |
| 90 # Preprocess source files by removing any prefix that comes before |
| 91 # the Java package name. |
| 92 for i, s in enumerate(src_files): |
| 93 prefix_position = s.find(JAVA_PACKAGE_PREFIX) |
| 94 if prefix_position != -1: |
| 95 src_files[i] = s[prefix_position:] |
| 96 |
| 97 # Create a dictionary that maps every source directory |
| 98 # to source files that it contains. |
| 99 dir_to_files_map = {} |
| 100 # Initialize the map. |
| 101 for src_search_dir in src_search_dirs: |
| 102 dir_to_files_map[src_search_dir] = [] |
| 103 # Fill the map. |
| 104 for src_file in src_files: |
| 105 number_of_file_instances = 0 |
| 106 for src_search_dir in src_search_dirs: |
| 107 if os.path.isfile(os.path.join(src_search_dir, src_file)): |
| 108 number_of_file_instances += 1 |
| 109 dir_to_files_map[src_search_dir].append(src_file) |
| 110 if (number_of_file_instances > 1): |
| 111 raise Exception( |
| 112 'There is more than one instance of file %s in %s' |
| 113 % (src_file, src_search_dirs)) |
| 114 if (number_of_file_instances < 1): |
| 115 raise Exception( |
| 116 'Unable to find file %s in %s' % (src_file, src_search_dirs)) |
| 117 |
| 118 # Jar the sources from every source search directory. |
| 119 for src_search_dir in src_search_dirs: |
| 120 if len(dir_to_files_map[src_search_dir]) > 0: |
| 121 JarSources(src_search_dir, dir_to_files_map[src_search_dir], |
| 122 options.jar_path) |
| 123 else: |
| 124 raise Exception( |
| 125 'Directory %s does not contain any files and can be' |
| 126 ' removed from the list of directories to search' % src_search_dir) |
| 127 |
| 128 # Jar additional src jars |
| 129 if unzipped_jar_path: |
| 130 JarSources(unzipped_jar_path, ['.'], options.jar_path) |
| 72 | 131 |
| 73 if options.depfile: | 132 if options.depfile: |
| 74 deps = [] | 133 deps = [] |
| 75 for src_dir in src_dirs: | 134 for src_dir in src_search_dirs: |
| 76 for root, _, filenames in os.walk(src_dir): | 135 for root, _, filenames in os.walk(src_dir): |
| 77 deps.extend(os.path.join(root, f) for f in filenames) | 136 deps.extend(os.path.join(root, f) for f in filenames) |
| 78 # Srcjar deps already captured in GN rules (no need to list them here). | 137 # Srcjar deps already captured in GN rules (no need to list them here). |
| 79 build_utils.WriteDepfile(options.depfile, options.jar_path, deps) | 138 build_utils.WriteDepfile(options.depfile, options.jar_path, deps) |
| 80 | 139 |
| 81 # Clean up temporary output directory. | 140 # Clean up temporary output directory. |
| 82 if unzipped_jar_path: | 141 if unzipped_jar_path: |
| 83 build_utils.DeleteDirectory(unzipped_jar_path) | 142 build_utils.DeleteDirectory(unzipped_jar_path) |
| 84 | 143 |
| 85 if options.stamp: | 144 if options.stamp: |
| 86 build_utils.Touch(options.stamp) | 145 build_utils.Touch(options.stamp) |
| 87 | 146 |
| 88 if __name__ == '__main__': | 147 if __name__ == '__main__': |
| 89 sys.exit(main()) | 148 sys.exit(main()) |
| 90 | 149 |
| OLD | NEW |