Class: Magica::Command::Linker

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

Overview

:nodoc:

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.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/magica/commands/linker.rb', line 8

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.



6
7
8
# File 'lib/magica/commands/linker.rb', line 6

def flags
  @flags
end

Instance Method Details

#combine_flags(library_paths = [], flags = []) ⇒ Object



21
22
23
24
25
26
# File 'lib/magica/commands/linker.rb', line 21

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/magica/commands/linker.rb', line 28

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