4 # Package that encapsulates a Visual C++ project file generation
6 # $PostgreSQL: pgsql/src/tools/msvc/Project.pm,v 1.21 2009/11/12 00:13:00 tgl Exp $
15 my ($junk, $name, $type, $solution) = @_;
21 confess("Bad project type: $type\n") unless exists $good_types->{$type};
25 guid => Win32::GuidGen(),
33 solution => $solution,
34 disablewarnings => '4018;4244;4273;4102;4090',
35 disablelinkerwarnings => ''
44 my ($self, $filename) = @_;
46 $self->{files}->{$filename} = 1;
56 $self->{files}->{$dir . "\\" . $f} = 1;
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;
78 elsif ($file =~ m/($re)/)
80 delete $self->{files}{$file};
81 $self->{files}{"$newname\\$filename"} = 1;
85 confess("Could not find file $filename to replace\n");
90 my ($self, $filename) = @_;
91 my $orig = scalar keys %{ $self->{files} };
92 delete $self->{files}->{$filename};
93 if ($orig > scalar keys %{$self->{files}} )
97 confess("Could not find file $filename to remove\n");
102 my ($self, $targetdir, $proc) = @_;
103 foreach my $f (keys %{$self->{files}}) {
106 $self->RemoveFile($f);
107 $self->AddFile($targetdir . '\\' . basename($f));
116 while (my $ref = shift)
118 push @{$self->{references}},$ref;
119 $self->AddLibrary("__CFGNAME__\\" . $ref->{name} . "\\" . $ref->{name} . ".lib");
125 my ($self, $lib, $dbgsuffix) = @_;
129 $lib = '"' . $lib . """;
132 push @{$self->{libraries}}, $lib;
135 push @{$self->{suffixlib}}, $lib;
141 my ($self, $inc) = @_;
143 if ($self->{includes} ne '')
145 $self->{includes} .= ';';
147 $self->{includes} .= $inc;
152 my ($self, $inc) = @_;
154 $self->{prefixincludes} = $inc . ';' . $self->{prefixincludes};
159 my ($self, $def) = @_;
161 $def =~ s/"/""/g;
162 $self->{defines} .= $def . ';';
167 my ($self, $libname) = @_;
169 $self->{builddef} = 1;
170 $self->{def} = ".\\__CFGNAME__\\$self->{name}\\$self->{name}.def";
171 $self->{implib} = "__CFGNAME__\\$self->{name}\\$libname";
176 my ($self, $def) = @_;
183 my ($self, $reldir) = @_;
188 open($MF,"$reldir\\Makefile")
189 || open($MF,"$reldir\\GNUMakefile")
190 || croak "Could not open $reldir\\Makefile\n";
194 $mf =~ s{\\\s*[\r\n]+}{}mg;
195 if ($mf =~ m{^(?:SUB)?DIRS[^=]*=\s*(.*)$}mg)
197 foreach my $subdir (split /\s+/,$1)
200 if $subdir eq "\$(top_builddir)/src/timezone"; #special case for non-standard include
202 if $reldir . "\\" . $subdir eq "src\\backend\\port\\darwin";
204 $self->AddDir($reldir . "\\" . $subdir);
207 while ($mf =~ m{^(?:EXTRA_)?OBJS[^=]*=\s*(.*)$}m)
210 my $filter_re = qr{\$\(filter ([^,]+),\s+\$\(([^\)]+)\)\)};
211 while ($s =~ /$filter_re/)
214 # Process $(filter a b c, $(VAR)) expressions
217 $list =~ s/\.o/\.c/g;
218 my @pieces = split /\s+/, $list;
220 foreach my $p (@pieces)
223 if ($filter eq "LIBOBJS")
225 if (grep(/$p/, @main::pgportfiles) == 1)
228 $matches .= $p . " ";
233 confess "Unknown filter $filter\n";
236 $s =~ s/$filter_re/$matches/;
238 foreach my $f (split /\s+/,$s)
240 next if $f =~ /^\s*$/;
242 next if $f =~ /\/SUBSYS.o$/;
243 $f =~ s/,$//; # Remove trailing comma that can show up from filter stuff
244 next unless $f =~ /.*\.o$/;
246 if ($f =~ /^\$\(top_builddir\)\/(.*)/)
250 $self->{files}->{$f} = 1;
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)
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") };
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)
294 if ($mf =~ /^PGAPPICON\s*=\s*(.*)$/m) { $ico = $1; }
295 $self->AddResourceFile($reldir,$desc,$ico);
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\"":"";
315 s/FILEDESC/"$desc"/gm;
317 s/(VERSION.*),0/$1,$d/;
318 if ($self->{type} eq "dll")
320 s/VFT_APP/VFT_DLL/gm;
327 $self->AddFile("$dir\\win32ver.rc");
330 sub DisableLinkerWarnings
332 my ($self, $warnings) = @_;
334 $self->{disablelinkerwarnings} .= ';' unless ($self->{disablelinkerwarnings} eq '');
335 $self->{disablelinkerwarnings} .= $warnings;
342 # If doing DLL and haven't specified a DEF file, do a full export of all symbols
344 if ($self->{type} eq "dll" && !$self->{def})
346 $self->FullExportDLL($self->{name} . ".lib");
350 open(F, ">$self->{name}.vcproj") || croak("Could not write to $self->{name}.vcproj\n");
351 $self->WriteHeader(*F);
352 $self->WriteReferences(*F);
358 foreach my $f (sort keys %{ $self->{files} })
360 confess "Bad format filename '$f'\n" unless ($f =~ /^(.*)\\([^\\]+)\.[r]?[cyl]$/);
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";
376 # Now walk forwards and create whatever directories are needed
377 while (join('\\',@dirstack) ne $dir)
379 my $left = substr($dir, length(join('\\',@dirstack)));
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\"";
391 $of =~ s{^src\\pl\\plpgsql\\src\\gram.c$}{src\\pl\\plpgsql\\src\\pl_gram.c};
393 . GenerateCustomTool('Running bison on ' . $f,
394 'cmd /V:ON /c src\tools\msvc\pgbison.bat ' . $f, $of)
402 . GenerateCustomTool('Running flex on ' . $f, 'src\tools\msvc\pgflex.bat ' . $f,$of)
405 elsif (defined($uniquefiles{$file}))
408 # File already exists, so fake a new name
412 "><FileConfiguration Name=\"Debug|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\debug\\$self->{name}\\$obj"
413 . "_$file.obj\" /></FileConfiguration><FileConfiguration Name=\"Release|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\release\\$self->{name}\\$obj"
414 . "_$file.obj\" /></FileConfiguration></File>\n";
418 $uniquefiles{$file} = 1;
422 while ($#dirstack >= 0)
424 print F ' ' x $#dirstack . " </Filter>\n";
431 sub GenerateCustomTool
433 my ($desc, $tool, $output, $cfg) = @_;
436 return GenerateCustomTool($desc, $tool, $output, 'Debug')
437 .GenerateCustomTool($desc, $tool, $output, 'Release');
440 "<FileConfiguration Name=\"$cfg|Win32\"><Tool Name=\"VCCustomBuildTool\" Description=\"$desc\" CommandLine=\"$tool\" AdditionalDependencies=\"\" Outputs=\"$output\" /></FileConfiguration>";
446 print $f " <References>\n";
447 foreach my $ref (@{$self->{references}})
450 " <ProjectReference ReferencedProjectIdentifier=\"$ref->{guid}\" Name=\"$ref->{name}\" />\n";
452 print $f " </References>\n";
460 <?xml version="1.0" encoding="Windows-1252"?>
461 <VisualStudioProject ProjectType="Visual C++" Version="8.00" Name="$self->{name}" ProjectGUID="$self->{guid}">
462 <Platforms><Platform Name="Win32"/></Platforms>
465 $self->WriteConfiguration($f, 'Debug',
466 { defs=>'_DEBUG;DEBUG=1;', wholeopt=>0, opt=>0, strpool=>'false', runtime=>3 });
467 $self->WriteConfiguration($f, 'Release',
468 { defs=>'', wholeopt=>0, opt=>3, strpool=>'true', runtime=>2 });
474 sub WriteConfiguration
476 my ($self, $f, $cfgname, $p) = @_;
477 my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4);
478 my $libcfg = (uc $cfgname eq "RELEASE")?"MD":"MDd";
480 foreach my $lib (@{$self->{libraries}})
483 foreach my $slib (@{$self->{suffixlib}})
487 $xlib =~ s/\.lib$/$libcfg.lib/;
491 $libs .= $xlib . " ";
494 $libs =~ s/__CFGNAME__/$cfgname/g;
496 <Configuration Name="$cfgname|Win32" OutputDirectory=".\\$cfgname\\$self->{name}" IntermediateDirectory=".\\$cfgname\\$self->{name}"
497 ConfigurationType="$cfgtype" UseOfMFC="0" ATLMinimizesCRunTimeLibraryUsage="FALSE" CharacterSet="2" WholeProgramOptimization="$p->{wholeopt}">
498 <Tool Name="VCCLCompilerTool" Optimization="$p->{opt}"
499 AdditionalIncludeDirectories="$self->{prefixincludes}src/include;src/include/port/win32;src/include/port/win32_msvc;$self->{includes}"
500 PreprocessorDefinitions="WIN32;_WINDOWS;__WINDOWS__;__WIN32__;EXEC_BACKEND;WIN32_STACK_RLIMIT=4194304;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE$self->{defines}$p->{defs}"
501 StringPooling="$p->{strpool}"
502 RuntimeLibrary="$p->{runtime}" DisableSpecificWarnings="$self->{disablewarnings}"
503 AdditionalOptions="/MP"
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";
523 my $l = $self->{implib};
524 $l =~ s/__CFGNAME__/$cfgname/g;
525 print $f "\t\tImportLibrary=\"$l\"\n";
529 my $d = $self->{def};
530 $d =~ s/__CFGNAME__/$cfgname/g;
531 print $f "\t\tModuleDefinitionFile=\"$d\"\n";
536 "\t<Tool Name=\"VCLibrarianTool\" OutputFile=\".\\$cfgname\\$self->{name}\\$self->{name}.lib\" IgnoreDefaultLibraryNames=\"libc\" />\n";
538 "\t<Tool Name=\"VCResourceCompilerTool\" AdditionalIncludeDirectories=\"src\\include\" />\n";
539 if ($self->{builddef})
542 "\t<Tool Name=\"VCPreLinkEventTool\" Description=\"Generate DEF file\" CommandLine=\"perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name}\" />\n";
556 </VisualStudioProject>
560 # Utility function that loads a complete file
563 my $filename = shift;
568 open($F, $filename) || croak "Could not open file $filename\n";