cmLocalVisualStudioGenerator.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "windows.h"
  19. //----------------------------------------------------------------------------
  20. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator()
  21. {
  22. this->WindowsShell = true;
  23. this->WindowsVSIDE = true;
  24. }
  25. //----------------------------------------------------------------------------
  26. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
  31. {
  32. // Identify the language of the source file.
  33. if(const char* lang = this->GetSourceFileLanguage(*sf))
  34. {
  35. // Check whether this source will actually be compiled.
  36. return (!sf->GetCustomCommand() &&
  37. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  38. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
  39. }
  40. else
  41. {
  42. // Unknown source file language. Assume it will not be compiled.
  43. return false;
  44. }
  45. }
  46. //----------------------------------------------------------------------------
  47. void cmLocalVisualStudioGenerator::CountObjectNames(
  48. const std::vector<cmSourceGroup>& groups,
  49. std::map<cmStdString, int>& counts)
  50. {
  51. for(unsigned int i = 0; i < groups.size(); ++i)
  52. {
  53. cmSourceGroup sg = groups[i];
  54. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  55. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  56. s != srcs.end(); ++s)
  57. {
  58. const cmSourceFile* sf = *s;
  59. if(this->SourceFileCompiles(sf))
  60. {
  61. std::string objectName = cmSystemTools::LowerCase(
  62. cmSystemTools::GetFilenameWithoutLastExtension(
  63. sf->GetFullPath()));
  64. objectName += ".obj";
  65. counts[objectName] += 1;
  66. }
  67. }
  68. this->CountObjectNames(sg.GetGroupChildren(), counts);
  69. }
  70. }
  71. //----------------------------------------------------------------------------
  72. void cmLocalVisualStudioGenerator::InsertNeedObjectNames(
  73. const std::vector<cmSourceGroup>& groups,
  74. std::map<cmStdString, int>& count)
  75. {
  76. for(unsigned int i = 0; i < groups.size(); ++i)
  77. {
  78. cmSourceGroup sg = groups[i];
  79. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  80. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  81. s != srcs.end(); ++s)
  82. {
  83. const cmSourceFile* sf = *s;
  84. if(this->SourceFileCompiles(sf))
  85. {
  86. std::string objectName = cmSystemTools::LowerCase(
  87. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  88. objectName += ".obj";
  89. if(count[objectName] > 1)
  90. {
  91. this->NeedObjectName.insert(sf);
  92. }
  93. }
  94. }
  95. this->InsertNeedObjectNames(sg.GetGroupChildren(), count);
  96. }
  97. }
  98. //----------------------------------------------------------------------------
  99. void cmLocalVisualStudioGenerator::ComputeObjectNameRequirements
  100. (std::vector<cmSourceGroup> const& sourceGroups)
  101. {
  102. // Clear the current set of requirements.
  103. this->NeedObjectName.clear();
  104. // Count the number of object files with each name. Note that
  105. // windows file names are not case sensitive.
  106. std::map<cmStdString, int> objectNameCounts;
  107. this->CountObjectNames(sourceGroups, objectNameCounts);
  108. // For all source files producing duplicate names we need unique
  109. // object name computation.
  110. this->InsertNeedObjectNames(sourceGroups, objectNameCounts);
  111. }
  112. //----------------------------------------------------------------------------
  113. std::string
  114. cmLocalVisualStudioGenerator
  115. ::ConstructScript(const cmCustomCommandLines& commandLines,
  116. const char* workingDirectory,
  117. const char* configName,
  118. bool escapeOldStyle,
  119. bool escapeAllowMakeVars,
  120. const char* newline_text)
  121. {
  122. // Avoid leading or trailing newlines.
  123. const char* newline = "";
  124. // Store the script in a string.
  125. std::string script;
  126. if(workingDirectory)
  127. {
  128. // Change the working directory.
  129. script += newline;
  130. newline = newline_text;
  131. script += "cd ";
  132. OSVERSIONINFO osv;
  133. osv.dwOSVersionInfoSize = sizeof(osv);
  134. GetVersionEx(&osv);
  135. if(osv.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  136. {
  137. script += "/d ";
  138. }
  139. script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
  140. // Change the working drive.
  141. if(workingDirectory[0] && workingDirectory[1] == ':')
  142. {
  143. script += newline;
  144. newline = newline_text;
  145. script += workingDirectory[0];
  146. script += workingDirectory[1];
  147. }
  148. }
  149. // for visual studio IDE add extra stuff to the PATH
  150. // if CMAKE_MSVCIDE_RUN_PATH is set.
  151. if(this->Makefile->GetDefinition("MSVC_IDE"))
  152. {
  153. const char* extraPath =
  154. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  155. if(extraPath)
  156. {
  157. script += newline;
  158. newline = newline_text;
  159. script += "set PATH=";
  160. script += extraPath;
  161. script += ";%PATH%";
  162. }
  163. }
  164. // Write each command on a single line.
  165. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  166. cl != commandLines.end(); ++cl)
  167. {
  168. // Start a new line.
  169. script += newline;
  170. newline = newline_text;
  171. // Start with the command name.
  172. const cmCustomCommandLine& commandLine = *cl;
  173. std::string commandName = this->GetRealLocation(commandLine[0].c_str(),
  174. configName);
  175. if(!workingDirectory)
  176. {
  177. script += this->Convert(commandName.c_str(),START_OUTPUT,SHELL);
  178. }
  179. else
  180. {
  181. script += this->Convert(commandName.c_str(),NONE,SHELL);
  182. }
  183. // Add the arguments.
  184. for(unsigned int j=1;j < commandLine.size(); ++j)
  185. {
  186. script += " ";
  187. if(escapeOldStyle)
  188. {
  189. script += this->EscapeForShellOldStyle(commandLine[j].c_str());
  190. }
  191. else
  192. {
  193. script += this->EscapeForShell(commandLine[j].c_str(),
  194. escapeAllowMakeVars);
  195. }
  196. }
  197. }
  198. return script;
  199. }