Class: Rscons::Builders::Object

Inherits:
Rscons::Builder show all
Defined in:
lib/rscons/builders/object.rb

Overview

A default Rscons builder which knows how to produce an object file from various types of source files.

Constant Summary collapse

KNOWN_SUFFIXES =

Mapping of known sources from which to build object files.

{
  "AS" => "ASSUFFIX",
  "CC" => "CSUFFIX",
  "CXX" => "CXXSUFFIX",
  "DC" => "DSUFFIX",
}

Instance Method Summary collapse

Methods inherited from Rscons::Builder

#create_build_target, #features, #name, #setup, #standard_build, #standard_finalize, #standard_threaded_build

Instance Method Details

#default_variables(env) ⇒ Hash

Return default construction variables for the builder.

Parameters:

  • env (Environment)

    The Environment using the builder.

Returns:

  • (Hash)

    Default construction variables for the builder.

[View source]

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
# File 'lib/rscons/builders/object.rb', line 20

def default_variables(env)
  {
    'OBJSUFFIX' => ['.o'],
    'DEPFILESUFFIX' => '.mf',

    'CPPDEFPREFIX' => '-D',
    'INCPREFIX' => '-I',

    'AS' => '${CC}',
    'ASFLAGS' => [],
    'ASSUFFIX' => ['.S'],
    'ASPPPATH' => '${CPPPATH}',
    'ASPPFLAGS' => '${CPPFLAGS}',
    'ASDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
    'ASCMD' => ['${AS}', '-c', '-o', '${_TARGET}', '${ASDEPGEN}', '${INCPREFIX}${ASPPPATH}', '${ASPPFLAGS}', '${ASFLAGS}', '${_SOURCES}'],

    'CPPFLAGS' => ['${CPPDEFPREFIX}${CPPDEFINES}'],
    'CPPDEFINES' => [],
    'CPPPATH' => [],

    'CCFLAGS' => [],

    'CC' => 'gcc',
    'CFLAGS' => [],
    'CSUFFIX' => ['.c'],
    'CCDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
    'CCCMD' => ['${CC}', '-c', '-o', '${_TARGET}', '${CCDEPGEN}', '${INCPREFIX}${CPPPATH}', '${CPPFLAGS}', '${CFLAGS}', '${CCFLAGS}', '${_SOURCES}'],

    'CXX' => 'g++',
    'CXXFLAGS' => [],
    'CXXSUFFIX' => ['.cc', '.cpp', '.cxx', '.C'],
    'CXXDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
    'CXXCMD' =>['${CXX}', '-c', '-o', '${_TARGET}', '${CXXDEPGEN}', '${INCPREFIX}${CPPPATH}', '${CPPFLAGS}', '${CXXFLAGS}', '${CCFLAGS}', '${_SOURCES}'],

    'DC' => 'gdc',
    'DFLAGS' => [],
    'DSUFFIX' => ['.d'],
    'DDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
    'D_IMPORT_PATH' => [],
    'DCCMD' => ['${DC}', '-c', '-o', '${_TARGET}', '${DDEPGEN}', '${INCPREFIX}${D_IMPORT_PATH}', '${DFLAGS}', '${_SOURCES}'],
  }
end

#finalize(options) ⇒ String?

Finalize the build operation.

Parameters:

  • options (Hash)

    Builder finalize options.

Returns:

  • (String, nil)

    Name of the target file on success or nil on failure.

[View source]

114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rscons/builders/object.rb', line 114

def finalize(options)
  if options[:command_status]
    target, deps, cache, env, vars = options.values_at(:target, :sources, :cache, :env, :vars)
    if File.exists?(vars['_DEPFILE'])
      deps += Environment.parse_makefile_deps(vars['_DEPFILE'])
      FileUtils.rm_f(vars['_DEPFILE'])
    end
    cache.register_build(target, options[:tc].command, deps.uniq, env)
    target
  end
end

#produces?(target, source, env) ⇒ Boolean

Return whether this builder object is capable of producing a given target file name from a given source file name.

Parameters:

  • target (String)

    The target file name.

  • source (String)

    The source file name.

  • env (Environment)

    The Environment.

Returns:

  • (Boolean)

    Whether this builder object is capable of producing a given target file name from a given source file name.

[View source]

76
77
78
79
80
81
# File 'lib/rscons/builders/object.rb', line 76

def produces?(target, source, env)
  target.end_with?(*env['OBJSUFFIX']) and
    KNOWN_SUFFIXES.find do |compiler, suffix_var|
      source.end_with?(*env[suffix_var])
    end
end

#run(options) ⇒ String, ThreadedCommand

Run the builder to produce a build target.

Parameters:

  • options (Hash)

    Builder run options.

Returns:

[View source]

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rscons/builders/object.rb', line 90

def run(options)
  target, sources, cache, env, vars = options.values_at(:target, :sources, :cache, :env, :vars)
  vars = vars.merge({
    '_TARGET' => target,
    '_SOURCES' => sources,
    '_DEPFILE' => Rscons.set_suffix(target, env.expand_varref("${DEPFILESUFFIX}", vars)),
  })
  com_prefix = KNOWN_SUFFIXES.find do |compiler, suffix_var|
    sources.first.end_with?(*env.expand_varref("${#{suffix_var}}", vars))
  end.tap do |v|
    v.nil? and raise "Error: unknown input file type: #{sources.first.inspect}"
  end.first
  command = env.build_command("${#{com_prefix}CMD}", vars)
  # Store vars back into options so new keys are accessible in #finalize.
  options[:vars] = vars
  standard_threaded_build("#{com_prefix} #{target}", target, command, sources, env, cache)
end