Name

SPVM::Builder::Config::Global - Excutable File Config

Description

The SPVM::Builder::Config::Global class has methods to manipulate the config for the excutable file generated by spvmcc command.

Usage

use SPVM::Builder::Config::Global;

my $config_global = SPVM::Builder::Config::Global->new;

Usage

use SPVM::Builder::Config::Global;

my $config_global = SPVM::Builder::Config::Global->new;

# Basic conditional update (Exact match)
# If language is 'c', set optimization to -O3
$config_global->compile_rule({language => 'c'}, {optimize => '-O3'});

# Regex matching and field appending (+)
# If dialect matches c11/c17, add a specific warning flag
$config_global->compile_rule({dialect => qr/^c1[17]$/}, {'+ccflags' => ['-Wpedantic']});

# Array Sensitivity (Checking if a flag exists in an array)
# If '-DDEBUG' is already in ccflags, add debug linker flags
$config_global->compile_rule({ccflags => '-DDEBUG'}, {'+ldflags' => ['-DEBUG']});

# Negative Match (!prefix)
# If '-Zi' is NOT in ccflags, enable linker optimizations
$config_global->compile_rule({'!ccflags' => '-Zi'}, {'+ld_optimize' => '-OPT:REF,ICF'});

# Complex conditions (AND logic)
# If it's C++ AND dialect is NOT c++11 (e.g. c++14), add exception handling
$config_global->compile_rule({language => 'cpp', '!dialect' => 'c++11'}, {
  '+ccflags' => ['-EHsc']
});

# Procedural update with a callback
# Dynamically modify the config object based on custom logic
$config_global->compile_rule({language => 'c'}, sub {
  my $config = shift;
  if ($ENV{MY_CUSTOM_OPT}) {
    $config->optimize('-Ofast');
  }
});

# Apply to all configurations (compile_rule_any)
# Ensure all configs have the -nologo flag regardless of any condition
$config_global->compile_rule_any({'+ccflags' => ['-nologo']});

# Reset or global initialization via callback
# Useful for clearing fields before applying specific rules
$config_global->compile_rule_any(sub {
  my $config = shift;
  $config->clear_system_fields;
});

Details

Warning: Should Not Change Compiler Flags

The fields for compiler flags in SPVM::Builder::Config such as "cc" in SPVM::Builder::Config, "std" in SPVM::Builder::Config should not be changed.

This is because the compiler flags are used to compile SPVM core source files and a bootstrap source file generagted by spvmcc command.

Super Class

Fields

before_compile_cbs

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

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

This affects all compilations.

optimize

$config->optimize($optimize, $condition);

Sets optimize field for the configs that match the condition $condition.

This method is a setter-only method. It calls "compile_rule" internally.

If $condition is not defined, the optimization setting is applied to all configs.

Examples:

# Set -O3 for all configs
$config->optimize('-O3');

# Set -O2 for native category configs
$config->optimize('-O2', {category => 'native'});

build_type

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

Sets and gets the build_type field.

The build_type field must be one of the following CMake-compatible values:

  • Debug

  • Release

  • RelWithDebInfo

  • MinSizeRel

If an invalid value is specified, an exception is thrown.

Examples:

# Set build_type
$config->build_type('Debug');

# Get build_type
my $build_type = $config->build_type;

Methods

new

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

Create a new SPVM::Builder::Config::Global object with "Fields" and fields of its super classes.

This method calls the new method of its super class given %fields with field default values applied.

Field Default Values:

add_before_compile_cb

$config_global->add_before_compile_cb(@before_compile_cbs);

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

Examples:

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

compile_rule

$global_config->compile_rule($condition, $match_config_or_cb);

Adds a rule to dynamically update the configuration before compilation if the given conditions are met.

Parameters:

  • $condition

    A hash reference specifying the match criteria. Each key represents a field name in the target configuration.

    A match occurs only if all specified conditions in the hash are satisfied (AND logic). For each individual field, the matching behavior is as follows:

    • Negative Match (!prefix): If a field name starts with ! (e.g., '!ccflags' = '-O2'>), the condition is inverted. It matches only if the criteria is not met.

    • Array Sensitivity: If the field value in the target configuration is an array reference, the condition matches if at least one element within that array satisfies the criteria below (OR logic within the array). When combined with the ! prefix, it matches only if none of the elements satisfy the criteria.

    • Regex Match: If the condition value is a Regexp object (e.g., qr/.../), it performs a regex match against the field value (or its elements).

    • Undef Match: If the condition value is undef, it matches if the target field (or an element within its array) is also undef.

    • String Match: Otherwise, it performs a string equality check (eq) against the field value (or its elements).

  • $match_config_or_cb

    A hash reference, an SPVM::Builder::Config object, or a code reference (callback).

    If it is a hash reference or a config object, the fields in the target configuration are updated. You can use the + prefix in the field name to append values instead of overwriting them:

    • +field = $string> : If the existing value is a string, it concatenates the new string. If the existing value is an array reference, it pushes the new string into the array.

    • +field = $array_ref> : Pushes the elements of the array reference into the existing array. If the existing value is a scalar, it is promoted to an array before the push.

    If it is a code reference, the callback is executed with the target SPVM::Builder::Config object as its first argument. This allows for complex, procedural updates to the configuration.

compile_rule_any

$global_config->compile_rule_any($match_config_or_cb);

A syntax sugar for "match" with no conditions. The $match_config will be applied to all configurations before compilation.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License