Elektra
0.8.21
|
To create different variants of the same feature, but avoid code duplications within plugins, you have multiple options:
add_lib
for creating such a library (in the folder libs). This should be used for rather common functionality that might be useful for many plugins or even applications.if
according to the configuration. This should be preferred when you want to (de)activate some features of a plugin at run-time.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:
#refnames
To use compilation variants, add your plugin in the CMake Cache Variable PLUGINS
multiple times. Then there can be an arbitrary number of variants. As naming convention you should have a base name with an additional variant appended with underscore, e.g.:
In the CMakeLists.txt of your plugin, you have two options. Option (A): When you can easily enlist every variant you simply list all plugins one after the other (outside of if (DEPENDENCY_PHASE)
):
Option (B): If you cannot enlist every possible compilation variant, you can iterate over all PLUGINS and check which names are requested. Then you create a plugin for every name that matches:
For the categories such as ALL
, however, you need to automatically append (using add_plugin
) a useful set of plugins.
Note that every plugin 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:
add_lib
to share code between compilation variants (only if code is also potentially useful for other plugins/applications)ELEKTRA_PLUGIN_FUNCTION(myplugin, open)
where myplugin is the name of the plugin and the second argument is how the function should be called.#ifdef
for different text) using the macro #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
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 are used in the resolver plugin.