OLD | NEW |
| (Empty) |
1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 # This python script compiles a set of java files and puts them all into a | |
6 # single .jar file. | |
7 | |
8 import os | |
9 import shutil | |
10 import sys | |
11 import tempfile | |
12 from optparse import OptionParser | |
13 | |
14 # Filters out all arguments until the next '--' argument | |
15 # occurs. | |
16 def ListArgCallback(option, value, parser): | |
17 if value is None: | |
18 value = [] | |
19 | |
20 for arg in parser.rargs: | |
21 if arg[:2].startswith('--'): | |
22 break | |
23 value.append(arg) | |
24 | |
25 del parser.rargs[:len(value)] | |
26 setattr(parser.values, option.dest, value) | |
27 | |
28 | |
29 # Compiles all the java source files in srcDir. | |
30 def javaCompile(javac, srcDirectories, srcList, classpath, | |
31 classOutputDir, buildConfig, javacArgs, | |
32 sources): | |
33 # Check & prepare directories. | |
34 for srcDir in srcDirectories: | |
35 if not os.path.exists(srcDir): | |
36 sys.stderr.write('source directory not found: ' + srcDir + '\n') | |
37 return False | |
38 | |
39 if os.path.exists(classOutputDir): | |
40 shutil.rmtree(classOutputDir) | |
41 os.makedirs(classOutputDir) | |
42 | |
43 # Find all java files and write them in a temp file. | |
44 (tempFileDescriptor, javaFilesTempFileName) = tempfile.mkstemp() | |
45 javaFilesTempFile = os.fdopen(tempFileDescriptor, "w") | |
46 try: | |
47 if srcDirectories: | |
48 def findJavaFiles(dirName, names): | |
49 for fileName in names: | |
50 (base, ext) = os.path.splitext(fileName) | |
51 if ext == '.java': | |
52 javaFilesTempFile.write(os.path.join(dirName, fileName) + '\n') | |
53 for srcDir in srcDirectories: | |
54 os.path.walk(srcDir, findJavaFiles, None) | |
55 | |
56 if srcList: | |
57 f = open(srcList, 'r') | |
58 for line in f: | |
59 javaFilesTempFile.write(os.path.abspath(line)) | |
60 f.close() | |
61 | |
62 javaFilesTempFile.flush() | |
63 javaFilesTempFile.close() | |
64 | |
65 # Prepare javac command. | |
66 # Use a large enough heap to be able to compile all of the classes in one | |
67 # big compilation step. | |
68 command = [javac, '-J-Xmx256m'] | |
69 | |
70 if buildConfig == 'Debug': | |
71 command.append('-g') | |
72 | |
73 if srcDirectories: | |
74 command.append('-sourcepath') | |
75 command.append(os.pathsep.join(srcDirectories)) | |
76 | |
77 if classpath: | |
78 command.append('-classpath') | |
79 command.append(os.pathsep.join(classpath)) | |
80 | |
81 command.append('-d') | |
82 command.append(classOutputDir) | |
83 | |
84 if srcDirectories or srcList: | |
85 command.append('@' + javaFilesTempFileName) | |
86 | |
87 command = command + javacArgs | |
88 | |
89 abs_sources = [os.path.abspath(source) for source in sources] | |
90 | |
91 command = [' '.join(command)] + abs_sources | |
92 | |
93 # Compile. | |
94 sys.stdout.write(' \\\n '.join(command) + '\n') | |
95 if os.system(' '.join(command)): | |
96 sys.stderr.write('java compilation failed\n') | |
97 return False | |
98 return True | |
99 finally: | |
100 os.remove(javaFilesTempFileName) | |
101 | |
102 def copyProperties(propertyFiles, classOutputDir): | |
103 for property_file in propertyFiles: | |
104 if not os.path.isfile(property_file): | |
105 sys.stderr.write('property file not found: ' + property_file + '\n') | |
106 return False | |
107 | |
108 if not os.path.exists(classOutputDir): | |
109 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') | |
110 return False | |
111 | |
112 if not propertyFiles: | |
113 return True | |
114 | |
115 command = ['cp'] + propertyFiles + [classOutputDir] | |
116 commandStr = ' '.join(command) | |
117 sys.stdout.write(commandStr + '\n') | |
118 if os.system(commandStr): | |
119 sys.stderr.write('property file copy failed\n') | |
120 return False | |
121 return True | |
122 | |
123 def createJar(classOutputDir, jarFileName): | |
124 if not os.path.exists(classOutputDir): | |
125 sys.stderr.write('classes dir not found: ' + classOutputDir + '\n') | |
126 return False | |
127 | |
128 command = ['jar', 'cf', jarFileName, '-C', classOutputDir, '.'] | |
129 commandStr = ' '.join(command) | |
130 sys.stdout.write(commandStr + '\n') | |
131 if os.system(commandStr): | |
132 sys.stderr.write('jar creation failed\n') | |
133 return False | |
134 return True | |
135 | |
136 | |
137 def main(): | |
138 try: | |
139 # Parse input. | |
140 parser = OptionParser() | |
141 parser.add_option("--javac", default="javac", | |
142 action="store", type="string", | |
143 help="location of javac command") | |
144 parser.add_option("--sourceDir", dest="sourceDirs", default=[], | |
145 action="callback", callback=ListArgCallback, | |
146 help="specify a list of directories to look for " + | |
147 ".java files to compile") | |
148 parser.add_option("--sources", dest="sources", default=[], | |
149 action="callback", callback=ListArgCallback, | |
150 help="specify a list of source files to compile") | |
151 parser.add_option("--sourceList", dest="sourceList", | |
152 action="store", type="string", | |
153 help="specify the file that contains the list of Java sour
ce files") | |
154 parser.add_option("--classpath", dest="classpath", default=[], | |
155 action="append", type="string", | |
156 help="specify referenced jar files") | |
157 parser.add_option("--classesDir", | |
158 action="store", type="string", | |
159 help="location of intermediate .class files") | |
160 parser.add_option("--jarFileName", | |
161 action="store", type="string", | |
162 help="name of the output jar file") | |
163 parser.add_option("--buildConfig", | |
164 action="store", type="string", default='Release', | |
165 help="Debug or Release") | |
166 parser.add_option("--javacArgs", dest="javacArgs", default=[], | |
167 action="callback", callback=ListArgCallback, | |
168 help="remaining args are passed directly to javac") | |
169 parser.add_option("--propertyFiles", dest="propertyFiles", default=[], | |
170 action="callback", callback=ListArgCallback, | |
171 help="specify a list of property files to copy") | |
172 | |
173 (options, args) = parser.parse_args() | |
174 if not options.classesDir: | |
175 sys.stderr.write('--classesDir not specified\n') | |
176 return -1 | |
177 if not options.jarFileName: | |
178 sys.stderr.write('--jarFileName not specified\n') | |
179 return -1 | |
180 if len(options.sourceDirs) > 0 and options.sourceList: | |
181 sys.stderr.write("--sourceDir and --sourceList cannot be both specified") | |
182 return -1 | |
183 | |
184 # Compile and put into .jar file. | |
185 if not javaCompile(options.javac, options.sourceDirs, | |
186 options.sourceList, options.classpath, | |
187 options.classesDir, options.buildConfig, | |
188 options.javacArgs, options.sources): | |
189 return -1 | |
190 if not copyProperties(options.propertyFiles, options.classesDir): | |
191 return -1 | |
192 if not createJar(options.classesDir, options.jarFileName): | |
193 return -1 | |
194 | |
195 return 0 | |
196 except Exception, inst: | |
197 sys.stderr.write('compile_java.py exception\n') | |
198 sys.stderr.write(str(inst)) | |
199 return -1 | |
200 | |
201 if __name__ == '__main__': | |
202 sys.exit(main()) | |
OLD | NEW |