Name

SPVM::Builder::Config::Linker - Config for Linking Native Classes

Inheritance

SPVM::Builder::Config::Base

Fields

ld

my $ld = $config->ld;
$config->ld($ld);

Gets and sets ld field, a linker name.

Examples:

$config->ld('g++');

lib_dirs

my $lib_dirs = $config->lib_dirs;
$config->lib_dirs($lib_dirs);

Gets and sets lib_dirs field, an array reference containing library search directories.

The values of this field are converted to -L options when the arguments of the linker "ld" are created.

# -L /path1 -L /path2
$config->lib_dirs(['/path1', '/path2']);

libs

my $libs = $config->libs;
$config->libs($libs);

Gets and sets libs field, an array reference containing library names such as z, and png or SPVM::Builder::LibInfo objects.

The values of this field are converted to -l options when the arguments of the linker "ld" are created.

See "Library Path Resolution" about resolving library paths.

Examples:

# -l libz -l libpng
$config->libs(['z', 'png']);

ldflags

my ldflags = $config->ldflags;
$config->ldflags(ldflags);

Gets and sets ldflags field, an array reference containing arguments of the linker "ld".

dynamic_lib_ldflags

my dynamic_lib_ldflags = $config->dynamic_lib_ldflags;
$config->dynamic_lib_ldflags(dynamic_lib_ldflags);

Gets and sets dynamic_lib_ldflags field, an array reference containing arguments of the linker "ld" for dynamic libraries.

This field is automatically set and users nomally do not change it.

thread_ldflags

my thread_ldflags = $config->thread_ldflags;
$config->thread_ldflags(thread_ldflags);

Gets and sets thread_ldflags field, an array reference containing arguments of the linker "ld" for threads.

This field is automatically set and users nomally do not change it.

bcrypt_ldflags

my $bcrypt_ldflags = $config->bcrypt_ldflags;
$config->bcrypt_ldflags($bcrypt_ldflags);

Gets and sets bcrypt_ldflags field, an array reference containing arguments of the linker "ld" for bcrypt.

This field is automatically set and users normally do not change it.

libcpp_ldflags

my $libcpp_ldflags = $config->libcpp_ldflags;
$config->libcpp_ldflags($libcpp_ldflags);

Gets and sets libcpp_ldflags field, an array reference containing arguments of the linker "ld" for the C++ standard library.

This field is automatically set depending on the OS, and users normally do not change it.

dynamic_lib_libcpp_ldflags

my $dynamic_lib_libcpp_ldflags = $config->dynamic_lib_libcpp_ldflags;
$config->dynamic_lib_libcpp_ldflags($dynamic_lib_libcpp_ldflags);

Gets and sets dynamic_lib_libcpp_ldflags field, an array reference containing arguments of the linker "ld" for the C++ standard library used for dynamic libraries.

This field is automatically set depending on the OS, and users normally do not change it.

exe_libcpp_ldflags

my $exe_libcpp_ldflags = $config->exe_libcpp_ldflags;
$config->exe_libcpp_ldflags($exe_libcpp_ldflags);

Gets and sets exe_libcpp_ldflags field, an array reference containing arguments of the linker "ld" for the C++ standard library used for executable files.

This field is automatically set depending on the OS, and users normally do not change it.

static_lib_ldflag

my static_lib_ldflag = $config->static_lib_ldflag;
$config->static_lib_ldflag(static_lib_ldflag);

Gets and sets static_lib_ldflag field, an array reference containing a pair of arguments to start statically linking and end it.

The library name added by the "add_static_lib" are surrounded by the values of the pair.

# -Wl,-Bstatic -llibfoo -Wl,-Bdynamic
$config->static_lib_ldflag(['-Wl,-Bstatic', '-Wl,-Bdynamic']);
$config->add_static_lib('foo');

This field is automatically set and users nomally do not change it.

This field only works correctly in Linux/Unix.

Mac does not support these options. If you want to search a static library, create a new library search directory, copy a static library to there, and add the new library search directory.

# /path_for_static_lib/libz.a
$config->add_lib_dir('/path_for_static_lib');
$config->add_lib('z');

MinGW on Windows supports these options, but instead of linking statically, it links dynamically with absolute paths. This is usually not the intended behavior. If you want to do static linking on Windows, you need to use -static option.

ld_optimize

my $ld_optimize = $config->ld_optimize;
$config->ld_optimize($ld_optimize);

Gets and sets ld_optimize field, an argument of the linker "ld" for optimization.

Examples:

$config->ld_optimize("-O3");
my $before_link_cbs = $config->before_link_cbs;
$config->before_link_cbs($before_link_cbs);

Gets and sets before_link_cbs field, an array reference containing callbacks called just before the link command "ld" is executed.

These callbacks are executed even if the link command is not actually executed because of caching.

The 1th argument of the callback is an SPVM::Builder::Config object.

The 2th argument of the callback is an SPVM::Builder::LinkInfo object.

my $after_link_cbs = $config->after_link_cbs;
$config->after_link_cbs($after_link_cbs);

Gets and sets after_link_cbs field, an array reference containing callbacks called just after the link command "ld" is executed.

These callbacks are executed even if the link command is not actually executed because of caching.

The 1st argument of the callback is an SPVM::Builder::Config object.

The 2nd argument of the callback is an SPVM::Builder::LinkInfo object.

output_type

my $output_type = $config->output_type;
$config->output_type($output_type);

Gets and sets output_type field, a type of the output file "output_file" generated by the linker "ld".

If thie field is exe, the output file is an executable file.

If thie field is dynamic_lib, the output file is a dynamic link library.

warn_ldflags

my $warn_ldflags = $config->warn_ldflags;
$config->warn_ldflags($warn_ldflags);

Gets and sets warn_ldflags field, an array reference containing arguments of the linker "ld" for warning settings.

debug_ldflags

my $debug_ldflags = $config->debug_ldflags;
$config->debug_ldflags($debug_ldflags);

Gets and sets debug_ldflags field, an array reference containing arguments of the linker "ld" for debug information.

lib_dir_option_name

my $lib_dir_option_name = $config->lib_dir_option_name;
$config->lib_dir_option_name($lib_dir_option_name);

Gets and sets lib_dir_option_name field, a string that is an option name to specify a library search path.

dynamic_lib_ext

my $dynamic_lib_ext = $config->dynamic_lib_ext;
$config->dynamic_lib_ext($dynamic_lib_ext);

Gets and sets dynamic_lib_ext field, the extension of a dynamic library such as so or dll.

The dot . is not included.

static_lib_ext

my $static_lib_ext = $config->static_lib_ext;
$config->static_lib_ext($static_lib_ext);

Gets and sets static_lib_ext field, the extension of a static library such as a or lib.

The dot . is not included.

exe_ext

my $exe_ext = $config->exe_ext;
$config->exe_ext($exe_ext);

Gets and sets exe_ext field, the extension of an executable file such as exe.

The dot . is not included.

lib_prefix

my $lib_prefix = $config->lib_prefix;
$config->lib_prefix($lib_prefix);

Gets and sets lib_prefix field, the prefix of a library name such as lib.

lib_option_suffix

my $lib_option_suffix = $config->lib_option_suffix;
$config->lib_option_suffix($lib_option_suffix);

Gets and sets lib_option_suffix field, the suffix of a library name such as .lib.

lib_option_name

my $lib_option_name = $config->lib_option_name;
$config->lib_option_name($lib_option_name);

Gets and sets lib_option_name field, a string that is an option name to specify a library name.

ld_output_option_name

my $ld_output_option_name = $config->ld_output_option_name;
$config->ld_output_option_name($ld_output_option_name);

Gets and sets ld_output_option_name field, a string that is an option name to specify a linker output file name.

output_dir

my $output_dir = $config->output_dir;
$config->output_dir($output_dir);

Gets and sets output_dir field, an output directory for the linker "ld".

This field is automatically set and users nomally do not change it.

output_file

my $output_file = $config->output_file;
$config->output_file($output_file);

Gets and sets output_file field. A path of a dinamic link library or an executable file generated by the linker "ld".

This field is automatically set and users nomally do not change it.

Class Methods

new

my $config = SPVM::Builder::Config::Linker->new(%fields);

Creates a new SPVM::Builder::Config::Linker object. This method calls the new method of the super class SPVM::Builder::Config::Base.

Field Default Values:

Instance Methods

add_ldflag

$config->add_ldflag(@ldflags);

Adds @ldflags to the end of "ldflags" field.

add_lib_dir

$config->add_lib_dir(@lib_dirs);

Adds @lib_dirs to the end of "lib_dirs" field.

add_lib

$config->add_lib(@libs);

Adds @libs to the end of "libs" field.

Examples:

$config->add_lib('gsl');
$config->add_lib('gsl', 'z');
$config->add_lib(
  SPVM::Builder::LibInfo->new(config => $config, name => 'gsl'),
  SPVM::Builder::LibInfo->new(config => $config, name => 'z', is_abs => 1),
);

add_lib_abs

$config->add_lib_abs(@libs);

Adds @libs to the end of "libs" field with SPVM::Builder::LibInfo#is_abs field set to a true value.

If a value in @libs is not an SPVM::Builder::LibInfo object, an SPVM::Builder::LibInfo object is created from the library name.

If the library is located in your user directory, it is good to use "add_lib_abs" method instead of "add_lib" method.

This is because if the generated dynamic link library has a relative path, that path cannot be resolved when it is loaded.

For system libraries, there is no problem because the linker knows the search directory for the library.

add_static_lib

$config->add_static_lib(@libs);

Adds @libs to the end of "libs" field with SPVM::Builder::LibInfo#is_static field set to a true value.

If a value in @libs is not an SPVM::Builder::LibInfo object, an SPVM::Builder::LibInfo object is created from the library name.

Examples:

$config->add_static_lib('gsl');
$config->add_static_lib('gsl', 'z');

add_static_lib_abs

$config->add_static_lib_abs(@libs);

Adds @libs to the end of "libs" field with SPVM::Builder::LibInfo#is_static field and SPVM::Builder::LibInfo#is_abs field set to a true value.

If a value in @libs is not an SPVM::Builder::LibInfo object, an SPVM::Builder::LibInfo object is created from the library name.

$config->add_before_link_cb(@before_link_cbs);

Adds @before_link_cbs to the end of "before_link_cbs" field.

Examples:

$config->add_before_link_cb(sub {
  my ($config, $link_info) = @_;
  
  my $object_files = $link_info->object_files;
  
  # Do something
  
});
$config->add_after_link_cb(@after_link_cbs);

Adds @after_link_cbs to the end of "after_link_cbs" field.

Examples:

$config->add_after_link_cb(sub {
  my ($config, $link_info) = @_;
  
  my $object_files = $link_info->object_files;
  
  # Do something
  
});

resource_loader_config

my $resource_loader_config = $config->resource_loader_config;
$config->resource_loader_config($resource_loader_config);

Gets and sets resource_loader_config field, the config file of the class that loaded a resource by "use_resource" method.

This field is automatically set and users nomally do not change it.

use_resource

my $resource = $config->use_resource($resource_name);
my $resource = $config->use_resource($resource_name, %options);

Loads a resource given a resource class name, and returns it. The return value is an SPVM::Builder::Resource object.

Examples:

$config->use_resource('Resource::MyResource');

get_resource

my $resource = $config->get_resource($resource_name);

Gets a resource loaded by "use_resource" method given a resource name, and returns it. The return value is an SPVM::Builder::Resource object.

get_resource_names

my $resource_names = $config->get_resource_names;

Returns resource names loaded by "use_resource" method.

clear_system_settings

$config->clear_system_settings;

Clears the fields that are set by default for a specific environment.

These fields might be updated in the future to support appropriate settings for different environments.

The following fields are set to [].

The following fields are set to [].

Library Path Resolution

The following is the rule of library path resolution.

Library names are converted to SPVM::Builder::LibInfo objects.

If SPVM::Builder::LibInfo#is_abs field is a false value, the linker "ld" resolves libaray paths.

If SPVM::Builder::LibInfo#is_abs field is a true value, libaray paths are resolved by the following rules.

A library is searched in the library search directories contained in "lib_dir" field from the beginning.

If SPVM::Builder::LibInfo#is_static field is a false value, the search is performed in the order of a dynamic library, a static library.

If SPVM::Builder::LibInfo#is_static field is a true value, the search is performed only in static libraries.

If a library is found, -l option of the linker "ld" is created using the found absolute path.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License