OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 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 shutil | 9 import shutil |
10 import sys | 10 import sys |
11 | 11 |
12 from util import build_utils | 12 from util import build_utils |
13 | 13 |
14 | 14 |
15 _RESOURCE_CLASSES = [ | 15 _RESOURCE_CLASSES = [ |
16 "R.class", | 16 "R.class", |
17 "R##*.class", | 17 "R##*.class", |
18 "Manifest.class", | 18 "Manifest.class", |
19 "Manifest##*.class", | 19 "Manifest##*.class", |
20 ] | 20 ] |
21 | 21 |
22 | 22 |
23 def Jar(class_files, classes_dir, jar_path, manifest_file=None, | 23 def Jar(class_files, classes_dir, jar_path, manifest_file=None, |
24 provider_configurations=None): | 24 provider_configurations=None, additional_files=None): |
25 jar_path = os.path.abspath(jar_path) | 25 jar_path = os.path.abspath(jar_path) |
26 | 26 |
27 # The paths of the files in the jar will be the same as they are passed in to | 27 # The paths of the files in the jar will be the same as they are passed in to |
28 # the command. Because of this, the command should be run in | 28 # the command. Because of this, the command should be run in |
29 # options.classes_dir so the .class file paths in the jar are correct. | 29 # options.classes_dir so the .class file paths in the jar are correct. |
30 jar_cwd = classes_dir | 30 jar_cwd = classes_dir |
31 class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files] | 31 class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files] |
32 jar_cmd = ['jar', 'cf0', jar_path] | 32 jar_cmd = ['jar', 'cf0', jar_path] |
33 if manifest_file: | 33 if manifest_file: |
34 jar_cmd[1] += 'm' | 34 jar_cmd[1] += 'm' |
35 jar_cmd.append(os.path.abspath(manifest_file)) | 35 jar_cmd.append(os.path.abspath(manifest_file)) |
36 jar_cmd.extend(class_files_rel) | 36 jar_cmd.extend(class_files_rel) |
37 | 37 |
| 38 for filepath, jar_filepath in additional_files or []: |
| 39 full_jar_filepath = os.path.join(jar_cwd, jar_filepath) |
| 40 jar_dir = os.path.dirname(full_jar_filepath) |
| 41 if not os.path.exists(jar_dir): |
| 42 os.makedirs(jar_dir) |
| 43 shutil.copy(filepath, full_jar_filepath) |
| 44 jar_cmd.append(jar_filepath) |
| 45 |
38 if provider_configurations: | 46 if provider_configurations: |
39 service_dir = os.path.join(jar_cwd, 'META-INF', 'services') | 47 service_dir = os.path.join(jar_cwd, 'META-INF', 'services') |
40 if not os.path.exists(service_dir): | 48 if not os.path.exists(service_dir): |
41 os.makedirs(service_dir) | 49 os.makedirs(service_dir) |
42 for config in provider_configurations: | 50 for config in provider_configurations: |
43 config_jar_path = os.path.join(service_dir, os.path.basename(config)) | 51 config_jar_path = os.path.join(service_dir, os.path.basename(config)) |
44 shutil.copy(config, config_jar_path) | 52 shutil.copy(config, config_jar_path) |
45 jar_cmd.append(os.path.relpath(config_jar_path, jar_cwd)) | 53 jar_cmd.append(os.path.relpath(config_jar_path, jar_cwd)) |
46 | 54 |
47 if not class_files_rel: | 55 if not class_files_rel: |
48 empty_file = os.path.join(classes_dir, '.empty') | 56 empty_file = os.path.join(classes_dir, '.empty') |
49 build_utils.Touch(empty_file) | 57 build_utils.Touch(empty_file) |
50 jar_cmd.append(os.path.relpath(empty_file, jar_cwd)) | 58 jar_cmd.append(os.path.relpath(empty_file, jar_cwd)) |
51 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) | 59 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) |
52 build_utils.Touch(jar_path, fail_if_missing=True) | 60 build_utils.Touch(jar_path, fail_if_missing=True) |
53 | 61 |
54 | 62 |
55 def JarDirectory(classes_dir, jar_path, manifest_file=None, predicate=None, | 63 def JarDirectory(classes_dir, jar_path, manifest_file=None, predicate=None, |
56 provider_configurations=None): | 64 provider_configurations=None, additional_files=None): |
57 class_files = build_utils.FindInDirectory(classes_dir, '*.class') | 65 class_files = build_utils.FindInDirectory(classes_dir, '*.class') |
58 if predicate: | 66 if predicate: |
59 class_files = [f for f in class_files if predicate(f)] | 67 class_files = [f for f in class_files if predicate(f)] |
60 | 68 |
61 Jar(class_files, classes_dir, jar_path, manifest_file=manifest_file, | 69 Jar(class_files, classes_dir, jar_path, manifest_file=manifest_file, |
62 provider_configurations=provider_configurations) | 70 provider_configurations=provider_configurations, |
| 71 additional_files=additional_files) |
63 | 72 |
64 | 73 |
65 def main(): | 74 def main(): |
66 parser = optparse.OptionParser() | 75 parser = optparse.OptionParser() |
67 parser.add_option('--classes-dir', help='Directory containing .class files.') | 76 parser.add_option('--classes-dir', help='Directory containing .class files.') |
68 parser.add_option('--input-jar', help='Jar to include .class files from') | 77 parser.add_option('--input-jar', help='Jar to include .class files from') |
69 parser.add_option('--jar-path', help='Jar output path.') | 78 parser.add_option('--jar-path', help='Jar output path.') |
70 parser.add_option('--excluded-classes', | 79 parser.add_option('--excluded-classes', |
71 help='GYP list of .class file patterns to exclude from the jar.') | 80 help='GYP list of .class file patterns to exclude from the jar.') |
72 parser.add_option('--strip-resource-classes-for', | 81 parser.add_option('--strip-resource-classes-for', |
(...skipping 25 matching lines...) Expand all Loading... |
98 classes_dir = temp_dir | 107 classes_dir = temp_dir |
99 JarDirectory(classes_dir, options.jar_path, predicate=predicate) | 108 JarDirectory(classes_dir, options.jar_path, predicate=predicate) |
100 | 109 |
101 if options.stamp: | 110 if options.stamp: |
102 build_utils.Touch(options.stamp) | 111 build_utils.Touch(options.stamp) |
103 | 112 |
104 | 113 |
105 if __name__ == '__main__': | 114 if __name__ == '__main__': |
106 sys.exit(main()) | 115 sys.exit(main()) |
107 | 116 |
OLD | NEW |