cmLocalVisualStudioGenerator.cxx 8.0 KB

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