cmLocalVisualStudioGenerator.cxx 7.6 KB

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