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