class CommonLibraryBuilder

This file lists all the Phusion Passenger C++ support library files and contains code for calculating how to compile and how to link them into executables. It's used by the build system (build/*.rb) and by src/ruby_supportlib/phusion_passenger/config/nginx_engine_compiler.rb

Attributes

all_components[R]
output_dir[R]
selected_components[R]

Public Class Methods

new(&block) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 34
def initialize(&block)
  @all_components = {}
  @all_ordered_components = []
  @selected_components = {}
  @namespace = "common"
  if defined?(COMMON_OUTPUT_DIR)
    @output_dir = COMMON_OUTPUT_DIR + "libpassenger_common"
  else
    @output_dir = "."
  end
  instance_eval(&block) if block
end

Public Instance Methods

define_component(object_name, options) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 54
def define_component(object_name, options)
  @all_components[object_name] = options
  @all_ordered_components << object_name
  @selected_components[object_name] = options
end
define_tasks(extra_compiler_flags = nil) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 98
def define_tasks(extra_compiler_flags = nil)
  group_all_components_by_category.each_pair do |category, object_names|
    define_category_tasks(category, object_names, extra_compiler_flags)
  end

  task("#{@namespace}:clean") do
    sh "rm -rf #{@output_dir}"
  end

  self
end
enable_optimizations!(lto = false) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 91
def enable_optimizations!(lto = false)
  @default_optimization_level = "-O"
  if lto
    @default_extra_optimization_flags = "-flto"
  end
end
exclude(*selector) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 64
def exclude(*selector)
  dup.send(:exclude!, *selector)
end
initialize_copy(other) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 47
def initialize_copy(other)
  [:all_components, :all_ordered_components, :selected_components, :namespace, :output_dir].each do |name|
    var_name = "@#{name}"
    instance_variable_set(var_name, other.instance_variable_get(var_name).dup)
  end
end
only(*selector) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 60
def only(*selector)
  dup.send(:only!, *selector)
end
set_namespace(namespace) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 68
def set_namespace(namespace)
  dup.send(:set_namespace!, namespace)
end
set_output_dir(dir) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 72
def set_output_dir(dir)
  dup.send(:set_output_dir!, dir)
end

Private Instance Methods

apply_selector(*selector) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 184
def apply_selector(*selector)
  result = {}
  selector = [selector].flatten
  selector.each do |condition|
    @selected_components.each do |object_name, options|
      if component_satisfies_condition?(object_name, options, condition)
        result[object_name] = options
      end
    end
  end
  return result
end
category_complete?(category) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 216
def category_complete?(category)
  expected = 0
  actual   = 0
  @all_components.each_value do |options|
    if options[:category] == category
      expected += 1
    end
  end
  @selected_components.each_value do |options|
    if options[:category] == category
      actual += 1
    end
  end
  return expected == actual
end
component_satisfies_condition?(object_name, options, condition) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 197
def component_satisfies_condition?(object_name, options, condition)
  case condition
  when Symbol
    return condition == :all || options[:category] == condition
  when String
    return object_name == condition
  else
    raise ArgumentError, "Invalid condition #{condition.inspect}"
  end
end
define_category_tasks(category, object_names, extra_compiler_flags) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 111
def define_category_tasks(category, object_names, extra_compiler_flags)
  object_names.each do |object_name|
    define_object_compilation_task(object_name, extra_compiler_flags)
  end

  object_filenames = object_filenames_for(object_names)
  task "#{@namespace}:clean" do
    sh "rm -f #{object_filenames.join(' ')}"
  end
end
define_object_compilation_task(object_name, extra_compiler_flags) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 122
def define_object_compilation_task(object_name, extra_compiler_flags)
  options     = @all_components[object_name]
  source_file = locate_source_file(options[:source])
  object_file = "#{@output_dir}/#{object_name}"

  case options[:optimize]
  when :light
    optimization_level = "-O"
  when true, :heavy
    optimization_level = "-O2"
  when :very_heavy
    optimization_level = "-O3"
  when nil
    optimization_level = @default_optimization_level
  else
    raise "Unknown optimization level #{options[:optimize]}"
  end

  optimize = "#{optimization_level} #{@default_extra_optimization_flags}".strip

  if options[:strict_aliasing] == false # and not nil
    optimize = "#{optimize} -fno-strict-aliasing"
    # Disable link-time optimization so that we can no-strict-aliasing
    # works: http://stackoverflow.com/a/25765338/20816
    optimize.sub!(/-flto/, "")
  end

  define_c_or_cxx_object_compilation_task(
    object_file,
    source_file,
    :include_paths => CXX_SUPPORTLIB_INCLUDE_PATHS,
    :flags => [
      LIBEV_CFLAGS,
      optimize,
      extra_compiler_flags
    ]
  )
end
exclude!(*selector) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 177
def exclude!(*selector)
  apply_selector(*selector).each_key do |object_name|
    @selected_components.delete(object_name)
  end
  return self
end
group_all_components_by_category() click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 246
def group_all_components_by_category
  categories = {}
  @all_ordered_components.each do |object_name|
    options  = @all_components[object_name]
    category = options[:category]
    categories[category] ||= []
    categories[category] << object_name
  end
  return categories
end
locate_source_file(path) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 257
def locate_source_file(path)
  "src/cxx_supportlib/#{path}"
end
object_filenames_for(object_names) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 242
def object_filenames_for(object_names)
  return object_names.map { |name| "#{@output_dir}/#{name}" }
end
only!(*selector) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 171
def only!(*selector)
  new_components = apply_selector(*selector)
  @selected_components = new_components
  return self
end
selected_categories() click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 208
def selected_categories
  categories = {}
  @selected_components.each_value do |options|
    categories[options[:category]] = true
  end
  return categories.keys
end
selected_objects_beloging_to_category(category) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 232
def selected_objects_beloging_to_category(category)
  result = []
  @selected_components.each_pair do |object_name, options|
    if options[:category] == category
      result << object_name
    end
  end
  return result
end
set_namespace!(namespace) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 161
def set_namespace!(namespace)
  @namespace = namespace
  return self
end
set_output_dir!(dir) click to toggle source
# File src/ruby_supportlib/phusion_passenger/common_library.rb, line 166
def set_output_dir!(dir)
  @output_dir = dir
  return self
end