cmLocalVisualStudioGenerator.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. std::string cmLocalVisualStudioGenerator::CheckForErrorLine()
  143. {
  144. return "if errorlevel 1 goto :VCReportError";
  145. }
  146. //----------------------------------------------------------------------------
  147. std::string cmLocalVisualStudioGenerator::GetCheckForErrorLine()
  148. {
  149. return this->CheckForErrorLine();
  150. }
  151. //----------------------------------------------------------------------------
  152. std::string
  153. cmLocalVisualStudioGenerator
  154. ::ConstructScript(cmCustomCommand const& cc,
  155. const char* configName,
  156. const char* newline_text)
  157. {
  158. const cmCustomCommandLines& commandLines = cc.GetCommandLines();
  159. const char* workingDirectory = cc.GetWorkingDirectory();
  160. cmCustomCommandGenerator ccg(cc, configName, this->Makefile);
  161. RelativeRoot relativeRoot = workingDirectory? NONE : START_OUTPUT;
  162. // Avoid leading or trailing newlines.
  163. const char* newline = "";
  164. // Store the script in a string.
  165. std::string script;
  166. if(workingDirectory)
  167. {
  168. // Change the working directory.
  169. script += newline;
  170. newline = newline_text;
  171. script += "cd ";
  172. script += this->Convert(workingDirectory, FULL, SHELL);
  173. // Change the working drive.
  174. if(workingDirectory[0] && workingDirectory[1] == ':')
  175. {
  176. script += newline;
  177. newline = newline_text;
  178. script += workingDirectory[0];
  179. script += workingDirectory[1];
  180. }
  181. }
  182. // for visual studio IDE add extra stuff to the PATH
  183. // if CMAKE_MSVCIDE_RUN_PATH is set.
  184. if(this->Makefile->GetDefinition("MSVC_IDE"))
  185. {
  186. const char* extraPath =
  187. this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
  188. if(extraPath)
  189. {
  190. script += newline;
  191. newline = newline_text;
  192. script += "set PATH=";
  193. script += extraPath;
  194. script += ";%PATH%";
  195. }
  196. }
  197. // Write each command on a single line.
  198. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  199. {
  200. // Start a new line.
  201. script += newline;
  202. newline = newline_text;
  203. // Add this command line.
  204. std::string cmd = ccg.GetCommand(c);
  205. script += this->Convert(cmd.c_str(), relativeRoot, SHELL);
  206. ccg.AppendArguments(c, script);
  207. // After each custom command, check for an error result.
  208. // If there was an error, jump to the VCReportError label,
  209. // skipping the run of any subsequent commands in this
  210. // sequence.
  211. //
  212. script += newline_text;
  213. script += this->GetCheckForErrorLine();
  214. }
  215. return script;
  216. }