1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGlobalVisualStudio7Generator.cxx,v $
6 Date: $Date: 2008-10-01 13:04:27 $
7 Version: $Revision: 1.102 $
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();
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 // or the all_build target
340 // then only use the one that is for the root
341 if(target
->GetType() == cmTarget::GLOBAL_TARGET
342 || !strcmp(target
->GetName(), CMAKE_CHECK_BUILD_SYSTEM_TARGET
)
343 || !strcmp(target
->GetName(), this->GetAllTargetName()))
345 if(target
->GetMakefile() != root
->GetMakefile())
350 // if not skipping the project then write it into the
354 const char *vcprojName
=
355 target
->GetProperty("GENERATOR_FILE_NAME");
358 cmMakefile
* tmf
= target
->GetMakefile();
359 std::string dir
= tmf
->GetStartOutputDirectory();
360 dir
= root
->Convert(dir
.c_str(),
361 cmLocalGenerator::START_OUTPUT
);
362 this->WriteProject(fout
, vcprojName
, dir
.c_str(),
371 void cmGlobalVisualStudio7Generator::WriteTargetDepends(
373 cmGlobalGenerator::TargetDependSet
& projectTargets
376 for(cmGlobalGenerator::TargetDependSet::iterator tt
=
377 projectTargets
.begin(); tt
!= projectTargets
.end(); ++tt
)
379 cmTarget
* target
= const_cast<cmTarget
*>(*tt
);
380 cmMakefile
* mf
= target
->GetMakefile();
381 if (strncmp(target
->GetName(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
383 cmCustomCommand cc
= target
->GetPostBuildCommands()[0];
384 const cmCustomCommandLines
& cmds
= cc
.GetCommandLines();
385 std::string name
= cmds
[0][0];
386 std::vector
<std::string
> depends
= cc
.GetDepends();
387 std::vector
<std::string
>::iterator iter
;
389 for(iter
= depends
.begin(); iter
!= depends
.end(); ++iter
)
391 std::string guid
= this->GetGUID(iter
->c_str());
394 std::string m
= "Target: ";
395 m
+= target
->GetName();
396 m
+= " depends on unknown target: ";
398 cmSystemTools::Error(m
.c_str());
401 fout
<< "\t\t{" << this->GetGUID(name
.c_str())
402 << "}." << depcount
<< " = {" << guid
.c_str() << "}\n";
408 const char *vcprojName
=
409 target
->GetProperty("GENERATOR_FILE_NAME");
412 std::string dir
= mf
->GetStartDirectory();
413 this->WriteProjectDepends(fout
, vcprojName
,
414 dir
.c_str(), *target
);
419 // Write a SLN file to the stream
420 void cmGlobalVisualStudio7Generator
421 ::WriteSLNFile(std::ostream
& fout
,
422 cmLocalGenerator
* root
,
423 std::vector
<cmLocalGenerator
*>& generators
)
425 // Write out the header for a SLN file
426 this->WriteSLNHeader(fout
);
428 // collect the set of targets for this project by
429 // tracing depends of all targets.
430 // also collect the set of targets that are explicitly
432 cmGlobalGenerator::TargetDependSet projectTargets
;
433 cmGlobalGenerator::TargetDependSet originalTargets
;
434 this->GetTargetSets(projectTargets
,
437 this->WriteTargetsToSolution(fout
, root
, projectTargets
, originalTargets
);
438 // Write out the configurations information for the solution
440 << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
443 for(std::vector
<std::string
>::iterator i
= this->Configurations
.begin();
444 i
!= this->Configurations
.end(); ++i
)
446 fout
<< "\t\tConfigName." << c
<< " = " << *i
<< "\n";
449 fout
<< "\tEndGlobalSection\n";
450 // Write out project(target) depends
451 fout
<< "\tGlobalSection(ProjectDependencies) = postSolution\n";
452 this->WriteTargetDepends(fout
, projectTargets
);
453 fout
<< "\tEndGlobalSection\n";
455 // Write out the configurations for all the targets in the project
456 fout
<< "\tGlobalSection(ProjectConfiguration) = postSolution\n";
457 this->WriteTargetConfigurations(fout
, root
, projectTargets
);
458 fout
<< "\tEndGlobalSection\n";
460 // Write the footer for the SLN file
461 this->WriteSLNFooter(fout
);
464 //----------------------------------------------------------------------------
466 cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path
)
468 // Convert to backslashes. Do not use ConvertToOutputPath because
469 // we will add quoting ourselves, and we know these projects always
470 // use windows slashes.
471 std::string d
= path
;
472 std::string::size_type pos
= 0;
473 while((pos
= d
.find('/', pos
)) != d
.npos
)
480 // Write a dsp file into the SLN file,
481 // Note, that dependencies from executables to
482 // the libraries it uses are also done here
483 void cmGlobalVisualStudio7Generator::WriteProject(std::ostream
& fout
,
485 const char* dir
, cmTarget
& target
)
487 // check to see if this is a fortran build
488 const char* ext
= ".vcproj";
489 const char* project
=
490 "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"";
491 if(this->TargetIsFortranOnly(target
))
494 project
= "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
498 << dspname
<< "\", \""
499 << this->ConvertToSolutionPath(dir
)
500 << "\\" << dspname
<< ext
<< "\", \"{"
501 << this->GetGUID(dspname
) << "}\"\nEndProject\n";
506 // Write a dsp file into the SLN file,
507 // Note, that dependencies from executables to
508 // the libraries it uses are also done here
510 cmGlobalVisualStudio7Generator
511 ::WriteProjectDepends(std::ostream
& fout
,
513 const char*, cmTarget
& target
)
516 // insert Begin Project Dependency Project_Dep_Name project stuff here
517 if (target
.GetType() != cmTarget::STATIC_LIBRARY
)
519 cmTarget::LinkLibraryVectorType::const_iterator j
, jend
;
520 j
= target
.GetLinkLibraries().begin();
521 jend
= target
.GetLinkLibraries().end();
524 if(j
->first
!= dspname
)
526 // is the library part of this SLN ? If so add dependency
527 if(this->FindTarget(0, j
->first
.c_str()))
529 std::string guid
= this->GetGUID(j
->first
.c_str());
532 std::string m
= "Target: ";
534 m
+= " depends on unknown target: ";
535 m
+= j
->first
.c_str();
536 cmSystemTools::Error(m
.c_str());
538 fout
<< "\t\t{" << this->GetGUID(dspname
) << "}."
539 << depcount
<< " = {" << guid
<< "}\n";
546 std::set
<cmStdString
>::const_iterator i
, end
;
547 // write utility dependencies.
548 i
= target
.GetUtilities().begin();
549 end
= target
.GetUtilities().end();
554 std::string name
= this->GetUtilityForTarget(target
, i
->c_str());
555 std::string guid
= this->GetGUID(name
.c_str());
558 std::string m
= "Target: ";
560 m
+= " depends on unknown target: ";
562 cmSystemTools::Error(m
.c_str());
565 fout
<< "\t\t{" << this->GetGUID(dspname
) << "}." << depcount
<< " = {"
573 // Write a dsp file into the SLN file, Note, that dependencies from
574 // executables to the libraries it uses are also done here
575 void cmGlobalVisualStudio7Generator
576 ::WriteProjectConfigurations(std::ostream
& fout
, const char* name
,
577 bool partOfDefaultBuild
)
579 std::string guid
= this->GetGUID(name
);
580 for(std::vector
<std::string
>::iterator i
= this->Configurations
.begin();
581 i
!= this->Configurations
.end(); ++i
)
583 fout
<< "\t\t{" << guid
<< "}." << *i
584 << ".ActiveCfg = " << *i
<< "|Win32\n";
585 if(partOfDefaultBuild
)
587 fout
<< "\t\t{" << guid
<< "}." << *i
588 << ".Build.0 = " << *i
<< "|Win32\n";
595 // Write a dsp file into the SLN file,
596 // Note, that dependencies from executables to
597 // the libraries it uses are also done here
598 void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream
& fout
,
600 const char* location
,
601 const std::vector
<std::string
>&)
603 std::cout
<< "WriteExternalProject vs7\n";
604 std::string d
= cmSystemTools::ConvertToOutputPath(location
);
605 fout
<< "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
607 << this->ConvertToSolutionPath(location
) << "\", \"{"
608 << this->GetGUID(name
)
610 fout
<< "EndProject\n";
615 // Standard end of dsw file
616 void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream
& fout
)
618 fout
<< "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
619 << "\tEndGlobalSection\n"
620 << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
621 << "\tEndGlobalSection\n"
626 // ouput standard header for dsw file
627 void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream
& fout
)
629 fout
<< "Microsoft Visual Studio Solution File, Format Version 7.00\n";
632 std::string
cmGlobalVisualStudio7Generator::GetGUID(const char* name
)
634 std::string guidStoreName
= name
;
635 guidStoreName
+= "_GUID_CMAKE";
636 const char* storedGUID
=
637 this->CMakeInstance
->GetCacheDefinition(guidStoreName
.c_str());
640 return std::string(storedGUID
);
642 cmSystemTools::Error("Unknown Target referenced : ",
648 void cmGlobalVisualStudio7Generator::CreateGUID(const char* name
)
650 std::string guidStoreName
= name
;
651 guidStoreName
+= "_GUID_CMAKE";
652 if(this->CMakeInstance
->GetCacheDefinition(guidStoreName
.c_str()))
658 unsigned char *uidstr
;
660 UuidToString(&uid
,&uidstr
);
661 ret
= reinterpret_cast<char*>(uidstr
);
662 RpcStringFree(&uidstr
);
663 ret
= cmSystemTools::UpperCase(ret
);
664 this->CMakeInstance
->AddCacheEntry(guidStoreName
.c_str(),
665 ret
.c_str(), "Stored GUID",
666 cmCacheManager::INTERNAL
);
669 std::vector
<std::string
> *cmGlobalVisualStudio7Generator::GetConfigurations()
671 return &this->Configurations
;
674 //----------------------------------------------------------------------------
675 void cmGlobalVisualStudio7Generator
676 ::GetDocumentation(cmDocumentationEntry
& entry
) const
678 entry
.Name
= this->GetName();
679 entry
.Brief
= "Generates Visual Studio .NET 2002 project files.";
683 // make sure "special" targets have GUID's
684 void cmGlobalVisualStudio7Generator::Configure()
686 cmGlobalGenerator::Configure();
687 this->CreateGUID("ALL_BUILD");
688 this->CreateGUID("INSTALL");
689 this->CreateGUID("RUN_TESTS");
690 this->CreateGUID("EDIT_CACHE");
691 this->CreateGUID("REBUILD_CACHE");
692 this->CreateGUID("PACKAGE");
695 //----------------------------------------------------------------------------
697 cmGlobalVisualStudio7Generator
698 ::AppendDirectoryForConfig(const char* prefix
,
711 bool cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(const char* project
,
714 if(target
->GetPropertyAsBool("EXCLUDE_FROM_DEFAULT_BUILD"))
718 // if it is a utilitiy target then only make it part of the
719 // default build if another target depends on it
720 int type
= target
->GetType();
721 if (type
== cmTarget::GLOBAL_TARGET
)
725 if(type
== cmTarget::UTILITY
)
727 return this->IsDependedOn(project
, target
);
729 // default is to be part of the build
733 //----------------------------------------------------------------------------
734 static cmVS7FlagTable cmVS7ExtraFlagTable
[] =
736 // Precompiled header and related options. Note that the
737 // UsePrecompiledHeader entries are marked as "Continue" so that the
738 // corresponding PrecompiledHeaderThrough entry can be found.
739 {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
740 cmVS7FlagTable::UserValueIgnored
| cmVS7FlagTable::Continue
},
741 {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
742 cmVS7FlagTable::UserValueRequired
},
743 {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
744 cmVS7FlagTable::UserValueIgnored
| cmVS7FlagTable::Continue
},
745 {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
746 cmVS7FlagTable::UserValueRequired
},
747 {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "TRUE", 0},
749 // Exception handling mode. If no entries match, it will be FALSE.
750 {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE", 0},
751 {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE", 0},
752 // The EHa option does not have an IDE setting. Let it go to false,
753 // and have EHa passed on the command line by leaving out the table
758 cmVS7FlagTable
const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
760 return cmVS7ExtraFlagTable
;