Class: Ucc::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/ucc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiler) ⇒ Runner

Here we pass the compiler (to be able to choose between gcc or g++)



15
16
17
18
19
20
# File 'lib/ucc.rb', line 15

def initialize(compiler)
  @compiler = compiler
  raise "Compiler must be specified" unless @compiler
  parse_options
  work
end

Instance Attribute Details

#source_filesObject (readonly)

Returns the value of attribute source_files.



12
13
14
# File 'lib/ucc.rb', line 12

def source_files
  @source_files
end

Instance Method Details

#app_filenameObject

Filename to use when executing compiled app



68
69
70
71
72
73
# File 'lib/ucc.rb', line 68

def app_filename
  return @app_filename if @app_filename
  @app_filename = source_files[0].sub(/\.\w+$/, '')
  @app_filename += ".exe" if WINDOWS
  @app_filename
end

#enquote(param) ⇒ Object

Enquotes param if needed, also pre-escapes quotes that already are inside the param



117
118
119
120
121
# File 'lib/ucc.rb', line 117

def enquote(param)
  param = param.gsub(/(["'])/, '\\\\\\1')
  param = %Q["#{param}"] if param =~ /\s/
  param
end

#optionsObject

Options array accessor



63
64
65
# File 'lib/ucc.rb', line 63

def options
  @options ||= {}
end

#optparseObject

Defines options for ucc



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ucc.rb', line 26

def optparse
  @optparse ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{CURRENT_EXECUTABLE} [options] file..."
    
    options[:runopts] = nil
    opts.on( '-r', '--runopts "STRING"', 'Pass STRING as the command line argument to the compiled app' ) do |s|
      options[:runopts] = s
    end
    
    options[:compileopts] = nil
    opts.on( '-c', '--compileopts "STRING"', 'Pass STRING as the command line argument to the compiler' ) do |s|
      options[:compileopts] = s
    end
    
    options[:memcheck] = false
    opts.on( '-V', '--valgrind', 'Run the app in valgrind' ) do
      options[:memcheck] = true
    end
            
    options[:verbose] = false
    opts.on( '--verbose', 'Enable debug messages' ) do
      options[:verbose] = true
    end
    
    opts.on_tail( '-v', '--version', 'Show app version' ) do
      puts "#{CURRENT_EXECUTABLE} #{VERSION}"
      exit
    end
    
    opts.on_tail( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end
end

#parse_optionsObject

Does option parsing using optparse



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ucc.rb', line 79

def parse_options
  begin
    optparse.parse!
  rescue OptionParser::InvalidOption=> e
    puts "#{CURRENT_EXECUTABLE}: #{e}"
    exit(1)
  end      
  
  # Here we have already parsed ARGV
  @source_files = ARGV
  if @source_files.empty?
    puts "#{CURRENT_EXECUTABLE}: no input files"
    exit(1)
  end
end

#trace(text) ⇒ Object

Prints text if verbose mode is on



112
113
114
# File 'lib/ucc.rb', line 112

def trace(text)
  puts "[ucc] Debug: #{text}" if options[:verbose]
end

#workObject

Everything special goes here



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ucc.rb', line 96

def work
  compilation_params = %Q[#{@compiler} -Wall -o "#{app_filename}" #{source_files.map{ |f| enquote(f) }.join(" ")}]
  compilation_params = "#{compilation_params} #{options[:compileopts]}" if options[:compileopts]
  trace compilation_params
  exit unless system compilation_params
  
  exec_params = enquote(app_filename)
  exec_params = "./#{exec_params}" unless WINDOWS
  exec_params = "#{exec_params} #{options[:runopts]}" if options[:runopts]
  exec_params = "valgrind #{exec_params}" if options[:memcheck]
  trace exec_params
  puts "=== Compiled successfully, executing... === "
  exec exec_params
end