Permalink
Browse files

Remove unused/warning-generating versions of the FFI generators

  • Loading branch information...
1 parent 4bc13b2 commit febffe30bfcfdf1f87067ddcc0313463ee0ec912 Wilson Bilkovich committed Oct 21, 2008
Showing with 8 additions and 291 deletions.
  1. +5 −11 lib/ffi/generator_task.rb
  2. +0 −82 rakelib/const_generator.rb
  3. +3 −3 rakelib/platform.rake
  4. +0 −195 rakelib/struct_generator.rb
View
@@ -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.
View
@@ -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 <stdio.h>"
-
- @includes.each do |inc|
- f.puts "#include <#{inc}>"
- end
-
- f.puts "#include <stddef.h>\n\n"
- f.puts "int main(int argc, char **argv)\n{"
-
- @constants.each_value do |const|
- f.puts <<EOF
- #ifdef #{const.name}
- printf("#{const.name} #{const.format}\\n", #{const.cast}#{const.name});
- #endif
-EOF
- end
-
- f.puts "\n\treturn 0;\n}"
- f.flush
-
- `gcc -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall #{f.path} -o #{binary}`
- end
-
- output = `./#{binary}`
- File.unlink(binary)
-
- output.each_line do |line|
- md = line.match(/^(\S+)\s+(.*)$/)
- const = @constants[md.captures.first]
- const.value = md.captures[1]
- end
- end
-end
View
@@ -1,6 +1,6 @@
-require 'lib/ffi/struct_generator'
-require 'lib/ffi/const_generator'
-require 'lib/ffi/types_generator'
+require 'ffi/struct_generator'
+require 'ffi/const_generator'
+require 'ffi/types_generator'
deps = %w[Rakefile] + Dir['lib/ffi/*rb']
View
@@ -1,195 +0,0 @@
-require "rake"
-require "rake/tasklib"
-require "tempfile"
-
-class StructGenerator
- attr_accessor :size
- attr_reader :fields
-
- def initialize
- @struct_name = nil
- @includes = []
- @fields = []
- @found = false
- @size = nil
- end
-
- def found?
- @found
- end
-
- def get_field(name)
- @fields.find { |f| name == f.name }
- end
-
- def self.generate_from_code(code)
- sg = StructGenerator.new
- sg.instance_eval(code)
- sg.calculate
- sg.generate_layout
- end
-
- def name(n)
- @struct_name = n
- end
-
- def include(i)
- @includes << i
- end
-
- def field(name, type=nil)
- fel = Field.new(name, type)
- @fields << fel
- return fel
- end
-
- def calculate
- binary = "rb_struct_gen_bin_#{Process.pid}"
-
- raise "struct name not set" if @struct_name.nil?
-
- Tempfile.open("rbx_struct_gen_tmp") do |f|
- f.puts "#include <stdio.h>"
-
- @includes.each do |inc|
- f.puts "#include <#{inc}>"
- end
-
- f.puts "#include <stddef.h>\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

0 comments on commit febffe3

Please sign in to comment.