cmLocalVisualStudioGenerator.cxx 8.1 KB

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