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