Method: BOAST::CKernel#setup_compiler

Defined in:
lib/BOAST/CKernel.rb

#setup_compiler(options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/BOAST/CKernel.rb', line 142

def setup_compiler(options = {})
  Rake::Task::clear
  verbose = options[:verbose]
  verbose = BOAST::get_verbose if not verbose
  Rake::verbose(verbose)
  Rake::FileUtilsExt.verbose_flag=verbose
  f_compiler = options[:FC]
  c_compiler = options[:CC]
  cxx_compiler = options[:CXX]
  cuda_compiler = options[:NVCC]
  f_flags = options[:FCFLAGS]
  f_flags += " -fPIC"
  f_flags += " -fno-second-underscore" if f_compiler == 'g95'
  ld_flags = options[:LDFLAGS]
  cuda_flags = options[:NVCCFLAGS]
  cuda_flags += " --compiler-options '-fPIC'"


  includes = "-I#{RbConfig::CONFIG["archdir"]}"
  includes += " -I#{RbConfig::CONFIG["rubyhdrdir"]} -I#{RbConfig::CONFIG["rubyhdrdir"]}/#{RbConfig::CONFIG["arch"]}"
  includes += " -I#{RbConfig::CONFIG["rubyarchhdrdir"]}" if RbConfig::CONFIG["rubyarchhdrdir"]
  ld_flags += " -L#{RbConfig::CONFIG["libdir"]} #{RbConfig::CONFIG["LIBRUBYARG"]} -lrt"
  ld_flags += " -lcudart" if @lang == BOAST::CUDA
  narray_path = nil
  begin
    spec = Gem::Specification::find_by_name('narray')
    narray_path = spec.full_gem_path
  rescue Gem::LoadError => e
  rescue NoMethodError => e
    spec = Gem::available?('narray')
    if spec then
      require 'narray' 
      narray_path = Gem.loaded_specs['narray'].full_gem_path
    end
  end
  includes += " -I#{narray_path}" if narray_path
  cflags = options[:CFLAGS]
  cxxflags = options[:CXXFLAGS]
  cflags += " -fPIC #{includes}"
  cxxflags += " -fPIC #{includes}"
  cflags += " -DHAVE_NARRAY_H" if narray_path
  fcflags = f_flags
  cudaflags = cuda_flags

  if options[:openmp] then
    case @lang
    when BOAST::C
      openmp_c_flags = BOAST::get_openmp_flags[c_compiler]
      if not openmp_c_flags then
        keys = BOAST::get_openmp_flags.keys
        keys.each { |k|
          openmp_c_flags = BOAST::get_openmp_flags[k] if c_compiler.match(k)
        }
      end
      raise "unkwown openmp flags for: #{c_compiler}" if not openmp_c_flags
      cflags += " #{openmp_c_flags}"
      openmp_cxx_flags = BOAST::get_openmp_flags[cxx_compiler]
      if not openmp_cxx_flags then
        keys = BOAST::get_openmp_flags.keys
        keys.each { |k|
          openmp_cxx_flags = BOAST::get_openmp_flags[k] if cxx_compiler.match(k)
        }
      end
      raise "unkwown openmp flags for: #{cxx_compiler}" if not openmp_cxx_flags
      cxxflags += " #{openmp_cxx_flags}"
    when BOAST::FORTRAN
      openmp_f_flags = BOAST::get_openmp_flags[f_compiler]
      if not openmp_f_flags then
        keys = BOAST::get_openmp_flags.keys
        keys.each { |k|
          openmp_f_flags = BOAST::get_openmp_flags[k] if f_compiler.match(k)
        }
      end
      raise "unkwown openmp flags for: #{f_compiler}" if not openmp_f_flags
      fcflags += " #{openmp_f_flags}"
    end
  end

  runner = lambda { |t, call_string|
    if verbose then
      sh call_string
    else
      status, stdout, stderr = systemu call_string
      if not status.success? then
        puts stderr
        fail "#{t.source}: compilation failed"
      end
      status.success?
    end
  }

  rule '.o' => '.c' do |t|
    c_call_string = "#{c_compiler} #{cflags} -c -o #{t.name} #{t.source}"
    runner.call(t, c_call_string)
  end

  rule '.o' => '.f90' do |t|
    f_call_string = "#{f_compiler} #{fcflags} -c -o #{t.name} #{t.source}"
    runner.call(t, f_call_string)
  end

  rule '.o' => '.cpp' do |t|
    cxx_call_string = "#{cxx_compiler} #{cxxflags} -c -o #{t.name} #{t.source}"
    runner.call(t, cxx_call_string)
  end

  rule '.o' => '.cu' do |t|
    cuda_call_string = "#{cuda_compiler} #{cudaflags} -c -o #{t.name} #{t.source}"
    runner.call(t, cuda_call_string)
  end
  return ld_flags
end