cmLocalVisualStudioGenerator.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 char* 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 char* configName,
  74. const char* newline_text)
  75. {
  76. bool useLocal = this->CustomCommandUseLocal();
  77. const cmCustomCommandLines& commandLines = cc.GetCommandLines();
  78. const char* workingDirectory = cc.GetWorkingDirectory();
  79. cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
  80. RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
  81. // Avoid leading or trailing newlines.
  82. const char* newline = "";
  83. // Line to check for error between commands.
  84. std::string check_error = newline_text;
  85. if(useLocal)
  86. {
  87. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  88. }
  89. else
  90. {
  91. check_error += "if errorlevel 1 goto ";
  92. check_error += this->GetReportErrorLabel();
  93. }
  94. // Store the script in a string.
  95. std::string script;
  96. // Open a local context.
  97. if(useLocal)
  98. {
  99. script += newline;
  100. newline = newline_text;
  101. script += "setlocal";
  102. }
  103. if(workingDirectory)
  104. {
  105. // Change the working directory.
  106. script += newline;
  107. newline = newline_text;
  108. script += "cd ";
  109. script += this->Convert(workingDirectory, FULL, SHELL);
  110. script += check_error;
  111. // Change the working drive.
  112. if(workingDirectory[0] && workingDirectory[1] == ':')
  113. {
  114. script += newline;
  115. newline = newline_text;
  116. script += workingDirectory[0];
  117. script += workingDirectory[1];
  118. script += check_error;
  119. }
  120. }
  121. // for visual studio IDE add extra stuff to the PATH
  122. // if CMAKE_MSVCIDE_RUN_PATH is set.
  123. if(this->Makefile->GetDefinition("MSVC_IDE"))
  124. {
  125. const char* extraPath =
  126. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  127. if(extraPath)
  128. {
  129. script += newline;
  130. newline = newline_text;
  131. script += "set PATH=";
  132. script += extraPath;
  133. script += ";%PATH%";
  134. }
  135. }
  136. // Write each command on a single line.
  137. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  138. {
  139. // Start a new line.
  140. script += newline;
  141. newline = newline_text;
  142. // Add this command line.
  143. std::string cmd = ccg.GetCommand(c);
  144. // Use "call " before any invocations of .bat or .cmd files
  145. // invoked as custom commands.
  146. //
  147. std::string suffix;
  148. if (cmd.size() > 4)
  149. {
  150. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size()-4));
  151. if (suffix == ".bat" || suffix == ".cmd")
  152. {
  153. script += "call ";
  154. }
  155. }
  156. script += this->Convert(cmd.c_str(), relativeRoot, SHELL);
  157. ccg.AppendArguments(c, script);
  158. // After each custom command, check for an error result.
  159. // If there was an error, jump to the VCReportError label,
  160. // skipping the run of any subsequent commands in this
  161. // sequence.
  162. script += check_error;
  163. }
  164. // Close the local context.
  165. if(useLocal)
  166. {
  167. script += newline;
  168. script += ":cmEnd";
  169. script += newline;
  170. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  171. script += newline;
  172. script += ":cmErrorLevel";
  173. script += newline;
  174. script += "exit /b %1";
  175. script += newline;
  176. script += ":cmDone";
  177. script += newline;
  178. script += "if %errorlevel% neq 0 goto ";
  179. script += this->GetReportErrorLabel();
  180. }
  181. return script;
  182. }