cmTarget.cxx 80 KB

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