cmLocalVisualStudioGenerator.cxx 7.3 KB

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