Skip to Content.
Sympa Menu

sm-commit - [SM-Commit] GIT changes to master grimoire by Justin Boffemmyer (c4f75aa7bff914120e810329f460b1fe87a69a7b)

sm-commit AT lists.ibiblio.org

Subject: Source Mage code commit list

List archive

Chronological Thread  
  • From: Justin Boffemmyer <scm AT sourcemage.org>
  • To: sm-commit AT lists.ibiblio.org
  • Subject: [SM-Commit] GIT changes to master grimoire by Justin Boffemmyer (c4f75aa7bff914120e810329f460b1fe87a69a7b)
  • Date: Mon, 20 Nov 2023 13:27:32 +0000

GIT changes to master grimoire by Justin Boffemmyer <flux AT sourcemage.org>:

build_system_handler.function | 315
++++++++++++++++++++++++++++++++++++++++++
1 file changed, 315 insertions(+)

New commits:
commit c4f75aa7bff914120e810329f460b1fe87a69a7b
Author: Justin Boffemmyer <flux AT sourcemage.org>
Commit: Justin Boffemmyer <flux AT sourcemage.org>

build_system_handler.function: new handler

This grimoire function file provides wrappers for the standard "depends"
and "optional_depends" functions that both abstracts across differing
build systems as well as providing the ability to use defaults for any
known and defined build systems. Currently defaults for
autotools/configure (--enable-OPTION_NAME and --disable-OPTION_NAME),
cmake (-DOPTION_NAME=ON and -DOPTION_NAME=OFF), and meson
(OPTION_NAME=enabled and OPTION_NAME=disabled) build systems are
included.

These wrappers are most useful for spells that allow for multiple
differing build systems. Typically this happens because upstream is
transitioning from one build system to another, but some spells may
simply offer support for different build systems as a matter of choice.
However, the functions provided here are also perfectly usable even when
only a single build system is available use with a spell.

The active build system (the one actually being used to build the spell)
is expected to be defined in the environment variable
SPELL_BUILD_SYSTEM. For spells that only have one build system but opt
to use the functions defined here, this variable can be defined in
DETAILS. For spells that offer an option of multiple build systems, this
variable would typically be defined via a form of config_query in
CONFIGURE. The value of SPELL_BUILD_SYSTEM (even if undefined) can be
overridden by use of the -a flag (see below).

The syntax to define the actual dependencies is as follows:

buildsys_depends[_optional] \
[-a active_build_system] \
[-g grimoire] \
[-o global_optname] \
spell_name \
...
[-b] build_systemN \
[-o local_optname] \
message \
[enabled] \
[disabled]
[-b] build_systemN+1 \
[-o local_optname] \
message \
[enabled] \
[disabled] \
...

Below are examples of how the functions are intended to be used.

DETAILS file:
<code>
...
SPELL_BUILD_SYSTEM=meson
...
</code>

DEPENDS file:
<code>
. /code/smgl/grimoire/build_system_handler.function

buildsys_depends spell1 \
-b meson \
-o "mesonopt" \
message \
-b configure \
message \
--with-foo \
&&

buildsys_optional_depends -o "foo" spell2 \
meson\
message \
"" \
"" \
&&
configure \
message \
&&
...
</code>

The use of the -b flag to indicate the next build system set is optional
if the build system "blocks" can be determined unambiguously. For
example, if surrounding build system blocks are specified with -b, the
one in middle need not be, or if all arguments are explicitly given, or
if it is the last build system block in the set. Note that quoting
arguments to pass them explicitly without giving them a value is
acceptable. However, if the enabled/disabled arguments are elided then
the -b flag is necessary to prevent remaining parameters from being used
to fill the elided arguments of the current build system set.

The -o flag sets the option name. If no -o flag is present then the
spell name is used as the option name. If -o is passed before the spell
name it sets the global option name for that spell. Passing -o within a
build system set overrides the option name locally. The order of
precedence for the option name is:

local option name > global option name > spell name

The local option name is cleared on entering a new build system set
(i.e. using -b build_system forces a reset of the option name).

Enabled/disabled settings use build system defaults if known unless
explicitly given. For defaults to be known they must be coded into a
function named buildsys_NAME_defaults, where NAME is the build system
name returned by buildsys_mapping. For example, buildsys_cmake_default
or buildsys_autotools_default. Note that explicitly giving the arguments
as empty strings "" does not fully override this behavior. To override
this behavior while also passing an empty string for an enabled/disabled
argument, the local option name must also be explicitly passed as an
empty string.

diff --git a/build_system_handler.function b/build_system_handler.function
new file mode 100644
index 0000000..a489394
--- /dev/null
+++ b/build_system_handler.function
@@ -0,0 +1,315 @@
+#---------------------------------------------------------------------
+## Checks the current return code from the previous command and prints
+## an error message if that return code is an error code.
+##
+## @param error_message_to_print_if_error
+##
+## @return current_return_code (gives back the same return code the
+## function starts with)
+#---------------------------------------------------------------------
+_buildsys_chkerr() {
+ local rc="$?"
+ [ "$rc" -eq 0 ] && return 0
+ message "${PROBLEM_COLOR}$*${DEFAULT_COLOR}"
+ return "$rc"
+}
+
+#---------------------------------------------------------------------
+## Maps build system names and aliases to unique build system names.
+## Outputs the unique name if there is a match on the input.
+##
+## @param build_system (the build system name to map from)
+#---------------------------------------------------------------------
+buildsys_mapping() {
+ local buildsys="$1"
+ case "$buildsys" in
+ autotools|configure)
+ echo autotools
+ ;;
+ cmake)
+ echo cmake
+ ;;
+ meson)
+ echo meson
+ ;;
+ esac
+}
+
+#---------------------------------------------------------------------
+## Routes requests for default option values to the matching build
+## system function, if available.
+##
+## @param active_build_system (the currently active build system)
+## @param setting_value (required, must be either enabled or disabled)
+## @param setting_name (name of the option to set)
+##
+## @return 1 on invalid parameters
+## @return 2 on unknown build system (no matching defaults)
+#---------------------------------------------------------------------
+buildsys_defaults() {
+ local buildsys="$1"
+ [ -n "$buildsys" ]
+ _buildsys_chkerr "build system not defined" || return 1
+ shift 1
+ local func="$(buildsys_mapping "$buildsys")"
+ [ -n func ]
+ _buildsys_chkerr "no matching defaults function for $buildsys" || return 2
+ func="buildsys_${func}_default"
+ local func_check="$(command -v "$func")"
+ case "$func_check" in
+ "$func") ;;
+ *) unset func ;;
+ esac
+ [ -n "$func" ]
+ _buildsys_chkerr "$buildsys defaults function not defined: $func" ||
return 2
+ "$func" "$@"
+}
+
+#---------------------------------------------------------------------
+## Outputs the default enabled or default disabled, depending on the
+## first parameter, for the given setting/option name.
+##
+## @param setting_value (required, must be either enabled or disabled)
+## @param setting_name (name of the option to set)
+##
+## @return 1 on invalid parameters
+#---------------------------------------------------------------------
+buildsys_autotools_default() {
+ [ "$#" -eq 2 ]
+ _buildsys_chkerr "invalid parameters" || return 1
+ local value="$1"
+ local name="$2"
+ printf '--%s-%s\n' "${value%d}" "$name"
+}
+
+#---------------------------------------------------------------------
+## Outputs the default enabled or default disabled, depending on the
+## first parameter, for the given setting/option name.
+##
+## @param setting_value (required, must be either enabled or disabled)
+## @param setting_name (name of the option to set)
+##
+## @return 1 on invalid parameters
+#---------------------------------------------------------------------
+buildsys_cmake_default() {
+ [ "$#" -eq 2 ]
+ _buildsys_chkerr "invalid parameters" || return 1
+ local value="$1"
+ local name="$2"
+ case "$value" in
+ enabled) value=ON ;;
+ disabled) value=OFF ;;
+ esac
+ printf '-D%s=%s\n' "$name" "$value"
+}
+
+#---------------------------------------------------------------------
+## Outputs the default enabled or default disabled, depending on the
+## first parameter, for the given setting/option name.
+##
+## @param setting_value (required, must be either enabled or disabled)
+## @param setting_name (name of the option to set)
+##
+## @return 1 on invalid parameters
+#---------------------------------------------------------------------
+buildsys_meson_default() {
+ [ "$#" -eq 2 ]
+ _buildsys_chkerr "invalid parameters" || return 1
+ local value="$1"
+ local name="$2"
+ printf '%s=%s\n' "$name" "$value"
+}
+
+#---------------------------------------------------------------------
+## A wrapper over the usual depends and optional_depends functions.
+## Translates dependencies in a DEPENDS file into the formats required
+## for processing by the relevant build system(s) currently enabled.
+##
+## @param dependency_type (must be one of depends or optional_depends)
+## @param -a active_build_system (optional, flag to specify the
+## currently active build system, overrides the build
+## system set in $SPELL_BUILD_SYSTEM)
+## @param -g grimoie (optional, flag to specify the grimoire to use)
+## @param -o global_optname (optional, flag to give the default option
+## name to use across all build systems)
+## @param spell_name (the name of the actual dependency as a spell)
+## @param [-b] build_system (the build system to define options for,
+## e.g. cmake, meson, autotools, etc.)
+## @param -o local_optname (optional, flag to override the
+## global_optname)
+## @param message (message to the user explaining this option)
+## @param enabled (the option or setting for when the dependency is
+## enabled)
+## @param disabled (the option or setting for when the dependency is
+## disabled)
+##
+## The syntax for the parameters is as follows:
+##
+## buildsys_depends[_optional] \
+## [-a active_build_system] \
+## [-g grimoire] \
+## [-o global_optname] \
+## spell_name \
+## ...
+## [-b] build_systemN \
+## [-o local_optname] \
+## message \
+## [enabled] \
+## [disabled]
+## [-b] build_systemN+1 \
+## [-o local_optname] \
+## message \
+## [enabled] \
+## [disabled] \
+## ...
+##
+## @return 1 on parameter error
+## @return 2 if the active build system does not have a matching build
+## system block defined
+## @return 3 when defaults are needed but not defined for the currently
+## active build system
+## @return return code of the wrapped depends or optional_depends
+## function otherwise
+#---------------------------------------------------------------------
+_buildsys_transform_depends() {
+ [ "$#" -gt 0 ]
+ _buildsys_chkerr "invalid parameters" || return 1
+ local buildsys
+ local disabled
+ local enabled
+ local global_optname
+ local grimoire
+ local match
+ local next
+ local optname
+ local spell
+ local targetsys
+ depends_func="$1" &&
+ shift 1 &&
+ case "$1" in
+ -a) targetsys="$2" && shift 2 ;;
+ esac &&
+ : "${targetsys:=$(buildsys_mapping "$SPELL_BUILD_SYSTEM")}" &&
+ [ -n "$targetsys" ]
+ _buildsys_chkerr "SPELL_BUILD_SYSTEM not defined" || return 1
+ while [ "$#" -gt 0 ] ;do
+ case "$1" in
+ -g) grimoire="$2" && shift 2 ;;
+ -o) global_optname="$2" && shift 2 ;;
+ *) break ;;
+ esac
+ done &&
+ spell="$1" &&
+ shift 1 &&
+ while [ "$#" -gt 0 ] ;do
+ case "$1" in
+ -b) buildsys="$2" && shift 2 ;;
+ *) buildsys="$1" && shift 1 ;;
+ esac &&
+ match=false &&
+ case "$(buildsys_mapping "$buildsys")" in
+ "$targetsys") match=true ;;
+ esac &&
+ unset optname &&
+ for arg in msg enabled disabled ;do
+ [ "$#" -eq 0 ] && break
+ if ! "$match" ;then
+ case "$1" in
+ -b) break ;;
+ esac
+ fi &&
+ case "$1" in
+ -b)
+ if [ -n "${msg+1}" ] ;then
+ break
+ else
+ _buildsys_chkerr "invalid parameters: empty build system block"
+ return 1
+ fi
+ ;;
+ -o)
+ optname="$2" &&
+ shift 2
+ ;;
+ esac &&
+ case "$arg" in
+ msg) msg="$1" ;;
+ enabled) enabled="$1" ;;
+ disabled) disabled="$1" ;;
+ esac &&
+ shift 1
+ done &&
+ if ! "$match" ;then
+ continue
+ fi &&
+ : "${optname=$global_optname}" &&
+ : "${optname=$spell}" &&
+ if [ -n "$optname" ] ;then
+ : "${enabled:=$(buildsys_defaults "$buildsys" enabled "$optname")}"
+ [ -n "$enabled" ]
+ _buildsys_chkerr "$enabled" || return 3
+ : "${disabled:=$(buildsys_defaults "$buildsys" disabled "$optname")}"
+ [ -n "$disabled" ]
+ _buildsys_chkerr "$disabled" || return 3
+ fi &&
+ "$match" && break
+ done &&
+ "$match"
+ _buildsys_chkerr "no matching build system found" || return 2
+ case "$depends_func" in
+ depends) unset disabled ;;
+ esac &&
+ "$depends_func" "$spell" "$enabled" ${disabled:+"$disabled"} "$msg"
"$grimoire"
+}
+
+#---------------------------------------------------------------------
+## A wrapper over the _buildsys_transform_depends function.
+## Calls _buildsys_transform_depends with the dependency type set to
+## "depends". See _buildsys_optional_depends for further details of
+## the parameters.
+##
+## @param -g grimoie (optional, flag to specify the grimoire to use)
+## @param -o global_optname (optional, flag to give the default option
+## name to use across all build systems)
+## @param spell_name (the name of the actual dependency as a spell)
+## @param [-b] build_system (the build system to define options for,
+## e.g. cmake, meson, autotools, etc.)
+## @param -o local_optname (optional, flag to override the
+## global_optname)
+## @param message (message to the user explaining this option)
+## @param enabled (the option or setting for when the dependency is
+## enabled)
+## @param disabled (the option or setting for when the dependency is
+## disabled)
+##
+## @return the return value of _buildsys_transform_depends
+#---------------------------------------------------------------------
+buildsys_depends() {
+ _buildsys_transform_depends "depends" "$@"
+}
+
+#---------------------------------------------------------------------
+## A wrapper over the _buildsys_transform_depends function.
+## Calls _buildsys_transform_depends with the dependency type set to
+## "optional_depends". See _buildsys_optional_depends for details of
+## the parameters.
+##
+## @param -g grimoie (optional, flag to specify the grimoire to use)
+## @param -o global_optname (optional, flag to give the default option
+## name to use across all build systems)
+## @param spell_name (the name of the actual dependency as a spell)
+## @param [-b] build_system (the build system to define options for,
+## e.g. cmake, meson, autotools, etc.)
+## @param -o local_optname (optional, flag to override the
+## global_optname)
+## @param message (message to the user explaining this option)
+## @param enabled (the option or setting for when the dependency is
+## enabled)
+## @param disabled (the option or setting for when the dependency is
+## disabled)
+##
+## @return the return value of _buildsys_transform_depends
+#---------------------------------------------------------------------
+buildsys_optional_depends() {
+ _buildsys_transform_depends "optional_depends" "$@"
+}



  • [SM-Commit] GIT changes to master grimoire by Justin Boffemmyer (c4f75aa7bff914120e810329f460b1fe87a69a7b), Justin Boffemmyer, 11/20/2023

Archive powered by MHonArc 2.6.24.

Top of Page