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