Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmLocalVisualStudio7Generator.cxx
blobafd79d7eef7efab02c3b5690fdc51017bee245d5
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalVisualStudio7Generator.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-28 17:02:29 $
7 Version: $Revision: 1.238 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmGlobalVisualStudio7Generator.h"
18 #include "cmLocalVisualStudio7Generator.h"
19 #include "cmXMLParser.h"
20 #include <cm_expat.h>
21 #include "cmMakefile.h"
22 #include "cmSystemTools.h"
23 #include "cmSourceFile.h"
24 #include "cmCacheManager.h"
25 #include "cmake.h"
27 #include "cmComputeLinkInformation.h"
28 #include "cmGeneratedFileStream.h"
30 #include <cmsys/System.h>
32 #include <ctype.h> // for isspace
34 static bool cmLVS6G_IsFAT(const char* dir);
36 class cmLocalVisualStudio7GeneratorInternals
38 public:
39 cmLocalVisualStudio7GeneratorInternals(cmLocalVisualStudio7Generator* e):
40 LocalGenerator(e) {}
41 typedef cmComputeLinkInformation::ItemVector ItemVector;
42 void OutputLibraries(std::ostream& fout, ItemVector const& libs);
43 private:
44 cmLocalVisualStudio7Generator* LocalGenerator;
47 extern cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[];
49 //----------------------------------------------------------------------------
50 cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator()
52 this->Version = 7;
53 this->PlatformName = "Win32";
54 this->ExtraFlagTable = 0;
55 this->Internal = new cmLocalVisualStudio7GeneratorInternals(this);
58 cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
60 delete this->Internal;
63 void cmLocalVisualStudio7Generator::AddHelperCommands()
65 std::set<cmStdString> lang;
66 lang.insert("C");
67 lang.insert("CXX");
68 lang.insert("RC");
69 lang.insert("IDL");
70 lang.insert("DEF");
71 lang.insert("Fortran");
72 this->CreateCustomTargetsAndCommands(lang);
73 this->FixGlobalTargets();
76 void cmLocalVisualStudio7Generator::Generate()
78 this->WriteProjectFiles();
79 this->WriteStampFiles();
82 void cmLocalVisualStudio7Generator::FixGlobalTargets()
84 // Visual Studio .NET 2003 Service Pack 1 will not run post-build
85 // commands for targets in which no sources are built. Add dummy
86 // rules to force these targets to build.
87 cmTargets &tgts = this->Makefile->GetTargets();
88 for(cmTargets::iterator l = tgts.begin();
89 l != tgts.end(); l++)
91 cmTarget& tgt = l->second;
92 if(tgt.GetType() == cmTarget::GLOBAL_TARGET)
94 std::vector<std::string> no_depends;
95 cmCustomCommandLine force_command;
96 force_command.push_back("cd");
97 force_command.push_back(".");
98 cmCustomCommandLines force_commands;
99 force_commands.push_back(force_command);
100 const char* no_main_dependency = 0;
101 std::string force = this->Makefile->GetStartOutputDirectory();
102 force += cmake::GetCMakeFilesDirectory();
103 force += "/";
104 force += tgt.GetName();
105 force += "_force";
106 this->Makefile->AddCustomCommandToOutput(force.c_str(), no_depends,
107 no_main_dependency,
108 force_commands, " ", 0, true);
109 if(cmSourceFile* file =
110 this->Makefile->GetSourceFileWithOutput(force.c_str()))
112 tgt.AddSourceFile(file);
118 // TODO
119 // for CommandLine= need to repleace quotes with &quot
120 // write out configurations
121 void cmLocalVisualStudio7Generator::WriteProjectFiles()
123 // If not an in source build, then create the output directory
124 if(strcmp(this->Makefile->GetStartOutputDirectory(),
125 this->Makefile->GetHomeDirectory()) != 0)
127 if(!cmSystemTools::MakeDirectory
128 (this->Makefile->GetStartOutputDirectory()))
130 cmSystemTools::Error("Error creating directory ",
131 this->Makefile->GetStartOutputDirectory());
135 // Get the set of targets in this directory.
136 cmTargets &tgts = this->Makefile->GetTargets();
138 // Create the regeneration custom rule.
139 if(!this->Makefile->IsOn("CMAKE_SUPPRESS_REGENERATION"))
141 // Create a rule to regenerate the build system when the target
142 // specification source changes.
143 if(cmSourceFile* sf = this->CreateVCProjBuildRule())
145 // Add the rule to targets that need it.
146 for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
148 if(l->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
150 l->second.AddSourceFile(sf);
156 // Create the project file for each target.
157 for(cmTargets::iterator l = tgts.begin();
158 l != tgts.end(); l++)
160 // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
161 // so don't build a projectfile for it
162 if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
164 this->CreateSingleVCProj(l->first.c_str(),l->second);
169 //----------------------------------------------------------------------------
170 void cmLocalVisualStudio7Generator::WriteStampFiles()
172 // Touch a timestamp file used to determine when the project file is
173 // out of date.
174 std::string stampName = this->Makefile->GetStartOutputDirectory();
175 stampName += cmake::GetCMakeFilesDirectory();
176 cmSystemTools::MakeDirectory(stampName.c_str());
177 stampName += "/";
178 stampName += "generate.stamp";
179 std::ofstream stamp(stampName.c_str());
180 stamp << "# CMake generation timestamp file this directory.\n";
182 // Create a helper file so CMake can determine when it is run
183 // through the rule created by CreateVCProjBuildRule whether it
184 // really needs to regenerate the project. This file lists its own
185 // dependencies. If any file listed in it is newer than itself then
186 // CMake must rerun. Otherwise the project files are up to date and
187 // the stamp file can just be touched.
188 std::string depName = stampName;
189 depName += ".depend";
190 std::ofstream depFile(depName.c_str());
191 depFile << "# CMake generation dependency list for this directory.\n";
192 std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
193 for(std::vector<std::string>::const_iterator lf = listFiles.begin();
194 lf != listFiles.end(); ++lf)
196 depFile << *lf << std::endl;
200 //----------------------------------------------------------------------------
201 void cmLocalVisualStudio7Generator
202 ::CreateSingleVCProj(const char *lname, cmTarget &target)
204 this->FortranProject =
205 static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
206 ->TargetIsFortranOnly(target);
207 // add to the list of projects
208 std::string pname = lname;
209 target.SetProperty("GENERATOR_FILE_NAME",lname);
210 // create the dsp.cmake file
211 std::string fname;
212 fname = this->Makefile->GetStartOutputDirectory();
213 fname += "/";
214 fname += lname;
215 if(this->FortranProject)
217 fname += ".vfproj";
219 else
221 fname += ".vcproj";
224 // Generate the project file and replace it atomically with
225 // copy-if-different. We use a separate timestamp so that the IDE
226 // does not reload project files unnecessarily.
227 cmGeneratedFileStream fout(fname.c_str());
228 fout.SetCopyIfDifferent(true);
229 this->WriteVCProjFile(fout,lname,target);
230 if (fout.Close())
232 this->GlobalGenerator->FileReplacedDuringGenerate(fname);
236 //----------------------------------------------------------------------------
237 cmSourceFile* cmLocalVisualStudio7Generator::CreateVCProjBuildRule()
239 std::string stampName = cmake::GetCMakeFilesDirectoryPostSlash();
240 stampName += "generate.stamp";
241 const char* dsprule =
242 this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
243 cmCustomCommandLine commandLine;
244 commandLine.push_back(dsprule);
245 std::string makefileIn = this->Makefile->GetStartDirectory();
246 makefileIn += "/";
247 makefileIn += "CMakeLists.txt";
248 makefileIn = cmSystemTools::CollapseFullPath(makefileIn.c_str());
249 std::string comment = "Building Custom Rule ";
250 comment += makefileIn;
251 std::string args;
252 args = "-H";
253 args += this->Convert(this->Makefile->GetHomeDirectory(),
254 START_OUTPUT, UNCHANGED, true);
255 commandLine.push_back(args);
256 args = "-B";
257 args +=
258 this->Convert(this->Makefile->GetHomeOutputDirectory(),
259 START_OUTPUT, UNCHANGED, true);
260 commandLine.push_back(args);
261 commandLine.push_back("--check-stamp-file");
262 commandLine.push_back(stampName.c_str());
264 std::vector<std::string> const& listFiles = this->Makefile->GetListFiles();
266 cmCustomCommandLines commandLines;
267 commandLines.push_back(commandLine);
268 const char* no_working_directory = 0;
269 this->Makefile->AddCustomCommandToOutput(stampName.c_str(), listFiles,
270 makefileIn.c_str(), commandLines,
271 comment.c_str(),
272 no_working_directory, true);
273 if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
275 return file;
277 else
279 cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
280 return 0;
284 void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
285 const char *libName,
286 cmTarget &target)
288 std::vector<std::string> *configs =
289 static_cast<cmGlobalVisualStudio7Generator *>
290 (this->GlobalGenerator)->GetConfigurations();
292 fout << "\t<Configurations>\n";
293 for( std::vector<std::string>::iterator i = configs->begin();
294 i != configs->end(); ++i)
296 this->WriteConfiguration(fout, i->c_str(), libName, target);
298 fout << "\t</Configurations>\n";
300 cmVS7FlagTable cmLocalVisualStudio7GeneratorFortranFlagTable[] =
302 {"Preprocess", "fpp", "Run Preprocessor on files", "preprocessYes", 0},
303 {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "true", 0},
304 {"DebugInformationFormat", "Zi", "full debug", "debugEnabled", 0},
305 {"DebugInformationFormat", "debug:full", "full debug", "debugEnabled", 0},
306 {"DebugInformationFormat", "Z7", "c7 compat", "debugOldStyleInfo", 0},
307 {"DebugInformationFormat", "Zd", "line numbers", "debugLineInfoOnly", 0},
308 {"Optimization", "Od", "disable optimization", "optimizeDisabled", 0},
309 {"Optimization", "O1", "min space", "optimizeMinSpace", 0},
310 {"Optimization", "O3", "full optimize", "optimizeFull", 0},
311 {"GlobalOptimizations", "Og", "global optimize", "true", 0},
312 {"InlineFunctionExpansion", "Ob0", "", "expandDisable", 0},
313 {"InlineFunctionExpansion", "Ob1", "", "expandOnlyInline", 0},
314 {"FavorSizeOrSpeed", "Os", "", "favorSize", 0},
315 {"OmitFramePointers", "Oy-", "", "false", 0},
316 {"OptimizeForProcessor", "GB", "", "procOptimizeBlended", 0},
317 {"OptimizeForProcessor", "G5", "", "procOptimizePentium", 0},
318 {"OptimizeForProcessor", "G6", "", "procOptimizePentiumProThruIII", 0},
319 {"UseProcessorExtensions", "QzxK", "", "codeForStreamingSIMD", 0},
320 {"OptimizeForProcessor", "QaxN", "", "codeForPentium4", 0},
321 {"OptimizeForProcessor", "QaxB", "", "codeForPentiumM", 0},
322 {"OptimizeForProcessor", "QaxP", "", "codeForCodeNamedPrescott", 0},
323 {"OptimizeForProcessor", "QaxT", "", "codeForCore2Duo", 0},
324 {"OptimizeForProcessor", "QxK", "", "codeExclusivelyStreamingSIMD", 0},
325 {"OptimizeForProcessor", "QxN", "", "codeExclusivelyPentium4", 0},
326 {"OptimizeForProcessor", "QxB", "", "codeExclusivelyPentiumM", 0},
327 {"OptimizeForProcessor", "QxP", "", "codeExclusivelyCodeNamedPrescott", 0},
328 {"OptimizeForProcessor", "QxT", "", "codeExclusivelyCore2Duo", 0},
329 {"OptimizeForProcessor", "QxO", "", "codeExclusivelyCore2StreamingSIMD", 0},
330 {"OptimizeForProcessor", "QxS", "", "codeExclusivelyCore2StreamingSIMD4", 0},
332 {"ModulePath", "module:", "", "",
333 cmVS7FlagTable::UserValueRequired},
334 {"LoopUnrolling", "Qunroll:", "", "",
335 cmVS7FlagTable::UserValueRequired},
336 {"AutoParallelThreshold", "Qpar-threshold:", "", "",
337 cmVS7FlagTable::UserValueRequired},
338 {"HeapArrays", "heap-arrays:", "", "",
339 cmVS7FlagTable::UserValueRequired},
340 {"ObjectText", "bintext:", "", "",
341 cmVS7FlagTable::UserValueRequired},
342 {"Parallelization", "Qparallel", "", "true", 0},
343 {"PrefetchInsertion", "Qprefetch-", "", "false", 0},
344 {"BufferedIO", "assume:buffered_io", "", "true", 0},
345 {"CallingConvention", "iface:stdcall", "", "callConventionStdCall", 0},
346 {"CallingConvention", "iface:cref", "", "callConventionCRef", 0},
347 {"CallingConvention", "iface:stdref", "", "callConventionStdRef", 0},
348 {"CallingConvention", "iface:stdcall", "", "callConventionStdCall", 0},
349 {"CallingConvention", "iface:cvf", "", "callConventionCVF", 0},
350 {"EnableRecursion", "recursive", "", "true", 0},
351 {"ReentrantCode", "reentrancy", "", "true", 0},
352 // done up to Language
353 {0,0,0,0,0}
355 // fill the table here currently the comment field is not used for
356 // anything other than documentation NOTE: Make sure the longer
357 // commandFlag comes FIRST!
358 cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
360 // option flags (some flags map to the same option)
361 {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1", 0},
362 {"BasicRuntimeChecks", "RTCsu",
363 "Both stack and uninitialized checks", "3", 0},
364 {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1", 0},
365 {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2", 0},
366 {"BasicRuntimeChecks", "RTC1",
367 "Both stack and uninitialized checks", "3", 0},
368 {"DebugInformationFormat", "Z7", "debug format", "1", 0},
369 {"DebugInformationFormat", "Zd", "debug format", "2", 0},
370 {"DebugInformationFormat", "Zi", "debug format", "3", 0},
371 {"DebugInformationFormat", "ZI", "debug format", "4", 0},
372 {"EnableEnhancedInstructionSet", "arch:SSE2",
373 "Use sse2 instructions", "2", 0},
374 {"EnableEnhancedInstructionSet", "arch:SSE",
375 "Use sse instructions", "1", 0},
376 {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1", 0},
377 {"FavorSizeOrSpeed", "Os", "Favor small code", "2", 0},
378 {"CompileAs", "TC", "Compile as c code", "1", 0},
379 {"CompileAs", "TP", "Compile as c++ code", "2", 0},
380 {"Optimization", "Od", "Non Debug", "0", 0},
381 {"Optimization", "O1", "Min Size", "1", 0},
382 {"Optimization", "O2", "Max Speed", "2", 0},
383 {"Optimization", "Ox", "Max Optimization", "3", 0},
384 {"OptimizeForProcessor", "GB", "Blended processor mode", "0", 0},
385 {"OptimizeForProcessor", "G5", "Pentium", "1", 0},
386 {"OptimizeForProcessor", "G6", "PPro PII PIII", "2", 0},
387 {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3", 0},
388 {"InlineFunctionExpansion", "Ob0", "no inlines", "0", 0},
389 {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1", 0},
390 {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2", 0},
391 {"RuntimeLibrary", "MTd", "Multithreded debug", "1", 0},
392 {"RuntimeLibrary", "MT", "Multithreded", "0", 0},
393 {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3", 0},
394 {"RuntimeLibrary", "MD", "Multithreded dll", "2", 0},
395 {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5", 0},
396 {"RuntimeLibrary", "ML", "Sinble Thread", "4", 0},
397 {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5", 0},
398 {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1", 0},
399 {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2", 0},
400 {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3", 0},
401 {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4", 0},
402 {"WarningLevel", "W0", "Warning level", "0", 0},
403 {"WarningLevel", "W1", "Warning level", "1", 0},
404 {"WarningLevel", "W2", "Warning level", "2", 0},
405 {"WarningLevel", "W3", "Warning level", "3", 0},
406 {"WarningLevel", "W4", "Warning level", "4", 0},
407 {"DisableSpecificWarnings", "wd", "Disable specific warnings", "",
408 cmVS7FlagTable::UserValue | cmVS7FlagTable::SemicolonAppendable},
410 // Precompiled header and related options. Note that the
411 // UsePrecompiledHeader entries are marked as "Continue" so that the
412 // corresponding PrecompiledHeaderThrough entry can be found.
413 {"UsePrecompiledHeader", "Yc", "Create Precompiled Header", "1",
414 cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
415 {"PrecompiledHeaderThrough", "Yc", "Precompiled Header Name", "",
416 cmVS7FlagTable::UserValueRequired},
417 {"PrecompiledHeaderFile", "Fp", "Generated Precompiled Header", "",
418 cmVS7FlagTable::UserValue},
419 // The YX and Yu options are in a per-global-generator table because
420 // their values differ based on the VS IDE version.
421 {"ForcedIncludeFiles", "FI", "Forced include files", "",
422 cmVS7FlagTable::UserValueRequired},
424 // boolean flags
425 {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE", 0},
426 {"BufferSecurityCheck", "GS-", "Turn off Buffer security check", "FALSE", 0},
427 {"Detect64BitPortabilityProblems", "Wp64",
428 "Detect 64-bit Portability Problems", "TRUE", 0},
429 {"EnableFiberSafeOptimizations", "GT", "Enable Fiber-safe Optimizations",
430 "TRUE", 0},
431 {"EnableFunctionLevelLinking", "Gy",
432 "EnableFunctionLevelLinking", "TRUE", 0},
433 {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE", 0},
434 {"GlobalOptimizations", "Og", "Global Optimize", "TRUE", 0},
435 {"ImproveFloatingPointConsistency", "Op",
436 "ImproveFloatingPointConsistency", "TRUE", 0},
437 {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE", 0},
438 {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE", 0},
439 {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE", 0},
440 {"RuntimeTypeInfo", "GR",
441 "Turn on Run time type information for c++", "TRUE", 0},
442 {"RuntimeTypeInfo", "GR-",
443 "Turn off Run time type information for c++", "FALSE", 0},
444 {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE", 0},
445 {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "TRUE", 0},
446 {"WarnAsError", "WX", "Treat warnings as errors", "TRUE", 0},
447 {0,0,0,0,0}
452 cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
454 // option flags (some flags map to the same option)
455 {"GenerateManifest", "MANIFEST:NO",
456 "disable manifest generation", "FALSE", 0},
457 {"GenerateManifest", "MANIFEST", "enable manifest generation", "TRUE", 0},
458 {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1", 0},
459 {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2", 0},
460 {"IgnoreDefaultLibraryNames", "NODEFAULTLIB:", "default libs to ignore", "",
461 cmVS7FlagTable::UserValue | cmVS7FlagTable::SemicolonAppendable},
462 {"IgnoreAllDefaultLibraries", "NODEFAULTLIB", "ignore all default libs",
463 "TRUE", 0},
464 {"FixedBaseAddress", "FIXED:NO", "Generate a relocation section", "1", 0},
465 {"FixedBaseAddress", "FIXED", "Image must be loaded at a fixed address",
466 "2", 0},
467 {"EnableCOMDATFolding", "OPT:NOICF", "Do not remove redundant COMDATs",
468 "1", 0},
469 {"EnableCOMDATFolding", "OPT:ICF", "Remove redundant COMDATs", "2", 0},
470 {"OptimizeReferences", "OPT:NOREF", "Keep unreferenced data", "1", 0},
471 {"OptimizeReferences", "OPT:REF", "Eliminate unreferenced data", "2", 0},
472 {"TargetMachine", "MACHINE:I386", "Machine x86", "1", 0},
473 {"TargetMachine", "MACHINE:X86", "Machine x86", "1", 0},
474 {"TargetMachine", "MACHINE:X64", "Machine x64", "17", 0},
475 {"ModuleDefinitionFile", "DEF:", "add an export def file", "",
476 cmVS7FlagTable::UserValue},
477 {"GenerateMapFile", "MAP", "enable generation of map file", "TRUE", 0},
478 {0,0,0,0,0}
481 //----------------------------------------------------------------------------
482 class cmLocalVisualStudio7GeneratorOptions
484 public:
485 // Construct an options table for a given tool.
486 enum Tool
488 Compiler,
489 Linker,
490 FortranCompiler
492 cmLocalVisualStudio7GeneratorOptions(cmLocalVisualStudio7Generator* lg,
493 int version,
494 Tool tool,
495 cmVS7FlagTable const* extraTable = 0);
497 // Store options from command line flags.
498 void Parse(const char* flags);
500 // Fix the ExceptionHandling option to default to off.
501 void FixExceptionHandlingDefault();
503 // Store options for verbose builds.
504 void SetVerboseMakefile(bool verbose);
506 // Store definitions and flags.
507 void AddDefine(const std::string& define);
508 void AddDefines(const char* defines);
509 void AddFlag(const char* flag, const char* value);
511 // Check for specific options.
512 bool UsingUnicode();
514 bool IsDebug();
515 // Write options to output.
516 void OutputPreprocessorDefinitions(std::ostream& fout,
517 const char* prefix,
518 const char* suffix);
519 void OutputFlagMap(std::ostream& fout, const char* indent);
520 void OutputAdditionalOptions(std::ostream& fout,
521 const char* prefix,
522 const char* suffix);
524 private:
525 cmLocalVisualStudio7Generator* LocalGenerator;
526 int Version;
528 // create a map of xml tags to the values they should have in the output
529 // for example, "BufferSecurityCheck" = "TRUE"
530 // first fill this table with the values for the configuration
531 // Debug, Release, etc,
532 // Then parse the command line flags specified in CMAKE_CXX_FLAGS
533 // and CMAKE_C_FLAGS
534 // and overwrite or add new values to this map
535 std::map<cmStdString, cmStdString> FlagMap;
537 // Preprocessor definitions.
538 std::vector<std::string> Defines;
540 // Unrecognized flags that get no special handling.
541 cmStdString FlagString;
543 Tool CurrentTool;
544 bool DoingDefine;
545 cmVS7FlagTable const* FlagTable;
546 cmVS7FlagTable const* ExtraFlagTable;
547 void HandleFlag(const char* flag);
548 bool CheckFlagTable(cmVS7FlagTable const* table, const char* flag,
549 bool& flag_handled);
552 void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
553 const char* configName,
554 const char *libName,
555 cmTarget &target)
557 const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
558 if(!mfcFlag)
560 mfcFlag = "0";
562 fout << "\t\t<Configuration\n"
563 << "\t\t\tName=\"" << configName << "|" << this->PlatformName << "\"\n"
564 << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
565 // This is an internal type to Visual Studio, it seems that:
566 // 4 == static library
567 // 2 == dll
568 // 1 == executable
569 // 10 == utility
570 const char* configType = "10";
571 const char* projectType = 0;
572 switch(target.GetType())
574 case cmTarget::STATIC_LIBRARY:
575 projectType = "typeStaticLibrary";
576 configType = "4";
577 break;
578 case cmTarget::SHARED_LIBRARY:
579 case cmTarget::MODULE_LIBRARY:
580 projectType = "typeDynamicLibrary";
581 configType = "2";
582 break;
583 case cmTarget::EXECUTABLE:
584 configType = "1";
585 break;
586 case cmTarget::UTILITY:
587 case cmTarget::GLOBAL_TARGET:
588 configType = "10";
589 default:
590 break;
592 if(this->FortranProject && projectType)
594 configType = projectType;
596 std::string flags;
597 if(strcmp(configType, "10") != 0)
599 const char* linkLanguage =
600 target.GetLinkerLanguage(this->GetGlobalGenerator());
601 if(!linkLanguage)
603 cmSystemTools::Error
604 ("CMake can not determine linker language for target:",
605 target.GetName());
606 return;
608 if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
609 || strcmp(linkLanguage, "Fortran") == 0)
611 std::string baseFlagVar = "CMAKE_";
612 baseFlagVar += linkLanguage;
613 baseFlagVar += "_FLAGS";
614 flags = this->Makefile->GetRequiredDefinition(baseFlagVar.c_str());
615 std::string flagVar = baseFlagVar + std::string("_") +
616 cmSystemTools::UpperCase(configName);
617 flags += " ";
618 flags += this->Makefile->GetRequiredDefinition(flagVar.c_str());
620 // set the correct language
621 if(strcmp(linkLanguage, "C") == 0)
623 flags += " /TC ";
625 if(strcmp(linkLanguage, "CXX") == 0)
627 flags += " /TP ";
631 // Add the target-specific flags.
632 if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
634 flags += " ";
635 flags += targetFlags;
638 std::string configUpper = cmSystemTools::UpperCase(configName);
639 std::string defPropName = "COMPILE_DEFINITIONS_";
640 defPropName += configUpper;
642 // Get preprocessor definitions for this directory.
643 std::string defineFlags = this->Makefile->GetDefineFlags();
644 Options::Tool t = Options::Compiler;
645 if(this->FortranProject)
647 t = Options::FortranCompiler;
649 Options targetOptions(this, this->Version, t, this->ExtraFlagTable);
650 targetOptions.FixExceptionHandlingDefault();
651 targetOptions.Parse(flags.c_str());
652 targetOptions.Parse(defineFlags.c_str());
653 targetOptions.AddDefines
654 (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
655 targetOptions.AddDefines(target.GetProperty("COMPILE_DEFINITIONS"));
656 targetOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
657 targetOptions.AddDefines(target.GetProperty(defPropName.c_str()));
658 targetOptions.SetVerboseMakefile(
659 this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
661 // Add a definition for the configuration name.
662 std::string configDefine = "CMAKE_INTDIR=\"";
663 configDefine += configName;
664 configDefine += "\"";
665 targetOptions.AddDefine(configDefine);
667 // Add the export symbol definition for shared library objects.
668 if(const char* exportMacro = target.GetExportMacro())
670 targetOptions.AddDefine(exportMacro);
673 // The intermediate directory name consists of a directory for the
674 // target and a subdirectory for the configuration name.
675 std::string intermediateDir = this->GetTargetDirectory(target);
676 intermediateDir += "/";
677 intermediateDir += configName;
678 fout << "\t\t\tIntermediateDirectory=\""
679 << this->ConvertToXMLOutputPath(intermediateDir.c_str())
680 << "\"\n"
681 << "\t\t\tConfigurationType=\"" << configType << "\"\n"
682 << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
683 << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
685 // If unicode is enabled change the character set to unicode, if not
686 // then default to MBCS.
687 if(targetOptions.UsingUnicode())
689 fout << "\t\t\tCharacterSet=\"1\">\n";
691 else
693 fout << "\t\t\tCharacterSet=\"2\">\n";
695 const char* tool = "VCCLCompilerTool";
696 if(this->FortranProject)
698 tool = "VFFortranCompilerTool";
700 fout << "\t\t\t<Tool\n"
701 << "\t\t\t\tName=\"" << tool << "\"\n";
702 if(this->FortranProject)
704 const char* target_mod_dir =
705 target.GetProperty("Fortran_MODULE_DIRECTORY");
706 std::string modDir;
707 if(target_mod_dir)
709 modDir = this->Convert(target_mod_dir,
710 cmLocalGenerator::START_OUTPUT,
711 cmLocalGenerator::UNCHANGED);
713 else
715 modDir = ".";
717 fout << "\t\t\t\tModulePath=\""
718 << this->ConvertToXMLOutputPath(modDir.c_str())
719 << "\\$(ConfigurationName)\"\n";
721 targetOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
722 fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
723 std::vector<std::string> includes;
724 this->GetIncludeDirectories(includes);
725 std::vector<std::string>::iterator i = includes.begin();
726 for(;i != includes.end(); ++i)
728 // output the include path
729 std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
730 fout << ipath << ";";
731 // if this is fortran then output the include with
732 // a ConfigurationName on the end of it.
733 if(this->FortranProject)
735 ipath = i->c_str();
736 ipath += "/$(ConfigurationName)";
737 ipath = this->ConvertToXMLOutputPath(ipath.c_str());
738 fout << ipath << ";";
741 fout << "\"\n";
742 targetOptions.OutputFlagMap(fout, "\t\t\t\t");
743 targetOptions.OutputPreprocessorDefinitions(fout, "\t\t\t\t", "\n");
744 fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
745 fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
746 if(target.GetType() == cmTarget::EXECUTABLE ||
747 target.GetType() == cmTarget::STATIC_LIBRARY ||
748 target.GetType() == cmTarget::SHARED_LIBRARY ||
749 target.GetType() == cmTarget::MODULE_LIBRARY)
751 // We need to specify a program database file name even for
752 // non-debug configurations because VS still creates .idb files.
753 fout << "\t\t\t\tProgramDataBaseFileName=\""
754 << target.GetDirectory(configName) << "/"
755 << target.GetPDBName(configName) << "\"\n";
757 fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
758 tool = "VCCustomBuildTool";
759 if(this->FortranProject)
761 tool = "VFCustomBuildTool";
763 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"/>\n";
764 tool = "VCResourceCompilerTool";
765 if(this->FortranProject)
767 tool = "VFResourceCompilerTool";
769 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n"
770 << "\t\t\t\tAdditionalIncludeDirectories=\"";
771 for(i = includes.begin();i != includes.end(); ++i)
773 std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
774 fout << ipath << ";";
776 // add the -D flags to the RC tool
777 fout << "\"";
778 targetOptions.OutputPreprocessorDefinitions(fout, "\n\t\t\t\t", "");
779 fout << "/>\n";
780 tool = "VCMIDLTool";
781 if(this->FortranProject)
783 tool = "VFMIDLTool";
785 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n";
786 targetOptions.OutputPreprocessorDefinitions(fout, "\t\t\t\t", "\n");
787 fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
788 if( this->PlatformName == "x64" )
790 fout << "\t\t\t\tTargetEnvironment=\"3\"\n";
792 else if( this->PlatformName == "ia64" )
794 fout << "\t\t\t\tTargetEnvironment=\"2\"\n";
796 else
798 fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
800 fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
801 fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
802 fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
803 fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
804 fout << "\t\t\t\tDLLDataFileName=\"\"\n";
805 fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
806 fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
807 // end of <Tool Name=VCMIDLTool
809 // Check if we need the FAT32 workaround.
810 if ( this->Version >= 8 )
812 // Check the filesystem type where the target will be written.
813 if(cmLVS6G_IsFAT(target.GetDirectory(configName).c_str()))
815 // Add a flag telling the manifest tool to use a workaround
816 // for FAT32 file systems, which can cause an empty manifest
817 // to be embedded into the resulting executable. See CMake
818 // bug #2617.
819 const char* tool = "VCManifestTool";
820 if(this->FortranProject)
822 tool = "VFManifestTool";
824 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"\n"
825 << "\t\t\t\tUseFAT32Workaround=\"true\"\n"
826 << "\t\t\t/>\n";
830 this->OutputTargetRules(fout, configName, target, libName);
831 this->OutputBuildTool(fout, configName, target, targetOptions.IsDebug());
832 fout << "\t\t</Configuration>\n";
835 //----------------------------------------------------------------------------
836 std::string
837 cmLocalVisualStudio7Generator
838 ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
840 std::string configTypeUpper = cmSystemTools::UpperCase(configName);
841 std::string extraLinkOptionsBuildTypeDef =
842 rootLinkerFlags + "_" + configTypeUpper;
844 std::string extraLinkOptionsBuildType =
845 this->Makefile->GetRequiredDefinition
846 (extraLinkOptionsBuildTypeDef.c_str());
848 return extraLinkOptionsBuildType;
851 void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
852 const char* configName,
853 cmTarget &target,
854 bool isDebug)
856 std::string temp;
857 std::string extraLinkOptions;
858 if(target.GetType() == cmTarget::EXECUTABLE)
860 extraLinkOptions =
861 this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS")
862 + std::string(" ")
863 + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
865 if(target.GetType() == cmTarget::SHARED_LIBRARY)
867 extraLinkOptions =
868 this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS")
869 + std::string(" ")
870 + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
872 if(target.GetType() == cmTarget::MODULE_LIBRARY)
874 extraLinkOptions =
875 this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS")
876 + std::string(" ")
877 + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
880 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
881 if(targetLinkFlags)
883 extraLinkOptions += " ";
884 extraLinkOptions += targetLinkFlags;
886 std::string configTypeUpper = cmSystemTools::UpperCase(configName);
887 std::string linkFlagsConfig = "LINK_FLAGS_";
888 linkFlagsConfig += configTypeUpper;
889 targetLinkFlags = target.GetProperty(linkFlagsConfig.c_str());
890 if(targetLinkFlags)
892 extraLinkOptions += " ";
893 extraLinkOptions += targetLinkFlags;
895 Options linkOptions(this, this->Version, Options::Linker);
896 linkOptions.Parse(extraLinkOptions.c_str());
897 switch(target.GetType())
899 case cmTarget::STATIC_LIBRARY:
901 std::string targetNameFull = target.GetFullName(configName);
902 std::string libpath = target.GetDirectory(configName);
903 libpath += "/";
904 libpath += targetNameFull;
905 const char* tool = "VCLibrarianTool";
906 if(this->FortranProject)
908 tool = "VFLibrarianTool";
910 fout << "\t\t\t<Tool\n"
911 << "\t\t\t\tName=\"" << tool << "\"\n";
912 if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
914 fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
916 fout << "\t\t\t\tOutputFile=\""
917 << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << "\"/>\n";
918 break;
920 case cmTarget::SHARED_LIBRARY:
921 case cmTarget::MODULE_LIBRARY:
923 std::string targetName;
924 std::string targetNameSO;
925 std::string targetNameFull;
926 std::string targetNameImport;
927 std::string targetNamePDB;
928 target.GetLibraryNames(targetName, targetNameSO, targetNameFull,
929 targetNameImport, targetNamePDB, configName);
931 // Compute the link library and directory information.
932 cmComputeLinkInformation* pcli = target.GetLinkInformation(configName);
933 if(!pcli)
935 return;
937 cmComputeLinkInformation& cli = *pcli;
938 const char* linkLanguage = cli.GetLinkLanguage();
940 // Compute the variable name to lookup standard libraries for this
941 // language.
942 std::string standardLibsVar = "CMAKE_";
943 standardLibsVar += linkLanguage;
944 standardLibsVar += "_STANDARD_LIBRARIES";
945 const char* tool = "VCLinkerTool";
946 if(this->FortranProject)
948 tool = "VFLinkerTool";
950 fout << "\t\t\t<Tool\n"
951 << "\t\t\t\tName=\"" << tool << "\"\n";
952 linkOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
953 // Use the NOINHERIT macro to avoid getting VS project default
954 // libraries which may be set by the user to something bad.
955 fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
956 << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
957 << " ";
958 this->Internal->OutputLibraries(fout, cli.GetItems());
959 fout << "\"\n";
960 temp = target.GetDirectory(configName);
961 temp += "/";
962 temp += targetNameFull;
963 fout << "\t\t\t\tOutputFile=\""
964 << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
965 this->WriteTargetVersionAttribute(fout, target);
966 linkOptions.OutputFlagMap(fout, "\t\t\t\t");
967 fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
968 this->OutputLibraryDirectories(fout, cli.GetDirectories());
969 fout << "\"\n";
970 this->OutputModuleDefinitionFile(fout, target);
971 temp = target.GetDirectory(configName);
972 temp += "/";
973 temp += targetNamePDB;
974 fout << "\t\t\t\tProgramDataBaseFile=\"" <<
975 this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
976 if(isDebug)
978 fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
980 std::string stackVar = "CMAKE_";
981 stackVar += linkLanguage;
982 stackVar += "_STACK_SIZE";
983 const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
984 if(stackVal)
986 fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
988 temp = target.GetDirectory(configName, true);
989 temp += "/";
990 temp += targetNameImport;
991 fout << "\t\t\t\tImportLibrary=\""
992 << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
994 break;
995 case cmTarget::EXECUTABLE:
997 std::string targetName;
998 std::string targetNameFull;
999 std::string targetNameImport;
1000 std::string targetNamePDB;
1001 target.GetExecutableNames(targetName, targetNameFull,
1002 targetNameImport, targetNamePDB, configName);
1004 // Compute the link library and directory information.
1005 cmComputeLinkInformation* pcli = target.GetLinkInformation(configName);
1006 if(!pcli)
1008 return;
1010 cmComputeLinkInformation& cli = *pcli;
1011 const char* linkLanguage = cli.GetLinkLanguage();
1013 // Compute the variable name to lookup standard libraries for this
1014 // language.
1015 std::string standardLibsVar = "CMAKE_";
1016 standardLibsVar += linkLanguage;
1017 standardLibsVar += "_STANDARD_LIBRARIES";
1018 const char* tool = "VCLinkerTool";
1019 if(this->FortranProject)
1021 tool = "VFLinkerTool";
1023 fout << "\t\t\t<Tool\n"
1024 << "\t\t\t\tName=\"" << tool << "\"\n";
1025 linkOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
1026 // Use the NOINHERIT macro to avoid getting VS project default
1027 // libraries which may be set by the user to something bad.
1028 fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
1029 << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
1030 << " ";
1031 this->Internal->OutputLibraries(fout, cli.GetItems());
1032 fout << "\"\n";
1033 temp = target.GetDirectory(configName);
1034 temp += "/";
1035 temp += targetNameFull;
1036 fout << "\t\t\t\tOutputFile=\""
1037 << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
1038 this->WriteTargetVersionAttribute(fout, target);
1039 linkOptions.OutputFlagMap(fout, "\t\t\t\t");
1040 fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
1041 this->OutputLibraryDirectories(fout, cli.GetDirectories());
1042 fout << "\"\n";
1043 fout << "\t\t\t\tProgramDataBaseFile=\""
1044 << target.GetDirectory(configName) << "/" << targetNamePDB
1045 << "\"\n";
1046 if(isDebug)
1048 fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
1050 if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
1052 fout << "\t\t\t\tSubSystem=\"2\"\n";
1054 else
1056 fout << "\t\t\t\tSubSystem=\"1\"\n";
1058 std::string stackVar = "CMAKE_";
1059 stackVar += linkLanguage;
1060 stackVar += "_STACK_SIZE";
1061 const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
1062 if(stackVal)
1064 fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
1066 temp = target.GetDirectory(configName, true);
1067 temp += "/";
1068 temp += targetNameImport;
1069 fout << "\t\t\t\tImportLibrary=\""
1070 << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
1071 break;
1073 case cmTarget::UTILITY:
1074 case cmTarget::GLOBAL_TARGET:
1075 break;
1079 //----------------------------------------------------------------------------
1080 void
1081 cmLocalVisualStudio7Generator
1082 ::WriteTargetVersionAttribute(std::ostream& fout, cmTarget& target)
1084 int major;
1085 int minor;
1086 target.GetTargetVersion(major, minor);
1087 fout << "\t\t\t\tVersion=\"" << major << "." << minor << "\"\n";
1090 void cmLocalVisualStudio7Generator
1091 ::OutputModuleDefinitionFile(std::ostream& fout,
1092 cmTarget &target)
1094 std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
1095 for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
1096 i != classes.end(); i++)
1098 cmSourceFile* sf = *i;
1099 if(cmSystemTools::UpperCase(sf->GetExtension()) == "DEF")
1101 fout << "\t\t\t\tModuleDefinitionFile=\""
1102 << this->ConvertToXMLOutputPath(sf->GetFullPath().c_str())
1103 << "\"\n";
1104 return;
1110 //----------------------------------------------------------------------------
1111 void
1112 cmLocalVisualStudio7GeneratorInternals
1113 ::OutputLibraries(std::ostream& fout, ItemVector const& libs)
1115 cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
1116 for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
1118 if(l->IsPath)
1120 std::string rel = lg->Convert(l->Value.c_str(),
1121 cmLocalGenerator::START_OUTPUT,
1122 cmLocalGenerator::UNCHANGED);
1123 fout << lg->ConvertToXMLOutputPath(rel.c_str()) << " ";
1125 else
1127 fout << l->Value << " ";
1132 //----------------------------------------------------------------------------
1133 void
1134 cmLocalVisualStudio7Generator
1135 ::OutputLibraryDirectories(std::ostream& fout,
1136 std::vector<std::string> const& dirs)
1138 const char* comma = "";
1139 for(std::vector<std::string>::const_iterator d = dirs.begin();
1140 d != dirs.end(); ++d)
1142 // Remove any trailing slash and skip empty paths.
1143 std::string dir = *d;
1144 if(dir[dir.size()-1] == '/')
1146 dir = dir.substr(0, dir.size()-1);
1148 if(dir.empty())
1150 continue;
1153 // Switch to a relative path specification if it is shorter.
1154 if(cmSystemTools::FileIsFullPath(dir.c_str()))
1156 std::string rel = this->Convert(dir.c_str(), START_OUTPUT, UNCHANGED);
1157 if(rel.size() < dir.size())
1159 dir = rel;
1163 // First search a configuration-specific subdirectory and then the
1164 // original directory.
1165 fout << comma << this->ConvertToXMLOutputPath((dir+"/$(OutDir)").c_str())
1166 << "," << this->ConvertToXMLOutputPath(dir.c_str());
1167 comma = ",";
1171 void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
1172 const char *libName,
1173 cmTarget &target)
1175 // get the configurations
1176 std::vector<std::string> *configs =
1177 static_cast<cmGlobalVisualStudio7Generator *>
1178 (this->GlobalGenerator)->GetConfigurations();
1180 // We may be modifying the source groups temporarily, so make a copy.
1181 std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
1183 // get the classes from the source lists then add them to the groups
1184 std::vector<cmSourceFile*>const & classes = target.GetSourceFiles();
1185 for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
1186 i != classes.end(); i++)
1188 // Add the file to the list of sources.
1189 std::string source = (*i)->GetFullPath();
1190 if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
1192 this->ModuleDefinitionFile = (*i)->GetFullPath();
1194 cmSourceGroup& sourceGroup =
1195 this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
1196 sourceGroup.AssignSource(*i);
1199 // Compute which sources need unique object computation.
1200 this->ComputeObjectNameRequirements(sourceGroups);
1202 // open the project
1203 this->WriteProjectStart(fout, libName, target, sourceGroups);
1204 // write the configuration information
1205 this->WriteConfigurations(fout, libName, target);
1207 fout << "\t<Files>\n";
1210 // Loop through every source group.
1211 for(unsigned int i = 0; i < sourceGroups.size(); ++i)
1213 cmSourceGroup sg = sourceGroups[i];
1214 this->WriteGroup(&sg, target, fout, libName, configs);
1219 fout << "\t</Files>\n";
1221 // Write the VCProj file's footer.
1222 this->WriteVCProjFooter(fout);
1225 struct cmLVS7GFileConfig
1227 std::string ObjectName;
1228 std::string CompileFlags;
1229 std::string CompileDefs;
1230 std::string CompileDefsConfig;
1231 std::string AdditionalDeps;
1232 bool ExcludedFromBuild;
1235 class cmLocalVisualStudio7GeneratorFCInfo
1237 public:
1238 cmLocalVisualStudio7GeneratorFCInfo(cmLocalVisualStudio7Generator* lg,
1239 cmTarget& target,
1240 cmSourceFile const& sf,
1241 std::vector<std::string>* configs,
1242 std::string const& dir_max);
1243 std::map<cmStdString, cmLVS7GFileConfig> FileConfigMap;
1246 cmLocalVisualStudio7GeneratorFCInfo
1247 ::cmLocalVisualStudio7GeneratorFCInfo(cmLocalVisualStudio7Generator* lg,
1248 cmTarget& target,
1249 cmSourceFile const& sf,
1250 std::vector<std::string>* configs,
1251 std::string const& dir_max)
1253 std::string objectName;
1254 if(lg->NeedObjectName.find(&sf) != lg->NeedObjectName.end())
1256 objectName = lg->GetObjectFileNameWithoutTarget(sf, dir_max);
1259 // Compute per-source, per-config information.
1260 for(std::vector<std::string>::iterator i = configs->begin();
1261 i != configs->end(); ++i)
1263 std::string configUpper = cmSystemTools::UpperCase(*i);
1264 cmLVS7GFileConfig fc;
1265 bool needfc = false;
1266 if(!objectName.empty())
1268 fc.ObjectName = objectName;
1269 needfc = true;
1271 if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
1273 fc.CompileFlags = cflags;
1274 needfc = true;
1276 if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
1278 fc.CompileDefs = cdefs;
1279 needfc = true;
1281 std::string defPropName = "COMPILE_DEFINITIONS_";
1282 defPropName += configUpper;
1283 if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
1285 fc.CompileDefsConfig = ccdefs;
1286 needfc = true;
1289 // Check for extra object-file dependencies.
1290 if(const char* deps = sf.GetProperty("OBJECT_DEPENDS"))
1292 std::vector<std::string> depends;
1293 cmSystemTools::ExpandListArgument(deps, depends);
1294 const char* sep = "";
1295 for(std::vector<std::string>::iterator j = depends.begin();
1296 j != depends.end(); ++j)
1298 fc.AdditionalDeps += sep;
1299 fc.AdditionalDeps += lg->ConvertToXMLOutputPath(j->c_str());
1300 sep = ";";
1301 needfc = true;
1305 const char* lang =
1306 lg->GlobalGenerator->GetLanguageFromExtension
1307 (sf.GetExtension().c_str());
1308 const char* sourceLang = lg->GetSourceFileLanguage(sf);
1309 const char* linkLanguage = target.GetLinkerLanguage
1310 (lg->GetGlobalGenerator());
1311 bool needForceLang = false;
1312 // source file does not match its extension language
1313 if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
1315 needForceLang = true;
1316 lang = sourceLang;
1318 // If HEADER_FILE_ONLY is set, we must suppress this generation in
1319 // the project file
1320 fc.ExcludedFromBuild =
1321 (sf.GetPropertyAsBool("HEADER_FILE_ONLY"));
1322 if(fc.ExcludedFromBuild)
1324 needfc = true;
1327 // if the source file does not match the linker language
1328 // then force c or c++
1329 if(needForceLang || (linkLanguage && lang
1330 && strcmp(lang, linkLanguage) != 0))
1332 if(strcmp(lang, "CXX") == 0)
1334 // force a C++ file type
1335 fc.CompileFlags += " /TP ";
1336 needfc = true;
1338 else if(strcmp(lang, "C") == 0)
1340 // force to c
1341 fc.CompileFlags += " /TC ";
1342 needfc = true;
1346 if(needfc)
1348 this->FileConfigMap[*i] = fc;
1353 void cmLocalVisualStudio7Generator
1354 ::WriteGroup(const cmSourceGroup *sg, cmTarget& target,
1355 std::ostream &fout, const char *libName,
1356 std::vector<std::string> *configs)
1358 const std::vector<const cmSourceFile *> &sourceFiles =
1359 sg->GetSourceFiles();
1360 // If the group is empty, don't write it at all.
1361 if(sourceFiles.empty() && sg->GetGroupChildren().empty())
1363 return;
1366 // If the group has a name, write the header.
1367 std::string name = sg->GetName();
1368 if(name != "")
1370 this->WriteVCProjBeginGroup(fout, name.c_str(), "");
1373 // Compute the maximum length configuration name.
1374 std::string config_max;
1375 for(std::vector<std::string>::iterator i = configs->begin();
1376 i != configs->end(); ++i)
1378 if(i->size() > config_max.size())
1380 config_max = *i;
1384 // Compute the maximum length full path to the intermediate
1385 // files directory for any configuration. This is used to construct
1386 // object file names that do not produce paths that are too long.
1387 std::string dir_max;
1388 dir_max += this->Makefile->GetCurrentOutputDirectory();
1389 dir_max += "/";
1390 dir_max += this->GetTargetDirectory(target);
1391 dir_max += "/";
1392 dir_max += config_max;
1393 dir_max += "/";
1395 // Loop through each source in the source group.
1396 std::string objectName;
1397 for(std::vector<const cmSourceFile *>::const_iterator sf =
1398 sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
1400 std::string source = (*sf)->GetFullPath();
1401 FCInfo fcinfo(this, target, *(*sf), configs, dir_max);
1403 if (source != libName || target.GetType() == cmTarget::UTILITY ||
1404 target.GetType() == cmTarget::GLOBAL_TARGET )
1406 fout << "\t\t\t<File\n";
1407 std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
1408 // Tell MS-Dev what the source is. If the compiler knows how to
1409 // build it, then it will.
1410 fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
1411 if(cmCustomCommand const* command = (*sf)->GetCustomCommand())
1413 this->WriteCustomRule(fout, source.c_str(), *command, fcinfo);
1415 else if(!fcinfo.FileConfigMap.empty())
1417 const char* aCompilerTool = "VCCLCompilerTool";
1418 std::string ext = (*sf)->GetExtension();
1419 ext = cmSystemTools::LowerCase(ext);
1420 if(ext == "idl")
1422 aCompilerTool = "VCMIDLTool";
1423 if(this->FortranProject)
1425 aCompilerTool = "VFMIDLTool";
1428 if(ext == "rc")
1430 aCompilerTool = "VCResourceCompilerTool";
1431 if(this->FortranProject)
1433 aCompilerTool = "VFResourceCompilerTool";
1436 if(ext == "def")
1438 aCompilerTool = "VCCustomBuildTool";
1439 if(this->FortranProject)
1441 aCompilerTool = "VFCustomBuildTool";
1444 for(std::map<cmStdString, cmLVS7GFileConfig>::const_iterator
1445 fci = fcinfo.FileConfigMap.begin();
1446 fci != fcinfo.FileConfigMap.end(); ++fci)
1448 cmLVS7GFileConfig const& fc = fci->second;
1449 fout << "\t\t\t\t<FileConfiguration\n"
1450 << "\t\t\t\t\tName=\"" << fci->first
1451 << "|" << this->PlatformName << "\"";
1452 if(fc.ExcludedFromBuild)
1454 fout << " ExcludedFromBuild=\"true\"";
1456 fout << ">\n";
1457 fout << "\t\t\t\t\t<Tool\n"
1458 << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
1459 if(!fc.CompileFlags.empty() ||
1460 !fc.CompileDefs.empty() ||
1461 !fc.CompileDefsConfig.empty())
1463 Options fileOptions(this, this->Version, Options::Compiler,
1464 this->ExtraFlagTable);
1465 fileOptions.Parse(fc.CompileFlags.c_str());
1466 fileOptions.AddDefines(fc.CompileDefs.c_str());
1467 fileOptions.AddDefines(fc.CompileDefsConfig.c_str());
1468 fileOptions.OutputAdditionalOptions(fout, "\t\t\t\t\t", "\n");
1469 fileOptions.OutputFlagMap(fout, "\t\t\t\t\t");
1470 fileOptions.OutputPreprocessorDefinitions(fout,
1471 "\t\t\t\t\t", "\n");
1473 if(!fc.AdditionalDeps.empty())
1475 fout << "\t\t\t\t\tAdditionalDependencies=\""
1476 << fc.AdditionalDeps.c_str() << "\"\n";
1478 if(!fc.ObjectName.empty())
1480 fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
1481 << fc.ObjectName.c_str() << "\"\n";
1483 fout << "\t\t\t\t\t/>\n"
1484 << "\t\t\t\t</FileConfiguration>\n";
1487 fout << "\t\t\t</File>\n";
1491 std::vector<cmSourceGroup> const& children = sg->GetGroupChildren();
1493 for(unsigned int i=0;i<children.size();++i)
1495 this->WriteGroup(&children[i], target, fout, libName, configs);
1498 // If the group has a name, write the footer.
1499 if(name != "")
1501 this->WriteVCProjEndGroup(fout);
1505 void cmLocalVisualStudio7Generator::
1506 WriteCustomRule(std::ostream& fout,
1507 const char* source,
1508 const cmCustomCommand& command,
1509 FCInfo& fcinfo)
1511 std::string comment = this->ConstructComment(command);
1513 // Write the rule for each configuration.
1514 std::vector<std::string>::iterator i;
1515 std::vector<std::string> *configs =
1516 static_cast<cmGlobalVisualStudio7Generator *>
1517 (this->GlobalGenerator)->GetConfigurations();
1518 const char* compileTool = "VCCLCompilerTool";
1519 if(this->FortranProject)
1521 compileTool = "VFCLCompilerTool";
1523 const char* customTool = "VCCustomBuildTool";
1524 if(this->FortranProject)
1526 customTool = "VFCustomBuildTool";
1528 for(i = configs->begin(); i != configs->end(); ++i)
1530 cmLVS7GFileConfig const& fc = fcinfo.FileConfigMap[*i];
1531 fout << "\t\t\t\t<FileConfiguration\n";
1532 fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
1533 if(!fc.CompileFlags.empty())
1535 fout << "\t\t\t\t\t<Tool\n"
1536 << "\t\t\t\t\tName=\"" << compileTool << "\"\n"
1537 << "\t\t\t\t\tAdditionalOptions=\""
1538 << this->EscapeForXML(fc.CompileFlags.c_str()) << "\"/>\n";
1541 std::string script =
1542 this->ConstructScript(command.GetCommandLines(),
1543 command.GetWorkingDirectory(),
1544 i->c_str(),
1545 command.GetEscapeOldStyle(),
1546 command.GetEscapeAllowMakeVars());
1547 fout << "\t\t\t\t\t<Tool\n"
1548 << "\t\t\t\t\tName=\"" << customTool << "\"\n"
1549 << "\t\t\t\t\tDescription=\""
1550 << this->EscapeForXML(comment.c_str()) << "\"\n"
1551 << "\t\t\t\t\tCommandLine=\""
1552 << this->EscapeForXML(script.c_str()) << "\"\n"
1553 << "\t\t\t\t\tAdditionalDependencies=\"";
1554 if(command.GetDepends().empty())
1556 // There are no real dependencies. Produce an artificial one to
1557 // make sure the rule runs reliably.
1558 if(!cmSystemTools::FileExists(source))
1560 std::ofstream depout(source);
1561 depout << "Artificial dependency for a custom command.\n";
1563 fout << this->ConvertToXMLOutputPath(source);
1565 else
1567 // Write out the dependencies for the rule.
1568 for(std::vector<std::string>::const_iterator d =
1569 command.GetDepends().begin();
1570 d != command.GetDepends().end();
1571 ++d)
1573 // Get the real name of the dependency in case it is a CMake target.
1574 std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
1575 fout << this->ConvertToXMLOutputPath(dep.c_str())
1576 << ";";
1579 fout << "\"\n";
1580 fout << "\t\t\t\t\tOutputs=\"";
1581 if(command.GetOutputs().empty())
1583 fout << source << "_force";
1585 else
1587 // Write a rule for the output generated by this command.
1588 const char* sep = "";
1589 for(std::vector<std::string>::const_iterator o =
1590 command.GetOutputs().begin();
1591 o != command.GetOutputs().end();
1592 ++o)
1594 fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
1595 sep = ";";
1598 fout << "\"/>\n";
1599 fout << "\t\t\t\t</FileConfiguration>\n";
1604 void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
1605 const char* group,
1606 const char* )
1608 fout << "\t\t<Filter\n"
1609 << "\t\t\tName=\"" << group << "\"\n"
1610 << "\t\t\tFilter=\"\">\n";
1614 void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
1616 fout << "\t\t</Filter>\n";
1620 // look for custom rules on a target and collect them together
1621 void cmLocalVisualStudio7Generator
1622 ::OutputTargetRules(std::ostream& fout,
1623 const char* configName,
1624 cmTarget &target,
1625 const char * /*libName*/)
1627 if (target.GetType() > cmTarget::GLOBAL_TARGET)
1629 return;
1631 const char* tool = "VCPreBuildEventTool";
1632 if(this->FortranProject)
1634 tool = "VFPreBuildEventTool";
1636 // add the pre build rules
1637 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"";
1638 bool init = false;
1639 for (std::vector<cmCustomCommand>::const_iterator cr =
1640 target.GetPreBuildCommands().begin();
1641 cr != target.GetPreBuildCommands().end(); ++cr)
1643 if(!init)
1645 const char* comment = cr->GetComment();
1646 if(comment && *comment)
1648 fout << "\nDescription=\""
1649 << this->EscapeForXML(comment) << "\"";
1651 fout << "\nCommandLine=\"";
1652 init = true;
1654 else
1656 fout << this->EscapeForXML("\n");
1658 std::string script =
1659 this->ConstructScript(cr->GetCommandLines(),
1660 cr->GetWorkingDirectory(),
1661 configName,
1662 cr->GetEscapeOldStyle(),
1663 cr->GetEscapeAllowMakeVars());
1664 fout << this->EscapeForXML(script.c_str()).c_str();
1666 if (init)
1668 fout << "\"";
1670 fout << "/>\n";
1672 // add the pre Link rules
1673 tool = "VCPreLinkEventTool";
1674 if(this->FortranProject)
1676 tool = "VFPreLinkEventTool";
1678 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"";
1679 init = false;
1680 for (std::vector<cmCustomCommand>::const_iterator cr =
1681 target.GetPreLinkCommands().begin();
1682 cr != target.GetPreLinkCommands().end(); ++cr)
1684 if(!init)
1686 const char* comment = cr->GetComment();
1687 if(comment && *comment)
1689 fout << "\nDescription=\""
1690 << this->EscapeForXML(comment) << "\"";
1692 fout << "\nCommandLine=\"";
1693 init = true;
1695 else
1697 fout << this->EscapeForXML("\n");
1699 std::string script =
1700 this->ConstructScript(cr->GetCommandLines(),
1701 cr->GetWorkingDirectory(),
1702 configName,
1703 cr->GetEscapeOldStyle(),
1704 cr->GetEscapeAllowMakeVars());
1705 fout << this->EscapeForXML(script.c_str()).c_str();
1707 if (init)
1709 fout << "\"";
1711 fout << "/>\n";
1713 // add the PostBuild rules
1714 tool = "VCPostBuildEventTool";
1715 if(this->FortranProject)
1717 tool = "VFPostBuildEventTool";
1719 fout << "\t\t\t<Tool\n\t\t\t\tName=\"" << tool << "\"";
1720 init = false;
1721 for (std::vector<cmCustomCommand>::const_iterator cr =
1722 target.GetPostBuildCommands().begin();
1723 cr != target.GetPostBuildCommands().end(); ++cr)
1725 if(!init)
1727 const char* comment = cr->GetComment();
1728 if(comment && *comment)
1730 fout << "\nDescription=\""
1731 << this->EscapeForXML(comment) << "\"";
1733 fout << "\nCommandLine=\"";
1734 init = true;
1736 else
1738 fout << this->EscapeForXML("\n");
1740 std::string script =
1741 this->ConstructScript(cr->GetCommandLines(),
1742 cr->GetWorkingDirectory(),
1743 configName,
1744 cr->GetEscapeOldStyle(),
1745 cr->GetEscapeAllowMakeVars());
1746 fout << this->EscapeForXML(script.c_str()).c_str();
1748 if (init)
1750 fout << "\"";
1752 fout << "/>\n";
1755 void
1756 cmLocalVisualStudio7Generator
1757 ::WriteProjectStartFortran(std::ostream& fout,
1758 const char *libName,
1759 cmTarget & target)
1762 cmGlobalVisualStudio7Generator* gg =
1763 static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
1764 fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
1765 << "<VisualStudioProject\n"
1766 << "\tProjectCreator=\"Intel Fortran\"\n"
1767 << "\tVersion=\"9.10\"\n";
1768 const char* keyword = target.GetProperty("VS_KEYWORD");
1769 if(!keyword)
1771 keyword = "Console Application";
1773 const char* projectType = 0;
1774 switch(target.GetType())
1776 case cmTarget::STATIC_LIBRARY:
1777 projectType = "typeStaticLibrary";
1778 if(keyword)
1780 keyword = "Static Library";
1782 break;
1783 case cmTarget::SHARED_LIBRARY:
1784 case cmTarget::MODULE_LIBRARY:
1785 projectType = "typeDynamicLibrary";
1786 if(!keyword)
1788 keyword = "Dll";
1790 break;
1791 case cmTarget::EXECUTABLE:
1792 if(!keyword)
1794 keyword = "Console Application";
1796 projectType = 0;
1797 break;
1798 case cmTarget::UTILITY:
1799 case cmTarget::GLOBAL_TARGET:
1800 default:
1801 break;
1803 if(projectType)
1805 fout << "\tProjectType=\"" << projectType << "\"\n";
1807 fout<< "\tKeyword=\"" << keyword << "\">\n"
1808 << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\">\n"
1809 << "\t<Platforms>\n"
1810 << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
1811 << "\t</Platforms>\n";
1815 void
1816 cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
1817 const char *libName,
1818 cmTarget & target,
1819 std::vector<cmSourceGroup> &)
1821 if(this->FortranProject)
1823 this->WriteProjectStartFortran(fout, libName, target);
1824 return;
1826 fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
1827 << "<VisualStudioProject\n"
1828 << "\tProjectType=\"Visual C++\"\n";
1829 if(this->Version == 71)
1831 fout << "\tVersion=\"7.10\"\n";
1833 else
1835 fout << "\tVersion=\"" << this->Version << ".00\"\n";
1837 const char* projLabel = target.GetProperty("PROJECT_LABEL");
1838 if(!projLabel)
1840 projLabel = libName;
1842 const char* keyword = target.GetProperty("VS_KEYWORD");
1843 if(!keyword)
1845 keyword = "Win32Proj";
1847 const char* vsProjectname = target.GetProperty("VS_SCC_PROJECTNAME");
1848 const char* vsLocalpath = target.GetProperty("VS_SCC_LOCALPATH");
1849 const char* vsProvider = target.GetProperty("VS_SCC_PROVIDER");
1850 cmGlobalVisualStudio7Generator* gg =
1851 static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
1852 fout << "\tName=\"" << projLabel << "\"\n";
1853 if(this->Version >= 8)
1855 fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
1857 // if we have all the required Source code control tags
1858 // then add that to the project
1859 if(vsProvider && vsLocalpath && vsProjectname)
1861 fout << "\tSccProjectName=\"" << vsProjectname << "\"\n"
1862 << "\tSccLocalPath=\"" << vsLocalpath << "\"\n"
1863 << "\tSccProvider=\"" << vsProvider << "\"\n";
1865 fout << "\tKeyword=\"" << keyword << "\">\n"
1866 << "\t<Platforms>\n"
1867 << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
1868 << "\t</Platforms>\n";
1872 void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
1874 fout << "\t<Globals>\n"
1875 << "\t</Globals>\n"
1876 << "</VisualStudioProject>\n";
1879 std::string cmLocalVisualStudio7GeneratorEscapeForXML(const char* s)
1881 std::string ret = s;
1882 cmSystemTools::ReplaceString(ret, "&", "&amp;");
1883 cmSystemTools::ReplaceString(ret, "\"", "&quot;");
1884 cmSystemTools::ReplaceString(ret, "<", "&lt;");
1885 cmSystemTools::ReplaceString(ret, ">", "&gt;");
1886 cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
1887 return ret;
1890 std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
1892 return cmLocalVisualStudio7GeneratorEscapeForXML(s);
1895 std::string cmLocalVisualStudio7Generator
1896 ::ConvertToXMLOutputPath(const char* path)
1898 std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
1899 cmSystemTools::ReplaceString(ret, "&", "&amp;");
1900 cmSystemTools::ReplaceString(ret, "\"", "&quot;");
1901 cmSystemTools::ReplaceString(ret, "<", "&lt;");
1902 cmSystemTools::ReplaceString(ret, ">", "&gt;");
1903 return ret;
1906 std::string cmLocalVisualStudio7Generator
1907 ::ConvertToXMLOutputPathSingle(const char* path)
1909 std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
1910 cmSystemTools::ReplaceString(ret, "\"", "");
1911 cmSystemTools::ReplaceString(ret, "&", "&amp;");
1912 cmSystemTools::ReplaceString(ret, "<", "&lt;");
1913 cmSystemTools::ReplaceString(ret, ">", "&gt;");
1914 return ret;
1918 // This class is used to parse an existing vs 7 project
1919 // and extract the GUID
1920 class cmVS7XMLParser : public cmXMLParser
1922 public:
1923 virtual void EndElement(const char* /* name */)
1926 virtual void StartElement(const char* name, const char** atts)
1928 // once the GUID is found do nothing
1929 if(this->GUID.size())
1931 return;
1933 int i =0;
1934 if(strcmp("VisualStudioProject", name) == 0)
1936 while(atts[i])
1938 if(strcmp(atts[i], "ProjectGUID") == 0)
1940 if(atts[i+1])
1942 this->GUID = atts[i+1];
1943 this->GUID = this->GUID.substr(1, this->GUID.size()-2);
1945 else
1947 this->GUID = "";
1949 return;
1951 ++i;
1955 int InitializeParser()
1957 int ret = cmXMLParser::InitializeParser();
1958 if(ret == 0)
1960 return ret;
1962 // visual studio projects have a strange encoding, but it is
1963 // really utf-8
1964 XML_SetEncoding(static_cast<XML_Parser>(this->Parser), "utf-8");
1965 return 1;
1967 std::string GUID;
1970 void cmLocalVisualStudio7Generator::ReadAndStoreExternalGUID(
1971 const char* name,
1972 const char* path)
1974 cmVS7XMLParser parser;
1975 parser.ParseFile(path);
1976 // if we can not find a GUID then create one
1977 if(parser.GUID.size() == 0)
1979 cmGlobalVisualStudio7Generator* gg =
1980 static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
1981 gg->CreateGUID(name);
1982 return;
1984 std::string guidStoreName = name;
1985 guidStoreName += "_GUID_CMAKE";
1986 // save the GUID in the cache
1987 this->GlobalGenerator->GetCMakeInstance()->
1988 AddCacheEntry(guidStoreName.c_str(),
1989 parser.GUID.c_str(),
1990 "Stored GUID",
1991 cmCacheManager::INTERNAL);
1995 void cmLocalVisualStudio7Generator::ConfigureFinalPass()
1997 cmLocalGenerator::ConfigureFinalPass();
1998 cmTargets &tgts = this->Makefile->GetTargets();
2000 cmGlobalVisualStudio7Generator* gg =
2001 static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
2002 for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
2004 if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
2006 cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
2007 const cmCustomCommandLines& cmds = cc.GetCommandLines();
2008 std::string project_name = cmds[0][0];
2009 this->ReadAndStoreExternalGUID(project_name.c_str(),
2010 cmds[0][1].c_str());
2012 else
2014 gg->CreateGUID(l->first.c_str());
2020 //----------------------------------------------------------------------------
2021 std::string cmLocalVisualStudio7Generator
2022 ::GetTargetDirectory(cmTarget const& target) const
2024 std::string dir;
2025 dir += target.GetName();
2026 dir += ".dir";
2027 return dir;
2030 //----------------------------------------------------------------------------
2031 cmLocalVisualStudio7GeneratorOptions
2032 ::cmLocalVisualStudio7GeneratorOptions(cmLocalVisualStudio7Generator* lg,
2033 int version,
2034 Tool tool,
2035 cmVS7FlagTable const* extraTable):
2036 LocalGenerator(lg), Version(version), CurrentTool(tool),
2037 DoingDefine(false), FlagTable(0), ExtraFlagTable(extraTable)
2039 // Choose the flag table for the requested tool.
2040 switch(tool)
2042 case Compiler:
2043 this->FlagTable = cmLocalVisualStudio7GeneratorFlagTable; break;
2044 case Linker:
2045 this->FlagTable = cmLocalVisualStudio7GeneratorLinkFlagTable; break;
2046 case FortranCompiler:
2047 this->FlagTable = cmLocalVisualStudio7GeneratorFortranFlagTable;
2048 default: break;
2052 //----------------------------------------------------------------------------
2053 void cmLocalVisualStudio7GeneratorOptions::FixExceptionHandlingDefault()
2055 // Exception handling is on by default because the platform file has
2056 // "/EHsc" in the flags. Normally, that will override this
2057 // initialization to off, but the user has the option of removing
2058 // the flag to disable exception handling. When the user does
2059 // remove the flag we need to override the IDE default of on.
2060 switch (this->Version)
2062 case 7:
2063 case 71:
2064 this->FlagMap["ExceptionHandling"] = "FALSE";
2065 break;
2067 default:
2068 this->FlagMap["ExceptionHandling"] = "0";
2069 break;
2073 //----------------------------------------------------------------------------
2074 void cmLocalVisualStudio7GeneratorOptions::SetVerboseMakefile(bool verbose)
2076 // If verbose makefiles have been requested and the /nologo option
2077 // was not given explicitly in the flags we want to add an attribute
2078 // to the generated project to disable logo suppression. Otherwise
2079 // the GUI default is to enable suppression.
2080 if(verbose &&
2081 this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end())
2083 this->FlagMap["SuppressStartupBanner"] = "FALSE";
2087 //----------------------------------------------------------------------------
2088 void cmLocalVisualStudio7GeneratorOptions::AddDefine(const std::string& def)
2090 this->Defines.push_back(def);
2093 //----------------------------------------------------------------------------
2094 void cmLocalVisualStudio7GeneratorOptions::AddDefines(const char* defines)
2096 if(defines)
2098 // Expand the list of definitions.
2099 cmSystemTools::ExpandListArgument(defines, this->Defines);
2103 //----------------------------------------------------------------------------
2104 void cmLocalVisualStudio7GeneratorOptions::AddFlag(const char* flag,
2105 const char* value)
2107 this->FlagMap[flag] = value;
2111 bool cmLocalVisualStudio7GeneratorOptions::IsDebug()
2113 return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
2116 //----------------------------------------------------------------------------
2117 bool cmLocalVisualStudio7GeneratorOptions::UsingUnicode()
2119 // Look for the a _UNICODE definition.
2120 for(std::vector<std::string>::const_iterator di = this->Defines.begin();
2121 di != this->Defines.end(); ++di)
2123 if(*di == "_UNICODE")
2125 return true;
2128 return false;
2131 //----------------------------------------------------------------------------
2132 void cmLocalVisualStudio7GeneratorOptions::Parse(const char* flags)
2134 // Parse the input string as a windows command line since the string
2135 // is intended for writing directly into the build files.
2136 std::vector<std::string> args;
2137 cmSystemTools::ParseWindowsCommandLine(flags, args);
2139 // Process flags that need to be represented specially in the IDE
2140 // project file.
2141 for(std::vector<std::string>::iterator ai = args.begin();
2142 ai != args.end(); ++ai)
2144 this->HandleFlag(ai->c_str());
2148 //----------------------------------------------------------------------------
2149 void cmLocalVisualStudio7GeneratorOptions::HandleFlag(const char* flag)
2151 // If the last option was -D then this option is the definition.
2152 if(this->DoingDefine)
2154 this->DoingDefine = false;
2155 this->Defines.push_back(flag);
2156 return;
2159 // Look for known arguments.
2160 if(flag[0] == '-' || flag[0] == '/')
2162 // Look for preprocessor definitions.
2163 if(this->CurrentTool == Compiler && flag[1] == 'D')
2165 if(flag[2] == '\0')
2167 // The next argument will have the definition.
2168 this->DoingDefine = true;
2170 else
2172 // Store this definition.
2173 this->Defines.push_back(flag+2);
2175 return;
2178 // Look through the available flag tables.
2179 bool flag_handled = false;
2180 if(this->FlagTable &&
2181 this->CheckFlagTable(this->FlagTable, flag, flag_handled))
2183 return;
2185 if(this->ExtraFlagTable &&
2186 this->CheckFlagTable(this->ExtraFlagTable, flag, flag_handled))
2188 return;
2191 // If any map entry handled the flag we are done.
2192 if(flag_handled)
2194 return;
2197 // This option is not known. Store it in the output flags.
2198 this->FlagString += " ";
2199 this->FlagString +=
2200 cmSystemTools::EscapeWindowsShellArgument(
2201 flag,
2202 cmsysSystem_Shell_Flag_AllowMakeVariables |
2203 cmsysSystem_Shell_Flag_VSIDE);
2206 //----------------------------------------------------------------------------
2207 bool
2208 cmLocalVisualStudio7GeneratorOptions
2209 ::CheckFlagTable(cmVS7FlagTable const* table, const char* flag,
2210 bool& flag_handled)
2212 // Look for an entry in the flag table matching this flag.
2213 for(cmVS7FlagTable const* entry = table; entry->IDEName; ++entry)
2215 bool entry_found = false;
2216 if(entry->special & cmVS7FlagTable::UserValue)
2218 // This flag table entry accepts a user-specified value. If
2219 // the entry specifies UserRequired we must match only if a
2220 // non-empty value is given.
2221 int n = static_cast<int>(strlen(entry->commandFlag));
2222 if(strncmp(flag+1, entry->commandFlag, n) == 0 &&
2223 (!(entry->special & cmVS7FlagTable::UserRequired) ||
2224 static_cast<int>(strlen(flag+1)) > n))
2226 if(entry->special & cmVS7FlagTable::UserIgnored)
2228 // Ignore the user-specified value.
2229 this->FlagMap[entry->IDEName] = entry->value;
2231 else if(entry->special & cmVS7FlagTable::SemicolonAppendable)
2233 const char *new_value = flag+1+n;
2235 std::map<cmStdString,cmStdString>::iterator itr;
2236 itr = this->FlagMap.find(entry->IDEName);
2237 if(itr != this->FlagMap.end())
2239 // Append to old value (if present) with semicolons;
2240 itr->second += ";";
2241 itr->second += new_value;
2243 else
2245 this->FlagMap[entry->IDEName] = new_value;
2248 else
2250 // Use the user-specified value.
2251 this->FlagMap[entry->IDEName] = flag+1+n;
2253 entry_found = true;
2256 else if(strcmp(flag+1, entry->commandFlag) == 0)
2258 // This flag table entry provides a fixed value.
2259 this->FlagMap[entry->IDEName] = entry->value;
2260 entry_found = true;
2263 // If the flag has been handled by an entry not requesting a
2264 // search continuation we are done.
2265 if(entry_found && !(entry->special & cmVS7FlagTable::Continue))
2267 return true;
2270 // If the entry was found the flag has been handled.
2271 flag_handled = flag_handled || entry_found;
2274 return false;
2277 //----------------------------------------------------------------------------
2278 void
2279 cmLocalVisualStudio7GeneratorOptions
2280 ::OutputPreprocessorDefinitions(std::ostream& fout,
2281 const char* prefix,
2282 const char* suffix)
2284 if(this->Defines.empty())
2286 return;
2289 fout << prefix << "PreprocessorDefinitions=\"";
2290 const char* comma = "";
2291 for(std::vector<std::string>::const_iterator di = this->Defines.begin();
2292 di != this->Defines.end(); ++di)
2294 // Escape the definition for the compiler.
2295 std::string define =
2296 this->LocalGenerator->EscapeForShell(di->c_str(), true);
2298 // Escape this flag for the IDE.
2299 define = cmLocalVisualStudio7GeneratorEscapeForXML(define.c_str());
2301 // Store the flag in the project file.
2302 fout << comma << define;
2303 comma = ",";
2305 fout << "\"" << suffix;
2308 //----------------------------------------------------------------------------
2309 void
2310 cmLocalVisualStudio7GeneratorOptions
2311 ::OutputFlagMap(std::ostream& fout, const char* indent)
2313 for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
2314 m != this->FlagMap.end(); ++m)
2316 fout << indent << m->first << "=\"" << m->second << "\"\n";
2320 //----------------------------------------------------------------------------
2321 void
2322 cmLocalVisualStudio7GeneratorOptions
2323 ::OutputAdditionalOptions(std::ostream& fout,
2324 const char* prefix,
2325 const char* suffix)
2327 if(!this->FlagString.empty())
2329 fout << prefix << "AdditionalOptions=\"";
2330 fout <<
2331 cmLocalVisualStudio7GeneratorEscapeForXML(this->FlagString.c_str());
2332 fout << "\"" << suffix;
2335 void cmLocalVisualStudio7Generator::
2336 GetTargetObjectFileDirectories(cmTarget* target,
2337 std::vector<std::string>&
2338 dirs)
2340 std::string dir = this->Makefile->GetCurrentOutputDirectory();
2341 dir += "/";
2342 dir += this->GetTargetDirectory(*target);
2343 dir += "/";
2344 dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
2345 dirs.push_back(dir);
2348 //----------------------------------------------------------------------------
2349 #include <windows.h>
2350 static bool cmLVS6G_IsFAT(const char* dir)
2352 if(dir[0] && dir[1] == ':')
2354 char volRoot[4] = "_:/";
2355 volRoot[0] = dir[0];
2356 char fsName[16];
2357 if(GetVolumeInformation(volRoot, 0, 0, 0, 0, 0, fsName, 16) &&
2358 strstr(fsName, "FAT") != 0)
2360 return true;
2363 return false;