cmTarget.cxx 76 KB

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