Class: Magica::Command::Compiler

Inherits:
Magica::Command show all
Defined in:
lib/magica/commands/compiler.rb

Instance Attribute Summary collapse

Attributes inherited from Magica::Command

#build, #command

Instance Method Summary collapse

Constructor Details

#initialize(build, source_exts = []) ⇒ Compiler

Returns a new instance of Compiler.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/magica/commands/compiler.rb', line 5

def initialize(build, source_exts = [])
  super(build)

  @command = ENV['CC'] || 'cc'
  @flags = [ENV['CFLAGS'] || []]
  @source_exts = source_exts
  @compile_options = "%{flags} -o %{outfile} -c %{infile}"
  @include_paths = ["include"]
  @defines = %w()

  @option_include_path = "-I%s"
  @option_defines = "-D%s"
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



3
4
5
# File 'lib/magica/commands/compiler.rb', line 3

def flags
  @flags
end

Instance Method Details

#combine_flags(_defines = [], _include_paths = [], _flags = []) ⇒ Object



19
20
21
22
23
# File 'lib/magica/commands/compiler.rb', line 19

def combine_flags(_defines = [], _include_paths = [], _flags = [])
  define_flags = [@defines, _defines].flatten.map { |define| @option_defines % define }
  include_path_flags = [@include_paths, _include_paths].flatten.map { |include_path| @option_include_path % filename(include_path) }
  [define_flags, include_path_flags, _flags].flatten.uniq.join(' ')
end

#run(outfile, infile, _defines = [], _include_paths = [], _flags = []) ⇒ Object



25
26
27
28
29
# File 'lib/magica/commands/compiler.rb', line 25

def run(outfile, infile, _defines = [], _include_paths = [], _flags = [])
  FileUtils.mkdir_p File.dirname(outfile)
  puts "COMPILE\t#{outfile}"
  _run @compile_options, { outfile: outfile, infile: infile, flags: combine_flags(_defines, _include_paths, _flags) }
end