You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.3 KiB

  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. script += newline;
  128. newline = newline_text;
  129. script += "cd ";
  130. script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
  131. }
  132. // for visual studio IDE add extra stuff to the PATH
  133. // if CMAKE_MSVCIDE_RUN_PATH is set.
  134. if(this->Makefile->GetDefinition("MSVC_IDE"))
  135. {
  136. const char* extraPath =
  137. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  138. if(extraPath)
  139. {
  140. script += newline;
  141. newline = newline_text;
  142. script += "set PATH=";
  143. script += extraPath;
  144. script += ";%PATH%";
  145. }
  146. }
  147. // Write each command on a single line.
  148. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  149. cl != commandLines.end(); ++cl)
  150. {
  151. // Start a new line.
  152. script += newline;
  153. newline = newline_text;
  154. // Start with the command name.
  155. const cmCustomCommandLine& commandLine = *cl;
  156. std::string commandName = this->GetRealLocation(commandLine[0].c_str(),
  157. configName);
  158. if(!workingDirectory)
  159. {
  160. script += this->Convert(commandName.c_str(),START_OUTPUT,SHELL);
  161. }
  162. else
  163. {
  164. script += this->Convert(commandName.c_str(),NONE,SHELL);
  165. }
  166. // Add the arguments.
  167. for(unsigned int j=1;j < commandLine.size(); ++j)
  168. {
  169. script += " ";
  170. if(escapeOldStyle)
  171. {
  172. script += this->EscapeForShellOldStyle(commandLine[j].c_str());
  173. }
  174. else
  175. {
  176. script += this->EscapeForShell(commandLine[j].c_str(),
  177. escapeAllowMakeVars);
  178. }
  179. }
  180. }
  181. return script;
  182. }