Class: Linker

Inherits:
Object
  • Object
show all
Defined in:
lib/makeconf/linker.rb

Overview

A linker combines multiple object files into a single executable or library file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinker

Returns a new instance of Linker.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/makeconf/linker.rb', line 8

def initialize
  @flags = []
  @objects = []
  @output = 'a.out'
  @shared_library = false
  @ldadd = []
  @quiet = false          # If true, output will be suppressed

  # Determine the path to the linker executable
  @path = nil
#TODO: support using Clang/GCC on windows
#if vendor == 'Microsoft'
  if Platform.is_windows?
    @path = 'LINK.EXE'
  else
    @path = 'cc' #XXX-FIXME horrible
  end

  if ENV['CC']
    @path = ENV['CC']
  end
  if ENV['LD']
    @path = ENV['LD']
  end
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



5
6
7
# File 'lib/makeconf/linker.rb', line 5

def objects
  @objects
end

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/makeconf/linker.rb', line 5

def output
  @output
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#quietObject

Returns the value of attribute quiet.



5
6
7
# File 'lib/makeconf/linker.rb', line 5

def quiet
  @quiet
end

#shared_libraryObject

Returns the value of attribute shared_library.



5
6
7
# File 'lib/makeconf/linker.rb', line 5

def shared_library
  @shared_library
end

Instance Method Details

#cloneObject



34
35
36
# File 'lib/makeconf/linker.rb', line 34

def clone
  Marshal.load(Marshal.dump(self))
end

#commandObject



104
105
106
107
108
109
110
111
112
# File 'lib/makeconf/linker.rb', line 104

def command
  # windows: 'link.exe /DLL /OUT:$@ ' + deps.join(' '))
  # linux: 'cc ' .... (see Compiler::)
  cmd = [ @path, flags, @objects, @ldadd ].flatten.join(' ')
  cmd += Platform.dev_null if @quiet
  log.debug "Linker command = `#{cmd}'"

  return cmd
end

#default_flagsObject

Try to determine a usable default set of linker flags



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/makeconf/linker.rb', line 138

def default_flags
  ldflags = []

  # GCC on Solaris 10 produces 32-bit code by default, so add -m64
  # when running in 64-bit mode.
  if Platform.is_solaris? and Platform.word_size == 64
     ldflags.push '-m64'
     ldflags.push '-R/usr/sfw/lib/amd64' if Platform.is_x86?
  end

  ldflags
end

#export_dynamicObject

Add all symbols to the dynamic symbol table (GNU ld only)



46
47
48
49
50
# File 'lib/makeconf/linker.rb', line 46

def export_dynamic
  unless Platform.is_windows?
   @flags.push 'export-dynamic'
  end
end

#flagsObject

Returns the linker flags suitable for passing to the compiler



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/makeconf/linker.rb', line 68

def flags
   tok = []

  # Set the output path
  throw 'Output pathname is required' if @output.nil?
  if Platform.is_windows?
    tok.push "/OUT:\"#{@output}"
  else
    tok.push '-o', @output
  end

  # Enable shared library output
  if @shared_library
    if Platform.is_windows?
      tok.push '/DLL'
    else
      tok.push '-shared'
      tok.push '-fpic'        # TODO: sometimes -fPIC is needed
    end
  end

  # Assume that we want to link with shared libraries
  # built within this project
  tok.push '-L', '.'

  @flags.each do |f|
     if f.kind_of?(Array)
       tok.push '-Wl,-' + f[0] + ',' + f[1]
     else
       tok.push '-Wl,-' + f
     end
  end

  return ' ' + tok.join(' ')
end

#flags=(tok) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/makeconf/linker.rb', line 124

def flags=(tok)
  @flags = default_flags
  return if tok.nil?
  if tok.kind_of?(Array)
    @flags.concat tok
  elsif tok.kind_of?(String)
    @flags.concat tok.split(' ') #XXX-broken, will not handle things like '-rpath /foo'
  else
    log.error tok.pretty_inspect
    throw 'Invalid flag type'
  end
end

#library(lib) ⇒ Object

Add one or more libraries to the list of files to link



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/makeconf/linker.rb', line 152

def library(lib)
  case lib.class.to_s
  when 'Array'
    tok = lib
  when 'String'
    tok = lib.split(' ')
  else
    throw "Invalid value: #{lib.class}"
  end
  @ldadd.concat tok
end

Execute the linker command



120
121
122
# File 'lib/makeconf/linker.rb', line 120

def link
  throw 'STUB'
end

#rpath=(dir) ⇒ Object

Override the normal search path for the dynamic linker



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/makeconf/linker.rb', line 53

def rpath=(dir)
  if Platform.is_solaris?
    @flags.push ['R', dir]
  elsif Platform.is_linux?
    @flags.push ['-rpath', dir]
  elsif Platform.is_windows?
    # XXX-FIXME Windows does not support the rpath concept
    return
  else
    throw 'Unsupported OS'
  end
  @flags.push ['-L', dir]
end

#ruleObject

Return the command formatted as a Makefile rule



115
116
117
# File 'lib/makeconf/linker.rb', line 115

def rule
   ['$(LD)', flags, '$(LDFLAGS)', @objects, @ldadd, '$(LDADD)'].flatten.join(' ')
end

#soname(s) ⇒ Object

Sets the ELF soname to the specified string



39
40
41
42
43
# File 'lib/makeconf/linker.rb', line 39

def soname(s)
  unless Platform.is_windows?
   @flags.push ['soname', s]
  end
end