The Perl Advent Calendar needs more articles for 2022. Submit your idea today!

Name

SPVM::Builder::Config - Configurations of Compile and Link of Native Sources

Usage

  use SPVM::Builder::Config;
  
  # Create a config
  my $config = SPVM::Builder::Config->new(file => __FILE__);
  
  # Create a SPVM::Builder::Config object with "GNU99"
  my $config = SPVM::Builder::Config->new_gnu99(file => __FILE__);

  # Create a SPVM::Builder::Config object with "C99"
  my $config = SPVM::Builder::Config->new_c99(file => __FILE__);

  # Create a SPVM::Builder::Config object as "C++"
  my $config = SPVM::Builder::Config->new_cpp(file => __FILE__);

  # Create a SPVM::Builder::Config object with "C++11" standard of "C++"
  my $config = SPVM::Builder::Config->new_cpp11(file => __FILE__);
  
  # Optimize
  $config->optimize('-O2');
  
  # Optimize with debug mode
  $config->optimize('-O0 -g');
  
  # Add source files
  $config->add_source_files('foo.c', 'bar.c', 'baz/baz.c');
  
  # Use resource
  $config->use_resource('TestCase::Resource::Zlib::V1_0_0');
  $config->use_resource('TestCase::Resource::Foo1::V1_0_0', mode => 'mode1', args => ['args1', 'args2']);
  
  # Get resouce information
  my $resource = $config->get_resource('TestCase::Resource::Zlib::V1_0_0');

Description

SPVM::Builder::Config is configuration of c/c++ compile and link.

Field Methods

Fields.

ext

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

Get and set the extension of the SPVM native source.

The default is undef.

Examples:

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

cc

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

Get and set a compiler name. The default is the value of cc of Config module.

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});

cc_each

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

Get and set a callback that returns the compiler name for each source file. The call back receives SPVM::Bulder::Config object and optional arguments as a hash reference.

Optional Arguments:

  • source_file

    Each source file.

  • class_name

    The class name

If cc_each is defined, the compiler use the return value of cc_each as the compiler name instead of cc.

Examples:

  $config->cc_each(sub {
    my ($config, $args) = @_;
    
    # Source file
    my $source_file = $args->{source_file};
    
    # Class name
    my $class_name = $args->{class_name}
    
    my $cc;
    # C source file
    if ($source_file =~ /\.c$/) {
      $cc = 'gcc';
    }
    # C++ source file
    elsif ($source_file =~ /\.cpp$/) {
      $cc = 'g++';
    }
    
    return $cc;
  });

include_dirs

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

Get and set header including directories of the compiler. This is same as -I option of gcc.

builder_include_dir

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

Get and set the header including directory of SPVM::Builder.

The default value is SPVM/Builder/include of one up of directory that SPVM::Buidler::Config is loaded.

builder_src_dir

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

Get and set the source directory of SPVM::Builder.

The default value is SPVM/Builder/src of one up of the directory that SPVM::Buidler::Config is loaded.

own_include_dir

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

Get and set the header including directory of this module.

The default value is the name that removing [.mode].config from the file and add .native/include.

own_src_dir

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

Get and set the source directory of this module.

The default value is the name that removing [.mode].config from the file and add .native/src.

ccflags

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

Get and set compiler flags.

Default:

  # $Config{cccdlflags} has -fPIC.
  ['-fPIC']
  
  # Other
  []

ccflags_each

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

Get and set a callback that returns the compiler flags for each source file. The call back receives SPVM::Bulder::Config object and optional arguments as a hash reference.

Optional Arguments:

  • source_file

    Each source file.

  • class_name

    The class name

  • cc

    The compiler name that is the value after the process of the process of cc or cc_each.

If ccflags_each is defined, the compiler use the return value of ccflags_each as the compiler flags instead of ccflags.

Examples:

  $config->ccflags_each(sub {
    my ($config, $source_file) = @_;

    # Source file
    my $source_file = $args->{source_file};
    
    # Class name
    my $class_name = $args->{class_name}

    # The compiler name
    my $cc = $args->{cc};
    
    # ccflags
    my $config_ccflags = $config->ccflags;
    
    my $ccflags = [];
    # C source file
    if ($source_file =~ /\.c$/) {
      $ccflags = ['-DFoo', @$config_ccflags];
    }
    # C++ source file
    elsif ($source_file =~ /\.cpp$/) {
      $ccflags = ['-DBar', @$config_ccflags];
    }
    
    return $ccflags;
  });

optimize

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

Get and set the option for optimization of the compiler.

The default is -O3.

Examples:

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

optimize_each

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

Get and set a callback that returns the value of optimize for each source file. The callback receives SPVM::Bulder::Config object and optional arguments as a hash reference.

Optional Arguments:

  • source_file

    Each source file.

  • class_name

    The class name

  • cc

    The compiler name that is the value after the process of the process of cc or cc_each.

If optimize_each is defined, the compiler use the return value of optimize_each as the compiler flags instead of optimize.

Examples:

  $config->optimize_each(sub {
    my ($config, $source_file) = @_;

    # Source file
    my $source_file = $args->{source_file};
    
    # Class name
    my $class_name = $args->{class_name}

    # The compiler name
    my $cc = $args->{cc};
    
    # optimize
    my $config_optimize = $config->optimize;
    
    my $optimize = [];
    # C source file
    if ($source_file =~ /\.c$/) {
      $optimize = '-O3';
    }
    # C++ source file
    elsif ($source_file =~ /\.cpp$/) {
      $optimize = '-O3';
    }
    
    return $optimize;
  });

source_files

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

Get and get source files. The file name is the relative pass from "own_src_dir".

Examples:

  $config->source_files(['foo.c', 'bar.c']);

ld

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

Get and set a linker. Default is ld of Config module.

lib_dirs

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

Get and set the directories that libraries are searched for by the linker. This is same as -L option of gcc.

Default:

Windows

  The directory that perlxxx.dll exists
  

Not Windows

  empty list

libs

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

Get and set library names or SPVM::Builder::LibInfo objects. These libraries are linked by "link" in SPVM::Builder::CC method.

ldflags

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

Get and set linker flags. The default value is an emtpy array reference.

dynamic_lib_ldflags

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

Get and set linker flags for dynamic link.

Default:

Windows

  ['-mdll', '-s']
  

Non-Windows

  ['-shared']

ld_optimize

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

Get and set the option for optimization of the linker such as -O3, -O2, -g3 -O0.

The default is -O2.

force

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

Get and set the flag to force compiles and links without caching. The default is undef.

undef means forcing is not determined by config.

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

Get and set the callback that is executed before the link. The callback receives SPVM::Builder::Config object and the SPVM::Builder::LinkInfo object used by the linker.

Examples:

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

quiet

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

Get and set the flag if the compiler and the linker output the results.

The default is undef. undef means quietness is not determined by config.

file

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

Get and set the config file path.

The default is 1.

file_optional

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

Get and set the value that indicates file field is needed for new|/"new" method.

The default is 0.

output_type

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

Class Methods

new

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

Create a SPVM::Builder::Config object.

"file" must be specified except for the case that "file_optional" is set to a true value.

new_c

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

Call "new". After that, call ext('c').

new_gnu99

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

Call "new_c". After that, call set_std('gnu99').

new_gnu11

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

Call "new_c". After that, call set_std('gnu11').

new_gnu99

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

Call "new_c". After that, call set_std('gnu99').

new_gnu11

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

Call "new_c". After that, call set_std('gnu11').

new_c99

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

Call "new_c". After that, call set_std('c99').

new_c11

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

Call "new_c". After that, call set_std('c11').

new_cpp

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

Call "new". After that, call ext('cpp') and set "cc" to C++ compiler, and set "ld" to C++ linker.

new_cpp11

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

Call "new_cpp". After that, call set_std('c++11').

new_cpp14

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

Call "new_cpp". After that, call set_std('c++14').

new_cpp17

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

Call "new_cpp". After that, call set_std('c++17').

Instance Methods

set_std

  $config->set_std($std);

Add the value that is converted to -std=$std after the last element of ccflags field.

Example:

  $config->set_std('gnu99');

add_ccflags

  $config->add_ccflags(@ccflags);

Add values after the last element of ccflags field.

add_ldflags

  $config->add_ldflags(@ldflags);

Add values after the last element of ldflags field.

add_include_dirs

  $config->add_include_dirs(@include_dirs);

Add values after the last element of include_dirs field.

add_lib_dirs

  $config->add_lib_dirs(@lib_dirs);

Add values after the last element of lib_dirs field.

add_source_files

  $config->add_source_files(@source_files);

Add the values after the last element of source_files field.

add_libs

  $config->add_libs(@libs);

Add library names or SPVM::Builder::LibInfo objects after the last element of "libs" field.

Examples:

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

add_static_libs

  $config->add_static_libs(@libs);

Add library names or SPVM::Builder::LibInfo objects after the last element of "libs" field.

static field is set to a true value.

Examples:

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

use_resource

  $config->use_resource($resource);
  $config->use_resource('Resource::Zlib::V1_0_0');
  $config->use_resource('Resource::Zlib::V1_0_0', mode => 'prod', args => ['foo', 'bar']);

Use a resource.

The first argument is a SPVM::Builder::Resource object.

If the first argument is a class name of the resource, a SPVM::Builder::Resource object is created by SPVM::Builder::Resource method with class_name option.

  my $resource = SPVM::Builder::Resource->new(class_name => 'Resource::Zlib::V1_0_0');
  $config->use_resource($resource);

If the rest arguments are used as the options of SPVM::Builder::Resource of SPVM::Builder::Resource.

  my $resource = SPVM::Builder::Resource->new(
    class_name => 'Resource::Zlib::V1_0_0',
    mode => 'prod',
    args => ['foo', 'bar'],
  );
  $config->use_resource($resource);

get_resource

  my $resource = $config->get_resource('Resource::Zlib::V1_0_0');

Get a resource. The resource is a SPVM::Builder::Resource object.

get_resource_names

  my $resource_names = $config->get_resource_names;

Get resource names.