From febffe30bfcfdf1f87067ddcc0313463ee0ec912 Mon Sep 17 00:00:00 2001 From: Wilson Bilkovich Date: Tue, 21 Oct 2008 15:27:11 -0400 Subject: [PATCH] Remove unused/warning-generating versions of the FFI generators --- lib/ffi/generator_task.rb | 16 +-- rakelib/const_generator.rb | 82 --------------- rakelib/platform.rake | 6 +- rakelib/struct_generator.rb | 195 ------------------------------------ 4 files changed, 8 insertions(+), 291 deletions(-) delete mode 100644 rakelib/const_generator.rb delete mode 100644 rakelib/struct_generator.rb diff --git a/lib/ffi/generator_task.rb b/lib/ffi/generator_task.rb index 07e234fea3..aea14aa837 100644 --- a/lib/ffi/generator_task.rb +++ b/lib/ffi/generator_task.rb @@ -1,18 +1,12 @@ -begin - require 'ffi/struct_generator' - require 'ffi/const_generator' - require 'ffi/generator' -rescue LoadError - # from Rakefile - require 'lib/ffi/struct_generator' - require 'lib/ffi/const_generator' - require 'lib/ffi/generator' -end - require 'rake' require 'rake/tasklib' require 'tempfile' +require 'ffi/struct_generator' +require 'ffi/const_generator' +require 'ffi/generator' + + ## # Rake task that calculates C structs for FFI::Struct. diff --git a/rakelib/const_generator.rb b/rakelib/const_generator.rb deleted file mode 100644 index ec1fe27ac3..0000000000 --- a/rakelib/const_generator.rb +++ /dev/null @@ -1,82 +0,0 @@ -require "tempfile" - -class ConstGenerator - class Constant - attr_reader :name, :format, :cast - attr_accessor :value - - def initialize(name, format, cast, converter=nil) - @name = name - @format = format - @cast = cast - @converter = converter - @value = nil - end - - def converted_value - if @converter - @converter.call(@value) - else - @value - end - end - end - - def initialize - @includes = [] - @constants = {} - end - - def get_const(name) - return @constants[name].value - end - - attr_reader :constants - - def include(i) - @includes << i - end - - def const(name, format="%d", cast="", &converter) - const = Constant.new(name, format, cast, converter) - @constants[name.to_s] = const - return const - end - - def calculate - binary = "rb_const_gen_bin_#{Process.pid}" - - Tempfile.open("rbx_const_gen_tmp") do |f| - f.puts "#include " - - @includes.each do |inc| - f.puts "#include <#{inc}>" - end - - f.puts "#include \n\n" - f.puts "int main(int argc, char **argv)\n{" - - @constants.each_value do |const| - f.puts <" - - @includes.each do |inc| - f.puts "#include <#{inc}>" - end - - f.puts "#include \n\n" - f.puts "int main(int argc, char **argv)\n{" - f.puts " #{@struct_name} s;" - f.puts %[ printf("sizeof(#{@struct_name}) %u\\n", (unsigned int) sizeof(#{@struct_name}));] - - @fields.each do |field| - f.puts <<-EOF - printf("#{field.name} %u %u\\n", (unsigned int) offsetof(#{@struct_name}, #{field.name}), - (unsigned int) sizeof(s.#{field.name})); -EOF - end - - f.puts "\n return 0;\n}" - f.flush - - if $verbose then - f.rewind - $stderr.puts f.read - end - - `gcc -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall #{f.path} -o #{binary}` - if $?.exitstatus == 1 - @found = false - return - end - end - - output = `./#{binary}`.split "\n" - File.unlink(binary) - - sizeof = output.shift - unless @size - m = /\s*sizeof\([^)]+\) (\d+)/.match sizeof - @size = m[1] - end - - line_no = 0 - output.each do |line| - md = line.match(/.+ (\d+) (\d+)/) - @fields[line_no].offset = md[1].to_i - @fields[line_no].size = md[2].to_i - - line_no += 1 - end - @found = true - end - - def generate_config(name) - @fields.inject(["rbx.platform.#{name}.sizeof = #{@size}"]) do |list, field| - list.concat field.to_config(name) - end.join "\n" - end - - def generate_layout - buf = "" - - @fields.each_with_index do |field, i| - if buf.empty? - buf << "layout :#{field.name}, :#{field.type}, #{field.offset}" - else - buf << " :#{field.name}, :#{field.type}, #{field.offset}" - end - - if i < @fields.length - 1 - buf << ",\n" - end - end - - buf - end -end - -class StructGenerator::Field - attr_reader :name - attr_reader :type - attr_reader :offset - attr_accessor :size - - def initialize(name, type) - @name = name - @type = type - @offset = nil - end - - def offset=(o) - @offset = o - end - - def to_config(name) - buf = [] - buf << "rbx.platform.#{name}.#{@name}.offset = #{@offset}" - buf << "rbx.platform.#{name}.#{@name}.size = #{@size}" - buf << "rbx.platform.#{name}.#{@name}.type = #{@type}" if @type - buf - end -end - -module Rake - class StructGeneratorTask < TaskLib - attr_accessor :dest - - def initialize - @dest = nil - - yield self if block_given? - - define - end - - def define - task :clean do - rm_f @dest - end - - file @dest => %W[#{@dest}.in #{__FILE__}] do |t| - puts "Generating #{@dest}..." - - file = File.read t.prerequisites.first - - new_file = file.gsub(/^( *)@@@(.*?)@@@/m) do - indent = $1 - original_lines = $2.count("\n") - 1 - - new_lines = StructGenerator.generate_from_code $2 - new_lines = new_lines.split("\n").map { |line| indent + line } - new_lines += [nil] * (original_lines - new_lines.length) - new_lines.join "\n" - end - - File.open(t.name, "w") do |f| - f.puts "# This file is generated by rake. Do not edit." - f.puts - f.puts new_file - end - end - end - end -end