cmLocalVisualStudioGenerator.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 cmLocalVisualStudioGenerator::CheckForErrorLine()
  138. {
  139. return "if errorlevel 1 goto :VCReportError";
  140. }
  141. //----------------------------------------------------------------------------
  142. std::string cmLocalVisualStudioGenerator::GetCheckForErrorLine()
  143. {
  144. return this->CheckForErrorLine();
  145. }
  146. //----------------------------------------------------------------------------
  147. std::string
  148. cmLocalVisualStudioGenerator
  149. ::ConstructScript(const cmCustomCommandLines& commandLines,
  150. const char* workingDirectory,
  151. const char* configName,
  152. bool escapeOldStyle,
  153. bool escapeAllowMakeVars,
  154. const char* newline_text)
  155. {
  156. // Avoid leading or trailing newlines.
  157. const char* newline = "";
  158. // Store the script in a string.
  159. std::string script;
  160. if(workingDirectory)
  161. {
  162. // Change the working directory.
  163. script += newline;
  164. newline = newline_text;
  165. script += "cd ";
  166. script += this->Convert(workingDirectory, FULL, SHELL);
  167. // Change the working drive.
  168. if(workingDirectory[0] && workingDirectory[1] == ':')
  169. {
  170. script += newline;
  171. newline = newline_text;
  172. script += workingDirectory[0];
  173. script += workingDirectory[1];
  174. }
  175. }
  176. // for visual studio IDE add extra stuff to the PATH
  177. // if CMAKE_MSVCIDE_RUN_PATH is set.
  178. if(this->Makefile->GetDefinition("MSVC_IDE"))
  179. {
  180. const char* extraPath =
  181. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  182. if(extraPath)
  183. {
  184. script += newline;
  185. newline = newline_text;
  186. script += "set PATH=";
  187. script += extraPath;
  188. script += ";%PATH%";
  189. }
  190. }
  191. // Write each command on a single line.
  192. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  193. cl != commandLines.end(); ++cl)
  194. {
  195. // Start a new line.
  196. script += newline;
  197. newline = newline_text;
  198. // Start with the command name.
  199. const cmCustomCommandLine& commandLine = *cl;
  200. std::string commandName = this->GetRealLocation(commandLine[0].c_str(),
  201. configName);
  202. if(!workingDirectory)
  203. {
  204. script += this->Convert(commandName.c_str(),START_OUTPUT,SHELL);
  205. }
  206. else
  207. {
  208. script += this->Convert(commandName.c_str(),NONE,SHELL);
  209. }
  210. // Add the arguments.
  211. for(unsigned int j=1;j < commandLine.size(); ++j)
  212. {
  213. script += " ";
  214. if(escapeOldStyle)
  215. {
  216. script += this->EscapeForShellOldStyle(commandLine[j].c_str());
  217. }
  218. else
  219. {
  220. script += this->EscapeForShell(commandLine[j].c_str(),
  221. escapeAllowMakeVars);
  222. }
  223. }
  224. // After each custom command, check for an error result.
  225. // If there was an error, jump to the VCReportError label,
  226. // skipping the run of any subsequent commands in this
  227. // sequence.
  228. //
  229. script += newline_text;
  230. script += this->GetCheckForErrorLine();
  231. }
  232. return script;
  233. }