summaryrefslogtreecommitdiff
path: root/ext/extmk.rb.in
blob: 25bf6b9027cada4f5759218fece6ca57d451f09e (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
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
#! /usr/local/bin/ruby

def older(file1, file2)
  if !File.exists(file1) then
    return TRUE
  end
  if !File.exists(file2) then
    return FALSE
  end
  if File.mtime(file1) < File.mtime(file2)
    return TRUE
  end
  return FALSE
end

if !File.exists("./Makefile") ||
  older("./Makefile", "../extmk.rb") ||
  older("./Makefile", "./extconf.rb") then

  LINK = "@CC@ -o conftest @CFLAGS@ @LDFLAGS@ conftest.c %s > /dev/null 2>&1"
  $defs = []

  def have_library(func, lib)
    cfile = open("conftest.c", "w")
    printf cfile, "\
int main() { return 0; }
int t() { %s(); return 0; }
", func
    cfile.close

    begin
      if system(format(LINK, "-l" + lib)) != 0
	return FALSE
      end
    ensure
      system "/bin/rm -f conftest*"
    end

    if $libs
      $libs = $libs + " -l" + lib
    else
      $libs = "-l" + lib
    end
    $defs.push(format("-DHAVE_LIB%s", lib.toupper))
    return TRUE
  end

  def have_func(func)

    cfile = open("conftest.c", "w")
    printf cfile, "\
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, libs)) != 0
	return FALSE
      end
    ensure
      system "/bin/rm -f conftest*"
    end
    $defs.push(format("-DHAVE_%s", func.toupper))
    return TRUE

  end

  def create_header()
    if $defs.length > 0
      hfile = open("extconf.h", "w")
      for line in $defs
	line =~ /^-D(.*)/
	printf hfile, "#define %s 1\n", $1
      end
      hfile.close
    end
  end

  def create_makefile(target)
    mfile = open("Makefile", "w")
    printf mfile, "\
SHELL = /bin/sh

#### Start of system configuration section. ####

srcdir = @srcdir@
VPATH = @srcdir@

CC = @CC@

CFLAGS = -I../.. @CCDLFLAGS@ @CFLAGS@ %s
LDDLFLAGS = @LDDLFLAGS@
", $defs.join(" ")

    printf mfile, "\

prefix = @prefix@
binprefix = 
exec_prefix = @exec_prefix@
bindir = $(exec_prefix)/bin
@SET_MAKE@

#### End of system configuration section. ####
"
    printf mfile, "OBJS = "
    if !$objs then
      $objs = Dir["*.c"]
      for f in $objs
	f.sub(/\.c$/, ".o")
      end
    end
    printf mfile, $objs.join(" ")
    printf mfile, "\n"

    printf mfile, "\
TARGET = %s.@DLEXT@

all:		$(TARGET)

clean:;		@rm -f *.o *.so
		@rm -f Makefile extconf.h conftest.*
		@rm -f core ruby *~

realclean:	clean
", target

    if "@DLEXT@" == "so"
      printf mfile, "\
.SUFFIXES: .so $(SUFFIXES)

$(TARGET).so: $(OBJS)
	ld $(LDDLFLAGS) -o $*.so $(OBJS)
"
    elsif !File.exists(target + ".c")
      printf mfile, "\
$(TARGET).o: $(OBJS)
	ld $(LDDLFLAGS) -r $*.o $(OBJS)
"
    end

    if File.exists("depend")
      dfile = open("depend", "r")
      printf mfile, "###\n"
      while line = dfile.gets()
	printf mfile, "%s", line
      end
      dfile.close
    end
    mfile.close
  end

  if File.exists("configure") &&
    (!File.exists("config.status") ||
     File.mtime("config.status") < File.mtime("configure")) then

    system "./configure"

  end

  if File.exists("extconf.rb")
    load "extconf.rb"
  else
    Dir.pwd =~ /[^\/]+$/
    create_makefile($&);
  end

end
system "make all" if File.exists("./Makefile")

#Local variables:
# mode: ruby
#end: