cmLocalVisualStudioGenerator.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "windows.h"
  5. #include "cmCustomCommand.h"
  6. #include "cmCustomCommandGenerator.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmSourceFile.h"
  11. #include "cmSystemTools.h"
  12. cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator(
  13. cmGlobalGenerator* gg, cmMakefile* mf)
  14. : cmLocalGenerator(gg, mf)
  15. {
  16. }
  17. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  18. {
  19. }
  20. cmGlobalVisualStudioGenerator::VSVersion
  21. cmLocalVisualStudioGenerator::GetVersion() const
  22. {
  23. cmGlobalVisualStudioGenerator* gg =
  24. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  25. return gg->GetVersion();
  26. }
  27. void cmLocalVisualStudioGenerator::ComputeObjectFilenames(
  28. std::map<cmSourceFile const*, std::string>& mapping,
  29. cmGeneratorTarget const* gt)
  30. {
  31. char const* custom_ext = gt->GetCustomObjectExtension();
  32. std::string dir_max = this->ComputeLongestObjectDirectory(gt);
  33. // Count the number of object files with each name. Note that
  34. // windows file names are not case sensitive.
  35. std::map<std::string, int> counts;
  36. for (auto const& si : mapping) {
  37. cmSourceFile const* sf = si.first;
  38. std::string objectNameLower = cmSystemTools::LowerCase(
  39. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  40. if (custom_ext) {
  41. objectNameLower += custom_ext;
  42. } else {
  43. objectNameLower +=
  44. this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  45. }
  46. counts[objectNameLower] += 1;
  47. }
  48. // For all source files producing duplicate names we need unique
  49. // object name computation.
  50. for (auto& si : mapping) {
  51. cmSourceFile const* sf = si.first;
  52. std::string objectName =
  53. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  54. if (custom_ext) {
  55. objectName += custom_ext;
  56. } else {
  57. objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  58. }
  59. if (counts[cmSystemTools::LowerCase(objectName)] > 1) {
  60. const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
  61. bool keptSourceExtension;
  62. objectName = this->GetObjectFileNameWithoutTarget(
  63. *sf, dir_max, &keptSourceExtension, custom_ext);
  64. }
  65. si.second = objectName;
  66. }
  67. }
  68. std::unique_ptr<cmCustomCommand>
  69. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
  70. const std::string& config,
  71. bool isFortran)
  72. {
  73. std::unique_ptr<cmCustomCommand> pcc;
  74. // If an executable exports symbols then VS wants to create an
  75. // import library but forgets to create the output directory.
  76. // The Intel Fortran plugin always forgets to the directory.
  77. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  78. !(isFortran && target->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  79. return pcc;
  80. }
  81. std::string outDir =
  82. target->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  83. std::string impDir =
  84. target->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  85. if (impDir == outDir) {
  86. return pcc;
  87. }
  88. // Add a pre-build event to create the directory.
  89. std::vector<std::string> no_output;
  90. std::vector<std::string> no_byproducts;
  91. std::vector<std::string> no_depends;
  92. bool stdPipesUTF8 = true;
  93. cmCustomCommandLines commands = cmMakeSingleCommandLine(
  94. { cmSystemTools::GetCMakeCommand(), "-E", "make_directory", impDir });
  95. pcc.reset(new cmCustomCommand(no_output, no_byproducts, no_depends, commands,
  96. cmListFileBacktrace(), nullptr, nullptr,
  97. stdPipesUTF8));
  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->Makefile->GetDefinition("MSVC_IDE")) {
  152. cmProp extraPath = this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  153. if (extraPath) {
  154. script += newline;
  155. newline = newline_text;
  156. script += "set PATH=";
  157. script += *extraPath;
  158. script += ";%PATH%";
  159. }
  160. }
  161. // Write each command on a single line.
  162. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  163. // Add this command line.
  164. std::string cmd = ccg.GetCommand(c);
  165. if (cmd.empty()) {
  166. continue;
  167. }
  168. // Start a new line.
  169. script += newline;
  170. newline = newline_text;
  171. // Use "call " before any invocations of .bat or .cmd files
  172. // invoked as custom commands.
  173. //
  174. std::string suffix;
  175. if (cmd.size() > 4) {
  176. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  177. if (suffix == ".bat" || suffix == ".cmd") {
  178. script += "call ";
  179. }
  180. }
  181. if (workingDirectory.empty()) {
  182. script +=
  183. this->ConvertToOutputFormat(this->MaybeConvertToRelativePath(
  184. this->GetCurrentBinaryDirectory(), cmd),
  185. 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. }