ext/dl/extconf.rb


DEFINITIONS

This source file includes following functions.


   1  require 'mkmf'
   2  
   3  begin # for the exception SystemExit
   4  
   5  $:.unshift File.dirname(__FILE__)
   6  require 'type'
   7  
   8  if( ARGV.include?("--help") )
   9    print <<EOF
  10    --help             print this messages
  11    --with-type-char   strictly use type 'char'
  12    --with-type-short  strictly use type 'short'
  13    --with-type-float  strictly use type 'float'
  14    --with-args=<max_arg>
  15    --with-callback=<max_callback>
  16    --enable-asm       use the embedded assembler for passing arguments.
  17                       (this option is available for i386 machine now.)
  18    --enable-dlstack   use a stack emulation for constructing function call.
  19  EOF
  20    exit(0)
  21  end
  22  
  23  ($CPPFLAGS || $CFLAGS) << " -I."
  24  
  25  if (Config::CONFIG['CC'] =~ /gcc/)  # from Win32API
  26    $CFLAGS << " -fno-defer-pop -fno-omit-frame-pointer"
  27  end
  28  
  29  if (Config::CONFIG['CC'] =~ /gcc/) && (Config::CONFIG['arch'] =~ /i.86/)
  30    $with_asm = true
  31  else
  32    $with_asm = false
  33  end
  34  $with_dlstack = ! $with_asm
  35  
  36  $with_type_int = try_run(<<EOF)
  37  int main(){ return sizeof(int) == sizeof(long); }
  38  EOF
  39  
  40  $with_type_float = try_run(<<EOF)
  41  int main(){ return sizeof(float) == sizeof(double); }
  42  EOF
  43  
  44  $with_type_voidp = try_run(<<EOF)
  45  int main(){
  46    return (sizeof(void *) == sizeof(long))
  47           || (sizeof(void *) == sizeof(int));
  48  }
  49  EOF
  50  
  51  $with_type_char  = DLTYPE[CHAR][:sym]
  52  $with_type_short = DLTYPE[SHORT][:sym]
  53  $with_type_long  = DLTYPE[LONG][:sym]
  54  $with_type_double= DLTYPE[DOUBLE][:sym]
  55  $with_type_int   &= DLTYPE[INT][:sym]
  56  $with_type_float &= DLTYPE[FLOAT][:sym]
  57  $with_type_voidp &= DLTYPE[VOIDP][:sym]
  58  
  59  $with_type_char  = enable_config("type-char", $with_type_char)
  60  $with_type_short = enable_config("type-short", $with_type_short)
  61  $with_type_float = enable_config("type-float", $with_type_float)
  62  
  63  $with_asm        = enable_config("asm", $with_asm)
  64  $with_dlstack    = enable_config("dlstack", $with_dlstack)
  65  
  66  args = with_config("args")
  67  max_arg = nil
  68  if( $with_asm || $with_dlstack )
  69    $with_type_char = true
  70    $with_type_short = true
  71    $with_type_float = true
  72    max_arg = 0
  73  end
  74  if( args )
  75    max_arg = args.to_i
  76    if( !max_arg )
  77      print("--with-args=<max_arg>\n")
  78      exit(1)
  79    end
  80  end
  81  max_arg   ||= 6
  82  
  83  max_callback = with_config("callback","10").to_i
  84  callback_types = DLTYPE.keys.length
  85  
  86  
  87  $dlconfig_h = <<EOF
  88  #define MAX_ARG           #{max_arg}
  89  EOF
  90  
  91  def dlc_define(const)
  92    $dlconfig_h << "#if !defined(#{const})\n" +
  93                   "# define #{const}\n" +
  94                   "#endif\n"
  95  end
  96  
  97  $dlconfig_h << "#define MAX_CALLBACK #{max_callback}\n"
  98  $dlconfig_h << "#define CALLBACK_TYPES #{callback_types}\n"
  99  if( $with_dlstack )
 100    $dlconfig_h << "#define USE_DLSTACK\n"
 101  else
 102    if( $with_asm )
 103      $dlconfig_h << "#define USE_INLINE_ASM\n"
 104    end
 105  end
 106  if( $with_type_char )
 107    $dlconfig_h << "#define WITH_TYPE_CHAR\n"
 108  end
 109  if( $with_type_short )
 110    $dlconfig_h << "#define WITH_TYPE_SHORT\n"
 111  end
 112  if( $with_type_long )
 113    $dlconfig_h << "#define WITH_TYPE_LONG\n"
 114  end
 115  if( $with_type_double )
 116    $dlconfig_h << "#define WITH_TYPE_DOUBLE\n"
 117  end
 118  if( $with_type_float )
 119    $dlconfig_h << "#define WITH_TYPE_FLOAT\n"
 120  end
 121  if( $with_type_int )
 122    $dlconfig_h << "#define WITH_TYPE_INT\n"
 123  end
 124  if( $with_type_voidp )
 125    $dlconfig_h << "#define WITH_TYPE_VOIDP\n"
 126  end
 127  
 128  if( have_header("dlfcn.h") )
 129    dlc_define("HAVE_DLFCN_H")
 130    have_library("dl")
 131    have_func("dlopen")
 132    have_func("dlclose")
 133    have_func("dlsym")
 134    if( have_func("dlerror") )
 135      dlc_define("HAVE_DLERROR")
 136    end
 137  elsif( have_header("windows.h") )
 138    dlc_define("HAVE_WINDOWS_H")
 139    have_func("LoadLibrary")
 140    have_func("FreeLibrary")
 141    have_func("GetProcAddress")
 142  else
 143    exit(0)
 144  end
 145  
 146  def File.update(file, str)
 147    begin
 148      open(file){|f|f.read} == str
 149    rescue Errno::ENOENT
 150      false
 151    end or open(file, "w"){|f|f.print(str)}
 152  end
 153  
 154  File.update("dlconfig.h", <<EOF)
 155  #ifndef DLCONFIG_H
 156  #define DLCONFIG_H
 157  #{$dlconfig_h}
 158  #endif /* DLCONFIG_H */
 159  EOF
 160  
 161  File.update("dlconfig.rb", <<EOF)
 162  MAX_ARG = #{max_arg}
 163  MAX_CALLBACK = #{max_callback}
 164  CALLBACK_TYPES = #{callback_types}
 165  DLTYPE[CHAR][:sym]  = #{$with_type_char}
 166  DLTYPE[SHORT][:sym] = #{$with_type_short}
 167  DLTYPE[INT][:sym]   = #{$with_type_int}
 168  DLTYPE[LONG][:sym]  = #{$with_type_long}
 169  DLTYPE[FLOAT][:sym] = #{$with_type_float}
 170  DLTYPE[DOUBLE][:sym]= #{$with_type_double}
 171  DLTYPE[VOIDP][:sym] = #{$with_type_voidp}
 172  EOF
 173  
 174  $INSTALLFILES = [
 175    ["./dlconfig.h", "$(archdir)$(target_prefix)", "."],
 176    ["dl.h", "$(archdir)$(target_prefix)", ""],
 177  ]
 178  
 179  if /bccwin32/ =~ RUBY_PLATFORM
 180    srcdir = $top_srcdir + "/ext/dl/"
 181    if !FileTest.exist?( srcdir+"dl.def.org" )
 182      File.copy( srcdir+"dl.def", srcdir+"dl.def.org" )
 183      open( srcdir+"dl.def.org" ){ |f|
 184        open( "dl.def", "w" ) { |g|
 185          g.print f.gets
 186          while line = f.gets
 187            g.print "_", line
 188          end
 189        }
 190      }
 191    end
 192  end
 193  
 194  create_makefile('dl')
 195  rescue SystemExit
 196    # do nothing
 197  end