58#define MOD_NAME "[lib] "
84static void push_basename_entry(
char ***binarynames,
const char *bnc,
size_t * templates) {
85 char * alt_v0 = strdup(bnc);
86 assert(alt_v0 != NULL);
87 char * bn_v0 = basename(alt_v0);
88 for (
size_t i = 0; i < *templates && bn_v0 != NULL; ++i) {
89 if (strcmp((*binarynames)[i], bn_v0) == 0)
93 *binarynames = (
char **)realloc(*binarynames, (*templates + 2)*
sizeof(**binarynames));
94 assert(*binarynames != NULL);
95 (*binarynames)[(*templates)++] = strdup(bn_v0);
96 (*binarynames)[*templates] = NULL;
101static int running_from_path(
char *
uv_argv[]) {
102 const char *env_path = getenv(
"PATH");
103 if (env_path == NULL) {
108 char *self_path = realpath(
"/proc/self/exe", NULL);
109 if (self_path == NULL) {
115 const char * binarynames_tpl[] = {
122 size_t templates =
sizeof(binarynames_tpl)/
sizeof(binarynames_tpl[0]);
123 char **binarynames = (
char **)calloc(templates + 1 + 3,
sizeof(*binarynames));
124 assert(binarynames != NULL);
125 for (
size_t i = 0; i < templates; ++i)
126 binarynames[i] = strdup(binarynames_tpl[i]);
128 push_basename_entry(&binarynames,
uv_argv[0], &templates);
129 if (strchr(
uv_argv[0],
'/') != NULL) {
130 char * alt_v0 = realpath(
uv_argv[0], NULL);
131 if (alt_v0 != NULL) {
132 push_basename_entry(&binarynames, alt_v0, &templates);
136 push_basename_entry(&binarynames, self_path, &templates);
138 char *pathdup = strdup(env_path);
139 char *tokencont = pathdup;
144 char *next_token = NULL;
147 const char *pathelem = strtok_r(tokencont,
":", &next_token);
149 if (pathelem == NULL)
152 for (
size_t i = 0; binarynames[i] != NULL && rval == 0; ++i) {
153 const size_t len = strlen(pathelem) + 1 + strlen(binarynames[i]) + 1;
154 char * candidate = (
char *)calloc(1,
len);
155 snprintf(candidate,
len,
"%s/%s", pathelem, binarynames[i]);
157 char * real_candidate = realpath(candidate, NULL);
158 if (real_candidate != NULL) {
159 rval = strcmp(real_candidate, self_path) == 0;
160 free(real_candidate);
166 for (
size_t i = 0; binarynames[i] != NULL; ++i)
167 free(binarynames[i]);
177#ifdef BUILD_LIBRARIES
182 if (!running_from_path(
uv_argv)) {
183 char *tmp = strdup(
uv_argv[0]);
184 char *dir = dirname(tmp);
185 snprintf(path,
sizeof(path),
"%s/../lib/ultragrid/%s", dir,
pattern);
188 snprintf(path,
sizeof(path), LIB_DIR
"/ultragrid/%s",
pattern);
191 glob(path, 0, NULL, &glob_buf);
193 for(
unsigned int i = 0; i < glob_buf.gl_pathc; ++i) {
194 void *handle = dlopen(glob_buf.gl_pathv[i], RTLD_NOW|RTLD_GLOBAL);
196 char *error = dlerror();
197 MSG(WARNING,
"Library %s opening warning: %s \n",
198 glob_buf.gl_pathv[i], error);
199 char *filename = basename(glob_buf.gl_pathv[i]);
200 if (filename && error) {
205 libs.push_back(handle);
230 bool operator() (
const unsigned char& c1,
const unsigned char& c2)
const {
231 return tolower (c1) < tolower (c2);
234 bool operator() (
const std::string & s1,
const std::string & s2)
const {
235 return std::lexicographical_compare
236 (s1.begin (), s1.end (),
237 s2.begin (), s2.end (),
246 static map<enum library_class, map<string, lib_info, ci_less>> libraries;
253 if (map.find(
name) != map.end()) {
256 map[
name] = {data, abi_version,
static_cast<bool>(hidden)};
263 auto it_module = it_cls->second.find(
name);
264 if (it_module != it_cls->second.end()) {
265 const auto& mod_pair = it_module->second;
266 if (mod_pair.abi_version == abi_version) {
267 return mod_pair.data;
270 abi_version <<
", have " << mod_pair.abi_version <<
")\n";
278 string filename =
"ultragrid_";
284 filename +=
name + string(
".so");
300 for (
auto &&
item : class_set) {
314 cout << cls_it->second.class_name <<
"\n";
315 auto it = libraries.find(cls_it->first);
316 if (it != libraries.end()) {
317 for (
auto &&
item : it->second) {
328 cout <<
"\t" <<
SRED(
item.first) <<
"\n\t\t" <<
item.second <<
"\n";
338 map<string, const void *> ret;
340 auto it = libraries.find(
cls);
341 if (it != libraries.end()) {
342 for (
auto &&
item : it->second) {
343 if (abi_version ==
item.second.abi_version) {
344 if (include_hidden || !
item.second.hidden) {
349 abi_version <<
", have " <<
item.second.abi_version <<
")\n";
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_WARNING
less severe errors
map< string, const void * > get_libraries_for_class(enum library_class cls, int abi_version, bool include_hidden)
void list_modules(enum library_class cls, int abi_version, bool full)
const map< enum library_class, library_class_info_t > library_class_info
static map< string, string > lib_errors
static auto & get_libmap()
void register_library(const char *name, const void *data, enum library_class cls, int abi_version, int hidden)
void open_all(const char *pattern, list< void * > &libs)
const void * load_library(const char *name, enum library_class cls, int abi_version)
@ LIBRARY_CLASS_VIDEO_POSTPROCESS
@ LIBRARY_CLASS_VIDEO_DECOMPRESS
@ LIBRARY_CLASS_VIDEO_CAPTURE
@ LIBRARY_CLASS_AUDIO_PLAYBACK
@ LIBRARY_CLASS_VIDEO_RXTX
@ LIBRARY_CLASS_AUDIO_CAPTURE
@ LIBRARY_CLASS_VIDEO_DISPLAY
@ LIBRARY_CLASS_UNDEFINED
@ LIBRARY_CLASS_CAPTURE_FILTER
@ LIBRARY_CLASS_AUDIO_COMPRESS
@ LIBRARY_CLASS_VIDEO_COMPRESS
bool operator()(const unsigned char &c1, const unsigned char &c2) const
bool operator()(const std::string &s1, const std::string &s2) const