Elektra
0.8.15
|
To create different variants of the same feature, but avoid code duplications within plugins, you have multiple options:
COMPILE_DEFINITIONS
(that are Macro definitions). This should be preferred when different macro definitions lead to different plugins. It should especially be used when the resulting plugins have different dependencies: it is possible to have different LINK_LIBRARIES
.The advantage of compilation variants are:
To use compilation variants, add your plugin in the CMake Cache Variable PLUGINS multiple times. There has to be the base variant, called in the same name as the directory. Then there can be an arbitrary number of variants that additional have a name appended with underscore, e.g.:
myplugin;myplugin_varianta;myplugin_variantb
In the CMakeLists.txt of your plugin, you need a loop over all PLUGINS and create a plugin per compilation variant:
foreach (plugin ${PLUGINS}) if (${plugin} MATCHES "myplugin_.*") # somehow process the variant names and include # or change sources and compile definitions # based on that. add_plugin(${plugin} SOURCES <your sources here..> COMPILE_DEFINITIONS <definitions here..> ELEKTRA_VARIANT=${plugin without prefix} LINK_LIBRARIES <libraries for variantb>
or simply list all plugins one after the other:
if (${plugin} MATCHES "myplugin_varianta") # somehow process the variant names and include # or change sources and compile definitions # based on that. add_plugin(myplugin_varianta SOURCES <your sources for varianta here..> COMPILE_DEFINITIONS VARIANTA ELEKTRA_VARIANT=varianta LINK_LIBRARIES <libraries for varianta> ) if (${plugin} MATCHES "myplugin_variantb") # somehow process the variant names and include # or change sources and compile definitions # based on that. add_plugin(myplugin_variantb SOURCES <your sources for variantb here..> COMPILE_DEFINITIONS VARIANTB ELEKTRA_VARIANT=variantb LINK_LIBRARIES <libraries for variantb>
Note that every plugin (except the base plugin, if available) needs to have ELEKTRA_VARIANT
differently set in COMPILE_DEFINITIONS
, otherwise you will get a linker error that libelektra_<pluginname>_LTX_elektraPluginSymbol
has multiple definitions.
Now every public function of the plugin conflicts with itself. To avoid that, you can use:
ELEKTRA_PLUGIN_FUNCTION(myplugin, open)
where myplugin is the name of the plugin and the second argument is how the function should be called.#include ELEKTRA_README(myplugin)
As a summary, you can have many plugins build out of the same source. Using pluginname_variantnames many plugins will be compiled, each with other SOURCES or COMPILE_DEFINITIONS
and even LINK_LIBRARIES
: If you, e.g. just set the variants name as macro you can use
#ifdef varianta #endif
within the code and can have two plugins: one (called myplugin_varianta) compiled included the #ifdef
the other (base variant called myplugin) without.
Currently compilation variants is used in the resolver plugin.