#! /usr/local/bin/ruby if ARGV[0] == 'static' $force_static = TRUE ARGV.shift elsif ARGV[0] == 'install' $install = TRUE ARGV.shift elsif ARGV[0] == 'clean' $clean = TRUE ARGV.shift end $extlist = [] $cache_mod = FALSE; $lib_cache = {} $func_cache = {} $hdr_cache = {} if File.exist?("config.cache") then f = open("config.cache", "r") while f.gets case $_ when /^lib: ([\w_]+) (yes|no)/ $lib_cache[$1] = $2 when /^func: ([\w_]+) (yes|no)/ $func_cache[$1] = $2 when /^hdr: (.+) (yes|no)/ $hdr_cache[$1] = $2 end end f.close end def older(file1, file2) if !File.exist?(file1) then return TRUE end if !File.exist?(file2) then return FALSE end if File.mtime(file1) < File.mtime(file2) return TRUE end return FALSE end LINK = "@CC@ -o conftest %s %s conftest.c %s > /dev/null 2>&1" CPP = "@CPP@ @CPPFLAGS@ %s conftest.c > /dev/null 2>&1" def have_library(lib, func) if $lib_cache[lib] if $lib_cache[lib] == "yes" if $libs $libs = $libs + " -l" + lib else $libs = "-l" + lib end return TRUE else return FALSE end end cfile = open("conftest.c", "w") cfile.printf "\ int main() { return 0; } int t() { %s(); return 0; } ", func cfile.close begin if $libs libs = "-l" + lib + " " + $libs else libs = "-l" + lib end if !system(format(LINK, $CFLAGS, $LDFLAGS, libs)) $lib_cache[lib] = 'no' $cache_mod = TRUE return FALSE end ensure system "/bin/rm -f conftest*" end $libs = libs $lib_cache[lib] = 'yes' $cache_mod = TRUE return TRUE end def have_func(func) if $func_cache[func] if $func_cache[func] == "yes" $defs.push(format("-DHAVE_%s", func.upcase)) return TRUE else return FALSE end end cfile = open("conftest.c", "w") cfile.printf "\ char %s(); int main() { return 0; } int t() { %s(); return 0; } ", func, func cfile.close libs = $libs libs = "" if libs == nil begin if !system(format(LINK, $CFLAGS, $LDFLAGS, libs)) $func_cache[func] = 'no' $cache_mod = TRUE return FALSE end ensure system "/bin/rm -f conftest*" end $defs.push(format("-DHAVE_%s", func.upcase)) $func_cache[func] = 'yes' $cache_mod = TRUE return TRUE end def have_header(header) if $hdr_cache[header] if $hdr_cache[header] == "yes" header.tr!("a-z./\055", "A-Z___") $defs.push(format("-DHAVE_%s", header)) return TRUE else return FALSE end end cfile = open("conftest.c", "w") cfile.printf "\ #include <%s> ", header cfile.close begin if !system(format(CPP, $CFLAGS)) $hdr_cache[header] = 'no' $cache_mod = TRUE return FALSE end ensure system "/bin/rm -f conftest*" end $hdr_cache[header] = 'yes' header.tr!("a-z./\055", "A-Z___") $defs.push(format("-DHAVE_%s", header)) $cache_mod = TRUE return TRUE end def create_header() if $defs.length > 0 hfile = open("extconf.h", "w") for line in $defs line =~ /^-D(.*)/ hfile.printf "#define %s 1\n", $1 end hfile.close end end def create_makefile(target) if $libs and "@DLEXT@" == "o" libs = $libs.split for lib in libs lib.sub!(/-l(.*)/, '"lib\1.a"') end $defs.push(format("-DEXTLIB='%s'", libs.join(","))) end $libs = "" if not $libs mfile = open("Makefile", "w") mfile.printf "\ SHELL = /bin/sh #### Start of system configuration section. #### srcdir = @srcdir@ VPATH = @srcdir@ CC = @CC@ CFLAGS = %s #$CFLAGS %s LDFLAGS = @LDFLAGS@ DLDFLAGS = @DLDFLAGS@ LDSHARED = @LDSHARED@ ", if $static then "" else "@CCDLFLAGS@" end, $defs.join(" ") mfile.printf "\ prefix = @prefix@ exec_prefix = @exec_prefix@ bindir = $(exec_prefix)/bin libdir = @archlib@ @SET_MAKE@ #### End of system configuration section. #### " mfile.printf "LIBS = %s\n", $libs mfile.printf "OBJS = " if !$objs then $objs = Dir["*.c"] for f in $objs f.sub!(/\.c$/, ".o") end end mfile.printf $objs.join(" ") mfile.printf "\n" dots = if "@INSTALL@" =~ /^\// then "" else "../" end mfile.printf "\ TARGET = %s.%s INSTALL = %s@INSTALL@ all: $(TARGET) clean:; @rm -f *.o *.so *.sl @rm -f Makefile extconf.h conftest.* @rm -f core ruby *~ realclean: clean ", target, if $static then "o" else "@DLEXT@" end, dots if !$static mfile.printf "\ install: $(libdir)/$(TARGET) $(libdir)/$(TARGET): $(TARGET) @test -d $(libdir) || mkdir $(libdir) $(INSTALL) $(TARGET) $(libdir)/$(TARGET) " else mfile.printf "\ install:; " end if !$static && "@DLEXT@" != "o" mfile.printf "\ $(TARGET): $(OBJS) $(LDSHARED) $(DLDFLAGS) -o $(TARGET) $(OBJS) $(LIBS) " elsif !File.exist?(target + ".c") mfile.printf "\ $(TARGET): $(OBJS) ld $(LDFLAGS) -r -o $(TARGET) $(OBJS) " end if File.exist?("depend") dfile = open("depend", "r") mfile.printf "###\n" while line = dfile.gets() mfile.printf "%s", line end dfile.close end mfile.close if $static $extlist.push [$static,target] end end def extmake(target) if $force_static or $static_ext[target] $static = target else $static = FALSE end return if $nodynamic and not $static $libs = nil $objs = nil $CFLAGS = "-I../.. @CFLAGS@" $LDFLAGS = "@STATIC@ @LDFLAGS@" begin Dir.chdir target if $static_ext.size > 0 || !File.exist?("./Makefile") || older("./Makefile", "../Setup") || older("./Makefile", "../extmk.rb") || older("./Makefile", "./extconf.rb") then $defs = [] if File.exist?("extconf.rb") load "extconf.rb" else create_makefile(target); end end if File.exist?("./Makefile") if $install system "make install" elsif $clean system "make clean" else system "make all" end end $extlibs += " " + $libs if $static && $libs ensure Dir.chdir ".." end end # get static-link modules $static_ext = {} if File.file? "./Setup" f = open("./Setup") while f.gets() $_.chop! sub!(/#.*$/, '') next if /^\s*$/ if /^option +nodynamic/ $nodynamic = TRUE next end $static_ext[$_.split[0]] = TRUE end f.close end for d in Dir["*"] File.directory?(d) || next File.file?(d + "/MANIFEST") || next d = $1 if d =~ /\/([\/]*)$/ if $install print "installing ", d, "\n" elsif $clean print "cleaning ", d, "\n" else print "compiling ", d, "\n" end extmake(d) end if $cache_mod f = open("config.cache", "w") for k,v in $lib_cache f.printf "lib: %s %s\n", k, v end for k,v in $func_cache f.printf "func: %s %s\n", k, v end for k,v in $hdr_cache f.printf "hdr: %s %s\n", k, v end f.close end exit if $install or $clean if $extlist.size > 0 for s,t in $extlist f = format("%s/%s.o", s, t) if File.exist?(f) $extinit += format("\ \tInit_%s();\n\ \trb_provide(\"%s.o\");\n\ ", t, t) $extobjs += "ext/" $extobjs += f $extobjs += " " else FALSE end end if older("extinit.c", "Setup") f = open("extinit.c", "w") f.printf "void Init_ext() {\n" f.printf $extinit f.printf "}\n" f.close end if older("extinit.o", "extinit.c") cmd = "@CC@ @CFLAGS@ -c extinit.c" print cmd, "\n" system cmd or exit 1 end Dir.chdir ".." if older("ruby", "ext/Setup") or older("ruby", "miniruby") `rm -f ruby` end $extobjs = "ext/extinit.o " + $extobjs system format('make ruby PROGRAM=ruby EXTOBJS="%s" EXTLIBS="%s"', $extobjs, $extlibs) else Dir.chdir ".." if older("ruby", "miniruby") `rm -f ruby` `cp miniruby ruby` end end #Local variables: # mode: ruby #end: