cmLocalVisualStudioGenerator.cxx 7.7 KB

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