Class: Magica::Command::Linker

Inherits:
Magica::Command show all
Defined in:
lib/magica/commands/linker.rb

Instance Attribute Summary collapse

Attributes inherited from Magica::Command

#build, #command

Instance Method Summary collapse

Constructor Details

#initialize(build) ⇒ Linker

Returns a new instance of Linker.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/magica/commands/linker.rb', line 5

def initialize(build)
  super

  @command = ENV['LD'] || 'ld'
  @flags = (ENV['LDFLAGS'] || [])
  @link_options = "%{flags} -o %{outfile} %{objects} %{libs}"
  @libraries = []
  @library_paths = []

  @option_library = "-l%s"
  @option_library_path = "-L%s"
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



3
4
5
# File 'lib/magica/commands/linker.rb', line 3

def flags
  @flags
end

Instance Method Details

#combine_flags(_library_paths = [], _flags = []) ⇒ Object



18
19
20
21
# File 'lib/magica/commands/linker.rb', line 18

def combine_flags(_library_paths = [], _flags = [])
  library_paths = [@library_paths, _library_paths].flatten.map { |path| @option_library_path % filename(path) }
  [flags, library_paths, _flags].flatten.uniq.join(" ")
end

#run(outfile, objects, _libraries = [], _library_paths = [], _flags = []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/magica/commands/linker.rb', line 23

def run(outfile, objects, _libraries = [], _library_paths = [], _flags = [])
  FileUtils.mkdir_p File.dirname(outfile)

  library_flags = [@libraries, _libraries].flatten.map { |library| @option_library % library }

  puts "LINK\t#{outfile}"
  _run @link_options, {
    outfile: outfile,
    objects: objects.join(" "),
    libs: library_flags.uniq.join(" "),
    flags: combine_flags(_library_paths, _flags)
  }
end