cmLocalVisualStudioGenerator.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <utility>
  5. #include <cm/memory>
  6. #include <cmext/string_view>
  7. #include "windows.h"
  8. #include "cmCustomCommand.h"
  9. #include "cmCustomCommandGenerator.h"
  10. #include "cmCustomCommandLines.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmOutputConverter.h"
  15. #include "cmSourceFile.h"
  16. #include "cmStateTypes.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. #include "cmValue.h"
  20. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator(
  21. cmGlobalGenerator* gg, cmMakefile* mf)
  22. : cmLocalGenerator(gg, mf)
  23. {
  24. }
  25. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator() = default;
  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. char const* custom_ext = gt->GetCustomObjectExtension();
  38. std::string dir_max = this->ComputeLongestObjectDirectory(gt);
  39. // Count the number of object files with each name. Note that
  40. // windows file names are not case sensitive.
  41. std::map<std::string, int> counts;
  42. for (auto const& si : mapping) {
  43. cmSourceFile const* sf = si.first;
  44. std::string objectNameLower = cmSystemTools::LowerCase(
  45. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  46. if (custom_ext) {
  47. objectNameLower += custom_ext;
  48. } else {
  49. objectNameLower +=
  50. this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  51. }
  52. counts[objectNameLower] += 1;
  53. }
  54. // For all source files producing duplicate names we need unique
  55. // object name computation.
  56. for (auto& si : mapping) {
  57. cmSourceFile const* sf = si.first;
  58. std::string objectName =
  59. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  60. if (custom_ext) {
  61. objectName += custom_ext;
  62. } else {
  63. objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  64. }
  65. if (counts[cmSystemTools::LowerCase(objectName)] > 1) {
  66. const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
  67. bool keptSourceExtension;
  68. objectName = this->GetObjectFileNameWithoutTarget(
  69. *sf, dir_max, &keptSourceExtension, custom_ext);
  70. }
  71. si.second = objectName;
  72. }
  73. }
  74. std::unique_ptr<cmCustomCommand>
  75. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
  76. const std::string& config,
  77. bool isFortran)
  78. {
  79. std::unique_ptr<cmCustomCommand> pcc;
  80. // If an executable exports symbols then VS wants to create an
  81. // import library but forgets to create the output directory.
  82. // The Intel Fortran plugin always forgets to the directory.
  83. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  84. !(isFortran && target->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  85. return pcc;
  86. }
  87. std::string outDir =
  88. target->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  89. std::string impDir =
  90. target->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  91. if (impDir == outDir) {
  92. return pcc;
  93. }
  94. // Add a pre-build event to create the directory.
  95. cmCustomCommandLines commands = cmMakeSingleCommandLine(
  96. { cmSystemTools::GetCMakeCommand(), "-E", "make_directory", impDir });
  97. pcc = cm::make_unique<cmCustomCommand>();
  98. pcc->SetCommandLines(commands);
  99. pcc->SetStdPipesUTF8(true);
  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;
  121. if (useLocal) {
  122. check_error = cmStrCat(newline_text, "if %errorlevel% neq 0 goto :cmEnd");
  123. } else {
  124. check_error = cmStrCat(newline_text, "if errorlevel 1 goto ",
  125. this->GetReportErrorLabel());
  126. }
  127. // Store the script in a string.
  128. std::string script;
  129. // Open a local context.
  130. if (useLocal) {
  131. script = cmStrCat(newline, "setlocal");
  132. newline = newline_text;
  133. }
  134. if (!workingDirectory.empty()) {
  135. // Change the working directory.
  136. script = cmStrCat(script, newline, "cd ",
  137. this->ConvertToOutputFormat(workingDirectory, SHELL),
  138. check_error);
  139. newline = newline_text;
  140. // Change the working drive.
  141. if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
  142. script = cmStrCat(script, newline, workingDirectory[0],
  143. workingDirectory[1], check_error);
  144. newline = newline_text;
  145. }
  146. }
  147. // for visual studio IDE add extra stuff to the PATH
  148. // if CMAKE_MSVCIDE_RUN_PATH is set.
  149. if (this->GetGlobalGenerator()->IsVisualStudio()) {
  150. cmValue extraPath =
  151. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  152. if (extraPath) {
  153. script = cmStrCat(script, newline, "set PATH=", *extraPath, ";%PATH%");
  154. newline = newline_text;
  155. }
  156. }
  157. // Write each command on a single line.
  158. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  159. // Add this command line.
  160. std::string cmd = ccg.GetCommand(c);
  161. if (cmd.empty()) {
  162. continue;
  163. }
  164. // Start a new line.
  165. script += newline;
  166. newline = newline_text;
  167. // Use "call " before any invocations of .bat or .cmd files
  168. // invoked as custom commands.
  169. //
  170. std::string suffix;
  171. if (cmd.size() > 4) {
  172. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  173. if (suffix == ".bat"_s || suffix == ".cmd"_s) {
  174. script += "call ";
  175. }
  176. }
  177. if (workingDirectory.empty()) {
  178. script += this->ConvertToOutputFormat(
  179. this->MaybeRelativeToCurBinDir(cmd), cmOutputConverter::SHELL);
  180. } else {
  181. script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
  182. }
  183. ccg.AppendArguments(c, script);
  184. // After each custom command, check for an error result.
  185. // If there was an error, jump to the VCReportError label,
  186. // skipping the run of any subsequent commands in this
  187. // sequence.
  188. script += check_error;
  189. }
  190. // Close the local context.
  191. if (useLocal) {
  192. // clang-format off
  193. script = cmStrCat(
  194. script
  195. , newline
  196. , ":cmEnd"
  197. , newline
  198. , "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone"
  199. , newline
  200. , ":cmErrorLevel"
  201. , newline
  202. , "exit /b %1"
  203. , newline
  204. , ":cmDone"
  205. , newline
  206. , "if %errorlevel% neq 0 goto ", this->GetReportErrorLabel()
  207. );
  208. // clang-format on
  209. }
  210. return script;
  211. }
  212. std::string cmLocalVisualStudioGenerator::FinishConstructScript(
  213. VsProjectType projectType, const std::string& newline)
  214. {
  215. bool useLocal = this->CustomCommandUseLocal();
  216. // Store the script in a string.
  217. std::string script;
  218. if (useLocal && projectType != VsProjectType::vcxproj) {
  219. // This label is not provided by MSBuild for C# projects.
  220. script += newline;
  221. script += this->GetReportErrorLabel();
  222. }
  223. return script;
  224. }