Description:
last commit is a dud...fixing it now.
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r152:394121525814 - - 1 file changed: 1 inserted, 1 deleted

@@ -1,144 +1,144
1 #!/usr/bin/env ruby
1 #!/usr/bin/env ruby
2
2
3 require 'fileutils'
3 require 'fileutils'
4
4
5 ##############################
5 ##############################
6 #
6 #
7 # Standard Compile Script
7 # Standard Compile Script
8 #
8 #
9 # Supported compilers:
9 # Supported compilers:
10 # gcc, g++, and fpc.
10 # gcc, g++, and fpc.
11 #
11 #
12 ##############################
12 ##############################
13
13
14 def talk(str='')
14 def talk(str='')
15 if ENV['TALKATIVE']!=nil
15 if ENV['TALKATIVE']!=nil
16 puts str
16 puts str
17 end
17 end
18 if ENV['GRADER_LOGGING']!=nil
18 if ENV['GRADER_LOGGING']!=nil
19 log_fname = ENV['GRADER_LOGGING']
19 log_fname = ENV['GRADER_LOGGING']
20 fp = File.open(log_fname,"a")
20 fp = File.open(log_fname,"a")
21 fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}")
21 fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}")
22 fp.close
22 fp.close
23 end
23 end
24 end
24 end
25
25
26 C_COMPILER = "/usr/bin/gcc"
26 C_COMPILER = "/usr/bin/gcc"
27 CPLUSPLUS_COMPILER = "/usr/bin/g++"
27 CPLUSPLUS_COMPILER = "/usr/bin/g++"
28 PASCAL_COMPILER = "/usr/bin/fpc"
28 PASCAL_COMPILER = "/usr/bin/fpc"
29 JAVA_COMPILER = "/usr/bin/javac"
29 JAVA_COMPILER = "/usr/bin/javac"
30 RUBY_INTEPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby"
30 RUBY_INTEPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby"
31
31
32 C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall"
32 C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall"
33 CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall"
33 CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall"
34 PASCAL_OPTIONS = "-O1 -XS -dCONTEST"
34 PASCAL_OPTIONS = "-O1 -XS -dCONTEST"
35 JAVA_OPTIONS = ""
35 JAVA_OPTIONS = ""
36
36
37 # Check for the correct number of arguments. Otherwise, print usage.
37 # Check for the correct number of arguments. Otherwise, print usage.
38 if ARGV.length == 0 or ARGV.length > 4
38 if ARGV.length == 0 or ARGV.length > 4
39 puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]"
39 puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]"
40 puts
40 puts
41 puts "<source-file> is defaulted to \"source\"."
41 puts "<source-file> is defaulted to \"source\"."
42 puts "<output-file> is defaulted to \"a.out\"."
42 puts "<output-file> is defaulted to \"a.out\"."
43 puts "<message-file> is defaulted to \"compiler_message\"."
43 puts "<message-file> is defaulted to \"compiler_message\"."
44 puts
44 puts
45 exit(127)
45 exit(127)
46 end
46 end
47
47
48 PARAMS = {
48 PARAMS = {
49 :source_file => [1,'source'],
49 :source_file => [1,'source'],
50 :output_file => [2,'a.out'],
50 :output_file => [2,'a.out'],
51 :message_file => [3,'compiler_message']
51 :message_file => [3,'compiler_message']
52 }
52 }
53
53
54 params = {}
54 params = {}
55 params[:prog_lang] = ARGV[0]
55 params[:prog_lang] = ARGV[0]
56 PARAMS.each_key do |param_name|
56 PARAMS.each_key do |param_name|
57 index, default = PARAMS[param_name]
57 index, default = PARAMS[param_name]
58 if ARGV.length > index
58 if ARGV.length > index
59 params[param_name] = ARGV[index]
59 params[param_name] = ARGV[index]
60 else
60 else
61 params[param_name] = default
61 params[param_name] = default
62 end
62 end
63 talk "#{param_name}: #{params[param_name]}"
63 talk "#{param_name}: #{params[param_name]}"
64 end
64 end
65
65
66 # Remove any remaining output files or message files.
66 # Remove any remaining output files or message files.
67 if FileTest.exists? params[:output_file]
67 if FileTest.exists? params[:output_file]
68 FileUtils.rm(params[:output_file])
68 FileUtils.rm(params[:output_file])
69 end
69 end
70 if FileTest.exists? params[:message_file]
70 if FileTest.exists? params[:message_file]
71 FileUtils.rm(params[:message_file])
71 FileUtils.rm(params[:message_file])
72 end
72 end
73
73
74 # Check if the source file exists before attempt compiling.
74 # Check if the source file exists before attempt compiling.
75 if !FileTest.exists? params[:source_file]
75 if !FileTest.exists? params[:source_file]
76 talk("ERROR: The source file does not exist!")
76 talk("ERROR: The source file does not exist!")
77 open(params[:message_file],"w") do |f|
77 open(params[:message_file],"w") do |f|
78 f.puts "ERROR: The source file did not exist."
78 f.puts "ERROR: The source file did not exist."
79 end
79 end
80 exit(127)
80 exit(127)
81 end
81 end
82
82
83 if params[:prog_lang]=='cpp'
83 if params[:prog_lang]=='cpp'
84 params[:prog_lang] = 'c++'
84 params[:prog_lang] = 'c++'
85 end
85 end
86
86
87 # Compile.
87 # Compile.
88 case params[:prog_lang]
88 case params[:prog_lang]
89
89
90 when "c"
90 when "c"
91 command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}"
91 command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}"
92 system(command)
92 system(command)
93
93
94 when "c++"
94 when "c++"
95 command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}"
95 command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}"
96 system(command)
96 system(command)
97
97
98 when "pas"
98 when "pas"
99 command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}"
99 command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}"
100 system(command)
100 system(command)
101 FileUtils.mv("output", params[:output_file])
101 FileUtils.mv("output", params[:output_file])
102
102
103 when "java"
103 when "java"
104 #rename the file to the public class name
104 #rename the file to the public class name
105
105
106 #get the class name
106 #get the class name
107 classname = 'DUMMY'
107 classname = 'DUMMY'
108 File.foreach(params[:source_file]) do |line|
108 File.foreach(params[:source_file]) do |line|
109 md = /\s*public\s*class\s*(\w*)/.match(line)
109 md = /\s*public\s*class\s*(\w*)/.match(line)
110 classname=md[1] if md
110 classname=md[1] if md
111 end
111 end
112 system("cp #{params[:source_file]} #{classname}.java")
112 system("cp #{params[:source_file]} #{classname}.java")
113 command = "#{JAVA_COMPILER} #{classname}.java 2> #{params[:message_file]}"
113 command = "#{JAVA_COMPILER} #{classname}.java 2> #{params[:message_file]}"
114 system(command)
114 system(command)
115 - if File.exists?(classname + ".class") begin
115 + if File.exists?(classname + ".class")
116 File.open(params[:output_file],"w") {|file| file.write("#!/bin/sh\n/usr/bin/java #{classname}\n")}
116 File.open(params[:output_file],"w") {|file| file.write("#!/bin/sh\n/usr/bin/java #{classname}\n")}
117 File.chmod(0755, params[:output_file])
117 File.chmod(0755, params[:output_file])
118 end
118 end
119
119
120 when "ruby"
120 when "ruby"
121 command = "#{RUBY_INTEPRETER} -c #{params[:source_file]} > #{params[:message_file]}"
121 command = "#{RUBY_INTEPRETER} -c #{params[:source_file]} > #{params[:message_file]}"
122 system(command)
122 system(command)
123 File.open(params[:output_file],"w") do |out_file|
123 File.open(params[:output_file],"w") do |out_file|
124 out_file.puts "#!#{RUBY_INTEPRETER}"
124 out_file.puts "#!#{RUBY_INTEPRETER}"
125 File.open(params[:source_file],"r").each do |line|
125 File.open(params[:source_file],"r").each do |line|
126 out_file.print line
126 out_file.print line
127 end
127 end
128 end
128 end
129 File.chmod(0755, params[:output_file])
129 File.chmod(0755, params[:output_file])
130
130
131 else
131 else
132 talk("ERROR: Invalid language specified!")
132 talk("ERROR: Invalid language specified!")
133 open(params[:message_file],"w") do |f|
133 open(params[:message_file],"w") do |f|
134 f.puts "ERROR: Invalid language specified!"
134 f.puts "ERROR: Invalid language specified!"
135 end
135 end
136 exit(127)
136 exit(127)
137 end
137 end
138
138
139 # Report success or failure.
139 # Report success or failure.
140 if FileTest.exists? params[:output_file]
140 if FileTest.exists? params[:output_file]
141 talk "Compilation was successful!"
141 talk "Compilation was successful!"
142 else
142 else
143 talk "ERROR: Something was wrong during the compilation!"
143 talk "ERROR: Something was wrong during the compilation!"
144 end
144 end
You need to be logged in to leave comments. Login now