cmLocalVisualStudioGenerator.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmLocalVisualStudioGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmSystemTools.h"
  18. //----------------------------------------------------------------------------
  19. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator()
  20. {
  21. this->WindowsShell = true;
  22. this->WindowsVSIDE = true;
  23. }
  24. //----------------------------------------------------------------------------
  25. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
  30. {
  31. // Identify the language of the source file.
  32. if(const char* lang = this->GetSourceFileLanguage(*sf))
  33. {
  34. // Check whether this source will actually be compiled.
  35. return (!sf->GetCustomCommand() &&
  36. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  37. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
  38. }
  39. else
  40. {
  41. // Unknown source file language. Assume it will not be compiled.
  42. return false;
  43. }
  44. }
  45. //----------------------------------------------------------------------------
  46. void cmLocalVisualStudioGenerator::CountObjectNames(
  47. const std::vector<cmSourceGroup>& groups,
  48. std::map<cmStdString, int>& counts)
  49. {
  50. for(unsigned int i = 0; i < groups.size(); ++i)
  51. {
  52. cmSourceGroup sg = groups[i];
  53. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  54. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  55. s != srcs.end(); ++s)
  56. {
  57. const cmSourceFile* sf = *s;
  58. if(this->SourceFileCompiles(sf))
  59. {
  60. std::string objectName = cmSystemTools::LowerCase(
  61. cmSystemTools::GetFilenameWithoutLastExtension(
  62. sf->GetFullPath()));
  63. objectName += ".obj";
  64. counts[objectName] += 1;
  65. }
  66. }
  67. this->CountObjectNames(sg.GetGroupChildren(), counts);
  68. }
  69. }
  70. //----------------------------------------------------------------------------
  71. void cmLocalVisualStudioGenerator::InsertNeedObjectNames(
  72. const std::vector<cmSourceGroup>& groups,
  73. std::map<cmStdString, int>& count)
  74. {
  75. for(unsigned int i = 0; i < groups.size(); ++i)
  76. {
  77. cmSourceGroup sg = groups[i];
  78. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  79. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  80. s != srcs.end(); ++s)
  81. {
  82. const cmSourceFile* sf = *s;
  83. if(this->SourceFileCompiles(sf))
  84. {
  85. std::string objectName = cmSystemTools::LowerCase(
  86. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  87. objectName += ".obj";
  88. if(count[objectName] > 1)
  89. {
  90. this->NeedObjectName.insert(sf);
  91. }
  92. }
  93. }
  94. this->InsertNeedObjectNames(sg.GetGroupChildren(), count);
  95. }
  96. }
  97. //----------------------------------------------------------------------------
  98. void cmLocalVisualStudioGenerator::ComputeObjectNameRequirements
  99. (std::vector<cmSourceGroup> const& sourceGroups)
  100. {
  101. // Clear the current set of requirements.
  102. this->NeedObjectName.clear();
  103. // Count the number of object files with each name. Note that
  104. // windows file names are not case sensitive.
  105. std::map<cmStdString, int> objectNameCounts;
  106. this->CountObjectNames(sourceGroups, objectNameCounts);
  107. // For all source files producing duplicate names we need unique
  108. // object name computation.
  109. this->InsertNeedObjectNames(sourceGroups, objectNameCounts);
  110. }
  111. //----------------------------------------------------------------------------
  112. std::string
  113. cmLocalVisualStudioGenerator
  114. ::ConstructScript(const cmCustomCommandLines& commandLines,
  115. const char* workingDirectory,
  116. const char* configName,
  117. bool escapeOldStyle,
  118. bool escapeAllowMakeVars,
  119. const char* newline_text)
  120. {
  121. // Avoid leading or trailing newlines.
  122. const char* newline = "";
  123. // Store the script in a string.
  124. std::string script;
  125. if(workingDirectory)
  126. {
  127. // Change the working directory.
  128. script += newline;
  129. newline = newline_text;
  130. script += "cd ";
  131. script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
  132. // Change the working drive.
  133. if(workingDirectory[0] && workingDirectory[1] == ':')
  134. {
  135. script += newline;
  136. newline = newline_text;
  137. script += workingDirectory[0];
  138. script += workingDirectory[1];
  139. }
  140. }
  141. // for visual studio IDE add extra stuff to the PATH
  142. // if CMAKE_MSVCIDE_RUN_PATH is set.
  143. if(this->Makefile->GetDefinition("MSVC_IDE"))
  144. {
  145. const char* extraPath =
  146. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  147. if(extraPath)
  148. {
  149. script += newline;
  150. newline = newline_text;
  151. script += "set PATH=";
  152. script += extraPath;
  153. script += ";%PATH%";
  154. }
  155. }
  156. // Write each command on a single line.
  157. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  158. cl != commandLines.end(); ++cl)
  159. {
  160. // Start a new line.
  161. script += newline;
  162. newline = newline_text;
  163. // Start with the command name.
  164. const cmCustomCommandLine& commandLine = *cl;
  165. std::string commandName = this->GetRealLocation(commandLine[0].c_str(),
  166. configName);
  167. if(!workingDirectory)
  168. {
  169. script += this->Convert(commandName.c_str(),START_OUTPUT,SHELL);
  170. }
  171. else
  172. {
  173. script += this->Convert(commandName.c_str(),NONE,SHELL);
  174. }
  175. // Add the arguments.
  176. for(unsigned int j=1;j < commandLine.size(); ++j)
  177. {
  178. script += " ";
  179. if(escapeOldStyle)
  180. {
  181. script += this->EscapeForShellOldStyle(commandLine[j].c_str());
  182. }
  183. else
  184. {
  185. script += this->EscapeForShell(commandLine[j].c_str(),
  186. escapeAllowMakeVars);
  187. }
  188. }
  189. }
  190. return script;
  191. }