1
0

cmLocalVisualStudioGenerator.cxx 8.1 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, cmMakefile* mf)
  20. : cmLocalGenerator(gg, mf)
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  25. {
  26. }
  27. //----------------------------------------------------------------------------
  28. cmGlobalVisualStudioGenerator::VSVersion
  29. cmLocalVisualStudioGenerator::GetVersion() const
  30. {
  31. cmGlobalVisualStudioGenerator* gg =
  32. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  33. return gg->GetVersion();
  34. }
  35. //----------------------------------------------------------------------------
  36. void cmLocalVisualStudioGenerator::ComputeObjectFilenames(
  37. std::map<cmSourceFile const*, std::string>& mapping,
  38. cmGeneratorTarget const* gt)
  39. {
  40. std::string dir_max = this->ComputeLongestObjectDirectory(gt);
  41. // Count the number of object files with each name. Note that
  42. // windows file names are not case sensitive.
  43. std::map<std::string, int> counts;
  44. for(std::map<cmSourceFile const*, std::string>::iterator
  45. si = mapping.begin(); si != mapping.end(); ++si)
  46. {
  47. cmSourceFile const* sf = si->first;
  48. std::string objectNameLower = cmSystemTools::LowerCase(
  49. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  50. objectNameLower += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  51. counts[objectNameLower] += 1;
  52. }
  53. // For all source files producing duplicate names we need unique
  54. // object name computation.
  55. for(std::map<cmSourceFile const*, std::string>::iterator
  56. si = mapping.begin(); si != mapping.end(); ++si)
  57. {
  58. cmSourceFile const* sf = si->first;
  59. std::string objectName =
  60. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  61. objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
  62. if(counts[cmSystemTools::LowerCase(objectName)] > 1)
  63. {
  64. const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
  65. objectName = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
  66. }
  67. si->second = objectName;
  68. }
  69. }
  70. //----------------------------------------------------------------------------
  71. cmsys::auto_ptr<cmCustomCommand>
  72. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmTarget& target,
  73. const std::string& config,
  74. bool isFortran)
  75. {
  76. cmsys::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() != cmState::EXECUTABLE &&
  81. !(isFortran && target.GetType() == cmState::SHARED_LIBRARY))
  82. { return pcc; }
  83. cmGeneratorTarget* gt =
  84. this->GetGlobalGenerator()->GetGeneratorTarget(&target);
  85. std::string outDir = gt->GetDirectory(config, false);
  86. std::string impDir = gt->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. }