public
Created

  • Download Gist
oraconf.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
require 'mkmf'
 
unless defined? macro_defined?
# ruby 1.6 doesn't have 'macro_defined?'.
def macro_defined?(macro, src, opt="")
try_cpp(src + <<"SRC", opt)
#ifndef #{macro}
# error
#endif
SRC
end
end
 
module Logging
unless Logging.respond_to?(:open)
# emulate Logging::open of ruby 1.6.8 or later.
 
if $log.nil? # ruby 1.6.2 doesn't have $log.
$log = open('mkmf.log', 'w')
end
def Logging::open
begin
$stderr.reopen($log)
$stdout.reopen($log)
yield
ensure
$stderr.reopen($orgerr)
$stdout.reopen($orgout)
end
end
end
end # module Logging
 
module MiniRegistry
class MiniRegistryError < StandardError
attr_reader :api_name
attr_reader :code
def initialize(api_name, code)
@api_name = api_name
@code = code
end
end
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw32|bccwin32/
# Windows
require 'Win32API' # raise LoadError when UNIX.
 
# I looked in Win32Module by MoonWolf <URL:http://www.moonwolf.com/ruby/>,
# copy the minimum code and reorganize it.
ERROR_SUCCESS = 0
ERROR_FILE_NOT_FOUND = 2
ERROR_NO_MORE_ITEMS = 259
 
HKEY_LOCAL_MACHINE = 0x80000002
KEY_ENUMERATE_SUB_KEYS = 0x0008
KEY_QUERY_VALUE = 0x0001
RegOpenKeyExA = Win32API.new('advapi32', 'RegOpenKeyExA', 'LPLLP', 'L')
RegEnumKeyExA = Win32API.new('advapi32', 'RegEnumKeyExA', 'LLPPPPPP', 'L')
RegQueryValueExA = Win32API.new('advapi32','RegQueryValueExA','LPPPPP','L')
RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L')
 
def self.get_str_value(hKey, name)
lpcbData = [0].pack('L')
code = RegQueryValueExA.call(hKey, name, nil, nil, nil, lpcbData)
if code == ERROR_FILE_NOT_FOUND
return nil
elsif code != ERROR_SUCCESS
raise MiniRegistryError.new("Win32::RegQueryValueExA",code)
end
len = lpcbData.unpack('L')[0]
lpType = [0].pack('L')
lpData = "\0"*len
lpcbData = [len].pack('L')
code = RegQueryValueExA.call(hKey, name, nil, lpType, lpData, lpcbData)
if code != ERROR_SUCCESS
raise MiniRegistryError.new("Win32::RegQueryValueExA",code)
end
lpData.unpack('Z*')[0]
end
 
def self.enum_homes
phkResult = [0].pack('L')
code = RegOpenKeyExA.call(HKEY_LOCAL_MACHINE, 'SOFTWARE\ORACLE', 0, 0x20019, phkResult)
if code != ERROR_SUCCESS
raise MiniRegistryError.new("Win32::RegOpenKeyExA", code)
end
hKey = phkResult.unpack('L')[0]
idx = 0
maxkeylen = 256
loop do
lpName = "\0" * maxkeylen
lpcName = [maxkeylen].pack('L')
code = RegEnumKeyExA.call(hKey, idx, lpName, lpcName, nil, nil, nil, nil);
break if code == ERROR_NO_MORE_ITEMS
if code != ERROR_SUCCESS
RegCloseKey.call(hKey)
raise MiniRegistryError.new("Win32::RegEnumKeyEx", code)
end
code = RegOpenKeyExA.call(hKey, lpName, 0, KEY_QUERY_VALUE, phkResult)
if code != ERROR_SUCCESS
RegCloseKey.call(hKey)
raise MiniRegistryError.new("Win32::RegEnumKeyEx", code)
end
hSubkey = phkResult.unpack('L')[0]
 
name = get_str_value(hSubkey, 'ORACLE_HOME_NAME')
path = get_str_value(hSubkey, 'ORACLE_HOME')
yield name, path
RegCloseKey.call(hSubkey)
idx += 1
end
RegCloseKey.call(hKey)
end
 
end
end # module MiniRegistry
 
# minimal implementation to read information of a shared object.
class MiniSOReader
attr_reader :file_format
attr_reader :cpu
attr_reader :endian
attr_reader :bits
 
def initialize(filename)
f = open(filename, 'rb')
begin
case file_header = f.read(2)
when "\177E"
# Linux, Solaris and HP-UX(64-bit)
read_elf(f) if f.read(2) == 'LF'
when "MZ"
# Windows
read_pe(f)
when "\x02\x10"
# HP-UX PA-RISC1.1
read_parisc(f)
when "\xfe\xed"
# Big-endian Mach-O File
read_mach_o_be(f)
when "\xce\xfa"
# 32-bit Little-endian Mach-O File
read_mach_o_le(f, 32)
when "\xcf\xfa"
# 64-bit Little-endian Mach-O File
read_mach_o_le(f, 64)
when "\xca\xfe"
# Universal binary
read_mach_o_unversal(f)
else
# AIX and Tru64
raise format("unknown file header: %02x %02x", file_header[0], file_header[1])
end
ensure
f.close
end
end
 
private
# ELF format
def read_elf(f)
# 0-3 "\177ELF"
@file_format = :elf
# 4
case f.read(1).unpack('C')[0]
when 1
@bits = 32
when 2
@bits = 64
else
raise 'Invalid ELF class'
end
# 5
case f.read(1).unpack('C')[0]
when 1
@endian = :little
pack_type_short = 'v'
when 2
@endian = :big
pack_type_short = 'n'
else
raise 'Invalid ELF byte order'
end
# 6
raise 'Invalid ELF header version' if f.read(1) != "\x01"
# 16-17
f.seek(16, IO::SEEK_SET)
raise 'Invalid ELF filetype' if f.read(2).unpack(pack_type_short)[0] != 3
# 18-19
case archtype = f.read(2).unpack(pack_type_short)[0]
when 2
@cpu = :sparc
when 3
@cpu = :i386
when 15
@cpu = :parisc
when 20
@cpu = :ppc
when 21
@cpu = :ppc64
when 22
@cpu = :s390
when 43
@cpu = :sparcv9
when 50
@cpu = :ia64
when 62
@cpu = :x86_64
else
raise "Invalid ELF archtype: #{archtype}"
end
end
 
# PE/COFF format
def read_pe(f)
# 0-1 "MZ"
@file_format = :pe
# 60-63
f.seek(60, IO::SEEK_SET)
pe_offset = f.read(4).unpack('V')[0]
# read PE signature
f.seek(pe_offset)
raise 'invalid pe format' if f.read(4) != "PE\000\000"
# read COFF header
case machine = f.read(2).unpack('v')[0]
when 0x014c
@cpu = :i386
@endian = :little
@bits = 32
when 0x0200
@cpu = :ia64
@endian = :little
@bits = 64
when 0x8664
@cpu = :x86_64
@endian = :little
@bits = 64
else
raise "Invalid coff machine: #{machine}"
end
end
 
# HP-UX PA-RISC(32 bit)
def read_parisc(f)
# 0-1 system_id - CPU_PA_RISC1_1
@file_format = :pa_risc
# 2-3 a_magic - SHL_MAGIC
raise 'invalid a_magic' if f.read(2).unpack('n')[0] != 0x10e
@bits = 32
@endian = :big
@cpu = :parisc
end
 
# Big-endian Mach-O File
def read_mach_o_be(f)
@file_format = :mach_o
@endian = :big
case f.read(2)
when "\xfa\xce" # feedface
@cpu = :ppc
@bits = 32
when "\xfa\xcf" # feedfacf
@cpu = :ppc64
@bits = 64
else
raise "unknown file format"
end
end
 
def read_mach_o_le(f, bits)
@file_format = :mach_o
@endian = :little
raise 'unknown file format' if f.read(2) != "\xed\xfe"
case bits
when 32
@cpu = :i386
@bits = 32
when 64
@cpu = :x86_64
@bits = 64
end
end
 
def read_mach_o_unversal(f)
raise 'unknown file format' if f.read(2) != "\xba\xbe" # cafebabe
@file_format = :universal
nfat_arch = f.read(4).unpack('N')[0]
@cpu = []
@endian = []
@bits = []
nfat_arch.times do
case cputype = f.read(4).unpack('N')[0]
when 7
@cpu << :i386
@endian << :little
@bits << 32
when 7 + 0x01000000
@cpu << :x86_64
@endian << :little
@bits << 64
when 18
@cpu << :ppc
@endian << :big
@bits << 32
when 18 + 0x01000000
@cpu << :ppc64
@endian << :big
@bits << 64
else
raise "Unknown mach-o cputype: #{cputype}"
end
f.seek(4 * 4, IO::SEEK_CUR)
end
end
end
 
class OraConf
attr_reader :cc_is_gcc
attr_reader :version
attr_reader :cflags
attr_reader :libs
 
def initialize
raise 'use OraConf.get instead'
end
 
def self.get
original_CFLAGS = $CFLAGS
original_defs = $defs
ic_dir = nil
begin
# check Oracle instant client
if with_config('instant-client')
puts <<EOS
=======================================================
 
'--with-instant-client' is an obsolete option. ignore it.
 
=======================================================
EOS
end
ic_dir = check_ic_dir
if ic_dir
OraConfIC.new(ic_dir)
else
OraConfFC.new()
end
rescue
case ENV['LANG']
when /^ja/
lang = 'ja'
else
lang = 'en'
end
print <<EOS
---------------------------------------------------
Error Message:
#{$!.to_s.gsub(/\n/, "\n ")}
Backtrace:
#{$!.backtrace.join("\n ")}
---------------------------------------------------
See:
* http://ruby-oci8.rubyforge.org/#{lang}/HowToInstall.html
* http://ruby-oci8.rubyforge.org/#{lang}/ReportInstallProblem.html
 
EOS
exc = RuntimeError.new
exc.set_backtrace($!.backtrace)
raise exc
ensure
$CFLAGS = original_CFLAGS
$defs = original_defs
end
end
 
def self.ld_envs
@@ld_envs
end
 
private
 
def self.make_proc_to_check_cpu(expect)
Proc.new do |file|
so = MiniSOReader.new(file)
if so.cpu == expect
true
else
puts " skip: #{file} is for #{so.cpu} cpu."
false
end
end
end
 
def self.check_ic_dir
puts "checking for load library path... "
STDOUT.flush
 
# get library load path names
oci_basename = 'libclntsh'
oci_glob_postfix = '.[0-9]*'
nls_data_basename = ['libociei', 'libociicus']
@@ld_envs = %w[LD_LIBRARY_PATH]
so_ext = 'so'
nls_data_ext = nil
check_proc = nil
is_32bit = [nil].pack('p').size == 4
is_big_endian = "\x01\x02".unpack('s')[0] == 0x0102
case RUBY_PLATFORM
when /mswin32|cygwin|mingw32|bccwin32/
oci_basename = 'oci'
oci_glob_postfix = ''
nls_data_basename = ['oraociei11', 'oraociicus11', 'oraociei10', 'oraociicus10']
@@ld_envs = %w[PATH]
so_ext = 'dll'
when /i.86-linux/
check_proc = make_proc_to_check_cpu(:i386)
when /ia64-linux/
check_proc = make_proc_to_check_cpu(:ia64)
when /x86_64-linux/
# RUBY_PLATFORM depends on the compilation environment.
# Even though it is x86_64-linux, the compiled ruby may
# be a 32-bit executable.
check_proc = make_proc_to_check_cpu(is_32bit ? :i386 : :x86_64)
when /solaris/
if is_32bit
@@ld_envs = %w[LD_LIBRARY_PATH_32 LD_LIBRARY_PATH]
if is_big_endian
check_proc = make_proc_to_check_cpu(:sparc)
else
check_proc = make_proc_to_check_cpu(:i386)
end
else
@@ld_envs = %w[LD_LIBRARY_PATH_64 LD_LIBRARY_PATH]
if is_big_endian
check_proc = make_proc_to_check_cpu(:sparcv9)
else
check_proc = make_proc_to_check_cpu(:x86_64)
end
end
when /aix/
oci_glob_postfix = ''
@@ld_envs = %w[LIBPATH]
so_ext = 'a'
nls_data_ext = 'so'
when /hppa.*-hpux/
if is_32bit
@@ld_envs = %w[SHLIB_PATH]
end
so_ext = 'sl'
when /darwin/
@@ld_envs = %w[DYLD_LIBRARY_PATH]
so_ext = 'dylib'
if is_32bit
if is_big_endian
this_cpu = :ppc # 32-bit big-endian
else
this_cpu = :i386 # 32-bit little-endian
end
else
if is_big_endian
this_cpu = :ppc64 # 64-bit big-endian
else
this_cpu = :x86_64 # 64-bit little-endian
end
end
check_proc = Proc.new do |file|
so = MiniSOReader.new(file)
if so.file_format == :universal
if so.cpu.include? this_cpu
true
else
if so.cpu.size > 1
arch_types = so.cpu[0..-2].join(', ') + ' and ' + so.cpu[-1].to_s
else
arch_types = so.cpu[0]
end
puts " skip: #{file} is for #{arch_types} cpu."
false
end
else
if so.cpu == this_cpu
true
else
puts " skip: #{file} is for #{so.cpu} cpu."
false
end
end
end
end
 
glob_name = "#{oci_basename}.#{so_ext}#{oci_glob_postfix}"
ld_path = nil
file = nil
@@ld_envs.each do |env|
if ENV[env].nil?
puts " #{env} is not set."
next
end
puts " #{env}... "
ld_path, file = check_lib_in_path(ENV[env], glob_name, check_proc)
break if ld_path
end
 
if ld_path.nil?
case RUBY_PLATFORM
when /linux/
open("|/sbin/ldconfig -p") do |f|
print " checking ld.so.conf... "
STDOUT.flush
while line = f.gets
if line =~ /libclntsh\.so\..* => (\/.*)\/libclntsh\.so\.(.*)/
file = "#$1/libclntsh.so.#$2"
path = $1
next if (check_proc && !check_proc.call(file))
ld_path = path
puts "yes"
break
end
end
puts "no"
end
when /solaris/
if is_32bit
crle_cmd = 'crle'
else
crle_cmd = 'crle -64'
end
open('|env LANG=C /usr/bin/' + crle_cmd) do |f|
while line = f.gets
if line =~ /Default Library Path[^:]*:\s*(\S*)/
puts " checking output of `#{crle_cmd}'... "
ld_path, file = check_lib_in_path($1, glob_name, check_proc)
break
end
end
end
end
end
 
if ld_path
nls_data_ext ||= so_ext # nls_data_ext is same with so_ext by default.
nls_data_basename.each do |basename|
if File.exist?(File.join(ld_path, "#{basename}.#{nls_data_ext}"))
puts " #{file} looks like an instant client."
return ld_path
end
end
puts " #{file} looks like a full client."
end
nil
end
 
def self.check_lib_in_path(paths, glob_name, check_proc)
paths.split(File::PATH_SEPARATOR).each do |path|
next if path.nil? or path == ''
print " checking #{path}... "
path.gsub!(/\\/, '/') if /mswin32|cygwin|mingw32|bccwin32/ =~ RUBY_PLATFORM
files = Dir.glob(File.join(path, glob_name))
if files.empty?
puts "no"
next
end
STDOUT.flush
next if (check_proc && !check_proc.call(files[0]))
puts "yes"
return path, files[0]
end
end
 
def init
check_cc()
@cc_is_gcc = check_cc_is_gcc()
@lp64 = check_lp64()
check_ruby_header()
end
 
def check_cc
print "checking for cc... "
STDOUT.flush
if try_run("int main() { return 0; }")
puts "ok"
else
puts "ng"
raise "C compiler doesn't work correctly."
end
end # check_cc
 
def check_cc_is_gcc
# bcc defines __GNUC__. why??
return false if RUBY_PLATFORM =~ /bccwin32/
 
print "checking for gcc... "
STDOUT.flush
if macro_defined?("__GNUC__", "")
print "yes\n"
return true
else
print "no\n"
return false
end
end # check_cc_is_gcc
 
def check_lp64
print "checking for LP64... "
STDOUT.flush
if try_run("int main() { return sizeof(long) == 8 ? 0 : 1; }")
puts "yes"
true
else
puts "no"
false
end
end # check_lp64
 
def check_ruby_header
print "checking for ruby header... "
STDOUT.flush
rubyhdrdir = Config::CONFIG["rubyhdrdir"] || Config::CONFIG['archdir']
unless File.exist?(rubyhdrdir + '/ruby.h')
puts "ng"
if RUBY_PLATFORM =~ /darwin/ and File.exist?("#{Config::CONFIG['archdir']}/../universal-darwin8.0/ruby.h")
raise <<EOS
#{Config::CONFIG['archdir']}/ruby.h doesn't exist.
Run the following commands to fix the problem.
 
cd #{Config::CONFIG['archdir']}
sudo ln -s ../universal-darwin8.0/* ./
EOS
else
raise <<EOS
#{Config::CONFIG['archdir']}/ruby.h doesn't exist.
Install the ruby development library.
EOS
end
end
if RUBY_PLATFORM =~ /linux/ and not File.exist?("/usr/include/sys/types.h")
raise <<EOS
Do you install glibc-devel(redhat) or libc6-dev(debian)?
You need /usr/include/sys/types.h to compile ruby-oci8.
EOS
end
puts "ok"
$stdout.flush
end # check_ruby_header
 
def try_link_oci
original_libs = $libs
begin
$libs += " -L#{CONFIG['libdir']} " + @libs
have_func("OCIInitialize", "oci.h")
ensure
$libs = original_libs
end
end
 
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw32|bccwin32/ # when Windows
 
def get_libs(base_dir = oci_base_dir)
case RUBY_PLATFORM
when /cygwin/
open("OCI.def", "w") do |f|
f.puts("EXPORTS")
open("|nm #{base_dir}/LIB/MSVC/OCI.LIB") do |r|
while line = r.gets
f.puts($') if line =~ / T _/
end
end
end
command = "dlltool -d OCI.def -D OCI.DLL -l libOCI.a"
print("Running: '#{command}' ...")
STDOUT.flush
system(command)
puts("done")
"-L. -lOCI"
when /bccwin32/
# replace '/' to '\\' because bcc linker misunderstands
# 'C:/foo/bar/OCI.LIB' as unknown option.
lib = "#{base_dir}/LIB/BORLAND/OCI.LIB"
return lib.tr('/', '\\') if File.exist?(lib)
raise <<EOS
#{lib} does not exist.
 
Your Oracle may not support Borland C++.
If you want to run this module, run the following command at your own risk.
cd #{base_dir.tr('/', '\\')}\\LIB
mkdir Borland
cd Borland
coff2omf ..\\MSVC\\OCI.LIB OCI.LIB
EOS
exit 1
else
"\"#{base_dir}/LIB/MSVC/OCI.LIB\""
end
end
 
end
end
 
# OraConf for Full Client
class OraConfFC < OraConf
def initialize
init
 
@oracle_home = get_home()
if RUBY_PLATFORM =~ /freebsd/ && @oracle_home == '/usr/local/oracle8-client'
@version = '817'
else
@version = get_version()
end
@cflags = get_cflags()
$CFLAGS += @cflags
 
if !@lp64 && File.exist?("#{@oracle_home}/lib32")
# ruby - 32bit
# oracle - 64bit
use_lib32 = true
else
use_lib32 = false
end
 
# default
if @version.to_i >= 900
if use_lib32
lib_dir = "#{@oracle_home}/lib32"
else
lib_dir = "#{@oracle_home}/lib"
end
case RUBY_PLATFORM
when /solaris/
@libs = " -L#{lib_dir} -R#{lib_dir} -lclntsh"
when /linux/
@libs = " -L#{lib_dir} -Wl,-rpath,#{lib_dir} -lclntsh"
else
@libs = " -L#{lib_dir} -lclntsh"
end
return if try_link_oci()
end
 
# get from demo_rdbms.mk
if use_lib32
if File.exist?("#{@oracle_home}/rdbms/demo/demo_rdbms32.mk")
@libs = get_libs('32', '')
else
@libs = get_libs('', '32')
end
else
@libs = get_libs()
end
return if try_link_oci()
 
raise 'cannot compile OCI'
end
 
private
 
def get_version
print("Get the version of Oracle from SQL*Plus... ")
STDOUT.flush
version = nil
dev_null = RUBY_PLATFORM =~ /mswin32|mingw32|bccwin32/ ? "nul" : "/dev/null"
if File.exist?("#{@oracle_home}/bin/plus80.exe")
sqlplus = "plus80.exe"
else
sqlplus = "sqlplus"
end
Logging::open do
ENV['NLS_LANG'] = 'american_america.us7ascii'
open("|#{@oracle_home}/bin/#{sqlplus} < #{dev_null}") do |f|
while line = f.gets
print line
if line =~ /(\d+)\.(\d)\.(\d)/
version = $1 + $2 + $3
break
end
end
end
end
if version.nil?
raise 'cannot get Oracle version from sqlplus'
end
puts version
version
end # get_version
 
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw32|bccwin32/ # when Windows
 
def is_valid_home?(oracle_home)
return false if oracle_home.nil?
sqlplus = "#{oracle_home}/bin/sqlplus.exe"
print("checking for ORACLE_HOME(#{oracle_home})... ")
STDOUT.flush
if File.exist?(sqlplus)
puts("yes")
true
else
puts("no")
false
end
end
 
def get_home()
oracle_home = ENV['ORACLE_HOME']
if oracle_home.nil?
struct = Struct.new("OracleHome", :name, :path)
oracle_homes = []
MiniRegistry.enum_homes do |name, path|
path.chomp!("\\") if path
oracle_homes << struct.new(name, path) if is_valid_home?(path)
end
if oracle_homes.empty?
raise <<EOS
Set the environment variable ORACLE_HOME if Oracle Full Client.
Append the path of Oracle client libraries to #{OraConf.ld_envs[0]} if Oracle Instant Client.
EOS
end
if oracle_homes.length == 1
oracle_home = oracle_homes[0].path
else
default_path = ''
if RUBY_PLATFORM =~ /cygwin/
path_sep = ':'
dir_sep = '/'
else
path_sep = ';'
dir_sep = '\\'
end
ENV['PATH'].split(path_sep).each do |path|
path.chomp!(dir_sep)
if File.exist?("#{path}/OCI.DLL")
default_path = path
break
end
end
puts "---------------------------------------------------"
puts "Multiple Oracle Homes are found."
printf " %-15s : %s\n", "[NAME]", "[PATH]"
oracle_homes.each do |home|
if RUBY_PLATFORM =~ /cygwin/
path = `cygpath -u '#{home.path}'`.chomp!
else
path = home.path
end
if default_path.downcase == "#{path.downcase}#{dir_sep}bin"
oracle_home = home
end
printf " %-15s : %s\n", home.name, home.path
end
if oracle_home.nil?
puts "default oracle home is not found."
puts "---------------------------------------------------"
raise 'Cannot get ORACLE_HOME. Please set the environment valiable ORACLE_HOME.'
else
printf "use %s\n", oracle_home.name
puts "run ohsel.exe to use another oracle home."
puts "---------------------------------------------------"
oracle_home = oracle_home.path
end
end
end
if RUBY_PLATFORM =~ /cygwin/
oracle_home = oracle_home.sub(/^([a-zA-Z]):/, "/cygdrive/\\1")
end
oracle_home.gsub(/\\/, '/')
end
 
def oci_base_dir
case @version
when /80./
"#{@oracle_home}/OCI80"
else
"#{@oracle_home}/OCI"
end
end
 
def get_cflags
unless File.exist?("#{oci_base_dir}/INCLUDE/OCI.H")
raise "'#{oci_base_dir}/INCLUDE/OCI.H' does not exists. Please install 'Oracle Call Interface'."
end
if RUBY_PLATFORM =~ /cygwin/
" \"-I#{oci_base_dir}/INCLUDE\" -D_int64=\"long long\""
else
" \"-I#{oci_base_dir}/INCLUDE\""
end
end
 
else # when UNIX
 
def get_home
oracle_home = ENV['ORACLE_HOME']
if oracle_home.nil?
msg = <<EOS
Set the environment variable ORACLE_HOME if Oracle Full Client.
Append the path of Oracle client libraries to #{OraConf.ld_envs[0]} if Oracle Instant Client.
EOS
 
# check sudo environment
sudo_command = ENV['SUDO_COMMAND']
if /\w*make\b/ =~ sudo_command
msg += <<EOS
 
The 'sudo' command unset some environment variables for security reasons.
Use it only when running 'make install' as follows
make
sudo make install
EOS
end
if /\w+\/gem\b/ =~ sudo_command
msg += <<EOS
 
The 'sudo' command unset some environment variables for security reasons.
Pass required varialbes as follows
sudo env #{OraConf.ld_envs[0]}=$#{OraConf.ld_envs[0]} #{sudo_command}
or
sudo env ORACLE_HOME=$ORACLE_HOME #{sudo_command}
EOS
end
raise msg
end
oracle_home
end
 
def get_cflags
cflags = ''
ok = false
original_CFLAGS = $CFLAGS
begin
for i in ["rdbms/public", "rdbms/demo", "network/public", "plsql/public"]
cflags += " -I#{@oracle_home}/#{i}"
$CFLAGS += " -I#{@oracle_home}/#{i}"
print("try #{cflags}\n");
if have_header("oci.h")
ok = true
break
end
end
unless ok
if @version.to_i >= 1000
oci_h = "#{@oracle_home}/rdbms/public/oci.h"
else
oci_h = "#{@oracle_home}/rdbms/demo/oci.h"
end
unless File.exist?(oci_h)
raise "'#{oci_h}' does not exists. Install 'Oracle Call Interface' component."
end
raise 'Cannot get proper cflags.'
end
cflags
ensure
$CFLAGS = original_CFLAGS
end
end # get_cflags
 
def get_libs(postfix1 = '', postfix2 = "")
print("Running make for $ORACLE_HOME/rdbms/demo/demo_rdbms#{postfix1}.mk (build#{postfix2}) ...")
STDOUT.flush
 
make_opt = "CC='echo MARKER' EXE=/dev/null ECHODO=echo ECHO=echo GENCLNTSH='echo genclntsh'"
if @cc_is_gcc && /solaris/ =~ RUBY_PLATFORM
# suggested by Brian Candler.
make_opt += " KPIC_OPTION= NOKPIC_CCFLAGS#{postfix2}="
end
 
command = "|make -f #{@oracle_home}/rdbms/demo/demo_rdbms#{postfix1}.mk build#{postfix2} #{make_opt}"
marker = /^\s*MARKER/
echo = /^\s*echo/
libs = nil
Logging::open do
puts command
open(command, "r") do |f|
while line = f.gets
puts line
line.chomp!
line = $' while line =~ echo
if line =~ marker
# found a line where a compiler runs.
libs = $'
libs.gsub!(/-o\s+\/dev\/null/, "")
libs.gsub!(/-o\s+build/, "")
end
end
end
end
raise 'Cannot get proper libs.' if libs.nil?
print("OK\n")
 
case RUBY_PLATFORM
when /hpux/
if @cc_is_gcc
# strip +DA2.0W, +DS2.0, -Wl,+s, -Wl,+n
libs.gsub!(/\+DA\S+(\s)*/, "")
libs.gsub!(/\+DS\S+(\s)*/, "")
libs.gsub!(/-Wl,\+[sn](\s)*/, "")
end
libs.gsub!(/ -Wl,/, " ")
when /aix/
if @cc_is_gcc
# strip -bI:/xxx
libs.gsub!(/(-bI:\S+)/, '')
end
end
 
# check whether object files are included.
if /\S+\.o\b/ =~ libs
 
# monkey patching!
Object.module_eval do
alias :link_command_pre_oci8 :link_command
def link_command(*args)
args[1] = "" if args[1] == $libs
link_command_pre_oci8(*args)
end
end
 
end
libs
end # get_libs
end
end
 
# OraConf for Instant Client
class OraConfIC < OraConf
def initialize(ic_dir)
init
 
if ic_dir =~ /^\/usr\/lib(?:64)?\/oracle\/(\d+(?:\.\d+)*)\/client(64)?\/lib(?:64)?/
# rpm package
# x86 rpms after 11.1.0.7.0:
# library: /usr/lib/oracle/X.X/client/lib/
# include: /usr/include/oracle/X.X/client/
#
# x86_64 rpms after 11.1.0.7.0:
# library: /usr/lib/oracle/X.X/client64/lib/
# include: /usr/include/oracle/X.X/client64/
#
# x86 rpms before 11.1.0.6.0:
# library: /usr/lib/oracle/X.X.X.X/client/lib/
# include: /usr/include/oracle/X.X.X.X/client/
#
# x86_64 rpms before 11.1.0.6.0:
# library: /usr/lib/oracle/X.X.X.X/client64/lib/
# include: /usr/include/oracle/X.X.X.X/client64/
#
# third-party x86_64 rpms(*1):
# library: /usr/lib64/oracle/X.X.X.X/client/lib/
# or /usr/lib64/oracle/X.X.X.X/client/lib64/
# include: /usr/include/oracle/X.X.X.X/client/
#
# *1 These had been used before Oracle released official x86_64 rpms.
#
lib_dir = ic_dir
inc_dir = "/usr/include/oracle/#{$1}/client#{$2}"
else
# zip package
lib_dir = ic_dir
inc_dir = "#{ic_dir}/sdk/include"
end
 
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw32|bccwin32/ # when Windows
unless File.exist?("#{ic_dir}/sdk/lib/msvc/oci.lib")
raise <<EOS
Could not compile with Oracle instant client.
#{ic_dir}/sdk/lib/msvc/oci.lib could not be found.
EOS
raise 'failed'
end
@cflags = " \"-I#{inc_dir}\""
@cflags += " -D_int64=\"long long\"" if RUBY_PLATFORM =~ /cygwin/
@libs = get_libs("#{ic_dir}/sdk")
ld_path = nil
else
@cflags = " -I#{inc_dir}"
# set ld_path and so_ext
case RUBY_PLATFORM
when /aix/
ld_path = 'LIBPATH'
so_ext = 'a'
when /hppa.*-hpux/
if @lp64
ld_path = 'LD_LIBRARY_PATH'
else
ld_path = 'SHLIB_PATH'
end
so_ext = 'sl'
when /darwin/
ld_path = 'DYLD_LIBRARY_PATH'
so_ext = 'dylib'
else
ld_path = 'LD_LIBRARY_PATH'
so_ext = 'so'
end
# check Oracle client library.
unless File.exist?("#{lib_dir}/libclntsh.#{so_ext}")
files = Dir.glob("#{lib_dir}/libclntsh.#{so_ext}.*")
if files.empty?
raise <<EOS
Could not compile with Oracle instant client.
'#{lib_dir}/libclntsh.#{so_ext}' could not be found.
Did you install instantclient-basic?
EOS
else
file = File.basename(files.sort[-1])
raise <<EOS
Could not compile with Oracle instant client.
#{lib_dir}/libclntsh.#{so_ext} could not be found.
You may need to make a symbolic link.
cd #{lib_dir}
ln -s #{file} libclntsh.#{so_ext}
EOS
end
raise 'failed'
end
@libs = " -L#{lib_dir} -lclntsh "
end
unless File.exist?("#{inc_dir}/oci.h")
raise <<EOS
'#{inc_dir}/oci.h' does not exist.
Install 'Instant Client SDK'.
EOS
end
$CFLAGS += @cflags
if try_link_oci()
major = try_constant("OCI_MAJOR_VERSION", "oci.h")
minor = try_constant("OCI_MINOR_VERSION", "oci.h")
if major and minor
@version = format('%d%d0', major, minor)
else
# 10.1.0 doesn't have OCI_MAJOR_VERSION and OCI_MINOR_VERSION in oci.h.
@version = "1010"
end
return
end
 
if RUBY_PLATFORM =~ /darwin/
open('mkmf.log', 'r') do |f|
while line = f.gets
if line.include? '/libclntsh.dylib load command 8 unknown cmd field'
raise <<EOS
Intel mac instant client is for Mac OS X 10.5.
It doesn't work on Mac OS X 10.4 or before.
 
You have three workarounds.
1. Compile ruby as ppc binary and use it with ppc instant client.
2. Use JRuby and JDBC
3. Use a third-party ODBC driver and ruby-odbc.
EOS
# '
end
 
case line
when /cputype \(\d+, architecture \w+\) does not match cputype \(\d+\) for specified -arch flag: (\w+)/
missing_arch = $1
when /Undefined symbols for architecture (\w+)/
missing_arch = $1
when /missing required architecture (\w+) in file/
missing_arch = $1
end
 
if missing_arch
if [nil].pack('p').size == 8
my_arch = 'x86_64'
elsif "\x01\x02".unpack('s')[0] == 0x0201
my_arch = 'i386'
else
my_arch = 'ppc'
end
raise <<EOS
Could not compile with Oracle instant client.
You may need to set the environment variable RC_ARCHS or ARCHFLAGS as follows:
 
RC_ARCHS=#{my_arch}
export RC_ARCHS
or
ARCHFLAGS='-arch #{my_arch}'
export RC_ARCHS
 
If it does not fix the problem, delete all '-arch #{missing_arch}'
in '#{Config::CONFIG['archdir']}/rbconfig.rb'.
EOS
end
end
end
end
 
unless ld_path.nil?
raise <<EOS
Could not compile with Oracle instant client.
You may need to set a environment variable:
#{ld_path}=#{lib_dir}
export #{ld_path}
EOS
end
raise 'failed'
end
end
oratest.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <ociap.h>
#include <ruby.h>
 
#define chk(func) do { \
sword rv = func; \
fprintf(stderr, " %s => %d\n", #func, rv); \
} while (0)
 
void Init_oratest1()
{
OCIEnv *envhp;
chk(OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL));
}
 
void Init_oratest2()
{
OCIEnv *envhp;
chk(OCIEnvCreate(&envhp, OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL));
}
 
void Init_oratest3()
{
OCIEnv *envhp;
chk(OCIEnvCreate(&envhp, OCI_OBJECT|OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL));
}
 
void Init_oratest4()
{
OCIEnv *envhp;
chk(OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL));
chk(OCIEnvInit(&envhp, OCI_DEFAULT, 0, NULL));
}
 
void Init_oratest5()
{
OCIEnv *envhp;
chk(OCIInitialize(OCI_OBJECT, NULL, NULL, NULL, NULL));
chk(OCIEnvInit(&envhp, OCI_DEFAULT, 0, NULL));
}
 
void Init_oratest6()
{
OCIEnv *envhp;
chk(OCIInitialize(OCI_OBJECT|OCI_THREADED, NULL, NULL, NULL, NULL));
chk(OCIEnvInit(&envhp, OCI_DEFAULT, 0, NULL));
}
oratest.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
require 'mkmf'
require 'rbconfig'
 
require File.dirname(__FILE__) + '/oraconf'
oraconf = OraConf.get()
 
$CFLAGS += oraconf.cflags
$libs += oraconf.libs
 
cfg = ::Config::CONFIG
 
rubypath = cfg['bindir'] + '/' + cfg['ruby_install_name']
if arg = cfg['configure_args'].split.detect {|a| /--with-make-prog=/ =~ a }
makeprog = arg.sub(/'/, '').split(/=/, 2)[1]
else
makeprog = 'make'
end
 
extname = 'oratest.' + cfg['DLEXT']
 
create_makefile('oratest')
File.exists? 'oratest.o' and File.delete 'oratest.o'
system(makeprog)
 
puts '=' * 60
 
 
1.upto 6 do |num|
puts "Testing oratest#{num}..."
target_name = "oratest#{num}.#{cfg['DLEXT']}"
File.delete(target_name) rescue nil
File.symlink(extname, target_name)
system(rubypath + " -e \"require './#{target_name}'\"")
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.