Move ici specific files to new directory ici; add new file ici/ici.c
[official-gcc.git] / gcc / ici / plugin-loader.c
bloba698755c9e57e182f3f0b84739850f9b3bbb9853
1 /* [Legacy] environment-based interface for loading high-level plugins.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 Contributed by Inria.
6 Authors: Grigori Fursin <grigori.fursin@inria.fr>, Cupertino Miranda
7 <cupertinomiranda@gmail.com>, Zbigniew Chamski <zbigniew.chamski@gmail.com>.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "hashtab.h"
29 #include "toplev.h"
30 #include "gcc.h"
31 #include "options.h" /* flag_ici */
32 #include "gcc-plugin.h"
34 #include "highlev-plugin-internal.h"
35 #include "feature-internal.h" /* init_features() */
37 #include <dlfcn.h>
39 int ici_loaded = 0;
41 /* plugin initialization/cleanup function type */
42 typedef void (*start_stop_func) (void);
44 typedef union {
45 void *data_ptr;
46 start_stop_func function_ptr;
47 plugin_callback_func callback_ptr;
48 } dl_symbol_t ;
50 /* pointers to initialization/cleanup functions */
51 start_stop_func ici_plugin_start = NULL;
53 /* report any errors encountered during a dl* operation */
54 static inline bool check_for_dlerror (void)
56 const char *dlError;
57 dlError = dlerror ();
58 if (dlError)
60 fprintf (stderr, "Plugin manager error: DLerror: %s ...\n", dlError);
61 return true;
63 return false;
67 /* load the ICI plugin shared library file named 'dynlib_file'.
68 Return 0 if successful, and 1 otherwise. */
69 static int
70 load_ici_plugin_file (char *dynlib_file)
72 void *dlhandle;
73 bool error = 0;
74 dl_symbol_t dl_symbol;
76 if (getenv ("ICI_VERBOSE") != NULL)
77 fprintf (stderr, "ICI: loading plugin ...\n");
79 dlhandle = dlopen (dynlib_file, RTLD_LAZY);
80 error |= check_for_dlerror ();
82 /* assignment of data pointers to function pointers is forbidden in strict
83 ANSI mode - use a union to work around this dlsym() issue. */
85 /* plugin initialization function */
86 dl_symbol.data_ptr = dlsym (dlhandle, "start");
87 ici_plugin_start = dl_symbol.function_ptr;
88 error |= check_for_dlerror ();
90 /* plugin cleanup function */
91 dl_symbol.data_ptr = dlsym (dlhandle, "stop");
92 register_callback (dynlib_file, PLUGIN_FINISH, dl_symbol.callback_ptr, NULL);
93 error |= check_for_dlerror ();
95 /* if init and cleanup functions are correctly resolverd, initialize the feature list */
96 if (!error)
97 init_features ();
99 return error;
103 /* load a plugin speficied by envariable ICI_PLUGIN if either -fici was
104 given on the command line or envariable ICI_USE is set to '1'. */
105 void load_ici_plugin (void)
107 char *ici_use = getenv ("ICI_USE");
109 if ((ici_use != NULL) && (ici_use[0] == '1'))
110 flag_ici = true;
112 if (flag_ici)
114 char *dynlib_file = getenv ("ICI_PLUGIN");
115 if (dynlib_file != NULL)
117 if ((load_ici_plugin_file (dynlib_file) == 0) && ici_plugin_start)
119 ici_plugin_start ();
120 ici_loaded = 1;
123 else
125 fprintf (stderr, "ICI error: Environment variable ICI_PLUGIN is not defined ...\n");
126 exit (-1);