1
0

cmLocalVisualStudioGenerator.cxx 7.1 KB

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