ComIn 0.5.1
ICON Community Interface
Loading...
Searching...
No Matches
comin_setup.cpp
Go to the documentation of this file.
1
12
13#include "comin.h"
14
15#include "comin_logging.hpp"
16#include "comin_plugin_info.hpp"
17#include "comin_state.hpp"
18#include "comin_util.hpp"
19
20#include <algorithm>
21#include <dlfcn.h>
22#include <string>
23
29 char name[COMIN_MAX_LEN_PLUGIN_NAME + 1];
31 char plugin_library[COMIN_MAX_LEN_PLUGIN_LIBRARY + 1];
33 char primary_constructor[COMIN_MAX_LEN_PRIMARY_CONSTRUCTOR + 1];
35 char options[COMIN_MAX_LEN_OPTIONS + 1];
37 char comm[COMIN_MAX_LEN_COMM + 1];
41};
42
43extern "C" {
44 // Load plugin modules and call their primary constructors.
45 // Takes an array with size of plugin descriptions. Loads each plugin and
46 // calls its primary constructor in order. The communicator and options
47 // strings are copied so the plugin can request them later.
48 //
49 // Called by the host model through a Fortran interface
50 // (comin_setup::comin_plugin_primaryconstructor).
51 void
53 int num_plugins) {
54 using comin::into_string;
55 using comin::state;
56
57 comin_state_set_num_plugins(num_plugins);
58
59 for (int i = 0; i < num_plugins; ++i) {
60 auto name = into_string(plugin_list[i].name);
61 auto plugin_library = into_string(plugin_list[i].plugin_library);
62 auto constructor = into_string(plugin_list[i].primary_constructor);
63
64 void* dlhandle;
65 if (plugin_library.empty()) {
66 dlhandle = dlopen(nullptr, RTLD_NOW);
67 } else {
68 dlhandle = dlopen(plugin_library.c_str(), RTLD_NOW);
69 }
70
71 if (name.empty()) {
72 if (plugin_library.empty()) {
73 name = "<self>";
74 } else if (auto pos = plugin_library.rfind('/');
75 pos != std::string::npos) {
76 name = std::string(plugin_library, pos + 1);
77 } else {
78 name = plugin_library;
79 }
80 name += "(" + constructor + ")";
81 }
82
83 if (!dlhandle) {
84 comin_plugin_finish_f("comin_plugin_primaryconstructor",
85 "Cannot load plugin <%s>: %s", name.c_str(),
86 dlerror());
87 return;
88 }
89
90 auto primary_constructor =
91 reinterpret_cast<void (*)()>(dlsym(dlhandle, constructor.c_str()));
92
93 if (!primary_constructor) {
95 "comin_plugin_primaryconstructor",
96 "Cannot load plugin <%s> primary constructor '%s': %s",
97 name.c_str(), constructor.c_str(), dlerror());
98 return;
99 }
100
101 if (plugin_list[i].log_severity < COMIN_SEVERITY_DEBUG ||
102 plugin_list[i].log_severity > COMIN_SEVERITY_ERROR) {
103 comin_plugin_finish_f("comin_plugin_primaryconstructor",
104 "Plugin <%s> log severity %d out of range.",
105 name.c_str(), plugin_list[i].log_severity);
106 return;
107 }
108 auto severity =
109 static_cast<t_comin_log_severity>(plugin_list[i].log_severity);
110
111 t_comin_plugin_info info;
112 info.comm = into_string(plugin_list[i].comm);
113 info.errors_return = false;
114 info.id = i + 1;
115 info.log_severity = severity;
116 info.name = std::move(name);
117 info.options = into_string(plugin_list[i].options);
118
119 state.plugin_info.push_back(std::move(info));
120
122 primary_constructor();
123 }
124
125 comin::logging::message(0, " Complete primary constructors");
127 }
128
129 void comin_setup_get_version(unsigned int* major, unsigned int* minor,
130 unsigned int* patch) {
131 *major = COMIN_VERSION_MAJOR;
132 *minor = COMIN_VERSION_MINOR;
133 *patch = COMIN_VERSION_PATCH;
134 }
135}
C interface for the ICON Community Interface.
void comin_plugin_finish_f(const char *routine, const char *fmt,...)
char comm[COMIN_MAX_LEN_COMM+1]
Name of the plugin MPI communicator.
char name[COMIN_MAX_LEN_PLUGIN_NAME+1]
Friendly name of the plugin. Shown to users.
void comin_setup_get_version(unsigned int *major, unsigned int *minor, unsigned int *patch)
char options[COMIN_MAX_LEN_OPTIONS+1]
String passed to the plugin to specify options or config files.
char primary_constructor[COMIN_MAX_LEN_PRIMARY_CONSTRUCTOR+1]
Function symbol name of the plugin's primary constructor.
char plugin_library[COMIN_MAX_LEN_PLUGIN_LIBRARY+1]
File name of the plugin module.
void comin_plugin_primaryconstructor(const t_comin_plugin_description *plugin_list, int num_plugins)
void comin_state_set_primary_done()
void comin_current_set_plugin_id(int plugin_id)
void comin_state_set_num_plugins(int num_plugins)