]>
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 //-----------------------------------------------------------------
87 int SWIG_main(int argc
, char *argv
[], Language
*l
, Documentation
*d
) {
92 extern void add_directory(char *);
93 extern char *get_time();
97 char *outfile_name
= 0;
98 extern int add_iname(char *);
100 int ignorecomments
= 0;
103 char *typemap_file
= 0;
104 char *includefiles
[256];
105 int includecount
= 0;
106 extern void check_suffix(char *);
107 extern void scanner_file(FILE *);
122 TypeStrict
= 2; // Very strict type checking
126 DataType::init_typedef(); // Initialize the type handler
128 // Set up some default symbols (available in both SWIG interface files
131 add_symbol("SWIG",0,0); // Define the SWIG symbol
133 add_symbol("SWIGMAC",0,0);
136 add_symbol("SWIGWIN32",0,0);
139 strcpy(LibDir
, getSwigLib());
140 SwigLib
= copy_string(LibDir
); // Make a copy of the real library location
142 sprintf(temp
,"%s:config", LibDir
);
144 add_directory(":swig_lib:config");
145 add_directory(LibDir
);
146 add_directory(":swig_lib");
148 sprintf(temp
,"%s/config", LibDir
);
150 add_directory("./swig_lib/config");
151 add_directory(LibDir
);
152 add_directory("./swig_lib");
153 sprintf(InitName
,"init_wrap");
156 sprintf(InitName
,"init_wrap");
159 for (i
= 1; i
< argc
; i
++) {
161 if (strncmp(argv
[i
],"-I",2) == 0) {
162 // Add a new directory search path
163 includefiles
[includecount
++] = copy_string(argv
[i
]+2);
165 } else if (strncmp(argv
[i
],"-D",2) == 0) {
167 add_symbol(argv
[i
]+2, (DataType
*) 0, (char *) 0);
169 } else if (strcmp(argv
[i
],"-strict") == 0) {
171 TypeStrict
= atoi(argv
[i
+1]);
178 } else if ((strcmp(argv
[i
],"-verbose") == 0) || (strcmp(argv
[i
],"-v") == 0)) {
181 } else if (strcmp(argv
[i
],"-dascii") == 0) {
184 } else if (strcmp(argv
[i
],"-dnone") == 0) {
187 } else if (strcmp(argv
[i
],"-dhtml") == 0) {
190 } else if (strcmp(argv
[i
],"-dlatex") == 0) {
193 } else if (strcmp(argv
[i
],"-nocomment") == 0) {
196 } else if (strcmp(argv
[i
],"-stat") == 0) {
199 } else if (strcmp(argv
[i
],"-c++") == 0) {
202 } else if (strcmp(argv
[i
],"-objc") == 0) {
205 } else if (strcmp(argv
[i
],"-c") == 0) {
208 } else if (strcmp(argv
[i
],"-make_default") == 0) {
211 } else if (strcmp(argv
[i
],"-swiglib") == 0) {
212 printf("%s\n", LibDir
);
214 } else if (strcmp(argv
[i
],"-o") == 0) {
217 outfile_name
= copy_string(argv
[i
+1]);
223 } else if (strcmp(argv
[i
],"-d") == 0) {
226 doc_file
= copy_string(argv
[i
+1]);
232 } else if (strcmp(argv
[i
],"-t") == 0) {
235 typemap_file
= copy_string(argv
[i
+1]);
241 } else if (strcmp(argv
[i
],"-version") == 0) {
242 fprintf(stderr
,"\nSWIG Version %d.%d %s\n", SWIG_MAJOR_VERSION
,
243 SWIG_MINOR_VERSION
, SWIG_SPIN
);
244 fprintf(stderr
,"Copyright (c) 1995-98\n");
245 fprintf(stderr
,"University of Utah and the Regents of the University of California\n");
246 fprintf(stderr
,"\nCompiled with %s\n", SWIG_CC
);
248 } else if (strncmp(argv
[i
],"-l",2) == 0) {
249 // Add a new directory search path
250 library_add(argv
[i
]+2);
252 } else if (strcmp(argv
[i
],"-co") == 0) {
255 } else if (strcmp(argv
[i
],"-ci") == 0) {
258 } else if (strcmp(argv
[i
],"-help") == 0) {
266 while (includecount
> 0) {
267 add_directory(includefiles
[--includecount
]);
270 // Create a new documentation handler
272 if (doc
== 0) doc
= new ASCII
;
274 // Open up a comment handler
276 comment_handler
= new CommentHandler();
277 comment_handler
->parse_args(argc
,argv
);
278 if (ignorecomments
) comment_handler
->style("ignore",0);
280 // Create a new documentation entry
282 doctitle
= new DocTitle("",0);
283 doctitle
->parse_args(argc
,argv
);
284 doc_entry
= doctitle
;
286 // Handle documentation module options
288 doc
->parse_args(argc
,argv
);
290 // Parse language dependent options
292 lang
->parse_args(argc
,argv
);
294 if (help
) SWIG_exit(0); // Exit if we're in help mode
296 // Check all of the options to make sure we're cool.
300 // If we made it this far, looks good. go for it....
302 // Create names of temporary files that are created
304 sprintf(infilename
,"%s", argv
[argc
-1]);
305 input_file
= new char[strlen(infilename
)+1];
306 strcpy(input_file
, infilename
);
308 // If the user has requested to check out a file, handle that
312 char *outfile
= input_file
;
314 outfile
= outfile_name
;
315 stat
= checkout_file(input_file
,outfile
);
317 fprintf(stderr
,"%s checked out from the SWIG library\n",input_file
);
319 FILE * f
= fopen(input_file
,"r");
321 fprintf(stderr
,"Unable to check-out %s. File already exists.\n", input_file
);
324 fprintf(stderr
,"Unable to check-out %s\n", input_file
);
327 } else if (checkin
) {
328 // Try to check-in a file to the SWIG library
330 char *outname
= input_file
;
332 outname
= outfile_name
;
333 stat
= checkin_file(SwigLib
, LibDir
, input_file
, outname
);
335 fprintf(stderr
,"%s checked-in to %s/%s/%s\n", input_file
, SwigLib
, LibDir
, outname
);
337 fprintf(stderr
,"Unable to check-in %s to %s/%s\n", input_file
, SwigLib
, LibDir
);
340 doctitle
->file
= copy_string(input_file
);
341 doctitle
->line_number
= -1000;
342 doctitle
->end_line
= -1000;
344 // Check the suffix for a .c file. If so, we're going to
345 // declare everything we see as "extern"
347 check_suffix(infilename
);
351 c
= infilename
+ strlen(infilename
);
352 while (c
!= infilename
) {
362 sprintf(fn_header
,"%s_wrap.c",infilename
);
363 strcpy(infile
,infilename
);
364 strcpy(output_dir
,"");
366 sprintf(fn_header
,"%s",outfile_name
);
367 // Try to identify the output directory
368 char *cc
= outfile_name
;
369 char *lastc
= outfile_name
;
372 if (*cc
== ':') lastc
= cc
+1;
374 if (*cc
== '/') lastc
= cc
+1;
379 char *dd
= output_dir
;
380 while (cc
!= lastc
) {
386 // Patch up the input filename
387 cc
= infilename
+ strlen(infilename
);
388 while (cc
!= infilename
) {
405 sprintf(fn_wrapper
,"%s%s_wrap.wrap",output_dir
,infile
);
406 sprintf(fn_init
,"%s%s_wrap.init",output_dir
,infile
);
408 sprintf(title
,"%s", fn_header
);
412 if ((f_input
= fopen(input_file
,"r")) == 0) {
413 // Okay. File wasn't found right away. Let's see if we can
414 // extract it from the SWIG library instead.
415 if ((checkout_file(input_file
,input_file
)) == -1) {
416 fprintf(stderr
,"Unable to open %s\n", input_file
);
419 // Successfully checked out a file from the library, print a warning and
422 fprintf(stderr
,"%s checked out from the SWIG library.\n",input_file
);
423 if ((f_input
= fopen(input_file
,"r")) == 0) {
424 fprintf(stderr
,"Unable to open %s\n", input_file
);
430 // Add to the include list
432 add_iname(infilename
);
434 // Initialize the scanner
437 scanner_file(LEX_in
);
439 // printf("fn_header = %s\n", fn_header);
440 // printf("fn_wrapper = %s\n", fn_wrapper);
441 // printf("fn_init = %s\n", fn_init);
443 if((f_header
= fopen(fn_header
,"w")) == 0) {
444 fprintf(stderr
,"Unable to open %s\n", fn_header
);
447 if((f_wrappers
= fopen(fn_wrapper
,"w")) == 0) {
448 fprintf(stderr
,"Unable to open %s\n",fn_wrapper
);
451 if ((f_init
= fopen(fn_init
,"w")) == 0) {
452 fprintf(stderr
,"Unable to open %s\n",fn_init
);
456 // Open up documentation
461 doc_file
= new char[strlen(infile
)+strlen(output_dir
)+8];
462 sprintf(doc_file
,"%s%s_wrap",output_dir
,infile
);
466 // Set up the typemap for handling new return strings
468 DataType
*temp_t
= new DataType(T_CHAR
);
469 temp_t
->is_pointer
++;
471 typemap_register("newfree",typemap_lang
,temp_t
,"","delete [] $source;\n",0);
473 typemap_register("newfree",typemap_lang
,temp_t
,"","free($source);\n",0);
478 // Define the __cplusplus symbol
480 add_symbol("__cplusplus",0,0);
483 // Load up the typemap file if given
486 if (include_file(typemap_file
) == -1) {
487 fprintf(stderr
,"Unable to locate typemap file %s. Aborting.\n", typemap_file
);
492 // If in Objective-C mode. Load in a configuration file
495 // Add the 'id' object type as a void *
496 /* DataType *t = new DataType(T_VOID);
499 t->typedef_add("id");
504 // Pass control over to the specific language interpreter
511 swig_append(fn_wrapper
,f_header
);
512 swig_append(fn_init
,f_header
);
516 // Print out documentation. Due to tree-like nature of documentation,
517 // printing out the title prints out everything.
520 doctitle
->output(doc
);
521 doctitle
= doctitle
->next
;
526 // Remove temporary files
531 // If only producing documentation, remove the wrapper file as well
536 // Check for undefined types that were used.
539 type_undefined_check();
542 fprintf(stderr
,"Wrapped %d functions\n", Stat_func
);
543 fprintf(stderr
,"Wrapped %d variables\n", Stat_var
);
544 fprintf(stderr
,"Wrapped %d constants\n", Stat_const
);
545 type_undefined_check();
549 // File was checked out from the SWIG library. Remove it now
555 } catch (SwigException
) {
564 // --------------------------------------------------------------------------
567 // Fatal parser error. Exit and cleanup
568 // --------------------------------------------------------------------------
570 void SWIG_exit(int) {
587 throw SwigException();
592 // --------------------------------------------------------------------------
593 // swig_pragma(char *name, char *value)
595 // Handle pragma directives. Not many supported right now
596 // --------------------------------------------------------------------------
598 void swig_pragma(char *name
, char *value
) {
600 if (strcmp(name
,"make_default") == 0) {
603 if (strcmp(name
,"no_default") == 0) {
606 if (strcmp(name
,"objc_new") == 0) {
607 objc_construct
= copy_string(value
);
609 if (strcmp(name
,"objc_delete") == 0) {
610 objc_destruct
= copy_string(value
);
620 // Check for SWIG_LIB environment variable
621 if ((c
= getenv("SWIG_LIB")) != (char *) 0) {
627 // use executable location
628 static char path
[256];
630 strcpy(path
, __argv
[0]);
631 last
= strrchr(path
, '/');
632 if (! last
) last
= strrchr(path
, '\\');
634 strcpy(last
+1, "swig_lib");
636 strcpy(path
, "swig_lib");
640 printf("Using swig lib at: %s\n", rv
);