OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 # | 5 # |
6 # Script to compile the analyzer. | 6 # Script to compile the analyzer. |
7 # | 7 # |
8 # Usage: compile_analyzer.py OPTIONS files | 8 # Usage: compile_analyzer.py OPTIONS files |
9 # | 9 # |
10 | 10 |
11 import optparse | 11 import optparse |
12 import os | 12 import os |
| 13 import platform |
13 import shutil | 14 import shutil |
14 import subprocess | 15 import subprocess |
15 import sys | 16 import sys |
16 | 17 |
| 18 from os.path import join |
| 19 |
17 def GetOptions(): | 20 def GetOptions(): |
18 options = optparse.OptionParser(usage='usage: %prog [options] <output>') | 21 options = optparse.OptionParser(usage='usage: %prog [options] <output>') |
19 options.add_option("--class_path_file", | 22 options.add_option("--class_path_file", |
20 help='File describing the classpath in manifest style') | 23 help='File describing the classpath in manifest style') |
21 options.add_option("--output_dir", | 24 options.add_option("--output_dir", |
22 help='Where to output files') | 25 help='Where to output files') |
23 options.add_option("--jar_file_name", | 26 options.add_option("--jar_file_name", |
24 help='Name of the resulting jar file') | 27 help='Name of the resulting jar file') |
25 options.add_option("--jar_entry_directory", | 28 options.add_option("--jar_entry_directory", |
26 help='Which directory within output to pack into the jar files') | 29 help='Which directory within output to pack into the jar files') |
27 options.add_option("--entry_point", | 30 options.add_option("--entry_point", |
28 help='The entry point for running the program.') | 31 help='The entry point for running the program.') |
29 options.add_option("--dependent_jar_files", | 32 options.add_option("--dependent_jar_files", |
30 help='The jar files that we link against, space separated.') | 33 help='The jar files that we link against, space separated.') |
31 return options.parse_args() | 34 return options.parse_args() |
32 | 35 |
33 def CompileAnalyzer(options, args): | 36 def CompileAnalyzer(options, args): |
34 # We rely on all jar files being copied to the output dir. | 37 # We rely on all jar files being copied to the output dir. |
35 class_path = options.output_dir + '*' | 38 class_path = options.output_dir + '*' |
36 cmd = ['javac', | 39 cmd = [GetJavacPath(), |
37 '-sourcepath', 'foobar', | 40 '-sourcepath', 'foobar', |
38 '-source', '6', | 41 '-source', '6', |
39 '-target', '6', | 42 '-target', '6', |
40 '-implicit:none', | 43 '-implicit:none', |
41 '-d', options.output_dir, | 44 '-d', options.output_dir, |
42 '-cp', class_path, | 45 '-cp', class_path, |
43 ] | 46 ] |
44 cmd.extend(args) | 47 cmd.extend(args) |
45 subprocess.call(cmd) | 48 subprocess.call(cmd) |
46 | 49 |
47 def CreateJarFile(options): | 50 def CreateJarFile(options): |
48 class_path_file_name = options.output_dir + options.class_path_file | 51 class_path_file_name = options.output_dir + options.class_path_file |
49 jar_file_name = options.output_dir + options.jar_file_name | 52 jar_file_name = options.output_dir + options.jar_file_name |
50 cmd = ['jar', 'cfem', jar_file_name, options.entry_point, | 53 cmd = [GetJarToolPath(), 'cfem', jar_file_name, options.entry_point, |
51 class_path_file_name, | 54 class_path_file_name, |
52 '-C', options.output_dir, options.jar_entry_directory]; | 55 '-C', options.output_dir, options.jar_entry_directory] |
53 subprocess.call(cmd) | 56 subprocess.call(cmd) |
54 | 57 |
55 def CopyFiles(options): | 58 def CopyFiles(options): |
56 # Strip " from the string | 59 # Strip " from the string |
57 files = options.dependent_jar_files.replace('"', ''); | 60 files = options.dependent_jar_files.replace('"', '') |
58 for f in files.split(" "): | 61 for f in files.split(" "): |
59 shutil.copy(f, options.output_dir) | 62 shutil.copy(f, options.output_dir) |
60 | 63 |
61 def CreateClassPathFile(options): | 64 def CreateClassPathFile(options): |
62 class_path_file_name = options.output_dir + options.class_path_file | 65 class_path_file_name = options.output_dir + options.class_path_file |
63 with open(class_path_file_name, 'w') as output: | 66 with open(class_path_file_name, 'w') as output: |
64 print >> output, 'Class-Path:', '.', | 67 print >> output, 'Class-Path:', '.', |
65 for r,d,f in os.walk(options.output_dir): | 68 for r,d,f in os.walk(options.output_dir): |
66 for file in f: | 69 for file in f: |
67 if file.endswith('.jar'): | 70 if file.endswith('.jar'): |
68 print >> output, file, | 71 print >> output, file, |
69 # Add new line | 72 # Add new line |
70 print >> output | 73 print >> output |
71 | 74 |
| 75 def GetJavacPath(): |
| 76 if 'JAVA_HOME' in os.environ: |
| 77 return join(os.environ['JAVA_HOME'], 'bin', 'javac' + GetExecutableExtension
()) |
| 78 else: |
| 79 return "javac" |
| 80 |
| 81 def GetJarToolPath(): |
| 82 if 'JAVA_HOME' in os.environ: |
| 83 return join(os.environ['JAVA_HOME'], 'bin', 'jar' + GetExecutableExtension()
) |
| 84 else: |
| 85 return "jar" |
| 86 |
| 87 def GetExecutableExtension(): |
| 88 id = platform.system() |
| 89 if id == "Windows" or id == "Microsoft": |
| 90 return '.exe' |
| 91 else: |
| 92 return '' |
| 93 |
72 def main(): | 94 def main(): |
73 (options, args) = GetOptions() | 95 (options, args) = GetOptions() |
74 # Clean out everything whenever we do a build, guarantees that we don't have | 96 # Clean out everything whenever we do a build, guarantees that we don't have |
75 # any leftover jar files. | 97 # any leftover jar files. |
76 shutil.rmtree(options.output_dir, ignore_errors=True) | 98 shutil.rmtree(options.output_dir, ignore_errors=True) |
77 os.makedirs(options.output_dir) | 99 os.makedirs(options.output_dir) |
78 | 100 |
79 CopyFiles(options) | 101 CopyFiles(options) |
80 CreateClassPathFile(options) | 102 CreateClassPathFile(options) |
81 CompileAnalyzer(options, args) | 103 CompileAnalyzer(options, args) |
82 CreateJarFile(options) | 104 CreateJarFile(options) |
83 | 105 |
84 | 106 |
85 if __name__=='__main__': | 107 if __name__=='__main__': |
86 main() | 108 main() |
OLD | NEW |