summaryrefslogtreecommitdiff
path: root/ext/zlib/extconf.rb
blob: fd2f16852293f4787ea75ad50340a8f0ac867d65 (plain)
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
# frozen_string_literal: true
#
# extconf.rb
#
# $Id$
#

require 'mkmf'
require 'rbconfig'

dir_config 'zlib'


if %w'z libz zlib1 zlib zdll zlibwapi'.find {|z| have_library(z, 'deflateReset')} and
    have_header('zlib.h') then
  have_zlib = true
else
  unless File.directory?(zsrc = "#{$srcdir}/zlib")
    dirs = Dir.open($srcdir) {|z| z.grep(/\Azlib-\d+[.\d]*\z/) {|x|"#{$srcdir}/#{x}"}}
    dirs.delete_if {|x| !File.directory?(x)}
    zsrc = dirs.max_by {|x| x.scan(/\d+/).map(&:to_i)}
  end
  if zsrc
    addconf = [
      "ZSRC = $(srcdir)/#{File.basename(zsrc)}\n",
      "all:\n",
    ]
    $INCFLAGS << " -I$(ZSRC)"
    if $mswin or $mingw
      dll = "zlib1.dll"
      $extso << dll
      $cleanfiles << "$(topdir)/#{dll}" << "$(ZIMPLIB)"
      zmk = "\t$(MAKE) -f $(ZMKFILE) TOP=$(ZSRC)"
      if $nmake
        zmkfile = "$(ZSRC)/win32/Makefile.msc"
        m = "#{zsrc}/win32/Makefile.msc"
      else
        zmkfile = "$(ZSRC)/win32/Makefile.gcc"
        m = "#{zsrc}/win32/Makefile.gcc"
        zmk += " PREFIX="
        zmk << CONFIG['CC'][/(.*-)gcc([^\/]*)\z/, 1]
        zmk << " CC=$(CC)" if $2
      end
      m = File.read(m)
      zimplib = m[/^IMPLIB[ \t]*=[ \t]*(\S+)/, 1]
      $LOCAL_LIBS << " " << zimplib
      unless $nmake or /^TOP[ \t]/ =~ m
        m.gsub!(/win32\/zlib\.def/, '$(TOP)/\&')
        m.gsub!(/^(\t.*[ \t])(\S+\.rc)/, '\1-I$(<D) $<')
        m = "TOP = .\n""VPATH=$(TOP)\n" + m
        zmkfile = File.basename(zmkfile)
        File.rename(zmkfile, zmkfile+".orig") if File.exist?(zmkfile)
        File.write(zmkfile, m)
      end
      addconf.push(
        "ZMKFILE = #{zmkfile}\n",
        "ZIMPLIB = #{zimplib}\n",
        "$(TARGET_SO): $(ZIMPLIB)\n",
        "$(ZIMPLIB):\n",
        "#{zmk} $@\n",
        "install-so: $(topdir)/#{dll}",
        "$(topdir)/#{dll}: $(ZIMPLIB)\n",
        "\t$(Q) $(COPY) #{dll} $(@D)\n",
        "clean: clean-zsrc\n",
        "clean-zsrc:\n",
        "#{zmk} clean\n",
      )
    end
    Logging.message "using zlib in #{zsrc}\n"
    $defs << "-DHAVE_ZLIB_H"
    have_zlib = true
  end
end

if have_zlib
  defines = []

  Logging::message 'checking for kind of operating system... '
  os_code = with_config('os-code') ||
    case RUBY_PLATFORM.split('-',2)[1]
    when 'amigaos' then
      os_code = 'AMIGA'
    when /mswin|mingw|bccwin/ then
      # NOTE: cygwin should be regarded as Unix.
      os_code = 'WIN32'
    else
      os_code = 'UNIX'
    end
  os_code = 'OS_' + os_code.upcase

  OS_NAMES = {
    'OS_MSDOS'   => 'MS-DOS',
    'OS_AMIGA'   => 'Amiga',
    'OS_VMS'     => 'VMS',
    'OS_UNIX'    => 'Unix',
    'OS_ATARI'   => 'Atari',
    'OS_MACOS'   => 'MacOS',
    'OS_TOPS20'  => 'TOPS20',
    'OS_WIN32'   => 'Win32',
    'OS_VMCMS'   => 'VM/CMS',
    'OS_ZSYSTEM' => 'Z-System',
    'OS_CPM'     => 'CP/M',
    'OS_QDOS'    => 'QDOS',
    'OS_RISCOS'  => 'RISCOS',
    'OS_UNKNOWN' => 'Unknown',
  }
  unless OS_NAMES.key? os_code then
    raise "invalid OS_CODE `#{os_code}'"
  end
  Logging::message "#{OS_NAMES[os_code]}\n"
  defines << "OS_CODE=#{os_code}"

  $defs.concat(defines.collect{|d|' -D'+d})

  if zsrc
    $defs << "-DHAVE_CRC32_COMBINE"
    $defs << "-DHAVE_ADLER32_COMBINE"
    $defs << "-DHAVE_TYPE_Z_CRC_T"
  else
    have_func('crc32_combine', 'zlib.h')
    have_func('adler32_combine', 'zlib.h')
    have_type('z_crc_t', 'zlib.h')
  end

  create_makefile('zlib') {|conf|
    if zsrc
      conf.concat addconf if addconf
    end
    conf
  }

end