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] | 66 command = [javac, '-J-Xmx256m'] |
67 | 67 |
68 # Use a large enough heap to be able to compile all of the classes in one | 68 # Use a large enough heap to be able to compile all of the classes in one |
ricow1
2014/06/27 07:35:34
this should go up above the command generation
| |
69 # big compilation step. | 69 # big compilation step. |
70 command.append('-J-Xmx256m') | |
71 | 70 |
72 if buildConfig == 'Debug': | 71 if buildConfig == 'Debug': |
73 command.append('-g') | 72 command.append('-g') |
74 | 73 |
75 if srcDirectories: | 74 if srcDirectories: |
76 command.append('-sourcepath') | 75 command.append('-sourcepath') |
77 command.append(os.pathsep.join(srcDirectories)) | 76 command.append(os.pathsep.join(srcDirectories)) |
78 | 77 |
79 if classpath: | 78 if classpath: |
80 command.append('-classpath') | 79 command.append('-classpath') |
(...skipping 14 matching lines...) Expand all Loading... | |
95 # Compile. | 94 # Compile. |
96 sys.stdout.write(' \\\n '.join(command) + '\n') | 95 sys.stdout.write(' \\\n '.join(command) + '\n') |
97 if os.system(' '.join(command)): | 96 if os.system(' '.join(command)): |
98 sys.stderr.write('java compilation failed\n') | 97 sys.stderr.write('java compilation failed\n') |
99 return False | 98 return False |
100 return True | 99 return True |
101 finally: | 100 finally: |
102 os.remove(javaFilesTempFileName) | 101 os.remove(javaFilesTempFileName) |
103 | 102 |
104 def copyProperties(propertyFiles, classOutputDir): | 103 def copyProperties(propertyFiles, classOutputDir): |
105 for file in propertyFiles: | 104 for property_file in propertyFiles: |
106 if not os.path.isfile(file): | 105 if not os.path.isfile(property_file): |
107 sys.stderr.write('property file not found: ' + file + '\n') | 106 sys.stderr.write('property file not found: ' + property_file + '\n') |
108 return False | 107 return False |
109 | 108 |
110 if not os.path.exists(classOutputDir): | 109 if not os.path.exists(classOutputDir): |
111 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') | 110 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') |
112 return False | 111 return False |
113 | 112 |
114 if propertyFiles == []: | 113 if not propertyFiles: |
115 return True | 114 return True |
116 | 115 |
117 command = ['cp'] + propertyFiles + [classOutputDir] | 116 command = ['cp'] + propertyFiles + [classOutputDir] |
118 commandStr = ' '.join(command) | 117 commandStr = ' '.join(command) |
119 sys.stdout.write(commandStr + '\n') | 118 sys.stdout.write(commandStr + '\n') |
120 if os.system(commandStr): | 119 if os.system(commandStr): |
121 sys.stderr.write('property file copy failed\n') | 120 sys.stderr.write('property file copy failed\n') |
122 return False | 121 return False |
123 return True | 122 return True |
124 | 123 |
125 def createJar(classOutputDir, jarFileName): | 124 def createJar(classOutputDir, jarFileName): |
126 if not os.path.exists(classOutputDir): | 125 if not os.path.exists(classOutputDir): |
127 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') | 126 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') |
128 return False | 127 return False |
129 | 128 |
130 command = ['jar', 'cf', jarFileName, '-C', classOutputDir, '.'] | 129 command = ['jar', 'cf', jarFileName, '-C', classOutputDir, '.'] |
131 commandStr = ' '.join(command) | 130 commandStr = ' '.join(command) |
132 sys.stdout.write(commandStr + '\n') | 131 sys.stdout.write(commandStr + '\n') |
133 if os.system(commandStr): | 132 if os.system(commandStr): |
134 sys.stderr.write('jar creation failed\n') | 133 sys.stderr.write('jar creation failed\n') |
135 return False | 134 return False |
136 return True | 135 return True |
137 | 136 |
138 | 137 |
139 def main(args): | 138 def main(): |
140 try: | 139 try: |
141 # Parse input. | 140 # Parse input. |
142 parser = OptionParser() | 141 parser = OptionParser() |
143 parser.add_option("--javac", default="javac", | 142 parser.add_option("--javac", default="javac", |
144 action="store", type="string", | 143 action="store", type="string", |
145 help="location of javac command") | 144 help="location of javac command") |
146 parser.add_option("--sourceDir", dest="sourceDirs", default=[], | 145 parser.add_option("--sourceDir", dest="sourceDirs", default=[], |
147 action="callback", callback=ListArgCallback, | 146 action="callback", callback=ListArgCallback, |
148 help="specify a list of directories to look for " + | 147 help="specify a list of directories to look for " + |
149 ".java files to compile") | 148 ".java files to compile") |
(...skipping 23 matching lines...) Expand all Loading... | |
173 help="specify a list of property files to copy") | 172 help="specify a list of property files to copy") |
174 | 173 |
175 (options, args) = parser.parse_args() | 174 (options, args) = parser.parse_args() |
176 if not options.classesDir: | 175 if not options.classesDir: |
177 sys.stderr.write('--classesDir not specified\n') | 176 sys.stderr.write('--classesDir not specified\n') |
178 return -1 | 177 return -1 |
179 if not options.jarFileName: | 178 if not options.jarFileName: |
180 sys.stderr.write('--jarFileName not specified\n') | 179 sys.stderr.write('--jarFileName not specified\n') |
181 return -1 | 180 return -1 |
182 if len(options.sourceDirs) > 0 and options.sourceList: | 181 if len(options.sourceDirs) > 0 and options.sourceList: |
183 sys.stderr.write("--sourceDir and --sourceList cannot be both specified"); | 182 sys.stderr.write("--sourceDir and --sourceList cannot be both specified") |
184 return -1 | 183 return -1 |
185 | 184 |
186 # Compile and put into .jar file. | 185 # Compile and put into .jar file. |
187 if not javaCompile(options.javac, options.sourceDirs, | 186 if not javaCompile(options.javac, options.sourceDirs, |
188 options.sourceList, options.classpath, | 187 options.sourceList, options.classpath, |
189 options.classesDir, options.buildConfig, | 188 options.classesDir, options.buildConfig, |
190 options.javacArgs, options.sources): | 189 options.javacArgs, options.sources): |
191 return -1 | 190 return -1 |
192 if not copyProperties(options.propertyFiles, options.classesDir): | 191 if not copyProperties(options.propertyFiles, options.classesDir): |
193 return -1 | 192 return -1 |
194 if not createJar(options.classesDir, options.jarFileName): | 193 if not createJar(options.classesDir, options.jarFileName): |
195 return -1 | 194 return -1 |
196 | 195 |
197 return 0 | 196 return 0 |
198 except Exception, inst: | 197 except Exception, inst: |
199 sys.stderr.write('compile_java.py exception\n') | 198 sys.stderr.write('compile_java.py exception\n') |
200 sys.stderr.write(str(inst)) | 199 sys.stderr.write(str(inst)) |
201 return -1 | 200 return -1 |
202 | 201 |
203 if __name__ == '__main__': | 202 if __name__ == '__main__': |
204 sys.exit(main(sys.argv)) | 203 sys.exit(main()) |
OLD | NEW |