]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/main.cxx
1 /*******************************************************************************
2 * Simplified Wrapper and Interface Generator (SWIG)
4 * Author : David Beazley
6 * Department of Computer Science
7 * University of Chicago
10 * beazley@cs.uchicago.edu
12 * Please read the file LICENSE for the copyright and terms by which SWIG
13 * can be used and distributed.
14 *******************************************************************************/
15 /***********************************************************************
22 ***********************************************************************/
36 class SwigException
{};
38 static char *usage
= "\
39 \nDocumentation Options\n\
40 -dascii - ASCII documentation.\n\
41 -dhtml - HTML documentation.\n\
42 -dlatex - LaTeX documentation.\n\
43 -dnone - No documentation.\n\n\
45 -c - Produce raw wrapper code (omit support code)\n\
46 -c++ - Enable C++ processing\n\
47 -ci - Check a file into the SWIG library\n\
48 -co - Check a file out of the SWIG library\n\
49 -d docfile - Set name of the documentation file.\n\
50 -Dsymbol - Define a symbol (for conditional compilation)\n\
51 -I<dir> - Look for SWIG files in <dir>\n\
52 -l<ifile> - Include SWIG library file.\n\
53 -make_default - Create default constructors/destructors\n\
54 -nocomment - Ignore all comments (for documentation).\n\
55 -o outfile - Set name of the output file.\n\
56 -objc - Enable Objective C processing\n\
57 -stat - Print statistics\n\
58 -strict n - Set pointer type-checking strictness\n\
59 -swiglib - Report location of SWIG library and exit\n\
60 -t typemap_file - Use a typemap file.\n\
61 -v - Run in verbose mode\n\
62 -version - Print SWIG version number\n\
63 -help - This output.\n\n";
65 //-----------------------------------------------------------------
68 // Main program. Initializes the files and starts the parser.
69 //-----------------------------------------------------------------
89 int SWIG_main(int argc
, char *argv
[], Language
*l
, Documentation
*d
) {
94 extern void add_directory(char *);
95 extern char *get_time();
99 char *outfile_name
= 0;
100 extern int add_iname(char *);
102 int ignorecomments
= 0;
105 char *typemap_file
= 0;
106 char *includefiles
[256];
107 int includecount
= 0;
108 extern void check_suffix(char *);
109 extern void scanner_file(FILE *);
126 TypeStrict
= 2; // Very strict type checking
130 DataType::init_typedef(); // Initialize the type handler
132 // Set up some default symbols (available in both SWIG interface files
135 add_symbol("SWIG",0,0); // Define the SWIG symbol
137 add_symbol("SWIGMAC",0,0);
140 add_symbol("SWIGWIN32",0,0);
143 strcpy(LibDir
, getSwigLib());
144 SwigLib
= copy_string(LibDir
); // Make a copy of the real library location
146 sprintf(temp
,"%s:config", LibDir
);
148 add_directory(":swig_lib:config");
149 add_directory(LibDir
);
150 add_directory(":swig_lib");
152 sprintf(temp
,"%s/config", LibDir
);
154 add_directory("./swig_lib/config");
155 add_directory(LibDir
);
156 add_directory("./swig_lib");
157 sprintf(InitName
,"init_wrap");
160 sprintf(InitName
,"init_wrap");
163 for (i
= 1; i
< argc
; i
++) {
165 if (strncmp(argv
[i
],"-I",2) == 0) {
166 // Add a new directory search path
167 includefiles
[includecount
++] = copy_string(argv
[i
]+2);
169 } else if (strncmp(argv
[i
],"-D",2) == 0) {
171 add_symbol(argv
[i
]+2, (DataType
*) 0, (char *) 0);
173 } else if (strcmp(argv
[i
],"-strict") == 0) {
175 TypeStrict
= atoi(argv
[i
+1]);
182 } else if ((strcmp(argv
[i
],"-verbose") == 0) || (strcmp(argv
[i
],"-v") == 0)) {
185 } else if (strcmp(argv
[i
],"-dascii") == 0) {
188 } else if (strcmp(argv
[i
],"-dnone") == 0) {
191 } else if (strcmp(argv
[i
],"-dhtml") == 0) {
194 } else if (strcmp(argv
[i
],"-dlatex") == 0) {
197 } else if (strcmp(argv
[i
],"-nocomment") == 0) {
200 } else if (strcmp(argv
[i
],"-stat") == 0) {
203 } else if (strcmp(argv
[i
],"-c++") == 0) {
206 } else if (strcmp(argv
[i
],"-objc") == 0) {
209 } else if (strcmp(argv
[i
],"-c") == 0) {
212 } else if (strcmp(argv
[i
],"-make_default") == 0) {
215 } else if (strcmp(argv
[i
],"-swiglib") == 0) {
216 printf("%s\n", LibDir
);
218 } else if (strcmp(argv
[i
],"-o") == 0) {
221 outfile_name
= copy_string(argv
[i
+1]);
227 } else if (strcmp(argv
[i
],"-d") == 0) {
230 doc_file
= copy_string(argv
[i
+1]);
236 } else if (strcmp(argv
[i
],"-t") == 0) {
239 typemap_file
= copy_string(argv
[i
+1]);
245 } else if (strcmp(argv
[i
],"-version") == 0) {
246 fprintf(stderr
,"\nSWIG Version %d.%d %s\n", SWIG_MAJOR_VERSION
,
247 SWIG_MINOR_VERSION
, SWIG_SPIN
);
248 fprintf(stderr
,"Copyright (c) 1995-98\n");
249 fprintf(stderr
,"University of Utah and the Regents of the University of California\n");
250 fprintf(stderr
,"\nCompiled with %s\n", SWIG_CC
);
252 } else if (strncmp(argv
[i
],"-l",2) == 0) {
253 // Add a new directory search path
254 library_add(argv
[i
]+2);
256 } else if (strcmp(argv
[i
],"-co") == 0) {
259 } else if (strcmp(argv
[i
],"-ci") == 0) {
262 } else if (strcmp(argv
[i
],"-help") == 0) {
270 while (includecount
> 0) {
271 add_directory(includefiles
[--includecount
]);
274 // Create a new documentation handler
276 if (doc
== 0) doc
= new ASCII
;
278 // Open up a comment handler
280 comment_handler
= new CommentHandler();
281 comment_handler
->parse_args(argc
,argv
);
282 if (ignorecomments
) comment_handler
->style("ignore",0);
284 // Create a new documentation entry
286 doctitle
= new DocTitle("",0);
287 doctitle
->parse_args(argc
,argv
);
288 doc_entry
= doctitle
;
290 // Handle documentation module options
292 doc
->parse_args(argc
,argv
);
294 // Parse language dependent options
296 lang
->parse_args(argc
,argv
);
298 if (help
) SWIG_exit(0); // Exit if we're in help mode
300 // Check all of the options to make sure we're cool.
304 // If we made it this far, looks good. go for it....
306 // Create names of temporary files that are created
308 sprintf(infilename
,"%s", argv
[argc
-1]);
309 input_file
= new char[strlen(infilename
)+1];
310 strcpy(input_file
, infilename
);
312 // If the user has requested to check out a file, handle that
316 char *outfile
= input_file
;
318 outfile
= outfile_name
;
319 stat
= checkout_file(input_file
,outfile
);
321 fprintf(stderr
,"%s checked out from the SWIG library\n",input_file
);
323 FILE * f
= fopen(input_file
,"r");
325 fprintf(stderr
,"Unable to check-out %s. File already exists.\n", input_file
);
328 fprintf(stderr
,"Unable to check-out %s\n", input_file
);
331 } else if (checkin
) {
332 // Try to check-in a file to the SWIG library
334 char *outname
= input_file
;
336 outname
= outfile_name
;
337 stat
= checkin_file(SwigLib
, LibDir
, input_file
, outname
);
339 fprintf(stderr
,"%s checked-in to %s/%s/%s\n", input_file
, SwigLib
, LibDir
, outname
);
341 fprintf(stderr
,"Unable to check-in %s to %s/%s\n", input_file
, SwigLib
, LibDir
);
344 doctitle
->file
= copy_string(input_file
);
345 doctitle
->line_number
= -1000;
346 doctitle
->end_line
= -1000;
348 // Check the suffix for a .c file. If so, we're going to
349 // declare everything we see as "extern"
351 check_suffix(infilename
);
355 c
= infilename
+ strlen(infilename
);
356 while (c
!= infilename
) {
366 sprintf(fn_header
,"%s_wrap.c",infilename
);
367 strcpy(infile
,infilename
);
368 strcpy(output_dir
,"");
370 sprintf(fn_header
,"%s",outfile_name
);
371 // Try to identify the output directory
372 char *cc
= outfile_name
;
373 char *lastc
= outfile_name
;
376 if (*cc
== ':') lastc
= cc
+1;
378 if (*cc
== '/') lastc
= cc
+1;
383 char *dd
= output_dir
;
384 while (cc
!= lastc
) {
390 // Patch up the input filename
391 cc
= infilename
+ strlen(infilename
);
392 while (cc
!= infilename
) {
409 sprintf(fn_wrapper
,"%s%s_wrap.wrap",output_dir
,infile
);
410 sprintf(fn_init
,"%s%s_wrap.init",output_dir
,infile
);
412 sprintf(title
,"%s", fn_header
);
416 if ((f_input
= fopen(input_file
,"r")) == 0) {
417 // Okay. File wasn't found right away. Let's see if we can
418 // extract it from the SWIG library instead.
419 if ((checkout_file(input_file
,input_file
)) == -1) {
420 fprintf(stderr
,"Unable to open %s\n", input_file
);
423 // Successfully checked out a file from the library, print a warning and
426 fprintf(stderr
,"%s checked out from the SWIG library.\n",input_file
);
427 if ((f_input
= fopen(input_file
,"r")) == 0) {
428 fprintf(stderr
,"Unable to open %s\n", input_file
);
434 // Add to the include list
436 add_iname(infilename
);
438 // Initialize the scanner
441 scanner_file(LEX_in
);
443 // printf("fn_header = %s\n", fn_header);
444 // printf("fn_wrapper = %s\n", fn_wrapper);
445 // printf("fn_init = %s\n", fn_init);
447 if((f_header
= fopen(fn_header
,"w")) == 0) {
448 fprintf(stderr
,"Unable to open %s\n", fn_header
);
451 if((f_wrappers
= fopen(fn_wrapper
,"w")) == 0) {
452 fprintf(stderr
,"Unable to open %s\n",fn_wrapper
);
455 if ((f_init
= fopen(fn_init
,"w")) == 0) {
456 fprintf(stderr
,"Unable to open %s\n",fn_init
);
460 // Open up documentation
465 doc_file
= new char[strlen(infile
)+strlen(output_dir
)+8];
466 sprintf(doc_file
,"%s%s_wrap",output_dir
,infile
);
470 // Set up the typemap for handling new return strings
472 DataType
*temp_t
= new DataType(T_CHAR
);
473 temp_t
->is_pointer
++;
475 typemap_register("newfree",typemap_lang
,temp_t
,"","delete [] $source;\n",0);
477 typemap_register("newfree",typemap_lang
,temp_t
,"","free($source);\n",0);
482 // Define the __cplusplus symbol
484 add_symbol("__cplusplus",0,0);
487 // Load up the typemap file if given
490 if (include_file(typemap_file
) == -1) {
491 fprintf(stderr
,"Unable to locate typemap file %s. Aborting.\n", typemap_file
);
496 // If in Objective-C mode. Load in a configuration file
499 // Add the 'id' object type as a void *
500 /* DataType *t = new DataType(T_VOID);
503 t->typedef_add("id");
508 // Pass control over to the specific language interpreter
515 swig_append(fn_wrapper
,f_header
);
516 swig_append(fn_init
,f_header
);
520 // Print out documentation. Due to tree-like nature of documentation,
521 // printing out the title prints out everything.
524 doctitle
->output(doc
);
525 doctitle
= doctitle
->next
;
530 // Remove temporary files
535 // If only producing documentation, remove the wrapper file as well
540 // Check for undefined types that were used.
543 type_undefined_check();
546 fprintf(stderr
,"Wrapped %d functions\n", Stat_func
);
547 fprintf(stderr
,"Wrapped %d variables\n", Stat_var
);
548 fprintf(stderr
,"Wrapped %d constants\n", Stat_const
);
549 type_undefined_check();
553 // File was checked out from the SWIG library. Remove it now
559 } catch (SwigException
) {
568 // --------------------------------------------------------------------------
571 // Fatal parser error. Exit and cleanup
572 // --------------------------------------------------------------------------
574 void SWIG_exit(int) {
591 throw SwigException();
596 // --------------------------------------------------------------------------
597 // swig_pragma(char *name, char *value)
599 // Handle pragma directives. Not many supported right now
600 // --------------------------------------------------------------------------
602 void swig_pragma(char *name
, char *value
) {
604 if (strcmp(name
,"make_default") == 0) {
607 if (strcmp(name
,"no_default") == 0) {
610 if (strcmp(name
,"objc_new") == 0) {
611 objc_construct
= copy_string(value
);
613 if (strcmp(name
,"objc_delete") == 0) {
614 objc_destruct
= copy_string(value
);
624 // Check for SWIG_LIB environment variable
625 if ((c
= getenv("SWIG_LIB")) != (char *) 0) {
631 // use executable location
632 static char path
[256];
634 strcpy(path
, __argv
[0]);
635 last
= strrchr(path
, '/');
636 if (! last
) last
= strrchr(path
, '\\');
638 strcpy(last
+1, "swig_lib");
640 strcpy(path
, "swig_lib");
644 printf("Using swig lib at: %s\n", rv
);