Class: FFI::Generators::Platform

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

Instance Method Summary collapse

Constructor Details

#initialize(kind = :c) ⇒ Platform

TODO: Make these configurable to enable cross-compiling



102
103
104
# File 'lib/ffi2/generators.rb', line 102

def initialize(kind=:c)
  @kind = kind
end

Instance Method Details

#compile(include_dirs, source, target) ⇒ Object



156
157
158
159
160
161
# File 'lib/ffi2/generators.rb', line 156

def compile(include_dirs, source, target)
  includes = include_dirs.map { |i| "-I#{i}" }.join(" ")
  compile_options = "#{defines} -x #{language} #{includes} -Wall -Werror"

  "#{compiler} #{compile_options} #{source} -o #{target} 2>&1"
end

#compilerObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/ffi2/generators.rb', line 134

def compiler
  case @kind
  when :c
    RbConfig::CONFIG["CC"]
  when :cpp, :cxx
    RbConfig::CONFIG["CXX"] || RbConfig::CONFIG["CC"]
  else
    RbConfig::CONFIG["CC"]
  end
end

#definesObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/ffi2/generators.rb', line 119

def defines
  case @kind
  when :c
    RbConfig::CONFIG["CFLAGS"]
  when :cpp, :cxx
    RbConfig::CONFIG["CPPFLAGS"] || RbConfig["CXXFLAGS"]
  else
    RbConfig::CONFIG["CFLAGS"]
  end
end

#executable_extObject



115
116
117
# File 'lib/ffi2/generators.rb', line 115

def executable_ext
  windows? ? ".exe" : ""
end

#languageObject



145
146
147
148
149
150
151
152
153
154
# File 'lib/ffi2/generators.rb', line 145

def language
  case @kind
  when :c
    "c"
  when :cpp, :cxx
    "c++"
  else
    "c"
  end
end

#source_extObject



106
107
108
109
110
111
112
113
# File 'lib/ffi2/generators.rb', line 106

def source_ext
  case @kind
  when :c
    ".c"
  when :cpp
    ".cpp"
  end
end

#windows?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/ffi2/generators.rb', line 130

def windows?
  RUBY_PLATFORM =~ /mswin|mingw/
end