cmLocalVisualStudioGenerator.cxx 7.4 KB

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