change PWD in ENV to not confuse make
[lwes-ruby.git] / ext / lwes / extconf.rb
blob81dfb42c7e60a90132f46d7a74c7147cbf6192b0
1 require 'net/http'
2 require 'mkmf'
3 require 'fileutils'
4 dir_config('lwes')
6 pwd = File.expand_path(File.dirname(__FILE__))
7 v = '0.22.3'
8 dir = "lwes-#{v}"
9 diff = "#{pwd}/#{dir}.diff"
10 inst = "#{pwd}/.inst"
11 tgz = "#{dir}.tar.gz"
12 url = "http://sourceforge.net/projects/lwes/files/lwes-c/#{v}/#{tgz}/download"
14 def sub_cd(dir, &b)
15   oldpwd = ENV["PWD"]
16   begin
17     ENV["PWD"] = dir
18     Dir.chdir(dir, &b)
19   ensure
20     if oldpwd
21       ENV["PWD"] = oldpwd
22     else
23       ENV.delete("PWD")
24     end
25   end
26 end
28 # from Net::HTTP example
29 def fetch(uri_str, limit = 10)
30   raise ArgumentError, 'HTTP redirect too deep' if limit == 0
32   response = Net::HTTP.get_response(URI.parse(uri_str))
33   case response
34   when Net::HTTPSuccess     then response
35   when Net::HTTPRedirection then fetch(response['location'], limit - 1)
36   else
37     response.error!
38   end
39 end
41 unless have_library('lwes') && have_header('lwes.h')
42   warn "LWES library not found, building locally"
43   sub_cd(pwd) do
44     unless test ?r, tgz
45       response = fetch(url)
46       File.open("#{tgz}.#{$$}.#{rand}.tmp", "wb") do |fp|
47         fp.write(response.body)
48         File.rename(fp.path, tgz)
49       end
50     end
51     unless test ?r, "#{inst}/.ok"
52       FileUtils.rm_rf(dir)
53       system('tar', 'zxf', tgz) or abort "tar failed with #{$?}"
54       sub_cd(dir) do
55         system("patch", "-p1", "-i", diff) or abort "patch failed: #{$?}"
56         args = %w(--disable-shared
57                   --disable-hardcore
58                   --with-pic
59                   --disable-dependency-tracking)
60         system("./configure", "--prefix=#{inst}", *args) or
61           abort "configure failed with #{$?}"
62         system("make") or abort "make failed with #{$?}"
63         system("make", "install") or abort "make install failed with #{$?}"
64       end
65       FileUtils.rm_rf(dir)
66       File.open("#{inst}/.ok", "wb") { }
67     end
68     $CFLAGS = "-I#{inst}/include/lwes-0 #{$CFLAGS}"
69     $LIBS += " #{inst}/lib/liblwes.a"
70     have_header('lwes.h') or
71       abort "installation failed"
72   end
73 end
75 create_makefile('lwes_ext')