RBBS_FIXEDSIZE should not affect _AdjustBands (found by Mike McCormack
[wine] / tools / winemaker
1 #!/usr/bin/perl -w
2
3 # Copyright 2000 Francois Gouget for CodeWeavers
4 # fgouget@codeweavers.com
5 #
6 my $version="0.5.7";
7
8 use Cwd;
9 use File::Basename;
10 use File::Copy;
11
12
13
14 #####
15 #
16 # Options
17 #
18 #####
19
20 # The following constants define what we do with the case of filenames
21
22 ##
23 # Never rename a file to lowercase
24 my $OPT_LOWER_NONE=0;
25
26 ##
27 # Rename all files to lowercase
28 my $OPT_LOWER_ALL=1;
29
30 ##
31 # Rename only files that are all uppercase to lowercase
32 my $OPT_LOWER_UPPERCASE=2;
33
34
35 # The following constants define whether to ask questions or not
36
37 ##
38 # No (synonym of never)
39 my $OPT_ASK_NO=0;
40
41 ##
42 # Yes (always)
43 my $OPT_ASK_YES=1;
44
45 ##
46 # Skip the questions till the end of this scope
47 my $OPT_ASK_SKIP=-1;
48
49
50 # General options
51
52 ##
53 # This is the directory in which winemaker will operate.
54 my $opt_work_dir;
55
56 ##
57 # Make a backup of the files
58 my $opt_backup;
59
60 ##
61 # Defines which files to rename
62 my $opt_lower;
63
64 ##
65 # If we don't find the file referenced by an include, lower it
66 my $opt_lower_include;
67
68
69 # Options for the 'Source' method
70
71 ##
72 # Specifies that we have only one target so that all sources relate 
73 # to this target. By default this variable is left undefined which 
74 # means winemaker should try to find out by itself what the targets 
75 # are. If not undefined then this contains the name of the default 
76 # target (without the extension).
77 my $opt_single_target;
78
79 ##
80 # If '$opt_single_target' has been specified then this is the type of 
81 # that target. Otherwise it specifies whether the default target type 
82 # is guiexe or cuiexe.
83 my $opt_target_type;
84
85 ##
86 # Contains the default set of flags to be used when creating a new target.
87 my $opt_flags;
88
89 ##
90 # If true then winemaker should ask questions to the user as it goes 
91 # along.
92 my $opt_is_interactive;
93 my $opt_ask_project_options;
94 my $opt_ask_target_options;
95
96 ##
97 # If false then winemaker should not generate any file, i.e. 
98 # no makefiles, but also no .spec files, no configure.in, etc.
99 my $opt_no_generated_files;
100
101 ##
102 # Specifies not to print the banner if set.
103 my $opt_no_banner;
104
105
106
107 #####
108 #
109 # Target modelization
110 #
111 #####
112
113 # The description of a target is stored in an array. The constants 
114 # below identify what is stored at each index of the array.
115
116 ##
117 # This is the name of the target.
118 my $T_NAME=0;
119
120 ##
121 # Defines the type of target we want to build. See the TT_xxx
122 # constants below
123 my $T_TYPE=1;
124
125 ##
126 # Defines the target's enty point, i.e. the function that is called
127 # on startup.
128 my $T_INIT=2;
129
130 ##
131 # This is a bitfield containing flags refining the way the target 
132 # should be handled. See the TF_xxx constants below
133 my $T_FLAGS=3;
134
135 ##
136 # This is a reference to an array containing the list of the 
137 # resp. C, C++, RC, other (.h, .hxx, etc.) source files.
138 my $T_SOURCES_C=4;
139 my $T_SOURCES_CXX=5;
140 my $T_SOURCES_RC=6;
141 my $T_SOURCES_MISC=7;
142
143 ##
144 # This is a reference to an array containing the list of macro 
145 # definitions
146 my $T_DEFINES=8;
147
148 ##
149 # This is a reference to an array containing the list of directory 
150 # names that constitute the include path
151 my $T_INCLUDE_PATH=9;
152
153 ##
154 # Same as T_INCLUDE_PATH but for the library search path
155 my $T_LIBRARY_PATH=10;
156
157 ##
158 # The list of Windows libraries to import
159 my $T_IMPORTS=11;
160
161 ##
162 # The list of Unix libraries to link with
163 my $T_LIBRARIES=12;
164
165 ##
166 # The list of dependencies between targets
167 my $T_DEPENDS=13;
168
169
170 # The following constants define the recognized types of target
171
172 ##
173 # This is not a real target. This type of target is used to collect 
174 # the sources that don't seem to belong to any other target. Thus no
175 # real target is generated for them, we just put the sources of the 
176 # fake target in the global source list.
177 my $TT_SETTINGS=0;
178
179 ##
180 # For executables in the windows subsystem
181 my $TT_GUIEXE=1;
182
183 ##
184 # For executables in the console subsystem
185 my $TT_CUIEXE=2;
186
187 ##
188 # For dynamically linked libraries
189 my $TT_DLL=3;
190
191
192 # The following constants further refine how the target should be handled
193
194 ##
195 # This target needs a wrapper
196 my $TF_WRAP=1;
197
198 ##
199 # This target is a wrapper
200 my $TF_WRAPPER=2;
201
202 ##
203 # This target is an MFC-based target
204 my $TF_MFC=4;
205
206 ##
207 # Initialize a target:
208 # - set the target type to TT_SETTINGS, i.e. no real target will 
209 #   be generated. 
210 sub target_init
211 {
212   my $target=$_[0];
213
214   @$target[$T_TYPE]=$TT_SETTINGS;
215   # leaving $T_INIT undefined
216   @$target[$T_FLAGS]=$opt_flags;
217   @$target[$T_SOURCES_C]=[];
218   @$target[$T_SOURCES_CXX]=[];
219   @$target[$T_SOURCES_RC]=[];
220   @$target[$T_SOURCES_MISC]=[];
221   @$target[$T_DEFINES]=[];
222   @$target[$T_INCLUDE_PATH]=[];
223   @$target[$T_LIBRARY_PATH]=[];
224   @$target[$T_IMPORTS]=[];
225   @$target[$T_LIBRARIES]=[];
226   @$target[$T_DEPENDS]=[];
227 }
228
229 sub get_default_init
230 {
231   my $type=$_[0];
232   if ($type == $TT_GUIEXE) {
233     return "WinMain";
234   } elsif ($type == $TT_CUIEXE) {
235     return "main";
236   } elsif ($type == $TT_DLL) {
237     return "DllMain";
238   }
239 }
240
241
242
243 #####
244 #
245 # Project modelization
246 #
247 #####
248
249 # First we have the notion of project. A project is described by an 
250 # array (since we don't have structs in perl). The constants below 
251 # identify what is stored at each index of the array.
252
253 ##
254 # This is the path in which this project is located. In other 
255 # words, this is the path to  the Makefile.
256 my $P_PATH=0;
257
258 ##
259 # This index contains a reference to an array containing the project-wide 
260 # settings. The structure of that arrray is actually identical to that of 
261 # a regular target since it can also contain extra sources.
262 my $P_SETTINGS=1;
263
264 ##
265 # This index contains a reference to an array of targets for this 
266 # project. Each target describes how an executable or library is to 
267 # be built. For each target this description takes the same form as 
268 # that of the project: an array. So this entry is an array of arrays.
269 my $P_TARGETS=2;
270
271 ##
272 # Initialize a project:
273 # - set the project's path
274 # - initialize the target list
275 # - create a default target (will be removed later if unnecessary)
276 sub project_init
277 {
278   my $project=$_[0];
279   my $path=$_[1];
280
281   my $project_settings=[];
282   target_init($project_settings);
283
284   @$project[$P_PATH]=$path;
285   @$project[$P_SETTINGS]=$project_settings;
286   @$project[$P_TARGETS]=[];
287 }
288
289
290
291 #####
292 #
293 # Global variables
294 #
295 #####
296
297 my %warnings;
298
299 my %templates;
300
301 ##
302 # Contains the list of all projects. This list tells us what are 
303 # the subprojects of the main Makefile and where we have to generate 
304 # Makefiles.
305 my @projects=();
306
307 ##
308 # This is the main project, i.e. the one in the "." directory. 
309 # It may well be empty in which case the main Makefile will only 
310 # call out subprojects.
311 my @main_project;
312
313 ##
314 # Contains the defaults for the include path, etc.
315 # We store the defaults as if this were a target except that we only 
316 # exploit the defines, include path, library path, library list and misc
317 # sources fields.
318 my @global_settings;
319
320 ##
321 # If one of the projects requires the MFc then we set this global variable 
322 # to true so that configure asks the user to provide a path tothe MFC
323 my $needs_mfc=0;
324
325
326
327 #####
328 #
329 # Utility functions
330 #
331 #####
332
333 ##
334 # Cleans up a name to make it an acceptable Makefile 
335 # variable name.
336 sub canonize
337 {
338   my $name=$_[0];
339
340   $name =~ tr/a-zA-Z0-9_/_/c;
341   return $name;
342 }
343
344 ##
345 # Returns true is the specified pathname is absolute.
346 # Note: pathnames that start with a variable '$' or 
347 # '~' are considered absolute.
348 sub is_absolute
349 {
350   my $path=$_[0];
351
352   return ($path =~ /^[\/~\$]/);
353 }
354
355 ##
356 # Performs a binary search looking for the specified item
357 sub bsearch
358 {
359   my $array=$_[0];
360   my $item=$_[1];
361   my $last=@{$array}-1;
362   my $first=0;
363
364   while ($first<=$last) {
365     my $index=int(($first+$last)/2);
366     my $cmp=@$array[$index] cmp $item;
367     if ($cmp<0) {
368       $first=$index+1;
369     } elsif ($cmp>0) {
370       $last=$index-1;
371     } else {
372       return $index;
373     }
374   }
375 }
376
377
378
379 #####
380 #
381 # 'Source'-based Project analysis
382 #
383 #####
384
385 ##
386 # Allows the user to specify makefile and target specific options
387 # - target: the structure in which to store the results
388 # - options: the string containing the options
389 sub source_set_options
390 {
391   my $target=$_[0];
392   my $options=$_[1];
393
394   #FIXME: we must deal with escaping of stuff and all
395   foreach $option (split / /,$options) {
396     if (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-D/) {
397       push @{@$target[$T_DEFINES]},$option;
398     } elsif (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-I/) {
399       push @{@$target[$T_INCLUDE_PATH]},$option;
400     } elsif ($option =~ /^-L/) {
401       push @{@$target[$T_LIBRARY_PATH]},$option;
402     } elsif ($option =~ /^-i/) {
403       push @{@$target[$T_IMPORTS]},$';
404     } elsif ($option =~ /^-l/) {
405       push @{@$target[$T_LIBRARIES]},$';
406     } elsif (@$target[$T_TYPE] != $TT_DLL and 
407              $option =~ /^--wrap/) {
408       @$target[$T_FLAGS]|=$TF_WRAP;
409     } elsif (@$target[$T_TYPE] != $TT_DLL and 
410              $option =~ /^--nowrap/) {
411       @$target[$T_FLAGS]&=~$TF_WRAP;
412     } elsif ($option =~ /^--mfc/) {
413       @$target[$T_FLAGS]|=$TF_MFC;
414       if (@$target[$T_TYPE] != $TT_DLL) {
415         @$target[$T_FLAGS]|=$TF_WRAP;
416       }
417     } elsif ($option =~ /^--nomfc/) {
418       @$target[$T_FLAGS]&=~$TF_MFC;
419       @$target[$T_FLAGS]&=~($TF_MFC|$TF_WRAP);
420     } else {
421       print STDERR "error: unknown option \"$option\"\n";
422       return 0;
423     }
424   }
425   return 1;
426 }
427
428 ##
429 # Scans the specified directory to:
430 # - see if we should create a Makefile in this directory. We normally do 
431 #   so if we find a project file and sources
432 # - get a list of targets for this directory
433 # - get the list of source files
434 sub source_scan_directory
435 {
436   # a reference to the parent's project
437   my $parent_project=$_[0];
438   # the full relative path to the current directory, including a 
439   # trailing '/', or an empty string if this is the top level directory
440   my $path=$_[1];
441   # the name of this directory, including a trailing '/', or an empty
442   # string if this is the top level directory
443   my $dirname=$_[2];
444   # if set then no targets will be looked for and the sources will all 
445   # end up in the parent_project's 'misc' bucket
446   my $no_target=$_[3];
447
448   # reference to the project for this directory. May not be used
449   my $project;
450   # list of targets found in the 'current' directory
451   my %targets;
452   # list of sources found in the current directory
453   my @sources_c=();
454   my @sources_cxx=();
455   my @sources_rc=();
456   my @sources_misc=();
457   # true if this directory contains a Windows project
458   my $has_win_project=0;
459   # If we don't find any executable/library then we might make up targets 
460   # from the list of .dsp/.mak files we find since they usually have the 
461   # same name as their target.
462   my @dsp_files=();
463   my @mak_files=();
464
465   if (defined $opt_single_target or $dirname eq "") {
466     # Either there is a single target and thus a single project, 
467     # or we are in the top level directory for which a project 
468     # already exists
469     $project=$parent_project;
470   } else {
471     $project=[];
472     project_init($project,$path);
473   }
474   my $project_settings=@$project[$P_SETTINGS];
475
476   # First find out what this directory contains:
477   # collect all sources, targets and subdirectories
478   my $directory=get_directory_contents($path);
479   foreach $dentry (@$directory) {
480     if ($dentry =~ /^\./) {
481       next;
482     }
483     my $fullentry="$path$dentry";
484     if (-d "$fullentry") {
485       if ($dentry =~ /^(Release|Debug)/i) {
486         # These directories are often used to store the object files and the 
487         # resulting executable/library. They should not contain anything else.
488         my @candidates=grep /\.(exe|dll)$/i, @{get_directory_contents("$fullentry")};
489         foreach $candidate (@candidates) {
490           if ($candidate =~ s/\.exe$//i) {
491             $targets{$candidate}=1;
492           } elsif ($candidate =~ s/^(.*)\.dll$/lib$1.so/i) {
493             $targets{$candidate}=1;
494           }
495         }
496       } elsif ($dentry =~ /^include/i) {
497         # This directory must contain headers we're going to need
498         push @{@$project_settings[$T_INCLUDE_PATH]},"-I$dentry";
499         source_scan_directory($project,"$fullentry/","$dentry/",1);
500       } else {
501         # Recursively scan this directory. Any source file that cannot be 
502         # attributed to a project in one of the subdirectories will be 
503         # attributed to this project.
504         source_scan_directory($project,"$fullentry/","$dentry/",$no_target);
505       }
506     } elsif (-f "$fullentry") {
507       if ($dentry =~ s/\.exe$//i) {
508         $targets{$dentry}=1;
509       } elsif ($dentry =~ s/^(.*)\.dll$/lib$1.so/i) {
510         $targets{$dentry}=1;
511       } elsif ($dentry =~ /\.c$/i and $dentry !~ /\.spec\.c$/) {
512         push @sources_c,"$dentry";
513       } elsif ($dentry =~ /\.(cpp|cxx)$/i) {
514         if ($dentry =~ /^stdafx.cpp$/i) {
515           push @sources_misc,"$dentry";
516           @$project_settings[$T_FLAGS]|=$TF_MFC;
517         } else {
518           push @sources_cxx,"$dentry";
519         }
520       } elsif ($dentry =~ /\.rc$/i) {
521         push @sources_rc,"$dentry";
522       } elsif ($dentry =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
523         push @sources_misc,"$dentry";
524         if ($dentry =~ /^stdafx.h$/i) {
525           @$project_settings[$T_FLAGS]|=$TF_MFC;
526         }
527       } elsif ($dentry =~ /\.dsp$/i) {
528         push @dsp_files,"$dentry";
529         $has_win_project=1;
530       } elsif ($dentry =~ /\.mak$/i) {
531         push @mak_files,"$dentry";
532         $has_win_project=1;
533       } elsif ($dentry =~ /^makefile/i) {
534         $has_win_project=1;
535       }
536     }
537   }
538   closedir(DIRECTORY);
539
540   # If we have a single target then all we have to do is assign 
541   # all the sources to it and we're done
542   # FIXME: does this play well with the --interactive mode?
543   if ($opt_single_target) {
544     my $target=@{@$project[$P_TARGETS]}[0];
545     push @{@$target[$T_SOURCES_C]},map "$path$_",@sources_c;
546     push @{@$target[$T_SOURCES_CXX]},map "$path$_",@sources_cxx;
547     push @{@$target[$T_SOURCES_RC]},map "$path$_",@sources_rc;
548     push @{@$target[$T_SOURCES_MISC]},map "$path$_",@sources_misc;
549     return;
550   }
551   if ($no_target) {
552     my $parent_settings=@$parent_project[$P_SETTINGS];
553     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_c;
554     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_cxx;
555     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_rc;
556     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
557     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
558     return;
559   }
560
561   my $source_count=@sources_c+@sources_cxx+@sources_rc+
562                    @{@$project_settings[$T_SOURCES_C]}+
563                    @{@$project_settings[$T_SOURCES_CXX]}+
564                    @{@$project_settings[$T_SOURCES_RC]};
565   if ($source_count == 0) {
566     # A project without real sources is not a project, get out!
567     if ($project!=$parent_project) {
568       my $parent_settings=@$parent_project[$P_SETTINGS];
569       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
570       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
571     }
572     return;
573   }
574   #print "targets=",%targets,"\n";
575   #print "target_count=$target_count\n";
576   #print "has_win_project=$has_win_project\n";
577   #print "dirname=$dirname\n";
578
579   my $target_count;
580   if (($has_win_project != 0) or ($dirname eq "")) {
581     # Deal with cases where we could not find any executable/library, and 
582     # thus have no target, although we did find some sort of windows project.
583     $target_count=keys %targets;
584     if ($target_count == 0) {
585       # Try to come up with a target list based on .dsp/.mak files
586       my $prj_list;
587       if (@dsp_files > 0) {
588         $prj_list=\@dsp_files;
589       } else {
590         $prj_list=\@mak_files;
591       }
592       foreach $filename (@$prj_list) {
593         $filename =~ s/\.(dsp|mak)$//i;
594         if ($opt_target_type == $TT_DLL) {
595           $filename = "lib$filename.so";
596         }
597         $targets{$filename}=1;
598       }
599       $target_count=keys %targets;
600       if ($target_count == 0) {
601         # Still nothing, try the name of the directory
602         my $name;
603         if ($dirname eq "") {
604           # Bad luck, this is the top level directory!
605           $name=(split /\//, cwd)[-1];
606         } else {
607           $name=$dirname;
608           # Remove the trailing '/'. Also eliminate whatever is after the last 
609           # '.' as it is likely to be meaningless (.orig, .new, ...)
610           $name =~ s+(/|\.[^.]*)$++;
611           if ($name eq "src") {
612             # 'src' is probably a subdirectory of the real project directory.
613             # Try again with the parent (if any).
614             my $parent=$path;
615             if ($parent =~ s+([^/]*)/[^/]*/$+$1+) {
616               $name=$parent;
617             } else {
618               $name=(split /\//, cwd)[-1];
619             }
620           }
621         }
622         $name =~ s+(/|\.[^.]*)$++;
623         if ($opt_target_type == $TT_DLL) {
624           $name = "lib$name.so";
625         }
626         $targets{$name}=1;
627       }
628     }
629
630     # Ask confirmation to the user if he wishes so
631     if ($opt_is_interactive == $OPT_ASK_YES) {
632       my $target_list=join " ",keys %targets;
633       print "\n*** In ",($path?$path:"./"),"\n";
634       print "* winemaker found the following list of (potential) targets\n";
635       print "*   $target_list\n";
636       print "* Type enter to use it as is, your own comma-separated list of\n";
637       print "* targets, 'none' to assign the source files to a parent directory,\n";
638       print "* or 'ignore' to ignore everything in this directory tree.\n";
639       print "* Target list:\n";
640       $target_list=<STDIN>;
641       chomp $target_list;
642       if ($target_list eq "") {
643         # Keep the target list as is, i.e. do nothing
644       } elsif ($target_list eq "none") {
645         # Empty the target list
646         undef %targets;
647       } elsif ($target_list eq "ignore") {
648         # Ignore this subtree altogether
649         return;
650       } else {
651         undef %targets;
652         foreach $target (split /,/,$target_list) {
653           $target =~ s+^\s*++;
654           $target =~ s+\s*$++;
655           # Also accept .exe and .dll as a courtesy
656           $target =~ s+(.*)\.dll$+lib$1.so+;
657           $target =~ s+\.exe$++;
658           $targets{$target}=1;
659         }
660       }
661     }
662   }
663
664   # If we have no project at this level, then transfer all 
665   # the sources to the parent project
666   $target_count=keys %targets;
667   if ($target_count == 0) {
668     if ($project!=$parent_project) {
669       my $parent_settings=@$parent_project[$P_SETTINGS];
670       push @{@$parent_settings[$T_SOURCES_C]},map "$dirname$_",@sources_c;
671       push @{@$parent_settings[$T_SOURCES_CXX]},map "$dirname$_",@sources_cxx;
672       push @{@$parent_settings[$T_SOURCES_RC]},map "$dirname$_",@sources_rc;
673       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
674       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
675     }
676     return;
677   }
678
679   # Otherwise add this project to the project list, except for 
680   # the main project which is already in the list.
681   if ($dirname ne "") {
682     push @projects,$project;
683   }
684
685   # Ask for project-wide options
686   if ($opt_ask_project_options == $OPT_ASK_YES) {
687     my $flag_desc="";
688     if ((@$project_settings[$T_FLAGS] & $TF_MFC)!=0) {
689       $flag_desc="mfc";
690     }
691     if ((@$project_settings[$T_FLAGS] & $TF_WRAP)!=0) {
692       if ($flag_desc ne "") {
693         $flag_desc.=", ";
694       }
695       $flag_desc.="wrapped";
696     }
697     print "* Type any project-wide options (-D/-I/-L/-i/-l/--mfc/--wrap),\n";
698     if (defined $flag_desc) {
699       print "* (currently $flag_desc)\n";
700     }
701     print "* or 'skip' to skip the target specific options,\n";
702     print "* or 'never' to not be asked this question again:\n";
703     while (1) {
704       my $options=<STDIN>;
705       chomp $options;
706       if ($options eq "skip") {
707         $opt_ask_target_options=$OPT_ASK_SKIP;
708         last;
709       } elsif ($options eq "never") {
710         $opt_ask_project_options=$OPT_ASK_NO;
711         last;
712       } elsif (source_set_options($project_settings,$options)) {
713         last;
714       }
715       print "Please re-enter the options:\n";
716     }
717   }
718
719   # - Create the targets
720   # - Check if we have both libraries and programs
721   # - Match each target with source files (sort in reverse 
722   #   alphabetical order to get the longest matches first)
723   my @local_imports=();
724   my @local_depends=();
725   my @exe_list=();
726   foreach $target_name (sort { $b cmp $a } keys %targets) {
727     # Create the target...
728     my $basename;
729     my $target=[];
730     target_init($target);
731     @$target[$T_NAME]=$target_name;
732     @$target[$T_FLAGS]|=@$project_settings[$T_FLAGS];
733     if ($target_name =~ /^lib(.*)\.so$/) {
734       @$target[$T_TYPE]=$TT_DLL;
735       @$target[$T_INIT]=get_default_init($TT_DLL);
736       @$target[$T_FLAGS]&=~$TF_WRAP;
737       $basename=$1;
738       push @local_depends,$target_name;
739       push @local_imports,$basename;
740     } else {
741       @$target[$T_TYPE]=$opt_target_type;
742       @$target[$T_INIT]=get_default_init($opt_target_type);
743       $basename=$target_name;
744       push @exe_list,$target;
745     }
746     # This is the default link list of Visual Studio, except odbccp32 
747     # which we don't have in Wine. Also I add ntdll which seems 
748     # necessary for Winelib.
749     my @std_imports=qw(advapi32.dll comdlg32.dll gdi32.dll kernel32.dll ntdll.dll odbc32.dll ole32.dll oleaut32.dll shell32.dll user32.dll winspool.drv);
750     @$target[$T_IMPORTS]=\@std_imports;
751     push @{@$project[$P_TARGETS]},$target;
752
753     # Ask for target-specific options
754     if ($opt_ask_target_options == $OPT_ASK_YES) {
755       my $flag_desc="";
756       if ((@$target[$T_FLAGS] & $TF_MFC)!=0) {
757         $flag_desc=" (mfc";
758       }
759       if ((@$target[$T_FLAGS] & $TF_WRAP)!=0) {
760         if ($flag_desc ne "") {
761           $flag_desc.=", ";
762         } else {
763           $flag_desc=" (";
764         }
765         $flag_desc.="wrapped";
766       }
767       if ($flag_desc ne "") {
768         $flag_desc.=")";
769       }
770       print "* Specify any link option (-L/-i/-l/--mfc/--wrap) specific to the target\n";
771       print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
772       while (1) {
773         my $options=<STDIN>;
774         chomp $options;
775         if ($options eq "never") {
776           $opt_ask_target_options=$OPT_ASK_NO;
777           last;
778         } elsif (source_set_options($target,$options)) {
779           last;
780         }
781         print "Please re-enter the options:\n";
782       }
783     }
784     if (@$target[$T_FLAGS] & $TF_MFC) {
785       @$project_settings[$T_FLAGS]|=$TF_MFC;
786       push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
787       push @{@$target[$T_IMPORTS]},"mfc.dll";
788       # FIXME: Link with the MFC in the Unix sense, until we 
789       # start exporting the functions properly.
790       push @{@$target[$T_LIBRARIES]},"mfc";
791     }
792
793     # Match sources...
794     if ($target_count == 1) {
795       push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
796       @$project_settings[$T_SOURCES_C]=[];
797       @sources_c=();
798
799       push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
800       @$project_settings[$T_SOURCES_CXX]=[];
801       @sources_cxx=();
802
803       push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
804       @$project_settings[$T_SOURCES_RC]=[];
805       @sources_rc=();
806
807       push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
808       # No need for sorting these sources
809       @$project_settings[$T_SOURCES_MISC]=[];
810       @sources_misc=();
811     } else {
812       foreach $source (@sources_c) {
813         if ($source =~ /^$basename/i) {
814           push @{@$target[$T_SOURCES_C]},$source;
815           $source="";
816         }
817       }
818       foreach $source (@sources_cxx) {
819         if ($source =~ /^$basename/i) {
820           push @{@$target[$T_SOURCES_CXX]},$source;
821           $source="";
822         }
823       }
824       foreach $source (@sources_rc) {
825         if ($source =~ /^$basename/i) {
826           push @{@$target[$T_SOURCES_RC]},$source;
827           $source="";
828         }
829       }
830       foreach $source (@sources_misc) {
831         if ($source =~ /^$basename/i) {
832           push @{@$target[$T_SOURCES_MISC]},$source;
833           $source="";
834         }
835       }
836     }
837     @$target[$T_SOURCES_C]=[sort @{@$target[$T_SOURCES_C]}];
838     @$target[$T_SOURCES_CXX]=[sort @{@$target[$T_SOURCES_CXX]}];
839     @$target[$T_SOURCES_RC]=[sort @{@$target[$T_SOURCES_RC]}];
840     @$target[$T_SOURCES_MISC]=[sort @{@$target[$T_SOURCES_MISC]}];
841   }
842   if ($opt_ask_target_options == $OPT_ASK_SKIP) {
843     $opt_ask_target_options=$OPT_ASK_YES;
844   }
845
846   if (@$project_settings[$T_FLAGS] & $TF_MFC) {
847     push @{@$project_settings[$T_INCLUDE_PATH]},"\$(MFC_INCLUDE_PATH)";
848   }
849   # The sources that did not match, if any, go to the extra 
850   # source list of the project settings
851   foreach $source (@sources_c) {
852     if ($source ne "") {
853       push @{@$project_settings[$T_SOURCES_C]},$source;
854     }
855   }
856   @$project_settings[$T_SOURCES_C]=[sort @{@$project_settings[$T_SOURCES_C]}];
857   foreach $source (@sources_cxx) {
858     if ($source ne "") {
859       push @{@$project_settings[$T_SOURCES_CXX]},$source;
860     }
861   }
862   @$project_settings[$T_SOURCES_CXX]=[sort @{@$project_settings[$T_SOURCES_CXX]}];
863   foreach $source (@sources_rc) {
864     if ($source ne "") {
865       push @{@$project_settings[$T_SOURCES_RC]},$source;
866     }
867   }
868   @$project_settings[$T_SOURCES_RC]=[sort @{@$project_settings[$T_SOURCES_RC]}];
869   foreach $source (@sources_misc) {
870     if ($source ne "") {
871       push @{@$project_settings[$T_SOURCES_MISC]},$source;
872     }
873   }
874   @$project_settings[$T_SOURCES_MISC]=[sort @{@$project_settings[$T_SOURCES_MISC]}];
875
876   # Finally if we are building both libraries and programs in 
877   # this directory, then the programs should be linked with all 
878   # the libraries
879   if (@local_imports > 0 and @exe_list > 0) {
880     foreach $target (@exe_list) {
881       push @{@$target[$T_LIBRARY_PATH]},"-L.";
882       push @{@$target[$T_IMPORTS]},map { "$_.dll" } @local_imports;
883       # Also link in the Unix sense since none of the functions 
884       # will be exported.
885       push @{@$target[$T_LIBRARIES]},@local_imports;
886       push @{@$target[$T_DEPENDS]},@local_depends;
887     }
888   }
889 }
890
891 ##
892 # Scan the source directories in search of things to build
893 sub source_scan
894 {
895   # If there's a single target then this is going to be the default target
896   if (defined $opt_single_target) {
897     # Create the main target
898     my $main_target=[];
899     target_init($main_target);
900     if ($opt_target_type == $TT_DLL) {
901       @$main_target[$T_NAME]="lib$opt_single_target.so";
902     } else {
903       @$main_target[$T_NAME]="$opt_single_target";
904     }
905     @$main_target[$T_TYPE]=$opt_target_type;
906
907     # Add it to the list
908     push @{$main_project[$P_TARGETS]},$main_target;
909   }
910
911   # The main directory is always going to be there
912   push @projects,\@main_project;
913
914   # Now scan the directory tree looking for source files and, maybe, targets
915   print "Scanning the source directories...\n";
916   source_scan_directory(\@main_project,"","",0);
917
918   @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
919 }
920
921
922
923 #####
924 #
925 # 'vc.dsp'-based Project analysis
926 #
927 #####
928
929 #sub analyze_vc_dsp
930 #{
931 #  
932 #}
933
934
935
936 #####
937 #
938 # Creating the wrapper targets
939 #
940 #####
941
942 sub postprocess_targets
943 {
944   foreach $project (@projects) {
945     foreach $target (@{@$project[$P_TARGETS]}) {
946       if ((@$target[$T_FLAGS] & $TF_WRAP) != 0) {
947         my $wrapper=[];
948         target_init($wrapper);
949         @$wrapper[$T_NAME]=@$target[$T_NAME];
950         @$wrapper[$T_TYPE]=@$target[$T_TYPE];
951         @$wrapper[$T_INIT]=get_default_init(@$target[$T_TYPE]);
952         @$wrapper[$T_FLAGS]=$TF_WRAPPER | (@$target[$T_FLAGS] & $TF_MFC);
953         @$wrapper[$T_IMPORTS]=[ "kernel32.dll", "ntdll.dll", "user32.dll" ];
954         push @{@$wrapper[$T_SOURCES_C]},"@$wrapper[$T_NAME]_wrapper.c";
955
956         my $index=bsearch(@$target[$T_SOURCES_C],"@$wrapper[$T_NAME]_wrapper.c");
957         if (defined $index) {
958           splice(@{@$target[$T_SOURCES_C]},$index,1);
959         }
960         @$target[$T_NAME]="lib@$target[$T_NAME].so";
961         @$target[$T_TYPE]=$TT_DLL;
962
963         push @{@$project[$P_TARGETS]},$wrapper;
964       }
965       if ((@$target[$T_FLAGS] & $TF_MFC) != 0) {
966         @{@$project[$P_SETTINGS]}[$T_FLAGS]|=$TF_MFC;
967         $needs_mfc=1;
968       }
969     }
970   }
971 }
972
973
974
975 #####
976 #
977 # Source search
978 #
979 #####
980
981 ##
982 # Performs a directory traversal and renames the files so that:
983 # - they have the case desired by the user
984 # - their extension is of the appropriate case
985 # - they don't contain annoying characters like ' ', '$', '#', ...
986 sub fix_file_and_directory_names
987 {
988   my $dirname=$_[0];
989
990   if (opendir(DIRECTORY, "$dirname")) {
991     foreach $dentry (readdir DIRECTORY) {
992       if ($dentry =~ /^\./ or $dentry eq "CVS") {
993         next;
994       }
995       # Set $warn to 1 if the user should be warned of the renaming
996       my $warn=0;
997
998       # autoconf and make don't support these characters well
999       my $new_name=$dentry;
1000       $new_name =~ s/[ \$]/_/g;
1001
1002       # Only all lowercase extensions are supported (because of the 
1003       # transformations ':.c=.o') .
1004       if (-f "$dirname/$new_name") {
1005         if ($new_name =~ /\.C$/) {
1006           $new_name =~ s/\.C$/.c/;
1007         }
1008         if ($new_name =~ /\.cpp$/i) {
1009           $new_name =~ s/\.cpp$/.cpp/i;
1010         }
1011         if ($new_name =~ s/\.cxx$/.cpp/i) {
1012           $warn=1;
1013         }
1014         if ($new_name =~ /\.rc$/i) {
1015           $new_name =~ s/\.rc$/.rc/i;
1016         }
1017         # And this last one is to avoid confusion then running make
1018         if ($new_name =~ s/^makefile$/makefile.win/) {
1019           $warn=1;
1020         }
1021       }
1022
1023       # Adjust the case to the user's preferences
1024       if (($opt_lower == $OPT_LOWER_ALL and $dentry =~ /[A-Z]/) or 
1025           ($opt_lower == $OPT_LOWER_UPPERCASE and $dentry !~ /[a-z]/)
1026          ) {
1027         $new_name=lc $new_name;
1028       }
1029
1030       # And finally, perform the renaming
1031       if ($new_name ne $dentry) {
1032         if ($warn) {
1033           print STDERR "warning: in \"$dirname\", renaming \"$dentry\" to \"$new_name\"\n";
1034         }
1035         if (!rename("$dirname/$dentry","$dirname/$new_name")) {
1036           print STDERR "error: in \"$dirname\", unable to rename \"$dentry\" to \"$new_name\"\n";
1037           print STDERR "       $!\n";
1038           $new_name=$dentry;
1039         }
1040       }
1041       if (-d "$dirname/$new_name") {
1042         fix_file_and_directory_names("$dirname/$new_name");
1043       }
1044     }
1045     closedir(DIRECTORY);
1046   }
1047 }
1048
1049
1050
1051 #####
1052 #
1053 # Source fixup
1054 #
1055 #####
1056
1057 ##
1058 # This maps a directory name to a reference to an array listing 
1059 # its contents (files and directories)
1060 my %directories;
1061
1062 ##
1063 # Retrieves the contents of the specified directory.
1064 # We either get it from the directories hashtable which acts as a 
1065 # cache, or use opendir, readdir, closedir and store the result 
1066 # in the hashtable.
1067 sub get_directory_contents
1068 {
1069   my $dirname=$_[0];
1070   my $directory;
1071
1072   #print "getting the contents of $dirname\n";
1073
1074   # check for a cached version
1075   $dirname =~ s+/$++;
1076   if ($dirname eq "") {
1077     $dirname=cwd;
1078   }
1079   $directory=$directories{$dirname};
1080   if (defined $directory) {
1081     #print "->@$directory\n";
1082     return $directory;
1083   }
1084   
1085   # Read this directory
1086   if (opendir(DIRECTORY, "$dirname")) {
1087     my @files=readdir DIRECTORY;
1088     closedir(DIRECTORY);
1089     $directory=\@files;
1090   } else {
1091     # Return an empty list
1092     #print "error: cannot open $dirname\n";
1093     my @files;
1094     $directory=\@files;
1095   }
1096   #print "->@$directory\n";
1097   $directories{$dirname}=$directory;
1098   return $directory;
1099 }
1100
1101 ##
1102 # Try to find a file for the specified filename. The attempt is 
1103 # case-insensitive which is why it's not trivial. If a match is 
1104 # found then we return the pathname with the correct case.
1105 sub search_from
1106 {
1107   my $dirname=$_[0];
1108   my $path=$_[1];
1109   my $real_path="";
1110
1111   if ($dirname eq "" or $dirname eq ".") {
1112     $dirname=cwd;
1113   } elsif ($dirname =~ m+^[^/]+) {
1114     $dirname=cwd . "/" . $dirname;
1115   }
1116   if ($dirname !~ m+/$+) {
1117     $dirname.="/";
1118   }
1119
1120   foreach $component (@$path) {
1121     #print "    looking for $component in \"$dirname\"\n";
1122     if ($component eq ".") {
1123       # Pass it as is
1124       $real_path.="./";
1125     } elsif ($component eq "..") {
1126       # Go up one level
1127       $dirname=dirname($dirname) . "/";
1128       $real_path.="../";
1129     } else {
1130       # The file/directory may have been renamed before. Also try to 
1131       # match the renamed file.
1132       my $renamed=$component;
1133       $renamed =~ s/[ \$]/_/g;
1134       if ($renamed eq $component) {
1135         undef $renamed;
1136       }
1137
1138       my $directory=get_directory_contents $dirname;
1139       my $found;
1140       foreach $dentry (@$directory) {
1141         if ($dentry =~ /^$component$/i or
1142             (defined $renamed and $dentry =~ /^$renamed$/i)
1143            ) {
1144           $dirname.="$dentry/";
1145           $real_path.="$dentry/";
1146           $found=1;
1147           last;
1148         }
1149       }
1150       if (!defined $found) {
1151         # Give up
1152         #print "    could not find $component in $dirname\n";
1153         return;
1154       }
1155     }
1156   }
1157   $real_path=~ s+/$++;
1158   #print "    -> found $real_path\n";
1159   return $real_path;
1160 }
1161
1162 ##
1163 # Performs a case-insensitive search for the specified file in the 
1164 # include path.
1165 # $line is the line number that should be referenced when an error occurs
1166 # $filename is the file we are looking for
1167 # $dirname is the directory of the file containing the '#include' directive
1168 #    if '"' was used, it is an empty string otherwise
1169 # $project and $target specify part of the include path
1170 sub get_real_include_name
1171 {
1172   my $line=$_[0];
1173   my $filename=$_[1];
1174   my $dirname=$_[2];
1175   my $project=$_[3];
1176   my $target=$_[4];
1177
1178   if ($filename =~ /^([a-zA-Z]:)?[\/]/ or $filename =~ /^[a-zA-Z]:[\/]?/) {
1179     # This is not a relative path, we cannot make any check
1180     my $warning="path:$filename";
1181     if (!defined $warnings{$warning}) {
1182       $warnings{$warning}="1";
1183       print STDERR "warning: cannot check the case of absolute pathnames:\n";
1184       print STDERR "$line:   $filename\n";
1185     }
1186   } else {
1187     # Here's how we proceed:
1188     # - split the filename we look for into its components
1189     # - then for each directory in the include path
1190     #   - trace the directory components starting from that directory
1191     #   - if we fail to find a match at any point then continue with 
1192     #     the next directory in the include path
1193     #   - otherwise, rejoice, our quest is over.
1194     my @file_components=split /[\/\\]+/, $filename;
1195     #print "  Searching for $filename from @$project[$P_PATH]\n";
1196
1197     my $real_filename;
1198     if ($dirname ne "") {
1199       # This is an 'include ""' -> look in dirname first.
1200       #print "    in $dirname (include \"\")\n";
1201       $real_filename=search_from($dirname,\@file_components);
1202       if (defined $real_filename) {
1203         return $real_filename;
1204       }
1205     }
1206     my $project_settings=@$project[$P_SETTINGS];
1207     foreach $include (@{@$target[$T_INCLUDE_PATH]}, @{@$project_settings[$T_INCLUDE_PATH]}) {
1208       my $dirname=$include;
1209       $dirname=~ s+^-I++;
1210       if (!is_absolute($dirname)) {
1211         $dirname="@$project[$P_PATH]$dirname";
1212       } else {
1213         $dirname=~ s+^\$\(TOPSRCDIR\)/++;
1214         $dirname=~ s+^\$\(SRCDIR\)/+@$project[$P_PATH]+;
1215       }
1216       #print "    in $dirname\n";
1217       $real_filename=search_from("$dirname",\@file_components);
1218       if (defined $real_filename) {
1219         return $real_filename;
1220       }
1221     }
1222     my $dotdotpath=@$project[$P_PATH];
1223     $dotdotpath =~ s/[^\/]+/../g;
1224     foreach $include (@{$global_settings[$T_INCLUDE_PATH]}) {
1225       my $dirname=$include;
1226       $dirname=~ s+^-I++;
1227       $dirname=~ s+^\$\(TOPSRCDIR\)\/++;
1228       $dirname=~ s+^\$\(SRCDIR\)\/+@$project[$P_PATH]+;
1229       #print "    in $dirname  (global setting)\n";
1230       $real_filename=search_from("$dirname",\@file_components);
1231       if (defined $real_filename) {
1232         return $real_filename;
1233       }
1234     }
1235   }
1236   $filename =~ s+\\\\+/+g; # in include ""
1237   $filename =~ s+\\+/+g; # in include <> !
1238   if ($opt_lower_include) {
1239     return lc "$filename";
1240   }
1241   return $filename;
1242 }
1243
1244 sub print_pack
1245 {
1246   my $indent=$_[0];
1247   my $size=$_[1];
1248   my $trailer=$_[2];
1249
1250   if ($size =~ /^(1|2|4|8)$/) {
1251     print FILEO "$indent#include <pshpack$size.h>$trailer";
1252   } else {
1253     print FILEO "$indent/* winemaker:warning: Unknown size \"$size\". Defaulting to 4 */\n";
1254     print FILEO "$indent#include <pshpack4.h>$trailer";
1255   }
1256 }
1257
1258 ##
1259 # 'Parses' a source file and fixes constructs that would not work with 
1260 # Winelib. The parsing is rather simple and not all non-portable features 
1261 # are corrected. The most important feature that is corrected is the case 
1262 # and path separator of '#include' directives. This requires that each 
1263 # source file be associated to a project & target so that the proper 
1264 # include path is used.
1265 # Also note that the include path is relative to the directory in which the 
1266 # compiler is run, i.e. that of the project, not to that of the file.
1267 sub fix_file
1268 {
1269   my $filename=$_[0];
1270   my $project=$_[1];
1271   my $target=$_[2];
1272   $filename="@$project[$P_PATH]$filename";
1273   if (! -e $filename) {
1274     return;
1275   }
1276
1277   my $is_rc=($filename =~ /\.(rc2?|dlg)$/i);
1278   my $dirname=dirname($filename);
1279   my $is_mfc=0;
1280   if (defined $target and (@$target[$T_FLAGS] & $TF_MFC)) {
1281     $is_mfc=1;
1282   }
1283
1284   print "  $filename\n";
1285   #FIXME:assuming that because there is a .bak file, this is what we want is 
1286   #probably flawed. Or is it???
1287   if (! -e "$filename.bak") {
1288     if (!copy("$filename","$filename.bak")) {
1289       print STDERR "error: unable to make a backup of $filename:\n";
1290       print STDERR "       $!\n";
1291       return;
1292     }
1293   }
1294   if (!open(FILEI,"$filename.bak")) {
1295     print STDERR "error: unable to open $filename.bak for reading:\n";
1296     print STDERR "       $!\n";
1297     return;
1298   }
1299   if (!open(FILEO,">$filename")) {
1300     print STDERR "error: unable to open $filename for writing:\n";
1301     print STDERR "       $!\n";
1302     return;
1303   }
1304   my $line=0;
1305   my $modified=0;
1306   my $rc_block_depth=0;
1307   my $rc_textinclude_state=0;
1308   my @pack_stack;
1309   while (<FILEI>) {
1310     $line++;
1311     s/\r\n$/\n/;
1312     if (!/\n$/) {
1313       # Make sure all files are '\n' terminated
1314       $_ .= "\n";
1315     }
1316     if ($is_rc and !$is_mfc and /^(\s*)(\#\s*include\s*)\"afxres\.h\"/) {
1317       # VC6 automatically includes 'afxres.h', an MFC specific header, in 
1318       # the RC files it generates (even in non-MFC projects). So we replace 
1319       # it with 'winres.h' its very close standard cousin so that non MFC 
1320       # projects can compile in Wine without the MFC sources.
1321       my $warning="mfc:afxres.h";
1322       if (!defined $warnings{$warning}) {
1323         $warnings{$warning}="1";
1324         print STDERR "warning: In non-MFC projects, winemaker replaces the MFC specific header 'afxres.h' with 'winres.h'\n";
1325         print STDERR "warning: the above warning is issued only once\n";
1326       }
1327       print FILEO "$1/* winemaker: $2\"afxres.h\" */\n";
1328       print FILEO "$1/* winemaker:warning: 'afxres.h' is an MFC specific header. Replacing it with 'winres.h' */\n";
1329       print FILEO "$1$2\"winres.h\"$'";
1330       $modified=1;
1331
1332     } elsif (/^(\s*\#\s*include\s*)([\"<])([^\"]+)([\">])/) {
1333       my $from_file=($2 eq "<"?"":$dirname);
1334       my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1335       print FILEO "$1$2$real_include_name$4$'";
1336       $modified|=($real_include_name ne $3);
1337
1338     } elsif (s/^(\s*)(\#\s*pragma\s+pack\s*\(\s*)//) {
1339       # Pragma pack handling
1340       #
1341       # pack_stack is an array of references describing the stack of 
1342       # pack directives currently in effect. Each directive if described 
1343       # by a reference to an array containing:
1344       # - "push" for pack(push,...) directives, "" otherwise
1345       # - the directive's identifier at index 1
1346       # - the directive's alignement value at index 2
1347       #
1348       # Don't believe a word of what the documentation says: it's all wrong.
1349       # The code below is based on the actual behavior of Visual C/C++ 6.
1350       my $pack_indent=$1;
1351       my $pack_header=$2;
1352       if (/^(\))/) {
1353         # pragma pack()
1354         # Pushes the default stack alignment
1355         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1356         print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1357         print_pack($pack_indent,4,$');
1358         push @pack_stack, [ "", "", 4 ];
1359
1360       } elsif (/^(pop\s*(,\s*\d+\s*)?\))/) {
1361         # pragma pack(pop)
1362         # pragma pack(pop,n)
1363         # Goes up the stack until it finds a pack(push,...), and pops it
1364         # Ignores any pack(n) entry
1365         # Issues a warning if the pack is of the form pack(push,label)
1366         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1367         my $pack_comment=$';
1368         $pack_comment =~ s/^\s*//;
1369         if ($pack_comment ne "") {
1370           print FILEO "$pack_indent$pack_comment";
1371         }
1372         while (1) {
1373           my $alignment=pop @pack_stack;
1374           if (!defined $alignment) {
1375             print FILEO "$pack_indent/* winemaker:warning: No pack(push,...) found. All the stack has been popped */\n";
1376             last;
1377           }
1378           if (@$alignment[1]) {
1379             print FILEO "$pack_indent/* winemaker:warning: Anonymous pop of pack(push,@$alignment[1]) (@$alignment[2]) */\n";
1380           }
1381           print FILEO "$pack_indent#include <poppack.h>\n";
1382           if (@$alignment[0]) {
1383             last;
1384           }
1385         }
1386
1387       } elsif (/^(pop\s*,\s*(\w+)\s*(,\s*\d+\s*)?\))/) {
1388         # pragma pack(pop,label[,n])
1389         # Goes up the stack until finding a pack(push,...) and pops it.
1390         # 'n', if specified, is ignored.
1391         # Ignores any pack(n) entry
1392         # Issues a warning if the label of the pack does not match,
1393         # or if it is in fact a pack(push,n)
1394         my $label=$2;
1395         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1396         my $pack_comment=$';
1397         $pack_comment =~ s/^\s*//;
1398         if ($pack_comment ne "") {
1399           print FILEO "$pack_indent$pack_comment";
1400         }
1401         while (1) {
1402           my $alignment=pop @pack_stack;
1403           if (!defined $alignment) {
1404             print FILEO "$pack_indent/* winemaker:warning: No pack(push,$label) found. All the stack has been popped */\n";
1405             last;
1406           }
1407           if (@$alignment[1] and @$alignment[1] ne $label) {
1408             print FILEO "$pack_indent/* winemaker:warning: Push/pop mismatch: \"@$alignment[1]\" (@$alignment[2]) != \"$label\" */\n";
1409           }
1410           print FILEO "$pack_indent#include <poppack.h>\n";
1411           if (@$alignment[0]) {
1412             last;
1413           }
1414         }
1415
1416       } elsif (/^(push\s*\))/) {
1417         # pragma pack(push)
1418         # Push the current alignment
1419         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1420         if (@pack_stack > 0) {
1421           my $alignment=$pack_stack[$#pack_stack];
1422           print_pack($pack_indent,@$alignment[2],$');
1423           push @pack_stack, [ "push", "", @$alignment[2] ];
1424         } else {
1425           print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1426           print_pack($pack_indent,4,$');
1427           push @pack_stack, [ "push", "", 4 ];
1428         }
1429
1430       } elsif (/^((push\s*,\s*)?(\d+)\s*\))/) {
1431         # pragma pack([push,]n)
1432         # Push new alignment n
1433         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1434         print_pack($pack_indent,$3,"$'");
1435         push @pack_stack, [ ($2 ? "push" : ""), "", $3 ];
1436
1437       } elsif (/^((\w+)\s*\))/) {
1438         # pragma pack(label)
1439         # label must in fact be a macro that resolves to an integer
1440         # Then behaves like 'pragma pack(n)'
1441         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1442         print FILEO "$pack_indent/* winemaker:warning: Assuming $2 == 4 */\n";
1443         print_pack($pack_indent,4,$');
1444         push @pack_stack, [ "", "", 4 ];
1445
1446       } elsif (/^(push\s*,\s*(\w+)\s*(,\s*(\d+)\s*)?\))/) {
1447         # pragma pack(push,label[,n])
1448         # Pushes a new label on the stack. It is possible to push the same
1449         # label multiple times. If 'n' is omitted then the alignment is 
1450         # unchanged. Otherwise it becomes 'n'.
1451         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1452         my $size;
1453         if (defined $4) {
1454           $size=$4;
1455         } elsif (@pack_stack > 0) {
1456           my $alignment=$pack_stack[$#pack_stack];
1457           $size=@$alignment[2];
1458         } else {
1459           print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1460           $size=4;
1461         }
1462         print_pack($pack_indent,$size,$');
1463         push @pack_stack, [ "push", $2, $size ];
1464
1465       } else {
1466         # pragma pack(???               -> What's that?
1467         print FILEO "$pack_indent/* winemaker:warning: Unknown type of pragma pack directive */\n";
1468         print FILEO "$pack_indent$pack_header$_";
1469
1470       }
1471       $modified=1;
1472
1473     } elsif ($is_rc) {
1474       if ($rc_block_depth == 0 and /^(\w+\s+(BITMAP|CURSOR|FONT|FONTDIR|ICON|MESSAGETABLE|TEXT|RTF)\s+((DISCARDABLE|FIXED|IMPURE|LOADONCALL|MOVEABLE|PRELOAD|PURE)\s+)*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
1475         my $from_file=($5 eq "<"?"":$dirname);
1476         my $real_include_name=get_real_include_name($line,$6,$from_file,$project,$target);
1477         print FILEO "$1$5$real_include_name$7$'";
1478         $modified|=($real_include_name ne $6);
1479
1480       } elsif (/^(\s*RCINCLUDE\s*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
1481         my $from_file=($2 eq "<"?"":$dirname);
1482         my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1483         print FILEO "$1$2$real_include_name$4$'";
1484         $modified|=($real_include_name ne $3);
1485
1486       } elsif ($is_rc and !$is_mfc and $rc_block_depth == 0 and /^\s*\d+\s+TEXTINCLUDE\s*/) {
1487         $rc_textinclude_state=1;
1488         print FILEO;
1489
1490       } elsif ($rc_textinclude_state == 3 and /^(\s*\"\#\s*include\s*\"\")afxres\.h(\"\"\\r\\n\")/) {
1491         print FILEO "$1winres.h$2$'";
1492         $modified=1;
1493
1494       } elsif (/^\s*BEGIN(\W.*)?$/) {
1495         $rc_textinclude_state|=2;
1496         $rc_block_depth++;
1497         print FILEO;
1498
1499       } elsif (/^\s*END(\W.*)?$/) {
1500         $rc_textinclude_state=0;
1501         if ($rc_block_depth>0) {
1502           $rc_block_depth--;
1503         }
1504         print FILEO;
1505
1506       } else {
1507         print FILEO;
1508       }
1509
1510     } else {
1511       print FILEO;
1512     }
1513   }
1514
1515   close(FILEI);
1516   close(FILEO);
1517   if ($opt_backup == 0 or $modified == 0) {
1518     if (!unlink("$filename.bak")) {
1519       print STDERR "error: unable to delete $filename.bak:\n";
1520       print STDERR "       $!\n";
1521     }
1522   }
1523 }
1524
1525 ##
1526 # Analyzes each source file in turn to find and correct issues 
1527 # that would cause it not to compile.
1528 sub fix_source
1529 {
1530   print "Fixing the source files...\n";
1531   foreach $project (@projects) {
1532     foreach $target (@$project[$P_SETTINGS],@{@$project[$P_TARGETS]}) {
1533       if (@$target[$T_FLAGS] & $TF_WRAPPER) {
1534         next;
1535       }
1536       foreach $source (@{@$target[$T_SOURCES_C]}, @{@$target[$T_SOURCES_CXX]}, @{@$target[$T_SOURCES_RC]}, @{@$target[$T_SOURCES_MISC]}) {
1537         fix_file($source,$project,$target);
1538       }
1539     }
1540   }
1541 }
1542
1543
1544
1545 #####
1546 #
1547 # File generation
1548 #
1549 #####
1550
1551 ##
1552 # Generates a target's .spec file
1553 sub generate_spec_file
1554 {
1555   my $path=$_[0];
1556   my $target=$_[1];
1557   my $project_settings=$_[2];
1558
1559   my $basename=@$target[$T_NAME];
1560   $basename =~ s+\.so$++;
1561   if (@$target[$T_FLAGS] & $TF_WRAP) {
1562     $basename =~ s+^lib++;
1563   } elsif (@$target[$T_FLAGS] & $TF_WRAPPER) {
1564     $basename.="_wrapper";
1565   }
1566
1567   if (!open(FILEO,">$path$basename.spec")) {
1568     print STDERR "error: could not open \"$path$basename.spec\" for writing\n";
1569     print STDERR "       $!\n";
1570     return;
1571   }
1572
1573   my $module=$basename;
1574   $module =~ s+^lib++;
1575   $module=canonize($module);
1576   print FILEO "name    $module\n";
1577   print FILEO "type    win32\n";
1578   if (@$target[$T_TYPE] == $TT_GUIEXE) {
1579     print FILEO "mode    guiexe\n";
1580   } elsif (@$target[$T_TYPE] == $TT_CUIEXE) {
1581     print FILEO "mode    cuiexe\n";
1582   } else {
1583     print FILEO "mode    dll\n";
1584   }
1585   if (defined @$target[$T_INIT] and ((@$target[$T_FLAGS] & $TF_WRAP) == 0)) {
1586     print FILEO "init    @$target[$T_INIT]\n";
1587   }
1588   if (@{@$target[$T_SOURCES_RC]} > 0) {
1589     if (@{@$target[$T_SOURCES_RC]} > 1) {
1590       print STDERR "warning: the target $basename has more than one RC file. Modify the Makefile.in to remove redundant RC files, and fix the spec file\n";
1591     }
1592     my $rcname=@{@$target[$T_SOURCES_RC]}[0];
1593     $rcname =~ s+\.rc$++i;
1594     $rcname =~ s+([^/\w])+\\$1+g;
1595     print FILEO "rsrc    $rcname.res\n";
1596   }
1597   print FILEO "\n";
1598   my %imports;
1599   foreach $library (@{$global_settings[$T_IMPORTS]}) {
1600     if (!defined $imports{$library}) {
1601       print FILEO "import $library\n";
1602       $imports{$library}=1;
1603     }
1604   }
1605   if (defined $project_settings) {
1606     foreach $library (@{@$project_settings[$T_IMPORTS]}) {
1607       if (!defined $imports{$library}) {
1608         print FILEO "import $library\n";
1609         $imports{$library}=1;
1610       }
1611     }
1612   }
1613   foreach $library (@{@$target[$T_IMPORTS]}) {
1614     if (!defined $imports{$library}) {
1615       print FILEO "import $library\n";
1616       $imports{$library}=1;
1617     }
1618   }
1619
1620   # Don't forget to export the 'Main' function for wrapped executables, 
1621   # except for MFC ones!
1622   if (@$target[$T_FLAGS] == $TF_WRAP) {
1623     if (@$target[$T_TYPE] == $TT_GUIEXE) {
1624       print FILEO "\n@ stdcall @$target[$T_INIT](long long ptr long) @$target[$T_INIT]\n";
1625     } elsif (@$target[$T_TYPE] == $TT_CUIEXE) {
1626       print FILEO "\n@ stdcall @$target[$T_INIT](long ptr ptr) @$target[$T_INIT]\n";
1627     } else {
1628       print FILEO "\n@ stdcall @$target[$T_INIT](ptr long ptr) @$target[$T_INIT]\n";
1629     }
1630   }
1631
1632   close(FILEO);
1633 }
1634
1635 ##
1636 # Generates a target's wrapper file
1637 sub generate_wrapper_file
1638 {
1639   my $path=$_[0];
1640   my $target=$_[1];
1641
1642   if (!defined $templates{"wrapper.c"}) {
1643     print STDERR "winemaker: internal error: No template called 'wrapper.c'\n";
1644     return;
1645   }
1646
1647   if (!open(FILEO,">$path@$target[$T_NAME]_wrapper.c")) {
1648     print STDERR "error: unable to open \"$path$basename.c\" for writing:\n";
1649     print STDERR "       $!\n";
1650     return;
1651   }
1652   my $app_name="\"@$target[$T_NAME]\"";
1653   my $app_type=(@$target[$T_TYPE]==$TT_GUIEXE?"GUIEXE":"CUIEXE");
1654   my $app_init=(@$target[$T_TYPE]==$TT_GUIEXE?"\"WinMain\"":"\"main\"");
1655   my $app_mfc=(@$target[$T_FLAGS] & $TF_MFC?"\"mfc\"":NULL);
1656   foreach $line (@{$templates{"wrapper.c"}}) {
1657     my $l=$line;
1658     $l =~ s/\#\#WINEMAKER_APP_NAME\#\#/$app_name/;
1659     $l =~ s/\#\#WINEMAKER_APP_TYPE\#\#/$app_type/;
1660     $l =~ s/\#\#WINEMAKER_APP_INIT\#\#/$app_init/;
1661     $l =~ s/\#\#WINEMAKER_APP_MFC\#\#/$app_mfc/;
1662     print FILEO $l;
1663   }
1664   close(FILEO);
1665 }
1666
1667 ##
1668 # A convenience function to generate all the lists (defines, 
1669 # C sources, C++ source, etc.) in the Makefile
1670 sub generate_list
1671 {
1672   my $name=$_[0];
1673   my $last=$_[1];
1674   my $list=$_[2];
1675   my $data=$_[3];
1676   my $first=$name;
1677
1678   if ($name) {
1679     printf FILEO "%-22s=",$name;
1680   }
1681   if (defined $list) {
1682     foreach $item (@$list) {
1683       my $value;
1684       if (defined $data) {
1685         $value=&$data($item);
1686       } else {
1687         $value=$item;
1688       }
1689       if ($value ne "") {
1690         if ($first) {
1691           print FILEO " $value";
1692           $first=0;
1693         } else {
1694           print FILEO " \\\n\t\t\t$value";
1695         }
1696       }
1697     }
1698   }
1699   if ($last) {
1700     print FILEO "\n";
1701   }
1702 }
1703
1704 ##
1705 # Generates a project's Makefile.in and all the target files
1706 sub generate_project_files
1707 {
1708   my $project=$_[0];
1709   my $project_settings=@$project[$P_SETTINGS];
1710   my @dll_list=();
1711   my @exe_list=();
1712
1713   # Then sort the targets and separate the libraries from the programs
1714   foreach $target (sort { @$a[$T_NAME] cmp @$b[$T_NAME] } @{@$project[$P_TARGETS]}) {
1715     if (@$target[$T_TYPE] == $TT_DLL) {
1716       push @dll_list,$target;
1717     } else {
1718       push @exe_list,$target;
1719     }
1720   }
1721   @$project[$P_TARGETS]=[];
1722   push @{@$project[$P_TARGETS]}, @dll_list;
1723   push @{@$project[$P_TARGETS]}, @exe_list;
1724
1725   if (!open(FILEO,">@$project[$P_PATH]Makefile.in")) {
1726     print STDERR "error: could not open \"@$project[$P_PATH]/Makefile.in\" for writing\n";
1727     print STDERR "       $!\n";
1728     return;
1729   }
1730
1731   print FILEO "### Generated by Winemaker\n";
1732   print FILEO "\n\n";
1733
1734   print FILEO "### Generic autoconf variables\n\n";
1735   generate_list("TOPSRCDIR",1,[ "\@top_srcdir\@" ]);
1736   generate_list("TOPOBJDIR",1,[ "." ]);
1737   generate_list("SRCDIR",1,[ "\@srcdir\@" ]);
1738   generate_list("VPATH",1,[ "\@srcdir\@" ]);
1739   print FILEO "\n";
1740   if (@$project[$P_PATH] eq "") {
1741     # This is the main project. It is also responsible for recursively 
1742     # calling the other projects
1743     generate_list("SUBDIRS",1,\@projects,sub 
1744                   {
1745                     if ($_[0] != \@main_project) {
1746                       my $subdir=@{$_[0]}[$P_PATH];
1747                       $subdir =~ s+/$++;
1748                       return $subdir;
1749                     }
1750                     # Eliminating the main project by returning undefined!
1751                   });
1752   }
1753   if (@{@$project[$P_TARGETS]} > 0) {
1754     generate_list("DLLS",1,\@dll_list,sub
1755                   {
1756                     return @{$_[0]}[$T_NAME];
1757                   });
1758     generate_list("EXES",1,\@exe_list,sub
1759                   {
1760                     return "@{$_[0]}[$T_NAME]";
1761                   });
1762     print FILEO "\n\n\n";
1763
1764     print FILEO "### Global settings\n\n";
1765     # Make it so that the project-wide settings override the global settings
1766     generate_list("DEFINES",0,@$project_settings[$T_DEFINES]);
1767     generate_list("",1,$global_settings[$T_DEFINES]);
1768     generate_list("INCLUDE_PATH",$no_extra,@$project_settings[$T_INCLUDE_PATH]);
1769     generate_list("",1,$global_settings[$T_INCLUDE_PATH],sub
1770                   {
1771                     if ($_[0] !~ /^-I/ or is_absolute($')) {
1772                       return "$_[0]";
1773                     }
1774                     return "-I\$(TOPSRCDIR)/$'";
1775                   });
1776     generate_list("LIBRARY_PATH",$no_extra,@$project_settings[$T_LIBRARY_PATH]);
1777     generate_list("",1,$global_settings[$T_LIBRARY_PATH],sub
1778                   {
1779                     if ($_[0] !~ /^-L/ or is_absolute($')) {
1780                       return "$_[0]";
1781                     }
1782                     return "-L\$(TOPSRCDIR)/$'";
1783                   });
1784     generate_list("LIBRARIES",$no_extra,@$project_settings[$T_LIBRARIES]);
1785     generate_list("",1,$global_settings[$T_LIBRARIES]);
1786     print FILEO "\n\n";
1787
1788     my $extra_source_count=@{@$project_settings[$T_SOURCES_C]}+
1789                            @{@$project_settings[$T_SOURCES_CXX]}+
1790                            @{@$project_settings[$T_SOURCES_RC]};
1791     my $no_extra=($extra_source_count == 0);
1792     if (!$no_extra) {
1793       print FILEO "### Extra source lists\n\n";
1794       generate_list("EXTRA_C_SRCS",1,@$project_settings[$T_SOURCES_C]);
1795       generate_list("EXTRA_CXX_SRCS",1,@$project_settings[$T_SOURCES_CXX]);
1796       generate_list("EXTRA_RC_SRCS",1,@$project_settings[$T_SOURCES_RC]);
1797       print FILEO "\n";
1798       generate_list("EXTRA_OBJS",1,["\$(EXTRA_C_SRCS:.c=.o)","\$(EXTRA_CXX_SRCS:.cpp=.o)"]);
1799       print FILEO "\n\n\n";
1800     }
1801
1802     # Iterate over all the targets...
1803     foreach $target (@{@$project[$P_TARGETS]}) {
1804       print FILEO "### @$target[$T_NAME] sources and settings\n\n";
1805       my $canon=canonize("@$target[$T_NAME]");
1806       $canon =~ s+_so$++;
1807       generate_list("${canon}_C_SRCS",1,@$target[$T_SOURCES_C]);
1808       generate_list("${canon}_CXX_SRCS",1,@$target[$T_SOURCES_CXX]);
1809       generate_list("${canon}_RC_SRCS",1,@$target[$T_SOURCES_RC]);
1810       my $basename=@$target[$T_NAME];
1811       $basename =~ s+\.so$++;
1812       if (@$target[$T_FLAGS] & $TF_WRAP) {
1813         $basename =~ s+^lib++;
1814       } elsif (@$target[$T_FLAGS] & $TF_WRAPPER) {
1815         $basename.="_wrapper";
1816       }
1817       generate_list("${canon}_SPEC_SRCS",1,[ "$basename.spec" ]);
1818       generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH]);
1819       generate_list("${canon}_LIBRARIES",1,@$target[$T_LIBRARIES]);
1820       generate_list("${canon}_DEPENDS",1,@$target[$T_DEPENDS]);
1821       print FILEO "\n";
1822       generate_list("${canon}_OBJS",1,["\$(${canon}_C_SRCS:.c=.o)","\$(${canon}_CXX_SRCS:.cpp=.o)","\$(EXTRA_OBJS)"]);
1823       print FILEO "\n\n\n";
1824     }
1825     print FILEO "### Global source lists\n\n";
1826     generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub
1827                   {
1828                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1829                     $canon =~ s+_so$++;
1830                     return "\$(${canon}_C_SRCS)";
1831                   });
1832     if (!$no_extra) {
1833       generate_list("",1,[ "\$(EXTRA_C_SRCS)" ]);
1834     }
1835     generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub
1836                   {
1837                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1838                     $canon =~ s+_so$++;
1839                     return "\$(${canon}_CXX_SRCS)";
1840                   });
1841     if (!$no_extra) {
1842       generate_list("",1,[ "\$(EXTRA_CXX_SRCS)" ]);
1843     }
1844     generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub
1845                   {
1846                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1847                     $canon =~ s+_so$++;
1848                     return "\$(${canon}_RC_SRCS)";
1849                   });
1850     if (!$no_extra) {
1851       generate_list("",1,[ "\$(EXTRA_RC_SRCS)" ]);
1852     }
1853     generate_list("SPEC_SRCS",1,@$project[$P_TARGETS],sub
1854                   {
1855                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1856                     $canon =~ s+_so$++;
1857                     return "\$(${canon}_SPEC_SRCS)";
1858                   });
1859   }
1860   print FILEO "\n\n\n";
1861
1862   print FILEO "### Generic autoconf targets\n\n";
1863   print FILEO "all:";
1864   if (@$project[$P_PATH] eq "") {
1865     print FILEO " \$(SUBDIRS)";
1866   }
1867   if (@{@$project[$P_TARGETS]} > 0) {
1868     print FILEO " \$(DLLS) \$(EXES:%=%.so)";
1869   }
1870   print FILEO "\n\n";
1871   print FILEO "\@MAKE_RULES\@\n";
1872   print FILEO "\n";
1873   print FILEO "install::\n";
1874   if (@$project[$P_PATH] eq "") {
1875     # This is the main project. It is also responsible for recursively 
1876     # calling the other projects
1877     print FILEO "\tfor i in \$(SUBDIRS); do (cd \$\$i; \$(MAKE) install) || exit 1; done\n";
1878   }
1879   if (@{@$project[$P_TARGETS]} > 0) {
1880     print FILEO "\tfor i in \$(EXES); do \$(INSTALL_PROGRAM) \$\$i \$(bindir); done\n";
1881     print FILEO "\tfor i in \$(EXES:%=%.so) \$(DLLS); do \$(INSTALL_PROGRAM) \$\$i \$(libdir); done\n";
1882   }
1883   print FILEO "\n";
1884   print FILEO "uninstall::\n";
1885   if (@$project[$P_PATH] eq "") {
1886     # This is the main project. It is also responsible for recursively 
1887     # calling the other projects
1888     print FILEO "\tfor i in \$(SUBDIRS); do (cd \$\$i; \$(MAKE) uninstall) || exit 1; done\n";
1889   }
1890   if (@{@$project[$P_TARGETS]} > 0) {
1891     print FILEO "\tfor i in \$(EXES); do \$(RM) \$(bindir)/\$\$i;done\n";
1892     print FILEO "\tfor i in \$(EXES:%=%.so) \$(DLLS); do \$(RM) \$(libdir)/\$\$i;done\n";
1893   }
1894   print FILEO "\n\n\n";
1895
1896   if (@{@$project[$P_TARGETS]} > 0) {
1897     print FILEO "### Target specific build rules\n\n";
1898     foreach $target (@{@$project[$P_TARGETS]}) {
1899       my $canon=canonize("@$target[$T_NAME]");
1900       $canon =~ s/_so$//;
1901       print FILEO "\$(${canon}_SPEC_SRCS:.spec=.tmp.o): \$(${canon}_OBJS)\n";
1902       print FILEO "\t\$(LDCOMBINE) \$(${canon}_OBJS) -o \$\@\n";
1903       print FILEO "\t-\$(STRIP) \$(STRIPFLAGS) \$\@\n";
1904       print FILEO "\n";
1905       print FILEO "\$(${canon}_SPEC_SRCS:.spec=.spec.c): \$(${canon}_SPEC_SRCS:.spec) \$(${canon}_SPEC_SRCS:.spec=.tmp.o) \$(${canon}_RC_SRCS:.rc=.res)\n";
1906       print FILEO "\t\$(LD_PATH) \$(WINEBUILD) -fPIC \$(${canon}_LIBRARY_PATH) \$(WINE_LIBRARY_PATH) -sym \$(${canon}_SPEC_SRCS:.spec=.tmp.o) -o \$\@ -spec \$(SRCDIR)/\$(${canon}_SPEC_SRCS)\n";
1907       print FILEO "\n";
1908       my $t_name=@$target[$T_NAME];
1909       if (@$target[$T_TYPE]!=$TT_DLL) {
1910         $t_name.=".so";
1911       }
1912       print FILEO "$t_name: \$(${canon}_SPEC_SRCS:.spec=.spec.o) \$(${canon}_OBJS) \$(${canon}_DEPENDS) \n";
1913       if (@{@$target[$T_SOURCES_CXX]} > 0 or @{@$project_settings[$T_SOURCES_CXX]} > 0) {
1914         print FILEO "\t\$(LDXXSHARED)";
1915       } else {
1916         print FILEO "\t\$(LDSHARED)";
1917       }
1918       print FILEO " \$(LDDLLFLAGS) -o \$\@ \$(${canon}_OBJS) \$(${canon}_SPEC_SRCS:.spec=.spec.o) \$(${canon}_LIBRARY_PATH) \$(${canon}_LIBRARIES:%=-l%) \$(DLL_LINK) \$(LIBS)\n";
1919       if (@$target[$T_TYPE] ne $TT_DLL) {
1920         print FILEO "\ttest -e @$target[$T_NAME] || \$(LN_S) \$(WINE) @$target[$T_NAME]\n";
1921       }
1922       print FILEO "\n\n";
1923     }
1924   }
1925   close(FILEO);
1926
1927   foreach $target (@{@$project[$P_TARGETS]}) {
1928     generate_spec_file(@$project[$P_PATH],$target,$project_settings);
1929     if (@$target[$T_FLAGS] & $TF_WRAPPER) {
1930       generate_wrapper_file(@$project[$P_PATH],$target);
1931     }
1932   }
1933 }
1934
1935 ##
1936 # Perform the replacements in the template configure files
1937 # Return 1 for success, 0 for failure
1938 sub generate_configure
1939 {
1940   my $filename=$_[0];
1941   my $a_source_file=$_[1];
1942
1943   if (!defined $templates{$filename}) {
1944     if ($filename ne "configure") {
1945       print STDERR "winemaker: internal error: No template called '$filename'\n";
1946     }
1947     return 0;
1948   }
1949
1950   if (!open(FILEO,">$filename")) {
1951     print STDERR "error: unable to open \"$filename\" for writing:\n";
1952     print STDERR "       $!\n";
1953     return 0;
1954   }
1955   foreach $line (@{$templates{$filename}}) {
1956     if ($line =~ /^\#\#WINEMAKER_PROJECTS\#\#$/) {
1957       foreach $project (@projects) {
1958         print FILEO "@$project[$P_PATH]Makefile\n";
1959       }
1960     } else {
1961       $line =~ s+\#\#WINEMAKER_SOURCE\#\#+$a_source_file+;
1962       $line =~ s+\#\#WINEMAKER_NEEDS_MFC\#\#+$needs_mfc+;
1963       print FILEO $line;
1964     }
1965   }
1966   close(FILEO);
1967   return 1;
1968 }
1969
1970 sub generate_generic
1971 {
1972   my $filename=$_[0];
1973
1974   if (!defined $templates{$filename}) {
1975     print STDERR "winemaker: internal error: No template called '$filename'\n";
1976     return;
1977   }
1978   if (!open(FILEO,">$filename")) {
1979     print STDERR "error: unable to open \"$filename\" for writing:\n";
1980     print STDERR "       $!\n";
1981     return;
1982   }
1983   foreach $line (@{$templates{$filename}}) {
1984     print FILEO $line;
1985   }
1986   close(FILEO);
1987 }
1988
1989 ##
1990 # Generates the global files:
1991 # configure
1992 # configure.in
1993 # Make.rules.in
1994 sub generate_global_files
1995 {
1996   generate_generic("Make.rules.in");
1997
1998   # Get the name of a source file for configure.in
1999   my $a_source_file;
2000   search_a_file: foreach $project (@projects) {
2001     foreach $target (@{@$project[$P_TARGETS]}, @$project[$P_SETTINGS]) {
2002       $a_source_file=@{@$target[$T_SOURCES_C]}[0];
2003       if (!defined $a_source_file) {
2004         $a_source_file=@{@$target[$T_SOURCES_CXX]}[0];
2005       }
2006       if (!defined $a_source_file) {
2007         $a_source_file=@{@$target[$T_SOURCES_RC]}[0];
2008       }
2009       if (defined $a_source_file) {
2010         $a_source_file="@$project[$P_PATH]$a_source_file";
2011         last search_a_file;
2012       }
2013     }
2014   }
2015   if (!defined $a_source_file) {
2016     $a_source_file="Makefile.in";
2017   }
2018
2019   generate_configure("configure.in",$a_source_file);
2020   unlink("configure");
2021   if (generate_configure("configure",$a_source_file) == 0) {
2022     system("autoconf");
2023   }
2024   # Add execute permission to configure for whoever has the right to read it
2025   my @st=stat("configure");
2026   if (@st) {
2027     my $mode=$st[2];
2028     $mode|=($mode & 0444) >>2;
2029     chmod($mode,"configure");
2030   } else {
2031     print "warning: could not generate the configure script. You need to run autoconf\n";
2032   }
2033 }
2034
2035 ##
2036
2037 sub generate_read_templates
2038 {
2039   my $file;
2040
2041   while (<DATA>) {
2042     if (/^--- ((\w\.?)+) ---$/) {
2043       my $filename=$1;
2044       if (defined $templates{$filename}) {
2045         print STDERR "winemaker: internal error: There is more than one template for $filename\n";
2046         undef $file;
2047       } else {
2048         $file=[];
2049         $templates{$filename}=$file;
2050       }
2051     } elsif (defined $file) {
2052       push @$file, $_;
2053     }
2054   }
2055 }
2056
2057 ##
2058 # This is where we finally generate files. In fact this method does not 
2059 # do anything itself but calls the methods that do the actual work.
2060 sub generate
2061 {
2062   print "Generating project files...\n";
2063   generate_read_templates();
2064   generate_global_files();
2065
2066   foreach $project (@projects) {
2067     my $path=@$project[$P_PATH];
2068     if ($path eq "") {
2069       $path=".";
2070     } else {
2071       $path =~ s+/$++;
2072     }
2073     print "  $path\n";
2074     generate_project_files($project);
2075   }
2076 }
2077
2078
2079
2080 #####
2081 #
2082 # Option defaults
2083 #
2084 #####
2085
2086 $opt_backup=1;
2087 $opt_lower=$OPT_LOWER_UPPERCASE;
2088 $opt_lower_include=1;
2089
2090 # $opt_work_dir=<undefined>
2091 # $opt_single_target=<undefined>
2092 $opt_target_type=$TT_GUIEXE;
2093 $opt_flags=0;
2094 $opt_is_interactive=$OPT_ASK_NO;
2095 $opt_ask_project_options=$OPT_ASK_NO;
2096 $opt_ask_target_options=$OPT_ASK_NO;
2097 $opt_no_generated_files=0;
2098 $opt_no_banner=0;
2099
2100
2101
2102 #####
2103 #
2104 # Main
2105 #
2106 #####
2107
2108 sub print_banner
2109 {
2110   print "Winemaker $version\n";
2111   print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
2112 }
2113
2114 sub usage
2115 {
2116   print_banner();
2117   print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup]\n";
2118   print STDERR "                 [--lower-none|--lower-all|--lower-uppercase]\n";
2119   print STDERR "                 [--lower-include|--nolower-include]\n";
2120   print STDERR "                 [--guiexe|--windows|--cuiexe|--console|--dll]\n";
2121   print STDERR "                 [--wrap|--nowrap] [--mfc|--nomfc]\n";
2122   print STDERR "                 [-Dmacro[=defn]] [-Idir] [-Ldir] [-idll] [-llibrary]\n";
2123   print STDERR "                 [--interactive] [--single-target name]\n";
2124   print STDERR "                 [--generated-files|--nogenerated-files]\n";
2125   print STDERR "                 work_directory\n";
2126   print STDERR "\nWinemaker is designed to recursively convert all the Windows sources found in\n";
2127   print STDERR "the specified directory so that they can be compiled with Winelib. During this\n";
2128   print STDERR "process it will modify and rename some of the files in that directory.\n";
2129   print STDERR "\tPlease read the manual page before use.\n";
2130   exit (2);
2131 }
2132
2133
2134 project_init(\@main_project,"");
2135
2136 while (@ARGV>0) {
2137   my $arg=shift @ARGV;
2138   # General options
2139   if ($arg eq "--nobanner") {
2140     $opt_no_banner=1;
2141   } elsif ($arg eq "--backup") {
2142     $opt_backup=1;
2143   } elsif ($arg eq "--nobackup") {
2144     $opt_backup=0;
2145   } elsif ($arg eq "--single-target") {
2146     $opt_single_target=shift @ARGV;
2147   } elsif ($arg eq "--lower-none") {
2148     $opt_lower=$OPT_LOWER_NONE;
2149   } elsif ($arg eq "--lower-all") {
2150     $opt_lower=$OPT_LOWER_ALL;
2151   } elsif ($arg eq "--lower-uppercase") {
2152     $opt_lower=$OPT_LOWER_UPPERCASE;
2153   } elsif ($arg eq "--lower-include") {
2154     $opt_lower_include=1;
2155   } elsif ($arg eq "--nolower-include") {
2156     $opt_lower_include=0;
2157   } elsif ($arg eq "--generated-files") {
2158     $opt_no_generated_files=0;
2159   } elsif ($arg eq "--nogenerated-files") {
2160     $opt_no_generated_files=1;
2161
2162   } elsif ($arg =~ /^-D/) {
2163     push @{$global_settings[$T_DEFINES]},$arg;
2164   } elsif ($arg =~ /^-I/) {
2165     push @{$global_settings[$T_INCLUDE_PATH]},$arg;
2166   } elsif ($arg =~ /^-L/) {
2167     push @{$global_settings[$T_LIBRARY_PATH]},$arg;
2168   } elsif ($arg =~ /^-i/) {
2169     push @{$global_settings[$T_IMPORTS]},$';
2170   } elsif ($arg =~ /^-l/) {
2171     push @{$global_settings[$T_LIBRARIES]},$';
2172
2173   # 'Source'-based method options
2174   } elsif ($arg eq "--dll") {
2175     $opt_target_type=$TT_DLL;
2176   } elsif ($arg eq "--guiexe" or $arg eq "--windows") {
2177     $opt_target_type=$TT_GUIEXE;
2178   } elsif ($arg eq "--cuiexe" or $arg eq "--console") {
2179     $opt_target_type=$TT_CUIEXE;
2180   } elsif ($arg eq "--interactive") {
2181     $opt_is_interactive=$OPT_ASK_YES;
2182     $opt_ask_project_options=$OPT_ASK_YES;
2183     $opt_ask_target_options=$OPT_ASK_YES;
2184   } elsif ($arg eq "--wrap") {
2185     $opt_flags|=$TF_WRAP;
2186   } elsif ($arg eq "--nowrap") {
2187     $opt_flags&=~$TF_WRAP;
2188   } elsif ($arg eq "--mfc") {
2189     $opt_flags|=$TF_MFC;
2190     $opt_flags|=$TF_MFC|$TF_WRAP;
2191     $needs_mfc=1;
2192   } elsif ($arg eq "--nomfc") {
2193     $opt_flags&=~($TF_MFC|$TF_WRAP);
2194     $needs_mfc=0;
2195
2196   # Catch errors
2197   } else {
2198     if ($arg ne "--help" and $arg ne "-h" and $arg ne "-?") {
2199       if (!defined $opt_work_dir) {
2200         $opt_work_dir=$arg;
2201       } else {
2202         print STDERR "error: the work directory, \"$arg\", has already been specified (was \"$opt_work_dir\")\n";
2203         usage();
2204       }
2205     } else {
2206       usage();
2207     }
2208   }
2209 }
2210
2211 if (!defined $opt_work_dir) {
2212   print STDERR "error: you must specify the directory containing the sources to be converted\n";
2213   usage();
2214 } elsif (!chdir $opt_work_dir) {
2215   print STDERR "error: could not chdir to the work directory\n";
2216   print STDERR "       $!\n";
2217   usage();
2218 }
2219
2220 if ($opt_no_banner == 0) {
2221   print_banner();
2222 }
2223
2224 # Fix the file and directory names
2225 fix_file_and_directory_names(".");
2226
2227 # Scan the sources to identify the projects and targets
2228 source_scan();
2229
2230 # Create targets for wrappers, etc.
2231 postprocess_targets();
2232
2233 # Fix the source files
2234 fix_source();
2235
2236 # Generate the Makefile and the spec file
2237 if (! $opt_no_generated_files) {
2238   generate();
2239 }
2240
2241
2242 __DATA__
2243 --- configure.in ---
2244 dnl Process this file with autoconf to produce a configure script.
2245 dnl Author: Michael Patra   <micky@marie.physik.tu-berlin.de>
2246 dnl                         <patra@itp1.physik.tu-berlin.de>
2247 dnl         Francois Gouget <fgouget@codeweavers.com> for CodeWeavers
2248
2249 AC_REVISION([configure.in 1.00])
2250 AC_INIT(##WINEMAKER_SOURCE##)
2251
2252 NEEDS_MFC=##WINEMAKER_NEEDS_MFC##
2253
2254 dnl **** Command-line arguments ****
2255
2256 AC_SUBST(OPTIONS)
2257
2258 dnl **** Check for some programs ****
2259
2260 AC_PROG_MAKE_SET
2261 AC_PROG_CC
2262 AC_PROG_CXX
2263 AC_PROG_CPP
2264 AC_PROG_LN_S
2265
2266 dnl **** Check for some libraries ****
2267
2268 dnl Check for -lm for BeOS
2269 AC_CHECK_LIB(m,sqrt)
2270 dnl Check for -lw for Solaris
2271 AC_CHECK_LIB(w,iswalnum)
2272 dnl Check for -lnsl for Solaris
2273 AC_CHECK_FUNCS(gethostbyname,, AC_CHECK_LIB(nsl, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lnsl", AC_CHECK_LIB(socket, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lnsl", , -lnsl), -lsocket))
2274 dnl Check for -lsocket for Solaris
2275 AC_CHECK_FUNCS(connect,,AC_CHECK_LIB(socket,connect))
2276
2277 dnl **** If ln -s doesn't work, use cp instead ****
2278 if test "$ac_cv_prog_LN_S" = "ln -s"; then : ; else LN_S=cp ; fi
2279
2280 dnl **** Check for gcc strength-reduce bug ****
2281
2282 if test "x${GCC}" = "xyes"
2283 then
2284   AC_CACHE_CHECK( "for gcc strength-reduce bug", ac_cv_c_gcc_strength_bug,
2285                   AC_TRY_RUN([
2286 int main(void) {
2287   static int Array[[3]];
2288   unsigned int B = 3;
2289   int i;
2290   for(i=0; i<B; i++) Array[[i]] = i - 3;
2291   exit( Array[[1]] != -2 );
2292 }],
2293     ac_cv_c_gcc_strength_bug="no",
2294     ac_cv_c_gcc_strength_bug="yes",
2295     ac_cv_c_gcc_strength_bug="yes") )
2296   if test "$ac_cv_c_gcc_strength_bug" = "yes"
2297   then
2298     CFLAGS="$CFLAGS -fno-strength-reduce"
2299   fi
2300 fi
2301
2302 dnl **** Check for underscore on external symbols ****
2303
2304 AC_CACHE_CHECK("whether external symbols need an underscore prefix",
2305                ac_cv_c_extern_prefix,
2306 [saved_libs=$LIBS
2307 LIBS="conftest_asm.s $LIBS"
2308 cat > conftest_asm.s <<EOF
2309         .globl _ac_test
2310 _ac_test:
2311         .long 0
2312 EOF
2313 AC_TRY_LINK([extern int ac_test;],[if (ac_test) return 1],
2314             ac_cv_c_extern_prefix="yes",ac_cv_c_extern_prefix="no")
2315 LIBS=$saved_libs])
2316 if test "$ac_cv_c_extern_prefix" = "yes"
2317 then
2318   AC_DEFINE(NEED_UNDERSCORE_PREFIX)
2319 fi
2320
2321 dnl **** Check for working dll ****
2322
2323 LDSHARED=""
2324 LDXXSHARED=""
2325 LDDLLFLAGS=""
2326 AC_CACHE_CHECK("whether we can build a Linux dll",
2327                ac_cv_c_dll_linux,
2328 [saved_cflags=$CFLAGS
2329 CFLAGS="$CFLAGS -fPIC -shared -Wl,-soname,conftest.so.1.0,-Bsymbolic"
2330 AC_TRY_LINK(,[return 1],ac_cv_c_dll_linux="yes",ac_cv_c_dll_linux="no")
2331 CFLAGS=$saved_cflags
2332 ])
2333 if test "$ac_cv_c_dll_linux" = "yes"
2334 then
2335   LDSHARED="\$(CC) -shared -Wl,-rpath,\$(libdir)"
2336   LDXXSHARED="\$(CXX) -shared -Wl,-rpath,\$(libdir)"
2337   LDDLLFLAGS="-Wl,-Bsymbolic"
2338 else
2339   AC_CACHE_CHECK(whether we can build a UnixWare (Solaris) dll,
2340                 ac_cv_c_dll_unixware,
2341   [saved_cflags=$CFLAGS
2342   CFLAGS="$CFLAGS -fPIC -Wl,-G,-h,conftest.so.1.0,-B,symbolic"
2343   AC_TRY_LINK(,[return 1],ac_cv_c_dll_unixware="yes",ac_cv_c_dll_unixware="no")
2344   CFLAGS=$saved_cflags
2345   ])
2346   if test "$ac_cv_c_dll_unixware" = "yes"
2347   then
2348     LDSHARED="\$(CC) -Wl,-G"
2349     LDXXSHARED="\$(CXX) -Wl,-G"
2350     LDDLLFLAGS="-Wl,-B,symbolic"
2351   else
2352     AC_CACHE_CHECK("whether we can build a NetBSD dll",
2353                    ac_cv_c_dll_netbsd,
2354     [saved_cflags=$CFLAGS
2355     CFLAGS="$CFLAGS -fPIC -Wl,-Bshareable,-Bforcearchive"
2356     AC_TRY_LINK(,[return 1],ac_cv_c_dll_netbsd="yes",ac_cv_c_dll_netbsd="no")
2357     CFLAGS=$saved_cflags
2358     ])
2359     if test "$ac_cv_c_dll_netbsd" = "yes"
2360     then
2361       LDSHARED="\$(CC) -Wl,-Bshareable,-Bforcearchive"
2362       LDXXSHARED="\$(CXX) -Wl,-Bshareable,-Bforcearchive"
2363       LDDLLFLAGS="" #FIXME
2364     fi
2365   fi
2366 fi
2367 if test "$ac_cv_c_dll_linux" = "no" -a "$ac_cv_c_dll_unixware" = "no" -a "$ac_cv_c_dll_netbsd" = "no"
2368 then
2369   AC_MSG_ERROR([Could not find how to build a dynamically linked library])
2370 fi
2371
2372 CFLAGS="$CFLAGS -fPIC"
2373 DLL_LINK="\$(WINE_LIBRARY_PATH) \$(LIBRARY_PATH) \$(LIBRARIES:%=-l%) -lwine -lwine_unicode -lwine_uuid"
2374
2375 AC_SUBST(DLL_LINK)
2376 AC_SUBST(LDSHARED)
2377 AC_SUBST(LDXXSHARED)
2378 AC_SUBST(LDDLLFLAGS)
2379
2380 dnl *** check for the need to define __i386__
2381
2382 AC_CACHE_CHECK("whether we need to define __i386__",ac_cv_cpp_def_i386,
2383  AC_EGREP_CPP(yes,[#if (defined(i386) || defined(__i386)) && !defined(__i386__)
2384 yes
2385 #endif],
2386  ac_cv_cpp_def_i386="yes", ac_cv_cpp_def_i386="no"))
2387 if test "$ac_cv_cpp_def_i386" = "yes"
2388 then
2389     CFLAGS="$CFLAGS -D__i386__"
2390 fi
2391
2392 dnl $GCC is set by autoconf
2393 GCC_NO_BUILTIN=""
2394 if test "$GCC" = "yes"
2395 then
2396     GCC_NO_BUILTIN="-fno-builtin"
2397 fi
2398 AC_SUBST(GCC_NO_BUILTIN)
2399
2400 dnl **** Test Winelib-related features of the C++ compiler
2401 AC_LANG_CPLUSPLUS()
2402 if test "x${GCC}" = "xyes"
2403 then
2404   OLDCXXFLAGS="$CXXFLAGS";
2405   CXXFLAGS="-fpermissive";
2406   AC_CACHE_CHECK("for g++ -fpermissive option", has_gxx_permissive,
2407     AC_TRY_COMPILE(,[
2408         for (int i=0;i<2;i++);
2409         i=0;
2410       ],
2411       [has_gxx_permissive="yes"],
2412       [has_gxx_permissive="no"])
2413    )
2414   CXXFLAGS="-fno-for-scope";
2415   AC_CACHE_CHECK("for g++ -fno-for-scope option", has_gxx_no_for_scope,
2416     AC_TRY_COMPILE(,[
2417         for (int i=0;i<2;i++);
2418         i=0;
2419       ],
2420       [has_gxx_no_for_scope="yes"],
2421       [has_gxx_no_for_scope="no"])
2422    )
2423   CXXFLAGS="$OLDCXXFLAGS";
2424   if test "$has_gxx_permissive" = "yes"
2425   then
2426     CXXFLAGS="$CXXFLAGS -fpermissive"
2427   fi
2428   if test "$has_gxx_no_for_scope" = "yes"
2429   then
2430     CXXFLAGS="$CXXFLAGS -fno-for-scope"
2431   fi
2432 fi
2433 AC_LANG_C()
2434
2435 dnl **** Test Winelib-related features of the C compiler
2436 dnl none for now
2437
2438 dnl **** Macros for finding a headers/libraries in a collection of places
2439
2440 dnl AC_PATH_HEADER(variable,header,action-if-not-found,default-locations)
2441 dnl Note that the above may set variable to an empty value if the header is 
2442 dnl already in the include path
2443 AC_DEFUN(AC_PATH_HEADER,[
2444 AC_MSG_CHECKING([for $2])
2445 AC_CACHE_VAL(ac_cv_path_$1,
2446 [
2447   ac_found=
2448   ac_dummy="ifelse([$4], , :/usr/local/include, [$4])"
2449   save_CPPFLAGS="$CPPFLAGS"
2450   IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS=":"
2451   for ac_dir in $ac_dummy; do
2452     IFS="$ac_save_ifs"
2453     if test -z "$ac_dir"
2454     then
2455       CPPFLAGS="$save_CPPFLAGS"
2456     else
2457       CPPFLAGS="-I$ac_dir $save_CPPFLAGS"
2458     fi
2459     AC_TRY_COMPILE([#include <$2>],,ac_found=1;ac_cv_path_$1="$ac_dir";break)
2460   done
2461   CPPFLAGS="$save_CPPFLAGS"
2462   ifelse([$3],,,[if test -z "$ac_found"
2463     then
2464       $3
2465     fi
2466   ])
2467 ])
2468 $1="$ac_cv_path_$1"
2469 if test -n "$ac_found" -o -n "[$]$1"
2470 then
2471   AC_MSG_RESULT([$]$1)
2472 else
2473   AC_MSG_RESULT(no)
2474 fi
2475 AC_SUBST($1)
2476 ])
2477
2478 dnl AC_PATH_LIBRARY(variable,libraries,extra libs,action-if-not-found,default-locations)
2479 AC_DEFUN(AC_PATH_LIBRARY,[
2480 AC_MSG_CHECKING([for $2])
2481 AC_CACHE_VAL(ac_cv_path_$1,
2482 [
2483   ac_found=
2484   ac_dummy="ifelse([$5], , :/usr/local/lib, [$5])"
2485   save_LIBS="$LIBS"
2486   IFS="${IFS=   }"; ac_save_ifs="$IFS"; IFS=":"
2487   for ac_dir in $ac_dummy; do
2488     IFS="$ac_save_ifs"
2489     if test -z "$ac_dir"
2490     then
2491       LIBS="$2 $3 $save_LIBS"
2492     else
2493       LIBS="-L$ac_dir $2 $3 $save_LIBS"
2494     fi
2495     AC_TRY_LINK(,,ac_found=1;ac_cv_path_$1="$ac_dir";break)
2496   done
2497   LIBS="$save_LIBS"
2498   ifelse([$4],,,[if test -z "$ac_found"
2499     then
2500       $4
2501     fi
2502   ])
2503 ])
2504 $1="$ac_cv_path_$1"
2505 if test -n "$ac_found" -o -n "[$]$1"
2506 then
2507   AC_MSG_RESULT([$]$1)
2508 else
2509   AC_MSG_RESULT(no)
2510 fi
2511 AC_SUBST($1)
2512 ])
2513
2514 dnl **** Try to find where winelib is located ****
2515
2516 LD_PATH="";
2517 WINE_INCLUDE_ROOT="";
2518 WINE_INCLUDE_PATH="";
2519 WINE_LIBRARY_ROOT="";
2520 WINE_LIBRARY_PATH="";
2521 WINE_TOOL_PATH="";
2522 WINE="";
2523 WINEBUILD="";
2524 WRC="";
2525
2526 AC_ARG_WITH(wine,
2527 [  --with-wine=DIR           the Wine package (or sources) is in DIR],
2528 [if test "$withval" != "no"; then
2529   WINE_ROOT="$withval";
2530   WINE_INCLUDES="";
2531   WINE_LIBRARIES="";
2532   WINE_TOOLS="";
2533 else
2534   WINE_ROOT="";
2535 fi])
2536 if test -n "$WINE_ROOT"
2537 then
2538   WINE_INCLUDE_ROOT="$WINE_ROOT/include:$WINE_ROOT/include/wine";
2539   WINE_LIBRARY_ROOT="$WINE_ROOT";
2540   WINE_TOOL_PATH="$WINE_ROOT:$WINE_ROOT/bin:$WINE_ROOT/tools/wrc:$WINE_ROOT/tools/winebuild:$PATH";
2541 fi
2542
2543 AC_ARG_WITH(wine-includes,
2544 [  --with-wine-includes=DIR  the Wine includes are in DIR],
2545 [if test "$withval" != "no"; then
2546   WINE_INCLUDES="$withval";
2547 else
2548   WINE_INCLUDES="";
2549 fi])
2550 if test -n "$WINE_INCLUDES"
2551 then
2552   WINE_INCLUDE_ROOT="$WINE_INCLUDES";
2553 fi
2554
2555 AC_ARG_WITH(wine-libraries,
2556 [  --with-wine-libraries=DIR the Wine libraries are in DIR],
2557 [if test "$withval" != "no"; then
2558   WINE_LIBRARIES="$withval";
2559 else
2560   WINE_LIBRARIES="";
2561 fi])
2562 if test -n "$WINE_LIBRARIES"
2563 then
2564   WINE_LIBRARY_ROOT="$WINE_LIBRARIES";
2565 fi
2566
2567 AC_ARG_WITH(wine-tools,
2568 [  --with-wine-tools=DIR     the Wine tools are in DIR],
2569 [if test "$withval" != "no"; then
2570   WINE_TOOLS="$withval";
2571 else
2572   WINE_TOOLS="";
2573 fi])
2574 if test -n "$WINE_TOOLS"
2575 then
2576   WINE_TOOL_PATH="$WINE_TOOLS:$WINE_TOOLS/wrc:$WINE_TOOLS/winebuild";
2577 fi
2578
2579 if test -z "$WINE_INCLUDE_ROOT"
2580 then
2581   WINE_INCLUDE_ROOT=":/usr/include/wine:/usr/local/include/wine:/opt/wine/include:/opt/wine/include/wine";
2582 fi
2583 AC_PATH_HEADER(WINE_INCLUDE_ROOT,windef.h,[
2584   AC_MSG_ERROR([Could not find the Wine includes])
2585 ],$WINE_INCLUDE_ROOT)
2586 if test -n "$WINE_INCLUDE_ROOT"
2587 then
2588   WINE_INCLUDE_PATH="-I$WINE_INCLUDE_ROOT"
2589 else
2590   WINE_INCLUDE_PATH=""
2591 fi
2592
2593 if test -z "$WINE_LIBRARY_ROOT"
2594 then
2595   WINE_LIBRARY_ROOT=":/usr/lib/wine:/usr/local/lib:/usr/local/lib/wine:/opt/wine/lib";
2596 else
2597   WINE_LIBRARY_ROOT="$WINE_LIBRARY_ROOT:$WINE_LIBRARY_ROOT/lib";
2598 fi
2599 AC_PATH_LIBRARY(WINE_LIBRARY_ROOT,[-lwine],[-lutil],[
2600   AC_MSG_ERROR([Could not find the Wine libraries (libwine.so)])
2601 ],$WINE_LIBRARY_ROOT)
2602 if test -n "$WINE_LIBRARY_ROOT"
2603 then
2604   WINE_LIBRARY_PATH="-L$WINE_LIBRARY_ROOT"
2605 else
2606   WINE_LIBRARY_PATH=""
2607 fi
2608 AC_PATH_LIBRARY(LIBNTDLL_PATH,[-lntdll],[$WINE_LIBRARY_PATH -lwine -lwine_unicode -lncurses -ldl -lutil],[
2609   AC_MSG_ERROR([Could not find the Wine libraries (libntdll.so)])
2610 ],[$WINE_LIBRARY_ROOT:$WINE_LIBRARY_ROOT/dlls])
2611 if test -n "$LIBNTDLL_PATH" -a "-L$LIBNTDLL_PATH" != "$WINE_LIBRARY_PATH"
2612 then
2613   WINE_LIBRARY_PATH="$WINE_LIBRARY_PATH -L$LIBNTDLL_PATH"
2614 fi
2615 if test -n "$WINE_LIBRARY_PATH"
2616 then
2617   LD_PATH="LD_LIBRARY_PATH=\"`echo $WINE_LIBRARY_PATH | sed -e 's/ *-L/:/g' -e 's/^://' -e 's/ *$//'`:\$\$LD_LIBRARY_PATH\""
2618 fi
2619
2620 if test -z "$WINE_TOOL_PATH"
2621 then
2622   WINE_TOOL_PATH="$PATH:/usr/local/bin:/opt/wine/bin";
2623 fi
2624 AC_PATH_PROG(WINE,wine,,$WINE_TOOL_PATH)
2625 if test -z "$WINE"
2626 then
2627   AC_MSG_ERROR([Could not find Wine's wine tool])
2628 fi
2629 AC_PATH_PROG(WINEBUILD,winebuild,,$WINE_TOOL_PATH)
2630 if test -z "$WINEBUILD"
2631 then
2632   AC_MSG_ERROR([Could not find Wine's winebuild tool])
2633 fi
2634 AC_PATH_PROG(WRC,wrc,,$WINE_TOOL_PATH)
2635 if test -z "$WRC"
2636 then
2637   AC_MSG_ERROR([Could not find Wine's wrc tool])
2638 fi
2639
2640 AC_SUBST(LD_PATH)
2641 AC_SUBST(WINE_INCLUDE_PATH)
2642 AC_SUBST(WINE_LIBRARY_PATH)
2643
2644 dnl **** Try to find where the MFC are located ****
2645 AC_LANG_CPLUSPLUS()
2646
2647 if test "x$NEEDS_MFC" = "x1"
2648 then
2649   ATL_INCLUDE_ROOT="";
2650   ATL_INCLUDE_PATH="";
2651   MFC_INCLUDE_ROOT="";
2652   MFC_INCLUDE_PATH="";
2653   MFC_LIBRARY_ROOT="";
2654   MFC_LIBRARY_PATH="";
2655
2656   AC_ARG_WITH(mfc,
2657   [  --with-mfc=DIR            the MFC package (or sources) is in DIR],
2658   [if test "$withval" != "no"; then
2659     MFC_ROOT="$withval";
2660     ATL_INCLUDES="";
2661     MFC_INCLUDES="";
2662     MFC_LIBRARIES="";
2663   else
2664     MFC_ROOT="";
2665   fi])
2666   if test -n "$MFC_ROOT"
2667   then
2668     ATL_INCLUDE_ROOT="$MFC_ROOT";
2669     MFC_INCLUDE_ROOT="$MFC_ROOT";
2670     MFC_LIBRARY_ROOT="$MFC_ROOT";
2671   fi
2672
2673   AC_ARG_WITH(atl-includes,
2674   [  --with-atl-includes=DIR   the ATL includes are in DIR],
2675   [if test "$withval" != "no"; then
2676     ATL_INCLUDES="$withval";
2677   else
2678     ATL_INCLUDES="";
2679   fi])
2680   if test -n "$ATL_INCLUDES"
2681   then
2682     ATL_INCLUDE_ROOT="$ATL_INCLUDES";
2683   fi
2684
2685   AC_ARG_WITH(mfc-includes,
2686   [  --with-mfc-includes=DIR   the MFC includes are in DIR],
2687   [if test "$withval" != "no"; then
2688     MFC_INCLUDES="$withval";
2689   else
2690     MFC_INCLUDES="";
2691   fi])
2692   if test -n "$MFC_INCLUDES"
2693   then
2694     MFC_INCLUDE_ROOT="$MFC_INCLUDES";
2695   fi
2696
2697   AC_ARG_WITH(mfc-libraries,
2698   [  --with-mfc-libraries=DIR  the MFC libraries are in DIR],
2699   [if test "$withval" != "no"; then
2700     MFC_LIBRARIES="$withval";
2701   else
2702     MFC_LIBRARIES="";
2703   fi])
2704   if test -n "$MFC_LIBRARIES"
2705   then
2706     MFC_LIBRARY_ROOT="$MFC_LIBRARIES";
2707   fi
2708
2709   OLDCPPFLAGS="$CPPFLAGS"
2710   dnl FIXME: We should not have defines in any of the include paths
2711   CPPFLAGS="$WINE_INCLUDE_PATH -I$WINE_INCLUDE_ROOT/mixedcrt -D_DLL -D_MT $CPPFLAGS"
2712   ATL_INCLUDE_PATH="-I\$(WINE_INCLUDE_ROOT)/mixedcrt -D_DLL -D_MT"
2713   if test -z "$ATL_INCLUDE_ROOT"
2714   then
2715     ATL_INCLUDE_ROOT=":$WINE_INCLUDE_ROOT/atl:/usr/include/atl:/usr/local/include/atl:/opt/mfc/include/atl:/opt/atl/include"
2716   else
2717     ATL_INCLUDE_ROOT="$ATL_INCLUDE_ROOT:$ATL_INCLUDE_ROOT/atl:$ATL_INCLUDE_ROOT/atl/include"
2718   fi
2719   AC_PATH_HEADER(ATL_INCLUDE_ROOT,atldef.h,[
2720     AC_MSG_ERROR([Could not find the ATL includes])
2721   ],$ATL_INCLUDE_ROOT)
2722   if test -n "$ATL_INCLUDE_ROOT"
2723   then
2724     ATL_INCLUDE_PATH="$ATL_INCLUDE_PATH -I$ATL_INCLUDE_ROOT"
2725   fi
2726
2727   MFC_INCLUDE_PATH="$ATL_INCLUDE_PATH"
2728   if test -z "$MFC_INCLUDE_ROOT"
2729   then
2730     MFC_INCLUDE_ROOT=":$WINE_INCLUDE_ROOT/mfc:/usr/include/mfc:/usr/local/include/mfc:/opt/mfc/include/mfc:/opt/mfc/include"
2731   else
2732     MFC_INCLUDE_ROOT="$MFC_INCLUDE_ROOT:$MFC_INCLUDE_ROOT/mfc:$MFC_INCLUDE_ROOT/mfc/include"
2733   fi
2734   AC_PATH_HEADER(MFC_INCLUDE_ROOT,afx.h,[
2735     AC_MSG_ERROR([Could not find the MFC includes])
2736   ],$MFC_INCLUDE_ROOT)
2737   if test -n "$MFC_INCLUDE_ROOT" -a "$ATL_INCLUDE_ROOT" != "$MFC_INCLUDE_ROOT"
2738   then
2739     MFC_INCLUDE_PATH="$MFC_INCLUDE_PATH -I$MFC_INCLUDE_ROOT"
2740   fi
2741   CPPFLAGS="$OLDCPPFLAGS"
2742
2743   if test -z "$MFC_LIBRARY_ROOT"
2744   then
2745     MFC_LIBRARY_ROOT=":$WINE_LIBRARY_ROOT:/usr/lib/mfc:/usr/local/lib:/usr/local/lib/mfc:/opt/mfc/lib";
2746   else
2747     MFC_LIBRARY_ROOT="$MFC_LIBRARY_ROOT:$MFC_LIBRARY_ROOT/lib:$MFC_LIBRARY_ROOT/mfc/src";
2748   fi
2749   AC_PATH_LIBRARY(MFC_LIBRARY_ROOT,[-lmfc],[$WINE_LIBRARY_PATH -lwine -lwine_unicode],[
2750     AC_MSG_ERROR([Could not find the MFC library])
2751   ],$MFC_LIBRARY_ROOT)
2752   if test -n "$MFC_LIBRARY_ROOT" -a "$MFC_LIBRARY_ROOT" != "$WINE_LIBRARY_ROOT"
2753   then
2754     MFC_LIBRARY_PATH="-L$MFC_LIBRARY_ROOT"
2755   else
2756     MFC_LIBRARY_PATH=""
2757   fi
2758
2759   AC_SUBST(ATL_INCLUDE_PATH)
2760   AC_SUBST(MFC_INCLUDE_PATH)
2761   AC_SUBST(MFC_LIBRARY_PATH)
2762 fi
2763
2764 AC_LANG_C()
2765
2766 dnl **** Generate output files ****
2767
2768 MAKE_RULES=Make.rules
2769 AC_SUBST_FILE(MAKE_RULES)
2770
2771 AC_OUTPUT([
2772 Make.rules
2773 ##WINEMAKER_PROJECTS##
2774  ])
2775
2776 echo
2777 echo "Configure finished.  Do 'make' to build the project."
2778 echo
2779
2780 dnl Local Variables:
2781 dnl comment-start: "dnl "
2782 dnl comment-end: ""
2783 dnl comment-start-skip: "\\bdnl\\b\\s *"
2784 dnl compile-command: "autoconf"
2785 dnl End:
2786 --- Make.rules.in ---
2787 # Copyright 2000 Francois Gouget for CodeWeavers
2788 # fgouget@codeweavers.com
2789 #
2790 # Global rules shared by all makefiles     -*-Makefile-*-
2791 #
2792 # Each individual makefile must define the following variables:
2793 # TOPOBJDIR    : top-level object directory
2794 # SRCDIR       : source directory for this module
2795 #
2796 # Each individual makefile may define the following additional variables:
2797 #
2798 # SUBDIRS      : subdirectories that contain a Makefile
2799 # DLLS         : WineLib libraries to be built
2800 # EXES         : WineLib executables to be built
2801 #
2802 # CEXTRA       : extra c flags (e.g. '-Wall')
2803 # CXXEXTRA     : extra c++ flags (e.g. '-Wall')
2804 # WRCEXTRA     : extra wrc flags (e.g. '-p _SysRes')
2805 # DEFINES      : defines (e.g. -DSTRICT)
2806 # INCLUDE_PATH : additional include path
2807 # LIBRARY_PATH : additional library path
2808 # LIBRARIES    : additional Unix libraries to link with
2809 #
2810 # C_SRCS       : C sources for the module
2811 # CXX_SRCS     : C++ sources for the module
2812 # RC_SRCS      : resource source files
2813 # SPEC_SRCS    : interface definition files
2814
2815
2816 # Where is Wine
2817
2818 WINE_INCLUDE_ROOT = @WINE_INCLUDE_ROOT@
2819 WINE_INCLUDE_PATH = @WINE_INCLUDE_PATH@
2820 WINE_LIBRARY_ROOT = @WINE_LIBRARY_ROOT@
2821 WINE_LIBRARY_PATH = @WINE_LIBRARY_PATH@
2822
2823 LD_PATH           = @LD_PATH@
2824
2825 # Where are the MFC
2826
2827 ATL_INCLUDE_ROOT = @ATL_INCLUDE_ROOT@
2828 ATL_INCLUDE_PATH = @ATL_INCLUDE_PATH@
2829 MFC_INCLUDE_ROOT = @MFC_INCLUDE_ROOT@
2830 MFC_INCLUDE_PATH = @MFC_INCLUDE_PATH@
2831 MFC_LIBRARY_ROOT = @MFC_LIBRARY_ROOT@
2832 MFC_LIBRARY_PATH = @MFC_LIBRARY_PATH@
2833
2834 # First some useful definitions
2835
2836 SHELL     = /bin/sh
2837 CC        = @CC@
2838 CPP       = @CPP@
2839 CXX       = @CXX@
2840 WRC       = @WRC@
2841 CFLAGS    = @CFLAGS@
2842 CXXFLAGS  = @CXXFLAGS@
2843 WRCFLAGS  = -r -L
2844 OPTIONS   = @OPTIONS@ -D_REENTRANT -DWINELIB
2845 DLL_LINK  = @DLL_LINK@
2846 LIBS      = @LIBS@ $(LIBRARY_PATH)
2847 LN_S      = @LN_S@
2848 ALLFLAGS  = $(DEFINES) -I$(SRCDIR) $(WINE_INCLUDE_PATH) $(INCLUDE_PATH)
2849 ALLCFLAGS = $(CFLAGS) $(CEXTRA) $(OPTIONS) $(ALLFLAGS)
2850 ALLCXXFLAGS=$(CXXFLAGS) $(CXXEXTRA) $(OPTIONS) $(ALLFLAGS)
2851 ALLWRCFLAGS=$(WRCFLAGS) $(WRCEXTRA) $(OPTIONS) $(ALLFLAGS)
2852 LDCOMBINE = ld -r
2853 LDSHARED  = @LDSHARED@
2854 LDXXSHARED= @LDXXSHARED@
2855 LDDLLFLAGS= @LDDLLFLAGS@
2856 STRIP     = strip
2857 STRIPFLAGS= --strip-unneeded
2858 RM        = rm -f
2859 MV        = mv
2860 MKDIR     = mkdir -p
2861 WINE      = @WINE@
2862 WINEBUILD = @WINEBUILD@
2863 @SET_MAKE@
2864
2865 # Installation infos
2866
2867 INSTALL         = install
2868 INSTALL_PROGRAM = $(INSTALL)
2869 INSTALL_DATA    = $(INSTALL) -m 644
2870 prefix          = @prefix@
2871 exec_prefix     = @exec_prefix@
2872 bindir          = @bindir@
2873 libdir          = @libdir@
2874 infodir         = @infodir@
2875 mandir          = @mandir@
2876 prog_manext     = 1
2877 conf_manext     = 5
2878
2879 OBJS            = $(C_SRCS:.c=.o) $(CXX_SRCS:.cpp=.o) \
2880                   $(SPEC_SRCS:.spec=.spec.o) 
2881 CLEAN_FILES     = *.spec.c y.tab.c y.tab.h lex.yy.c \
2882                   core *.orig *.rej \
2883                   \\\#*\\\# *~ *% .\\\#*
2884
2885 # Implicit rules
2886
2887 .SUFFIXES: .cpp .rc .res .tmp.o .spec .spec.c .spec.o
2888
2889 .c.o:
2890         $(CC) -c $(ALLCFLAGS) -o $@ $<
2891
2892 .cpp.o:
2893         $(CXX) -c $(ALLCXXFLAGS) -o $@ $<
2894
2895 .cxx.o:
2896         $(CXX) -c $(ALLCXXFLAGS) -o $@ $<
2897
2898 .rc.res:
2899         $(LD_PATH) $(WRC) $(ALLWRCFLAGS) -o $@ $<
2900
2901 .PHONY: all install uninstall clean distclean depend dummy
2902
2903 # 'all' target first in case the enclosing Makefile didn't define any target
2904
2905 all: Makefile
2906
2907 # Rules for makefile
2908
2909 Makefile: Makefile.in $(TOPSRCDIR)/configure
2910         @echo Makefile is older than $?, please rerun $(TOPSRCDIR)/configure
2911         @exit 1
2912
2913 # Rules for cleaning
2914
2915 $(SUBDIRS:%=%/__clean__): dummy
2916         cd `dirname $@` && $(MAKE) clean
2917
2918 $(EXTRASUBDIRS:%=%/__clean__): dummy
2919         -cd `dirname $@` && $(RM) $(CLEAN_FILES)
2920
2921 clean:: $(SUBDIRS:%=%/__clean__) $(EXTRASUBDIRS:%=%/__clean__)
2922         $(RM) $(CLEAN_FILES) $(RC_SRCS:.rc=.res) $(OBJS) $(SPEC_SRCS:.spec=.tmp.o) $(EXES) $(EXES:%=%.so) $(DLLS)
2923
2924 # Rules for installing
2925
2926 $(SUBDIRS:%=%/__install__): dummy
2927         cd `dirname $@` && $(MAKE) install
2928
2929 $(SUBDIRS:%=%/__uninstall__): dummy
2930         cd `dirname $@` && $(MAKE) uninstall
2931
2932 # Misc. rules
2933
2934 $(SUBDIRS): dummy
2935         @cd $@ && $(MAKE)
2936
2937 dummy:
2938
2939 # End of global rules
2940 --- wrapper.c ---
2941 /*
2942  * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers
2943  */
2944
2945 #include <dlfcn.h>
2946 #include <windows.h>
2947
2948
2949
2950 /*
2951  * Describe the wrapped application
2952  */
2953
2954 /**
2955  * This is either CUIEXE for a console based application or
2956  * GUIEXE for a regular windows application.
2957  */
2958 #define      APP_TYPE      ##WINEMAKER_APP_TYPE##
2959
2960 /**
2961  * This is the application library's base name, i.e. 'hello' if the 
2962  * library is called 'libhello.so'.
2963  */
2964 static char* appName     = ##WINEMAKER_APP_NAME##;
2965
2966 /**
2967  * This is the name of the application's Windows module. If left NULL 
2968  * then appName is used.
2969  */
2970 static char* appModule   = NULL;
2971
2972 /**
2973  * This is the application's entry point. This is usually "WinMain" for a 
2974  * GUIEXE and 'main' for a CUIEXE application.
2975  */
2976 static char* appInit     = ##WINEMAKER_APP_INIT##;
2977
2978 /**
2979  * This is either non-NULL for MFC-based applications and is the name of the 
2980  * MFC's module. This is the module in which we will take the 'WinMain' 
2981  * function.
2982  */
2983 static char* mfcModule   = ##WINEMAKER_APP_MFC##;
2984
2985
2986
2987 /*
2988  * Implement the main.
2989  */
2990
2991 #if APP_TYPE == GUIEXE
2992 typedef int WINAPI (*WinMainFunc)(HINSTANCE hInstance, HINSTANCE hPrevInstance,
2993                                   PSTR szCmdLine, int iCmdShow);
2994 #else
2995 typedef int WINAPI (*MainFunc)(int argc, char** argv, char** envp);
2996 #endif
2997
2998 #if APP_TYPE == GUIEXE
2999 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
3000                    PSTR szCmdLine, int iCmdShow)
3001 #else
3002 int WINAPI Main(int argc, char** argv, char** envp)
3003 #endif
3004 {
3005     void* appLibrary;
3006     HINSTANCE hApp,hMFC,hMain;
3007     void* appMain;
3008     char* libName;
3009     int retcode;
3010
3011     /* Load the application's library */
3012     libName=(char*)malloc(strlen(appName)+5+3+1);
3013     /* FIXME: we should get the wrapper's path and use that as the base for 
3014      * the library 
3015      */
3016     sprintf(libName,"./lib%s.so",appName);
3017     appLibrary=dlopen(libName,RTLD_NOW);
3018     if (appLibrary==NULL) {
3019         sprintf(libName,"lib%s.so",appName);
3020         appLibrary=dlopen(libName,RTLD_NOW);
3021     }
3022     if (appLibrary==NULL) {
3023         char format[]="Could not load the %s library:\r\n%s";
3024         char* error;
3025         char* msg;
3026
3027         error=dlerror();
3028         msg=(char*)malloc(strlen(format)+strlen(libName)+strlen(error));
3029         sprintf(msg,format,libName,error);
3030         MessageBox(NULL,msg,"dlopen error",MB_OK);
3031         free(msg);
3032         return 1;
3033     }
3034
3035     /* Then if this application is MFC based, load the MFC module */
3036     /* FIXME: I'm not sure this is really necessary */
3037     if (mfcModule!=NULL) {
3038         hMFC=LoadLibrary(mfcModule);
3039         if (hMFC==NULL) {
3040             char format[]="Could not load the MFC module %s (%d)";
3041             char* msg;
3042
3043             msg=(char*)malloc(strlen(format)+strlen(mfcModule)+11);
3044             sprintf(msg,format,mfcModule,GetLastError());
3045             MessageBox(NULL,msg,"LoadLibrary error",MB_OK);
3046             free(msg);
3047             return 1;
3048         }
3049         /* MFC is a special case: the WinMain is in the MFC library, 
3050          * instead of the application's library.
3051          */
3052         hMain=hMFC;
3053     } else {
3054         hMFC=NULL;
3055     }
3056
3057     /* Load the application's module */
3058     if (appModule==NULL) {
3059         appModule=appName;
3060     }
3061     hApp=LoadLibrary(appModule);
3062     if (hApp==NULL) {
3063         char format[]="Could not load the application's module %s (%d)";
3064         char* msg;
3065
3066         msg=(char*)malloc(strlen(format)+strlen(appModule)+11);
3067         sprintf(msg,format,appModule,GetLastError());
3068         MessageBox(NULL,msg,"LoadLibrary error",MB_OK);
3069         free(msg);
3070         return 1;
3071     } else if (hMain==NULL) {
3072         hMain=hApp;
3073     }
3074
3075     /* Get the address of the application's entry point */
3076     appMain=(WinMainFunc*)GetProcAddress(hMain, appInit);
3077     if (appMain==NULL) {
3078         char format[]="Could not get the address of %s (%d)";
3079         char* msg;
3080
3081         msg=(char*)malloc(strlen(format)+strlen(appInit)+11);
3082         sprintf(msg,format,appInit,GetLastError());
3083         MessageBox(NULL,msg,"GetProcAddress error",MB_OK);
3084         free(msg);
3085         return 1;
3086     }
3087
3088     /* And finally invoke the application's entry point */
3089 #if APP_TYPE == GUIEXE
3090     retcode=(*((WinMainFunc)appMain))(hApp,hPrevInstance,szCmdLine,iCmdShow);
3091 #else
3092     retcode=(*((MainFunc)appMain))(argc,argv,envp);
3093 #endif
3094
3095     /* Cleanup and done */
3096     FreeLibrary(hApp);
3097     if (hMFC!=NULL) {
3098         FreeLibrary(hMFC);
3099     }
3100     dlclose(appLibrary);
3101     free(libName);
3102
3103     return retcode;
3104 }