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->build_rule({language => 'c'}, {optimize => '-O3'});
# Regex matching and field appending (+)
# If dialect matches c11/c17, add a specific warning flag
$config_global->build_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->build_rule({ccflags => '-DDEBUG'}, {'+ldflags' => ['-DEBUG']});
# Negative Match (!prefix)
# If '-Zi' is NOT in ccflags, enable linker optimizations
$config_global->build_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->build_rule({language => 'cpp', '!dialect' => 'c++11'}, {
'+ccflags' => ['-EHsc']
});
# Procedural update with a callback
# Dynamically modify the config object based on custom logic
$config_global->build_rule({language => 'c'}, sub {
my $config = shift;
if ($ENV{MY_CUSTOM_OPT}) {
$config->optimize('-Ofast');
}
});
# Apply to all configurations (build_rule_any)
# Ensure all configs have the -nologo flag regardless of any condition
$config_global->build_rule_any({'+ccflags' => ['-nologo']});
# Reset or global initialization via callback
# Useful for clearing fields before applying specific rules
$config_global->build_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.
Fields
before_compile_cbs
my $before_compile_cbs = $self->before_compile_cbs;
$self->before_compile_cbs($before_compile_cbs);
Gets and sets the before_compile_cbs field, an array reference of callbacks that work globally.
These callbacks are executed just after an SPVM::Builder::CompileInfo object is completely constructed and just before the compile command "cc" is planned to be executed.
This affects all compilations.
Note that even if the actual compile command is skipped (for example, when the object file is already up-to-date), these callbacks are still executed as long as the compilation is planned in the build pipeline.
The 1st argument of each callback is an SPVM::Builder::CompileInfo object.
You can modify each field of the SPVM::Builder::CompileInfo object and its internal SPVM::Builder::Config object (obtained via the config method) within the callbacks to dynamically customize the compilation process.
before_link_cbs
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 that work globally.
These callbacks are executed just after an SPVM::Builder::LinkInfo object is completely constructed and just before the link command "ld" is planned to be executed.
This affects all linkings.
Note that even if the actual link command is skipped (for example, when the dynamic link file is already up-to-date), these callbacks are still executed as long as the linking is planned in the build pipeline.
The 1st argument of each callback is an SPVM::Builder::LinkInfo object.
You can modify each field of the SPVM::Builder::LinkInfo object and its internal SPVM::Builder::Config object (obtained via the config method) within the callbacks to dynamically customize the linking process.
after_link_cbs
my $after_link_cbs = $config->after_link_cbs;
$config->after_link_cbs($after_link_cbs);
Gets and sets the after_link_cbs field, an array reference of callbacks that work globally.
These callbacks are executed just after the link command "ld" is executed or planned to be executed.
This affects all linkings.
Note that even if the actual link command is skipped (for example, when the dynamic link file is already up-to-date), these callbacks are still executed as long as the linking is planned in the build pipeline.
The 1st argument of each callback is an SPVM::Builder::LinkInfo object.
You can check each field of the SPVM::Builder::LinkInfo object and its internal SPVM::Builder::Config object (obtained via the config method) within the callbacks to perform post-linking processes.
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:
DebugReleaseRelWithDebInfoMinSizeRel
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:
-
"exe" -
[] -
[] -
[] Other Fields
undef
add_before_compile_cb
$self->add_before_compile_cb(@before_compile_cbs);
Adds @before_compile_cbs to the end of "before_compile_cbs" field.
Examples:
$self->add_before_compile_cb(sub {
my ($compile_info) = @_;
my $config = $compile_info->config;
my $cc_command = $compile_info->to_command;
# Do something
});
add_before_link_cb
$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 ($link_info) = @_;
my $config = $link_info->config;
my $object_file_infos = $link_info->object_file_infos;
# Do something
});
add_after_link_cb
$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 ($link_info) = @_;
my $config = $link_info->config;
my $object_file_infos = $link_info->object_file_infos;
# Do something
});
build_rule
$global_config->build_rule($condition, $match_config_or_cb);
Adds a rule to dynamically update the configuration before compilation if the given conditions are met.
Parameters:
$conditionA 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
Regexpobject (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 alsoundef.String Match: Otherwise, it performs a string equality check (
eq) against the field value (or its elements).
$match_config_or_cbA 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.
build_rule_any
$global_config->build_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