cmTarget.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmTarget.h"
  14. #include "cmMakefile.h"
  15. #include <map>
  16. #include <set>
  17. void cmTarget::GenerateSourceFilesFromSourceLists( cmMakefile &mf)
  18. {
  19. // this is only done for non install targets
  20. if ((this->m_TargetType == cmTarget::INSTALL_FILES)
  21. || (this->m_TargetType == cmTarget::INSTALL_PROGRAMS))
  22. {
  23. return;
  24. }
  25. // for each src lists add the classes
  26. for (std::vector<std::string>::const_iterator s = m_SourceLists.begin();
  27. s != m_SourceLists.end(); ++s)
  28. {
  29. // replace any variables
  30. std::string temps = *s;
  31. mf.ExpandVariablesInString(temps);
  32. // look for a srclist
  33. if (mf.GetSources().find(temps) != mf.GetSources().end())
  34. {
  35. const std::vector<cmSourceFile*> &clsList =
  36. mf.GetSources().find(temps)->second;
  37. // if we ahave a limited build list, use it
  38. m_SourceFiles.insert(m_SourceFiles.end(),
  39. clsList.begin(),
  40. clsList.end());
  41. }
  42. // if one wasn't found then assume it is a single class
  43. else
  44. {
  45. cmSourceFile file;
  46. file.SetIsAnAbstractClass(false);
  47. file.SetName(temps.c_str(), mf.GetCurrentDirectory(),
  48. mf.GetSourceExtensions(),
  49. mf.GetHeaderExtensions());
  50. m_SourceFiles.push_back(mf.AddSource(file));
  51. }
  52. }
  53. // expand any link library variables whle we are at it
  54. LinkLibraries::iterator p = m_LinkLibraries.begin();
  55. for (;p != m_LinkLibraries.end(); ++p)
  56. {
  57. mf.ExpandVariablesInString(p->first);
  58. }
  59. }
  60. void cmTarget::MergeLinkLibraries( cmMakefile& mf,
  61. const char *selfname,
  62. const LinkLibraries& libs )
  63. {
  64. for( LinkLibraries::const_iterator i = libs.begin();
  65. i != libs.end(); ++i )
  66. {
  67. if(m_PrevLinkedLibraries.insert(i->first).second)
  68. {
  69. // We call this so that the dependencies get written to the cache
  70. this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
  71. }
  72. }
  73. }
  74. void cmTarget::AddLinkDirectory(const char* d)
  75. {
  76. // Make sure we don't add unnecessary search directories.
  77. if( std::find( m_LinkDirectories.begin(), m_LinkDirectories.end(), d )
  78. == m_LinkDirectories.end() )
  79. m_LinkDirectories.push_back( d );
  80. }
  81. void cmTarget::AddLinkLibrary(const std::string& lib,
  82. LinkLibraryType llt)
  83. {
  84. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  85. }
  86. void cmTarget::AddLinkLibrary(cmMakefile& mf,
  87. const char *target, const char* lib,
  88. LinkLibraryType llt)
  89. {
  90. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  91. if(llt != cmTarget::GENERAL)
  92. {
  93. std::string linkTypeName = lib;
  94. linkTypeName += "_LINK_TYPE";
  95. switch(llt)
  96. {
  97. case cmTarget::DEBUG:
  98. mf.AddCacheDefinition(linkTypeName.c_str(),
  99. "debug", "Library is used for debug links only",
  100. cmCacheManager::STATIC);
  101. break;
  102. case cmTarget::OPTIMIZED:
  103. mf.AddCacheDefinition(linkTypeName.c_str(),
  104. "optimized", "Library is used for debug links only",
  105. cmCacheManager::STATIC);
  106. break;
  107. }
  108. }
  109. // Add the explicit dependency information for this target. This is
  110. // simply a set of libraries separated by ";". There should always
  111. // be a trailing ";". These library names are not canonical, in that
  112. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  113. std::string targetEntry = target;
  114. targetEntry += "_LIB_DEPENDS";
  115. std::string dependencies;
  116. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  117. if( old_val )
  118. {
  119. dependencies += old_val;
  120. }
  121. if( dependencies.find( lib ) == std::string::npos )
  122. {
  123. dependencies += lib;
  124. dependencies += ";";
  125. }
  126. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  127. "Dependencies for the target", cmCacheManager::STATIC );
  128. }
  129. bool cmTarget::HasCxx() const
  130. {
  131. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  132. i != m_SourceFiles.end(); ++i)
  133. {
  134. if((*i)->GetSourceExtension() != "c")
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. void
  142. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  143. {
  144. // There are two key parts of the dependency analysis: (1)
  145. // determining the libraries in the link line, and (2) constructing
  146. // the dependency graph for those libraries.
  147. //
  148. // The latter is done using the cache entries that record the
  149. // dependencies of each library.
  150. //
  151. // The former is a more thorny issue, since it is not clear how to
  152. // determine if two libraries listed on the link line refer to the a
  153. // single library or not. For example, consider the link "libraries"
  154. // /usr/lib/libtiff.so -ltiff
  155. // Is this one library or two? The solution implemented here is the
  156. // simplest (and probably the only practical) one: two libraries are
  157. // the same if their "link strings" are identical. Thus, the two
  158. // libraries above are considered distinct. This also means that for
  159. // dependency analysis to be effective, the CMake user must specify
  160. // libraries build by his project without using any linker flags or
  161. // file extensions. That is,
  162. // LINK_LIBRARIES( One Two )
  163. // instead of
  164. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  165. // The former is probably what most users would do, but it never
  166. // hurts to document the assumptions. :-) Therefore, in the analysis
  167. // code, the "canonical name" of a library is simply its name as
  168. // given to a LINK_LIBRARIES command.
  169. //
  170. // Also, we will leave the original link line intact; we will just add any
  171. // dependencies that were missing.
  172. typedef std::vector< std::string > LinkLine;
  173. // The dependency map.
  174. DependencyMap dep_map;
  175. // Keeps track of which dependencies have already been emitted for a given
  176. // target. This could be via this function, or because they were already
  177. // satisfied on the original link line.
  178. DependencyMap satisfied;
  179. // If LIBRARY_OUTPUT_PATH is not set, then we must add search paths
  180. // for all the new libraries added by the dependency analysis.
  181. const char* libOutPath = mf.GetDefinition("LIBRARY_OUTPUT_PATH");
  182. bool addLibDirs = (libOutPath==0 || strcmp(libOutPath,"")==0);
  183. // 1. Determine the dependencies already satisfied by the original link
  184. // line.
  185. for(LinkLibraries::iterator lib = m_LinkLibraries.begin();
  186. lib != m_LinkLibraries.end(); ++lib)
  187. {
  188. for( LinkLibraries::iterator lib2 = lib;
  189. lib2 != m_LinkLibraries.end(); ++lib2)
  190. {
  191. satisfied[ lib->first ].insert( lib2->first );
  192. }
  193. }
  194. // 2. Build the explicit dependency map
  195. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  196. lib != m_LinkLibraries.rend(); ++lib)
  197. {
  198. this->GatherDependencies( mf, lib->first, dep_map );
  199. }
  200. // 3. Create the new link line by simply emitting any dependencies that are
  201. // missing. Start from the back and keep adding.
  202. LinkLibraries newLinkLibraries = m_LinkLibraries;
  203. std::set<cmStdString> done, visited;
  204. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  205. lib != m_LinkLibraries.rend(); ++lib)
  206. {
  207. // skip zero size library entries, this may happen
  208. // if a variable expands to nothing.
  209. if (lib->first.size() == 0) continue;
  210. std::vector<std::string> link_line;
  211. Emit( lib->first, dep_map, done, visited, link_line );
  212. if( link_line.size() == 0 )
  213. {
  214. // everything for this library is already on the link line, but since
  215. // we are not going to touch the user's link line, we will output the
  216. // library anyway.
  217. newLinkLibraries.push_back( *lib );
  218. }
  219. else
  220. {
  221. for( std::vector<std::string>::reverse_iterator k = link_line.rbegin();
  222. k != link_line.rend(); ++k )
  223. {
  224. if( satisfied[lib->first].insert( *k ).second )
  225. {
  226. if( addLibDirs )
  227. {
  228. const char* libpath = mf.GetDefinition( k->c_str() );
  229. if( libpath )
  230. {
  231. // Don't add a link directory that is already present.
  232. if(std::find(m_LinkDirectories.begin(),
  233. m_LinkDirectories.end(), libpath) == m_LinkDirectories.end())
  234. {
  235. m_LinkDirectories.push_back(libpath);
  236. }
  237. }
  238. }
  239. std::string linkType = *k;
  240. linkType += "_LINK_TYPE";
  241. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  242. const char* linkTypeString = mf.GetDefinition( linkType.c_str() );
  243. if(linkTypeString)
  244. {
  245. if(strcmp(linkTypeString, "debug") == 0)
  246. {
  247. llt = cmTarget::DEBUG;
  248. }
  249. if(strcmp(linkTypeString, "optimized") == 0)
  250. {
  251. llt = cmTarget::OPTIMIZED;
  252. }
  253. }
  254. newLinkLibraries.push_back( std::make_pair(*k,llt) );
  255. }
  256. }
  257. }
  258. }
  259. m_LinkLibraries = newLinkLibraries;
  260. }
  261. void cmTarget::Emit( const std::string& lib,
  262. const DependencyMap& dep_map,
  263. std::set<cmStdString>& emitted,
  264. std::set<cmStdString>& visited,
  265. std::vector<std::string>& link_line ) const
  266. {
  267. // It's already been emitted
  268. if( emitted.find(lib) != emitted.end() )
  269. {
  270. return;
  271. }
  272. // If this library hasn't been visited before, then emit all its
  273. // dependencies before emitting the library itself. If it has been
  274. // visited before, then there is a dependency cycle. Just emit the
  275. // library itself, and let the recursion that got us here deal with
  276. // emitting the dependencies for the library.
  277. if( visited.insert(lib).second )
  278. {
  279. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  280. {
  281. const std::set<cmStdString>& dep_on = dep_map.find( lib )->second;
  282. std::set<cmStdString>::const_iterator i;
  283. for( i = dep_on.begin(); i != dep_on.end(); ++i )
  284. {
  285. Emit( *i, dep_map, emitted, visited, link_line );
  286. }
  287. }
  288. }
  289. link_line.push_back( lib );
  290. emitted.insert(lib);
  291. }
  292. void cmTarget::GatherDependencies( const cmMakefile& mf,
  293. const std::string& lib,
  294. DependencyMap& dep_map )
  295. {
  296. // If the library is already in the dependency map, then it has
  297. // already been fully processed.
  298. if( dep_map.find(lib) != dep_map.end() )
  299. return;
  300. const char* deps = mf.GetDefinition( (lib+"_LIB_DEPENDS").c_str() );
  301. if( deps && strcmp(deps,"") != 0 )
  302. {
  303. // Make sure this library is in the map, even if it has an empty
  304. // set of dependencies. This distinguishes the case of explicitly
  305. // no dependencies with that of unspecified dependencies.
  306. dep_map[lib];
  307. // Parse the dependency information, which is simply a set of
  308. // libraries separated by ";". There is always a trailing ";".
  309. std::string depline = deps;
  310. std::string::size_type start = 0;
  311. std::string::size_type end;
  312. end = depline.find( ";", start );
  313. while( end != std::string::npos )
  314. {
  315. std::string l = depline.substr( start, end-start );
  316. if( l.size() != 0 )
  317. {
  318. dep_map[ lib ].insert( l );
  319. GatherDependencies( mf, l, dep_map );
  320. }
  321. start = end+1; // skip the ;
  322. end = depline.find( ";", start );
  323. }
  324. dep_map[lib].erase(lib); // cannot depend on itself
  325. }
  326. }
  327. // return true if lib1 depends on lib2
  328. bool cmTarget::DependsOn( const std::string& lib1, const std::string& lib2,
  329. const DependencyMap& dep_map,
  330. std::set<cmStdString>& visited ) const
  331. {
  332. if( !visited.insert( lib1 ).second )
  333. {
  334. return false; // already visited here
  335. }
  336. if( lib1 == lib2 )
  337. {
  338. return false;
  339. }
  340. if( dep_map.find(lib1) == dep_map.end() )
  341. {
  342. return false; // lib1 doesn't have any dependencies
  343. }
  344. const std::set<cmStdString>& dep_set = dep_map.find(lib1)->second;
  345. if( dep_set.end() != dep_set.find( lib2 ) )
  346. {
  347. return true; // lib1 doesn't directly depend on lib2.
  348. }
  349. // Do a recursive check: does lib1 depend on x which depends on lib2?
  350. for( std::set<cmStdString>::const_iterator itr = dep_set.begin();
  351. itr != dep_set.end(); ++itr )
  352. {
  353. if( this->DependsOn( *itr, lib2, dep_map, visited ) )
  354. {
  355. return true;
  356. }
  357. }
  358. return false;
  359. }