-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rakefile.local
312 lines (243 loc) · 9.29 KB
/
Rakefile.local
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!rake
require 'uri'
require 'tempfile'
require 'rbconfig'
MISCDIR = BASEDIR + 'misc'
EXT_MAKEFILE = EXTDIR + 'Makefile'
EXT_SOURCES = FileList[ EXTDIR + '*.c' ]
EXT_SO = EXTDIR + "pg_ext.#{CONFIG['DLEXT']}"
NUM_CPUS = if File.exist?('/proc/cpuinfo')
File.read('/proc/cpuinfo').scan('processor').length
elsif RUBY_PLATFORM.include?( 'darwin' )
`system_profiler SPHardwareDataType | grep 'Cores' | awk '{print $5}'`.chomp
else
1
end
# Cross-compilation constants
OPENSSL_VERSION = ENV['OPENSSL_VERSION'] || '1.0.0a'
POSTGRESQL_VERSION = ENV['POSTGRESQL_VERSION'] || '9.0.1'
COMPILE_HOME = Pathname( "~/.rake-compiler" ).expand_path
STATIC_SOURCESDIR = COMPILE_HOME + 'sources'
STATIC_BUILDDIR = COMPILE_HOME + 'builds'
# Static OpenSSL build vars
STATIC_OPENSSL_BUILDDIR = STATIC_BUILDDIR + "openssl-#{OPENSSL_VERSION}"
OPENSSL_SOURCE_URI =
URI( "http://www.openssl.org/source/openssl-#{OPENSSL_VERSION}.tar.gz" )
OPENSSL_TARBALL = STATIC_SOURCESDIR + File.basename( OPENSSL_SOURCE_URI.path )
OPENSSL_MAKEFILE = STATIC_OPENSSL_BUILDDIR + 'Makefile'
LIBSSLEAY32 = STATIC_OPENSSL_BUILDDIR + 'libssleay32.a'
LIBEAY32 = STATIC_OPENSSL_BUILDDIR + 'libeay32.a'
OPENSSL_PATCHES = Rake::FileList[ MISCDIR + "openssl-#{OPENSSL_VERSION}.*.patch" ]
# Static PostgreSQL build vars
STATIC_POSTGRESQL_BUILDDIR = STATIC_BUILDDIR + "postgresql-#{POSTGRESQL_VERSION}"
POSTGRESQL_SOURCE_URI = begin
uristring = "http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/" +
"v%s/postgresql-%s.tar.gz" % [ POSTGRESQL_VERSION, POSTGRESQL_VERSION ]
URI( uristring )
end
POSTGRESQL_TARBALL = STATIC_SOURCESDIR + File.basename( POSTGRESQL_SOURCE_URI.path )
STATIC_POSTGRESQL_SRCDIR = STATIC_POSTGRESQL_BUILDDIR + 'src'
STATIC_POSTGRESQL_LIBDIR = STATIC_POSTGRESQL_SRCDIR + 'interfaces/libpq'
STATIC_POSTGRESQL_INCDIR = STATIC_POSTGRESQL_SRCDIR + 'include'
POSTGRESQL_GLOBAL_MAKEFILE = STATIC_POSTGRESQL_SRCDIR + 'Makefile.global'
POSTGRESQL_SHLIB_MAKEFILE = STATIC_POSTGRESQL_SRCDIR + 'Makefile.shlib'
POSTGRESQL_SHLIB_MF_ORIG = STATIC_POSTGRESQL_SRCDIR + 'Makefile.shlib.orig'
POSTGRESQL_LIB = STATIC_POSTGRESQL_LIBDIR + 'libpq.a'
CROSS_PREFIX = if RUBY_PLATFORM.include?( 'darwin' )
'i386-mingw32'
else
'i586-mingw32msvc'
end
# Make sure the spec data is packaged up with the gem
SPEC_DATA = Rake::FileList[ SPECDIR + 'data/*' ]
GEMSPEC.test_files += SPEC_DATA.to_a
# Clean up any testing database directories
TESTING_TMPDIRS = Rake::FileList[ "#{BASEDIR}/tmp_test_*" ]
CLOBBER.include( STATIC_SOURCESDIR.to_s, *TESTING_TMPDIRS )
# clean intermediate files and folders
CLEAN.include( STATIC_BUILDDIR.to_s )
#####################################################################
### T A S K S
#####################################################################
# Make both the default task and the spec task depend on building the extension
task :local => :compile
task :spec => :compile
namespace :spec do
task :doc => [ :compile ]
task :quiet => [ :compile ]
task :html => [ :compile ]
task :text => [ :compile ]
end
ENV['RUBY_CC_VERSION'] = '1.8.6:1.9.2'
begin
require 'rake/clean'
require 'rake/extensiontask'
require 'rake/extensioncompiler'
Rake::ExtensionTask.new do |ext|
ext.name = 'pg_ext'
ext.gem_spec = GEMSPEC
ext.ext_dir = EXTDIR.to_s
ext.lib_dir = LIBDIR.to_s
ext.source_pattern = "*.{c,h}"
# If there's an explicit 'compile' argument, use everything after it as options.
if offset = ARGV.index( 'compile' )
trace "config options = %p" % [ ARGV[(offset + 1)..-1] ]
ext.config_options = ARGV[ (offset + 1)..-1 ]
# Otherwise, just grab everything from the first option onward
elsif offset = ARGV.index( ARGV.find {|arg| arg =~ /^--/ } )
trace "config options = %p" % [ ARGV[offset..-1] ]
ext.config_options = ARGV[ offset..-1 ]
else
trace "No config options (ARGV = %p)" % [ ARGV ]
end
ext.cross_compile = true
ext.cross_platform = %w[i386-mswin32 i386-mingw32]
# configure options only for cross compile
ext.cross_config_options += [
"--with-pg-include=#{STATIC_POSTGRESQL_LIBDIR}",
"--with-opt-include=#{STATIC_POSTGRESQL_INCDIR}",
"--with-pg-lib=#{STATIC_POSTGRESQL_LIBDIR}",
"--with-opt-lib=#{STATIC_OPENSSL_BUILDDIR}",
"--enable-static-build",
]
end
#####################################################################
### C R O S S - C O M P I L A T I O N - T A S K S
#####################################################################
directory STATIC_SOURCESDIR.to_s
#
# Static OpenSSL build tasks
#
directory STATIC_OPENSSL_BUILDDIR.to_s
# openssl source file should be stored there
file OPENSSL_TARBALL => STATIC_SOURCESDIR do |t|
download( OPENSSL_SOURCE_URI, t.name )
end
# Extract the openssl builds
file STATIC_OPENSSL_BUILDDIR => OPENSSL_TARBALL do |t|
trace "extracting %s to %s" % [ OPENSSL_TARBALL, STATIC_OPENSSL_BUILDDIR.parent ]
STATIC_OPENSSL_BUILDDIR.mkpath
run 'tar', '-xzf', OPENSSL_TARBALL.to_s, '-C', STATIC_OPENSSL_BUILDDIR.parent.to_s
OPENSSL_MAKEFILE.unlink if OPENSSL_MAKEFILE.exist?
OPENSSL_PATCHES.each do |patchfile|
trace " applying patch #{patchfile}..."
run 'patch', '-Np1', '-d', STATIC_OPENSSL_BUILDDIR.to_s,
'-i', File.expand_path( patchfile, BASEDIR )
end
end
CMD_PRELUDE = [
'env',
"CC=#{CROSS_PREFIX}-gcc",
"CFLAGS=-DDSO_WIN32",
"AR=#{CROSS_PREFIX}-ar",
"RANLIB=#{CROSS_PREFIX}-ranlib"
]
# generate the makefile in a clean build location
file OPENSSL_MAKEFILE => STATIC_OPENSSL_BUILDDIR do |t|
Dir.chdir( STATIC_OPENSSL_BUILDDIR ) do
cmd = CMD_PRELUDE.dup
cmd << "./Configure" << 'mingw'
run( *cmd )
end
end
desc "compile static openssl libraries"
task :openssl_libs => [ LIBSSLEAY32, LIBEAY32 ]
task :compile_static_openssl => OPENSSL_MAKEFILE do |t|
Dir.chdir( STATIC_OPENSSL_BUILDDIR ) do
cmd = CMD_PRELUDE.dup
cmd << 'make' << "-j#{NUM_CPUS}" << 'build_libs'
run( *cmd )
end
end
desc "compile static #{LIBEAY32}"
file LIBEAY32 => :compile_static_openssl do |t|
FileUtils.cp( STATIC_OPENSSL_BUILDDIR + 'libcrypto.a', LIBEAY32.to_s )
end
desc "compile static #{LIBSSLEAY32}"
file LIBSSLEAY32 => :compile_static_openssl do |t|
FileUtils.cp( STATIC_OPENSSL_BUILDDIR + 'libssl.a', LIBSSLEAY32.to_s )
end
#
# Static PostgreSQL build tasks
#
directory STATIC_POSTGRESQL_BUILDDIR.to_s
# postgresql source file should be stored there
file POSTGRESQL_TARBALL => STATIC_SOURCESDIR do |t|
download( POSTGRESQL_SOURCE_URI, t.name )
end
# Extract the postgresql sources
file STATIC_POSTGRESQL_BUILDDIR => POSTGRESQL_TARBALL do |t|
trace "extracting %s to %s" % [ POSTGRESQL_TARBALL, STATIC_POSTGRESQL_BUILDDIR.parent ]
STATIC_POSTGRESQL_BUILDDIR.mkpath
run 'tar', '-xzf', POSTGRESQL_TARBALL.to_s, '-C', STATIC_POSTGRESQL_BUILDDIR.parent.to_s
mv POSTGRESQL_SHLIB_MAKEFILE, POSTGRESQL_SHLIB_MF_ORIG
end
# generate the makefile in a clean build location
file POSTGRESQL_GLOBAL_MAKEFILE => [ STATIC_POSTGRESQL_BUILDDIR, :openssl_libs ] do |t|
options = [
'--target=i386-mingw32',
"--host=#{Rake::ExtensionCompiler.mingw_host}",
'--with-openssl',
'--without-zlib',
'--disable-shared',
]
Dir.chdir( STATIC_POSTGRESQL_BUILDDIR ) do
configure_path = STATIC_POSTGRESQL_BUILDDIR + 'configure'
cmd = [ configure_path.to_s, *options ]
cmd << "CFLAGS=-L#{STATIC_OPENSSL_BUILDDIR}"
cmd << "LDFLAGS=-L#{STATIC_OPENSSL_BUILDDIR}"
cmd << "LDFLAGS_SL=-L#{STATIC_OPENSSL_BUILDDIR}"
cmd << "LIBS=-lwsock32 -lgdi32"
cmd << "CPPFLAGS=-I#{STATIC_OPENSSL_BUILDDIR}/include"
run( *cmd )
end
end
# patch the Makefile.shlib -- depend on the build dir so it's only
# rewritten if the tarball is re-extracted.
file POSTGRESQL_SHLIB_MAKEFILE => POSTGRESQL_SHLIB_MF_ORIG do |t|
tf = Tempfile.new( POSTGRESQL_SHLIB_MAKEFILE.basename )
POSTGRESQL_SHLIB_MF_ORIG.open( File::RDONLY ) do |ifh|
ifh.each_line do |line|
tf.print( line.sub(/^(\s*haslibarule\s*=\s*yes)/, "# \\1 ") )
end
end
tf.close
FileUtils.mv( tf.path, t.name, :verbose => $trace )
end
# make libpq.a
file POSTGRESQL_LIB => [ POSTGRESQL_GLOBAL_MAKEFILE, POSTGRESQL_SHLIB_MAKEFILE ] do |t|
Dir.chdir( POSTGRESQL_LIB.dirname ) do
sh 'make', "-j#{NUM_CPUS}", POSTGRESQL_LIB.basename.to_s, 'PORTNAME=win32'
end
end
#desc 'compile static libpg.a'
task :static_libpq => POSTGRESQL_LIB
desc 'cross compile pg for win32'
task :cross do
ENV['CROSS_COMPILING'] = 'yes'
end
task :cross => [ :mingw32, :static_libpq ]
task :mingw32 do
# Use Rake::ExtensionCompiler helpers to find the proper host
unless Rake::ExtensionCompiler.mingw_host then
warn "You need to install mingw32 cross compile functionality to be able to continue."
warn "Please refer to your distribution/package manager documentation about installation."
fail
end
end
rescue LoadError => err
task :no_rake_compiler do
log "You'll need to install rake-compiler to compile this."
fail
end
task :compile => :no_rake_compiler
task :cross => :no_rake_compiler
task :mingw32 => :no_rake_compiler
task :static_libpq => :no_rake_compiler
end
desc "Stop any Postmaster instances that remain after testing."
task :cleanup_testing_dbs do
require 'spec/lib/helpers'
PgTestingHelpers.stop_existing_postmasters()
Rake::Task[:clean].invoke
end