cmLocalVisualStudioGenerator.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmLocalVisualStudioGenerator.h"
  11. #include "cmCustomCommandGenerator.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmSourceFile.h"
  15. #include "cmSystemTools.h"
  16. #include "windows.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. std::string dir_max = this->ComputeLongestObjectDirectory(gt);
  37. // Count the number of object files with each name. Note that
  38. // windows file names are not case sensitive.
  39. std::map<std::string, int> counts;
  40. for (std::map<cmSourceFile const*, std::string>::iterator si =
  41. mapping.begin();
  42. si != mapping.end(); ++si) {
  43. cmSourceFile const* sf = si->first;
  44. std::string objectNameLower = cmSystemTools::LowerCase(
  45. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  46. objectNameLower += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  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. objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  58. if (counts[cmSystemTools::LowerCase(objectName)] > 1) {
  59. const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
  60. objectName = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
  61. }
  62. si->second = objectName;
  63. }
  64. }
  65. CM_AUTO_PTR<cmCustomCommand>
  66. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
  67. const std::string& config,
  68. bool isFortran)
  69. {
  70. CM_AUTO_PTR<cmCustomCommand> pcc;
  71. // If an executable exports symbols then VS wants to create an
  72. // import library but forgets to create the output directory.
  73. // The Intel Fortran plugin always forgets to the directory.
  74. if (target->GetType() != cmState::EXECUTABLE &&
  75. !(isFortran && target->GetType() == cmState::SHARED_LIBRARY)) {
  76. return pcc;
  77. }
  78. std::string outDir = target->GetDirectory(config, false);
  79. std::string impDir = target->GetDirectory(config, true);
  80. if (impDir == outDir) {
  81. return pcc;
  82. }
  83. // Add a pre-build event to create the directory.
  84. cmCustomCommandLine command;
  85. command.push_back(cmSystemTools::GetCMakeCommand());
  86. command.push_back("-E");
  87. command.push_back("make_directory");
  88. command.push_back(impDir);
  89. std::vector<std::string> no_output;
  90. std::vector<std::string> no_byproducts;
  91. std::vector<std::string> no_depends;
  92. cmCustomCommandLines commands;
  93. commands.push_back(command);
  94. pcc.reset(new cmCustomCommand(0, no_output, no_byproducts, no_depends,
  95. commands, 0, 0));
  96. pcc->SetEscapeOldStyle(false);
  97. pcc->SetEscapeAllowMakeVars(true);
  98. return pcc;
  99. }
  100. const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  101. {
  102. return ":VCReportError";
  103. }
  104. const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  105. {
  106. return this->ReportErrorLabel();
  107. }
  108. std::string cmLocalVisualStudioGenerator::ConstructScript(
  109. cmCustomCommandGenerator const& ccg, const std::string& newline_text)
  110. {
  111. bool useLocal = this->CustomCommandUseLocal();
  112. std::string workingDirectory = ccg.GetWorkingDirectory();
  113. // Avoid leading or trailing newlines.
  114. std::string newline = "";
  115. // Line to check for error between commands.
  116. std::string check_error = newline_text;
  117. if (useLocal) {
  118. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  119. } else {
  120. check_error += "if errorlevel 1 goto ";
  121. check_error += this->GetReportErrorLabel();
  122. }
  123. // Store the script in a string.
  124. std::string script;
  125. // Open a local context.
  126. if (useLocal) {
  127. script += newline;
  128. newline = newline_text;
  129. script += "setlocal";
  130. }
  131. if (!workingDirectory.empty()) {
  132. // Change the working directory.
  133. script += newline;
  134. newline = newline_text;
  135. script += "cd ";
  136. script += this->ConvertToOutputFormat(
  137. cmSystemTools::CollapseFullPath(workingDirectory), SHELL);
  138. script += check_error;
  139. // Change the working drive.
  140. if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
  141. script += newline;
  142. newline = newline_text;
  143. script += workingDirectory[0];
  144. script += workingDirectory[1];
  145. script += check_error;
  146. }
  147. }
  148. // for visual studio IDE add extra stuff to the PATH
  149. // if CMAKE_MSVCIDE_RUN_PATH is set.
  150. if (this->Makefile->GetDefinition("MSVC_IDE")) {
  151. const char* extraPath =
  152. 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. // Start a new line.
  164. script += newline;
  165. newline = newline_text;
  166. // Add this command line.
  167. std::string cmd = ccg.GetCommand(c);
  168. // Use "call " before any invocations of .bat or .cmd files
  169. // invoked as custom commands.
  170. //
  171. std::string suffix;
  172. if (cmd.size() > 4) {
  173. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
  174. if (suffix == ".bat" || suffix == ".cmd") {
  175. script += "call ";
  176. }
  177. }
  178. if (workingDirectory.empty()) {
  179. script += this->Convert(cmd.c_str(), START_OUTPUT, SHELL);
  180. } else {
  181. script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
  182. }
  183. ccg.AppendArguments(c, script);
  184. // After each custom command, check for an error result.
  185. // If there was an error, jump to the VCReportError label,
  186. // skipping the run of any subsequent commands in this
  187. // sequence.
  188. script += check_error;
  189. }
  190. // Close the local context.
  191. if (useLocal) {
  192. script += newline;
  193. script += ":cmEnd";
  194. script += newline;
  195. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  196. script += newline;
  197. script += ":cmErrorLevel";
  198. script += newline;
  199. script += "exit /b %1";
  200. script += newline;
  201. script += ":cmDone";
  202. script += newline;
  203. script += "if %errorlevel% neq 0 goto ";
  204. script += this->GetReportErrorLabel();
  205. }
  206. return script;
  207. }