1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGlobalVisualStudio7Generator.cxx,v $
6 Date: $Date: 2008/01/30 21:22:51 $
7 Version: $Revision: 1.98 $
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_Fortran", "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();
198 // If any solution or project files changed during the generation,
199 // tell Visual Studio to reload them...
200 if(!cmSystemTools::GetErrorOccuredFlag())
202 this->CallVisualStudioMacro(MacroReload
);
206 void cmGlobalVisualStudio7Generator
207 ::OutputSLNFile(cmLocalGenerator
* root
,
208 std::vector
<cmLocalGenerator
*>& generators
)
210 if(generators
.size() == 0)
214 this->CurrentProject
= root
->GetMakefile()->GetProjectName();
215 std::string fname
= root
->GetMakefile()->GetStartOutputDirectory();
217 fname
+= root
->GetMakefile()->GetProjectName();
219 cmGeneratedFileStream
fout(fname
.c_str());
220 fout
.SetCopyIfDifferent(true);
225 this->WriteSLNFile(fout
, root
, generators
);
228 this->FileReplacedDuringGenerate(fname
);
232 // output the SLN file
233 void cmGlobalVisualStudio7Generator::OutputSLNFile()
235 std::map
<cmStdString
, std::vector
<cmLocalGenerator
*> >::iterator it
;
236 for(it
= this->ProjectMap
.begin(); it
!= this->ProjectMap
.end(); ++it
)
238 this->OutputSLNFile(it
->second
[0], it
->second
);
243 void cmGlobalVisualStudio7Generator::AddAllBuildDepends(
244 cmLocalGenerator
* root
,
246 cmGlobalGenerator::TargetDependSet
& originalTargets
)
248 // if this is the special ALL_BUILD utility, then
249 // make it depend on every other non UTILITY project.
250 for(cmGlobalGenerator::TargetDependSet::iterator ot
=
251 originalTargets
.begin(); ot
!= originalTargets
.end(); ++ot
)
253 cmTarget
* t
= const_cast<cmTarget
*>(*ot
);
254 if(!this->IsExcluded(root
, *t
))
256 if (t
->GetType() == cmTarget::UTILITY
||
257 t
->GetType() == cmTarget::GLOBAL_TARGET
)
259 target
->AddUtility(t
->GetName());
263 target
->AddLinkLibrary(t
->GetName(),cmTarget::GENERAL
);
269 void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
271 cmLocalGenerator
* root
,
272 cmGlobalGenerator::TargetDependSet
& projectTargets
)
274 // loop over again and write out configurations for each target
276 for(cmGlobalGenerator::TargetDependSet::iterator tt
=
277 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
279 cmTarget
* target
= const_cast<cmTarget
*>(*tt
);
280 if (strncmp(target
->GetName(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
282 cmCustomCommand cc
= target
->GetPostBuildCommands()[0];
283 const cmCustomCommandLines
& cmds
= cc
.GetCommandLines();
284 std::string project
= cmds
[0][0];
285 this->WriteProjectConfigurations(fout
, project
.c_str(),
290 bool partOfDefaultBuild
= this->IsPartOfDefaultBuild(
291 root
->GetMakefile()->GetProjectName(), target
);
292 const char *vcprojName
=
293 target
->GetProperty("GENERATOR_FILE_NAME");
296 this->WriteProjectConfigurations(fout
, vcprojName
,
304 void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
306 cmLocalGenerator
* root
,
307 cmGlobalGenerator::TargetDependSet
& projectTargets
,
308 cmGlobalGenerator::TargetDependSet
& originalTargets
311 std::string rootdir
= root
->GetMakefile()->GetStartOutputDirectory();
313 for(cmGlobalGenerator::TargetDependSet::iterator tt
=
314 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
316 cmTarget
* target
= const_cast<cmTarget
*>(*tt
);
317 cmMakefile
* mf
= target
->GetMakefile();
318 // look for the all_build rule and add depends to all
319 // of the original targets (none that were "pulled" into this project)
320 if(mf
== root
->GetMakefile() &&
321 strcmp(target
->GetName(), "ALL_BUILD") == 0)
323 this->AddAllBuildDepends(root
, target
, originalTargets
);
325 // handle external vc project files
326 if (strncmp(target
->GetName(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
328 cmCustomCommand cc
= target
->GetPostBuildCommands()[0];
329 const cmCustomCommandLines
& cmds
= cc
.GetCommandLines();
330 std::string project
= cmds
[0][0];
331 std::string location
= cmds
[0][1];
332 this->WriteExternalProject(fout
, project
.c_str(),
333 location
.c_str(), cc
.GetDepends());
338 // if it is a global target or the check build system target
339 // then only use the one that is for the root
340 if(target
->GetType() == cmTarget::GLOBAL_TARGET
341 || !strcmp(target
->GetName(), CMAKE_CHECK_BUILD_SYSTEM_TARGET
))
343 if(target
->GetMakefile() != root
->GetMakefile())
348 // if not skipping the project then write it into the
352 const char *vcprojName
=
353 target
->GetProperty("GENERATOR_FILE_NAME");
356 cmMakefile
* tmf
= target
->GetMakefile();
357 std::string dir
= tmf
->GetStartOutputDirectory();
358 dir
= root
->Convert(dir
.c_str(),
359 cmLocalGenerator::START_OUTPUT
);
360 this->WriteProject(fout
, vcprojName
, dir
.c_str(),
369 void cmGlobalVisualStudio7Generator::WriteTargetDepends(
371 cmGlobalGenerator::TargetDependSet
& projectTargets
374 for(cmGlobalGenerator::TargetDependSet::iterator tt
=
375 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
377 cmTarget
* target
= const_cast<cmTarget
*>(*tt
);
378 cmMakefile
* mf
= target
->GetMakefile();
379 if (strncmp(target
->GetName(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
381 cmCustomCommand cc
= target
->GetPostBuildCommands()[0];
382 const cmCustomCommandLines
& cmds
= cc
.GetCommandLines();
383 std::string name
= cmds
[0][0];
384 std::vector
<std::string
> depends
= cc
.GetDepends();
385 std::vector
<std::string
>::iterator iter
;
387 for(iter
= depends
.begin(); iter
!= depends
.end(); ++iter
)
389 std::string guid
= this->GetGUID(iter
->c_str());
392 std::string m
= "Target: ";
393 m
+= target
->GetName();
394 m
+= " depends on unknown target: ";
396 cmSystemTools::Error(m
.c_str());
399 fout
<< "\t\t{" << this->GetGUID(name
.c_str())
400 << "}." << depcount
<< " = {" << guid
.c_str() << "}\n";
406 const char *vcprojName
=
407 target
->GetProperty("GENERATOR_FILE_NAME");
410 std::string dir
= mf
->GetStartDirectory();
411 this->WriteProjectDepends(fout
, vcprojName
,
412 dir
.c_str(), *target
);
417 // Write a SLN file to the stream
418 void cmGlobalVisualStudio7Generator
419 ::WriteSLNFile(std::ostream
& fout
,
420 cmLocalGenerator
* root
,
421 std::vector
<cmLocalGenerator
*>& generators
)
423 // Write out the header for a SLN file
424 this->WriteSLNHeader(fout
);
426 // collect the set of targets for this project by
427 // tracing depends of all targets.
428 // also collect the set of targets that are explicitly
430 cmGlobalGenerator::TargetDependSet projectTargets
;
431 cmGlobalGenerator::TargetDependSet originalTargets
;
432 this->GetTargetSets(projectTargets
,
435 this->WriteTargetsToSolution(fout
, root
, projectTargets
, originalTargets
);
436 // Write out the configurations information for the solution
438 << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
441 for(std::vector
<std::string
>::iterator i
= this->Configurations
.begin();
442 i
!= this->Configurations
.end(); ++i
)
444 fout
<< "\t\tConfigName." << c
<< " = " << *i
<< "\n";
447 fout
<< "\tEndGlobalSection\n";
448 // Write out project(target) depends
449 fout
<< "\tGlobalSection(ProjectDependencies) = postSolution\n";
450 this->WriteTargetDepends(fout
, projectTargets
);
451 fout
<< "\tEndGlobalSection\n";
453 // Write out the configurations for all the targets in the project
454 fout
<< "\tGlobalSection(ProjectConfiguration) = postSolution\n";
455 this->WriteTargetConfigurations(fout
, root
, projectTargets
);
456 fout
<< "\tEndGlobalSection\n";
458 // Write the footer for the SLN file
459 this->WriteSLNFooter(fout
);
462 //----------------------------------------------------------------------------
464 cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path
)
466 // Convert to backslashes. Do not use ConvertToOutputPath because
467 // we will add quoting ourselves, and we know these projects always
468 // use windows slashes.
469 std::string d
= path
;
470 std::string::size_type pos
= 0;
471 while((pos
= d
.find('/', pos
)) != d
.npos
)
478 // Write a dsp file into the SLN file,
479 // Note, that dependencies from executables to
480 // the libraries it uses are also done here
481 void cmGlobalVisualStudio7Generator::WriteProject(std::ostream
& fout
,
483 const char* dir
, cmTarget
&)
485 fout
<< "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
486 << dspname
<< "\", \""
487 << this->ConvertToSolutionPath(dir
)
488 << "\\" << dspname
<< ".vcproj\", \"{"
489 << this->GetGUID(dspname
) << "}\"\nEndProject\n";
494 // Write a dsp file into the SLN file,
495 // Note, that dependencies from executables to
496 // the libraries it uses are also done here
498 cmGlobalVisualStudio7Generator
499 ::WriteProjectDepends(std::ostream
& fout
,
501 const char*, cmTarget
& target
)
504 // insert Begin Project Dependency Project_Dep_Name project stuff here
505 if (target
.GetType() != cmTarget::STATIC_LIBRARY
)
507 cmTarget::LinkLibraryVectorType::const_iterator j
, jend
;
508 j
= target
.GetLinkLibraries().begin();
509 jend
= target
.GetLinkLibraries().end();
512 if(j
->first
!= dspname
)
514 // is the library part of this SLN ? If so add dependency
515 if(this->FindTarget(0, j
->first
.c_str()))
517 std::string guid
= this->GetGUID(j
->first
.c_str());
520 std::string m
= "Target: ";
522 m
+= " depends on unknown target: ";
523 m
+= j
->first
.c_str();
524 cmSystemTools::Error(m
.c_str());
526 fout
<< "\t\t{" << this->GetGUID(dspname
) << "}."
527 << depcount
<< " = {" << guid
<< "}\n";
534 std::set
<cmStdString
>::const_iterator i
, end
;
535 // write utility dependencies.
536 i
= target
.GetUtilities().begin();
537 end
= target
.GetUtilities().end();
542 std::string name
= this->GetUtilityForTarget(target
, i
->c_str());
543 std::string guid
= this->GetGUID(name
.c_str());
546 std::string m
= "Target: ";
548 m
+= " depends on unknown target: ";
550 cmSystemTools::Error(m
.c_str());
553 fout
<< "\t\t{" << this->GetGUID(dspname
) << "}." << depcount
<< " = {"
561 // Write a dsp file into the SLN file, Note, that dependencies from
562 // executables to the libraries it uses are also done here
563 void cmGlobalVisualStudio7Generator
564 ::WriteProjectConfigurations(std::ostream
& fout
, const char* name
,
565 bool partOfDefaultBuild
)
567 std::string guid
= this->GetGUID(name
);
568 for(std::vector
<std::string
>::iterator i
= this->Configurations
.begin();
569 i
!= this->Configurations
.end(); ++i
)
571 fout
<< "\t\t{" << guid
<< "}." << *i
572 << ".ActiveCfg = " << *i
<< "|Win32\n";
573 if(partOfDefaultBuild
)
575 fout
<< "\t\t{" << guid
<< "}." << *i
576 << ".Build.0 = " << *i
<< "|Win32\n";
583 // Write a dsp file into the SLN file,
584 // Note, that dependencies from executables to
585 // the libraries it uses are also done here
586 void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream
& fout
,
588 const char* location
,
589 const std::vector
<std::string
>&)
591 std::cout
<< "WriteExternalProject vs7\n";
592 std::string d
= cmSystemTools::ConvertToOutputPath(location
);
593 fout
<< "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
595 << this->ConvertToSolutionPath(location
) << "\", \"{"
596 << this->GetGUID(name
)
598 fout
<< "EndProject\n";
603 // Standard end of dsw file
604 void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream
& fout
)
606 fout
<< "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
607 << "\tEndGlobalSection\n"
608 << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
609 << "\tEndGlobalSection\n"
614 // ouput standard header for dsw file
615 void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream
& fout
)
617 fout
<< "Microsoft Visual Studio Solution File, Format Version 7.00\n";
620 std::string
cmGlobalVisualStudio7Generator::GetGUID(const char* name
)
622 std::string guidStoreName
= name
;
623 guidStoreName
+= "_GUID_CMAKE";
624 const char* storedGUID
=
625 this->CMakeInstance
->GetCacheDefinition(guidStoreName
.c_str());
628 return std::string(storedGUID
);
630 cmSystemTools::Error("Unknown Target referenced : ",
636 void cmGlobalVisualStudio7Generator::CreateGUID(const char* name
)
638 std::string guidStoreName
= name
;
639 guidStoreName
+= "_GUID_CMAKE";
640 if(this->CMakeInstance
->GetCacheDefinition(guidStoreName
.c_str()))
646 unsigned char *uidstr
;
648 UuidToString(&uid
,&uidstr
);
649 ret
= reinterpret_cast<char*>(uidstr
);
650 RpcStringFree(&uidstr
);
651 ret
= cmSystemTools::UpperCase(ret
);
652 this->CMakeInstance
->AddCacheEntry(guidStoreName
.c_str(),
653 ret
.c_str(), "Stored GUID",
654 cmCacheManager::INTERNAL
);
657 std::vector
<std::string
> *cmGlobalVisualStudio7Generator::GetConfigurations()
659 return &this->Configurations
;
662 //----------------------------------------------------------------------------
663 void cmGlobalVisualStudio7Generator
664 ::GetDocumentation(cmDocumentationEntry
& entry
) const
666 entry
.Name
= this->GetName();
667 entry
.Brief
= "Generates Visual Studio .NET 2002 project files.";
671 // make sure "special" targets have GUID's
672 void cmGlobalVisualStudio7Generator::Configure()
674 cmGlobalGenerator::Configure();
675 this->CreateGUID("ALL_BUILD");
676 this->CreateGUID("INSTALL");
677 this->CreateGUID("RUN_TESTS");
678 this->CreateGUID("EDIT_CACHE");
679 this->CreateGUID("REBUILD_CACHE");
680 this->CreateGUID("PACKAGE");
683 //----------------------------------------------------------------------------
685 cmGlobalVisualStudio7Generator
686 ::AppendDirectoryForConfig(const char* prefix
,
699 bool cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(const char* project
,
702 if(target
->GetPropertyAsBool("EXCLUDE_FROM_DEFAULT_BUILD"))
706 // if it is a utilitiy target then only make it part of the
707 // default build if another target depends on it
708 int type
= target
->GetType();
709 if (type
== cmTarget::GLOBAL_TARGET
)
713 if(type
== cmTarget::UTILITY
)
715 return this->IsDependedOn(project
, target
);
717 // default is to be part of the build
721 //----------------------------------------------------------------------------
722 static cmVS7FlagTable cmVS7ExtraFlagTable
[] =
724 // Precompiled header and related options. Note that the
725 // UsePrecompiledHeader entries are marked as "Continue" so that the
726 // corresponding PrecompiledHeaderThrough entry can be found.
727 {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
728 cmVS7FlagTable::UserValueIgnored
| cmVS7FlagTable::Continue
},
729 {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
730 cmVS7FlagTable::UserValueRequired
},
731 {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
732 cmVS7FlagTable::UserValueIgnored
| cmVS7FlagTable::Continue
},
733 {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
734 cmVS7FlagTable::UserValueRequired
},
736 // Exception handling mode. If no entries match, it will be FALSE.
737 {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE", 0},
738 {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE", 0},
739 // The EHa option does not have an IDE setting. Let it go to false,
740 // and have EHa passed on the command line by leaving out the table
745 cmVS7FlagTable
const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
747 return cmVS7ExtraFlagTable
;