OLD | NEW |
1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 # for details. All rights reserved. Use of this source code is governed by a | 2 # for details. All rights reserved. Use of this source code is governed by a |
3 # BSD-style license that can be found in the LICENSE file. | 3 # BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 # This python script compiles a set of java files and puts them all into a | 5 # This python script compiles a set of java files and puts them all into a |
6 # single .jar file. | 6 # single .jar file. |
7 | 7 |
8 import os | 8 import os |
9 import shutil | 9 import shutil |
10 import sys | 10 import sys |
11 import tempfile | 11 import tempfile |
12 from optparse import OptionParser | 12 from optparse import OptionParser |
13 | 13 |
14 # Filters out all arguments until the next '--' argument | 14 # Filters out all arguments until the next '--' argument |
15 # occurs. | 15 # occurs. |
16 def ListArgCallback(option, opt_str, value, parser): | 16 def ListArgCallback(option, value, parser): |
17 if value is None: | 17 if value is None: |
18 value = [] | 18 value = [] |
19 | 19 |
20 for arg in parser.rargs: | 20 for arg in parser.rargs: |
21 if arg[:2].startswith('--'): | 21 if arg[:2].startswith('--'): |
22 break | 22 break |
23 value.append(arg) | 23 value.append(arg) |
24 | 24 |
25 del parser.rargs[:len(value)] | 25 del parser.rargs[:len(value)] |
26 setattr(parser.values, option.dest, value) | 26 setattr(parser.values, option.dest, value) |
(...skipping 11 matching lines...) Expand all Loading... |
38 | 38 |
39 if os.path.exists(classOutputDir): | 39 if os.path.exists(classOutputDir): |
40 shutil.rmtree(classOutputDir) | 40 shutil.rmtree(classOutputDir) |
41 os.makedirs(classOutputDir) | 41 os.makedirs(classOutputDir) |
42 | 42 |
43 # Find all java files and write them in a temp file. | 43 # Find all java files and write them in a temp file. |
44 (tempFileDescriptor, javaFilesTempFileName) = tempfile.mkstemp() | 44 (tempFileDescriptor, javaFilesTempFileName) = tempfile.mkstemp() |
45 javaFilesTempFile = os.fdopen(tempFileDescriptor, "w") | 45 javaFilesTempFile = os.fdopen(tempFileDescriptor, "w") |
46 try: | 46 try: |
47 if srcDirectories: | 47 if srcDirectories: |
48 def findJavaFiles(arg, dirName, names): | 48 def findJavaFiles(dirName, names): |
49 for fileName in names: | 49 for fileName in names: |
50 (base, ext) = os.path.splitext(fileName) | 50 (base, ext) = os.path.splitext(fileName) |
51 if ext == '.java': | 51 if ext == '.java': |
52 javaFilesTempFile.write(os.path.join(dirName, fileName) + '\n') | 52 javaFilesTempFile.write(os.path.join(dirName, fileName) + '\n') |
53 for srcDir in srcDirectories: | 53 for srcDir in srcDirectories: |
54 os.path.walk(srcDir, findJavaFiles, None) | 54 os.path.walk(srcDir, findJavaFiles, None) |
55 | 55 |
56 if srcList: | 56 if srcList: |
57 f = open(srcList, 'r') | 57 f = open(srcList, 'r') |
58 for line in f: | 58 for line in f: |
59 javaFilesTempFile.write(os.path.abspath(line)) | 59 javaFilesTempFile.write(os.path.abspath(line)) |
60 f.close() | 60 f.close() |
61 | 61 |
62 javaFilesTempFile.flush() | 62 javaFilesTempFile.flush() |
63 javaFilesTempFile.close() | 63 javaFilesTempFile.close() |
64 | 64 |
65 # Prepare javac command. | 65 # Prepare javac command. |
66 command = [javac] | |
67 | |
68 # Use a large enough heap to be able to compile all of the classes in one | 66 # Use a large enough heap to be able to compile all of the classes in one |
69 # big compilation step. | 67 # big compilation step. |
70 command.append('-J-Xmx256m') | 68 command = [javac, '-J-Xmx256m'] |
71 | 69 |
72 if buildConfig == 'Debug': | 70 if buildConfig == 'Debug': |
73 command.append('-g') | 71 command.append('-g') |
74 | 72 |
75 if srcDirectories: | 73 if srcDirectories: |
76 command.append('-sourcepath') | 74 command.append('-sourcepath') |
77 command.append(os.pathsep.join(srcDirectories)) | 75 command.append(os.pathsep.join(srcDirectories)) |
78 | 76 |
79 if classpath: | 77 if classpath: |
80 command.append('-classpath') | 78 command.append('-classpath') |
(...skipping 14 matching lines...) Expand all Loading... |
95 # Compile. | 93 # Compile. |
96 sys.stdout.write(' \\\n '.join(command) + '\n') | 94 sys.stdout.write(' \\\n '.join(command) + '\n') |
97 if os.system(' '.join(command)): | 95 if os.system(' '.join(command)): |
98 sys.stderr.write('java compilation failed\n') | 96 sys.stderr.write('java compilation failed\n') |
99 return False | 97 return False |
100 return True | 98 return True |
101 finally: | 99 finally: |
102 os.remove(javaFilesTempFileName) | 100 os.remove(javaFilesTempFileName) |
103 | 101 |
104 def copyProperties(propertyFiles, classOutputDir): | 102 def copyProperties(propertyFiles, classOutputDir): |
105 for file in propertyFiles: | 103 for property_file in propertyFiles: |
106 if not os.path.isfile(file): | 104 if not os.path.isfile(property_file): |
107 sys.stderr.write('property file not found: ' + file + '\n') | 105 sys.stderr.write('property file not found: ' + property_file + '\n') |
108 return False | 106 return False |
109 | 107 |
110 if not os.path.exists(classOutputDir): | 108 if not os.path.exists(classOutputDir): |
111 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') | 109 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') |
112 return False | 110 return False |
113 | 111 |
114 if propertyFiles == []: | 112 if not propertyFiles: |
115 return True | 113 return True |
116 | 114 |
117 command = ['cp'] + propertyFiles + [classOutputDir] | 115 command = ['cp'] + propertyFiles + [classOutputDir] |
118 commandStr = ' '.join(command) | 116 commandStr = ' '.join(command) |
119 sys.stdout.write(commandStr + '\n') | 117 sys.stdout.write(commandStr + '\n') |
120 if os.system(commandStr): | 118 if os.system(commandStr): |
121 sys.stderr.write('property file copy failed\n') | 119 sys.stderr.write('property file copy failed\n') |
122 return False | 120 return False |
123 return True | 121 return True |
124 | 122 |
125 def createJar(classOutputDir, jarFileName): | 123 def createJar(classOutputDir, jarFileName): |
126 if not os.path.exists(classOutputDir): | 124 if not os.path.exists(classOutputDir): |
127 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') | 125 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') |
128 return False | 126 return False |
129 | 127 |
130 command = ['jar', 'cf', jarFileName, '-C', classOutputDir, '.'] | 128 command = ['jar', 'cf', jarFileName, '-C', classOutputDir, '.'] |
131 commandStr = ' '.join(command) | 129 commandStr = ' '.join(command) |
132 sys.stdout.write(commandStr + '\n') | 130 sys.stdout.write(commandStr + '\n') |
133 if os.system(commandStr): | 131 if os.system(commandStr): |
134 sys.stderr.write('jar creation failed\n') | 132 sys.stderr.write('jar creation failed\n') |
135 return False | 133 return False |
136 return True | 134 return True |
137 | 135 |
138 | 136 |
139 def main(args): | 137 def main(): |
140 try: | 138 try: |
141 # Parse input. | 139 # Parse input. |
142 parser = OptionParser() | 140 parser = OptionParser() |
143 parser.add_option("--javac", default="javac", | 141 parser.add_option("--javac", default="javac", |
144 action="store", type="string", | 142 action="store", type="string", |
145 help="location of javac command") | 143 help="location of javac command") |
146 parser.add_option("--sourceDir", dest="sourceDirs", default=[], | 144 parser.add_option("--sourceDir", dest="sourceDirs", default=[], |
147 action="callback", callback=ListArgCallback, | 145 action="callback", callback=ListArgCallback, |
148 help="specify a list of directories to look for " + | 146 help="specify a list of directories to look for " + |
149 ".java files to compile") | 147 ".java files to compile") |
(...skipping 23 matching lines...) Expand all Loading... |
173 help="specify a list of property files to copy") | 171 help="specify a list of property files to copy") |
174 | 172 |
175 (options, args) = parser.parse_args() | 173 (options, args) = parser.parse_args() |
176 if not options.classesDir: | 174 if not options.classesDir: |
177 sys.stderr.write('--classesDir not specified\n') | 175 sys.stderr.write('--classesDir not specified\n') |
178 return -1 | 176 return -1 |
179 if not options.jarFileName: | 177 if not options.jarFileName: |
180 sys.stderr.write('--jarFileName not specified\n') | 178 sys.stderr.write('--jarFileName not specified\n') |
181 return -1 | 179 return -1 |
182 if len(options.sourceDirs) > 0 and options.sourceList: | 180 if len(options.sourceDirs) > 0 and options.sourceList: |
183 sys.stderr.write("--sourceDir and --sourceList cannot be both specified"); | 181 sys.stderr.write("--sourceDir and --sourceList cannot be both specified") |
184 return -1 | 182 return -1 |
185 | 183 |
186 # Compile and put into .jar file. | 184 # Compile and put into .jar file. |
187 if not javaCompile(options.javac, options.sourceDirs, | 185 if not javaCompile(options.javac, options.sourceDirs, |
188 options.sourceList, options.classpath, | 186 options.sourceList, options.classpath, |
189 options.classesDir, options.buildConfig, | 187 options.classesDir, options.buildConfig, |
190 options.javacArgs, options.sources): | 188 options.javacArgs, options.sources): |
191 return -1 | 189 return -1 |
192 if not copyProperties(options.propertyFiles, options.classesDir): | 190 if not copyProperties(options.propertyFiles, options.classesDir): |
193 return -1 | 191 return -1 |
194 if not createJar(options.classesDir, options.jarFileName): | 192 if not createJar(options.classesDir, options.jarFileName): |
195 return -1 | 193 return -1 |
196 | 194 |
197 return 0 | 195 return 0 |
198 except Exception, inst: | 196 except Exception, inst: |
199 sys.stderr.write('compile_java.py exception\n') | 197 sys.stderr.write('compile_java.py exception\n') |
200 sys.stderr.write(str(inst)) | 198 sys.stderr.write(str(inst)) |
201 return -1 | 199 return -1 |
202 | 200 |
203 if __name__ == '__main__': | 201 if __name__ == '__main__': |
204 sys.exit(main(sys.argv)) | 202 sys.exit(main()) |
OLD | NEW |