File: Rakefile.cross

package info (click to toggle)
ruby-pg 1.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,200 kB
  • sloc: ansic: 7,296; ruby: 6,545; makefile: 14
file content (298 lines) | stat: -rw-r--r-- 9,688 bytes parent folder | download
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
# -*- rake -*-

require 'uri'
require 'tempfile'
require 'rbconfig'
require 'rake/clean'
require 'rake/extensiontask'
require 'rake/extensioncompiler'
require 'ostruct'

MISCDIR = BASEDIR + 'misc'

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

class CrossLibrary < OpenStruct
	include Rake::DSL

	def initialize(for_platform, openssl_config, toolchain)
		super()

		self.for_platform               = for_platform
		self.openssl_config             = openssl_config
		self.host_platform              = toolchain

		# Cross-compilation constants
		self.openssl_version            = ENV['OPENSSL_VERSION'] || '1.1.0h'
		self.postgresql_version         = ENV['POSTGRESQL_VERSION'] || '10.4'

		# Check if symlinks work in the current working directory.
		# This fails, if rake-compiler-dock is running on a Windows box.
		begin
			FileUtils.rm_f '.test_symlink'
			FileUtils.ln_s '/', '.test_symlink'
		rescue SystemCallError
			# Symlinks don't work -> use home directory instead
			self.compile_home               = Pathname( "~/.ruby-pg-build" ).expand_path
		else
			self.compile_home               = Pathname( "./build" ).expand_path
		end
		self.static_sourcesdir          = compile_home + 'sources'
		self.static_builddir            = compile_home + 'builds' + for_platform

		# Static OpenSSL build vars
		self.static_openssl_builddir    = static_builddir + "openssl-#{openssl_version}"

		self.openssl_source_uri         =
			URI( "http://www.openssl.org/source/openssl-#{openssl_version}.tar.gz" )
		self.openssl_tarball            = static_sourcesdir + File.basename( openssl_source_uri.path )
		self.openssl_makefile           = static_openssl_builddir + 'Makefile'

		self.libssl                     = static_openssl_builddir + 'libssl.a'
		self.libcrypto                  = static_openssl_builddir + 'libcrypto.a'

		self.openssl_patches            = Rake::FileList[ (MISCDIR + "openssl-#{openssl_version}.*.patch").to_s ]

		# Static PostgreSQL build vars
		self.static_postgresql_builddir = static_builddir + "postgresql-#{postgresql_version}"
		self.postgresql_source_uri      = begin
			uristring = "http://ftp.postgresql.org/pub/source/v%s/postgresql-%s.tar.bz2" %
				[ postgresql_version, postgresql_version ]
			URI( uristring )
		end
		self.postgresql_tarball         = static_sourcesdir + File.basename( postgresql_source_uri.path )

		self.static_postgresql_srcdir   = static_postgresql_builddir + 'src'
		self.static_postgresql_libdir   = static_postgresql_srcdir + 'interfaces/libpq'
		self.static_postgresql_incdir   = static_postgresql_srcdir + 'include'

		self.postgresql_global_makefile = static_postgresql_srcdir + 'Makefile.global'
		self.postgresql_shlib_makefile  = static_postgresql_srcdir + 'Makefile.shlib'
		self.postgresql_shlib_mf_orig   = static_postgresql_srcdir + 'Makefile.shlib.orig'
		self.postgresql_lib             = static_postgresql_libdir + 'libpq.dll'
		self.postgresql_patches         = Rake::FileList[ (MISCDIR + "postgresql-#{postgresql_version}.*.patch").to_s ]

		# clean intermediate files and folders
		CLEAN.include( static_builddir.to_s )


		def download(url, save_to)
			part = save_to+".part"
			sh "wget #{url.to_s.inspect} -O #{part.inspect} || curl #{url.to_s.inspect} -o #{part.inspect}"
			FileUtils.mv part, save_to
		end

		def run(*args)
			sh *args
		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|
			puts "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|
				puts "  applying patch #{patchfile}..."
				run 'patch', '-Np1', '-d', static_openssl_builddir.to_s,
				'-i', File.expand_path( patchfile, BASEDIR )
			end
		end

		self.cmd_prelude = [
			"env",
			"CROSS_COMPILE=#{host_platform}-",
			"CFLAGS=-DDSO_WIN32",
		]


		# generate the makefile in a clean build location
		file openssl_makefile => static_openssl_builddir do |t|
			chdir( static_openssl_builddir ) do
				cmd = cmd_prelude.dup
				cmd << "./Configure" << openssl_config

				run( *cmd )
			end
		end

		desc "compile static openssl libraries"
		task :openssl_libs => [ libssl, libcrypto ]

		task :compile_static_openssl => openssl_makefile do |t|
			chdir( static_openssl_builddir ) do
				cmd = cmd_prelude.dup
				cmd << 'make' << "-j#{NUM_CPUS}" << 'build_libs'

				run( *cmd )
			end
		end

		desc "compile static #{libssl}"
		file libssl => :compile_static_openssl do |t|
			rm t.name.gsub(/\.a$/, ".dll.a")
		end

		desc "compile static #{libcrypto}"
		file libcrypto => :compile_static_openssl do |t|
			rm t.name.gsub(/\.a$/, ".dll.a")
		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|
			puts "extracting %s to %s" % [ postgresql_tarball, static_postgresql_builddir.parent ]
			static_postgresql_builddir.mkpath
			run 'tar', '-xjf', postgresql_tarball.to_s, '-C', static_postgresql_builddir.parent.to_s

			postgresql_patches.each do |patchfile|
				puts "  applying patch #{patchfile}..."
				run 'patch', '-Np1', '-d', static_postgresql_builddir.to_s,
				'-i', File.expand_path( patchfile, BASEDIR )
			end
		end

		# generate the makefile in a clean build location
		file postgresql_global_makefile => [ static_postgresql_builddir, :openssl_libs ] do |t|
			options = [
				"--target=#{host_platform}",
				"--host=#{host_platform}",
				'--with-openssl',
				'--without-zlib',
			]

			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 -lws2_32"
				cmd << "CPPFLAGS=-I#{static_openssl_builddir}/include"

				run( *cmd )
			end
		end


		# make libpq.dll
		task postgresql_lib => [ postgresql_global_makefile ] do |t|
			# Work around missing dependency to libcommon in PostgreSQL-9.4.0
			chdir( static_postgresql_srcdir + "common" ) do
				sh 'make', "-j#{NUM_CPUS}"
			end

			chdir( postgresql_lib.dirname ) do
				sh 'make',
					"-j#{NUM_CPUS}",
					postgresql_lib.basename.to_s,
					'SHLIB_LINK=-lssl -lcrypto -lcrypt32 -lgdi32 -lsecur32 -lwsock32 -lws2_32'
			end
		end


		#desc 'compile libpg.a'
		task :libpq => postgresql_lib

		# copy libpq.dll to lib dir
		dest_libpq = "lib/#{postgresql_lib.basename}"
		directory File.dirname(dest_libpq)
		file dest_libpq => [postgresql_lib, File.dirname(dest_libpq)] do
			cp postgresql_lib, dest_libpq
		end

		stage_libpq = "tmp/#{for_platform}/stage/#{dest_libpq}"
		directory File.dirname(stage_libpq)
		file stage_libpq => [postgresql_lib, File.dirname(stage_libpq)] do |t|
			cp postgresql_lib, stage_libpq
		end
	end
end

if File.exist?(File.expand_path("~/.rake-compiler/config.yml"))
	CrossLibraries = [
		['i386-mingw32', 'mingw', 'i686-w64-mingw32'],
		['x64-mingw32', 'mingw64', 'x86_64-w64-mingw32'],
	].map do |platform, openssl_config, toolchain|
		CrossLibrary.new platform, openssl_config, toolchain
	end
else
	$stderr.puts "Cross-compilation disabled -- rake-compiler not properly installed"
	CrossLibraries = []
end

desc 'cross compile pg for win32'
task :cross => [ :mingw32, :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

# To reduce the gem file size strip mingw32 dlls before packaging
ENV['RUBY_CC_VERSION'].to_s.split(':').each do |ruby_version|
  task "tmp/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/pg_ext.so" do |t|
    sh "i686-w64-mingw32-strip -S tmp/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/pg_ext.so"
  end

  task "tmp/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/pg_ext.so" do |t|
    sh "x86_64-w64-mingw32-strip -S tmp/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/pg_ext.so"
  end
end

desc "Build the windows binary gems"
task 'gem:windows' => ['ChangeLog'] do
  require 'rake_compiler_dock'

  # Copy gem signing key and certs to be accessable from the docker container
  mkdir_p 'build/gem'
  sh "cp ~/.gem/gem-*.pem build/gem/ || true"
  sh "bundle package"

  RakeCompilerDock.sh <<-EOT
    mkdir ~/.gem &&
    (cp build/gem/gem-*.pem ~/.gem/ || true) &&
    bundle install --local &&
    rake cross native gem RUBY_CC_VERSION=2.5.0:2.4.0:2.3.0:2.2.2:2.1.6:2.0.0 MAKE="make -j`nproc`"
  EOT
end