forked from globegit/postgresql-plruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extconf.rb
executable file
·267 lines (217 loc) · 6.12 KB
/
extconf.rb
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
#!/usr/bin/ruby
ARGV.collect! {|x|
x = x.sub(/\A--((?:en|dis)able)-shared\z/) { "--#$1-plruby-shared" }
}
orig_argv = ARGV.dup
require 'mkmf'
class AX
def marshal_dump
"abc"
end
end
def check_autoload(safe = 12)
File.open("a.rb", "w") {|f| f.puts "class A; end" }
autoload :A, "a.rb"
Thread.new do
begin
$SAFE = safe
A.new
rescue Exception
false
end
end.value
ensure
File.unlink("a.rb")
end
def check_md
Marshal.load(Marshal.dump(AX.new))
false
rescue
true
end
def create_lang(version = 74, suffix = '', safe = 0)
language, opaque = 'C', 'language_handler'
opaque = "opaque" if version == 72
safe = safe.to_i
trusted = if safe >= 4
"trusted"
else
""
end
puts <<-EOT
========================================================================
After the installation use *something like this* to create the language
plruby#{suffix}
create function plruby#{suffix}_call_handler() returns #{opaque}
as '#{RbConfig::CONFIG["sitearchdir"]}/plruby#{suffix}.#{RbConfig::CONFIG["DLEXT"]}'
language '#{language}';
create #{trusted} language 'plruby#{suffix}'
handler plruby#{suffix}_call_handler;
========================================================================
EOT
end
def rule(target, clean = nil)
wr = "#{target}:
\t@for subdir in $(SUBDIRS); do \\
\t\t(cd $${subdir} && $(MAKE) #{target}); \\
\tdone;
"
if clean != nil
wr << "\t@-rm tmp/* tests/tmp/* 2> /dev/null\n"
wr << "\t@rm Makefile\n" if clean
end
wr
end
include_dir, = dir_config("pgsql")
if include_dir
raise "--with-pgsql-include/--with-pgsql-dir is obsolete. Use --with-pg-config instead only if necessary."
end
pg_config = with_config('pg-config', 'pg_config')
include_dir = `#{pg_config} --includedir`.strip
$CFLAGS << " -I" << include_dir
$CFLAGS << " -I" << `#{pg_config} --includedir-server`.strip
if safe = with_config("safe-level")
safe = Integer(safe)
if safe < 0
raise "invalid value for safe #{safe}"
end
$CFLAGS += " -DSAFE_LEVEL=#{safe}"
else
safe = 12
end
if with_config("greenplum")
$CFLAGS += " -I" << File.join( include_dir, "postgresql", "internal" )
$CFLAGS += " -DWITH_GREENPLUM=1"
end
if timeout = with_config("timeout")
timeout = Integer(timeout)
if timeout < 2
raise "Invalid value for timeout #{timeout}"
end
$CFLAGS += " -DPLRUBY_TIMEOUT=#{timeout}"
if mainsafe = with_config("main-safe-level")
$CFLAGS += " -DMAIN_SAFE_LEVEL=#{mainsafe}"
end
end
if ! have_header("catalog/pg_proc.h")
raise "Some include file are missing (see README for the installation)"
end
if have_func("rb_hash_delete", "ruby.h")
$CFLAGS += " -DHAVE_RB_HASH_DELETE"
end
case version_str = `#{pg_config} --version`
when /^PostgreSQL ([7-9])\.([0-9])(\.[0-9]+)?$/
version = 10 * $1.to_i + $2.to_i
else
version = 0
end
if version < 73
raise <<-EOT
============================================================
#{version_str} is unsupported. Try plruby-0.4.2.
============================================================
EOT
end
if "aa".respond_to?(:initialize_copy, true)
$CFLAGS += " -DHAVE_RB_INITIALIZE_COPY"
end
have_func("rb_block_call")
have_header("ruby/st.h")
have_header("st.h")
if version >= 74
if !have_header("server/utils/array.h")
if !have_header("utils/array.h")
raise "I cant't find server/utils/array.h"
end
$CFLAGS += " -DPG_UTILS_ARRAY"
end
end
if macro_defined?("PG_TRY", %Q{#include "c.h"\n#include "utils/elog.h"})
$CFLAGS += " -DPG_PL_TRYCATCH"
end
have_library('pq', 'PQconnectdb')
enable_conversion = false
if enable_conversion = enable_config("conversion", true)
$CFLAGS += " -DPLRUBY_ENABLE_CONVERSION"
if check_autoload(safe.to_i)
$CFLAGS += " -DRUBY_CAN_USE_AUTOLOAD"
end
if check_md
$CFLAGS += " -DRUBY_CAN_USE_MARSHAL_DUMP"
end
end
conversions = {}
subdirs = []
if enable_conversion
Dir.foreach("src/conversions") do |dir|
next if dir[0] == ?. || !File.directory?("src/conversions/" + dir)
conversions[dir] = true
end
if !conversions.empty?
File.open("src/conversions.h", "w") do |conv|
conversions.sort.each do |key, val|
conv.puts "#include \"conversions/#{key}/conversions.h\""
subdirs << "src/conversions/#{key}"
end
end
end
end
$CFLAGS += " -DPG_PL_VERSION=#{version}"
suffix = with_config('suffix').to_s
$CFLAGS += " -DPLRUBY_CALL_HANDLER=plruby#{suffix}_call_handler"
$CFLAGS += " -DPLRUBY_VALIDATOR=plruby#{suffix}_validator"
subdirs.each do |key|
orig_argv << "--with-cflags='#$CFLAGS -I.. -I ../..'"
orig_argv << "--with-ldflags='#$LDFLAGS'"
orig_argv << "--with-cppflags='#$CPPFLAGS'"
cmd = "#{CONFIG['RUBY_INSTALL_NAME']} extconf.rb #{orig_argv.join(' ')}"
system("cd #{key}; #{cmd}")
end
subdirs.unshift("src")
begin
Dir.chdir("src")
if CONFIG["ENABLE_SHARED"] == "no"
libs = if CONFIG.key?("LIBRUBYARG_STATIC")
Config::expand(CONFIG["LIBRUBYARG_STATIC"].dup).sub(/^-l/, '')
else
Config::expand(CONFIG["LIBRUBYARG"].dup).sub(/lib([^.]*).*/, '\\1')
end
find_library(libs, "ruby_init", Config::expand(CONFIG["archdir"].dup))
end
$objs = ["plruby.o", "plplan.o", "plpl.o", "pltrans.o"] unless $objs
create_makefile("plruby#{suffix}")
ensure
Dir.chdir("..")
end
make = open("Makefile", "w")
make.print <<-EOF
SUBDIRS = #{subdirs.join(' ')}
#{rule('all')}
#{rule('clean', false)}
#{rule('distclean', true)}
#{rule('realclean', true)}
#{rule('install')}
#{rule('site-install')}
%.html: %.rd
\trd2 $< > ${<:%.rd=%.html}
plruby.html: plruby.rd
rd2: html
html: plruby.html
rdoc: docs/doc/index.html
docs/doc/index.html: $(RDOC)
\t@-(cd docs; rdoc plruby.rb)
ri:
\t@-(cd docs; rdoc -r plruby.rb)
ri-site:
\t@-(cd docs; rdoc -R plruby.rb)
test: src/$(DLLIB)
EOF
regexp = %r{\Atest/conv_(.*)}
Dir["test/*"].each do |dir|
if regexp =~ dir
next unless subdirs.include?("src/conversions/#{$1}")
end
make.puts "\t-(cd #{dir} ; RUBY='#{$ruby}' sh ./runtest #{version} #{suffix})"
end
make.close
create_lang(version, suffix, safe)