cmGeneratorTarget.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2012 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 "cmGeneratorTarget.h"
  11. #include "cmTarget.h"
  12. #include "cmMakefile.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmSourceFile.h"
  16. //----------------------------------------------------------------------------
  17. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t): Target(t)
  18. {
  19. this->Makefile = this->Target->GetMakefile();
  20. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  21. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  22. this->ClassifySources();
  23. this->LookupObjectLibraries();
  24. }
  25. //----------------------------------------------------------------------------
  26. void cmGeneratorTarget::ClassifySources()
  27. {
  28. cmsys::RegularExpression header(CM_HEADER_REGEX);
  29. bool isObjLib = this->Target->GetType() == cmTarget::OBJECT_LIBRARY;
  30. std::vector<cmSourceFile*> badObjLib;
  31. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  32. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  33. si != sources.end(); ++si)
  34. {
  35. cmSourceFile* sf = *si;
  36. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  37. if(sf->GetCustomCommand())
  38. {
  39. this->CustomCommands.push_back(sf);
  40. }
  41. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  42. {
  43. this->HeaderSources.push_back(sf);
  44. }
  45. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  46. {
  47. this->ExternalObjects.push_back(sf);
  48. if(isObjLib) { badObjLib.push_back(sf); }
  49. }
  50. else if(sf->GetLanguage())
  51. {
  52. this->ObjectSources.push_back(sf);
  53. }
  54. else if(ext == "def")
  55. {
  56. this->ModuleDefinitionFile = sf->GetFullPath();
  57. if(isObjLib) { badObjLib.push_back(sf); }
  58. }
  59. else if(ext == "idl")
  60. {
  61. this->IDLSources.push_back(sf);
  62. if(isObjLib) { badObjLib.push_back(sf); }
  63. }
  64. else if(header.find(sf->GetFullPath().c_str()))
  65. {
  66. this->HeaderSources.push_back(sf);
  67. }
  68. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  69. {
  70. // We only get here if a source file is not an external object
  71. // and has an extension that is listed as an ignored file type.
  72. // No message or diagnosis should be given.
  73. this->ExtraSources.push_back(sf);
  74. }
  75. else
  76. {
  77. this->ExtraSources.push_back(sf);
  78. if(isObjLib && ext != "txt")
  79. {
  80. badObjLib.push_back(sf);
  81. }
  82. }
  83. }
  84. if(!badObjLib.empty())
  85. {
  86. cmOStringStream e;
  87. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  88. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  89. i != badObjLib.end(); ++i)
  90. {
  91. e << " " << (*i)->GetLocation().GetName() << "\n";
  92. }
  93. e << "but may contain only headers and sources that compile.";
  94. this->GlobalGenerator->GetCMakeInstance()
  95. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  96. this->Target->GetBacktrace());
  97. }
  98. }
  99. //----------------------------------------------------------------------------
  100. void cmGeneratorTarget::LookupObjectLibraries()
  101. {
  102. std::vector<std::string> const& objLibs =
  103. this->Target->GetObjectLibraries();
  104. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  105. oli != objLibs.end(); ++oli)
  106. {
  107. std::string const& objLibName = *oli;
  108. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName.c_str()))
  109. {
  110. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  111. {
  112. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  113. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  114. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  115. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  116. {
  117. this->GlobalGenerator->GetCMakeInstance()
  118. ->IssueMessage(cmake::FATAL_ERROR,
  119. "Only executables and non-OBJECT libraries may "
  120. "reference target objects.",
  121. this->Target->GetBacktrace());
  122. return;
  123. }
  124. this->Target->AddUtility(objLib->GetName());
  125. this->ObjectLibraries.push_back(objLib);
  126. }
  127. else
  128. {
  129. cmOStringStream e;
  130. e << "Objects of target \"" << objLibName
  131. << "\" referenced but is not an OBJECT library.";
  132. this->GlobalGenerator->GetCMakeInstance()
  133. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  134. this->Target->GetBacktrace());
  135. return;
  136. }
  137. }
  138. else
  139. {
  140. cmOStringStream e;
  141. e << "Objects of target \"" << objLibName
  142. << "\" referenced but no such target exists.";
  143. this->GlobalGenerator->GetCMakeInstance()
  144. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  145. this->Target->GetBacktrace());
  146. return;
  147. }
  148. }
  149. }
  150. //----------------------------------------------------------------------------
  151. void cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs)
  152. {
  153. for(std::vector<cmTarget*>::const_iterator
  154. ti = this->ObjectLibraries.begin();
  155. ti != this->ObjectLibraries.end(); ++ti)
  156. {
  157. cmTarget* objLib = *ti;
  158. cmGeneratorTarget* ogt =
  159. this->GlobalGenerator->GetGeneratorTarget(objLib);
  160. for(std::vector<cmSourceFile*>::const_iterator
  161. si = ogt->ObjectSources.begin();
  162. si != ogt->ObjectSources.end(); ++si)
  163. {
  164. std::string obj = ogt->ObjectDirectory;
  165. obj += ogt->Objects[*si];
  166. objs.push_back(obj);
  167. }
  168. }
  169. }