1MRuby::Toolchain.new(:gcc) do |conf, params|
2  default_command = params[:default_command] || 'gcc'
3  compiler_flags = %w(-g -O3 -Wall -Wundef)
4  c_mandatory_flags = %w(-std=gnu99)
5  cxx_invalid_flags = %w(-Wdeclaration-after-statement -Werror-implicit-function-declaration)
6
7  [conf.cc, conf.objc, conf.asm, conf.cxx].each do |compiler|
8    if compiler == conf.cxx
9      compiler.command = ENV['CXX'] || default_command.sub(/cc|$/, '++')
10      compiler.flags = [ENV['CXXFLAGS'] || ENV['CFLAGS'] || compiler_flags]
11    else
12      compiler.command = ENV['CC'] || default_command
13      compiler.flags = [c_mandatory_flags, ENV['CFLAGS'] || [compiler_flags, cxx_invalid_flags, %w(-Wwrite-strings)]]
14    end
15    compiler.option_include_path = %q[-I"%s"]
16    compiler.option_define = '-D%s'
17    compiler.compile_options = %q[%{flags} -MMD -o "%{outfile}" -c "%{infile}"]
18    compiler.cxx_compile_flag = '-x c++ -std=gnu++03'
19    compiler.cxx_exception_flag = '-fexceptions'
20    compiler.cxx_invalid_flags = c_mandatory_flags + cxx_invalid_flags
21  end
22
23  conf.linker do |linker|
24    linker.command = ENV['LD'] || ENV['CXX'] || ENV['CC'] || default_command
25    linker.flags = [ENV['LDFLAGS'] || %w()]
26    linker.libraries = %w(m)
27    linker.library_paths = []
28    linker.option_library = '-l%s'
29    linker.option_library_path = '-L%s'
30    linker.link_options = '%{flags} -o "%{outfile}" %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}'
31  end
32
33  [[conf.cc, 'c'], [conf.cxx, 'c++']].each do |cc, lang|
34    cc.instance_variable_set :@header_search_language, lang
35    def cc.header_search_paths
36      if @header_search_command != command
37        result = `echo | #{build.filename command} -x#{@header_search_language} -Wp,-v - -fsyntax-only 2>&1`
38        result = `echo | #{command} -x#{@header_search_language} -Wp,-v - -fsyntax-only 2>&1` if $?.exitstatus != 0
39        return include_paths if  $?.exitstatus != 0
40
41        @frameworks = []
42        @header_search_paths = result.lines.map { |v|
43          framework = v.match(/^ (.*)(?: \(framework directory\))$/)
44          if framework
45            @frameworks << framework[1]
46            next nil
47          end
48
49          v.match(/^ (.*)$/)
50        }.compact.map { |v| v[1] }.select { |v| File.directory? v }
51        @header_search_paths += include_paths
52        @header_search_command = command
53      end
54      @header_search_paths
55    end
56  end
57
58  def conf.enable_sanitizer(*opts)
59    fail 'sanitizer already set' if @sanitizer_list
60
61    @sanitizer_list = opts
62    flg = "-fsanitize=#{opts.join ','}"
63    [self.cc, self.cxx, self.linker].each{|cmd| cmd.flags << flg }
64  end
65end
66