1 /*
2     This file is part of darktable,
3     Copyright (C) 2011-2020 darktable developers.
4 
5     darktable is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     darktable is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with darktable.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifdef HAVE_OPENCL
20 
21 #include <stdlib.h>
22 #ifndef __APPLE__
23 #include <stdio.h>
24 #include <string.h>
25 #else //!__APPLE__
26 #include <dlfcn.h>
27 #include <glib.h>
28 #endif //!__APPLE__
29 
30 #include "common/dynload.h"
31 
32 
33 #ifndef __APPLE__
34 /* check if gmodules is supported on this platform */
dt_gmodule_supported(void)35 int dt_gmodule_supported(void)
36 {
37   int success = g_module_supported();
38 
39   return success;
40 }
41 
42 
43 /* dynamically load library */
dt_gmodule_open(const char * library)44 dt_gmodule_t *dt_gmodule_open(const char *library)
45 {
46   dt_gmodule_t *module = NULL;
47   GModule *gmodule;
48   char *name;
49 
50   if(strchr(library, '/') == NULL)
51   {
52     name = g_module_build_path(NULL, library);
53   }
54   else
55   {
56     name = g_strdup(library);
57   }
58 
59   gmodule = g_module_open(name, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
60 
61   if(gmodule != NULL)
62   {
63     module = (dt_gmodule_t *)malloc(sizeof(dt_gmodule_t));
64     module->gmodule = gmodule;
65     module->library = name;
66   }
67   else
68     g_free(name);
69 
70   return module;
71 }
72 
73 
74 /* get pointer to symbol */
dt_gmodule_symbol(dt_gmodule_t * module,const char * name,void (** pointer)(void))75 int dt_gmodule_symbol(dt_gmodule_t *module, const char *name, void (**pointer)(void))
76 {
77   int success = g_module_symbol(module->gmodule, name, (gpointer)pointer);
78 
79   return success;
80 }
81 #else //!__APPLE__
82 /* check if gmodules is supported on this platform */
dt_gmodule_supported(void)83 int dt_gmodule_supported(void)
84 {
85   return TRUE;
86 }
87 
88 /* dynamically load library */
dt_gmodule_open(const char * library)89 dt_gmodule_t *dt_gmodule_open(const char *library)
90 {
91   // logic here is simplified since it's used only specifically for OpenCL
92   dt_gmodule_t *module = NULL;
93   void *gmodule = dlopen(library, RTLD_LAZY | RTLD_LOCAL);
94 
95   if(gmodule != NULL)
96   {
97     module = (dt_gmodule_t *)malloc(sizeof(dt_gmodule_t));
98     module->gmodule = gmodule;
99     module->library = g_strdup(library);
100   }
101 
102   return module;
103 }
104 
105 /* get pointer to symbol */
dt_gmodule_symbol(dt_gmodule_t * module,const char * name,void (** pointer)(void))106 int dt_gmodule_symbol(dt_gmodule_t *module, const char *name, void (**pointer)(void))
107 {
108   *pointer = dlsym(module->gmodule, name);
109 
110   return *pointer != NULL ? TRUE : FALSE;
111 }
112 #endif //!__APPLE__
113 #endif //HAVE_OPENCL
114 
115 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
116 // vim: shiftwidth=2 expandtab tabstop=2 cindent
117 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
118