cmLocalVisualStudioGenerator.cxx 7.1 KB

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