cmLocalVisualStudioGenerator.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "cmCustomCommandGenerator.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmSourceFile.h"
  16. #include "cmSystemTools.h"
  17. #include "windows.h"
  18. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator(
  19. cmGlobalGenerator* gg, cmMakefile* mf)
  20. : cmLocalGenerator(gg, mf)
  21. {
  22. }
  23. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  24. {
  25. }
  26. cmGlobalVisualStudioGenerator::VSVersion
  27. cmLocalVisualStudioGenerator::GetVersion() const
  28. {
  29. cmGlobalVisualStudioGenerator* gg =
  30. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  31. return gg->GetVersion();
  32. }
  33. void cmLocalVisualStudioGenerator::ComputeObjectFilenames(
  34. std::map<cmSourceFile const*, std::string>& mapping,
  35. cmGeneratorTarget const* gt)
  36. {
  37. std::string dir_max = this->ComputeLongestObjectDirectory(gt);
  38. // Count the number of object files with each name. Note that
  39. // windows file names are not case sensitive.
  40. std::map<std::string, int> counts;
  41. for (std::map<cmSourceFile const*, std::string>::iterator si =
  42. mapping.begin();
  43. si != mapping.end(); ++si) {
  44. cmSourceFile const* sf = si->first;
  45. std::string objectNameLower = cmSystemTools::LowerCase(
  46. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  47. objectNameLower += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  48. counts[objectNameLower] += 1;
  49. }
  50. // For all source files producing duplicate names we need unique
  51. // object name computation.
  52. for (std::map<cmSourceFile const*, std::string>::iterator si =
  53. mapping.begin();
  54. si != mapping.end(); ++si) {
  55. cmSourceFile const* sf = si->first;
  56. std::string objectName =
  57. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  58. objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  59. if (counts[cmSystemTools::LowerCase(objectName)] > 1) {
  60. const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
  61. objectName = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
  62. }
  63. si->second = objectName;
  64. }
  65. }
  66. CM_AUTO_PTR<cmCustomCommand>
  67. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
  68. const std::string& config,
  69. bool isFortran)
  70. {
  71. CM_AUTO_PTR<cmCustomCommand> pcc;
  72. // If an executable exports symbols then VS wants to create an
  73. // import library but forgets to create the output directory.
  74. // The Intel Fortran plugin always forgets to the directory.
  75. if (target->GetType() != cmState::EXECUTABLE &&
  76. !(isFortran && target->GetType() == cmState::SHARED_LIBRARY)) {
  77. return pcc;
  78. }
  79. std::string outDir = target->GetDirectory(config, false);
  80. std::string impDir = target->GetDirectory(config, true);
  81. if (impDir == outDir) {
  82. return pcc;
  83. }
  84. // Add a pre-build event to create the directory.
  85. cmCustomCommandLine command;
  86. command.push_back(cmSystemTools::GetCMakeCommand());
  87. command.push_back("-E");
  88. command.push_back("make_directory");
  89. command.push_back(impDir);
  90. std::vector<std::string> no_output;
  91. std::vector<std::string> no_byproducts;
  92. std::vector<std::string> no_depends;
  93. cmCustomCommandLines commands;
  94. commands.push_back(command);
  95. pcc.reset(new cmCustomCommand(0, no_output, no_byproducts, no_depends,
  96. commands, 0, 0));
  97. pcc->SetEscapeOldStyle(false);
  98. pcc->SetEscapeAllowMakeVars(true);
  99. return pcc;
  100. }
  101. const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  102. {
  103. return ":VCReportError";
  104. }
  105. const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  106. {
  107. return this->ReportErrorLabel();
  108. }
  109. std::string cmLocalVisualStudioGenerator::ConstructScript(
  110. cmCustomCommandGenerator const& ccg, const std::string& newline_text)
  111. {
  112. bool useLocal = this->CustomCommandUseLocal();
  113. std::string workingDirectory = ccg.GetWorkingDirectory();
  114. // Avoid leading or trailing newlines.
  115. std::string newline = "";
  116. // Line to check for error between commands.
  117. std::string check_error = newline_text;
  118. if (useLocal) {
  119. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  120. } else {
  121. check_error += "if errorlevel 1 goto ";
  122. check_error += this->GetReportErrorLabel();
  123. }
  124. // Store the script in a string.
  125. std::string script;
  126. // Open a local context.
  127. if (useLocal) {
  128. script += newline;
  129. newline = newline_text;
  130. script += "setlocal";
  131. }
  132. if (!workingDirectory.empty()) {
  133. // Change the working directory.
  134. script += newline;
  135. newline = newline_text;
  136. script += "cd ";
  137. script += this->ConvertToOutputFormat(
  138. cmSystemTools::CollapseFullPath(workingDirectory), SHELL);
  139. script += check_error;
  140. // Change the working drive.
  141. if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
  142. script += newline;
  143. newline = newline_text;
  144. script += workingDirectory[0];
  145. script += workingDirectory[1];
  146. script += check_error;
  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. const char* extraPath =
  153. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  154. if (extraPath) {
  155. script += newline;
  156. newline = newline_text;
  157. script += "set PATH=";
  158. script += extraPath;
  159. script += ";%PATH%";
  160. }
  161. }
  162. // Write each command on a single line.
  163. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  164. // Start a new line.
  165. script += newline;
  166. newline = newline_text;
  167. // Add this command line.
  168. std::string cmd = ccg.GetCommand(c);
  169. // Use "call " before any invocations of .bat or .cmd files
  170. // invoked as custom commands.
  171. //
  172. std::string suffix;
  173. if (cmd.size() > 4) {
  174. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  175. if (suffix == ".bat" || suffix == ".cmd") {
  176. script += "call ";
  177. }
  178. }
  179. if (workingDirectory.empty()) {
  180. script += this->ConvertToOutputFormat(
  181. this->ConvertToRelativePath(this->GetCurrentBinaryDirectory(), cmd),
  182. cmOutputConverter::SHELL);
  183. } else {
  184. script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
  185. }
  186. ccg.AppendArguments(c, script);
  187. // After each custom command, check for an error result.
  188. // If there was an error, jump to the VCReportError label,
  189. // skipping the run of any subsequent commands in this
  190. // sequence.
  191. script += check_error;
  192. }
  193. // Close the local context.
  194. if (useLocal) {
  195. script += newline;
  196. script += ":cmEnd";
  197. script += newline;
  198. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  199. script += newline;
  200. script += ":cmErrorLevel";
  201. script += newline;
  202. script += "exit /b %1";
  203. script += newline;
  204. script += ":cmDone";
  205. script += newline;
  206. script += "if %errorlevel% neq 0 goto ";
  207. script += this->GetReportErrorLabel();
  208. }
  209. return script;
  210. }