2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
7 #include <gclib/gclib.h>
13 extern "C" DArray
* plugin_init (struct env_t
*env
);
15 EHash
* __hv_compile_env
;
21 void __aa (EArray
* a
, DArray
* b
)
29 count
= b
->get_size ();
30 for (i
= 0; i
< count
; ++i
)
31 a
->add (strdup (LPCHAR (b
->get (i
))));
34 void __astr (EArray
* a
, char *str
)
41 d_split
= Dsplit (str
, (char *) " ");
46 d_split
->foreach (free
);
50 void __array_log (EArray
* a
)
55 for (i
= 0; i
< a
->get_size (); ++i
) {
57 printf ("%s ", a
->get (i
));
62 int compile_cppfile (char * name
)
66 char * oname
= name2obj (name
);
73 printf ("$CXX $CXXFLAGS $OPTS $INCLUDE -c -o %s %s\n", oname
, name
);
77 d_param
= new EArray(32);
78 d_param
->add ( (*__hv_compile_env
)["CXX"] );
79 __astr (d_param
, (*__hv_compile_env
)["CXXFLAGS"]);
80 if (strlen ((*__hv_compile_env
)["OPTS"]))
81 __astr (d_param
, (*__hv_compile_env
)["OPTS"]);
82 if (strlen ((*__hv_compile_env
)["INCLUDE"]))
83 __astr (d_param
, (*__hv_compile_env
)["INCLUDE"]);
85 *d_param
<< (char *) "-c" << (char *) "-o" << oname
<< name
<< NULL
;
87 if (SB_FLGET (SB_FLVERBOSE
))
88 __array_log (d_param
);
90 printf ("\tCXX\t%s\n", oname
);
94 execvp ((*__hv_compile_env
)["CXX"], d_param
->get_skeleton ());
96 waitpid (pid
, &status
, 0);
102 int compile_cfile (char * name
)
104 char * oname
= name2obj (name
);
113 printf ("$CC $CFLAGS $OPTS $INCLUDE -c -o %s %s\n", oname
, name
);
117 d_param
= new EArray(32);
118 *d_param
<< (*__hv_compile_env
)["CC"];
119 __astr (d_param
, (*__hv_compile_env
)["CFLAGS"]);
120 if (strlen ((*__hv_compile_env
)["OPTS"]))
121 __astr (d_param
, (*__hv_compile_env
)["OPTS"]);
122 if (strlen ( (*__hv_compile_env
)["INCLUDE"] ))
123 __astr (d_param
, (*__hv_compile_env
)["INCLUDE"]);
125 *d_param
<< (char *) "-c" << (char *) "-o" << oname
<< name
<< NULL
;
127 if (SB_FLGET (SB_FLVERBOSE
))
128 __array_log (d_param
);
130 printf ("\tCC\t%s\n", oname
);
134 execvp ((*__hv_compile_env
)["CC"], d_param
->get_skeleton ());
136 waitpid (pid
, &status
, 0);
142 int __cc_build_target ()
151 count
= ENV
->d_files
->get_size ();
152 cmd
= new EArray (ENV
->d_files
->get_size ()+32);
153 *cmd
<< (*__hv_compile_env
)["CC"];
156 __astr (cmd
, (*__hv_compile_env
)["LDFLAGS"]);
157 __astr (cmd
, (*__hv_compile_env
)["LIBS"]);
159 *cmd
<< (char *) "$LDFLAGS";
160 *cmd
<< (char *) "$LIBS";
164 *cmd
<< (char *) "-shared";
166 for (i
= 0; i
< count
; ++i
)
167 cmd
->add (name2obj (ENV
->d_files
->get (i
)));
169 if (__cc_shared
&& __cc_program
) {
170 fprintf (stderr
, "only one --program or --shared option can be used,\n");
174 *cmd
<< (char *) "-o";
178 oname
= __cc_program
;
181 *cmd
<< LPCHAR(NULL
);
183 if (__cc_script
|| SB_FLGET (SB_FLVERBOSE
)) {
187 printf ("\tLD\t%s\n", oname
);
194 execvp ((*cmd
)[0], cmd
->get_skeleton ());
196 waitpid (pid
, &status
, 0);
209 if (__cc_shared
&& ! strstr ((*__hv_compile_env
)["OPTS"], "-fPIC")) {
210 strcpy (opts_buf
, "-fPIC -DPIC");
211 strcat (opts_buf
, (*__hv_compile_env
)["OPTS"]);
212 free ((*__hv_compile_env
)["OPTS"]);
213 __hv_compile_env
->set ((char *) "OPTS", opts_buf
);
217 printf ("export CC=\"%s\"\n", (*__hv_compile_env
)["CC"]);
218 printf ("export CXX=\"%s\"\n", (*__hv_compile_env
)["CXX"]);
219 printf ("export CFLAGS=\"%s\"\n", (*__hv_compile_env
)["CFLAGS"]);
220 printf ("export CXXFLAGS=\"%s\"\n", (*__hv_compile_env
)["CXXFLAGS"]);
221 printf ("export INCLUDE=\"%s\"\n", (*__hv_compile_env
)["INCLUDE"]);
222 printf ("export OPTS=\"%s\"\n", (*__hv_compile_env
)["OPTS"]);
223 printf ("export LDFLAGS=\"%s\"\n", (*__hv_compile_env
)["LDFLAGS"]);
224 printf ("export LIBS=\"%s\"\n\n", (*__hv_compile_env
)["LIBS"]);
227 count
= ENV
->d_files
->get_size ();
228 for (i
= 0; i
< count
; ++i
) {
229 S
= rindex (ENV
->d_files
->get (i
), '.');
232 if (EQ (S
, ".cpp") || EQ (S
, ".cxx"))
233 compile_cppfile (ENV
->d_files
->get (i
));
235 compile_cfile (ENV
->d_files
->get (i
));
238 if (__cc_shared
|| __cc_program
)
239 __cc_build_target ();
244 char compile_opt (DArray
* d_opts
, int * pos
)
248 if (! d_opts
|| ! pos
)
251 opt
= d_opts
->get (*pos
);
252 count
= d_opts
->get_size ();
253 if (EQ (opt
, "--cc-script")) {
258 if (EQ (opt
, "--cc-shared") ||
259 EQ (opt
, "--compile-shared")) {
263 __cc_shared
= d_opts
->get (*pos
);
267 if (EQ (opt
, "--cc-program") ||
268 EQ (opt
, "--compile-program")) {
272 __cc_program
= d_opts
->get (*pos
);
276 if (EQ (opt
, "--cc-env") ||
277 EQ (opt
, "--compile-env")) {
278 __compile_env
= true;
285 char compile_opt2 (DArray
* d_opts
, int * pos
)
289 if (! d_opts
|| ! pos
)
292 S
= d_opts
->get (*pos
);
293 if (EQ (S
, "--compile") ||
295 __hv_compile_env
= bob_init_compile_env ();
303 void compile_short_info ()
305 printf ("C/C++ compilation plugin.");
308 void compile_long_info ()
310 printf ("C/C++ compilation plugin.\n");
311 printf ("Version: 0.3\n");
312 printf ("options: -cc --cc-script --cc-shared --cc-program) \n");
315 DArray
* plugin_init (struct env_t
*env
)
320 __compile_env
= false;
322 Ret
= new DArray (1);
323 pm
= CNEW (mod_t
, 1);
324 memset (pm
, 0, sizeof (mod_t
));
325 pm
->Version
= strdup ("1.0");
326 pm
->short_info
= compile_short_info
;
327 pm
->long_info
= compile_long_info
;
328 pm
->opt
= compile_opt
;
329 pm
->opt2
= compile_opt2
;
334 ENV
->listOptions
->add ("--cc-script");
335 ENV
->listOptions
->add ("-compile");
336 ENV
->listOptions
->add ("-cc");
337 ENV
->listOptions
->add ("--cc-shared");
338 ENV
->listOptions
->add ("--compile-shared");
339 ENV
->listOptions
->add ("--cc-program");
340 ENV
->listOptions
->add ("--compile-program");
341 ENV
->listOptions
->add ("--cc-env");
342 ENV
->listOptions
->add ("--compile-env");
344 Ret
->add (LPCHAR (pm
));