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