Fix Xcode project references to the source tree
[cmake.git] / Source / cmGlobalVisualStudio7Generator.cxx
blob8e2e1d008e85075479070099971fe2eb3d4bb67f
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGlobalVisualStudio7Generator.cxx,v $
5 Language: C++
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"
22 #include "cmake.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");
54 if(extraPath)
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
76 (void) ignoreErrors;
78 // now build the test
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);
92 #endif
93 makeCommand += " ";
94 makeCommand += projectName;
95 makeCommand += ".sln ";
96 bool clean = false;
97 if ( targetName && strcmp(targetName, "clean") == 0 )
99 clean = true;
100 targetName = "ALL_BUILD";
102 if(clean)
104 makeCommand += "/clean ";
106 else
108 makeCommand += "/build ";
111 if(config && strlen(config))
113 makeCommand += config;
115 else
117 makeCommand += "Debug";
119 makeCommand += " /project ";
121 if (targetName && strlen(targetName))
123 makeCommand += targetName;
125 else
127 makeCommand += "ALL_BUILD";
129 if ( additionalOptions )
131 makeCommand += " ";
132 makeCommand += additionalOptions;
134 return makeCommand;
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);
143 return lg;
146 void cmGlobalVisualStudio7Generator::GenerateConfigurations(cmMakefile* mf)
148 // process the configurations
149 const char* ct
150 = this->CMakeInstance->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
151 if ( ct )
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)
177 configs += ";";
178 configs += this->Configurations[i];
181 mf->AddCacheDefinition(
182 "CMAKE_CONFIGURATION_TYPES",
183 configs.c_str(),
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)
211 return;
213 this->CurrentProject = root->GetMakefile()->GetProjectName();
214 std::string fname = root->GetMakefile()->GetStartOutputDirectory();
215 fname += "/";
216 fname += root->GetMakefile()->GetProjectName();
217 fname += ".sln";
218 cmGeneratedFileStream fout(fname.c_str());
219 fout.SetCopyIfDifferent(true);
220 if(!fout)
222 return;
224 this->WriteSLNFile(fout, root, generators);
225 if (fout.Close())
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(
243 std::ostream& fout,
244 cmLocalGenerator* root,
245 OrderedTargetDependSet const& projectTargets)
247 // loop over again and write out configurations for each target
248 // in the solution
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");
254 if(expath)
256 this->WriteProjectConfigurations(fout, target->GetName(),
257 true);
259 else
261 bool partOfDefaultBuild = this->IsPartOfDefaultBuild(
262 root->GetMakefile()->GetProjectName(), target);
263 const char *vcprojName =
264 target->GetProperty("GENERATOR_FILE_NAME");
265 if (vcprojName)
267 this->WriteProjectConfigurations(fout, vcprojName,
268 partOfDefaultBuild);
275 void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
276 std::ostream& fout,
277 cmLocalGenerator* root,
278 OrderedTargetDependSet const& projectTargets)
280 std::string rootdir = root->GetMakefile()->GetStartOutputDirectory();
281 rootdir += "/";
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");
288 if(expath)
290 std::string project = target->GetName();
291 std::string location = expath;
292 this->WriteExternalProject(fout, project.c_str(),
293 location.c_str(), target->GetUtilities());
295 else
297 bool skip = false;
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())
307 skip = true;
310 // if not skipping the project then write it into the
311 // solution
312 if(!skip)
314 const char *vcprojName =
315 target->GetProperty("GENERATOR_FILE_NAME");
316 if(vcprojName)
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(),
323 *target);
331 void cmGlobalVisualStudio7Generator::WriteTargetDepends(
332 std::ostream& fout,
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");
343 if (vcprojName)
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
364 // in this project.
365 cmGlobalGenerator::TargetDependSet projectTargets;
366 cmGlobalGenerator::TargetDependSet originalTargets;
367 this->GetTargetSets(projectTargets,
368 originalTargets,
369 root, generators);
370 OrderedTargetDependSet orderedProjectTargets(projectTargets);
371 this->WriteTargetsToSolution(fout, root, orderedProjectTargets);
372 // Write out the configurations information for the solution
373 fout << "Global\n"
374 << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
376 int c = 0;
377 for(std::vector<std::string>::iterator i = this->Configurations.begin();
378 i != this->Configurations.end(); ++i)
380 fout << "\t\tConfigName." << c << " = " << *i << "\n";
381 c++;
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 //----------------------------------------------------------------------------
399 std::string
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)
409 d[pos++] = '\\';
411 return d;
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,
418 const char* dspname,
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))
427 ext = ".vfproj";
428 project = "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
431 fout << project
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
443 void
444 cmGlobalVisualStudio7Generator
445 ::WriteProjectDepends(std::ostream& fout,
446 const char* dspname,
447 const char*, cmTarget& target)
449 int depcount = 0;
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();
456 for(;j!= jend; ++j)
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());
464 if(guid.size() == 0)
466 std::string m = "Target: ";
467 m += dspname;
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";
474 depcount++;
480 std::set<cmStdString>::const_iterator i, end;
481 // write utility dependencies.
482 i = target.GetUtilities().begin();
483 end = target.GetUtilities().end();
484 for(;i!= end; ++i)
486 if(*i != dspname)
488 std::string name = this->GetUtilityForTarget(target, i->c_str());
489 std::string guid = this->GetGUID(name.c_str());
490 if(guid.size() == 0)
492 std::string m = "Target: ";
493 m += dspname;
494 m += " depends on unknown target: ";
495 m += name.c_str();
496 cmSystemTools::Error(m.c_str());
499 fout << "\t\t{" << this->GetGUID(dspname) << "}." << depcount << " = {"
500 << guid << "}\n";
501 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,
533 const char* name,
534 const char* location,
535 const std::set<cmStdString>&)
537 std::string d = cmSystemTools::ConvertToOutputPath(location);
538 fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
539 << name << "\", \""
540 << this->ConvertToSolutionPath(location) << "\", \"{"
541 << this->GetGUID(name)
542 << "}\"\n";
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"
555 << "EndGlobal\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());
571 if(storedGUID)
573 return std::string(storedGUID);
575 cmSystemTools::Error("Unknown Target referenced : ",
576 name);
577 return "";
581 void cmGlobalVisualStudio7Generator::CreateGUID(const char* name)
583 std::string guidStoreName = name;
584 guidStoreName += "_GUID_CMAKE";
585 if(this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str()))
587 return;
589 std::string ret;
590 UUID uid;
591 unsigned char *uidstr;
592 UuidCreate(&uid);
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.";
613 entry.Full = "";
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 //----------------------------------------------------------------------------
629 void
630 cmGlobalVisualStudio7Generator
631 ::AppendDirectoryForConfig(const char* prefix,
632 const char* config,
633 const char* suffix,
634 std::string& dir)
636 if(config)
638 dir += prefix;
639 dir += config;
640 dir += suffix;
644 bool cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(const char* project,
645 cmTarget* target)
647 if(target->GetPropertyAsBool("EXCLUDE_FROM_DEFAULT_BUILD"))
649 return false;
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)
656 return false;
658 if(type == cmTarget::UTILITY)
660 return this->IsDependedOn(project, target);
662 // default is to be part of the build
663 return true;
666 //----------------------------------------------------------------------------
667 bool
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)
674 return false;
676 if(strcmp(l->GetName(), "ALL_BUILD") == 0)
678 return true;
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)
690 this->insert(*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
715 // entry.
717 {0,0,0,0,0}
719 cmIDEFlagTable const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
721 return cmVS7ExtraFlagTable;