Module: L::LD::Linker

Defined in:
lib/rub/l/ld.rb

Overview

An abstraction for a linker.

Constant Summary collapse

@@name_map =
{
  exe:    '%s',
  shared: 'lib%s.so',
  static: 'lib%s.a',
}

Class Method Summary collapse

Class Method Details

.available?true, false

Is this linker available on this system?

Returns:

  • (true, false)

    true if the linker is available.



107
108
109
# File 'lib/rub/l/ld.rb', line 107

def self.available?
  false
end

.builtin_library_pathArray<Pathname>

Return the linker’s builtin library path.

Returns:

  • (Array<Pathname>)


114
115
116
117
118
119
120
121
122
123
# File 'lib/rub/l/ld.rb', line 114

def self.builtin_library_path
  @builtin_library_path ||= [
    '/lib/',
    '/usr/lib/',
    '/usr/local/lib/',
    '~/.local/lib/',
  ].map do |l|
    C.path(l)
  end.uniq
end

Perform a link.

Parameters:

  • files (Set<Pathname,String>, Array<Pathname,String>, Pathname, String)

    The object files to link.

  • libs (Set<String>, Array<String>, String)

    Libraries to link with.

  • out (Pathname, String)

    The output file.

  • format (Symbol)

    The type of output to produce. One of:

    :exe

    An executable binary.

    :shared

    A shared library.

  • opt (Options)

    An options object.

Returns:

  • (R::Command)

    the process that linked the file.



152
153
154
155
156
# File 'lib/rub/l/ld.rb', line 152

def self.do_link(opt, files, libs, out, format)
  c = R::Command.new(link_command(opt, files, libs, out, format))
  c.run
  c
end

.find_lib(opt, name) ⇒ Pathname

Locate a library.

Locates the library that would be used with when linking.

Parameters:

  • name (String)

    The basename of the library.

  • options (Options)

    The options to use when linking.

Returns:

  • (Pathname)

    The path to the library.



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rub/l/ld.rb', line 191

def self.find_lib(opt, name)
  sp = library_path(opt)
  if name.to_s.include? '/'
    return C.path name
  end
  name = full_name name, (opt.static ? :static : :shared)
  
  sp.each do |d|
    l = d + name
    
    l.exist? and return l
  end
end

.full_name(base, type) ⇒ String

Generate an appropriate name.

Parameters:

  • base (String)

    The basename.

  • type (Symbol)

    The output format.

Returns:

  • (String)

    A suitable name for the output type on the current machine.



180
181
182
# File 'lib/rub/l/ld.rb', line 180

def self.full_name(base, type)
  @@name_map[type] % base
end

.library_path(opt) ⇒ Array<Pathname>

Return the path which to search for libraries.

Returns:

  • (Array<Pathname>)


128
129
130
# File 'lib/rub/l/ld.rb', line 128

def self.library_path(opt)
  opt.library_dirs + builtin_library_path
end

Generate a command to perform the link.

Parameters:

  • files (Set<Pathname,String>, Array<Pathname,String>, Pathname, String)

    The object files to link.

  • libs (Set<String>, Array<String>, String)

    Libraries to link with.

  • out (Pathname, String)

    The output file.

  • format (Symbol)

    The type of output to produce. One of:

    :exe

    An executable binary.

    :shared

    A shared library.

  • opt (Options)

    An options object.

Returns:

  • (Set<Pathname>)

    The output file.

Raises:

  • (NotImplementedError)


144
145
146
# File 'lib/rub/l/ld.rb', line 144

def self.link_command(opt, files, libs, out, format)
  raise NotImplementedError
end

.nameSymbol

The name of the linker.

Returns:

  • (Symbol)


100
101
102
# File 'lib/rub/l/ld.rb', line 100

def self.name
  :default
end

Peform a test link.

Parameters:

  • files (Set<Pathname,String>, Array<Pathname,String>, Pathname, String)

    The object files to link.

  • libs (Set<String>, Array<String>, String)

    Libraries to link with.

  • out (Pathname, String)

    The output file.

  • format (Symbol)

    The type of output to produce. One of:

    :exe

    An executable binary.

    :shared

    A shared library.

  • opt (Options)

    An options object.

Returns:

  • (true, false)

    true if the link succeeded.



162
163
164
165
166
# File 'lib/rub/l/ld.rb', line 162

def self.test_link(opt, files, libs, format)
  c = do_link(opt, files, libs, File::NULL, format)
  #p c.success?, c.stdin, c.stdout, c.stderr
  c.success?
end