Class: MRuby::Command::Compiler

Inherits:
MRuby::Command show all
Defined in:
ext/enterprise_script_service/mruby/lib/mruby/build/command.rb

Constant Summary

Constants inherited from MRuby::Command

NotFoundCommands

Instance Attribute Summary collapse

Attributes inherited from MRuby::Command

#build, #command

Instance Method Summary collapse

Methods inherited from MRuby::Command

#clone, #shellquote

Constructor Details

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

Returns a new instance of Compiler.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 54

def initialize(build, source_exts=[])
  super(build)
  @command = ENV['CC'] || 'cc'
  @flags = [ENV['CFLAGS'] || []]
  @source_exts = source_exts
  @include_paths = ["#{MRUBY_ROOT}/include"]
  @defines = %w()
  @option_include_path = %q[-I"%s"]
  @option_define = %q[-D"%s"]
  @compile_options = %q[%{flags} -o "%{outfile}" -c "%{infile}"]
  @cxx_invalid_flags = []
end

Instance Attribute Details

#compile_optionsObject

Returns the value of attribute compile_options.



51
52
53
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 51

def compile_options
  @compile_options
end

#cxx_compile_flagObject

Returns the value of attribute cxx_compile_flag.



52
53
54
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 52

def cxx_compile_flag
  @cxx_compile_flag
end

#cxx_exception_flagObject

Returns the value of attribute cxx_exception_flag.



52
53
54
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 52

def cxx_exception_flag
  @cxx_exception_flag
end

#cxx_invalid_flagsObject

Returns the value of attribute cxx_invalid_flags.



52
53
54
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 52

def cxx_invalid_flags
  @cxx_invalid_flags
end

#definesObject

Returns the value of attribute defines.



50
51
52
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 50

def defines
  @defines
end

#flagsObject

Returns the value of attribute flags.



50
51
52
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 50

def flags
  @flags
end

#include_pathsObject Also known as: header_search_paths

Returns the value of attribute include_paths.



50
51
52
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 50

def include_paths
  @include_paths
end

#option_defineObject

Returns the value of attribute option_define.



51
52
53
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 51

def option_define
  @option_define
end

#option_include_pathObject

Returns the value of attribute option_include_path.



51
52
53
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 51

def option_include_path
  @option_include_path
end

#out_extObject

Returns the value of attribute out_ext.



51
52
53
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 51

def out_ext
  @out_ext
end

#source_extsObject

Returns the value of attribute source_exts.



50
51
52
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 50

def source_exts
  @source_exts
end

Instance Method Details

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



79
80
81
82
83
84
85
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 79

def all_flags(_defines=[], _include_paths=[], _flags=[])
  define_flags = [defines, _defines].flatten.map{ |d| option_define % d }
  include_path_flags = [include_paths, _include_paths].flatten.map do |f|
    option_include_path % filename(f)
  end
  [flags, define_flags, include_path_flags, _flags].flatten.join(' ')
end

#define_rules(build_dir, source_dir = '') ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 94

def define_rules(build_dir, source_dir='')
  @out_ext = build.exts.object
  gemrake = File.join(source_dir, "mrbgem.rake")
  rakedep = File.exist?(gemrake) ? [ gemrake ] : []

  if build_dir.include? "mrbgems/"
    generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(.*)#{Regexp.escape out_ext}$")
  else
    generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbgems/.+/)(.*)#{Regexp.escape out_ext}$")
  end
  source_exts.each do |ext, compile|
    rule generated_file_matcher => [
      proc { |file|
        file.sub(generated_file_matcher, "#{source_dir}/\\1#{ext}")
      },
      proc { |file|
        get_dependencies(file) + rakedep
      }
    ] do |t|
      run t.name, t.prerequisites.first
    end

    rule generated_file_matcher => [
      proc { |file|
        file.sub(generated_file_matcher, "#{build_dir}/\\1#{ext}")
      },
      proc { |file|
        get_dependencies(file) + rakedep
      }
    ] do |t|
      run t.name, t.prerequisites.first
    end
  end
end

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



87
88
89
90
91
92
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 87

def run(outfile, infile, _defines=[], _include_paths=[], _flags=[])
  mkdir_p File.dirname(outfile)
  _pp "CC", infile.relative_path, outfile.relative_path
  _run compile_options, { :flags => all_flags(_defines, _include_paths, _flags),
                          :infile => filename(infile), :outfile => filename(outfile) }
end

#search_header(name) ⇒ Object



74
75
76
77
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 74

def search_header(name)
  path = search_header_path name
  path && build.filename("#{path}/#{name}").sub(/^"(.*)"$/, '\1')
end

#search_header_path(name) ⇒ Object



68
69
70
71
72
# File 'ext/enterprise_script_service/mruby/lib/mruby/build/command.rb', line 68

def search_header_path(name)
  header_search_paths.find do |v|
    File.exist? build.filename("#{v}/#{name}").sub(/^"(.*)"$/, '\1')
  end
end