cmLocalVisualStudioGenerator.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
  61. {
  62. // Identify the language of the source file.
  63. if(const char* lang = this->GetSourceFileLanguage(*sf))
  64. {
  65. // Check whether this source will actually be compiled.
  66. return (!sf->GetCustomCommand() &&
  67. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  68. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
  69. }
  70. else
  71. {
  72. // Unknown source file language. Assume it will not be compiled.
  73. return false;
  74. }
  75. }
  76. //----------------------------------------------------------------------------
  77. void
  78. cmLocalVisualStudioGenerator::ComputeObjectNameRequirements(
  79. std::vector<cmSourceFile*> const& sources
  80. )
  81. {
  82. // Clear the current set of requirements.
  83. this->NeedObjectName.clear();
  84. // Count the number of object files with each name. Note that
  85. // windows file names are not case sensitive.
  86. std::map<cmStdString, int> counts;
  87. for(std::vector<cmSourceFile*>::const_iterator s = sources.begin();
  88. s != sources.end(); ++s)
  89. {
  90. const cmSourceFile* sf = *s;
  91. if(this->SourceFileCompiles(sf))
  92. {
  93. std::string objectName = cmSystemTools::LowerCase(
  94. cmSystemTools::GetFilenameWithoutLastExtension(
  95. sf->GetFullPath()));
  96. objectName += ".obj";
  97. counts[objectName] += 1;
  98. }
  99. }
  100. // For all source files producing duplicate names we need unique
  101. // object name computation.
  102. for(std::vector<cmSourceFile*>::const_iterator s = sources.begin();
  103. s != sources.end(); ++s)
  104. {
  105. const cmSourceFile* sf = *s;
  106. if(this->SourceFileCompiles(sf))
  107. {
  108. std::string objectName = cmSystemTools::LowerCase(
  109. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  110. objectName += ".obj";
  111. if(counts[objectName] > 1)
  112. {
  113. this->NeedObjectName.insert(sf);
  114. }
  115. }
  116. }
  117. }
  118. //----------------------------------------------------------------------------
  119. const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  120. {
  121. return ":VCReportError";
  122. }
  123. //----------------------------------------------------------------------------
  124. const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  125. {
  126. return this->ReportErrorLabel();
  127. }
  128. //----------------------------------------------------------------------------
  129. std::string
  130. cmLocalVisualStudioGenerator
  131. ::ConstructScript(cmCustomCommand const& cc,
  132. const char* configName,
  133. const char* newline_text)
  134. {
  135. bool useLocal = this->CustomCommandUseLocal();
  136. const cmCustomCommandLines& commandLines = cc.GetCommandLines();
  137. const char* workingDirectory = cc.GetWorkingDirectory();
  138. cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
  139. RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
  140. // Avoid leading or trailing newlines.
  141. const char* newline = "";
  142. // Line to check for error between commands.
  143. std::string check_error = newline_text;
  144. if(useLocal)
  145. {
  146. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  147. }
  148. else
  149. {
  150. check_error += "if errorlevel 1 goto ";
  151. check_error += this->GetReportErrorLabel();
  152. }
  153. // Store the script in a string.
  154. std::string script;
  155. // Open a local context.
  156. if(useLocal)
  157. {
  158. script += newline;
  159. newline = newline_text;
  160. script += "setlocal";
  161. }
  162. if(workingDirectory)
  163. {
  164. // Change the working directory.
  165. script += newline;
  166. newline = newline_text;
  167. script += "cd ";
  168. script += this->Convert(workingDirectory, FULL, SHELL);
  169. script += check_error;
  170. // Change the working drive.
  171. if(workingDirectory[0] && workingDirectory[1] == ':')
  172. {
  173. script += newline;
  174. newline = newline_text;
  175. script += workingDirectory[0];
  176. script += workingDirectory[1];
  177. script += check_error;
  178. }
  179. }
  180. // for visual studio IDE add extra stuff to the PATH
  181. // if CMAKE_MSVCIDE_RUN_PATH is set.
  182. if(this->Makefile->GetDefinition("MSVC_IDE"))
  183. {
  184. const char* extraPath =
  185. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  186. if(extraPath)
  187. {
  188. script += newline;
  189. newline = newline_text;
  190. script += "set PATH=";
  191. script += extraPath;
  192. script += ";%PATH%";
  193. }
  194. }
  195. // Write each command on a single line.
  196. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  197. {
  198. // Start a new line.
  199. script += newline;
  200. newline = newline_text;
  201. // Add this command line.
  202. std::string cmd = ccg.GetCommand(c);
  203. // Use "call " before any invocations of .bat or .cmd files
  204. // invoked as custom commands.
  205. //
  206. std::string suffix;
  207. if (cmd.size() > 4)
  208. {
  209. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size()-4));
  210. if (suffix == ".bat" || suffix == ".cmd")
  211. {
  212. script += "call ";
  213. }
  214. }
  215. script += this->Convert(cmd.c_str(), relativeRoot, SHELL);
  216. ccg.AppendArguments(c, script);
  217. // After each custom command, check for an error result.
  218. // If there was an error, jump to the VCReportError label,
  219. // skipping the run of any subsequent commands in this
  220. // sequence.
  221. script += check_error;
  222. }
  223. // Close the local context.
  224. if(useLocal)
  225. {
  226. script += newline;
  227. script += ":cmEnd";
  228. script += newline;
  229. script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
  230. script += newline;
  231. script += ":cmErrorLevel";
  232. script += newline;
  233. script += "exit /b %1";
  234. script += newline;
  235. script += ":cmDone";
  236. script += newline;
  237. script += "if %errorlevel% neq 0 goto ";
  238. script += this->GetReportErrorLabel();
  239. }
  240. return script;
  241. }