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