cmGeneratorTarget.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 && cmSystemTools::LowerCase(sf->GetExtension()) != "txt")
  74. {
  75. badObjLib.push_back(sf);
  76. }
  77. }
  78. }
  79. if(!badObjLib.empty())
  80. {
  81. cmOStringStream e;
  82. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  83. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  84. i != badObjLib.end(); ++i)
  85. {
  86. e << " " << (*i)->GetLocation().GetName() << "\n";
  87. }
  88. e << "but may contain only headers and sources that compile.";
  89. this->GlobalGenerator->GetCMakeInstance()
  90. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  91. this->Target->GetBacktrace());
  92. }
  93. }
  94. //----------------------------------------------------------------------------
  95. void cmGeneratorTarget::LookupObjectLibraries()
  96. {
  97. std::vector<std::string> const& objLibs =
  98. this->Target->GetObjectLibraries();
  99. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  100. oli != objLibs.end(); ++oli)
  101. {
  102. std::string const& objLibName = *oli;
  103. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName.c_str()))
  104. {
  105. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  106. {
  107. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  108. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  109. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  110. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  111. {
  112. this->GlobalGenerator->GetCMakeInstance()
  113. ->IssueMessage(cmake::FATAL_ERROR,
  114. "Only executables and non-OBJECT libraries may "
  115. "reference target objects.",
  116. this->Target->GetBacktrace());
  117. return;
  118. }
  119. this->Target->AddUtility(objLib->GetName());
  120. this->ObjectLibraries.push_back(objLib);
  121. }
  122. else
  123. {
  124. cmOStringStream e;
  125. e << "Objects of target \"" << objLibName
  126. << "\" referenced but is not an OBJECT library.";
  127. this->GlobalGenerator->GetCMakeInstance()
  128. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  129. this->Target->GetBacktrace());
  130. return;
  131. }
  132. }
  133. else
  134. {
  135. cmOStringStream e;
  136. e << "Objects of target \"" << objLibName
  137. << "\" referenced but no such target exists.";
  138. this->GlobalGenerator->GetCMakeInstance()
  139. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  140. this->Target->GetBacktrace());
  141. return;
  142. }
  143. }
  144. }
  145. //----------------------------------------------------------------------------
  146. void cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs)
  147. {
  148. for(std::vector<cmTarget*>::const_iterator
  149. ti = this->ObjectLibraries.begin();
  150. ti != this->ObjectLibraries.end(); ++ti)
  151. {
  152. cmTarget* objLib = *ti;
  153. cmGeneratorTarget* ogt =
  154. this->GlobalGenerator->GetGeneratorTarget(objLib);
  155. for(std::vector<cmSourceFile*>::const_iterator
  156. si = ogt->ObjectSources.begin();
  157. si != ogt->ObjectSources.end(); ++si)
  158. {
  159. std::string obj = ogt->ObjectDirectory;
  160. obj += ogt->Objects[*si];
  161. objs.push_back(obj);
  162. }
  163. }
  164. }