cmLocalVisualStudioGenerator.cxx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "cmCustomCommandGenerator.h"
  16. #include "windows.h"
  17. //----------------------------------------------------------------------------
  18. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator(VSVersion v)
  19. {
  20. this->WindowsShell = true;
  21. this->WindowsVSIDE = true;
  22. this->Version = v;
  23. }
  24. //----------------------------------------------------------------------------
  25. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. cmsys::auto_ptr<cmCustomCommand>
  30. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmTarget& target,
  31. const char* config,
  32. bool isFortran)
  33. {
  34. cmsys::auto_ptr<cmCustomCommand> pcc;
  35. // If an executable exports symbols then VS wants to create an
  36. // import library but forgets to create the output directory.
  37. // The Intel Fortran plugin always forgets to the directory.
  38. if(target.GetType() != cmTarget::EXECUTABLE &&
  39. !(isFortran && target.GetType() == cmTarget::SHARED_LIBRARY))
  40. { return pcc; }
  41. std::string outDir = target.GetDirectory(config, false);
  42. std::string impDir = target.GetDirectory(config, true);
  43. if(impDir == outDir) { return pcc; }
  44. // Add a pre-build event to create the directory.
  45. cmCustomCommandLine command;
  46. command.push_back(this->Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  47. command.push_back("-E");
  48. command.push_back("make_directory");
  49. command.push_back(impDir);
  50. std::vector<std::string> no_output;
  51. std::vector<std::string> no_depends;
  52. cmCustomCommandLines commands;
  53. commands.push_back(command);
  54. pcc.reset(new cmCustomCommand(0, no_output, no_depends, commands, 0, 0));
  55. pcc->SetEscapeOldStyle(false);
  56. pcc->SetEscapeAllowMakeVars(true);
  57. return pcc;
  58. }
  59. //----------------------------------------------------------------------------
  60. bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
  61. {
  62. // Identify the language of the source file.
  63. if(const char* lang = this->GetSourceFileLanguage(*sf))
  64. {
  65. // Check whether this source will actually be compiled.
  66. return (!sf->GetCustomCommand() &&
  67. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  68. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
  69. }
  70. else
  71. {
  72. // Unknown source file language. Assume it will not be compiled.
  73. return false;
  74. }
  75. }
  76. //----------------------------------------------------------------------------
  77. void cmLocalVisualStudioGenerator::CountObjectNames(
  78. const std::vector<cmSourceGroup>& groups,
  79. std::map<cmStdString, int>& counts)
  80. {
  81. for(unsigned int i = 0; i < groups.size(); ++i)
  82. {
  83. cmSourceGroup sg = groups[i];
  84. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  85. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  86. s != srcs.end(); ++s)
  87. {
  88. const cmSourceFile* sf = *s;
  89. if(this->SourceFileCompiles(sf))
  90. {
  91. std::string objectName = cmSystemTools::LowerCase(
  92. cmSystemTools::GetFilenameWithoutLastExtension(
  93. sf->GetFullPath()));
  94. objectName += ".obj";
  95. counts[objectName] += 1;
  96. }
  97. }
  98. this->CountObjectNames(sg.GetGroupChildren(), counts);
  99. }
  100. }
  101. //----------------------------------------------------------------------------
  102. void cmLocalVisualStudioGenerator::InsertNeedObjectNames(
  103. const std::vector<cmSourceGroup>& groups,
  104. std::map<cmStdString, int>& count)
  105. {
  106. for(unsigned int i = 0; i < groups.size(); ++i)
  107. {
  108. cmSourceGroup sg = groups[i];
  109. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  110. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  111. s != srcs.end(); ++s)
  112. {
  113. const cmSourceFile* sf = *s;
  114. if(this->SourceFileCompiles(sf))
  115. {
  116. std::string objectName = cmSystemTools::LowerCase(
  117. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  118. objectName += ".obj";
  119. if(count[objectName] > 1)
  120. {
  121. this->NeedObjectName.insert(sf);
  122. }
  123. }
  124. }
  125. this->InsertNeedObjectNames(sg.GetGroupChildren(), count);
  126. }
  127. }
  128. //----------------------------------------------------------------------------
  129. void cmLocalVisualStudioGenerator::ComputeObjectNameRequirements
  130. (std::vector<cmSourceGroup> const& sourceGroups)
  131. {
  132. // Clear the current set of requirements.
  133. this->NeedObjectName.clear();
  134. // Count the number of object files with each name. Note that
  135. // windows file names are not case sensitive.
  136. std::map<cmStdString, int> objectNameCounts;
  137. this->CountObjectNames(sourceGroups, objectNameCounts);
  138. // For all source files producing duplicate names we need unique
  139. // object name computation.
  140. this->InsertNeedObjectNames(sourceGroups, objectNameCounts);
  141. }
  142. //----------------------------------------------------------------------------
  143. const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  144. {
  145. return ":VCReportError";
  146. }
  147. //----------------------------------------------------------------------------
  148. const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  149. {
  150. return this->ReportErrorLabel();
  151. }
  152. //----------------------------------------------------------------------------
  153. std::string
  154. cmLocalVisualStudioGenerator
  155. ::ConstructScript(cmCustomCommand const& cc,
  156. const char* configName,
  157. const char* newline_text)
  158. {
  159. bool useLocal = this->CustomCommandUseLocal();
  160. const cmCustomCommandLines& commandLines = cc.GetCommandLines();
  161. const char* workingDirectory = cc.GetWorkingDirectory();
  162. cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
  163. RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
  164. // Avoid leading or trailing newlines.
  165. const char* newline = "";
  166. // Line to check for error between commands.
  167. std::string check_error = newline_text;
  168. if(useLocal)
  169. {
  170. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  171. }
  172. else
  173. {
  174. check_error += "if errorlevel 1 goto ";
  175. check_error += this->GetReportErrorLabel();
  176. }
  177. // Store the script in a string.
  178. std::string script;
  179. // Open a local context.
  180. if(useLocal)
  181. {
  182. script += newline;
  183. newline = newline_text;
  184. script += "setlocal";
  185. }
  186. if(workingDirectory)
  187. {
  188. // Change the working directory.
  189. script += newline;
  190. newline = newline_text;
  191. script += "cd ";
  192. script += this->Convert(workingDirectory, FULL, SHELL);
  193. script += check_error;
  194. // Change the working drive.
  195. if(workingDirectory[0] && workingDirectory[1] == ':')
  196. {
  197. script += newline;
  198. newline = newline_text;
  199. script += workingDirectory[0];
  200. script += workingDirectory[1];
  201. script += check_error;
  202. }
  203. }
  204. // for visual studio IDE add extra stuff to the PATH
  205. // if CMAKE_MSVCIDE_RUN_PATH is set.
  206. if(this->Makefile->GetDefinition("MSVC_IDE"))
  207. {
  208. const char* extraPath =
  209. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  210. if(extraPath)
  211. {
  212. script += newline;
  213. newline = newline_text;
  214. script += "set PATH=";
  215. script += extraPath;
  216. script += ";%PATH%";
  217. }
  218. }
  219. // Write each command on a single line.
  220. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  221. {
  222. // Start a new line.
  223. script += newline;
  224. newline = newline_text;
  225. // Add this command line.
  226. std::string cmd = ccg.GetCommand(c);
  227. // Use "call " before any invocations of .bat or .cmd files
  228. // invoked as custom commands.
  229. //
  230. std::string suffix;
  231. if (cmd.size() > 4)
  232. {
  233. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size()-4));
  234. if (suffix == ".bat" || suffix == ".cmd")
  235. {
  236. script += "call ";
  237. }
  238. }
  239. script += this->Convert(cmd.c_str(), relativeRoot, SHELL);
  240. ccg.AppendArguments(c, script);
  241. // After each custom command, check for an error result.
  242. // If there was an error, jump to the VCReportError label,
  243. // skipping the run of any subsequent commands in this
  244. // sequence.
  245. script += check_error;
  246. }
  247. // Close the local context.
  248. if(useLocal)
  249. {
  250. script += newline;
  251. script += ":cmEnd";
  252. script += newline;
  253. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  254. script += newline;
  255. script += ":cmErrorLevel";
  256. script += newline;
  257. script += "exit /b %1";
  258. script += newline;
  259. script += ":cmDone";
  260. script += newline;
  261. script += "if %errorlevel% neq 0 goto ";
  262. script += this->GetReportErrorLabel();
  263. }
  264. return script;
  265. }