cmLocalVisualStudioGenerator.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 baseObjectName;
  45. if (gt->GetUseShortObjectNames()) {
  46. baseObjectName = this->GetShortObjectFileName(*sf);
  47. } else {
  48. baseObjectName =
  49. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  50. }
  51. std::string objectNameLower = cmSystemTools::LowerCase(baseObjectName);
  52. if (custom_ext) {
  53. objectNameLower += custom_ext;
  54. } else {
  55. objectNameLower +=
  56. this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  57. }
  58. counts[objectNameLower] += 1;
  59. }
  60. // For all source files producing duplicate names we need unique
  61. // object name computation.
  62. for (auto& si : mapping) {
  63. cmSourceFile const* sf = si.first;
  64. std::string objectName;
  65. if (gt->GetUseShortObjectNames()) {
  66. objectName = this->GetShortObjectFileName(*sf);
  67. } else {
  68. objectName =
  69. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  70. }
  71. if (custom_ext) {
  72. objectName += custom_ext;
  73. } else {
  74. objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  75. }
  76. if (counts[cmSystemTools::LowerCase(objectName)] > 1) {
  77. const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
  78. bool keptSourceExtension;
  79. objectName = this->GetObjectFileNameWithoutTarget(
  80. *sf, dir_max, &keptSourceExtension, custom_ext);
  81. }
  82. si.second = objectName;
  83. }
  84. }
  85. std::string cmLocalVisualStudioGenerator::GetObjectOutputRoot(
  86. cmStateEnums::IntermediateDirKind kind) const
  87. {
  88. if (this->UseShortObjectNames(kind)) {
  89. return cmStrCat(this->GetCurrentBinaryDirectory(), '/',
  90. this->GetGlobalGenerator()->GetShortBinaryOutputDir());
  91. }
  92. return this->GetCurrentBinaryDirectory();
  93. }
  94. bool cmLocalVisualStudioGenerator::AlwaysUsesCMFPaths() const
  95. {
  96. return false;
  97. }
  98. std::unique_ptr<cmCustomCommand>
  99. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
  100. std::string const& config,
  101. bool isFortran)
  102. {
  103. std::unique_ptr<cmCustomCommand> pcc;
  104. // If an executable exports symbols then VS wants to create an
  105. // import library but forgets to create the output directory.
  106. // The Intel Fortran plugin always forgets to the directory.
  107. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  108. !(isFortran && target->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  109. return pcc;
  110. }
  111. std::string outDir =
  112. target->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  113. std::string impDir =
  114. target->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  115. if (impDir == outDir) {
  116. return pcc;
  117. }
  118. // Add a pre-build event to create the directory.
  119. cmCustomCommandLines commands = cmMakeSingleCommandLine(
  120. { cmSystemTools::GetCMakeCommand(), "-E", "make_directory", impDir });
  121. pcc = cm::make_unique<cmCustomCommand>();
  122. pcc->SetCommandLines(commands);
  123. pcc->SetStdPipesUTF8(true);
  124. pcc->SetEscapeOldStyle(false);
  125. pcc->SetEscapeAllowMakeVars(true);
  126. return pcc;
  127. }
  128. char const* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  129. {
  130. return ":VCReportError";
  131. }
  132. char const* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  133. {
  134. return this->ReportErrorLabel();
  135. }
  136. std::string cmLocalVisualStudioGenerator::ConstructScript(
  137. cmCustomCommandGenerator const& ccg, std::string const& newline_text)
  138. {
  139. bool useLocal = this->CustomCommandUseLocal();
  140. std::string workingDirectory = ccg.GetWorkingDirectory();
  141. // Avoid leading or trailing newlines.
  142. std::string newline;
  143. // Line to check for error between commands.
  144. std::string check_error;
  145. if (useLocal) {
  146. check_error = cmStrCat(newline_text, "if %errorlevel% neq 0 goto :cmEnd");
  147. } else {
  148. check_error = cmStrCat(newline_text, "if errorlevel 1 goto ",
  149. this->GetReportErrorLabel());
  150. }
  151. // Store the script in a string.
  152. std::string script;
  153. // Open a local context.
  154. if (useLocal) {
  155. script = cmStrCat(newline, "setlocal");
  156. newline = newline_text;
  157. }
  158. if (!workingDirectory.empty()) {
  159. // Change the working directory.
  160. script = cmStrCat(script, newline, "cd ",
  161. this->ConvertToOutputFormat(workingDirectory, SHELL),
  162. check_error);
  163. newline = newline_text;
  164. // Change the working drive.
  165. if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
  166. script = cmStrCat(script, newline, workingDirectory[0],
  167. workingDirectory[1], check_error);
  168. newline = newline_text;
  169. }
  170. }
  171. // for visual studio IDE add extra stuff to the PATH
  172. // if CMAKE_MSVCIDE_RUN_PATH is set.
  173. if (this->GetGlobalGenerator()->IsVisualStudio()) {
  174. cmValue extraPath =
  175. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  176. if (extraPath) {
  177. script = cmStrCat(script, newline, "set PATH=", *extraPath, ";%PATH%");
  178. newline = newline_text;
  179. }
  180. }
  181. // Write each command on a single line.
  182. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  183. // Add this command line.
  184. std::string cmd = ccg.GetCommand(c);
  185. if (cmd.empty()) {
  186. continue;
  187. }
  188. // Start a new line.
  189. script += newline;
  190. newline = newline_text;
  191. // Use "call " before any invocations of .bat or .cmd files
  192. // invoked as custom commands.
  193. //
  194. std::string suffix;
  195. if (cmd.size() > 4) {
  196. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  197. if (suffix == ".bat"_s || suffix == ".cmd"_s) {
  198. script += "call ";
  199. }
  200. }
  201. if (workingDirectory.empty()) {
  202. script += this->ConvertToOutputFormat(
  203. this->MaybeRelativeToCurBinDir(cmd), cmOutputConverter::SHELL);
  204. } else {
  205. script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
  206. }
  207. ccg.AppendArguments(c, script);
  208. // After each custom command, check for an error result.
  209. // If there was an error, jump to the VCReportError label,
  210. // skipping the run of any subsequent commands in this
  211. // sequence.
  212. script += check_error;
  213. }
  214. // Close the local context.
  215. if (useLocal) {
  216. // clang-format off
  217. script = cmStrCat(
  218. script
  219. , newline
  220. , ":cmEnd"
  221. , newline
  222. , "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone"
  223. , newline
  224. , ":cmErrorLevel"
  225. , newline
  226. , "exit /b %1"
  227. , newline
  228. , ":cmDone"
  229. , newline
  230. , "if %errorlevel% neq 0 goto ", this->GetReportErrorLabel()
  231. );
  232. // clang-format on
  233. }
  234. return script;
  235. }
  236. std::string cmLocalVisualStudioGenerator::FinishConstructScript(
  237. VsProjectType projectType, std::string const& newline)
  238. {
  239. bool useLocal = this->CustomCommandUseLocal();
  240. // Store the script in a string.
  241. std::string script;
  242. if (useLocal && projectType != VsProjectType::vcxproj) {
  243. // This label is not provided by MSBuild for C# projects.
  244. script += newline;
  245. script += this->GetReportErrorLabel();
  246. }
  247. return script;
  248. }