cmLocalVisualStudioGenerator.cxx 7.5 KB

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