Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob-1.2 / src / plugins / plugin_compile.cpp
bloba2e8657123c0726c8a30ca836b1ee6574d3becd1
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <gclib/gclib.h>
8 #include <sys/wait.h>
9 #include "../mod.h"
10 #include "../head.h"
11 #include "../dbg.h"
13 extern "C" DArray * plugin_init (struct env_t *env);
14 struct env_t *ENV;
15 EHash * compile_env;
17 char * __name2obj (char * name)
19 char *S;
20 char m_buf[512];
21 strcpy (m_buf, name);
22 S = rindex (m_buf, '.');
23 if (! S)
24 return NULL;
25 strcpy (S, ".o");
26 return strdup (m_buf);
29 void __aa (EArray * a, DArray * b)
31 int count;
32 int i;
34 if (! a || ! b)
35 return;
37 count = b->get_size ();
38 for (i = 0; i < count; ++i)
39 a->add (strdup (LPCHAR (b->get (i))));
42 void __astr (EArray * a, char *str)
44 DArray * d_split;
46 if (! a || ! str)
47 return;
49 d_split = Dsplit (str, " ");
50 if (! d_split)
51 return;
53 __aa (a, d_split);
54 d_split->foreach (free);
55 delete d_split;
58 void __array_log (EArray * a)
60 int i;
61 if (! a)
62 return;
63 for (i = 0; i < a->get_size (); ++i) {
64 if (a->get (i))
65 printf ("%s ", a->get (i));
67 printf ("\n");
70 int compile_cppfile (char * name)
72 int i;
73 int pid;
74 int status;
75 char * oname = __name2obj (name);
76 EArray * d_param;
78 if (! oname)
79 return -1;
81 d_param = new EArray(32);
82 d_param->add (compile_env->get ("CXX"));
83 __astr (d_param, compile_env->get ("CXXFLAGS"));
84 if (compile_env->get ("OPTS"))
85 __astr (d_param, compile_env->get ("OPTS"));
86 d_param->add ("-c");
87 d_param->add ("-o");
88 d_param->add (oname);
89 d_param->add (name);
90 d_param->add (LPCHAR (NULL));
92 if (SB_FLGET (SB_FLVERBOSE))
93 __array_log (d_param);
94 else
95 printf ("\tCXX\t%s\n", oname);
97 pid = vfork ();
98 if (pid == 0)
99 execvp (compile_env->get ("CXX"), d_param->get_skeleton ());
100 else
101 waitpid (pid, &status, 0);
103 free (oname);
106 int compile_cfile (char * name)
108 int i;
109 int pid;
110 int status;
111 char * oname = __name2obj (name);
112 EArray * d_param;
114 if (! oname)
115 return -1;
117 d_param = new EArray(32);
118 d_param->add (compile_env->get ("CC"));
119 __astr (d_param, compile_env->get ("CFLAGS"));
120 if (compile_env->get ("OPTS"))
121 __astr (d_param, compile_env->get ("OPTS"));
122 d_param->add ("-c");
123 d_param->add ("-o");
124 d_param->add (oname);
125 d_param->add (name);
126 d_param->add (LPCHAR (NULL));
128 if (SB_FLGET (SB_FLVERBOSE))
129 __array_log (d_param);
130 else
131 printf ("\tCC\t%s\n", oname);
133 pid = vfork ();
134 if (pid == 0)
135 execvp (compile_env->get ("CC"), d_param->get_skeleton ());
136 else
137 waitpid (pid, &status, 0);
139 free (oname);
142 int compile_files ()
144 char *S;
145 int count;
146 int i;
148 count = ENV->d_files->get_size ();
149 for (i = 0; i < count; ++i) {
150 S = rindex (ENV->d_files->get (i), '.');
151 if (! S)
152 continue;
153 if (EQ (S, ".cpp") || EQ (S, ".cxx"))
154 compile_cppfile (ENV->d_files->get (i));
155 if (EQ (S, ".c"))
156 compile_cfile (ENV->d_files->get (i));
159 return 0;
162 void __init_compile_env ()
164 char *cc;
165 char *cxx;
166 char *cflags;
167 char *cxxflags;
168 char *opts;
170 compile_env = new EHash;
171 cc = ENV->settings->get ("CC");
172 cxx = ENV->settings->get ("CXX");
173 cflags = ENV->settings->get ("CFLAGS");
174 cxxflags = ENV->settings->get ("CXXFLAGS");
175 opts = ENV->settings->get ("OPTS");
177 if (! cc)
178 cc = "gcc";
179 if (! cxx)
180 cxx = "g++";
181 if (! cflags)
182 cflags = "-O3 -Wall -pipe";
183 if (! cxxflags)
184 cxxflags = cflags;
186 if (SB_FLGET (SB_FLVERBOSE)) {
187 printf ("C compiler: %s\n", cc);
188 printf ("C++ compiler: %s\n", cxx);
189 printf ("C flags: %s\n", cflags);
190 printf ("C++ flags: %s\n", cxxflags);
192 compile_env->set ("CC", strdup (cc));
193 compile_env->set ("CXX", strdup (cxx));
194 compile_env->set ("CFLAGS", strdup (cflags));
195 compile_env->set ("CXXFLAGS", strdup (cxxflags));
196 if (opts)
197 compile_env->set ("OPTS", strdup (opts));
200 char compile_opt2 (DArray * d_opts, int * pos)
202 char m_buf[512];
203 char * S;
205 if (! d_opts || ! pos)
206 return 0;
208 S = d_opts->get (*pos);
209 if (EQ (S, "--compile")) {
210 __init_compile_env ();
211 compile_files ();
212 exit (0);
215 return 0;
218 void compile_info ()
220 printf ("C/C++ compilation plugin.\n");
221 printf ("Version: 0.2\n");
222 printf ("options: --compile\n");
225 DArray * plugin_init (struct env_t *env)
227 DArray * Ret;
228 struct mod_feature * pm;
230 ENV = env;
231 Ret = new DArray (1);
232 pm = CNEW (mod_feature, 1);
233 memset (pm, 0, sizeof (mod_feature));
234 pm->mod.Type = TYPE_FEATURE;
235 pm->mod.Version = strdup ("1.0");
236 pm->mod.info = compile_info;
237 pm->opt2 = compile_opt2;
239 Ret->add (LPCHAR (pm));
240 return Ret;