Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(503)

Side by Side Diff: components/cronet/tools/jar_src.py

Issue 2347233002: Add src_files to src_jar GN template (Closed)
Patch Set: Added source_deps that packages sources from the deps .sources files. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.')
48 parser.add_option('--jar-path', help='Jar output path.') 53 parser.add_option('--jar-path', help='Jar output path.')
49 parser.add_option('--stamp', help='Path to touch on success.') 54 parser.add_option('--stamp', help='Path to touch on success.')
50 55
51 options, _ = parser.parse_args() 56 options, _ = parser.parse_args()
52 57
53 # A temporary directory to put the output of jar files. 58 # A temporary directory to put the output of jar files.
54 unzipped_jar_path = None 59 unzipped_jar_path = None
55 generated_src_dirs = []
56 if options.src_jars: 60 if options.src_jars:
57 unzipped_jar_path = tempfile.mkdtemp(dir=os.path.dirname(options.jar_path)) 61 unzipped_jar_path = tempfile.mkdtemp(dir=os.path.dirname(options.jar_path))
58 generated_src_dirs.append(unzipped_jar_path)
59 jar_list = [] 62 jar_list = []
60 for gn_list in options.src_jars: 63 for gn_list in options.src_jars:
61 jar_list.extend(build_utils.ParseGnList(gn_list)) 64 jar_list.extend(build_utils.ParseGnList(gn_list))
62 65
63 for jar in jar_list: 66 for jar in jar_list:
64 UnzipSourceJar(jar, unzipped_jar_path) 67 UnzipSourceJar(jar, unzipped_jar_path)
65 68
66 src_dirs = [] 69 src_search_dirs = []
67 for src_dir in options.src_dir: 70 for gn_src_search_dirs in options.src_search_dirs:
68 src_dirs.extend(build_utils.ParseGnList(src_dir)) 71 src_search_dirs.extend(build_utils.ParseGnList(gn_src_search_dirs))
69 72
70 for src_dir in src_dirs + generated_src_dirs: 73 src_files = []
71 JarSources(src_dir, options.jar_path) 74 for gn_src_files in options.src_files:
75 src_files.extend(build_utils.ParseGnList(gn_src_files))
76
77 # Preprocess source files by removing any prefix that comes before
78 # the Java package name.
79 for i, s in enumerate(src_files):
80 prefix_position = s.find(JAVA_PACKAGE_PREFIX)
81 if prefix_position != -1:
82 src_files[i] = s[prefix_position:]
83
84 # Create a dictionary that maps every source directory
85 # to source files that it contains.
86 dir_to_files_map = {}
87 # Initialize the map.
88 for src_search_dir in src_search_dirs:
89 dir_to_files_map[src_search_dir] = []
90 # Fill the map.
91 for src_file in src_files:
92 number_of_file_instances = 0
93 for src_search_dir in src_search_dirs:
94 if os.path.isfile(os.path.join(src_search_dir, src_file)):
95 number_of_file_instances += 1
96 dir_to_files_map[src_search_dir].append(src_file)
97 if (number_of_file_instances > 1):
98 raise Exception(
99 'There is more than one instance of file %s in %s'
100 % (src_file, src_search_dirs))
101 if (number_of_file_instances < 1):
102 raise Exception(
103 'Unable to find file %s in %s' % (src_file, src_search_dirs))
104
105 # Jar the sources from every source search directory.
106 for src_search_dir in src_search_dirs:
107 if len(dir_to_files_map[src_search_dir]) > 0:
108 JarSources(src_search_dir, dir_to_files_map[src_search_dir],
109 options.jar_path)
110 else:
111 raise Exception(
112 'Directory %s does not contain any files and can be'
113 'removed from the list of directories to search' % src_search_dir)
114
115 # Jar additional src jars
116 if unzipped_jar_path:
117 JarSources(unzipped_jar_path, ['.'], options.jar_path)
72 118
73 if options.depfile: 119 if options.depfile:
74 deps = [] 120 deps = []
75 for src_dir in src_dirs: 121 for src_dir in src_search_dirs:
76 for root, _, filenames in os.walk(src_dir): 122 for root, _, filenames in os.walk(src_dir):
77 deps.extend(os.path.join(root, f) for f in filenames) 123 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). 124 # Srcjar deps already captured in GN rules (no need to list them here).
79 build_utils.WriteDepfile(options.depfile, options.jar_path, deps) 125 build_utils.WriteDepfile(options.depfile, options.jar_path, deps)
80 126
81 # Clean up temporary output directory. 127 # Clean up temporary output directory.
82 if unzipped_jar_path: 128 if unzipped_jar_path:
83 build_utils.DeleteDirectory(unzipped_jar_path) 129 build_utils.DeleteDirectory(unzipped_jar_path)
84 130
85 if options.stamp: 131 if options.stamp:
86 build_utils.Touch(options.stamp) 132 build_utils.Touch(options.stamp)
87 133
88 if __name__ == '__main__': 134 if __name__ == '__main__':
89 sys.exit(main()) 135 sys.exit(main())
90 136
OLDNEW
« components/cronet/android/BUILD.gn ('K') | « components/cronet/android/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698