cmLocalVisualStudioGenerator.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, const std::string& newline_text)
  113. {
  114. bool useLocal = this->CustomCommandUseLocal();
  115. std::string workingDirectory = ccg.GetWorkingDirectory();
  116. // Avoid leading or trailing newlines.
  117. std::string newline;
  118. // Line to check for error between commands.
  119. std::string check_error = newline_text;
  120. if (useLocal) {
  121. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  122. } else {
  123. check_error += "if errorlevel 1 goto ";
  124. check_error += this->GetReportErrorLabel();
  125. }
  126. // Store the script in a string.
  127. std::string script;
  128. // Open a local context.
  129. if (useLocal) {
  130. script += newline;
  131. newline = newline_text;
  132. script += "setlocal";
  133. }
  134. if (!workingDirectory.empty()) {
  135. // Change the working directory.
  136. script += newline;
  137. newline = newline_text;
  138. script += "cd ";
  139. script += this->ConvertToOutputFormat(workingDirectory, SHELL);
  140. script += check_error;
  141. // Change the working drive.
  142. if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
  143. script += newline;
  144. newline = newline_text;
  145. script += workingDirectory[0];
  146. script += workingDirectory[1];
  147. script += check_error;
  148. }
  149. }
  150. // for visual studio IDE add extra stuff to the PATH
  151. // if CMAKE_MSVCIDE_RUN_PATH is set.
  152. if (this->GetGlobalGenerator()->IsVisualStudio()) {
  153. cmValue extraPath =
  154. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  155. if (extraPath) {
  156. script += newline;
  157. newline = newline_text;
  158. script += "set PATH=";
  159. script += *extraPath;
  160. script += ";%PATH%";
  161. }
  162. }
  163. // Write each command on a single line.
  164. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  165. // Add this command line.
  166. std::string cmd = ccg.GetCommand(c);
  167. if (cmd.empty()) {
  168. continue;
  169. }
  170. // Start a new line.
  171. script += newline;
  172. newline = newline_text;
  173. // Use "call " before any invocations of .bat or .cmd files
  174. // invoked as custom commands.
  175. //
  176. std::string suffix;
  177. if (cmd.size() > 4) {
  178. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  179. if (suffix == ".bat" || suffix == ".cmd") {
  180. script += "call ";
  181. }
  182. }
  183. if (workingDirectory.empty()) {
  184. script += this->ConvertToOutputFormat(
  185. this->MaybeRelativeToCurBinDir(cmd), cmOutputConverter::SHELL);
  186. } else {
  187. script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
  188. }
  189. ccg.AppendArguments(c, script);
  190. // After each custom command, check for an error result.
  191. // If there was an error, jump to the VCReportError label,
  192. // skipping the run of any subsequent commands in this
  193. // sequence.
  194. script += check_error;
  195. }
  196. // Close the local context.
  197. if (useLocal) {
  198. script += newline;
  199. script += ":cmEnd";
  200. script += newline;
  201. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  202. script += newline;
  203. script += ":cmErrorLevel";
  204. script += newline;
  205. script += "exit /b %1";
  206. script += newline;
  207. script += ":cmDone";
  208. script += newline;
  209. script += "if %errorlevel% neq 0 goto ";
  210. script += this->GetReportErrorLabel();
  211. }
  212. return script;
  213. }