The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

SPVM::Builder::Config - Compiler and Linker Configuration for Native Classes

Description

The SPVM::Builder::Config class has methods to manipulate compiler and linker configuration for native classes.

Usage

  use SPVM::Builder::Config;
  
  # Create a config
  my $config = SPVM::Builder::Config->new(file => __FILE__);
  
  # C99
  my $config = SPVM::Builder::Config->new_c99(file => __FILE__);
  
  # GNU C99
  my $config = SPVM::Builder::Config->new_gnu99(file => __FILE__);
  
  # C++
  my $config = SPVM::Builder::Config->new_cpp(file => __FILE__);
  
  # C++11
  my $config = SPVM::Builder::Config->new_cpp11(file => __FILE__);
  
  # C++17
  my $config = SPVM::Builder::Config->new_cpp17(file => __FILE__);
  
  # Optimize
  $config->optimize('-O2');
  
  # Optimize with debug mode
  $config->optimize('-O0 -g');
  
  # Add libraries
  $config->add_lib('gdi32', 'd2d1', 'Dwrite');

  # Add source files
  $config->add_source_file('foo.c', 'bar.c', 'baz/baz.c');
  
  # Use resource
  $config->use_resource('TestCase::Resource::Zlib');
  $config->use_resource('TestCase::Resource::Foo1', mode => 'mode1', argv => ['args1', 'args2']);
  
  # Get resouce information
  my $resource = $config->get_resource('TestCase::Resource::Zlib');

Details

See SPVM::Document::NativeClass about creating native classes and those configuration.

Fields

ext

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

Gets and sets the ext field, the extension of a native class.

Examples:

  # Foo/Bar.c
  $config->ext('c');
  
  # Foo/Bar.cpp
  $config->ext('cpp');

cc

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

Gets and sets the cc field, a compiler name.

Examples:

  # gcc
  $config->cc('gcc');
  
  # g++ for C++
  $config->cc('g++');
  
  # nvcc for CUDA/GUP
  $config->cc('nvcc');
  
  # cc that compiled this Perl
  use Config;
  $config->cc($Config{cc});

include_dirs

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

Gets and sets the include_dirs field, an array reference of header file search directories.

The values of this field are converted to the -I options for the compiler "cc".

  # -I/path1 -I/path2
  $config->include_dirs('/path1', '/path2');

spvm_core_include_dir

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

Gets and sets the spvm_core_include_dir field, the SPVM core header file search directory.

This field are converted to the -I option for the compiler "cc".

native_include_dir

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

Gets and sets the native_include_dir field, the search directory for header files of this native class.

This field are converted to the -I option for the compiler "cc".

native_src_dir

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

Gets and sets the native_src_dir field, the search directory for source files of this native class.

ccflags

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

Gets and sets the ccflags field, an array reference of compiler options for the compiler "cc".

dynamic_lib_ccflags

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

Gets and sets the dynamic_lib_ccflags field, an array reference of dynamic link options for the compiler "cc".

thread_ccflags

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

Gets and sets the thread_ccflags field, an array reference of thread options for the compiler "cc".

std

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

Gets and sets the std field, a language standard.

This field is converted to the -std option for the compiler "cc".

Examples:

  # -std=c99
  $config->std('c99');
  
  # -std=gnu99
  $config->std('gnu99');
  
  # -std=cpp
  $config->std('cpp');
  
  # -std=cpp11
  $config->std('cpp11');
  
  # -std=cpp17
  $config->std('cpp17');

optimize

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

Gets and sets the optimize field, an optimization option for the compiler "cc".

Examples:

  $config->optimize('-O3');
  $config->optimize('-O2');
  $config->optimize('-g3 -O0');

source_files

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

Gets and sets the source_files field, an array reference of source files used by this native class.

The source file names are specified as relative paths from the "native_src_dir" field.

after_create_compile_info_cbs

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

Gets and sets the after_create_compile_info_cbs field, an array reference of callbacks called just after creating compilation information.

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

The 2th argument of the callback is a SPVM::Builder::CompileInfo object.

before_compile_cbs

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

Gets and sets the before_compile_cbs field, an array reference of callbacks called just before the compile command "cc" is executed.

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

The 2th argument of the callback is a SPVM::Builder::CompileInfo object.

ld

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

Gets and sets the ld field, a linker name.

Examples:

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

lib_dirs

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

Gets and sets the lib_dirs field, an array reference of library search path for the linker "ld".

This is equivalent to the -L option for the linker "ld".

libs

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

Gets and sets the libs field, an array reference of library names or SPVM::Builder::LibInfo objects for the linker "ld".

ldflags

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

Gets and sets the ldflags field, an array reference of options for the linker "ld".

dynamic_lib_ldflags

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

Gets and sets the dynamic_lib_ldflags field, an array reference of dynamic library options for the linker "ld".

thread_ldflags

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

Gets and sets the thread_ldflags field, an array reference of thread options for the linker "ld".

static_lib_ldflag

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

Gets and sets the static_lib_ldflag field, an array reference of options that specify the begining and end of a static library.

Examples:

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

ld_optimize

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

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

Examples:

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

Gets and sets the after_create_link_info_cbs field, an array reference of callbacks called just after creating link information.

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

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

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

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

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

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

force

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

Gets and sets the force field.

If this field is a true value, the compilation and linking is forced without using the cache.

If this field is a false value except for undef, the compilation and linking uses the cached if the cache exists.

If this field is undef, whether the compilation and linking is forced is not specified.

quiet

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

Gets and sets the quiet field.

If this field is a true value, compiler and linker messages are output.

If this field is a false value except for undef, compiler and linker messages are are not output.

If this field is undef, whether compiler and linker messages are output is not specified.

class_name

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

Gets and sets the class_name field, the class name configured by this config.

file

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

Gets and sets the file field, the file path of this config.

file_optional

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

Gets and sets the file_optional field.

If this field is a true value, even if the file field is not given, an exception is not thrown.

output_type

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

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

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

If thie field is static_lib, the output file is a static link library.

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

no_compile_resource

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

Gets and sets the no_compile_resource field.

If this value is a true value, all resources loaded by the "use_resource" method are disabled.

This field is mainly internal use and automatically set.

resource_loader_config

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

Gets and sets the resource_loader_config field, the config file that uses this config as a resource by the "use_resource" method.

This field is mainly internal use and automatically set.

category

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

Gets and sets the category field.

If a source file is compiled by the precompile option, the value is precompile, otherwise native.

This field is mainly internal use and automatically set.

config_exe

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

Gets and sets the config_exe field.

If an excutable file is generated by the spvmcc command, this field of each config is set to a SPVM::Builder::Config::Exe object.

This field is mainly internal use and automatically set.

cc_input_dir

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

Gets and sets the cc_input_dir field.

This field is mainly internal use and automatically set.

This field is automatically set.

cc_output_dir

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

Gets and sets the cc_output_dir field.

This field is mainly internal use and automatically set.

This field is automatically set.

output_dir

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

Gets and sets the output_dir field.

This field is mainly internal use and automatically set.

This field is automatically set.

output_file

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

Gets and sets the output_file field.

The path that a dinamic link library or an executable file is output to.

This field is mainly internal use and automatically set.

used_as_resource

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

Gets and sets the used_as_resource field.

If this field is true, this config is used as a resource.

This field is mainly internal use and automatically set.

Class Methods

new

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

Creates a new SPVM::Builder::Config object with fields, and returns it.

If a field is not defined, the field is set to the following default value.

Exceptions:

The \"file" field must be defined. Otherwise an exception is thrown.

new_c

  my $config = SPVM::Builder::Config->new_c(file => __FILE__);

Calls the "new" method and sets the "ext" field to c, and returns the return value of the "new_c" method.

new_c99

  my $config = SPVM::Builder::Config->new_c99(file => __FILE__);

Calls the "new_c" method and sets the "std" field to c99, and returns the return value of the "new_c" method.

new_c11

  my $config = SPVM::Builder::Config->new_c11(file => __FILE__);

Calls the "new_c" method and sets the "std" field to c11, and returns the return value of the "new_c" method.

new_gnu99

  my $config = SPVM::Builder::Config->new_gnu99(file => __FILE__);

Calls the "new_c" method and sets the "std" field to gnu99, and returns the return value of the "new_c" method.

new_gnu11

  my $config = SPVM::Builder::Config->new_gnu11(file => __FILE__);

Calls the "new_c" method and sets the "std" field to gnu11, and returns the return value of the "new_c" method.

new_cpp

  my $config = SPVM::Builder::Config->new_cpp(file => __FILE__);

Calls the "new" method and sets the "ext" field to cpp and sets the "cc" field to a C++ compiler and sets the "ld" field to a C++ linker, and returns the return value of the "new" method.

If the compiler included in $Config{gccversion} is clang, the "cc" field and the "ld" field are set to clang++.

Otherwise the "cc" field and the "ld" field are set to g++.

new_cpp11

  my $config = SPVM::Builder::Config->new_cpp11(file => __FILE__);

Calls the "new_cpp" method and sets the "std" field to c++11, and returns the return value of the "new_cpp" method.

new_cpp14

  my $config = SPVM::Builder::Config->new_cpp14(file => __FILE__);

Calls the "new_cpp" method and sets the "std" field to c++14, and returns the return value of the "new_cpp" method.

new_cpp17

  my $config = SPVM::Builder::Config->new_cpp17(file => __FILE__);

Calls the "new_cpp" method and sets the "std" field to c++17, and returns the return value of the "new_cpp" method.

Instance Methods

add_ccflag

  $config->add_ccflag(@ccflags);

Adds compiler options at the end of the "ccflags" field.

add_ldflag

  $config->add_ldflag(@ldflags);

Adds linker options at the end of the "ldflags" field.

add_include_dir

  $config->add_include_dir(@include_dirs);

Adds header file search directories at the end of the "include_dirs" field.

add_source_file

  $config->add_source_file(@source_files);

Adds source files used by this native class at the end of the "source_files" field.

Examples:

  $config->add_source_file('foo.c', 'bar.c');

add_after_create_compile_info_cb

  $config->add_after_create_compile_info_cb(@after_create_compile_info_cbs);

Adds callbacks called just after creating compilation information at the end of the "after_create_compile_info_cbs" field.

Examples:

  $config->add_after_create_compile_info_cb(sub {
    my ($config) = @_;
    
    my $cc = $config->cc;
    
    # Do something
  });

add_before_compile_cb

  $config->add_before_compile_cb(@before_compile_cbs);

Adds callbacks called just before the compile command "cc" is executed at the end of the "before_compile_cbs" field.

Examples:

  $config->add_before_compile_cb(sub {
    my ($config, $compile_info) = @_;
    
    my $cc_command = $compile_info->to_command;
    
    # Do something
  });

add_lib_dir

  $config->add_lib_dir(@lib_dirs);

Adds library search directories at the end of the "lib_dirs" field.

add_lib

  $config->add_lib(@libs);

Adds library names or SPVM::Builder::LibInfo objects at the end of the "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', abs => 1),
  );

add_static_lib

  $config->add_static_lib(@libs);

Adds library names or SPVM::Builder::LibInfo objects at the end of the "libs" field with the static field set to a true value.

Examples:

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

Adds callbacks called just before the link command "ld" is executed at the end of the "before_link_cbs" field.

Examples:

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

use_resource

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

Loads a resource(a SPVM::Builder::Resource object) given a resource name and options, and returns it.

See SPVM::Document::Resource about creating and using resources.

Options:

  • mode

    The same as the mode option in the new method in the SPVM::Builder::Resource class.

  • argv

    The same as the argv option in the new method in the SPVM::Builder::Resource class.

Examples:

  $config->use_resource('Resource::Zlib');
  $config->use_resource('Resource::Foo', mode => 'mode1', argv => ['args1', 'args2']);

get_resource

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

Gets a resource(a SPVM::Builder::Resource object) loaded by the "use_resource" method given a resource name, and returns it.

get_resource_names

  my $resource_names = $config->get_resource_names;

Returns resource names loaded by the "use_resource" method.

load_config

  my $config = $config->load_config($config_file, @argv);

Loads a config file, and returns a SPVM::Builder::Config object.

The argument @argv is set to the @ARGV of the config file.

load_base_config

  my $config = $config->load_base_config($config_file, @argv);

Loads the base config file of the config file given as the argument, and returns a SPVM::Builder::Config object.

The base config file is the config file that removes its mode from the config file given as the argument.

The argument @argv is set to the @ARGV of the base config file.

If the config file given as the argument is /path/Foo.devel.config, the base config file is /path/Foo.config.

Examples:

  my $config = SPVM::Builder::Config::Exe->load_base_config(__FILE__);

load_mode_config

  my $config = $config->load_mode_config($config_file, $mode, @argv);

Loads the mode config file of the config file given as the argument, and returns a SPVM::Builder::Config object.

The mode config file is the config file that removes its mode(if the mode exists) from the config file given as the argument and added the mode given as the argument.

The argument @argv is set to the @ARGV of the mode config file.

If the config file given as the argument is /path/Foo.config and the mode given as the argument is production, the mode config file is /path/Foo.production.config.

If the config file given as the argument is /path/Foo.devel.config and the mode given as the argument is production, the mode config file is /path/Foo.production.config.

  my $config = SPVM::Builder::Config::Exe->load_mode_config(__FILE__, "production");

get_loaded_config_files

Returns the config files loaded by the "load_config" method.

clone

  my $clone = $self->clone;

Clones the SPVM::Builder::Config object, and returns it.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License