cmLocalVisualStudioGenerator.cxx 7.6 KB

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