cmLocalVisualStudioGenerator.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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()
  19. {
  20. this->WindowsShell = true;
  21. this->WindowsVSIDE = true;
  22. }
  23. //----------------------------------------------------------------------------
  24. cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
  25. {
  26. }
  27. //----------------------------------------------------------------------------
  28. cmsys::auto_ptr<cmCustomCommand>
  29. cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmTarget& target,
  30. const char* config,
  31. bool isFortran)
  32. {
  33. cmsys::auto_ptr<cmCustomCommand> pcc;
  34. // If an executable exports symbols then VS wants to create an
  35. // import library but forgets to create the output directory.
  36. // The Intel Fortran plugin always forgets to the directory.
  37. if(target.GetType() != cmTarget::EXECUTABLE &&
  38. !(isFortran && target.GetType() == cmTarget::SHARED_LIBRARY))
  39. { return pcc; }
  40. std::string outDir = target.GetDirectory(config, false);
  41. std::string impDir = target.GetDirectory(config, true);
  42. if(impDir == outDir) { return pcc; }
  43. // Add a pre-build event to create the directory.
  44. cmCustomCommandLine command;
  45. command.push_back(this->Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  46. command.push_back("-E");
  47. command.push_back("make_directory");
  48. command.push_back(impDir);
  49. std::vector<std::string> no_output;
  50. std::vector<std::string> no_depends;
  51. cmCustomCommandLines commands;
  52. commands.push_back(command);
  53. pcc.reset(new cmCustomCommand(0, no_output, no_depends, commands, 0, 0));
  54. pcc->SetEscapeOldStyle(false);
  55. pcc->SetEscapeAllowMakeVars(true);
  56. return pcc;
  57. }
  58. //----------------------------------------------------------------------------
  59. bool cmLocalVisualStudioGenerator::SourceFileCompiles(const cmSourceFile* sf)
  60. {
  61. // Identify the language of the source file.
  62. if(const char* lang = this->GetSourceFileLanguage(*sf))
  63. {
  64. // Check whether this source will actually be compiled.
  65. return (!sf->GetCustomCommand() &&
  66. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  67. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"));
  68. }
  69. else
  70. {
  71. // Unknown source file language. Assume it will not be compiled.
  72. return false;
  73. }
  74. }
  75. //----------------------------------------------------------------------------
  76. void cmLocalVisualStudioGenerator::CountObjectNames(
  77. const std::vector<cmSourceGroup>& groups,
  78. std::map<cmStdString, int>& counts)
  79. {
  80. for(unsigned int i = 0; i < groups.size(); ++i)
  81. {
  82. cmSourceGroup sg = groups[i];
  83. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  84. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  85. s != srcs.end(); ++s)
  86. {
  87. const cmSourceFile* sf = *s;
  88. if(this->SourceFileCompiles(sf))
  89. {
  90. std::string objectName = cmSystemTools::LowerCase(
  91. cmSystemTools::GetFilenameWithoutLastExtension(
  92. sf->GetFullPath()));
  93. objectName += ".obj";
  94. counts[objectName] += 1;
  95. }
  96. }
  97. this->CountObjectNames(sg.GetGroupChildren(), counts);
  98. }
  99. }
  100. //----------------------------------------------------------------------------
  101. void cmLocalVisualStudioGenerator::InsertNeedObjectNames(
  102. const std::vector<cmSourceGroup>& groups,
  103. std::map<cmStdString, int>& count)
  104. {
  105. for(unsigned int i = 0; i < groups.size(); ++i)
  106. {
  107. cmSourceGroup sg = groups[i];
  108. std::vector<const cmSourceFile*> const& srcs = sg.GetSourceFiles();
  109. for(std::vector<const cmSourceFile*>::const_iterator s = srcs.begin();
  110. s != srcs.end(); ++s)
  111. {
  112. const cmSourceFile* sf = *s;
  113. if(this->SourceFileCompiles(sf))
  114. {
  115. std::string objectName = cmSystemTools::LowerCase(
  116. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  117. objectName += ".obj";
  118. if(count[objectName] > 1)
  119. {
  120. this->NeedObjectName.insert(sf);
  121. }
  122. }
  123. }
  124. this->InsertNeedObjectNames(sg.GetGroupChildren(), count);
  125. }
  126. }
  127. //----------------------------------------------------------------------------
  128. void cmLocalVisualStudioGenerator::ComputeObjectNameRequirements
  129. (std::vector<cmSourceGroup> const& sourceGroups)
  130. {
  131. // Clear the current set of requirements.
  132. this->NeedObjectName.clear();
  133. // Count the number of object files with each name. Note that
  134. // windows file names are not case sensitive.
  135. std::map<cmStdString, int> objectNameCounts;
  136. this->CountObjectNames(sourceGroups, objectNameCounts);
  137. // For all source files producing duplicate names we need unique
  138. // object name computation.
  139. this->InsertNeedObjectNames(sourceGroups, objectNameCounts);
  140. }
  141. //----------------------------------------------------------------------------
  142. const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
  143. {
  144. return ":VCReportError";
  145. }
  146. //----------------------------------------------------------------------------
  147. const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
  148. {
  149. return this->ReportErrorLabel();
  150. }
  151. //----------------------------------------------------------------------------
  152. std::string
  153. cmLocalVisualStudioGenerator
  154. ::ConstructScript(cmCustomCommand const& cc,
  155. const char* configName,
  156. const char* newline_text)
  157. {
  158. bool useLocal = this->CustomCommandUseLocal();
  159. const cmCustomCommandLines& commandLines = cc.GetCommandLines();
  160. const char* workingDirectory = cc.GetWorkingDirectory();
  161. cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
  162. RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
  163. // Avoid leading or trailing newlines.
  164. const char* newline = "";
  165. // Line to check for error between commands.
  166. std::string check_error = newline_text;
  167. if(useLocal)
  168. {
  169. check_error += "if %errorlevel% neq 0 goto :cmEnd";
  170. }
  171. else
  172. {
  173. check_error += "if errorlevel 1 goto ";
  174. check_error += this->GetReportErrorLabel();
  175. }
  176. // Store the script in a string.
  177. std::string script;
  178. // Open a local context.
  179. if(useLocal)
  180. {
  181. script += newline;
  182. newline = newline_text;
  183. script += "set errlev=";
  184. script += newline;
  185. script += "setlocal";
  186. }
  187. if(workingDirectory)
  188. {
  189. // Change the working directory.
  190. script += newline;
  191. newline = newline_text;
  192. script += "cd ";
  193. script += this->Convert(workingDirectory, FULL, SHELL);
  194. script += check_error;
  195. // Change the working drive.
  196. if(workingDirectory[0] && workingDirectory[1] == ':')
  197. {
  198. script += newline;
  199. newline = newline_text;
  200. script += workingDirectory[0];
  201. script += workingDirectory[1];
  202. script += check_error;
  203. }
  204. }
  205. // for visual studio IDE add extra stuff to the PATH
  206. // if CMAKE_MSVCIDE_RUN_PATH is set.
  207. if(this->Makefile->GetDefinition("MSVC_IDE"))
  208. {
  209. const char* extraPath =
  210. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  211. if(extraPath)
  212. {
  213. script += newline;
  214. newline = newline_text;
  215. script += "set PATH=";
  216. script += extraPath;
  217. script += ";%PATH%";
  218. }
  219. }
  220. // Write each command on a single line.
  221. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  222. {
  223. // Start a new line.
  224. script += newline;
  225. newline = newline_text;
  226. // Add this command line.
  227. std::string cmd = ccg.GetCommand(c);
  228. script += this->Convert(cmd.c_str(), relativeRoot, SHELL);
  229. ccg.AppendArguments(c, script);
  230. // After each custom command, check for an error result.
  231. // If there was an error, jump to the VCReportError label,
  232. // skipping the run of any subsequent commands in this
  233. // sequence.
  234. script += check_error;
  235. }
  236. // Close the local context.
  237. if(useLocal)
  238. {
  239. script += newline;
  240. script += ":cmEnd";
  241. script += newline;
  242. script += "endlocal & set errlev=%errorlevel%";
  243. script += newline;
  244. script += "if %errlev% neq 0 goto ";
  245. script += this->GetReportErrorLabel();
  246. }
  247. return script;
  248. }