cmLocalVisualStudioGenerator.cxx 7.7 KB

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