Class: Rbind::GeneratorC

Inherits:
Object
  • Object
show all
Defined in:
lib/rbind/generator_c.rb

Defined Under Namespace

Classes: CMakeListsHelper, ConstsHelper, ConversionsHelper, ConversionsHelperHDR, HelperBase, OperationsHDRHelper, OperationsHelper, TypesHelper, TypesHelperHDR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, name, library_name) ⇒ GeneratorC

Returns a new instance of GeneratorC.



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rbind/generator_c.rb', line 343

def initialize(root,name,library_name)
    raise "wrong type #{root}" unless root.is_a? RNamespace
    @root = root
    @erb_types_hdr = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","types.h")).read)
    @erb_types = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","types.cc")).read)
    @erb_consts = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","consts.h")).read)
    @erb_operations = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","operations.cc")).read)
    @erb_operations_hdr = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","operations.h")).read)
    @erb_conversions = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","conversions.cc")).read)
    @erb_conversions_hdr = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","conversions.hpp")).read)
    @erb_cmakelists = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","CMakeLists.txt")).read)
    @erb_find_package = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","c","find_package.txt")).read)
    @includes = Array.new
    @pkg_config= Array.new
    @library_name = library_name
    @name = name
    @generate_cmake = true
    @libs = []
end

Instance Attribute Details

#generate_cmakeObject

Returns the value of attribute generate_cmake.



339
340
341
# File 'lib/rbind/generator_c.rb', line 339

def generate_cmake
  @generate_cmake
end

#includesObject

Returns the value of attribute includes.



335
336
337
# File 'lib/rbind/generator_c.rb', line 335

def includes
  @includes
end

#library_nameObject

Returns the value of attribute library_name.



336
337
338
# File 'lib/rbind/generator_c.rb', line 336

def library_name
  @library_name
end

#libsObject

Returns the value of attribute libs.



337
338
339
# File 'lib/rbind/generator_c.rb', line 337

def libs
  @libs
end

#output_pathObject

Returns the value of attribute output_path.



340
341
342
# File 'lib/rbind/generator_c.rb', line 340

def output_path
  @output_path
end

#pkg_configObject

Returns the value of attribute pkg_config.



338
339
340
# File 'lib/rbind/generator_c.rb', line 338

def pkg_config
  @pkg_config
end

#ruby_pathObject

Returns the value of attribute ruby_path.



341
342
343
# File 'lib/rbind/generator_c.rb', line 341

def ruby_path
  @ruby_path
end

Instance Method Details

#generate(path = @output_path, ruby_path = @ruby_path) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/rbind/generator_c.rb', line 363

def generate(path = @output_path,ruby_path = @ruby_path)
    @output_path = path
    @ruby_path = ruby_path
    FileUtils.mkdir_p(path) if path && !File.directory?(path)
    file_types_hdr = File.new(File.join(path,"types.h"),"w")
    file_types = File.new(File.join(path,"types.cc"),"w")
    file_consts = File.new(File.join(path,"constants.h"),"w")
    file_operations = File.new(File.join(path,"operations.cc"),"w")
    file_operations_hdr = File.new(File.join(path,"operations.h"),"w")
    file_conversions = File.new(File.join(path,"conversions.cc"),"w")
    file_conversions_hdr = File.new(File.join(path,"conversions.hpp"),"w")

    # mark all extern types which must be wrapped
    extern_types = @root.used_extern_types

    types_hdr = TypesHelperHDR.new("_#{library_name.upcase}_TYPES_H_",@root,extern_types)
    file_types_hdr.write @erb_types_hdr.result(types_hdr.binding)

    types = TypesHelper.new("types",@root,extern_types)
    file_types.write @erb_types.result(types.binding)

    consts = ConstsHelper.new("_#{library_name.upcase}_CONSTS_H_",@root)
    file_consts.write @erb_consts.result(consts.binding)

    conversions_hdr = ConversionsHelperHDR.new("#{library_name.upcase}_CONVERSIONS_H_",@root,extern_types)
    conversions_hdr.includes += includes
    file_conversions_hdr.write @erb_conversions_hdr.result(conversions_hdr.binding)

    conversions = ConversionsHelper.new("conversions",@root,extern_types)
    file_conversions.write @erb_conversions.result(conversions.binding)

    operations_hdr = OperationsHDRHelper.new("_#{library_name.upcase}_OPERATIONS_H_",@root,extern_types)
    file_operations_hdr.write @erb_operations_hdr.result(operations_hdr.binding)

    operations = OperationsHelper.new("operations",@root,extern_types)
    file_operations.write @erb_operations.result(operations.binding)

    if generate_cmake && !File.exist?(File.join(path,"CMakeLists.txt"))
        file_cmakelists = File.new(File.join(path,"CMakeLists.txt"),"w")
        cmakelists = CMakeListsHelper.new(@name,@library_name,@pkg_config,@libs,@ruby_path)
        file_cmakelists.write @erb_cmakelists.result(cmakelists.binding)
        src_path = File.join(File.dirname(__FILE__),"templates","c","cmake")
        cmake_path = File.join(path,"cmake")
        FileUtils.mkdir_p(cmake_path) if !File.directory?(cmake_path)
        FileUtils.copy(File.join(src_path,"FindRuby.cmake"),File.join(cmake_path,"FindRuby.cmake"))
    end
end