Fix (hopefully) some oversights in recent Bison cleanup patch.
[PostgreSQL.git] / src / tools / msvc / Project.pm
blob22beaf720f25d22327170adc847867e8af709cb5
1 package Project;
4 # Package that encapsulates a Visual C++ project file generation
6 # $PostgreSQL$
8 use Carp;
9 use strict;
10 use warnings;
11 use File::Basename;
13 sub new
15 my ($junk, $name, $type, $solution) = @_;
16 my $good_types = {
17 lib => 1,
18 exe => 1,
19 dll => 1,
21 confess("Bad project type: $type\n") unless exists $good_types->{$type};
22 my $self = {
23 name => $name,
24 type => $type,
25 guid => Win32::GuidGen(),
26 files => {},
27 references => [],
28 libraries => [],
29 suffixlib => [],
30 includes => '',
31 prefixincludes => '',
32 defines => ';',
33 solution => $solution,
34 disablewarnings => '4018;4244;4273;4102;4090',
35 disablelinkerwarnings => ''
38 bless $self;
39 return $self;
42 sub AddFile
44 my ($self, $filename) = @_;
46 $self->{files}->{$filename} = 1;
49 sub AddFiles
51 my $self = shift;
52 my $dir = shift;
54 while (my $f = shift)
56 $self->{files}->{$dir . "\\" . $f} = 1;
60 sub ReplaceFile
62 my ($self, $filename, $newname) = @_;
63 my $re = "\\\\$filename\$";
65 foreach my $file ( keys %{ $self->{files} } )
68 # Match complete filename
69 if ($filename =~ /\\/)
71 if ($file eq $filename)
73 delete $self->{files}{$file};
74 $self->{files}{$newname} = 1;
75 return;
78 elsif ($file =~ m/($re)/)
80 delete $self->{files}{$file};
81 $self->{files}{"$newname\\$filename"} = 1;
82 return;
85 confess("Could not find file $filename to replace\n");
88 sub RemoveFile
90 my ($self, $filename) = @_;
91 my $orig = scalar keys %{ $self->{files} };
92 delete $self->{files}->{$filename};
93 if ($orig > scalar keys %{$self->{files}} )
95 return;
97 confess("Could not find file $filename to remove\n");
100 sub RelocateFiles
102 my ($self, $targetdir, $proc) = @_;
103 foreach my $f (keys %{$self->{files}}) {
104 my $r = &$proc($f);
105 if ($r) {
106 $self->RemoveFile($f);
107 $self->AddFile($targetdir . '\\' . basename($f));
112 sub AddReference
114 my $self = shift;
116 while (my $ref = shift)
118 push @{$self->{references}},$ref;
119 $self->AddLibrary("__CFGNAME__\\" . $ref->{name} . "\\" . $ref->{name} . ".lib");
123 sub AddLibrary
125 my ($self, $lib, $dbgsuffix) = @_;
127 if ($lib =~ m/\s/)
129 $lib = '"' . $lib . """;
132 push @{$self->{libraries}}, $lib;
133 if ($dbgsuffix)
135 push @{$self->{suffixlib}}, $lib;
139 sub AddIncludeDir
141 my ($self, $inc) = @_;
143 if ($self->{includes} ne '')
145 $self->{includes} .= ';';
147 $self->{includes} .= $inc;
150 sub AddPrefixInclude
152 my ($self, $inc) = @_;
154 $self->{prefixincludes} = $inc . ';' . $self->{prefixincludes};
157 sub AddDefine
159 my ($self, $def) = @_;
161 $def =~ s/"/""/g;
162 $self->{defines} .= $def . ';';
165 sub FullExportDLL
167 my ($self, $libname) = @_;
169 $self->{builddef} = 1;
170 $self->{def} = ".\\__CFGNAME__\\$self->{name}\\$self->{name}.def";
171 $self->{implib} = "__CFGNAME__\\$self->{name}\\$libname";
174 sub UseDef
176 my ($self, $def) = @_;
178 $self->{def} = $def;
181 sub AddDir
183 my ($self, $reldir) = @_;
184 my $MF;
186 my $t = $/;
187 undef $/;
188 open($MF,"$reldir\\Makefile")
189 || open($MF,"$reldir\\GNUMakefile")
190 || croak "Could not open $reldir\\Makefile\n";
191 my $mf = <$MF>;
192 close($MF);
194 $mf =~ s{\\\s*[\r\n]+}{}mg;
195 if ($mf =~ m{^(?:SUB)?DIRS[^=]*=\s*(.*)$}mg)
197 foreach my $subdir (split /\s+/,$1)
199 next
200 if $subdir eq "\$(top_builddir)/src/timezone"; #special case for non-standard include
201 next
202 if $reldir . "\\" . $subdir eq "src\\backend\\port\\darwin";
204 $self->AddDir($reldir . "\\" . $subdir);
207 while ($mf =~ m{^(?:EXTRA_)?OBJS[^=]*=\s*(.*)$}m)
209 my $s = $1;
210 my $filter_re = qr{\$\(filter ([^,]+),\s+\$\(([^\)]+)\)\)};
211 while ($s =~ /$filter_re/)
214 # Process $(filter a b c, $(VAR)) expressions
215 my $list = $1;
216 my $filter = $2;
217 $list =~ s/\.o/\.c/g;
218 my @pieces = split /\s+/, $list;
219 my $matches = "";
220 foreach my $p (@pieces)
223 if ($filter eq "LIBOBJS")
225 if (grep(/$p/, @main::pgportfiles) == 1)
227 $p =~ s/\.c/\.o/;
228 $matches .= $p . " ";
231 else
233 confess "Unknown filter $filter\n";
236 $s =~ s/$filter_re/$matches/;
238 foreach my $f (split /\s+/,$s)
240 next if $f =~ /^\s*$/;
241 next if $f eq "\\";
242 next if $f =~ /\/SUBSYS.o$/;
243 $f =~ s/,$//; # Remove trailing comma that can show up from filter stuff
244 next unless $f =~ /.*\.o$/;
245 $f =~ s/\.o$/\.c/;
246 if ($f =~ /^\$\(top_builddir\)\/(.*)/)
248 $f = $1;
249 $f =~ s/\//\\/g;
250 $self->{files}->{$f} = 1;
252 else
254 $f =~ s/\//\\/g;
255 $self->{files}->{"$reldir\\$f"} = 1;
258 $mf =~ s{OBJS[^=]*=\s*(.*)$}{}m;
261 # Match rules that pull in source files from different directories
262 # example: pg_crc.c: $(top_srcdir)/src/backend/utils/hash/pg_crc.c
263 my $replace_re = qr{^([^:\n\$]+\.c)\s*:\s*(?:%\s*: )?\$(\([^\)]+\))\/(.*)\/[^\/]+$}m;
264 while ($mf =~ m{$replace_re}m)
266 my $match = $1;
267 my $top = $2;
268 my $target = $3;
269 $target =~ s{/}{\\}g;
270 my @pieces = split /\s+/,$match;
271 foreach my $fn (@pieces)
273 if ($top eq "(top_srcdir)")
275 eval { $self->ReplaceFile($fn, $target) };
277 elsif ($top eq "(backend_src)")
279 eval { $self->ReplaceFile($fn, "src\\backend\\$target") };
281 else
283 confess "Bad replacement top: $top, on line $_\n";
286 $mf =~ s{$replace_re}{}m;
289 # See if this Makefile contains a description, and should have a RC file
290 if ($mf =~ /^PGFILEDESC\s*=\s*\"([^\"]+)\"/m)
292 my $desc = $1;
293 my $ico;
294 if ($mf =~ /^PGAPPICON\s*=\s*(.*)$/m) { $ico = $1; }
295 $self->AddResourceFile($reldir,$desc,$ico);
297 $/ = $t;
300 sub AddResourceFile
302 my ($self, $dir, $desc, $ico) = @_;
304 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
305 my $d = ($year - 100) . "$yday";
307 if (Solution::IsNewer("$dir\\win32ver.rc",'src\port\win32ver.rc'))
309 print "Generating win32ver.rc for $dir\n";
310 open(I,'src\port\win32ver.rc') || confess "Could not open win32ver.rc";
311 open(O,">$dir\\win32ver.rc") || confess "Could not write win32ver.rc";
312 my $icostr = $ico?"IDI_ICON ICON \"src/port/$ico.ico\"":"";
313 while (<I>)
315 s/FILEDESC/"$desc"/gm;
316 s/_ICO_/$icostr/gm;
317 s/(VERSION.*),0/$1,$d/;
318 if ($self->{type} eq "dll")
320 s/VFT_APP/VFT_DLL/gm;
322 print O;
325 close(O);
326 close(I);
327 $self->AddFile("$dir\\win32ver.rc");
330 sub DisableLinkerWarnings
332 my ($self, $warnings) = @_;
334 $self->{disablelinkerwarnings} .= ';' unless ($self->{disablelinkerwarnings} eq '');
335 $self->{disablelinkerwarnings} .= $warnings;
338 sub Save
340 my ($self) = @_;
342 # If doing DLL and haven't specified a DEF file, do a full export of all symbols
343 # in the project.
344 if ($self->{type} eq "dll" && !$self->{def})
346 $self->FullExportDLL($self->{name} . ".lib");
349 # Dump the project
350 open(F, ">$self->{name}.vcproj") || croak("Could not write to $self->{name}.vcproj\n");
351 $self->WriteHeader(*F);
352 $self->WriteReferences(*F);
353 print F <<EOF;
354 <Files>
356 my @dirstack = ();
357 my %uniquefiles;
358 foreach my $f (sort keys %{ $self->{files} })
360 confess "Bad format filename '$f'\n" unless ($f =~ /^(.*)\\([^\\]+)\.[r]?[cyl]$/);
361 my $dir = $1;
362 my $file = $2;
364 # Walk backwards down the directory stack and close any dirs we're done with
365 while ($#dirstack >= 0)
367 if (join('\\',@dirstack) eq substr($dir, 0, length(join('\\',@dirstack))))
369 last if (length($dir) == length(join('\\',@dirstack)));
370 last if (substr($dir, length(join('\\',@dirstack)),1) eq '\\');
372 print F ' ' x $#dirstack . " </Filter>\n";
373 pop @dirstack;
376 # Now walk forwards and create whatever directories are needed
377 while (join('\\',@dirstack) ne $dir)
379 my $left = substr($dir, length(join('\\',@dirstack)));
380 $left =~ s/^\\//;
381 my @pieces = split /\\/, $left;
382 push @dirstack, $pieces[0];
383 print F ' ' x $#dirstack . " <Filter Name=\"$pieces[0]\" Filter=\"\">\n";
386 print F ' ' x $#dirstack . " <File RelativePath=\"$f\"";
387 if ($f =~ /\.y$/)
389 my $of = $f;
390 $of =~ s/\.y$/.c/;
391 $of =~ s{^src\\pl\\plpgsql\\src\\gram.c$}{src\\pl\\plpgsql\\src\\pl_gram.c};
392 print F '>'
393 . GenerateCustomTool('Running bison on ' . $f,
394 'cmd /V:ON /c src\tools\msvc\pgbison.bat ' . $f, $of)
395 . '</File>' . "\n";
397 elsif ($f =~ /\.l$/)
399 my $of = $f;
400 $of =~ s/\.l$/.c/;
401 $of =~ s{^src\\pl\\plpgsql\\src\\scan.c$}{src\\pl\\plpgsql\\src\\pl_scan.c};
402 print F '>'
403 . GenerateCustomTool('Running flex on ' . $f, 'src\tools\msvc\pgflex.bat ' . $f,$of)
404 . '</File>' . "\n";
406 elsif (defined($uniquefiles{$file}))
409 # File already exists, so fake a new name
410 my $obj = $dir;
411 $obj =~ s/\\/_/g;
412 print F
413 "><FileConfiguration Name=\"Debug|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\debug\\$self->{name}\\$obj"
414 . "_$file.obj\" /></FileConfiguration><FileConfiguration Name=\"Release|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\release\\$self->{name}\\$obj"
415 . "_$file.obj\" /></FileConfiguration></File>\n";
417 else
419 $uniquefiles{$file} = 1;
420 print F " />\n";
423 while ($#dirstack >= 0)
425 print F ' ' x $#dirstack . " </Filter>\n";
426 pop @dirstack;
428 $self->Footer(*F);
429 close(F);
432 sub GenerateCustomTool
434 my ($desc, $tool, $output, $cfg) = @_;
435 if (!defined($cfg))
437 return GenerateCustomTool($desc, $tool, $output, 'Debug')
438 .GenerateCustomTool($desc, $tool, $output, 'Release');
440 return
441 "<FileConfiguration Name=\"$cfg|Win32\"><Tool Name=\"VCCustomBuildTool\" Description=\"$desc\" CommandLine=\"$tool\" AdditionalDependencies=\"\" Outputs=\"$output\" /></FileConfiguration>";
444 sub WriteReferences
446 my ($self, $f) = @_;
447 print $f " <References>\n";
448 foreach my $ref (@{$self->{references}})
450 print $f
451 " <ProjectReference ReferencedProjectIdentifier=\"$ref->{guid}\" Name=\"$ref->{name}\" />\n";
453 print $f " </References>\n";
456 sub WriteHeader
458 my ($self, $f) = @_;
460 print $f <<EOF;
461 <?xml version="1.0" encoding="Windows-1252"?>
462 <VisualStudioProject ProjectType="Visual C++" Version="8.00" Name="$self->{name}" ProjectGUID="$self->{guid}">
463 <Platforms><Platform Name="Win32"/></Platforms>
464 <Configurations>
466 $self->WriteConfiguration($f, 'Debug',
467 { defs=>'_DEBUG;DEBUG=1;', wholeopt=>0, opt=>0, strpool=>'false', runtime=>3 });
468 $self->WriteConfiguration($f, 'Release',
469 { defs=>'', wholeopt=>0, opt=>3, strpool=>'true', runtime=>2 });
470 print $f <<EOF;
471 </Configurations>
475 sub WriteConfiguration
477 my ($self, $f, $cfgname, $p) = @_;
478 my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4);
479 my $libcfg = (uc $cfgname eq "RELEASE")?"MD":"MDd";
480 my $libs = '';
481 foreach my $lib (@{$self->{libraries}})
483 my $xlib = $lib;
484 foreach my $slib (@{$self->{suffixlib}})
486 if ($slib eq $lib)
488 $xlib =~ s/\.lib$/$libcfg.lib/;
489 last;
492 $libs .= $xlib . " ";
494 $libs =~ s/ $//;
495 $libs =~ s/__CFGNAME__/$cfgname/g;
496 print $f <<EOF;
497 <Configuration Name="$cfgname|Win32" OutputDirectory=".\\$cfgname\\$self->{name}" IntermediateDirectory=".\\$cfgname\\$self->{name}"
498 ConfigurationType="$cfgtype" UseOfMFC="0" ATLMinimizesCRunTimeLibraryUsage="FALSE" CharacterSet="2" WholeProgramOptimization="$p->{wholeopt}">
499 <Tool Name="VCCLCompilerTool" Optimization="$p->{opt}"
500 AdditionalIncludeDirectories="$self->{prefixincludes}src/include;src/include/port/win32;src/include/port/win32_msvc;$self->{includes}"
501 PreprocessorDefinitions="WIN32;_WINDOWS;__WINDOWS__;__WIN32__;EXEC_BACKEND;WIN32_STACK_RLIMIT=4194304;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE$self->{defines}$p->{defs}"
502 StringPooling="$p->{strpool}"
503 RuntimeLibrary="$p->{runtime}" DisableSpecificWarnings="$self->{disablewarnings}"
505 print $f <<EOF;
506 AssemblerOutput="0" AssemblerListingLocation=".\\$cfgname\\$self->{name}\\" ObjectFile=".\\$cfgname\\$self->{name}\\"
507 ProgramDataBaseFileName=".\\$cfgname\\$self->{name}\\" BrowseInformation="0"
508 WarningLevel="3" SuppressStartupBanner="TRUE" DebugInformationFormat="3" CompileAs="0"/>
509 <Tool Name="VCLinkerTool" OutputFile=".\\$cfgname\\$self->{name}\\$self->{name}.$self->{type}"
510 AdditionalDependencies="$libs"
511 LinkIncremental="0" SuppressStartupBanner="TRUE" AdditionalLibraryDirectories="" IgnoreDefaultLibraryNames="libc"
512 StackReserveSize="4194304" DisableSpecificWarnings="$self->{disablewarnings}"
513 GenerateDebugInformation="TRUE" ProgramDatabaseFile=".\\$cfgname\\$self->{name}\\$self->{name}.pdb"
514 GenerateMapFile="FALSE" MapFileName=".\\$cfgname\\$self->{name}\\$self->{name}.map"
515 SubSystem="1" TargetMachine="1"
517 if ($self->{disablelinkerwarnings})
519 print $f "\t\tAdditionalOptions=\"/ignore:$self->{disablelinkerwarnings}\"\n";
521 if ($self->{implib})
523 my $l = $self->{implib};
524 $l =~ s/__CFGNAME__/$cfgname/g;
525 print $f "\t\tImportLibrary=\"$l\"\n";
527 if ($self->{def})
529 my $d = $self->{def};
530 $d =~ s/__CFGNAME__/$cfgname/g;
531 print $f "\t\tModuleDefinitionFile=\"$d\"\n";
534 print $f "\t/>\n";
535 print $f
536 "\t<Tool Name=\"VCLibrarianTool\" OutputFile=\".\\$cfgname\\$self->{name}\\$self->{name}.lib\" IgnoreDefaultLibraryNames=\"libc\" />\n";
537 print $f
538 "\t<Tool Name=\"VCResourceCompilerTool\" AdditionalIncludeDirectories=\"src\\include\" />\n";
539 if ($self->{builddef})
541 print $f
542 "\t<Tool Name=\"VCPreLinkEventTool\" Description=\"Generate DEF file\" CommandLine=\"perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name}\" />\n";
544 print $f <<EOF;
545 </Configuration>
549 sub Footer
551 my ($self, $f) = @_;
553 print $f <<EOF;
554 </Files>
555 <Globals/>
556 </VisualStudioProject>
560 # Utility function that loads a complete file
561 sub read_file
563 my $filename = shift;
564 my $F;
565 my $t = $/;
567 undef $/;
568 open($F, $filename) || croak "Could not open file $filename\n";
569 my $txt = <$F>;
570 close($F);
571 $/ = $t;
573 return $txt;