cmTarget.cxx 88 KB

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