2.4 Libraries loading and embedding

We can include in scripts the code needed to source a library at run–time or we can embed a whole library itself. Searching and sourcing libraries at run–time for details on how to use the linker library to load libraries.

Preprocessor Macro: mbfl_embed_library
Preprocessor Macro: mbfl_embed_library (LIBRARY_FILE_PATHNAME)

Expands into the contents of a library file; the resulting output script does not need to load the library at runtime, let’s say it is “statically linked”.

  • If the optional argument LIBRARY_FILE_PATHNAME is not used:
    • If the option --libmbfl is not used on the command line of mbflpp.bash: the library libmbfl-core.bash installed on the system is embedded.
    • If the option --libmbfl is used on the command line of mbflpp.bash: the file pathname selected by the option is used to locate the library libmbfl-core.bash to be embedded.
  • If the optional argument LIBRARY_FILE_PATHNAME is used: that argument is interpreted as file system pathname of a library to embed in the source file. Does not matter which library, either a libmbfl-core.bash implementation or some other library.

The macro expands into a use of the m4 built–in m4_undivert():

m4_undivert(LIBRARY_FILE_PATHNAME)

so the argument LIBRARY_FILE_PATHNAME must be a m4 expression expanding into a file system pathname. See Including named files, for details on including files. See Undiverting output, for details on undiverting named files.

As example, we could preprocess a script with the following command line:

mbflpp.bash --define=__LIBFILE_PTN__=/path/to/libmy.bash -oscript.bash script.bash.m4

and in the script’s source file we write:

mbfl_embed_library(__LIBFILE_PTN__)
Preprocessor Macro: mbfl_library_loader
Preprocessor Macro: mbfl_library_loader (LIBRARY_FILE_PATHNAME)
Preprocessor Macro: mbfl_load_library
Preprocessor Macro: mbfl_load_library (LIBRARY_FILE_PATHNAME)

Expand to a block of code that sources a library file; the resulting output script does need to source the library at runtime, let’s say it is “dynamically linked”.

  • If the optional argument LIBRARY_FILE_PATHNAME is not used:
    • If the option --libmbfl is not used on the command line of mbflpp.bash: the macro use expands into shell code that sources the library libmbfl-core.bash installed on the system.
    • If the option --libmbfl is used on the command line of mbflpp.bash: the macro use expands into shell code that sources the file pathname selected by the option; it is meant to be an implementation of libmbfl-core.bash.
  • If the optional argument LIBRARY_FILE_PATHNAME is used: that argument is interpreted as file system pathname of a library to source in the code; does not matter which library, either a libmbfl-core.bash implementation or some other library. The macro use expands into shell code that sources the library.

The macro use expands into:

source LIBRARY_FILE_PATHNAME || exit 100

so, in the general case, the argument LIBRARY_FILE_PATHNAME must be a M4 expression expanding into a Bash expression evaluating into a file system pathname.

As example, we can load a library selecting it from an environment variable as follows:

mbfl_load_library("${MY_LIBPTN:?'variable not set'}")

or we can select only the library’s installation directory with an environment variable:

mbfl_load_library("${MY_LIBPATH:?'variable not set'}/libmy.bash")

As other example, we could preprocess a script with the following command line:

mbflpp.bash --define=__LIBFILE_PTN__=/path/to/libmy.bash -oscript.bash script.bash.m4

and in the script’s source file we write:

mbfl_load_library('__LIBFILE_PTN__')

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.