cmGeneratorTarget.cxx 5.8 KB

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