cmLocalVisualStudioGenerator.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. cmsys::auto_ptr<cmCustomCommand>
  30. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmTarget& target,
  31. const std::string& config,
  32. bool isFortran)
  33. {
  34. cmsys::auto_ptr<cmCustomCommand> pcc;
  35. // If an executable exports symbols then VS wants to create an
  36. // import library but forgets to create the output directory.
  37. // The Intel Fortran plugin always forgets to the directory.
  38. if(target.GetType() != cmTarget::EXECUTABLE &&
  39. !(isFortran && target.GetType() == cmTarget::SHARED_LIBRARY))
  40. { return pcc; }
  41. std::string outDir = target.GetDirectory(config, false);
  42. std::string impDir = target.GetDirectory(config, true);
  43. if(impDir == outDir) { return pcc; }
  44. // Add a pre-build event to create the directory.
  45. cmCustomCommandLine command;
  46. command.push_back(this->Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  47. command.push_back("-E");
  48. command.push_back("make_directory");
  49. command.push_back(impDir);
  50. std::vector<std::string> no_output;
  51. std::vector<std::string> no_depends;
  52. cmCustomCommandLines commands;
  53. commands.push_back(command);
  54. pcc.reset(new cmCustomCommand(0, no_output, no_depends, commands, 0, 0));
  55. pcc->SetEscapeOldStyle(false);
  56. pcc->SetEscapeAllowMakeVars(true);
  57. return pcc;
  58. }
  59. //----------------------------------------------------------------------------
  60. const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  61. {
  62. return ":VCReportError";
  63. }
  64. //----------------------------------------------------------------------------
  65. const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  66. {
  67. return this->ReportErrorLabel();
  68. }
  69. //----------------------------------------------------------------------------
  70. std::string
  71. cmLocalVisualStudioGenerator
  72. ::ConstructScript(cmCustomCommand const& cc,
  73. const std::string& configName,
  74. const std::string& newline_text)
  75. {
  76. bool useLocal = this->CustomCommandUseLocal();
  77. const char* workingDirectory = cc.GetWorkingDirectory();
  78. cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
  79. RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
  80. // Avoid leading or trailing newlines.
  81. std::string newline = "";
  82. // Line to check for error between commands.
  83. std::string check_error = newline_text;
  84. if(useLocal)
  85. {
  86. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  87. }
  88. else
  89. {
  90. check_error += "if errorlevel 1 goto ";
  91. check_error += this->GetReportErrorLabel();
  92. }
  93. // Store the script in a string.
  94. std::string script;
  95. // Open a local context.
  96. if(useLocal)
  97. {
  98. script += newline;
  99. newline = newline_text;
  100. script += "setlocal";
  101. }
  102. if(workingDirectory)
  103. {
  104. // Change the working directory.
  105. script += newline;
  106. newline = newline_text;
  107. script += "cd ";
  108. script += this->Convert(workingDirectory, FULL, SHELL);
  109. script += check_error;
  110. // Change the working drive.
  111. if(workingDirectory[0] && workingDirectory[1] == ':')
  112. {
  113. script += newline;
  114. newline = newline_text;
  115. script += workingDirectory[0];
  116. script += workingDirectory[1];
  117. script += check_error;
  118. }
  119. }
  120. // for visual studio IDE add extra stuff to the PATH
  121. // if CMAKE_MSVCIDE_RUN_PATH is set.
  122. if(this->Makefile->GetDefinition("MSVC_IDE"))
  123. {
  124. const char* extraPath =
  125. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  126. if(extraPath)
  127. {
  128. script += newline;
  129. newline = newline_text;
  130. script += "set PATH=";
  131. script += extraPath;
  132. script += ";%PATH%";
  133. }
  134. }
  135. // Write each command on a single line.
  136. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  137. {
  138. // Start a new line.
  139. script += newline;
  140. newline = newline_text;
  141. // Add this command line.
  142. std::string cmd = ccg.GetCommand(c);
  143. // Use "call " before any invocations of .bat or .cmd files
  144. // invoked as custom commands.
  145. //
  146. std::string suffix;
  147. if (cmd.size() > 4)
  148. {
  149. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size()-4));
  150. if (suffix == ".bat" || suffix == ".cmd")
  151. {
  152. script += "call ";
  153. }
  154. }
  155. script += this->Convert(cmd.c_str(), relativeRoot, SHELL);
  156. ccg.AppendArguments(c, script);
  157. // After each custom command, check for an error result.
  158. // If there was an error, jump to the VCReportError label,
  159. // skipping the run of any subsequent commands in this
  160. // sequence.
  161. script += check_error;
  162. }
  163. // Close the local context.
  164. if(useLocal)
  165. {
  166. script += newline;
  167. script += ":cmEnd";
  168. script += newline;
  169. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  170. script += newline;
  171. script += ":cmErrorLevel";
  172. script += newline;
  173. script += "exit /b %1";
  174. script += newline;
  175. script += ":cmDone";
  176. script += newline;
  177. script += "if %errorlevel% neq 0 goto ";
  178. script += this->GetReportErrorLabel();
  179. }
  180. return script;
  181. }