Class: Benchcc::GCC

Inherits:
Compiler show all
Defined in:
lib/benchcc/compiler.rb

Instance Method Summary collapse

Methods inherited from Compiler

guess_from_binary, #to_s

Constructor Details

#initialize(binary) ⇒ GCC

Returns a new instance of GCC.



93
94
95
96
# File 'lib/benchcc/compiler.rb', line 93

def initialize(binary)
  @exe = `which #{binary}`.strip
  raise "#{binary} not found" unless $?.success?
end

Instance Method Details

#compile(file, *args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/benchcc/compiler.rb', line 98

def compile(file, *args)
  file = Pathname.new(file).expand_path
  command = "/usr/bin/time -l #{@exe} #{args.join(' ')} -ftime-report #{file}"
  stdout, stderr, status = Open3.capture3(command)

  if status.success?
    time = stderr.match(/TOTAL.+/).to_s.split[-3].to_f
    memusg = stderr.match(/(\d+)\s+maximum/)[1].to_i
    return {
      compilation_time: time,
      memory_usage: memusg
    }
  else
    err_string = "      > \#{command}\n      \#{stderr}\n\n      [compiling:\n      \#{file.read}\n      ]\n    EOS\n    raise CompilationError.new(err_string)\n  end\nend\n".strip_heredoc