Module: PlatformInfo

Defined in:
lib/phusion_passenger/platform_info.rb

Overview

This module autodetects various platform-specific information, and provides that information through constants.

Users can change the detection behavior by setting the environment variable APXS2 to the correct ‘apxs’ (or ‘apxs2’) binary, as provided by Apache.

Constant Summary collapse

RUBY =

The absolute path to the current Ruby interpreter.

GEM =

The correct ‘gem’ command for this Ruby interpreter.

locate_ruby_executable('gem')

Class Method Summary collapse

Class Method Details

.apache2_bindirObject

The absolute path to the Apache 2 ‘bin’ directory.



312
313
314
315
316
317
318
# File 'lib/phusion_passenger/platform_info.rb', line 312

def self.apache2_bindir
	if apxs2.nil?
		return nil
	else
		return `#{apxs2} -q BINDIR 2>/dev/null`.strip
	end
end

.apache2_module_cflags(with_apr_flags = true) ⇒ Object

The C compiler flags that are necessary to compile an Apache module. Possibly includes APR and APU compiler flags.



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/phusion_passenger/platform_info.rb', line 384

def self.apache2_module_cflags(with_apr_flags = true)
	flags = ["-fPIC"]
	if with_apr_flags
		flags << apr_flags
		flags << apu_flags
	end
	if !apxs2.nil?
		apxs2_flags = `#{apxs2} -q CFLAGS`.strip << " -I" << `#{apxs2} -q INCLUDEDIR`.strip
		apxs2_flags.gsub!(/-O\d? /, '')

		# Remove flags not supported by GCC
		if RUBY_PLATFORM =~ /solaris/ # TODO: Add support for people using SunStudio
			# The big problem is Coolstack apxs includes a bunch of solaris -x directives.
			options = apxs2_flags.split
			options.reject! { |f| f =~ /^\-x/ }
			options.reject! { |f| f =~ /^\-Xa/ }
			options.reject! { |f| f =~ /^\-fast/ }
			options.reject! { |f| f =~ /^\-mt/ }
			apxs2_flags = options.join(' ')
		end
		
		apxs2_flags.strip!
		flags << apxs2_flags
	end
	if !httpd.nil? && RUBY_PLATFORM =~ /darwin/
		# The default Apache install on OS X is a universal binary.
		# Figure out which architectures it's compiled for and do the same
		# thing for mod_passenger. We use the 'file' utility to do this.
		#
		# Running 'file' on the Apache executable usually outputs something
		# like this:
		#
		#   /usr/sbin/httpd: Mach-O universal binary with 4 architectures
		#   /usr/sbin/httpd (for architecture ppc7400):     Mach-O executable ppc
		#   /usr/sbin/httpd (for architecture ppc64):       Mach-O 64-bit executable ppc64
		#   /usr/sbin/httpd (for architecture i386):        Mach-O executable i386
		#   /usr/sbin/httpd (for architecture x86_64):      Mach-O 64-bit executable x86_64
		#
		# But on some machines, it may output just:
		#
		#   /usr/sbin/httpd: Mach-O fat file with 4 architectures
		#
		# (http://code.google.com/p/phusion-passenger/issues/detail?id=236)
		output = `file "#{httpd}"`.strip
		if output =~ /Mach-O fat file/ && output !~ /for architecture/
			architectures = ["-arch i386 -arch ppc -arch x86_64 -arch ppc64"]
		else
			architectures = []
			output.split("\n").grep(/for architecture/).each do |line|
				line =~ /for architecture (.*?)\)/
				architectures << "-arch #{$1}"
			end
		end
		flags << architectures.compact.join(' ')
	end
	return flags.compact.join(' ').strip
end

.apache2_module_ldflagsObject

Linker flags that are necessary for linking an Apache module. Possibly includes APR and APU linker flags.



445
446
447
448
449
# File 'lib/phusion_passenger/platform_info.rb', line 445

def self.apache2_module_ldflags
	flags = "-fPIC #{apr_libs} #{apu_libs}"
	flags.strip!
	return flags
end

.apache2_sbindirObject

The absolute path to the Apache 2 ‘sbin’ directory.



322
323
324
325
326
327
328
# File 'lib/phusion_passenger/platform_info.rb', line 322

def self.apache2_sbindir
	if apxs2.nil?
		return nil
	else
		return `#{apxs2} -q SBINDIR`.strip
	end
end

.apache2ctlObject

The absolute path to the ‘apachectl’ or ‘apache2ctl’ binary.



234
235
236
# File 'lib/phusion_passenger/platform_info.rb', line 234

def self.apache2ctl
	return find_apache2_executable('apache2ctl', 'apachectl2', 'apachectl')
end

.apr_configObject

The absolute path to the ‘apr-config’ or ‘apr-1-config’ executable.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/phusion_passenger/platform_info.rb', line 258

def self.apr_config
	if env_defined?('APR_CONFIG')
		return ENV['APR_CONFIG']
	elsif apxs2.nil?
		return nil
	else
		filename = `#{apxs2} -q APR_CONFIG 2>/dev/null`.strip
		if filename.empty?
			apr_bindir = `#{apxs2} -q APR_BINDIR 2>/dev/null`.strip
			if apr_bindir.empty?
				return nil
			else
				return select_executable(apr_bindir,
					"apr-1-config", "apr-config")
			end
		elsif File.exist?(filename)
			return filename
		else
			return nil
		end
	end
end

.apr_config_needed_for_building_apache_modules?Boolean

Returns whether it is necessary to use information outputted by ‘apr-config’ and ‘apu-config’ in order to compile an Apache module. When Apache is installed with –with-included-apr, the APR/APU headers are placed into the same directory as the Apache headers, and so ‘apr-config’ and ‘apu-config’ won’t be necessary in that case.

Returns:

  • (Boolean)


481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/phusion_passenger/platform_info.rb', line 481

def self.apr_config_needed_for_building_apache_modules?
	filename = File.join("/tmp/passenger-platform-check-#{Process.pid}.c")
	File.open(filename, "w") do |f|
		f.puts("#include <apr.h>")
	end
	begin
		return !system("(gcc #{apache2_module_cflags(false)} -c '#{filename}' -o '#{filename}.o') >/dev/null 2>/dev/null")
	ensure
		File.unlink(filename) rescue nil
		File.unlink("#{filename}.o") rescue nil
	end
end

.apr_flagsObject

The C compiler flags that are necessary for programs that use APR.



453
454
455
# File 'lib/phusion_passenger/platform_info.rb', line 453

def self.apr_flags
	return determine_apr_info[0]
end

.apr_libsObject

The linker flags that are necessary for linking programs that use APR.



458
459
460
# File 'lib/phusion_passenger/platform_info.rb', line 458

def self.apr_libs
	return determine_apr_info[1]
end

.apu_configObject

The absolute path to the ‘apu-config’ or ‘apu-1-config’ executable.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/phusion_passenger/platform_info.rb', line 283

def self.apu_config
	if env_defined?('APU_CONFIG')
		return ENV['APU_CONFIG']
	elsif apxs2.nil?
		return nil
	else
		filename = `#{apxs2} -q APU_CONFIG 2>/dev/null`.strip
		if filename.empty?
			apu_bindir = `#{apxs2} -q APU_BINDIR 2>/dev/null`.strip
			if apu_bindir.empty?
				return nil
			else
				return select_executable(apu_bindir,
					"apu-1-config", "apu-config")
			end
		elsif File.exist?(filename)
			return filename
		else
			return nil
		end
	end
end

.apu_flagsObject

The C compiler flags that are necessary for programs that use APR-Util.



463
464
465
# File 'lib/phusion_passenger/platform_info.rb', line 463

def self.apu_flags
	return determine_apu_info[0]
end

.apu_libsObject

The linker flags that are necessary for linking programs that use APR-Util.



468
469
470
# File 'lib/phusion_passenger/platform_info.rb', line 468

def self.apu_libs
	return determine_apu_info[1]
end

.apxs2Object

The absolute path to the ‘apxs’ or ‘apxs2’ executable, or nil if not found.



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/phusion_passenger/platform_info.rb', line 219

def self.apxs2
	if env_defined?("APXS2")
		return ENV["APXS2"]
	end
	['apxs2', 'apxs'].each do |name|
		command = find_command(name)
		if !command.nil?
			return command
		end
	end
	return nil
end

.debugging_cflagsObject

C compiler flags that should be passed in order to enable debugging information.



371
372
373
374
375
376
377
378
379
380
# File 'lib/phusion_passenger/platform_info.rb', line 371

def self.debugging_cflags
	if RUBY_PLATFORM =~ /openbsd/
		# According to OpenBSD's pthreads man page, pthreads do not work
		# correctly when an app is compiled with -g. It recommends using
		# -ggdb instead.
		return '-ggdb'
	else
		return '-g'
	end
end

.find_command(name) ⇒ Object

Check whether the specified command is in $PATH, and return its absolute filename. Returns nil if the command is not found.

This function exists because system(‘which’) doesn’t always behave correctly, for some weird reason.



188
189
190
191
192
193
194
195
196
# File 'lib/phusion_passenger/platform_info.rb', line 188

def self.find_command(name)
	ENV['PATH'].split(File::PATH_SEPARATOR).detect do |directory|
		path = File.join(directory, name.to_s)
		if File.executable?(path)
			return path
		end
	end
	return nil
end

.httpdObject

The absolute path to the Apache binary (that is, ‘httpd’, ‘httpd2’, ‘apache’ or ‘apache2’).



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/phusion_passenger/platform_info.rb', line 240

def self.httpd
	if env_defined?('HTTPD')
		return ENV['HTTPD']
	elsif apxs2.nil?
		["apache2", "httpd2", "apache", "httpd"].each do |name|
			command = find_command(name)
			if !command.nil?
				return command
			end
		end
		return nil
	else
		return find_apache2_executable(`#{apxs2} -q TARGET`.strip)
	end
end

.library_extensionObject

The current platform’s shared library extension (‘so’ on most Unices).



496
497
498
499
500
501
502
# File 'lib/phusion_passenger/platform_info.rb', line 496

def self.library_extension
	if RUBY_PLATFORM =~ /darwin/
		return "bundle"
	else
		return "so"
	end
end

.linux_distroObject

An identifier for the current Linux distribution. nil if the operating system is not Linux.



505
506
507
508
509
510
511
512
# File 'lib/phusion_passenger/platform_info.rb', line 505

def self.linux_distro
	tags = linux_distro_tags
	if tags
		return tags.first
	else
		return nil
	end
end

.linux_distro_tagsObject

Autodetects the current Linux distribution and return a number of identifier tags. The first tag identifies the distribution while the other tags indicate which distributions it is likely compatible with. Returns nil if the operating system is not Linux.



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/phusion_passenger/platform_info.rb', line 518

def self.linux_distro_tags
	if RUBY_PLATFORM !~ /linux/
		return nil
	end
	lsb_release = read_file("/etc/lsb-release")
	if lsb_release =~ /Ubuntu/
		return [:ubuntu, :debian]
	elsif File.exist?("/etc/debian_version")
		return [:debian]
	elsif File.exist?("/etc/redhat-release")
		redhat_release = read_file("/etc/redhat-release")
		if redhat_release =~ /CentOS/
			return [:centos, :redhat]
		elsif redhat_release =~ /Fedora/
			return [:fedora, :redhat]
		elsif redhat_release =~ /Mandriva/
			return [:mandriva, :redhat]
		else
			# On official RHEL distros, the content is in the form of
			# "Red Hat Enterprise Linux Server release 5.1 (Tikanga)"
			return [:rhel, :redhat]
		end
	elsif File.exist?("/etc/suse-release")
		return [:suse]
	elsif File.exist?("/etc/gentoo-release")
		return [:gentoo]
	else
		return [:unknown]
	end
	# TODO: Slackware
end

.portability_cflagsObject

Compiler flags that should be used for compiling every C/C++ program, for portability reasons. These flags should be specified as last when invoking the compiler.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/phusion_passenger/platform_info.rb', line 338

def self.portability_cflags
	flags = ["-D_REENTRANT -I/usr/local/include"]
	if RUBY_PLATFORM =~ /solaris/
		flags << '-D_XOPEN_SOURCE=500 -D_XPG4_2 -D__EXTENSIONS__ -D__SOLARIS__ -D_FILE_OFFSET_BITS=64'
		flags << '-DBOOST_HAS_STDINT_H' unless RUBY_PLATFORM =~ /solaris2.9/
		flags << '-D__SOLARIS9__ -DBOOST__STDC_CONSTANT_MACROS_DEFINED' if RUBY_PLATFORM =~ /solaris2.9/
		flags << '-mcpu=ultrasparc' if RUBY_PLATFORM =~ /sparc/
	elsif RUBY_PLATFORM =~ /openbsd/
		flags << '-DBOOST_HAS_STDINT_H -D_GLIBCPP__PTHREADS'
	elsif RUBY_PLATFORM =~ /aix/
		flags << '-DOXT_DISABLE_BACKTRACES'
	elsif RUBY_PLATFORM =~ /(sparc-linux|arm-linux|sh4-linux)/
		# http://code.google.com/p/phusion-passenger/issues/detail?id=200
		# http://groups.google.com/group/phusion-passenger/t/6b904a962ee28e5c
		flags << '-DBOOST_SP_USE_PTHREADS'
	end
	return flags.compact.join(" ").strip
end

.portability_ldflagsObject

Linker flags that should be used for linking every C/C++ program, for portability reasons. These flags should be specified as last when invoking the linker.



361
362
363
364
365
366
367
# File 'lib/phusion_passenger/platform_info.rb', line 361

def self.portability_ldflags
	if RUBY_PLATFORM =~ /solaris/
		return '-lxnet -lrt -lsocket -lnsl -lpthread'
	else
		return '-lpthread'
	end
end

.rakeObject

Returns the absolute path to the Rake executable that belongs to the current Ruby interpreter. Returns nil if it doesn’t exist.



205
206
207
# File 'lib/phusion_passenger/platform_info.rb', line 205

def self.rake
	return locate_ruby_executable('rake')
end

.rspecObject

Returns the absolute path to the RSpec runner program that belongs to the current Ruby interpreter. Returns nil if it doesn’t exist.



213
214
215
# File 'lib/phusion_passenger/platform_info.rb', line 213

def self.rspec
	return locate_ruby_executable('spec')
end