cmTarget.cxx 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 "cmake.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmGlobalGenerator.h"
  19. #include <map>
  20. #include <set>
  21. #include <queue>
  22. #include <stdlib.h> // required for atof
  23. const char* cmTarget::TargetTypeNames[] = {
  24. "EXECUTABLE", "STATIC_LIBRARY",
  25. "SHARED_LIBRARY", "MODULE_LIBRARY", "UTILITY", "GLOBAL_TARGET",
  26. "INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY"
  27. };
  28. //----------------------------------------------------------------------------
  29. cmTarget::cmTarget()
  30. {
  31. this->Makefile = 0;
  32. this->LinkLibrariesAnalyzed = false;
  33. this->LinkDirectoriesComputed = false;
  34. this->HaveInstallRule = false;
  35. this->DLLPlatform = false;
  36. this->IsImportedTarget = false;
  37. }
  38. // define properties
  39. void cmTarget::DefineProperties(cmake *cm)
  40. {
  41. cm->DefineProperty
  42. ("BUILD_WITH_INSTALL_RPATH", cmProperty::TARGET,
  43. "Should build tree targets have install tree rpaths.",
  44. "BUILD_WITH_INSTALL_RPATH is a boolean specifying whether to link "
  45. "the target in the build tree with the INSTALL_RPATH. This takes "
  46. "precedence over SKIP_BUILD_RPATH and avoids the need for relinking "
  47. "before installation.");
  48. cm->DefineProperty
  49. ("CLEAN_DIRECT_OUTPUT", cmProperty::TARGET,
  50. "Do not delete other varients of this target.",
  51. "When a library is built CMake by default generates code to remove "
  52. "any existing library using all possible names. This is needed "
  53. "to support libraries that switch between STATIC and SHARED by "
  54. "a user option. However when using OUTPUT_NAME to build a static "
  55. "and shared library of the same name using different logical target "
  56. "names the two targets will remove each other's files. This can be "
  57. "prevented by setting the CLEAN_DIRECT_OUTPUT property to 1.");
  58. cm->DefineProperty
  59. ("COMPILE_FLAGS", cmProperty::TARGET,
  60. "Additional flags to yse when compiling this target's sources.",
  61. "The COMPILE_FLAGS property sets additional compiler flags used "
  62. "to build sources within the target. It may also be used to pass "
  63. "additional preprocessor definitions.");
  64. cm->DefineProperty
  65. ("DEFINE_SYMBOL", cmProperty::TARGET,
  66. "Define a symbol when compiling this target's sources.",
  67. "DEFINE_SYMBOL sets the name of the preprocessor symbol defined when "
  68. "compiling sources in a shared library. "
  69. "If not set here then it is set to target_EXPORTS by default "
  70. "(with some substitutions if the target is not a valid C "
  71. "identifier). This is useful for headers to know whether they are "
  72. "being included from inside their library our outside to properly "
  73. "setup dllexport/dllimport decorations. ");
  74. cm->DefineProperty
  75. ("DEBUG_POSTFIX", cmProperty::TARGET,
  76. "A postfix that will be applied to this target when build debug.",
  77. "A property on a target that sepcifies a postfix to add to the "
  78. "target name when built in debug mode. For example foo.dll "
  79. "versus fooD.dll");
  80. cm->DefineProperty
  81. ("EchoString", cmProperty::TARGET,
  82. "A message to be displayed when the target is built.",
  83. "A message to display on some generaters (such as makefiles) when "
  84. "the target is built.");
  85. cm->DefineProperty
  86. ("HAS_CXX", cmProperty::TARGET,
  87. "Force a target to use the CXX linker.",
  88. "Setting HAS_CXX on a target will force the target to use the "
  89. "C++ linker (and C++ runtime libraries) for linking even if the "
  90. "target has no C++ code in it.");
  91. cm->DefineProperty
  92. ("IMPORT_PREFIX", cmProperty::TARGET,
  93. "What comes before the import library name.",
  94. "Similar to the target property PREFIX, but used for import libraries "
  95. "(typically corresponding to a DLL) instead of regular libraries. "
  96. "A target property that can be set to override the prefix "
  97. "(such as \"lib\") on an import library name.");
  98. cm->DefineProperty
  99. ("IMPORT_SUFFIX", cmProperty::TARGET,
  100. "What comes after the import library name.",
  101. "Similar to the target property SUFFIX, but used for import libraries "
  102. "(typically corresponding to a DLL) instead of regular libraries. "
  103. "A target property that can be set to override the suffix "
  104. "(such as \".lib\") on an import library name.");
  105. cm->DefineProperty
  106. ("EXCLUDE_FROM_ALL", cmProperty::TARGET,
  107. "Exclude the target from the all target.",
  108. "A property on a target that indicates if the target is excluded "
  109. "from the default build target. If it is not, then with a Makefile "
  110. "for example typing make will couse this target to be built as well. "
  111. "The same concept applies to the default build of other generators.",
  112. true);
  113. cm->DefineProperty
  114. ("INSTALL_NAME_DIR", cmProperty::TARGET,
  115. "Mac OSX directory name for installed targets.",
  116. "INSTALL_NAME_DIR is a string specifying the "
  117. "directory portion of the \"install_name\" field of shared libraries "
  118. "on Mac OSX to use in the installed targets. ");
  119. cm->DefineProperty
  120. ("INSTALL_RPATH", cmProperty::TARGET,
  121. "The rpath to use for installed targets.",
  122. "A semicolon-separated list specifying the rpath "
  123. "to use in installed targets (for platforms that support it).");
  124. cm->DefineProperty
  125. ("INSTALL_RPATH_USE_LINK_PATH", cmProperty::TARGET,
  126. "Add paths to linker search and installed rpath.",
  127. "INSTALL_RPATH_USE_LINK_PATH is a boolean that if set to true will "
  128. "append directories in the linker search path and outside the "
  129. "project to the INSTALL_RPATH. ");
  130. cm->DefineProperty
  131. ("LINK_FLAGS", cmProperty::TARGET,
  132. "Additional flags to use when linking this target.",
  133. "The LINK_FLAGS property can be used to add extra flags to the "
  134. "link step of a target. LINK_FLAGS_<CONFIG> will add to the "
  135. "configuration <CONFIG>, "
  136. "for example, DEBUG, RELEASE, MINSIZEREL, RELWITHDEBINFO. ");
  137. cm->DefineProperty
  138. ("LINKER_LANGUAGE", cmProperty::TARGET,
  139. "What tool to use for linking, based on language.",
  140. "The LINKER_LANGUAGE property is used to change the tool "
  141. "used to link an executable or shared library. The default is "
  142. "set the language to match the files in the library. CXX and C "
  143. "are common values for this property.");
  144. cm->DefineProperty
  145. ("LOCATION", cmProperty::TARGET,
  146. "Where a target will be written on disk.",
  147. "A read only property on a target that indicates where that target "
  148. "will be written. For libraries and execuatables this will be where "
  149. "the file is written on disk. This property is computed based on a "
  150. "number of other settings.");
  151. cm->DefineProperty
  152. ("OUTPUT_NAME", cmProperty::TARGET,
  153. "Sets the real name of a target when it is built.",
  154. "Sets the real name of a target when it is built and "
  155. "can be used to help create two targets of the same name even though "
  156. "CMake requires unique logical target names. There is also a "
  157. "<CONFIG>_OUTPUT_NAME that can set the output name on a "
  158. "per-configuration basis.");
  159. cm->DefineProperty
  160. ("PRE_INSTALL_SCRIPT", cmProperty::TARGET,
  161. "Deprecated install support.",
  162. "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
  163. "old way to specify CMake scripts to run before and after "
  164. "installing a target. They are used only when the old "
  165. "INSTALL_TARGETS command is used to install the target. Use the "
  166. "INSTALL command instead.");
  167. cm->DefineProperty
  168. ("PREFIX", cmProperty::TARGET,
  169. "What comes before the library name.",
  170. "A target property that can be set to override the prefix "
  171. "(such as \"lib\") on a library name.");
  172. cm->DefineProperty
  173. ("POST_INSTALL_SCRIPT", cmProperty::TARGET,
  174. "Deprecated install support.",
  175. "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
  176. "old way to specify CMake scripts to run before and after "
  177. "installing a target. They are used only when the old "
  178. "INSTALL_TARGETS command is used to install the target. Use the "
  179. "INSTALL command instead.");
  180. cm->DefineProperty
  181. ("SKIP_BUILD_RPATH", cmProperty::TARGET,
  182. "Should rpaths be used for the build tree.",
  183. "SKIP_BUILD_RPATH is a boolean specifying whether to skip automatic "
  184. "generation of an rpath allowing the target to run from the "
  185. "build tree. ");
  186. cm->DefineProperty
  187. ("SOVERSION", cmProperty::TARGET,
  188. "What version number is this target.",
  189. "For shared libraries VERSION and SOVERSION can be used to specify "
  190. "the build version and api version respectively. When building or "
  191. "installing appropriate symlinks are created if the platform "
  192. "supports symlinks and the linker supports so-names. "
  193. "If only one of both is specified the missing is assumed to have "
  194. "the same version number. "
  195. "For shared libraries and executables on Windows the VERSION "
  196. "attribute is parsed to extract a \"major.minor\" version number. "
  197. "These numbers are used as the image version of the binary. ");
  198. cm->DefineProperty
  199. ("STATIC_LIBRARY_FLAGS", cmProperty::TARGET,
  200. "Extra flags to use when linking static libraries.",
  201. "Extra flags to use when linking a static library.");
  202. cm->DefineProperty
  203. ("SUFFIX", cmProperty::TARGET,
  204. "What comes after the library name.",
  205. "A target property that can be set to override the suffix "
  206. "(such as \".so\") on a library name.");
  207. cm->DefineProperty
  208. ("VERSION", cmProperty::TARGET,
  209. "What version number is this target.",
  210. "For shared libraries VERSION and SOVERSION can be used to specify "
  211. "the build version and api version respectively. When building or "
  212. "installing appropriate symlinks are created if the platform "
  213. "supports symlinks and the linker supports so-names. "
  214. "If only one of both is specified the missing is assumed to have "
  215. "the same version number. "
  216. "For executables VERSION can be used to specify the build version. "
  217. "When building or installing appropriate symlinks are created if "
  218. "the platform supports symlinks. "
  219. "For shared libraries and executables on Windows the VERSION "
  220. "attribute is parsed to extract a \"major.minor\" version number. "
  221. "These numbers are used as the image version of the binary. ");
  222. cm->DefineProperty
  223. ("WIN32_EXECUTABLE", cmProperty::TARGET,
  224. "Used to specify Windows executable with a WinMain entry point.",
  225. "This can be set to indicate that a target is a Windows executable "
  226. "in contrast to a console application for example. This changes "
  227. "how the executable will be linked.");
  228. cm->DefineProperty
  229. ("ENABLE_EXPORTS", cmProperty::TARGET,
  230. "Specify whether an executable exports symbols for loadable modules.",
  231. "Normally an executable does not export any symbols because it is "
  232. "the final program. It is possible for an executable to export "
  233. "symbols to be used by loadable modules. When this property is "
  234. "set to true CMake will allow other targets to \"link\" to the "
  235. "executable with the TARGET_LINK_LIBRARIES command. "
  236. "On all platforms a target-level dependency on the executable is "
  237. "created for targets that link to it. "
  238. "For non-DLL platforms the link rule is simply ignored since "
  239. "the dynamic loader will automatically bind symbols when the "
  240. "module is loaded. "
  241. "For DLL platforms an import library will be created for the "
  242. "exported symbols and then used for linking. "
  243. "All Windows-based systems including Cygwin are DLL platforms.");
  244. cm->DefineProperty
  245. ("GENERATOR_FILE_NAME", cmProperty::TARGET,
  246. "Generator's file for this target.",
  247. "An internal property used by some generators to record the name of "
  248. "project or dsp file associated with this target.");
  249. #if 0
  250. cm->DefineProperty
  251. ("OBJECT_FILES", cmProperty::TARGET,
  252. "Used to get the resulting list of object files that make up a "
  253. "target.",
  254. "This can be used to put object files from one library "
  255. "into another library. It is a read only property. It "
  256. "converts the source list for the target into a list of full "
  257. "paths to object names that will be produced by the target.");
  258. #endif
  259. #define CM_TARGET_FILE_TYPES_DOC \
  260. "There are three kinds of target files that may be built: " \
  261. "archive, library, and runtime. " \
  262. "Executables are always treated as runtime targets. " \
  263. "Static libraries are always treated as archive targets. " \
  264. "Module libraries are always treated as library targets. " \
  265. "For non-DLL platforms shared libraries are treated as library " \
  266. "targets. " \
  267. "For DLL platforms the DLL part of a shared library is treated as " \
  268. "a runtime target and the corresponding import library is treated as " \
  269. "an archive target. " \
  270. "All Windows-based systems including Cygwin are DLL platforms."
  271. cm->DefineProperty
  272. ("ARCHIVE_OUTPUT_DIRECTORY", cmProperty::TARGET,
  273. "Output directory in which to build ARCHIVE target files.",
  274. "This property specifies the directory into which archive target files "
  275. "should be built. "
  276. CM_TARGET_FILE_TYPES_DOC " "
  277. "This property is initialized by the value of the variable "
  278. "CMAKE_ARCHIVE_OUTPUT_DIRECTORY if it is set when a target is created.");
  279. cm->DefineProperty
  280. ("LIBRARY_OUTPUT_DIRECTORY", cmProperty::TARGET,
  281. "Output directory in which to build LIBRARY target files.",
  282. "This property specifies the directory into which library target files "
  283. "should be built. "
  284. CM_TARGET_FILE_TYPES_DOC " "
  285. "This property is initialized by the value of the variable "
  286. "CMAKE_LIBRARY_OUTPUT_DIRECTORY if it is set when a target is created.");
  287. cm->DefineProperty
  288. ("RUNTIME_OUTPUT_DIRECTORY", cmProperty::TARGET,
  289. "Output directory in which to build RUNTIME target files.",
  290. "This property specifies the directory into which runtime target files "
  291. "should be built. "
  292. CM_TARGET_FILE_TYPES_DOC " "
  293. "This property is initialized by the value of the variable "
  294. "CMAKE_RUNTIME_OUTPUT_DIRECTORY if it is set when a target is created.");
  295. // define some properties without documentation
  296. cm->DefineProperty("DEBUG_OUTPUT_NAME", cmProperty::TARGET,0,0);
  297. cm->DefineProperty("RELEASE_OUTPUT_NAME", cmProperty::TARGET,0,0);
  298. cm->DefineProperty("LINK_FLAGS_DEBUG", cmProperty::TARGET,0,0);
  299. }
  300. void cmTarget::SetType(TargetType type, const char* name)
  301. {
  302. this->Name = name;
  303. if(type == cmTarget::INSTALL_FILES ||
  304. type == cmTarget::INSTALL_PROGRAMS ||
  305. type == cmTarget::INSTALL_DIRECTORY)
  306. {
  307. abort();
  308. }
  309. // only add dependency information for library targets
  310. this->TargetTypeValue = type;
  311. if(this->TargetTypeValue >= STATIC_LIBRARY
  312. && this->TargetTypeValue <= MODULE_LIBRARY)
  313. {
  314. this->RecordDependencies = true;
  315. }
  316. else
  317. {
  318. this->RecordDependencies = false;
  319. }
  320. }
  321. //----------------------------------------------------------------------------
  322. void cmTarget::SetMakefile(cmMakefile* mf)
  323. {
  324. // Set our makefile.
  325. this->Makefile = mf;
  326. // set the cmake instance of the properties
  327. this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
  328. // Check whether this is a DLL platform.
  329. this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
  330. this->Makefile->IsOn("CYGWIN") ||
  331. this->Makefile->IsOn("MINGW"));
  332. // Setup default property values.
  333. this->SetPropertyDefault("INSTALL_NAME_DIR", "");
  334. this->SetPropertyDefault("INSTALL_RPATH", "");
  335. this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
  336. this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
  337. this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
  338. this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
  339. this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
  340. this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
  341. // Collect the set of configuration types.
  342. std::vector<std::string> configNames;
  343. if(const char* configurationTypes =
  344. mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  345. {
  346. cmSystemTools::ExpandListArgument(configurationTypes, configNames);
  347. }
  348. else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
  349. {
  350. if(*buildType)
  351. {
  352. configNames.push_back(buildType);
  353. }
  354. }
  355. // Setup per-configuration property default values.
  356. for(std::vector<std::string>::iterator ci = configNames.begin();
  357. ci != configNames.end(); ++ci)
  358. {
  359. // Initialize per-configuration name postfix property from the
  360. // variable only for non-executable targets. This preserves
  361. // compatibility with previous CMake versions in which executables
  362. // did not support this variable. Projects may still specify the
  363. // property directly. TODO: Make this depend on backwards
  364. // compatibility setting.
  365. if(this->TargetTypeValue != cmTarget::EXECUTABLE)
  366. {
  367. std::string property = cmSystemTools::UpperCase(*ci);
  368. property += "_POSTFIX";
  369. this->SetPropertyDefault(property.c_str(), 0);
  370. }
  371. }
  372. }
  373. //----------------------------------------------------------------------------
  374. class cmTargetTraceDependencies
  375. {
  376. public:
  377. cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile);
  378. void Trace();
  379. private:
  380. cmTarget* Target;
  381. cmMakefile* Makefile;
  382. cmGlobalGenerator* GlobalGenerator;
  383. std::queue<cmStdString> DependencyQueue;
  384. std::set<cmStdString> DependenciesQueued;
  385. std::set<cmSourceFile*> TargetSources;
  386. void QueueOnce(std::string const& name);
  387. void QueueOnce(std::vector<std::string> const& names);
  388. void QueueDependencies(cmSourceFile* sf);
  389. bool IsUtility(std::string const& dep);
  390. void CheckCustomCommand(cmCustomCommand const& cc);
  391. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  392. };
  393. //----------------------------------------------------------------------------
  394. cmTargetTraceDependencies
  395. ::cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile):
  396. Target(target)
  397. {
  398. // Convenience.
  399. this->Makefile = this->Target->GetMakefile();
  400. this->GlobalGenerator =
  401. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  402. // Queue all the source files already specified for the target.
  403. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  404. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  405. si != sources.end(); ++si)
  406. {
  407. // Queue the source file itself in case it is generated.
  408. this->QueueOnce((*si)->GetFullPath());
  409. // Queue the dependencies of the source file in case they are
  410. // generated.
  411. this->QueueDependencies(*si);
  412. // Track the sources already known to the target.
  413. this->TargetSources.insert(*si);
  414. }
  415. // Queue the VS project file to check dependencies on the rule to
  416. // generate it.
  417. if(vsProjectFile)
  418. {
  419. this->QueueOnce(vsProjectFile);
  420. }
  421. // Queue pre-build, pre-link, and post-build rule dependencies.
  422. this->CheckCustomCommands(this->Target->GetPreBuildCommands());
  423. this->CheckCustomCommands(this->Target->GetPreLinkCommands());
  424. this->CheckCustomCommands(this->Target->GetPostBuildCommands());
  425. }
  426. //----------------------------------------------------------------------------
  427. void cmTargetTraceDependencies::Trace()
  428. {
  429. // Process one dependency at a time until the queue is empty.
  430. while(!this->DependencyQueue.empty())
  431. {
  432. // Get the next dependency in from queue.
  433. std::string dep = this->DependencyQueue.front();
  434. this->DependencyQueue.pop();
  435. // Check if we know how to generate this dependency.
  436. if(cmSourceFile* sf =
  437. this->Makefile->GetSourceFileWithOutput(dep.c_str()))
  438. {
  439. // Queue dependencies needed to generate this file.
  440. this->QueueDependencies(sf);
  441. // Make sure this file is in the target.
  442. if(this->TargetSources.insert(sf).second)
  443. {
  444. this->Target->AddSourceFile(sf);
  445. }
  446. }
  447. }
  448. }
  449. //----------------------------------------------------------------------------
  450. void cmTargetTraceDependencies::QueueOnce(std::string const& name)
  451. {
  452. if(this->DependenciesQueued.insert(name).second)
  453. {
  454. this->DependencyQueue.push(name);
  455. }
  456. }
  457. //----------------------------------------------------------------------------
  458. void
  459. cmTargetTraceDependencies::QueueOnce(std::vector<std::string> const& names)
  460. {
  461. for(std::vector<std::string>::const_iterator i = names.begin();
  462. i != names.end(); ++i)
  463. {
  464. this->QueueOnce(*i);
  465. }
  466. }
  467. //----------------------------------------------------------------------------
  468. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  469. {
  470. // Dependencies on targets (utilities) are supposed to be named by
  471. // just the target name. However for compatibility we support
  472. // naming the output file generated by the target (assuming there is
  473. // no output-name property which old code would not have set). In
  474. // that case the target name will be the file basename of the
  475. // dependency.
  476. std::string util = cmSystemTools::GetFilenameName(dep);
  477. if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
  478. {
  479. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  480. }
  481. // Check for a non-imported target with this name.
  482. if(cmTarget* t =
  483. this->GlobalGenerator->FindTarget(0, util.c_str(), false))
  484. {
  485. // If we find the target and the dep was given as a full path,
  486. // then make sure it was not a full path to something else, and
  487. // the fact that the name matched a target was just a coincidence.
  488. if(cmSystemTools::FileIsFullPath(dep.c_str()))
  489. {
  490. // This is really only for compatibility so we do not need to
  491. // worry about configuration names and output names.
  492. std::string tLocation = t->GetLocation(0);
  493. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  494. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  495. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  496. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  497. if(depLocation == tLocation)
  498. {
  499. this->Target->AddUtility(util.c_str());
  500. return true;
  501. }
  502. }
  503. else
  504. {
  505. // The original name of the dependency was not a full path. It
  506. // must name a target, so add the target-level dependency.
  507. this->Target->AddUtility(util.c_str());
  508. return true;
  509. }
  510. }
  511. // The dependency does not name a target built in this project.
  512. return false;
  513. }
  514. //----------------------------------------------------------------------------
  515. void cmTargetTraceDependencies::QueueDependencies(cmSourceFile* sf)
  516. {
  517. // Queue dependency added explicitly by the user.
  518. if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
  519. {
  520. std::vector<std::string> objDeps;
  521. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  522. this->QueueOnce(objDeps);
  523. }
  524. // Queue dependencies added programatically by commands.
  525. this->QueueOnce(sf->GetDepends());
  526. // Queue custom command dependencies.
  527. if(cmCustomCommand const* cc = sf->GetCustomCommand())
  528. {
  529. this->CheckCustomCommand(*cc);
  530. }
  531. }
  532. //----------------------------------------------------------------------------
  533. void
  534. cmTargetTraceDependencies
  535. ::CheckCustomCommand(cmCustomCommand const& cc)
  536. {
  537. // Transform command names that reference targets built in this
  538. // project to corresponding target-level dependencies.
  539. for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
  540. cit != cc.GetCommandLines().end(); ++cit)
  541. {
  542. std::string const& command = *cit->begin();
  543. // Look for a non-imported target with this name.
  544. if(cmTarget* t =
  545. this->GlobalGenerator->FindTarget(0, command.c_str(), false))
  546. {
  547. if(t->GetType() == cmTarget::EXECUTABLE)
  548. {
  549. // The command refers to an executable target built in
  550. // this project. Add the target-level dependency to make
  551. // sure the executable is up to date before this custom
  552. // command possibly runs.
  553. this->Target->AddUtility(command.c_str());
  554. }
  555. }
  556. }
  557. // Queue the custom command dependencies.
  558. std::vector<std::string> const& depends = cc.GetDepends();
  559. for(std::vector<std::string>::const_iterator di = depends.begin();
  560. di != depends.end(); ++di)
  561. {
  562. std::string const& dep = *di;
  563. if(!this->IsUtility(dep))
  564. {
  565. // The dependency does not name a target and may be a file we
  566. // know how to generate. Queue it.
  567. this->QueueOnce(dep);
  568. }
  569. }
  570. }
  571. //----------------------------------------------------------------------------
  572. void
  573. cmTargetTraceDependencies
  574. ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
  575. {
  576. for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
  577. cli != commands.end(); ++cli)
  578. {
  579. this->CheckCustomCommand(*cli);
  580. }
  581. }
  582. //----------------------------------------------------------------------------
  583. void cmTarget::TraceDependencies(const char* vsProjectFile)
  584. {
  585. // Use a helper object to trace the dependencies.
  586. cmTargetTraceDependencies tracer(this, vsProjectFile);
  587. tracer.Trace();
  588. }
  589. //----------------------------------------------------------------------------
  590. void cmTarget::AddSources(std::vector<std::string> const& srcs)
  591. {
  592. for(std::vector<std::string>::const_iterator i = srcs.begin();
  593. i != srcs.end(); ++i)
  594. {
  595. this->AddSource(i->c_str());
  596. }
  597. }
  598. cmSourceFile* cmTarget::AddSource(const char* s)
  599. {
  600. std::string src = s;
  601. // For backwards compatibility replace varibles in source names.
  602. // This should eventually be removed.
  603. this->Makefile->ExpandVariablesInString(src);
  604. cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
  605. this->AddSourceFile(sf);
  606. return sf;
  607. }
  608. void cmTarget::MergeLinkLibraries( cmMakefile& mf,
  609. const char *selfname,
  610. const LinkLibraryVectorType& libs )
  611. {
  612. // Only add on libraries we haven't added on before.
  613. // Assumption: the global link libraries could only grow, never shrink
  614. LinkLibraryVectorType::const_iterator i = libs.begin();
  615. i += this->PrevLinkedLibraries.size();
  616. for( ; i != libs.end(); ++i )
  617. {
  618. // We call this so that the dependencies get written to the cache
  619. this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
  620. }
  621. this->PrevLinkedLibraries = libs;
  622. }
  623. //----------------------------------------------------------------------------
  624. void cmTarget::AddLinkDirectory(const char* d)
  625. {
  626. // Make sure we don't add unnecessary search directories.
  627. if(std::find(this->ExplicitLinkDirectories.begin(),
  628. this->ExplicitLinkDirectories.end(), d)
  629. == this->ExplicitLinkDirectories.end() )
  630. {
  631. this->ExplicitLinkDirectories.push_back( d );
  632. this->LinkDirectoriesComputed = false;
  633. }
  634. }
  635. //----------------------------------------------------------------------------
  636. const std::vector<std::string>& cmTarget::GetLinkDirectories()
  637. {
  638. // Make sure all library dependencies have been analyzed.
  639. if(!this->LinkLibrariesAnalyzed && !this->LinkLibraries.empty())
  640. {
  641. cmSystemTools::Error(
  642. "cmTarget::GetLinkDirectories called before "
  643. "cmTarget::AnalyzeLibDependencies on target ",
  644. this->Name.c_str());
  645. }
  646. // Make sure the complete set of link directories has been computed.
  647. if(!this->LinkDirectoriesComputed)
  648. {
  649. // Check whether we should use an import library for linking a target.
  650. bool implib =
  651. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX") != 0;
  652. // Compute the full set of link directories including the
  653. // locations of targets that have been linked in. Start with the
  654. // link directories given explicitly.
  655. this->LinkDirectories = this->ExplicitLinkDirectories;
  656. for(LinkLibraryVectorType::iterator ll = this->LinkLibraries.begin();
  657. ll != this->LinkLibraries.end(); ++ll)
  658. {
  659. // If this library is a CMake target then add its location as a
  660. // link directory.
  661. std::string lib = ll->first;
  662. cmTarget* tgt = 0;
  663. if(this->Makefile && this->Makefile->GetLocalGenerator() &&
  664. this->Makefile->GetLocalGenerator()->GetGlobalGenerator())
  665. {
  666. tgt = (this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  667. ->FindTarget(0, lib.c_str(), false));
  668. }
  669. if(tgt)
  670. {
  671. // Add the directory only if it is not already present. This
  672. // is an N^2 algorithm for adding the directories, but N
  673. // should not get very big.
  674. const char* libpath = tgt->GetDirectory(0, implib);
  675. if(std::find(this->LinkDirectories.begin(),
  676. this->LinkDirectories.end(),
  677. libpath) == this->LinkDirectories.end())
  678. {
  679. this->LinkDirectories.push_back(libpath);
  680. }
  681. }
  682. }
  683. // The complete set of link directories has now been computed.
  684. this->LinkDirectoriesComputed = true;
  685. }
  686. // Return the complete set of link directories.
  687. return this->LinkDirectories;
  688. }
  689. void cmTarget::ClearDependencyInformation( cmMakefile& mf,
  690. const char* target )
  691. {
  692. // Clear the dependencies. The cache variable must exist iff we are
  693. // recording dependency information for this target.
  694. std::string depname = target;
  695. depname += "_LIB_DEPENDS";
  696. if (this->RecordDependencies)
  697. {
  698. mf.AddCacheDefinition(depname.c_str(), "",
  699. "Dependencies for target", cmCacheManager::STATIC);
  700. }
  701. else
  702. {
  703. if (mf.GetDefinition( depname.c_str() ))
  704. {
  705. std::string message = "Target ";
  706. message += target;
  707. message += " has dependency information when it shouldn't.\n";
  708. message += "Your cache is probably stale. Please remove the entry\n ";
  709. message += depname;
  710. message += "\nfrom the cache.";
  711. cmSystemTools::Error( message.c_str() );
  712. }
  713. }
  714. }
  715. void cmTarget::AddLinkLibrary(const std::string& lib,
  716. LinkLibraryType llt)
  717. {
  718. this->AddFramework(lib.c_str(), llt);
  719. cmTarget::LibraryID tmp;
  720. tmp.first = lib;
  721. tmp.second = llt;
  722. this->LinkLibraries.push_back(tmp);
  723. }
  724. bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
  725. {
  726. (void)llt; // TODO: What is this?
  727. if(cmSystemTools::IsPathToFramework(libname.c_str()))
  728. {
  729. std::string frameworkDir = libname;
  730. frameworkDir += "/../";
  731. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  732. std::vector<std::string>::iterator i =
  733. std::find(this->Frameworks.begin(),
  734. this->Frameworks.end(), frameworkDir);
  735. if(i == this->Frameworks.end())
  736. {
  737. this->Frameworks.push_back(frameworkDir);
  738. }
  739. return true;
  740. }
  741. return false;
  742. }
  743. void cmTarget::AddLinkLibrary(cmMakefile& mf,
  744. const char *target, const char* lib,
  745. LinkLibraryType llt)
  746. {
  747. // Never add a self dependency, even if the user asks for it.
  748. if(strcmp( target, lib ) == 0)
  749. {
  750. return;
  751. }
  752. this->AddFramework(lib, llt);
  753. cmTarget::LibraryID tmp;
  754. tmp.first = lib;
  755. tmp.second = llt;
  756. this->LinkLibraries.push_back( tmp );
  757. // Add the explicit dependency information for this target. This is
  758. // simply a set of libraries separated by ";". There should always
  759. // be a trailing ";". These library names are not canonical, in that
  760. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  761. // We shouldn't remove duplicates here because external libraries
  762. // may be purposefully duplicated to handle recursive dependencies,
  763. // and we removing one instance will break the link line. Duplicates
  764. // will be appropriately eliminated at emit time.
  765. if(this->RecordDependencies)
  766. {
  767. std::string targetEntry = target;
  768. targetEntry += "_LIB_DEPENDS";
  769. std::string dependencies;
  770. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  771. if( old_val )
  772. {
  773. dependencies += old_val;
  774. }
  775. switch (llt)
  776. {
  777. case cmTarget::GENERAL:
  778. dependencies += "general";
  779. break;
  780. case cmTarget::DEBUG:
  781. dependencies += "debug";
  782. break;
  783. case cmTarget::OPTIMIZED:
  784. dependencies += "optimized";
  785. break;
  786. }
  787. dependencies += ";";
  788. dependencies += lib;
  789. dependencies += ";";
  790. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  791. "Dependencies for the target",
  792. cmCacheManager::STATIC );
  793. }
  794. }
  795. void
  796. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  797. {
  798. // There are two key parts of the dependency analysis: (1)
  799. // determining the libraries in the link line, and (2) constructing
  800. // the dependency graph for those libraries.
  801. //
  802. // The latter is done using the cache entries that record the
  803. // dependencies of each library.
  804. //
  805. // The former is a more thorny issue, since it is not clear how to
  806. // determine if two libraries listed on the link line refer to the a
  807. // single library or not. For example, consider the link "libraries"
  808. // /usr/lib/libtiff.so -ltiff
  809. // Is this one library or two? The solution implemented here is the
  810. // simplest (and probably the only practical) one: two libraries are
  811. // the same if their "link strings" are identical. Thus, the two
  812. // libraries above are considered distinct. This also means that for
  813. // dependency analysis to be effective, the CMake user must specify
  814. // libraries build by his project without using any linker flags or
  815. // file extensions. That is,
  816. // LINK_LIBRARIES( One Two )
  817. // instead of
  818. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  819. // The former is probably what most users would do, but it never
  820. // hurts to document the assumptions. :-) Therefore, in the analysis
  821. // code, the "canonical name" of a library is simply its name as
  822. // given to a LINK_LIBRARIES command.
  823. //
  824. // Also, we will leave the original link line intact; we will just add any
  825. // dependencies that were missing.
  826. //
  827. // There is a problem with recursive external libraries
  828. // (i.e. libraries with no dependency information that are
  829. // recursively dependent). We must make sure that the we emit one of
  830. // the libraries twice to satisfy the recursion, but we shouldn't
  831. // emit it more times than necessary. In particular, we must make
  832. // sure that handling this improbable case doesn't cost us when
  833. // dealing with the common case of non-recursive libraries. The
  834. // solution is to assume that the recursion is satisfied at one node
  835. // of the dependency tree. To illustrate, assume libA and libB are
  836. // extrenal and mutually dependent. Suppose libX depends on
  837. // libA, and libY on libA and libX. Then
  838. // TARGET_LINK_LIBRARIES( Y X A B A )
  839. // TARGET_LINK_LIBRARIES( X A B A )
  840. // TARGET_LINK_LIBRARIES( Exec Y )
  841. // would result in "-lY -lX -lA -lB -lA". This is the correct way to
  842. // specify the dependencies, since the mutual dependency of A and B
  843. // is resolved *every time libA is specified*.
  844. //
  845. // Something like
  846. // TARGET_LINK_LIBRARIES( Y X A B A )
  847. // TARGET_LINK_LIBRARIES( X A B )
  848. // TARGET_LINK_LIBRARIES( Exec Y )
  849. // would result in "-lY -lX -lA -lB", and the mutual dependency
  850. // information is lost. This is because in some case (Y), the mutual
  851. // dependency of A and B is listed, while in another other case (X),
  852. // it is not. Depending on which line actually emits A, the mutual
  853. // dependency may or may not be on the final link line. We can't
  854. // handle this pathalogical case cleanly without emitting extra
  855. // libraries for the normal cases. Besides, the dependency
  856. // information for X is wrong anyway: if we build an executable
  857. // depending on X alone, we would not have the mutual dependency on
  858. // A and B resolved.
  859. //
  860. // IMPROVEMENTS:
  861. // -- The current algorithm will not always pick the "optimal" link line
  862. // when recursive dependencies are present. It will instead break the
  863. // cycles at an aribtrary point. The majority of projects won't have
  864. // cyclic dependencies, so this is probably not a big deal. Note that
  865. // the link line is always correct, just not necessary optimal.
  866. {
  867. // Expand variables in link library names. This is for backwards
  868. // compatibility with very early CMake versions and should
  869. // eventually be removed. This code was moved here from the end of
  870. // old source list processing code which was called just before this
  871. // method.
  872. for(LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
  873. p != this->LinkLibraries.end(); ++p)
  874. {
  875. this->Makefile->ExpandVariablesInString(p->first, true, true);
  876. }
  877. }
  878. typedef std::vector< std::string > LinkLine;
  879. // The dependency map.
  880. DependencyMap dep_map;
  881. if ( this->OriginalLinkLibraries.size() == 0 )
  882. {
  883. this->OriginalLinkLibraries = this->LinkLibraries;
  884. }
  885. // 1. Build the dependency graph
  886. //
  887. for(LinkLibraryVectorType::reverse_iterator lib
  888. = this->LinkLibraries.rbegin();
  889. lib != this->LinkLibraries.rend(); ++lib)
  890. {
  891. this->GatherDependencies( mf, *lib, dep_map);
  892. }
  893. // 2. Remove any dependencies that are already satisfied in the original
  894. // link line.
  895. //
  896. for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
  897. lib != this->LinkLibraries.end(); ++lib)
  898. {
  899. for( LinkLibraryVectorType::iterator lib2 = lib;
  900. lib2 != this->LinkLibraries.end(); ++lib2)
  901. {
  902. this->DeleteDependency( dep_map, *lib, *lib2);
  903. }
  904. }
  905. // 3. Create the new link line by simply emitting any dependencies that are
  906. // missing. Start from the back and keep adding.
  907. //
  908. std::set<DependencyMap::key_type> done, visited;
  909. std::vector<DependencyMap::key_type> newLinkLibraries;
  910. for(LinkLibraryVectorType::reverse_iterator lib =
  911. this->LinkLibraries.rbegin();
  912. lib != this->LinkLibraries.rend(); ++lib)
  913. {
  914. // skip zero size library entries, this may happen
  915. // if a variable expands to nothing.
  916. if (lib->first.size() != 0)
  917. {
  918. this->Emit( *lib, dep_map, done, visited, newLinkLibraries );
  919. }
  920. }
  921. // 4. Add the new libraries to the link line.
  922. //
  923. for( std::vector<DependencyMap::key_type>::reverse_iterator k =
  924. newLinkLibraries.rbegin();
  925. k != newLinkLibraries.rend(); ++k )
  926. {
  927. // get the llt from the dep_map
  928. this->LinkLibraries.push_back( std::make_pair(k->first,k->second) );
  929. }
  930. this->LinkLibrariesAnalyzed = true;
  931. }
  932. void cmTarget::InsertDependency( DependencyMap& depMap,
  933. const LibraryID& lib,
  934. const LibraryID& dep)
  935. {
  936. depMap[lib].push_back(dep);
  937. }
  938. void cmTarget::DeleteDependency( DependencyMap& depMap,
  939. const LibraryID& lib,
  940. const LibraryID& dep)
  941. {
  942. // Make sure there is an entry in the map for lib. If so, delete all
  943. // dependencies to dep. There may be repeated entries because of
  944. // external libraries that are specified multiple times.
  945. DependencyMap::iterator map_itr = depMap.find( lib );
  946. if( map_itr != depMap.end() )
  947. {
  948. DependencyList& depList = map_itr->second;
  949. DependencyList::iterator itr;
  950. while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
  951. depList.end() )
  952. {
  953. depList.erase( itr );
  954. }
  955. }
  956. }
  957. void cmTarget::Emit(const LibraryID lib,
  958. const DependencyMap& dep_map,
  959. std::set<LibraryID>& emitted,
  960. std::set<LibraryID>& visited,
  961. DependencyList& link_line )
  962. {
  963. // It's already been emitted
  964. if( emitted.find(lib) != emitted.end() )
  965. {
  966. return;
  967. }
  968. // Emit the dependencies only if this library node hasn't been
  969. // visited before. If it has, then we have a cycle. The recursion
  970. // that got us here should take care of everything.
  971. if( visited.insert(lib).second )
  972. {
  973. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  974. {
  975. const DependencyList& dep_on = dep_map.find( lib )->second;
  976. DependencyList::const_reverse_iterator i;
  977. // To cater for recursive external libraries, we must emit
  978. // duplicates on this link line *unless* they were emitted by
  979. // some other node, in which case we assume that the recursion
  980. // was resolved then. We making the simplifying assumption that
  981. // any duplicates on a single link line are on purpose, and must
  982. // be preserved.
  983. // This variable will keep track of the libraries that were
  984. // emitted directory from the current node, and not from a
  985. // recursive call. This way, if we come across a library that
  986. // has already been emitted, we repeat it iff it has been
  987. // emitted here.
  988. std::set<DependencyMap::key_type> emitted_here;
  989. for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
  990. {
  991. if( emitted_here.find(*i) != emitted_here.end() )
  992. {
  993. // a repeat. Must emit.
  994. emitted.insert(*i);
  995. link_line.push_back( *i );
  996. }
  997. else
  998. {
  999. // Emit only if no-one else has
  1000. if( emitted.find(*i) == emitted.end() )
  1001. {
  1002. // emit dependencies
  1003. Emit( *i, dep_map, emitted, visited, link_line );
  1004. // emit self
  1005. emitted.insert(*i);
  1006. emitted_here.insert(*i);
  1007. link_line.push_back( *i );
  1008. }
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. void cmTarget::GatherDependencies( const cmMakefile& mf,
  1015. const LibraryID& lib,
  1016. DependencyMap& dep_map)
  1017. {
  1018. // If the library is already in the dependency map, then it has
  1019. // already been fully processed.
  1020. if( dep_map.find(lib) != dep_map.end() )
  1021. {
  1022. return;
  1023. }
  1024. const char* deps = mf.GetDefinition( (lib.first+"_LIB_DEPENDS").c_str() );
  1025. if( deps && strcmp(deps,"") != 0 )
  1026. {
  1027. // Make sure this library is in the map, even if it has an empty
  1028. // set of dependencies. This distinguishes the case of explicitly
  1029. // no dependencies with that of unspecified dependencies.
  1030. dep_map[lib];
  1031. // Parse the dependency information, which is a set of
  1032. // type, library pairs separated by ";". There is always a trailing ";".
  1033. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  1034. std::string depline = deps;
  1035. std::string::size_type start = 0;
  1036. std::string::size_type end;
  1037. end = depline.find( ";", start );
  1038. while( end != std::string::npos )
  1039. {
  1040. std::string l = depline.substr( start, end-start );
  1041. if( l.size() != 0 )
  1042. {
  1043. if (l == "debug")
  1044. {
  1045. llt = cmTarget::DEBUG;
  1046. }
  1047. else if (l == "optimized")
  1048. {
  1049. llt = cmTarget::OPTIMIZED;
  1050. }
  1051. else if (l == "general")
  1052. {
  1053. llt = cmTarget::GENERAL;
  1054. }
  1055. else
  1056. {
  1057. LibraryID lib2(l,llt);
  1058. this->InsertDependency( dep_map, lib, lib2);
  1059. this->GatherDependencies( mf, lib2, dep_map);
  1060. llt = cmTarget::GENERAL;
  1061. }
  1062. }
  1063. start = end+1; // skip the ;
  1064. end = depline.find( ";", start );
  1065. }
  1066. // cannot depend on itself
  1067. this->DeleteDependency( dep_map, lib, lib);
  1068. }
  1069. }
  1070. void cmTarget::SetProperty(const char* prop, const char* value)
  1071. {
  1072. if (!prop)
  1073. {
  1074. return;
  1075. }
  1076. if (!value)
  1077. {
  1078. value = "NOTFOUND";
  1079. }
  1080. this->Properties.SetProperty(prop, value, cmProperty::TARGET);
  1081. }
  1082. void cmTarget::MarkAsImported()
  1083. {
  1084. this->IsImportedTarget = true;
  1085. }
  1086. const char* cmTarget::GetDirectory(const char* config, bool implib)
  1087. {
  1088. if (this->IsImported())
  1089. {
  1090. return this->ImportedGetDirectory(config, implib);
  1091. }
  1092. else
  1093. {
  1094. return this->NormalGetDirectory(config, implib);
  1095. }
  1096. }
  1097. const char* cmTarget::ImportedGetDirectory(const char* config, bool)
  1098. {
  1099. const char* location=this->GetLocation(config);
  1100. std::string directory=cmSystemTools::GetFilenamePath(location);
  1101. return directory.c_str();
  1102. }
  1103. const char* cmTarget::NormalGetDirectory(const char* config, bool implib)
  1104. {
  1105. if(config && *config)
  1106. {
  1107. this->Directory = this->GetOutputDir(implib);
  1108. // Add the configuration's subdirectory.
  1109. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  1110. AppendDirectoryForConfig("/", config, "", this->Directory);
  1111. return this->Directory.c_str();
  1112. }
  1113. else
  1114. {
  1115. return this->GetOutputDir(implib);
  1116. }
  1117. }
  1118. const char* cmTarget::GetLocation(const char* config)
  1119. {
  1120. if (this->IsImported())
  1121. {
  1122. return this->ImportedGetLocation(config);
  1123. }
  1124. else
  1125. {
  1126. return this->NormalGetLocation(config);
  1127. }
  1128. }
  1129. const char* cmTarget::ImportedGetLocation(const char* config)
  1130. {
  1131. if ((config) && (strlen(config)))
  1132. {
  1133. std::string propertyName=config;
  1134. propertyName+="_LOCATION";
  1135. const char* configLocation=this->GetProperty(propertyName.c_str());
  1136. if ((configLocation) && (strlen(configLocation)))
  1137. {
  1138. return configLocation;
  1139. }
  1140. }
  1141. return this->GetProperty("LOCATION");
  1142. }
  1143. const char* cmTarget::NormalGetLocation(const char* config)
  1144. {
  1145. this->Location = this->GetDirectory();
  1146. if(!this->Location.empty())
  1147. {
  1148. this->Location += "/";
  1149. }
  1150. const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  1151. if(cfgid && strcmp(cfgid, ".") != 0)
  1152. {
  1153. this->Location += cfgid;
  1154. this->Location += "/";
  1155. }
  1156. this->Location += this->GetFullName(config, false);
  1157. return this->Location.c_str();
  1158. }
  1159. //----------------------------------------------------------------------------
  1160. void cmTarget::GetTargetVersion(int& major, int& minor)
  1161. {
  1162. // Set the default values.
  1163. major = 0;
  1164. minor = 0;
  1165. // Look for a VERSION property.
  1166. if(const char* version = this->GetProperty("VERSION"))
  1167. {
  1168. // Try to parse the version number and store the results that were
  1169. // successfully parsed.
  1170. int parsed_major;
  1171. int parsed_minor;
  1172. switch(sscanf(version, "%d.%d", &parsed_major, &parsed_minor))
  1173. {
  1174. case 2: minor = parsed_minor; // no break!
  1175. case 1: major = parsed_major; // no break!
  1176. default: break;
  1177. }
  1178. }
  1179. }
  1180. const char *cmTarget::GetProperty(const char* prop)
  1181. {
  1182. return this->GetProperty(prop, cmProperty::TARGET);
  1183. }
  1184. void cmTarget::ComputeObjectFiles()
  1185. {
  1186. if (this->IsImported())
  1187. {
  1188. return;
  1189. }
  1190. #if 0
  1191. std::vector<std::string> dirs;
  1192. this->Makefile->GetLocalGenerator()->
  1193. GetTargetObjectFileDirectories(this,
  1194. dirs);
  1195. std::string objectFiles;
  1196. std::string objExtensionLookup1 = "CMAKE_";
  1197. std::string objExtensionLookup2 = "_OUTPUT_EXTENSION";
  1198. for(std::vector<std::string>::iterator d = dirs.begin();
  1199. d != dirs.end(); ++d)
  1200. {
  1201. for(std::vector<cmSourceFile*>::iterator s = this->SourceFiles.begin();
  1202. s != this->SourceFiles.end(); ++s)
  1203. {
  1204. cmSourceFile* sf = *s;
  1205. if(const char* lang = sf->GetLanguage())
  1206. {
  1207. std::string lookupObj = objExtensionLookup1 + lang;
  1208. lookupObj += objExtensionLookup2;
  1209. const char* obj = this->Makefile->GetDefinition(lookupObj.c_str());
  1210. if(obj)
  1211. {
  1212. if(objectFiles.size())
  1213. {
  1214. objectFiles += ";";
  1215. }
  1216. std::string objFile = *d;
  1217. objFile += "/";
  1218. objFile += this->Makefile->GetLocalGenerator()->
  1219. GetSourceObjectName(*sf);
  1220. objFile += obj;
  1221. objectFiles += objFile;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. this->SetProperty("OBJECT_FILES", objectFiles.c_str());
  1227. #endif
  1228. }
  1229. const char *cmTarget::GetProperty(const char* prop,
  1230. cmProperty::ScopeType scope)
  1231. {
  1232. if(!prop)
  1233. {
  1234. return 0;
  1235. }
  1236. // don't use GetLocation() for imported targets, because there this
  1237. // calls GetProperty() to get the location...
  1238. if (!this->IsImported())
  1239. {
  1240. // watch for special "computed" properties that are dependent on other
  1241. // properties or variables, always recompute them
  1242. if (!strcmp(prop,"LOCATION"))
  1243. {
  1244. // Set the LOCATION property of the target. Note that this cannot take
  1245. // into account the per-configuration name of the target because the
  1246. // configuration type may not be known at CMake time. We should
  1247. // deprecate this feature and instead support transforming an executable
  1248. // target name given as the command part of custom commands into the
  1249. // proper path at build time. Alternatively we could put environment
  1250. // variable settings in all custom commands that hold the name of the
  1251. // target for each configuration and then give a reference to the
  1252. // variable in the location.
  1253. this->SetProperty("LOCATION", this->GetLocation(0));
  1254. }
  1255. // Per-configuration location can be computed.
  1256. int len = static_cast<int>(strlen(prop));
  1257. if(len > 9 && strcmp(prop+len-9, "_LOCATION") == 0)
  1258. {
  1259. std::string configName(prop, len-9);
  1260. this->SetProperty(prop, this->GetLocation(configName.c_str()));
  1261. }
  1262. if(strcmp(prop, "OBJECT_FILES") == 0)
  1263. {
  1264. this->ComputeObjectFiles();
  1265. }
  1266. }
  1267. if (strcmp(prop,"IMPORTED") == 0)
  1268. {
  1269. return this->IsImported()?"TRUE":"FALSE";
  1270. }
  1271. // the type property returns what type the target is
  1272. if (!strcmp(prop,"TYPE"))
  1273. {
  1274. switch( this->GetType() )
  1275. {
  1276. case cmTarget::STATIC_LIBRARY:
  1277. return "STATIC_LIBRARY";
  1278. // break; /* unreachable */
  1279. case cmTarget::MODULE_LIBRARY:
  1280. return "MODULE_LIBRARY";
  1281. // break; /* unreachable */
  1282. case cmTarget::SHARED_LIBRARY:
  1283. return "SHARED_LIBRARY";
  1284. // break; /* unreachable */
  1285. case cmTarget::EXECUTABLE:
  1286. return "EXECUTABLE";
  1287. // break; /* unreachable */
  1288. case cmTarget::UTILITY:
  1289. return "UTILITY";
  1290. // break; /* unreachable */
  1291. case cmTarget::GLOBAL_TARGET:
  1292. return "GLOBAL_TARGET";
  1293. // break; /* unreachable */
  1294. case cmTarget::INSTALL_FILES:
  1295. return "INSTALL_FILES";
  1296. // break; /* unreachable */
  1297. case cmTarget::INSTALL_PROGRAMS:
  1298. return "INSTALL_PROGRAMS";
  1299. // break; /* unreachable */
  1300. case cmTarget::INSTALL_DIRECTORY:
  1301. return "INSTALL_DIRECTORY";
  1302. // break; /* unreachable */
  1303. }
  1304. return 0;
  1305. }
  1306. bool chain = false;
  1307. const char *retVal =
  1308. this->Properties.GetPropertyValue(prop, scope, chain);
  1309. if (chain)
  1310. {
  1311. return this->Makefile->GetProperty(prop,scope);
  1312. }
  1313. return retVal;
  1314. }
  1315. bool cmTarget::GetPropertyAsBool(const char* prop)
  1316. {
  1317. return cmSystemTools::IsOn(this->GetProperty(prop));
  1318. }
  1319. const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
  1320. {
  1321. if(this->GetProperty("HAS_CXX"))
  1322. {
  1323. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
  1324. }
  1325. const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
  1326. if(linkerLang)
  1327. {
  1328. return linkerLang;
  1329. }
  1330. std::set<cmStdString> languages;
  1331. for(std::vector<cmSourceFile*>::const_iterator i
  1332. = this->SourceFiles.begin();
  1333. i != this->SourceFiles.end(); ++i)
  1334. {
  1335. if(const char* lang = (*i)->GetLanguage())
  1336. {
  1337. languages.insert(lang);
  1338. }
  1339. }
  1340. if(languages.size() == 0)
  1341. {
  1342. return 0;
  1343. }
  1344. if(languages.size() == 1)
  1345. {
  1346. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE",
  1347. languages.begin()->c_str());
  1348. return this->GetProperty("LINKER_LANGUAGE");
  1349. }
  1350. const char* prefLang = 0;
  1351. for(std::set<cmStdString>::const_iterator s = languages.begin();
  1352. s != languages.end(); ++s)
  1353. {
  1354. const char* lpref = gg->GetLinkerPreference(s->c_str());
  1355. if(lpref[0] == 'P')
  1356. {
  1357. if(prefLang && !(*s == prefLang))
  1358. {
  1359. std::string m = "Error Target: ";
  1360. m += this->Name + " Contains more than one Prefered language: ";
  1361. m += *s;
  1362. m += " and ";
  1363. m += prefLang;
  1364. m += "\nYou must set the LINKER_LANGUAGE property for this target.";
  1365. cmSystemTools::Error(m.c_str());
  1366. }
  1367. else
  1368. {
  1369. prefLang = s->c_str();
  1370. }
  1371. }
  1372. }
  1373. if(!prefLang)
  1374. {
  1375. prefLang = languages.begin()->c_str();
  1376. }
  1377. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", prefLang);
  1378. return this->GetProperty("LINKER_LANGUAGE");
  1379. }
  1380. const char* cmTarget::GetCreateRuleVariable()
  1381. {
  1382. switch(this->GetType())
  1383. {
  1384. case cmTarget::STATIC_LIBRARY:
  1385. return "_CREATE_STATIC_LIBRARY";
  1386. case cmTarget::SHARED_LIBRARY:
  1387. return "_CREATE_SHARED_LIBRARY";
  1388. case cmTarget::MODULE_LIBRARY:
  1389. return "_CREATE_SHARED_MODULE";
  1390. case cmTarget::EXECUTABLE:
  1391. return "_LINK_EXECUTABLE";
  1392. case cmTarget::UTILITY:
  1393. case cmTarget::GLOBAL_TARGET:
  1394. case cmTarget::INSTALL_FILES:
  1395. case cmTarget::INSTALL_PROGRAMS:
  1396. case cmTarget::INSTALL_DIRECTORY:
  1397. break;
  1398. }
  1399. return "";
  1400. }
  1401. const char* cmTarget::GetSuffixVariableInternal(TargetType type,
  1402. bool implib)
  1403. {
  1404. switch(type)
  1405. {
  1406. case cmTarget::STATIC_LIBRARY:
  1407. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  1408. case cmTarget::SHARED_LIBRARY:
  1409. return (implib
  1410. ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  1411. : "CMAKE_SHARED_LIBRARY_SUFFIX");
  1412. case cmTarget::MODULE_LIBRARY:
  1413. return (implib
  1414. ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  1415. : "CMAKE_SHARED_MODULE_SUFFIX");
  1416. case cmTarget::EXECUTABLE:
  1417. return (implib
  1418. ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  1419. : "CMAKE_EXECUTABLE_SUFFIX");
  1420. case cmTarget::UTILITY:
  1421. case cmTarget::GLOBAL_TARGET:
  1422. case cmTarget::INSTALL_FILES:
  1423. case cmTarget::INSTALL_PROGRAMS:
  1424. case cmTarget::INSTALL_DIRECTORY:
  1425. break;
  1426. }
  1427. return "";
  1428. }
  1429. const char* cmTarget::GetPrefixVariableInternal(TargetType type,
  1430. bool implib)
  1431. {
  1432. switch(type)
  1433. {
  1434. case cmTarget::STATIC_LIBRARY:
  1435. return "CMAKE_STATIC_LIBRARY_PREFIX";
  1436. case cmTarget::SHARED_LIBRARY:
  1437. return (implib
  1438. ? "CMAKE_IMPORT_LIBRARY_PREFIX"
  1439. : "CMAKE_SHARED_LIBRARY_PREFIX");
  1440. case cmTarget::MODULE_LIBRARY:
  1441. return (implib
  1442. ? "CMAKE_IMPORT_LIBRARY_PREFIX"
  1443. : "CMAKE_SHARED_MODULE_PREFIX");
  1444. case cmTarget::EXECUTABLE:
  1445. return (implib? "CMAKE_IMPORT_LIBRARY_PREFIX" : "");
  1446. case cmTarget::UTILITY:
  1447. case cmTarget::GLOBAL_TARGET:
  1448. case cmTarget::INSTALL_FILES:
  1449. case cmTarget::INSTALL_PROGRAMS:
  1450. case cmTarget::INSTALL_DIRECTORY:
  1451. break;
  1452. }
  1453. return "";
  1454. }
  1455. //----------------------------------------------------------------------------
  1456. std::string cmTarget::GetPDBName(const char* config)
  1457. {
  1458. std::string prefix;
  1459. std::string base;
  1460. std::string suffix;
  1461. this->GetFullNameInternal(this->GetType(), config, false,
  1462. prefix, base, suffix);
  1463. return prefix+base+".pdb";
  1464. }
  1465. //----------------------------------------------------------------------------
  1466. std::string cmTarget::GetFullName(const char* config, bool implib)
  1467. {
  1468. return this->GetFullNameInternal(this->GetType(), config, implib);
  1469. }
  1470. //----------------------------------------------------------------------------
  1471. void cmTarget::GetFullName(std::string& prefix, std::string& base,
  1472. std::string& suffix, const char* config,
  1473. bool implib)
  1474. {
  1475. this->GetFullNameInternal(this->GetType(), config, implib,
  1476. prefix, base, suffix);
  1477. }
  1478. //----------------------------------------------------------------------------
  1479. std::string cmTarget::GetFullPath(const char* config, bool implib)
  1480. {
  1481. // Start with the output directory for the target.
  1482. std::string fpath = this->GetDirectory(config, implib);
  1483. fpath += "/";
  1484. // Add the full name of the target.
  1485. fpath += this->GetFullName(config, implib);
  1486. return fpath;
  1487. }
  1488. //----------------------------------------------------------------------------
  1489. std::string cmTarget::GetFullNameInternal(TargetType type, const char* config,
  1490. bool implib)
  1491. {
  1492. std::string prefix;
  1493. std::string base;
  1494. std::string suffix;
  1495. this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
  1496. return prefix+base+suffix;
  1497. }
  1498. void cmTarget::GetFullNameInternal(TargetType type,
  1499. const char* config,
  1500. bool implib,
  1501. std::string& outPrefix,
  1502. std::string& outBase,
  1503. std::string& outSuffix)
  1504. {
  1505. if (this->IsImported())
  1506. {
  1507. this->ImportedGetFullNameInternal(type, config, implib,
  1508. outPrefix, outBase, outSuffix);
  1509. }
  1510. else
  1511. {
  1512. this->NormalGetFullNameInternal(type, config, implib,
  1513. outPrefix, outBase, outSuffix);
  1514. }
  1515. }
  1516. void cmTarget::ImportedGetFullNameInternal(TargetType ,
  1517. const char* config,
  1518. bool ,
  1519. std::string& outPrefix,
  1520. std::string& outBase,
  1521. std::string& outSuffix)
  1522. {
  1523. // find the basename, suffix and prefix from getLocation()
  1524. // implib ?
  1525. std::string location=this->GetLocation(config);
  1526. outBase=cmSystemTools::GetFilenameWithoutExtension(location);
  1527. outSuffix = cmSystemTools::GetFilenameExtension(location);
  1528. outPrefix = "";
  1529. }
  1530. //----------------------------------------------------------------------------
  1531. void cmTarget::NormalGetFullNameInternal(TargetType type,
  1532. const char* config,
  1533. bool implib,
  1534. std::string& outPrefix,
  1535. std::string& outBase,
  1536. std::string& outSuffix)
  1537. {
  1538. // Use just the target name for non-main target types.
  1539. if(type != cmTarget::STATIC_LIBRARY &&
  1540. type != cmTarget::SHARED_LIBRARY &&
  1541. type != cmTarget::MODULE_LIBRARY &&
  1542. type != cmTarget::EXECUTABLE)
  1543. {
  1544. outPrefix = "";
  1545. outBase = this->GetName();
  1546. outSuffix = "";
  1547. return;
  1548. }
  1549. // Return an empty name for the import library if this platform
  1550. // does not support import libraries.
  1551. if(implib &&
  1552. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  1553. {
  1554. outPrefix = "";
  1555. outBase = "";
  1556. outSuffix = "";
  1557. return;
  1558. }
  1559. // The implib option is only allowed for shared libraries, module
  1560. // libraries, and executables.
  1561. if(type != cmTarget::SHARED_LIBRARY &&
  1562. type != cmTarget::MODULE_LIBRARY &&
  1563. type != cmTarget::EXECUTABLE)
  1564. {
  1565. implib = false;
  1566. }
  1567. // Compute the full name for main target types.
  1568. const char* targetPrefix = (implib
  1569. ? this->GetProperty("IMPORT_PREFIX")
  1570. : this->GetProperty("PREFIX"));
  1571. const char* targetSuffix = (implib
  1572. ? this->GetProperty("IMPORT_SUFFIX")
  1573. : this->GetProperty("SUFFIX"));
  1574. const char* configPostfix = 0;
  1575. if(config && *config)
  1576. {
  1577. std::string configProp = cmSystemTools::UpperCase(config);
  1578. configProp += "_POSTFIX";
  1579. configPostfix = this->GetProperty(configProp.c_str());
  1580. }
  1581. const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
  1582. const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
  1583. const char* ll =
  1584. this->GetLinkerLanguage(
  1585. this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
  1586. // first try language specific suffix
  1587. if(ll)
  1588. {
  1589. if(!targetSuffix && suffixVar && *suffixVar)
  1590. {
  1591. std::string langSuff = suffixVar + std::string("_") + ll;
  1592. targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
  1593. }
  1594. if(!targetPrefix && prefixVar && *prefixVar)
  1595. {
  1596. std::string langPrefix = prefixVar + std::string("_") + ll;
  1597. targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
  1598. }
  1599. }
  1600. // if there is no prefix on the target use the cmake definition
  1601. if(!targetPrefix && prefixVar)
  1602. {
  1603. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
  1604. }
  1605. // if there is no suffix on the target use the cmake definition
  1606. if(!targetSuffix && suffixVar)
  1607. {
  1608. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
  1609. }
  1610. #if defined(__APPLE__)
  1611. // frameworks do not have a prefix or a suffix
  1612. if(this->GetPropertyAsBool("FRAMEWORK"))
  1613. {
  1614. targetPrefix = 0;
  1615. targetSuffix = 0;
  1616. }
  1617. #endif
  1618. // Begin the final name with the prefix.
  1619. outPrefix = targetPrefix?targetPrefix:"";
  1620. // Append the target name or property-specified name.
  1621. const char* outName = 0;
  1622. if(config && *config)
  1623. {
  1624. std::string configProp = cmSystemTools::UpperCase(config);
  1625. configProp += "_OUTPUT_NAME";
  1626. outName = this->GetProperty(configProp.c_str());
  1627. }
  1628. if(!outName)
  1629. {
  1630. outName = this->GetProperty("OUTPUT_NAME");
  1631. }
  1632. if(outName)
  1633. {
  1634. outBase = outName;
  1635. }
  1636. else
  1637. {
  1638. outBase = this->GetName();
  1639. }
  1640. // Append the per-configuration postfix.
  1641. outBase += configPostfix?configPostfix:"";
  1642. // Name shared libraries with their version number on some platforms.
  1643. if(const char* version = this->GetProperty("VERSION"))
  1644. {
  1645. if(type == cmTarget::SHARED_LIBRARY && !implib &&
  1646. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION"))
  1647. {
  1648. outBase += "-";
  1649. outBase += version;
  1650. }
  1651. }
  1652. // Append the suffix.
  1653. outSuffix = targetSuffix?targetSuffix:"";
  1654. }
  1655. void cmTarget::GetLibraryNames(std::string& name,
  1656. std::string& soName,
  1657. std::string& realName,
  1658. std::string& impName,
  1659. std::string& pdbName,
  1660. const char* config)
  1661. {
  1662. // Get the names based on the real type of the library.
  1663. this->GetLibraryNamesInternal(name, soName, realName, impName, pdbName,
  1664. this->GetType(), config);
  1665. }
  1666. void cmTarget::GetLibraryCleanNames(std::string& staticName,
  1667. std::string& sharedName,
  1668. std::string& sharedSOName,
  1669. std::string& sharedRealName,
  1670. std::string& importName,
  1671. std::string& pdbName,
  1672. const char* config)
  1673. {
  1674. // Get the name as if this were a static library.
  1675. std::string soName;
  1676. std::string realName;
  1677. std::string impName;
  1678. this->GetLibraryNamesInternal(staticName, soName, realName, impName,
  1679. pdbName, cmTarget::STATIC_LIBRARY, config);
  1680. // Get the names as if this were a shared library.
  1681. if(this->GetType() == cmTarget::STATIC_LIBRARY)
  1682. {
  1683. // Since the real type is static then the user either specified
  1684. // STATIC or did not specify a type. In the former case the
  1685. // shared library will never be present. In the latter case the
  1686. // type will never be MODULE. Either way the only names that
  1687. // might have to be cleaned are the shared library names.
  1688. this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
  1689. importName, pdbName,
  1690. cmTarget::SHARED_LIBRARY, config);
  1691. }
  1692. else
  1693. {
  1694. // Use the name of the real type of the library (shared or module).
  1695. this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
  1696. importName, pdbName, this->GetType(),
  1697. config);
  1698. }
  1699. }
  1700. void cmTarget::GetLibraryNamesInternal(std::string& name,
  1701. std::string& soName,
  1702. std::string& realName,
  1703. std::string& impName,
  1704. std::string& pdbName,
  1705. TargetType type,
  1706. const char* config)
  1707. {
  1708. // Construct the name of the soname flag variable for this language.
  1709. const char* ll =
  1710. this->GetLinkerLanguage(
  1711. this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
  1712. std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
  1713. if(ll)
  1714. {
  1715. sonameFlag += "_";
  1716. sonameFlag += ll;
  1717. }
  1718. sonameFlag += "_FLAG";
  1719. // Check for library version properties.
  1720. const char* version = this->GetProperty("VERSION");
  1721. const char* soversion = this->GetProperty("SOVERSION");
  1722. if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
  1723. !this->Makefile->GetDefinition(sonameFlag.c_str()))
  1724. {
  1725. // Versioning is supported only for shared libraries and modules,
  1726. // and then only when the platform supports an soname flag.
  1727. version = 0;
  1728. soversion = 0;
  1729. }
  1730. if(version && !soversion)
  1731. {
  1732. // The soversion must be set if the library version is set. Use
  1733. // the library version as the soversion.
  1734. soversion = version;
  1735. }
  1736. // Get the components of the library name.
  1737. std::string prefix;
  1738. std::string base;
  1739. std::string suffix;
  1740. this->GetFullNameInternal(type, config, false, prefix, base, suffix);
  1741. // The library name.
  1742. name = prefix+base+suffix;
  1743. // The library's soname.
  1744. #if defined(__APPLE__)
  1745. soName = prefix+base;
  1746. #else
  1747. soName = name;
  1748. #endif
  1749. if(soversion)
  1750. {
  1751. soName += ".";
  1752. soName += soversion;
  1753. }
  1754. #if defined(__APPLE__)
  1755. soName += suffix;
  1756. #endif
  1757. // The library's real name on disk.
  1758. #if defined(__APPLE__)
  1759. realName = prefix+base;
  1760. #else
  1761. realName = name;
  1762. #endif
  1763. if(version)
  1764. {
  1765. realName += ".";
  1766. realName += version;
  1767. }
  1768. else if(soversion)
  1769. {
  1770. realName += ".";
  1771. realName += soversion;
  1772. }
  1773. #if defined(__APPLE__)
  1774. realName += suffix;
  1775. #endif
  1776. // The import library name.
  1777. if(type == cmTarget::SHARED_LIBRARY ||
  1778. type == cmTarget::MODULE_LIBRARY)
  1779. {
  1780. impName = this->GetFullNameInternal(type, config, true);
  1781. }
  1782. else
  1783. {
  1784. impName = "";
  1785. }
  1786. // The program database file name.
  1787. pdbName = prefix+base+".pdb";
  1788. }
  1789. void cmTarget::GetExecutableNames(std::string& name,
  1790. std::string& realName,
  1791. std::string& impName,
  1792. std::string& pdbName,
  1793. const char* config)
  1794. {
  1795. // Get the names based on the real type of the executable.
  1796. this->GetExecutableNamesInternal(name, realName, impName, pdbName,
  1797. this->GetType(), config);
  1798. }
  1799. void cmTarget::GetExecutableCleanNames(std::string& name,
  1800. std::string& realName,
  1801. std::string& impName,
  1802. std::string& pdbName,
  1803. const char* config)
  1804. {
  1805. // Get the name and versioned name of this executable.
  1806. this->GetExecutableNamesInternal(name, realName, impName, pdbName,
  1807. cmTarget::EXECUTABLE, config);
  1808. }
  1809. void cmTarget::GetExecutableNamesInternal(std::string& name,
  1810. std::string& realName,
  1811. std::string& impName,
  1812. std::string& pdbName,
  1813. TargetType type,
  1814. const char* config)
  1815. {
  1816. // This versioning is supported only for executables and then only
  1817. // when the platform supports symbolic links.
  1818. #if defined(_WIN32) && !defined(__CYGWIN__)
  1819. const char* version = 0;
  1820. #else
  1821. // Check for executable version properties.
  1822. const char* version = this->GetProperty("VERSION");
  1823. if(type != cmTarget::EXECUTABLE)
  1824. {
  1825. version = 0;
  1826. }
  1827. #endif
  1828. // Get the components of the executable name.
  1829. std::string prefix;
  1830. std::string base;
  1831. std::string suffix;
  1832. this->GetFullNameInternal(type, config, false, prefix, base, suffix);
  1833. // The executable name.
  1834. name = prefix+base+suffix;
  1835. // The executable's real name on disk.
  1836. #if defined(__CYGWIN__)
  1837. realName = prefix+base;
  1838. #else
  1839. realName = name;
  1840. #endif
  1841. if(version)
  1842. {
  1843. realName += "-";
  1844. realName += version;
  1845. }
  1846. #if defined(__CYGWIN__)
  1847. realName += suffix;
  1848. #endif
  1849. // The import library name.
  1850. impName = this->GetFullNameInternal(type, config, true);
  1851. // The program database file name.
  1852. pdbName = prefix+base+".pdb";
  1853. }
  1854. //----------------------------------------------------------------------------
  1855. void cmTarget::SetPropertyDefault(const char* property,
  1856. const char* default_value)
  1857. {
  1858. // Compute the name of the variable holding the default value.
  1859. std::string var = "CMAKE_";
  1860. var += property;
  1861. if(const char* value = this->Makefile->GetDefinition(var.c_str()))
  1862. {
  1863. this->SetProperty(property, value);
  1864. }
  1865. else if(default_value)
  1866. {
  1867. this->SetProperty(property, default_value);
  1868. }
  1869. }
  1870. //----------------------------------------------------------------------------
  1871. bool cmTarget::HaveBuildTreeRPATH()
  1872. {
  1873. return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
  1874. !this->LinkLibraries.empty());
  1875. }
  1876. //----------------------------------------------------------------------------
  1877. bool cmTarget::HaveInstallTreeRPATH()
  1878. {
  1879. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  1880. return install_rpath && *install_rpath;
  1881. }
  1882. //----------------------------------------------------------------------------
  1883. bool cmTarget::NeedRelinkBeforeInstall()
  1884. {
  1885. // Only executables and shared libraries can have an rpath and may
  1886. // need relinking.
  1887. if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
  1888. this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
  1889. this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
  1890. {
  1891. return false;
  1892. }
  1893. // If there is no install location this target will not be installed
  1894. // and therefore does not need relinking.
  1895. if(!this->GetHaveInstallRule())
  1896. {
  1897. return false;
  1898. }
  1899. // If skipping all rpaths completely then no relinking is needed.
  1900. if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1901. {
  1902. return false;
  1903. }
  1904. // If building with the install-tree rpath no relinking is needed.
  1905. if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
  1906. {
  1907. return false;
  1908. }
  1909. // Check for rpath support on this platform.
  1910. if(const char* ll = this->GetLinkerLanguage(
  1911. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
  1912. {
  1913. std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1914. flagVar += ll;
  1915. flagVar += "_FLAG";
  1916. if(!this->Makefile->IsSet(flagVar.c_str()))
  1917. {
  1918. // There is no rpath support on this platform so nothing needs
  1919. // relinking.
  1920. return false;
  1921. }
  1922. }
  1923. else
  1924. {
  1925. // No linker language is known. This error will be reported by
  1926. // other code.
  1927. return false;
  1928. }
  1929. // If either a build or install tree rpath is set then the rpath
  1930. // will likely change between the build tree and install tree and
  1931. // this target must be relinked.
  1932. return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
  1933. }
  1934. //----------------------------------------------------------------------------
  1935. std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
  1936. {
  1937. // If building directly for installation then the build tree install_name
  1938. // is the same as the install tree.
  1939. if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
  1940. {
  1941. return GetInstallNameDirForInstallTree(config);
  1942. }
  1943. // Use the build tree directory for the target.
  1944. if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
  1945. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
  1946. !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
  1947. {
  1948. std::string dir = this->GetDirectory(config);
  1949. dir += "/";
  1950. return dir;
  1951. }
  1952. else
  1953. {
  1954. return "";
  1955. }
  1956. }
  1957. //----------------------------------------------------------------------------
  1958. std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
  1959. {
  1960. // Lookup the target property.
  1961. const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  1962. if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
  1963. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
  1964. install_name_dir && *install_name_dir)
  1965. {
  1966. std::string dir = install_name_dir;
  1967. dir += "/";
  1968. return dir;
  1969. }
  1970. else
  1971. {
  1972. return "";
  1973. }
  1974. }
  1975. //----------------------------------------------------------------------------
  1976. const char* cmTarget::GetOutputDir(bool implib)
  1977. {
  1978. // The implib option is only allowed for shared libraries, module
  1979. // libraries, and executables.
  1980. if(this->GetType() != cmTarget::SHARED_LIBRARY &&
  1981. this->GetType() != cmTarget::MODULE_LIBRARY &&
  1982. this->GetType() != cmTarget::EXECUTABLE)
  1983. {
  1984. implib = false;
  1985. }
  1986. // Sanity check. Only generators on platforms supporting import
  1987. // libraries should be asking for the import library output
  1988. // directory.
  1989. if(implib &&
  1990. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  1991. {
  1992. abort();
  1993. }
  1994. if(implib && !this->DLLPlatform)
  1995. {
  1996. abort();
  1997. }
  1998. // Select whether we are constructing the directory for the main
  1999. // target or the import library.
  2000. std::string& out = implib? this->OutputDirImplib : this->OutputDir;
  2001. if(out.empty())
  2002. {
  2003. // Look for a target property defining the target output directory
  2004. // based on the target type.
  2005. const char* propertyName = 0;
  2006. switch(this->GetType())
  2007. {
  2008. case cmTarget::SHARED_LIBRARY:
  2009. {
  2010. // For non-DLL platforms shared libraries are treated as
  2011. // library targets. For DLL platforms the DLL part of a
  2012. // shared library is treated as a runtime target and the
  2013. // corresponding import library is treated as an archive
  2014. // target.
  2015. if(this->DLLPlatform)
  2016. {
  2017. if(implib)
  2018. {
  2019. propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
  2020. }
  2021. else
  2022. {
  2023. propertyName = "RUNTIME_OUTPUT_DIRECTORY";
  2024. }
  2025. }
  2026. else
  2027. {
  2028. propertyName = "LIBRARY_OUTPUT_DIRECTORY";
  2029. }
  2030. } break;
  2031. case cmTarget::STATIC_LIBRARY:
  2032. {
  2033. // Static libraries are always treated as archive targets.
  2034. propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
  2035. } break;
  2036. case cmTarget::MODULE_LIBRARY:
  2037. {
  2038. // Module libraries are always treated as library targets.
  2039. // Module import libraries are treated as archive targets.
  2040. if(implib)
  2041. {
  2042. propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
  2043. }
  2044. else
  2045. {
  2046. propertyName = "LIBRARY_OUTPUT_DIRECTORY";
  2047. }
  2048. } break;
  2049. case cmTarget::EXECUTABLE:
  2050. {
  2051. // Executables are always treated as runtime targets.
  2052. // Executable import libraries are treated as archive targets.
  2053. if(implib)
  2054. {
  2055. propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
  2056. }
  2057. else
  2058. {
  2059. propertyName = "RUNTIME_OUTPUT_DIRECTORY";
  2060. }
  2061. } break;
  2062. default: break;
  2063. }
  2064. // Select an output directory.
  2065. if(const char* outdir = this->GetProperty(propertyName))
  2066. {
  2067. // Use the user-specified output directory.
  2068. out = outdir;
  2069. }
  2070. else if(this->GetType() == cmTarget::EXECUTABLE)
  2071. {
  2072. // Lookup the output path for executables.
  2073. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2074. }
  2075. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  2076. this->GetType() == cmTarget::SHARED_LIBRARY ||
  2077. this->GetType() == cmTarget::MODULE_LIBRARY)
  2078. {
  2079. // Lookup the output path for libraries.
  2080. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  2081. }
  2082. if(out.empty())
  2083. {
  2084. // Default to the current output directory.
  2085. out = ".";
  2086. }
  2087. // Convert the output path to a full path in case it is
  2088. // specified as a relative path. Treat a relative path as
  2089. // relative to the current output directory for this makefile.
  2090. out =
  2091. cmSystemTools::CollapseFullPath
  2092. (out.c_str(), this->Makefile->GetStartOutputDirectory());
  2093. #if defined(__APPLE__)
  2094. // frameworks do not have a prefix or a suffix
  2095. if(this->GetPropertyAsBool("FRAMEWORK"))
  2096. {
  2097. out += "/";
  2098. out += this->GetFullName(0, implib);
  2099. out += ".framework";
  2100. }
  2101. #endif
  2102. // Make sure the output path exists on disk.
  2103. if(!cmSystemTools::MakeDirectory(out.c_str()))
  2104. {
  2105. cmSystemTools::Error("Error failed to create output directory:",
  2106. out.c_str());
  2107. }
  2108. }
  2109. return out.c_str();
  2110. }
  2111. //----------------------------------------------------------------------------
  2112. const char* cmTarget::GetExportMacro()
  2113. {
  2114. // Define the symbol for targets that export symbols.
  2115. if(this->GetType() == cmTarget::SHARED_LIBRARY ||
  2116. this->GetType() == cmTarget::MODULE_LIBRARY ||
  2117. this->GetType() == cmTarget::EXECUTABLE &&
  2118. this->GetPropertyAsBool("ENABLE_EXPORTS"))
  2119. {
  2120. if(const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL"))
  2121. {
  2122. this->ExportMacro = custom_export_name;
  2123. }
  2124. else
  2125. {
  2126. std::string in = this->GetName();
  2127. in += "_EXPORTS";
  2128. this->ExportMacro = cmSystemTools::MakeCindentifier(in.c_str());
  2129. }
  2130. return this->ExportMacro.c_str();
  2131. }
  2132. else
  2133. {
  2134. return 0;
  2135. }
  2136. }