cmLocalVisualStudioGenerator.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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::string cmLocalVisualStudioGenerator::GetObjectOutputRoot() const
  75. {
  76. return this->GetCurrentBinaryDirectory();
  77. }
  78. bool cmLocalVisualStudioGenerator::AlwaysUsesCMFPaths() const
  79. {
  80. return false;
  81. }
  82. std::unique_ptr<cmCustomCommand>
  83. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
  84. std::string const& config,
  85. bool isFortran)
  86. {
  87. std::unique_ptr<cmCustomCommand> pcc;
  88. // If an executable exports symbols then VS wants to create an
  89. // import library but forgets to create the output directory.
  90. // The Intel Fortran plugin always forgets to the directory.
  91. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  92. !(isFortran && target->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  93. return pcc;
  94. }
  95. std::string outDir =
  96. target->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  97. std::string impDir =
  98. target->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  99. if (impDir == outDir) {
  100. return pcc;
  101. }
  102. // Add a pre-build event to create the directory.
  103. cmCustomCommandLines commands = cmMakeSingleCommandLine(
  104. { cmSystemTools::GetCMakeCommand(), "-E", "make_directory", impDir });
  105. pcc = cm::make_unique<cmCustomCommand>();
  106. pcc->SetCommandLines(commands);
  107. pcc->SetStdPipesUTF8(true);
  108. pcc->SetEscapeOldStyle(false);
  109. pcc->SetEscapeAllowMakeVars(true);
  110. return pcc;
  111. }
  112. char const* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  113. {
  114. return ":VCReportError";
  115. }
  116. char const* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  117. {
  118. return this->ReportErrorLabel();
  119. }
  120. std::string cmLocalVisualStudioGenerator::ConstructScript(
  121. cmCustomCommandGenerator const& ccg, std::string const& newline_text)
  122. {
  123. bool useLocal = this->CustomCommandUseLocal();
  124. std::string workingDirectory = ccg.GetWorkingDirectory();
  125. // Avoid leading or trailing newlines.
  126. std::string newline;
  127. // Line to check for error between commands.
  128. std::string check_error;
  129. if (useLocal) {
  130. check_error = cmStrCat(newline_text, "if %errorlevel% neq 0 goto :cmEnd");
  131. } else {
  132. check_error = cmStrCat(newline_text, "if errorlevel 1 goto ",
  133. this->GetReportErrorLabel());
  134. }
  135. // Store the script in a string.
  136. std::string script;
  137. // Open a local context.
  138. if (useLocal) {
  139. script = cmStrCat(newline, "setlocal");
  140. newline = newline_text;
  141. }
  142. if (!workingDirectory.empty()) {
  143. // Change the working directory.
  144. script = cmStrCat(script, newline, "cd ",
  145. this->ConvertToOutputFormat(workingDirectory, SHELL),
  146. check_error);
  147. newline = newline_text;
  148. // Change the working drive.
  149. if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
  150. script = cmStrCat(script, newline, workingDirectory[0],
  151. workingDirectory[1], check_error);
  152. newline = newline_text;
  153. }
  154. }
  155. // for visual studio IDE add extra stuff to the PATH
  156. // if CMAKE_MSVCIDE_RUN_PATH is set.
  157. if (this->GetGlobalGenerator()->IsVisualStudio()) {
  158. cmValue extraPath =
  159. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  160. if (extraPath) {
  161. script = cmStrCat(script, newline, "set PATH=", *extraPath, ";%PATH%");
  162. newline = newline_text;
  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"_s || suffix == ".cmd"_s) {
  182. script += "call ";
  183. }
  184. }
  185. if (workingDirectory.empty()) {
  186. script += this->ConvertToOutputFormat(
  187. this->MaybeRelativeToCurBinDir(cmd), cmOutputConverter::SHELL);
  188. } else {
  189. script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
  190. }
  191. ccg.AppendArguments(c, script);
  192. // After each custom command, check for an error result.
  193. // If there was an error, jump to the VCReportError label,
  194. // skipping the run of any subsequent commands in this
  195. // sequence.
  196. script += check_error;
  197. }
  198. // Close the local context.
  199. if (useLocal) {
  200. // clang-format off
  201. script = cmStrCat(
  202. script
  203. , newline
  204. , ":cmEnd"
  205. , newline
  206. , "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone"
  207. , newline
  208. , ":cmErrorLevel"
  209. , newline
  210. , "exit /b %1"
  211. , newline
  212. , ":cmDone"
  213. , newline
  214. , "if %errorlevel% neq 0 goto ", this->GetReportErrorLabel()
  215. );
  216. // clang-format on
  217. }
  218. return script;
  219. }
  220. std::string cmLocalVisualStudioGenerator::FinishConstructScript(
  221. VsProjectType projectType, std::string const& newline)
  222. {
  223. bool useLocal = this->CustomCommandUseLocal();
  224. // Store the script in a string.
  225. std::string script;
  226. if (useLocal && projectType != VsProjectType::vcxproj) {
  227. // This label is not provided by MSBuild for C# projects.
  228. script += newline;
  229. script += this->GetReportErrorLabel();
  230. }
  231. return script;
  232. }