cmLocalVisualStudioGenerator.cxx 9.2 KB

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