cmLocalVisualStudioGenerator.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmLocalVisualStudioGenerator.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmSourceFile.h"
  14. #include "cmSystemTools.h"
  15. #include "windows.h"
  16. //----------------------------------------------------------------------------
  17. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator()
  18. {
  19. this->WindowsShell = true;
  20. this->WindowsVSIDE = true;
  21. }
  22. //----------------------------------------------------------------------------
  23. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. cmsys::auto_ptr<cmCustomCommand>
  28. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmTarget& target,
  29. const char* config)
  30. {
  31. cmsys::auto_ptr<cmCustomCommand> pcc;
  32. // If an executable exports symbols then VS wants to create an
  33. // import library but forgets to create the output directory.
  34. if(target.GetType() != cmTarget::EXECUTABLE) { return pcc; }
  35. std::string outDir = target.GetDirectory(config, false);
  36. std::string impDir = target.GetDirectory(config, true);
  37. if(impDir == outDir) { return pcc; }
  38. // Add a pre-build event to create the directory.
  39. cmCustomCommandLine command;
  40. command.push_back(this->Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  41. command.push_back("-E");
  42. command.push_back("make_directory");
  43. command.push_back(impDir);
  44. std::vector<std::string> no_output;
  45. std::vector<std::string> no_depends;
  46. cmCustomCommandLines commands;
  47. commands.push_back(command);
  48. pcc.reset(new cmCustomCommand(no_output, no_depends, commands, 0, 0));
  49. pcc->SetEscapeOldStyle(false);
  50. pcc->SetEscapeAllowMakeVars(true);
  51. return pcc;
  52. }
  53. //----------------------------------------------------------------------------
  54. bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
  55. {
  56. // Identify the language of the source file.
  57. if(const char* lang = this->GetSourceFileLanguage(*sf))
  58. {
  59. // Check whether this source will actually be compiled.
  60. return (!sf->GetCustomCommand() &&
  61. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  62. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
  63. }
  64. else
  65. {
  66. // Unknown source file language. Assume it will not be compiled.
  67. return false;
  68. }
  69. }
  70. //----------------------------------------------------------------------------
  71. void cmLocalVisualStudioGenerator::CountObjectNames(
  72. const std::vector<cmSourceGroup>& groups,
  73. std::map<cmStdString, int>& counts)
  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(
  87. sf->GetFullPath()));
  88. objectName += ".obj";
  89. counts[objectName] += 1;
  90. }
  91. }
  92. this->CountObjectNames(sg.GetGroupChildren(), counts);
  93. }
  94. }
  95. //----------------------------------------------------------------------------
  96. void cmLocalVisualStudioGenerator::InsertNeedObjectNames(
  97. const std::vector<cmSourceGroup>& groups,
  98. std::map<cmStdString, int>& count)
  99. {
  100. for(unsigned int i = 0; i < groups.size(); ++i)
  101. {
  102. cmSourceGroup sg = groups[i];
  103. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  104. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  105. s != srcs.end(); ++s)
  106. {
  107. const cmSourceFile* sf = *s;
  108. if(this->SourceFileCompiles(sf))
  109. {
  110. std::string objectName = cmSystemTools::LowerCase(
  111. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  112. objectName += ".obj";
  113. if(count[objectName] > 1)
  114. {
  115. this->NeedObjectName.insert(sf);
  116. }
  117. }
  118. }
  119. this->InsertNeedObjectNames(sg.GetGroupChildren(), count);
  120. }
  121. }
  122. //----------------------------------------------------------------------------
  123. void cmLocalVisualStudioGenerator::ComputeObjectNameRequirements
  124. (std::vector<cmSourceGroup> const& sourceGroups)
  125. {
  126. // Clear the current set of requirements.
  127. this->NeedObjectName.clear();
  128. // Count the number of object files with each name. Note that
  129. // windows file names are not case sensitive.
  130. std::map<cmStdString, int> objectNameCounts;
  131. this->CountObjectNames(sourceGroups, objectNameCounts);
  132. // For all source files producing duplicate names we need unique
  133. // object name computation.
  134. this->InsertNeedObjectNames(sourceGroups, objectNameCounts);
  135. }
  136. //----------------------------------------------------------------------------
  137. std::string
  138. cmLocalVisualStudioGenerator
  139. ::ConstructScript(const cmCustomCommandLines& commandLines,
  140. const char* workingDirectory,
  141. const char* configName,
  142. bool escapeOldStyle,
  143. bool escapeAllowMakeVars,
  144. const char* newline_text)
  145. {
  146. // Avoid leading or trailing newlines.
  147. const char* newline = "";
  148. // Store the script in a string.
  149. std::string script;
  150. if(workingDirectory)
  151. {
  152. // Change the working directory.
  153. script += newline;
  154. newline = newline_text;
  155. script += "cd ";
  156. script += this->Convert(workingDirectory, FULL, SHELL);
  157. // Change the working drive.
  158. if(workingDirectory[0] && workingDirectory[1] == ':')
  159. {
  160. script += newline;
  161. newline = newline_text;
  162. script += workingDirectory[0];
  163. script += workingDirectory[1];
  164. }
  165. }
  166. // for visual studio IDE add extra stuff to the PATH
  167. // if CMAKE_MSVCIDE_RUN_PATH is set.
  168. if(this->Makefile->GetDefinition("MSVC_IDE"))
  169. {
  170. const char* extraPath =
  171. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  172. if(extraPath)
  173. {
  174. script += newline;
  175. newline = newline_text;
  176. script += "set PATH=";
  177. script += extraPath;
  178. script += ";%PATH%";
  179. }
  180. }
  181. // Write each command on a single line.
  182. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  183. cl != commandLines.end(); ++cl)
  184. {
  185. // Start a new line.
  186. script += newline;
  187. newline = newline_text;
  188. // Start with the command name.
  189. const cmCustomCommandLine& commandLine = *cl;
  190. std::string commandName = this->GetRealLocation(commandLine[0].c_str(),
  191. configName);
  192. if(!workingDirectory)
  193. {
  194. script += this->Convert(commandName.c_str(),START_OUTPUT,SHELL);
  195. }
  196. else
  197. {
  198. script += this->Convert(commandName.c_str(),NONE,SHELL);
  199. }
  200. // Add the arguments.
  201. for(unsigned int j=1;j < commandLine.size(); ++j)
  202. {
  203. script += " ";
  204. if(escapeOldStyle)
  205. {
  206. script += this->EscapeForShellOldStyle(commandLine[j].c_str());
  207. }
  208. else
  209. {
  210. script += this->EscapeForShell(commandLine[j].c_str(),
  211. escapeAllowMakeVars);
  212. }
  213. }
  214. // After each custom command, check for an error result.
  215. // If there was an error, jump to the VCReportError label,
  216. // skipping the run of any subsequent commands in this
  217. // sequence.
  218. //
  219. script += newline_text;
  220. script += "if errorlevel 1 goto VCReportError";
  221. }
  222. return script;
  223. }