1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGlobalVisualStudio7Generator.cxx,v $
6 Date: $Date: 2009-07-29 15:28:53 $
7 Version: $Revision: 1.108 $
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 "windows.h" // this must be first to define GetCurrentDirectory
18 #include "cmGlobalVisualStudio7Generator.h"
19 #include "cmGeneratedFileStream.h"
20 #include "cmLocalVisualStudio7Generator.h"
21 #include "cmMakefile.h"
24 cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator()
26 this->FindMakeProgramFile
= "CMakeVS7FindMake.cmake";
30 void cmGlobalVisualStudio7Generator
31 ::EnableLanguage(std::vector
<std::string
>const & lang
,
32 cmMakefile
*mf
, bool optional
)
34 mf
->AddDefinition("CMAKE_GENERATOR_CC", "cl");
35 mf
->AddDefinition("CMAKE_GENERATOR_CXX", "cl");
36 mf
->AddDefinition("CMAKE_GENERATOR_RC", "rc");
37 mf
->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
38 mf
->AddDefinition("CMAKE_GENERATOR_FC", "ifort");
40 this->AddPlatformDefinitions(mf
);
42 // Create list of configurations requested by user's cache, if any.
43 this->cmGlobalGenerator::EnableLanguage(lang
, mf
, optional
);
44 this->GenerateConfigurations(mf
);
46 // if this environment variable is set, then copy it to
47 // a static cache entry. It will be used by
48 // cmLocalGenerator::ConstructScript, to add an extra PATH
49 // to all custom commands. This is because the VS IDE
50 // does not use the environment it is run in, and this allows
51 // for running commands and using dll's that the IDE environment
52 // does not know about.
53 const char* extraPath
= cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH");
56 mf
->AddCacheDefinition
57 ("CMAKE_MSVCIDE_RUN_PATH", extraPath
,
58 "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
59 cmCacheManager::STATIC
);
64 void cmGlobalVisualStudio7Generator::AddPlatformDefinitions(cmMakefile
* mf
)
66 mf
->AddDefinition("MSVC70", "1");
69 std::string cmGlobalVisualStudio7Generator
70 ::GenerateBuildCommand(const char* makeProgram
,
71 const char *projectName
,
72 const char* additionalOptions
, const char *targetName
,
73 const char* config
, bool ignoreErrors
, bool)
75 // Ingoring errors is not implemented in visual studio 6
79 std::string makeCommand
=
80 cmSystemTools::ConvertToOutputPath(makeProgram
);
81 std::string lowerCaseCommand
= makeCommand
;
82 cmSystemTools::LowerCase(lowerCaseCommand
);
84 // if there are spaces in the makeCommand, assume a full path
85 // and convert it to a path with no spaces in it as the
86 // RunSingleCommand does not like spaces
87 #if defined(_WIN32) && !defined(__CYGWIN__)
88 if(makeCommand
.find(' ') != std::string::npos
)
90 cmSystemTools::GetShortPath(makeCommand
.c_str(), makeCommand
);
94 makeCommand
+= projectName
;
95 makeCommand
+= ".sln ";
97 if ( targetName
&& strcmp(targetName
, "clean") == 0 )
100 targetName
= "ALL_BUILD";
104 makeCommand
+= "/clean ";
108 makeCommand
+= "/build ";
111 if(config
&& strlen(config
))
113 makeCommand
+= config
;
117 makeCommand
+= "Debug";
119 makeCommand
+= " /project ";
121 if (targetName
&& strlen(targetName
))
123 makeCommand
+= targetName
;
127 makeCommand
+= "ALL_BUILD";
129 if ( additionalOptions
)
132 makeCommand
+= additionalOptions
;
137 ///! Create a local generator appropriate to this Global Generator
138 cmLocalGenerator
*cmGlobalVisualStudio7Generator::CreateLocalGenerator()
140 cmLocalVisualStudio7Generator
*lg
= new cmLocalVisualStudio7Generator
;
141 lg
->SetExtraFlagTable(this->GetExtraFlagTableVS7());
142 lg
->SetGlobalGenerator(this);
146 void cmGlobalVisualStudio7Generator::GenerateConfigurations(cmMakefile
* mf
)
148 // process the configurations
150 = this->CMakeInstance
->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
153 std::vector
<std::string
> argsOut
;
154 cmSystemTools::ExpandListArgument(ct
, argsOut
);
155 for(std::vector
<std::string
>::iterator i
= argsOut
.begin();
156 i
!= argsOut
.end(); ++i
)
158 if(std::find(this->Configurations
.begin(),
159 this->Configurations
.end(),
160 *i
) == this->Configurations
.end())
162 this->Configurations
.push_back(*i
);
166 // default to at least Debug and Release
167 if(this->Configurations
.size() == 0)
169 this->Configurations
.push_back("Debug");
170 this->Configurations
.push_back("Release");
173 // Reset the entry to have a semi-colon separated list.
174 std::string configs
= this->Configurations
[0];
175 for(unsigned int i
=1; i
< this->Configurations
.size(); ++i
)
178 configs
+= this->Configurations
[i
];
181 mf
->AddCacheDefinition(
182 "CMAKE_CONFIGURATION_TYPES",
184 "Semicolon separated list of supported configuration types, "
185 "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
186 "anything else will be ignored.",
187 cmCacheManager::STRING
);
190 void cmGlobalVisualStudio7Generator::Generate()
192 // first do the superclass method
193 this->cmGlobalVisualStudioGenerator::Generate();
195 // Now write out the DSW
196 this->OutputSLNFile();
197 // If any solution or project files changed during the generation,
198 // tell Visual Studio to reload them...
199 if(!cmSystemTools::GetErrorOccuredFlag())
201 this->CallVisualStudioMacro(MacroReload
);
205 void cmGlobalVisualStudio7Generator
206 ::OutputSLNFile(cmLocalGenerator
* root
,
207 std::vector
<cmLocalGenerator
*>& generators
)
209 if(generators
.size() == 0)
213 this->CurrentProject
= root
->GetMakefile()->GetProjectName();
214 std::string fname
= root
->GetMakefile()->GetStartOutputDirectory();
216 fname
+= root
->GetMakefile()->GetProjectName();
218 cmGeneratedFileStream
fout(fname
.c_str());
219 fout
.SetCopyIfDifferent(true);
224 this->WriteSLNFile(fout
, root
, generators
);
227 this->FileReplacedDuringGenerate(fname
);
231 // output the SLN file
232 void cmGlobalVisualStudio7Generator::OutputSLNFile()
234 std::map
<cmStdString
, std::vector
<cmLocalGenerator
*> >::iterator it
;
235 for(it
= this->ProjectMap
.begin(); it
!= this->ProjectMap
.end(); ++it
)
237 this->OutputSLNFile(it
->second
[0], it
->second
);
242 void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
244 cmLocalGenerator
* root
,
245 OrderedTargetDependSet
const& projectTargets
)
247 // loop over again and write out configurations for each target
249 for(OrderedTargetDependSet::const_iterator tt
=
250 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
252 cmTarget
* target
= *tt
;
253 const char* expath
= target
->GetProperty("EXTERNAL_MSPROJECT");
256 this->WriteProjectConfigurations(fout
, target
->GetName(),
261 bool partOfDefaultBuild
= this->IsPartOfDefaultBuild(
262 root
->GetMakefile()->GetProjectName(), target
);
263 const char *vcprojName
=
264 target
->GetProperty("GENERATOR_FILE_NAME");
267 this->WriteProjectConfigurations(fout
, vcprojName
,
275 void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
277 cmLocalGenerator
* root
,
278 OrderedTargetDependSet
const& projectTargets
)
280 std::string rootdir
= root
->GetMakefile()->GetStartOutputDirectory();
282 for(OrderedTargetDependSet::const_iterator tt
=
283 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
285 cmTarget
* target
= *tt
;
286 // handle external vc project files
287 const char* expath
= target
->GetProperty("EXTERNAL_MSPROJECT");
290 std::string project
= target
->GetName();
291 std::string location
= expath
;
292 this->WriteExternalProject(fout
, project
.c_str(),
293 location
.c_str(), target
->GetUtilities());
298 // if it is a global target or the check build system target
299 // or the all_build target
300 // then only use the one that is for the root
301 if(target
->GetType() == cmTarget::GLOBAL_TARGET
302 || !strcmp(target
->GetName(), CMAKE_CHECK_BUILD_SYSTEM_TARGET
)
303 || !strcmp(target
->GetName(), this->GetAllTargetName()))
305 if(target
->GetMakefile() != root
->GetMakefile())
310 // if not skipping the project then write it into the
314 const char *vcprojName
=
315 target
->GetProperty("GENERATOR_FILE_NAME");
318 cmMakefile
* tmf
= target
->GetMakefile();
319 std::string dir
= tmf
->GetStartOutputDirectory();
320 dir
= root
->Convert(dir
.c_str(),
321 cmLocalGenerator::START_OUTPUT
);
322 this->WriteProject(fout
, vcprojName
, dir
.c_str(),
331 void cmGlobalVisualStudio7Generator::WriteTargetDepends(
333 OrderedTargetDependSet
const& projectTargets
336 for(OrderedTargetDependSet::const_iterator tt
=
337 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
339 cmTarget
* target
= *tt
;
340 cmMakefile
* mf
= target
->GetMakefile();
341 const char *vcprojName
=
342 target
->GetProperty("GENERATOR_FILE_NAME");
345 std::string dir
= mf
->GetStartDirectory();
346 this->WriteProjectDepends(fout
, vcprojName
,
347 dir
.c_str(), *target
);
352 // Write a SLN file to the stream
353 void cmGlobalVisualStudio7Generator
354 ::WriteSLNFile(std::ostream
& fout
,
355 cmLocalGenerator
* root
,
356 std::vector
<cmLocalGenerator
*>& generators
)
358 // Write out the header for a SLN file
359 this->WriteSLNHeader(fout
);
361 // collect the set of targets for this project by
362 // tracing depends of all targets.
363 // also collect the set of targets that are explicitly
365 cmGlobalGenerator::TargetDependSet projectTargets
;
366 cmGlobalGenerator::TargetDependSet originalTargets
;
367 this->GetTargetSets(projectTargets
,
370 OrderedTargetDependSet
orderedProjectTargets(projectTargets
);
371 this->WriteTargetsToSolution(fout
, root
, orderedProjectTargets
);
372 // Write out the configurations information for the solution
374 << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
377 for(std::vector
<std::string
>::iterator i
= this->Configurations
.begin();
378 i
!= this->Configurations
.end(); ++i
)
380 fout
<< "\t\tConfigName." << c
<< " = " << *i
<< "\n";
383 fout
<< "\tEndGlobalSection\n";
384 // Write out project(target) depends
385 fout
<< "\tGlobalSection(ProjectDependencies) = postSolution\n";
386 this->WriteTargetDepends(fout
, orderedProjectTargets
);
387 fout
<< "\tEndGlobalSection\n";
389 // Write out the configurations for all the targets in the project
390 fout
<< "\tGlobalSection(ProjectConfiguration) = postSolution\n";
391 this->WriteTargetConfigurations(fout
, root
, orderedProjectTargets
);
392 fout
<< "\tEndGlobalSection\n";
394 // Write the footer for the SLN file
395 this->WriteSLNFooter(fout
);
398 //----------------------------------------------------------------------------
400 cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path
)
402 // Convert to backslashes. Do not use ConvertToOutputPath because
403 // we will add quoting ourselves, and we know these projects always
404 // use windows slashes.
405 std::string d
= path
;
406 std::string::size_type pos
= 0;
407 while((pos
= d
.find('/', pos
)) != d
.npos
)
414 // Write a dsp file into the SLN file,
415 // Note, that dependencies from executables to
416 // the libraries it uses are also done here
417 void cmGlobalVisualStudio7Generator::WriteProject(std::ostream
& fout
,
419 const char* dir
, cmTarget
& target
)
421 // check to see if this is a fortran build
422 const char* ext
= ".vcproj";
423 const char* project
=
424 "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"";
425 if(this->TargetIsFortranOnly(target
))
428 project
= "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
432 << dspname
<< "\", \""
433 << this->ConvertToSolutionPath(dir
)
434 << "\\" << dspname
<< ext
<< "\", \"{"
435 << this->GetGUID(dspname
) << "}\"\nEndProject\n";
440 // Write a dsp file into the SLN file,
441 // Note, that dependencies from executables to
442 // the libraries it uses are also done here
444 cmGlobalVisualStudio7Generator
445 ::WriteProjectDepends(std::ostream
& fout
,
447 const char*, cmTarget
& target
)
450 // insert Begin Project Dependency Project_Dep_Name project stuff here
451 if (target
.GetType() != cmTarget::STATIC_LIBRARY
)
453 cmTarget::LinkLibraryVectorType::const_iterator j
, jend
;
454 j
= target
.GetLinkLibraries().begin();
455 jend
= target
.GetLinkLibraries().end();
458 if(j
->first
!= dspname
)
460 // is the library part of this SLN ? If so add dependency
461 if(this->FindTarget(0, j
->first
.c_str()))
463 std::string guid
= this->GetGUID(j
->first
.c_str());
466 std::string m
= "Target: ";
468 m
+= " depends on unknown target: ";
469 m
+= j
->first
.c_str();
470 cmSystemTools::Error(m
.c_str());
472 fout
<< "\t\t{" << this->GetGUID(dspname
) << "}."
473 << depcount
<< " = {" << guid
<< "}\n";
480 std::set
<cmStdString
>::const_iterator i
, end
;
481 // write utility dependencies.
482 i
= target
.GetUtilities().begin();
483 end
= target
.GetUtilities().end();
488 std::string name
= this->GetUtilityForTarget(target
, i
->c_str());
489 std::string guid
= this->GetGUID(name
.c_str());
492 std::string m
= "Target: ";
494 m
+= " depends on unknown target: ";
496 cmSystemTools::Error(m
.c_str());
499 fout
<< "\t\t{" << this->GetGUID(dspname
) << "}." << depcount
<< " = {"
507 // Write a dsp file into the SLN file, Note, that dependencies from
508 // executables to the libraries it uses are also done here
509 void cmGlobalVisualStudio7Generator
510 ::WriteProjectConfigurations(std::ostream
& fout
, const char* name
,
511 bool partOfDefaultBuild
)
513 std::string guid
= this->GetGUID(name
);
514 for(std::vector
<std::string
>::iterator i
= this->Configurations
.begin();
515 i
!= this->Configurations
.end(); ++i
)
517 fout
<< "\t\t{" << guid
<< "}." << *i
518 << ".ActiveCfg = " << *i
<< "|Win32\n";
519 if(partOfDefaultBuild
)
521 fout
<< "\t\t{" << guid
<< "}." << *i
522 << ".Build.0 = " << *i
<< "|Win32\n";
529 // Write a dsp file into the SLN file,
530 // Note, that dependencies from executables to
531 // the libraries it uses are also done here
532 void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream
& fout
,
534 const char* location
,
535 const std::set
<cmStdString
>&)
537 std::string d
= cmSystemTools::ConvertToOutputPath(location
);
538 fout
<< "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
540 << this->ConvertToSolutionPath(location
) << "\", \"{"
541 << this->GetGUID(name
)
543 fout
<< "EndProject\n";
548 // Standard end of dsw file
549 void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream
& fout
)
551 fout
<< "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
552 << "\tEndGlobalSection\n"
553 << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
554 << "\tEndGlobalSection\n"
559 // ouput standard header for dsw file
560 void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream
& fout
)
562 fout
<< "Microsoft Visual Studio Solution File, Format Version 7.00\n";
565 std::string
cmGlobalVisualStudio7Generator::GetGUID(const char* name
)
567 std::string guidStoreName
= name
;
568 guidStoreName
+= "_GUID_CMAKE";
569 const char* storedGUID
=
570 this->CMakeInstance
->GetCacheDefinition(guidStoreName
.c_str());
573 return std::string(storedGUID
);
575 cmSystemTools::Error("Unknown Target referenced : ",
581 void cmGlobalVisualStudio7Generator::CreateGUID(const char* name
)
583 std::string guidStoreName
= name
;
584 guidStoreName
+= "_GUID_CMAKE";
585 if(this->CMakeInstance
->GetCacheDefinition(guidStoreName
.c_str()))
591 unsigned char *uidstr
;
593 UuidToString(&uid
,&uidstr
);
594 ret
= reinterpret_cast<char*>(uidstr
);
595 RpcStringFree(&uidstr
);
596 ret
= cmSystemTools::UpperCase(ret
);
597 this->CMakeInstance
->AddCacheEntry(guidStoreName
.c_str(),
598 ret
.c_str(), "Stored GUID",
599 cmCacheManager::INTERNAL
);
602 std::vector
<std::string
> *cmGlobalVisualStudio7Generator::GetConfigurations()
604 return &this->Configurations
;
607 //----------------------------------------------------------------------------
608 void cmGlobalVisualStudio7Generator
609 ::GetDocumentation(cmDocumentationEntry
& entry
) const
611 entry
.Name
= this->GetName();
612 entry
.Brief
= "Generates Visual Studio .NET 2002 project files.";
616 // make sure "special" targets have GUID's
617 void cmGlobalVisualStudio7Generator::Configure()
619 cmGlobalGenerator::Configure();
620 this->CreateGUID("ALL_BUILD");
621 this->CreateGUID("INSTALL");
622 this->CreateGUID("RUN_TESTS");
623 this->CreateGUID("EDIT_CACHE");
624 this->CreateGUID("REBUILD_CACHE");
625 this->CreateGUID("PACKAGE");
628 //----------------------------------------------------------------------------
630 cmGlobalVisualStudio7Generator
631 ::AppendDirectoryForConfig(const char* prefix
,
644 bool cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(const char* project
,
647 if(target
->GetPropertyAsBool("EXCLUDE_FROM_DEFAULT_BUILD"))
651 // if it is a utilitiy target then only make it part of the
652 // default build if another target depends on it
653 int type
= target
->GetType();
654 if (type
== cmTarget::GLOBAL_TARGET
)
658 if(type
== cmTarget::UTILITY
)
660 return this->IsDependedOn(project
, target
);
662 // default is to be part of the build
666 //----------------------------------------------------------------------------
668 cmGlobalVisualStudio7Generator::TargetCompare
669 ::operator()(cmTarget
const* l
, cmTarget
const* r
)
671 // Make sure ALL_BUILD is first so it is the default active project.
672 if(strcmp(r
->GetName(), "ALL_BUILD") == 0)
676 if(strcmp(l
->GetName(), "ALL_BUILD") == 0)
680 return strcmp(l
->GetName(), r
->GetName()) < 0;
683 //----------------------------------------------------------------------------
684 cmGlobalVisualStudio7Generator::OrderedTargetDependSet
685 ::OrderedTargetDependSet(cmGlobalGenerator::TargetDependSet
const& targets
)
687 for(cmGlobalGenerator::TargetDependSet::const_iterator ti
=
688 targets
.begin(); ti
!= targets
.end(); ++ti
)
694 //----------------------------------------------------------------------------
695 static cmVS7FlagTable cmVS7ExtraFlagTable
[] =
697 // Precompiled header and related options. Note that the
698 // UsePrecompiledHeader entries are marked as "Continue" so that the
699 // corresponding PrecompiledHeaderThrough entry can be found.
700 {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
701 cmVS7FlagTable::UserValueIgnored
| cmVS7FlagTable::Continue
},
702 {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
703 cmVS7FlagTable::UserValueRequired
},
704 {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
705 cmVS7FlagTable::UserValueIgnored
| cmVS7FlagTable::Continue
},
706 {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
707 cmVS7FlagTable::UserValueRequired
},
708 {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "TRUE", 0},
710 // Exception handling mode. If no entries match, it will be FALSE.
711 {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE", 0},
712 {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE", 0},
713 // The EHa option does not have an IDE setting. Let it go to false,
714 // and have EHa passed on the command line by leaving out the table
719 cmIDEFlagTable
const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
721 return cmVS7ExtraFlagTable
;