cmTarget.cxx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmTarget.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmGeneratorExpression.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmListFileCache.h"
  9. #include "cmMakefile.h"
  10. #include "cmMessenger.h"
  11. #include "cmOutputConverter.h"
  12. #include "cmProperty.h"
  13. #include "cmSourceFile.h"
  14. #include "cmSourceFileLocation.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTargetPropertyComputer.h"
  17. #include "cmake.h"
  18. #include <algorithm>
  19. #include <assert.h>
  20. #include <cmsys/RegularExpression.hxx>
  21. #include <map>
  22. #include <set>
  23. #include <sstream>
  24. #include <string.h>
  25. #if defined(CMake_HAVE_CXX_UNORDERED_SET)
  26. #include <unordered_set>
  27. #define UNORDERED_SET std::unordered_set
  28. #elif defined(CMAKE_BUILD_WITH_CMAKE)
  29. #include <cmsys/hash_set.hxx>
  30. #define UNORDERED_SET cmsys::hash_set
  31. #else
  32. #define UNORDERED_SET std::set
  33. #endif
  34. class cmTargetInternals
  35. {
  36. public:
  37. std::vector<std::string> IncludeDirectoriesEntries;
  38. std::vector<cmListFileBacktrace> IncludeDirectoriesBacktraces;
  39. std::vector<std::string> CompileOptionsEntries;
  40. std::vector<cmListFileBacktrace> CompileOptionsBacktraces;
  41. std::vector<std::string> CompileFeaturesEntries;
  42. std::vector<cmListFileBacktrace> CompileFeaturesBacktraces;
  43. std::vector<std::string> CompileDefinitionsEntries;
  44. std::vector<cmListFileBacktrace> CompileDefinitionsBacktraces;
  45. std::vector<std::string> SourceEntries;
  46. std::vector<cmListFileBacktrace> SourceBacktraces;
  47. std::vector<std::string> LinkImplementationPropertyEntries;
  48. std::vector<cmListFileBacktrace> LinkImplementationPropertyBacktraces;
  49. };
  50. cmTarget::cmTarget(std::string const& name, cmState::TargetType type,
  51. Visibility vis, cmMakefile* mf)
  52. {
  53. assert(mf);
  54. this->Name = name;
  55. this->TargetTypeValue = type;
  56. this->Makefile = mf;
  57. this->HaveInstallRule = false;
  58. this->DLLPlatform = false;
  59. this->IsAndroid = false;
  60. this->IsImportedTarget =
  61. (vis == VisibilityImported || vis == VisibilityImportedGlobally);
  62. this->ImportedGloballyVisible = vis == VisibilityImportedGlobally;
  63. this->BuildInterfaceIncludesAppended = false;
  64. // only add dependency information for library targets
  65. if (this->TargetTypeValue >= cmState::STATIC_LIBRARY &&
  66. this->TargetTypeValue <= cmState::MODULE_LIBRARY) {
  67. this->RecordDependencies = true;
  68. } else {
  69. this->RecordDependencies = false;
  70. }
  71. // Check whether this is a DLL platform.
  72. this->DLLPlatform =
  73. (this->Makefile->IsOn("WIN32") || this->Makefile->IsOn("CYGWIN") ||
  74. this->Makefile->IsOn("MINGW"));
  75. // Check whether we are targeting an Android platform.
  76. this->IsAndroid =
  77. strcmp(this->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME"),
  78. "Android") == 0;
  79. // Setup default property values.
  80. if (this->GetType() != cmState::INTERFACE_LIBRARY &&
  81. this->GetType() != cmState::UTILITY) {
  82. this->SetPropertyDefault("ANDROID_API", CM_NULLPTR);
  83. this->SetPropertyDefault("ANDROID_API_MIN", CM_NULLPTR);
  84. this->SetPropertyDefault("ANDROID_ARCH", CM_NULLPTR);
  85. this->SetPropertyDefault("ANDROID_STL_TYPE", CM_NULLPTR);
  86. this->SetPropertyDefault("ANDROID_SKIP_ANT_STEP", CM_NULLPTR);
  87. this->SetPropertyDefault("ANDROID_PROCESS_MAX", CM_NULLPTR);
  88. this->SetPropertyDefault("ANDROID_PROGUARD", CM_NULLPTR);
  89. this->SetPropertyDefault("ANDROID_PROGUARD_CONFIG_PATH", CM_NULLPTR);
  90. this->SetPropertyDefault("ANDROID_SECURE_PROPS_PATH", CM_NULLPTR);
  91. this->SetPropertyDefault("ANDROID_NATIVE_LIB_DIRECTORIES", CM_NULLPTR);
  92. this->SetPropertyDefault("ANDROID_NATIVE_LIB_DEPENDENCIES", CM_NULLPTR);
  93. this->SetPropertyDefault("ANDROID_JAVA_SOURCE_DIR", CM_NULLPTR);
  94. this->SetPropertyDefault("ANDROID_JAR_DIRECTORIES", CM_NULLPTR);
  95. this->SetPropertyDefault("ANDROID_JAR_DEPENDENCIES", CM_NULLPTR);
  96. this->SetPropertyDefault("ANDROID_ASSETS_DIRECTORIES", CM_NULLPTR);
  97. this->SetPropertyDefault("ANDROID_ANT_ADDITIONAL_OPTIONS", CM_NULLPTR);
  98. this->SetPropertyDefault("BUILD_RPATH", CM_NULLPTR);
  99. this->SetPropertyDefault("INSTALL_NAME_DIR", CM_NULLPTR);
  100. this->SetPropertyDefault("INSTALL_RPATH", "");
  101. this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
  102. this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
  103. this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
  104. this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", CM_NULLPTR);
  105. this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", CM_NULLPTR);
  106. this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", CM_NULLPTR);
  107. this->SetPropertyDefault("PDB_OUTPUT_DIRECTORY", CM_NULLPTR);
  108. this->SetPropertyDefault("COMPILE_PDB_OUTPUT_DIRECTORY", CM_NULLPTR);
  109. this->SetPropertyDefault("Fortran_FORMAT", CM_NULLPTR);
  110. this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", CM_NULLPTR);
  111. this->SetPropertyDefault("GNUtoMS", CM_NULLPTR);
  112. this->SetPropertyDefault("OSX_ARCHITECTURES", CM_NULLPTR);
  113. this->SetPropertyDefault("IOS_INSTALL_COMBINED", CM_NULLPTR);
  114. this->SetPropertyDefault("AUTOMOC", CM_NULLPTR);
  115. this->SetPropertyDefault("AUTOUIC", CM_NULLPTR);
  116. this->SetPropertyDefault("AUTORCC", CM_NULLPTR);
  117. this->SetPropertyDefault("AUTOMOC_MOC_OPTIONS", CM_NULLPTR);
  118. this->SetPropertyDefault("AUTOUIC_OPTIONS", CM_NULLPTR);
  119. this->SetPropertyDefault("AUTORCC_OPTIONS", CM_NULLPTR);
  120. this->SetPropertyDefault("LINK_DEPENDS_NO_SHARED", CM_NULLPTR);
  121. this->SetPropertyDefault("LINK_INTERFACE_LIBRARIES", CM_NULLPTR);
  122. this->SetPropertyDefault("WIN32_EXECUTABLE", CM_NULLPTR);
  123. this->SetPropertyDefault("MACOSX_BUNDLE", CM_NULLPTR);
  124. this->SetPropertyDefault("MACOSX_RPATH", CM_NULLPTR);
  125. this->SetPropertyDefault("NO_SYSTEM_FROM_IMPORTED", CM_NULLPTR);
  126. this->SetPropertyDefault("C_CLANG_TIDY", CM_NULLPTR);
  127. this->SetPropertyDefault("C_COMPILER_LAUNCHER", CM_NULLPTR);
  128. this->SetPropertyDefault("C_INCLUDE_WHAT_YOU_USE", CM_NULLPTR);
  129. this->SetPropertyDefault("LINK_WHAT_YOU_USE", CM_NULLPTR);
  130. this->SetPropertyDefault("C_STANDARD", CM_NULLPTR);
  131. this->SetPropertyDefault("C_STANDARD_REQUIRED", CM_NULLPTR);
  132. this->SetPropertyDefault("C_EXTENSIONS", CM_NULLPTR);
  133. this->SetPropertyDefault("CXX_CLANG_TIDY", CM_NULLPTR);
  134. this->SetPropertyDefault("CXX_COMPILER_LAUNCHER", CM_NULLPTR);
  135. this->SetPropertyDefault("CXX_INCLUDE_WHAT_YOU_USE", CM_NULLPTR);
  136. this->SetPropertyDefault("CXX_STANDARD", CM_NULLPTR);
  137. this->SetPropertyDefault("CXX_STANDARD_REQUIRED", CM_NULLPTR);
  138. this->SetPropertyDefault("CXX_EXTENSIONS", CM_NULLPTR);
  139. this->SetPropertyDefault("LINK_SEARCH_START_STATIC", CM_NULLPTR);
  140. this->SetPropertyDefault("LINK_SEARCH_END_STATIC", CM_NULLPTR);
  141. }
  142. // Collect the set of configuration types.
  143. std::vector<std::string> configNames;
  144. mf->GetConfigurations(configNames);
  145. // Setup per-configuration property default values.
  146. if (this->GetType() != cmState::UTILITY) {
  147. const char* configProps[] = {
  148. /* clang-format needs this comment to break after the opening brace */
  149. "ARCHIVE_OUTPUT_DIRECTORY_",
  150. "LIBRARY_OUTPUT_DIRECTORY_",
  151. "RUNTIME_OUTPUT_DIRECTORY_",
  152. "PDB_OUTPUT_DIRECTORY_",
  153. "COMPILE_PDB_OUTPUT_DIRECTORY_",
  154. "MAP_IMPORTED_CONFIG_",
  155. CM_NULLPTR
  156. };
  157. for (std::vector<std::string>::iterator ci = configNames.begin();
  158. ci != configNames.end(); ++ci) {
  159. std::string configUpper = cmSystemTools::UpperCase(*ci);
  160. for (const char** p = configProps; *p; ++p) {
  161. if (this->TargetTypeValue == cmState::INTERFACE_LIBRARY &&
  162. strcmp(*p, "MAP_IMPORTED_CONFIG_") != 0) {
  163. continue;
  164. }
  165. std::string property = *p;
  166. property += configUpper;
  167. this->SetPropertyDefault(property, CM_NULLPTR);
  168. }
  169. // Initialize per-configuration name postfix property from the
  170. // variable only for non-executable targets. This preserves
  171. // compatibility with previous CMake versions in which executables
  172. // did not support this variable. Projects may still specify the
  173. // property directly.
  174. if (this->TargetTypeValue != cmState::EXECUTABLE &&
  175. this->TargetTypeValue != cmState::INTERFACE_LIBRARY) {
  176. std::string property = cmSystemTools::UpperCase(*ci);
  177. property += "_POSTFIX";
  178. this->SetPropertyDefault(property, CM_NULLPTR);
  179. }
  180. }
  181. }
  182. // Save the backtrace of target construction.
  183. this->Backtrace = this->Makefile->GetBacktrace();
  184. if (!this->IsImported()) {
  185. // Initialize the INCLUDE_DIRECTORIES property based on the current value
  186. // of the same directory property:
  187. const cmStringRange parentIncludes =
  188. this->Makefile->GetIncludeDirectoriesEntries();
  189. const cmBacktraceRange parentIncludesBts =
  190. this->Makefile->GetIncludeDirectoriesBacktraces();
  191. this->Internal->IncludeDirectoriesEntries.insert(
  192. this->Internal->IncludeDirectoriesEntries.end(), parentIncludes.begin(),
  193. parentIncludes.end());
  194. this->Internal->IncludeDirectoriesBacktraces.insert(
  195. this->Internal->IncludeDirectoriesBacktraces.end(),
  196. parentIncludesBts.begin(), parentIncludesBts.end());
  197. const std::set<std::string> parentSystemIncludes =
  198. this->Makefile->GetSystemIncludeDirectories();
  199. this->SystemIncludeDirectories.insert(parentSystemIncludes.begin(),
  200. parentSystemIncludes.end());
  201. const cmStringRange parentOptions =
  202. this->Makefile->GetCompileOptionsEntries();
  203. const cmBacktraceRange parentOptionsBts =
  204. this->Makefile->GetCompileOptionsBacktraces();
  205. this->Internal->CompileOptionsEntries.insert(
  206. this->Internal->CompileOptionsEntries.end(), parentOptions.begin(),
  207. parentOptions.end());
  208. this->Internal->CompileOptionsBacktraces.insert(
  209. this->Internal->CompileOptionsBacktraces.end(), parentOptionsBts.begin(),
  210. parentOptionsBts.end());
  211. }
  212. if (this->GetType() != cmState::INTERFACE_LIBRARY &&
  213. this->GetType() != cmState::UTILITY) {
  214. this->SetPropertyDefault("C_VISIBILITY_PRESET", CM_NULLPTR);
  215. this->SetPropertyDefault("CXX_VISIBILITY_PRESET", CM_NULLPTR);
  216. this->SetPropertyDefault("VISIBILITY_INLINES_HIDDEN", CM_NULLPTR);
  217. }
  218. if (this->TargetTypeValue == cmState::EXECUTABLE) {
  219. this->SetPropertyDefault("ANDROID_GUI", CM_NULLPTR);
  220. this->SetPropertyDefault("CROSSCOMPILING_EMULATOR", CM_NULLPTR);
  221. this->SetPropertyDefault("ENABLE_EXPORTS", CM_NULLPTR);
  222. }
  223. if (this->TargetTypeValue == cmState::SHARED_LIBRARY ||
  224. this->TargetTypeValue == cmState::MODULE_LIBRARY) {
  225. this->SetProperty("POSITION_INDEPENDENT_CODE", "True");
  226. }
  227. if (this->TargetTypeValue == cmState::SHARED_LIBRARY ||
  228. this->TargetTypeValue == cmState::EXECUTABLE) {
  229. this->SetPropertyDefault("WINDOWS_EXPORT_ALL_SYMBOLS", CM_NULLPTR);
  230. }
  231. if (this->GetType() != cmState::INTERFACE_LIBRARY &&
  232. this->GetType() != cmState::UTILITY) {
  233. this->SetPropertyDefault("POSITION_INDEPENDENT_CODE", CM_NULLPTR);
  234. }
  235. // Record current policies for later use.
  236. this->Makefile->RecordPolicies(this->PolicyMap);
  237. if (this->TargetTypeValue == cmState::INTERFACE_LIBRARY) {
  238. // This policy is checked in a few conditions. The properties relevant
  239. // to the policy are always ignored for cmState::INTERFACE_LIBRARY targets,
  240. // so ensure that the conditions don't lead to nonsense.
  241. this->PolicyMap.Set(cmPolicies::CMP0022, cmPolicies::NEW);
  242. }
  243. if (this->GetType() != cmState::INTERFACE_LIBRARY &&
  244. this->GetType() != cmState::UTILITY) {
  245. this->SetPropertyDefault("JOB_POOL_COMPILE", CM_NULLPTR);
  246. this->SetPropertyDefault("JOB_POOL_LINK", CM_NULLPTR);
  247. }
  248. }
  249. void cmTarget::AddUtility(const std::string& u, cmMakefile* makefile)
  250. {
  251. if (this->Utilities.insert(u).second && makefile) {
  252. this->UtilityBacktraces.insert(
  253. std::make_pair(u, makefile->GetBacktrace()));
  254. }
  255. }
  256. cmListFileBacktrace const* cmTarget::GetUtilityBacktrace(
  257. const std::string& u) const
  258. {
  259. std::map<std::string, cmListFileBacktrace>::const_iterator i =
  260. this->UtilityBacktraces.find(u);
  261. if (i == this->UtilityBacktraces.end()) {
  262. return CM_NULLPTR;
  263. }
  264. return &i->second;
  265. }
  266. cmListFileBacktrace const& cmTarget::GetBacktrace() const
  267. {
  268. return this->Backtrace;
  269. }
  270. bool cmTarget::IsExecutableWithExports() const
  271. {
  272. return (this->GetType() == cmState::EXECUTABLE &&
  273. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  274. }
  275. bool cmTarget::HasImportLibrary() const
  276. {
  277. return (this->DLLPlatform && (this->GetType() == cmState::SHARED_LIBRARY ||
  278. this->IsExecutableWithExports()));
  279. }
  280. bool cmTarget::IsFrameworkOnApple() const
  281. {
  282. return (this->GetType() == cmState::SHARED_LIBRARY &&
  283. this->Makefile->IsOn("APPLE") &&
  284. this->GetPropertyAsBool("FRAMEWORK"));
  285. }
  286. bool cmTarget::IsAppBundleOnApple() const
  287. {
  288. return (this->GetType() == cmState::EXECUTABLE &&
  289. this->Makefile->IsOn("APPLE") &&
  290. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  291. }
  292. void cmTarget::AddTracedSources(std::vector<std::string> const& srcs)
  293. {
  294. if (!srcs.empty()) {
  295. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  296. this->Internal->SourceEntries.push_back(cmJoin(srcs, ";"));
  297. this->Internal->SourceBacktraces.push_back(lfbt);
  298. }
  299. }
  300. void cmTarget::AddSources(std::vector<std::string> const& srcs)
  301. {
  302. std::string srcFiles;
  303. const char* sep = "";
  304. for (std::vector<std::string>::const_iterator i = srcs.begin();
  305. i != srcs.end(); ++i) {
  306. std::string filename = *i;
  307. const char* src = filename.c_str();
  308. if (!(src[0] == '$' && src[1] == '<')) {
  309. if (!filename.empty()) {
  310. filename = this->ProcessSourceItemCMP0049(filename);
  311. if (filename.empty()) {
  312. return;
  313. }
  314. }
  315. this->Makefile->GetOrCreateSource(filename);
  316. }
  317. srcFiles += sep;
  318. srcFiles += filename;
  319. sep = ";";
  320. }
  321. if (!srcFiles.empty()) {
  322. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  323. this->Internal->SourceEntries.push_back(srcFiles);
  324. this->Internal->SourceBacktraces.push_back(lfbt);
  325. }
  326. }
  327. std::string cmTarget::ProcessSourceItemCMP0049(const std::string& s)
  328. {
  329. std::string src = s;
  330. // For backwards compatibility replace varibles in source names.
  331. // This should eventually be removed.
  332. this->Makefile->ExpandVariablesInString(src);
  333. if (src != s) {
  334. std::ostringstream e;
  335. bool noMessage = false;
  336. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  337. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0049)) {
  338. case cmPolicies::WARN:
  339. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0049) << "\n";
  340. break;
  341. case cmPolicies::OLD:
  342. noMessage = true;
  343. break;
  344. case cmPolicies::REQUIRED_ALWAYS:
  345. case cmPolicies::REQUIRED_IF_USED:
  346. case cmPolicies::NEW:
  347. messageType = cmake::FATAL_ERROR;
  348. }
  349. if (!noMessage) {
  350. e << "Legacy variable expansion in source file \"" << s
  351. << "\" expanded to \"" << src << "\" in target \"" << this->GetName()
  352. << "\". This behavior will be removed in a "
  353. "future version of CMake.";
  354. this->Makefile->IssueMessage(messageType, e.str());
  355. if (messageType == cmake::FATAL_ERROR) {
  356. return "";
  357. }
  358. }
  359. }
  360. return src;
  361. }
  362. cmSourceFile* cmTarget::AddSourceCMP0049(const std::string& s)
  363. {
  364. std::string src = this->ProcessSourceItemCMP0049(s);
  365. if (!s.empty() && src.empty()) {
  366. return CM_NULLPTR;
  367. }
  368. return this->AddSource(src);
  369. }
  370. struct CreateLocation
  371. {
  372. cmMakefile const* Makefile;
  373. CreateLocation(cmMakefile const* mf)
  374. : Makefile(mf)
  375. {
  376. }
  377. cmSourceFileLocation operator()(const std::string& filename)
  378. {
  379. return cmSourceFileLocation(this->Makefile, filename);
  380. }
  381. };
  382. struct LocationMatcher
  383. {
  384. const cmSourceFileLocation& Needle;
  385. LocationMatcher(const cmSourceFileLocation& needle)
  386. : Needle(needle)
  387. {
  388. }
  389. bool operator()(cmSourceFileLocation& loc)
  390. {
  391. return loc.Matches(this->Needle);
  392. }
  393. };
  394. struct TargetPropertyEntryFinder
  395. {
  396. private:
  397. const cmSourceFileLocation& Needle;
  398. public:
  399. TargetPropertyEntryFinder(const cmSourceFileLocation& needle)
  400. : Needle(needle)
  401. {
  402. }
  403. bool operator()(std::string const& entry)
  404. {
  405. std::vector<std::string> files;
  406. cmSystemTools::ExpandListArgument(entry, files);
  407. std::vector<cmSourceFileLocation> locations(files.size());
  408. std::transform(files.begin(), files.end(), locations.begin(),
  409. CreateLocation(this->Needle.GetMakefile()));
  410. return std::find_if(locations.begin(), locations.end(),
  411. LocationMatcher(this->Needle)) != locations.end();
  412. }
  413. };
  414. cmSourceFile* cmTarget::AddSource(const std::string& src)
  415. {
  416. cmSourceFileLocation sfl(this->Makefile, src);
  417. if (std::find_if(this->Internal->SourceEntries.begin(),
  418. this->Internal->SourceEntries.end(),
  419. TargetPropertyEntryFinder(sfl)) ==
  420. this->Internal->SourceEntries.end()) {
  421. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  422. this->Internal->SourceEntries.push_back(src);
  423. this->Internal->SourceBacktraces.push_back(lfbt);
  424. }
  425. if (cmGeneratorExpression::Find(src) != std::string::npos) {
  426. return CM_NULLPTR;
  427. }
  428. return this->Makefile->GetOrCreateSource(src);
  429. }
  430. void cmTarget::AddLinkDirectory(const std::string& d)
  431. {
  432. // Make sure we don't add unnecessary search directories.
  433. if (this->LinkDirectoriesEmmitted.insert(d).second) {
  434. this->LinkDirectories.push_back(d);
  435. }
  436. }
  437. const std::vector<std::string>& cmTarget::GetLinkDirectories() const
  438. {
  439. return this->LinkDirectories;
  440. }
  441. void cmTarget::ClearDependencyInformation(cmMakefile& mf,
  442. const std::string& target)
  443. {
  444. // Clear the dependencies. The cache variable must exist iff we are
  445. // recording dependency information for this target.
  446. std::string depname = target;
  447. depname += "_LIB_DEPENDS";
  448. if (this->RecordDependencies) {
  449. mf.AddCacheDefinition(depname, "", "Dependencies for target",
  450. cmState::STATIC);
  451. } else {
  452. if (mf.GetDefinition(depname)) {
  453. std::string message = "Target ";
  454. message += target;
  455. message += " has dependency information when it shouldn't.\n";
  456. message += "Your cache is probably stale. Please remove the entry\n ";
  457. message += depname;
  458. message += "\nfrom the cache.";
  459. cmSystemTools::Error(message.c_str());
  460. }
  461. }
  462. }
  463. std::string cmTarget::GetDebugGeneratorExpressions(
  464. const std::string& value, cmTargetLinkLibraryType llt) const
  465. {
  466. if (llt == GENERAL_LibraryType) {
  467. return value;
  468. }
  469. // Get the list of configurations considered to be DEBUG.
  470. std::vector<std::string> debugConfigs =
  471. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  472. std::string configString = "$<CONFIG:" + debugConfigs[0] + ">";
  473. if (debugConfigs.size() > 1) {
  474. for (std::vector<std::string>::const_iterator li =
  475. debugConfigs.begin() + 1;
  476. li != debugConfigs.end(); ++li) {
  477. configString += ",$<CONFIG:" + *li + ">";
  478. }
  479. configString = "$<OR:" + configString + ">";
  480. }
  481. if (llt == OPTIMIZED_LibraryType) {
  482. configString = "$<NOT:" + configString + ">";
  483. }
  484. return "$<" + configString + ":" + value + ">";
  485. }
  486. static std::string targetNameGenex(const std::string& lib)
  487. {
  488. return "$<TARGET_NAME:" + lib + ">";
  489. }
  490. bool cmTarget::PushTLLCommandTrace(TLLSignature signature,
  491. cmListFileContext const& lfc)
  492. {
  493. bool ret = true;
  494. if (!this->TLLCommands.empty()) {
  495. if (this->TLLCommands.back().first != signature) {
  496. ret = false;
  497. }
  498. }
  499. if (this->TLLCommands.empty() || this->TLLCommands.back().second != lfc) {
  500. this->TLLCommands.push_back(std::make_pair(signature, lfc));
  501. }
  502. return ret;
  503. }
  504. void cmTarget::GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const
  505. {
  506. const char* sigString =
  507. (sig == cmTarget::KeywordTLLSignature ? "keyword" : "plain");
  508. s << "The uses of the " << sigString << " signature are here:\n";
  509. typedef std::vector<std::pair<TLLSignature, cmListFileContext> > Container;
  510. cmOutputConverter converter(this->GetMakefile()->GetStateSnapshot());
  511. for (Container::const_iterator it = this->TLLCommands.begin();
  512. it != this->TLLCommands.end(); ++it) {
  513. if (it->first == sig) {
  514. cmListFileContext lfc = it->second;
  515. lfc.FilePath = converter.ConvertToRelativePath(
  516. this->Makefile->GetState()->GetSourceDirectory(), lfc.FilePath);
  517. s << " * " << lfc << std::endl;
  518. }
  519. }
  520. }
  521. void cmTarget::AddLinkLibrary(cmMakefile& mf, const std::string& lib,
  522. cmTargetLinkLibraryType llt)
  523. {
  524. cmTarget* tgt = this->Makefile->FindTargetToUse(lib);
  525. {
  526. const bool isNonImportedTarget = tgt && !tgt->IsImported();
  527. const std::string libName =
  528. (isNonImportedTarget && llt != GENERAL_LibraryType)
  529. ? targetNameGenex(lib)
  530. : lib;
  531. this->AppendProperty(
  532. "LINK_LIBRARIES",
  533. this->GetDebugGeneratorExpressions(libName, llt).c_str());
  534. }
  535. if (cmGeneratorExpression::Find(lib) != std::string::npos ||
  536. (tgt && tgt->GetType() == cmState::INTERFACE_LIBRARY) ||
  537. (this->Name == lib)) {
  538. return;
  539. }
  540. cmTarget::LibraryID tmp;
  541. tmp.first = lib;
  542. tmp.second = llt;
  543. this->OriginalLinkLibraries.push_back(tmp);
  544. // Add the explicit dependency information for this target. This is
  545. // simply a set of libraries separated by ";". There should always
  546. // be a trailing ";". These library names are not canonical, in that
  547. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  548. // We shouldn't remove duplicates here because external libraries
  549. // may be purposefully duplicated to handle recursive dependencies,
  550. // and we removing one instance will break the link line. Duplicates
  551. // will be appropriately eliminated at emit time.
  552. if (this->RecordDependencies) {
  553. std::string targetEntry = this->Name;
  554. targetEntry += "_LIB_DEPENDS";
  555. std::string dependencies;
  556. const char* old_val = mf.GetDefinition(targetEntry);
  557. if (old_val) {
  558. dependencies += old_val;
  559. }
  560. switch (llt) {
  561. case GENERAL_LibraryType:
  562. dependencies += "general";
  563. break;
  564. case DEBUG_LibraryType:
  565. dependencies += "debug";
  566. break;
  567. case OPTIMIZED_LibraryType:
  568. dependencies += "optimized";
  569. break;
  570. }
  571. dependencies += ";";
  572. dependencies += lib;
  573. dependencies += ";";
  574. mf.AddCacheDefinition(targetEntry, dependencies.c_str(),
  575. "Dependencies for the target", cmState::STATIC);
  576. }
  577. }
  578. void cmTarget::AddSystemIncludeDirectories(const std::set<std::string>& incs)
  579. {
  580. this->SystemIncludeDirectories.insert(incs.begin(), incs.end());
  581. }
  582. cmStringRange cmTarget::GetIncludeDirectoriesEntries() const
  583. {
  584. return cmMakeRange(this->Internal->IncludeDirectoriesEntries);
  585. }
  586. cmBacktraceRange cmTarget::GetIncludeDirectoriesBacktraces() const
  587. {
  588. return cmMakeRange(this->Internal->IncludeDirectoriesBacktraces);
  589. }
  590. cmStringRange cmTarget::GetCompileOptionsEntries() const
  591. {
  592. return cmMakeRange(this->Internal->CompileOptionsEntries);
  593. }
  594. cmBacktraceRange cmTarget::GetCompileOptionsBacktraces() const
  595. {
  596. return cmMakeRange(this->Internal->CompileOptionsBacktraces);
  597. }
  598. cmStringRange cmTarget::GetCompileFeaturesEntries() const
  599. {
  600. return cmMakeRange(this->Internal->CompileFeaturesEntries);
  601. }
  602. cmBacktraceRange cmTarget::GetCompileFeaturesBacktraces() const
  603. {
  604. return cmMakeRange(this->Internal->CompileFeaturesBacktraces);
  605. }
  606. cmStringRange cmTarget::GetCompileDefinitionsEntries() const
  607. {
  608. return cmMakeRange(this->Internal->CompileDefinitionsEntries);
  609. }
  610. cmBacktraceRange cmTarget::GetCompileDefinitionsBacktraces() const
  611. {
  612. return cmMakeRange(this->Internal->CompileDefinitionsBacktraces);
  613. }
  614. cmStringRange cmTarget::GetSourceEntries() const
  615. {
  616. return cmMakeRange(this->Internal->SourceEntries);
  617. }
  618. cmBacktraceRange cmTarget::GetSourceBacktraces() const
  619. {
  620. return cmMakeRange(this->Internal->SourceBacktraces);
  621. }
  622. cmStringRange cmTarget::GetLinkImplementationEntries() const
  623. {
  624. return cmMakeRange(this->Internal->LinkImplementationPropertyEntries);
  625. }
  626. cmBacktraceRange cmTarget::GetLinkImplementationBacktraces() const
  627. {
  628. return cmMakeRange(this->Internal->LinkImplementationPropertyBacktraces);
  629. }
  630. void cmTarget::SetProperty(const std::string& prop, const char* value)
  631. {
  632. if (!cmTargetPropertyComputer::PassesWhitelist(
  633. this->GetType(), prop, this->Makefile->GetMessenger(),
  634. this->Makefile->GetBacktrace())) {
  635. return;
  636. }
  637. if (prop == "NAME") {
  638. std::ostringstream e;
  639. e << "NAME property is read-only\n";
  640. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  641. return;
  642. }
  643. if (prop == "INCLUDE_DIRECTORIES") {
  644. this->Internal->IncludeDirectoriesEntries.clear();
  645. this->Internal->IncludeDirectoriesBacktraces.clear();
  646. if (value) {
  647. this->Internal->IncludeDirectoriesEntries.push_back(value);
  648. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  649. this->Internal->IncludeDirectoriesBacktraces.push_back(lfbt);
  650. }
  651. } else if (prop == "COMPILE_OPTIONS") {
  652. this->Internal->CompileOptionsEntries.clear();
  653. this->Internal->CompileOptionsBacktraces.clear();
  654. if (value) {
  655. this->Internal->CompileOptionsEntries.push_back(value);
  656. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  657. this->Internal->CompileOptionsBacktraces.push_back(lfbt);
  658. }
  659. } else if (prop == "COMPILE_FEATURES") {
  660. this->Internal->CompileFeaturesEntries.clear();
  661. this->Internal->CompileFeaturesBacktraces.clear();
  662. if (value) {
  663. this->Internal->CompileFeaturesEntries.push_back(value);
  664. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  665. this->Internal->CompileFeaturesBacktraces.push_back(lfbt);
  666. }
  667. } else if (prop == "COMPILE_DEFINITIONS") {
  668. this->Internal->CompileDefinitionsEntries.clear();
  669. this->Internal->CompileDefinitionsBacktraces.clear();
  670. if (value) {
  671. this->Internal->CompileDefinitionsEntries.push_back(value);
  672. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  673. this->Internal->CompileDefinitionsBacktraces.push_back(lfbt);
  674. }
  675. } else if (prop == "EXPORT_NAME" && this->IsImported()) {
  676. std::ostringstream e;
  677. e << "EXPORT_NAME property can't be set on imported targets (\""
  678. << this->Name << "\")\n";
  679. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  680. } else if (prop == "LINK_LIBRARIES") {
  681. this->Internal->LinkImplementationPropertyEntries.clear();
  682. this->Internal->LinkImplementationPropertyBacktraces.clear();
  683. if (value) {
  684. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  685. this->Internal->LinkImplementationPropertyEntries.push_back(value);
  686. this->Internal->LinkImplementationPropertyBacktraces.push_back(lfbt);
  687. }
  688. } else if (prop == "SOURCES") {
  689. if (this->IsImported()) {
  690. std::ostringstream e;
  691. e << "SOURCES property can't be set on imported targets (\""
  692. << this->Name << "\")\n";
  693. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  694. return;
  695. }
  696. this->Internal->SourceEntries.clear();
  697. this->Internal->SourceBacktraces.clear();
  698. if (value) {
  699. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  700. this->Internal->SourceEntries.push_back(value);
  701. this->Internal->SourceBacktraces.push_back(lfbt);
  702. }
  703. } else {
  704. this->Properties.SetProperty(prop, value);
  705. }
  706. }
  707. void cmTarget::AppendProperty(const std::string& prop, const char* value,
  708. bool asString)
  709. {
  710. if (!cmTargetPropertyComputer::PassesWhitelist(
  711. this->GetType(), prop, this->Makefile->GetMessenger(),
  712. this->Makefile->GetBacktrace())) {
  713. return;
  714. }
  715. if (prop == "NAME") {
  716. std::ostringstream e;
  717. e << "NAME property is read-only\n";
  718. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  719. return;
  720. }
  721. if (prop == "INCLUDE_DIRECTORIES") {
  722. if (value && *value) {
  723. this->Internal->IncludeDirectoriesEntries.push_back(value);
  724. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  725. this->Internal->IncludeDirectoriesBacktraces.push_back(lfbt);
  726. }
  727. } else if (prop == "COMPILE_OPTIONS") {
  728. if (value && *value) {
  729. this->Internal->CompileOptionsEntries.push_back(value);
  730. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  731. this->Internal->CompileOptionsBacktraces.push_back(lfbt);
  732. }
  733. } else if (prop == "COMPILE_FEATURES") {
  734. if (value && *value) {
  735. this->Internal->CompileFeaturesEntries.push_back(value);
  736. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  737. this->Internal->CompileFeaturesBacktraces.push_back(lfbt);
  738. }
  739. } else if (prop == "COMPILE_DEFINITIONS") {
  740. if (value && *value) {
  741. this->Internal->CompileDefinitionsEntries.push_back(value);
  742. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  743. this->Internal->CompileDefinitionsBacktraces.push_back(lfbt);
  744. }
  745. } else if (prop == "EXPORT_NAME" && this->IsImported()) {
  746. std::ostringstream e;
  747. e << "EXPORT_NAME property can't be set on imported targets (\""
  748. << this->Name << "\")\n";
  749. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  750. } else if (prop == "LINK_LIBRARIES") {
  751. if (value && *value) {
  752. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  753. this->Internal->LinkImplementationPropertyEntries.push_back(value);
  754. this->Internal->LinkImplementationPropertyBacktraces.push_back(lfbt);
  755. }
  756. } else if (prop == "SOURCES") {
  757. if (this->IsImported()) {
  758. std::ostringstream e;
  759. e << "SOURCES property can't be set on imported targets (\""
  760. << this->Name << "\")\n";
  761. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  762. return;
  763. }
  764. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  765. this->Internal->SourceEntries.push_back(value);
  766. this->Internal->SourceBacktraces.push_back(lfbt);
  767. } else {
  768. this->Properties.AppendProperty(prop, value, asString);
  769. }
  770. }
  771. void cmTarget::AppendBuildInterfaceIncludes()
  772. {
  773. if (this->GetType() != cmState::SHARED_LIBRARY &&
  774. this->GetType() != cmState::STATIC_LIBRARY &&
  775. this->GetType() != cmState::MODULE_LIBRARY &&
  776. this->GetType() != cmState::INTERFACE_LIBRARY &&
  777. !this->IsExecutableWithExports()) {
  778. return;
  779. }
  780. if (this->BuildInterfaceIncludesAppended) {
  781. return;
  782. }
  783. this->BuildInterfaceIncludesAppended = true;
  784. if (this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE")) {
  785. const char* binDir = this->Makefile->GetCurrentBinaryDirectory();
  786. const char* srcDir = this->Makefile->GetCurrentSourceDirectory();
  787. const std::string dirs = std::string(binDir ? binDir : "") +
  788. std::string(binDir ? ";" : "") + std::string(srcDir ? srcDir : "");
  789. if (!dirs.empty()) {
  790. this->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  791. ("$<BUILD_INTERFACE:" + dirs + ">").c_str());
  792. }
  793. }
  794. }
  795. void cmTarget::InsertInclude(std::string const& entry,
  796. cmListFileBacktrace const& bt, bool before)
  797. {
  798. std::vector<std::string>::iterator position = before
  799. ? this->Internal->IncludeDirectoriesEntries.begin()
  800. : this->Internal->IncludeDirectoriesEntries.end();
  801. std::vector<cmListFileBacktrace>::iterator btPosition = before
  802. ? this->Internal->IncludeDirectoriesBacktraces.begin()
  803. : this->Internal->IncludeDirectoriesBacktraces.end();
  804. this->Internal->IncludeDirectoriesEntries.insert(position, entry);
  805. this->Internal->IncludeDirectoriesBacktraces.insert(btPosition, bt);
  806. }
  807. void cmTarget::InsertCompileOption(std::string const& entry,
  808. cmListFileBacktrace const& bt, bool before)
  809. {
  810. std::vector<std::string>::iterator position = before
  811. ? this->Internal->CompileOptionsEntries.begin()
  812. : this->Internal->CompileOptionsEntries.end();
  813. std::vector<cmListFileBacktrace>::iterator btPosition = before
  814. ? this->Internal->CompileOptionsBacktraces.begin()
  815. : this->Internal->CompileOptionsBacktraces.end();
  816. this->Internal->CompileOptionsEntries.insert(position, entry);
  817. this->Internal->CompileOptionsBacktraces.insert(btPosition, bt);
  818. }
  819. void cmTarget::InsertCompileDefinition(std::string const& entry,
  820. cmListFileBacktrace const& bt)
  821. {
  822. this->Internal->CompileDefinitionsEntries.push_back(entry);
  823. this->Internal->CompileDefinitionsBacktraces.push_back(bt);
  824. }
  825. static void cmTargetCheckLINK_INTERFACE_LIBRARIES(const std::string& prop,
  826. const char* value,
  827. cmMakefile* context,
  828. bool imported)
  829. {
  830. // Look for link-type keywords in the value.
  831. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  832. if (!keys.find(value)) {
  833. return;
  834. }
  835. // Support imported and non-imported versions of the property.
  836. const char* base = (imported ? "IMPORTED_LINK_INTERFACE_LIBRARIES"
  837. : "LINK_INTERFACE_LIBRARIES");
  838. // Report an error.
  839. std::ostringstream e;
  840. e << "Property " << prop << " may not contain link-type keyword \""
  841. << keys.match(2) << "\". "
  842. << "The " << base << " property has a per-configuration "
  843. << "version called " << base << "_<CONFIG> which may be "
  844. << "used to specify per-configuration rules.";
  845. if (!imported) {
  846. e << " "
  847. << "Alternatively, an IMPORTED library may be created, configured "
  848. << "with a per-configuration location, and then named in the "
  849. << "property value. "
  850. << "See the add_library command's IMPORTED mode for details."
  851. << "\n"
  852. << "If you have a list of libraries that already contains the "
  853. << "keyword, use the target_link_libraries command with its "
  854. << "LINK_INTERFACE_LIBRARIES mode to set the property. "
  855. << "The command automatically recognizes link-type keywords and sets "
  856. << "the LINK_INTERFACE_LIBRARIES and LINK_INTERFACE_LIBRARIES_DEBUG "
  857. << "properties accordingly.";
  858. }
  859. context->IssueMessage(cmake::FATAL_ERROR, e.str());
  860. }
  861. static void cmTargetCheckINTERFACE_LINK_LIBRARIES(const char* value,
  862. cmMakefile* context)
  863. {
  864. // Look for link-type keywords in the value.
  865. static cmsys::RegularExpression keys("(^|;)(debug|optimized|general)(;|$)");
  866. if (!keys.find(value)) {
  867. return;
  868. }
  869. // Report an error.
  870. std::ostringstream e;
  871. e << "Property INTERFACE_LINK_LIBRARIES may not contain link-type "
  872. "keyword \""
  873. << keys.match(2)
  874. << "\". The INTERFACE_LINK_LIBRARIES "
  875. "property may contain configuration-sensitive generator-expressions "
  876. "which may be used to specify per-configuration rules.";
  877. context->IssueMessage(cmake::FATAL_ERROR, e.str());
  878. }
  879. void cmTarget::CheckProperty(const std::string& prop,
  880. cmMakefile* context) const
  881. {
  882. // Certain properties need checking.
  883. if (cmHasLiteralPrefix(prop, "LINK_INTERFACE_LIBRARIES")) {
  884. if (const char* value = this->GetProperty(prop)) {
  885. cmTargetCheckLINK_INTERFACE_LIBRARIES(prop, value, context, false);
  886. }
  887. }
  888. if (cmHasLiteralPrefix(prop, "IMPORTED_LINK_INTERFACE_LIBRARIES")) {
  889. if (const char* value = this->GetProperty(prop)) {
  890. cmTargetCheckLINK_INTERFACE_LIBRARIES(prop, value, context, true);
  891. }
  892. }
  893. if (cmHasLiteralPrefix(prop, "INTERFACE_LINK_LIBRARIES")) {
  894. if (const char* value = this->GetProperty(prop)) {
  895. cmTargetCheckINTERFACE_LINK_LIBRARIES(value, context);
  896. }
  897. }
  898. }
  899. const char* cmTarget::GetProperty(const std::string& prop) const
  900. {
  901. return this->GetProperty(prop, this->Makefile);
  902. }
  903. const char* cmTarget::GetProperty(const std::string& prop,
  904. cmMakefile* context) const
  905. {
  906. if (!cmTargetPropertyComputer::PassesWhitelist(this->GetType(), prop,
  907. context->GetMessenger(),
  908. context->GetBacktrace())) {
  909. return CM_NULLPTR;
  910. }
  911. if (const char* result = cmTargetPropertyComputer::GetProperty(
  912. this, prop, context->GetMessenger(), context->GetBacktrace())) {
  913. return result;
  914. }
  915. if (cmSystemTools::GetFatalErrorOccured()) {
  916. return CM_NULLPTR;
  917. }
  918. static UNORDERED_SET<std::string> specialProps;
  919. #define MAKE_STATIC_PROP(PROP) static const std::string prop##PROP = #PROP
  920. MAKE_STATIC_PROP(LINK_LIBRARIES);
  921. MAKE_STATIC_PROP(TYPE);
  922. MAKE_STATIC_PROP(INCLUDE_DIRECTORIES);
  923. MAKE_STATIC_PROP(COMPILE_FEATURES);
  924. MAKE_STATIC_PROP(COMPILE_OPTIONS);
  925. MAKE_STATIC_PROP(COMPILE_DEFINITIONS);
  926. MAKE_STATIC_PROP(IMPORTED);
  927. MAKE_STATIC_PROP(NAME);
  928. MAKE_STATIC_PROP(BINARY_DIR);
  929. MAKE_STATIC_PROP(SOURCE_DIR);
  930. MAKE_STATIC_PROP(SOURCES);
  931. #undef MAKE_STATIC_PROP
  932. if (specialProps.empty()) {
  933. specialProps.insert(propLINK_LIBRARIES);
  934. specialProps.insert(propTYPE);
  935. specialProps.insert(propINCLUDE_DIRECTORIES);
  936. specialProps.insert(propCOMPILE_FEATURES);
  937. specialProps.insert(propCOMPILE_OPTIONS);
  938. specialProps.insert(propCOMPILE_DEFINITIONS);
  939. specialProps.insert(propIMPORTED);
  940. specialProps.insert(propNAME);
  941. specialProps.insert(propBINARY_DIR);
  942. specialProps.insert(propSOURCE_DIR);
  943. specialProps.insert(propSOURCES);
  944. }
  945. if (specialProps.count(prop)) {
  946. if (prop == propLINK_LIBRARIES) {
  947. if (this->Internal->LinkImplementationPropertyEntries.empty()) {
  948. return CM_NULLPTR;
  949. }
  950. static std::string output;
  951. output = cmJoin(this->Internal->LinkImplementationPropertyEntries, ";");
  952. return output.c_str();
  953. }
  954. // the type property returns what type the target is
  955. if (prop == propTYPE) {
  956. return cmState::GetTargetTypeName(this->GetType());
  957. }
  958. if (prop == propINCLUDE_DIRECTORIES) {
  959. if (this->Internal->IncludeDirectoriesEntries.empty()) {
  960. return CM_NULLPTR;
  961. }
  962. static std::string output;
  963. output = cmJoin(this->Internal->IncludeDirectoriesEntries, ";");
  964. return output.c_str();
  965. }
  966. if (prop == propCOMPILE_FEATURES) {
  967. if (this->Internal->CompileFeaturesEntries.empty()) {
  968. return CM_NULLPTR;
  969. }
  970. static std::string output;
  971. output = cmJoin(this->Internal->CompileFeaturesEntries, ";");
  972. return output.c_str();
  973. }
  974. if (prop == propCOMPILE_OPTIONS) {
  975. if (this->Internal->CompileOptionsEntries.empty()) {
  976. return CM_NULLPTR;
  977. }
  978. static std::string output;
  979. output = cmJoin(this->Internal->CompileOptionsEntries, ";");
  980. return output.c_str();
  981. }
  982. if (prop == propCOMPILE_DEFINITIONS) {
  983. if (this->Internal->CompileDefinitionsEntries.empty()) {
  984. return CM_NULLPTR;
  985. }
  986. static std::string output;
  987. output = cmJoin(this->Internal->CompileDefinitionsEntries, ";");
  988. return output.c_str();
  989. }
  990. if (prop == propIMPORTED) {
  991. return this->IsImported() ? "TRUE" : "FALSE";
  992. }
  993. if (prop == propNAME) {
  994. return this->GetName().c_str();
  995. }
  996. if (prop == propBINARY_DIR) {
  997. return this->GetMakefile()->GetCurrentBinaryDirectory();
  998. }
  999. if (prop == propSOURCE_DIR) {
  1000. return this->GetMakefile()->GetCurrentSourceDirectory();
  1001. }
  1002. }
  1003. const char* retVal = this->Properties.GetPropertyValue(prop);
  1004. if (!retVal) {
  1005. const bool chain = this->GetMakefile()->GetState()->IsPropertyChained(
  1006. prop, cmProperty::TARGET);
  1007. if (chain) {
  1008. return this->Makefile->GetProperty(prop, chain);
  1009. }
  1010. }
  1011. return retVal;
  1012. }
  1013. bool cmTarget::GetPropertyAsBool(const std::string& prop) const
  1014. {
  1015. return cmSystemTools::IsOn(this->GetProperty(prop));
  1016. }
  1017. const char* cmTarget::GetSuffixVariableInternal(bool implib) const
  1018. {
  1019. switch (this->GetType()) {
  1020. case cmState::STATIC_LIBRARY:
  1021. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  1022. case cmState::SHARED_LIBRARY:
  1023. return (implib ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  1024. : "CMAKE_SHARED_LIBRARY_SUFFIX");
  1025. case cmState::MODULE_LIBRARY:
  1026. return (implib ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  1027. : "CMAKE_SHARED_MODULE_SUFFIX");
  1028. case cmState::EXECUTABLE:
  1029. return (implib
  1030. ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  1031. // Android GUI application packages store the native
  1032. // binary as a shared library.
  1033. : (this->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI")
  1034. ? "CMAKE_SHARED_LIBRARY_SUFFIX"
  1035. : "CMAKE_EXECUTABLE_SUFFIX"));
  1036. default:
  1037. break;
  1038. }
  1039. return "";
  1040. }
  1041. const char* cmTarget::GetPrefixVariableInternal(bool implib) const
  1042. {
  1043. switch (this->GetType()) {
  1044. case cmState::STATIC_LIBRARY:
  1045. return "CMAKE_STATIC_LIBRARY_PREFIX";
  1046. case cmState::SHARED_LIBRARY:
  1047. return (implib ? "CMAKE_IMPORT_LIBRARY_PREFIX"
  1048. : "CMAKE_SHARED_LIBRARY_PREFIX");
  1049. case cmState::MODULE_LIBRARY:
  1050. return (implib ? "CMAKE_IMPORT_LIBRARY_PREFIX"
  1051. : "CMAKE_SHARED_MODULE_PREFIX");
  1052. case cmState::EXECUTABLE:
  1053. return (implib
  1054. ? "CMAKE_IMPORT_LIBRARY_PREFIX"
  1055. // Android GUI application packages store the native
  1056. // binary as a shared library.
  1057. : (this->IsAndroid && this->GetPropertyAsBool("ANDROID_GUI")
  1058. ? "CMAKE_SHARED_LIBRARY_PREFIX"
  1059. : ""));
  1060. default:
  1061. break;
  1062. }
  1063. return "";
  1064. }
  1065. std::string cmTarget::ImportedGetFullPath(const std::string& config,
  1066. bool pimplib) const
  1067. {
  1068. assert(this->IsImported());
  1069. // Lookup/compute/cache the import information for this
  1070. // configuration.
  1071. std::string config_upper;
  1072. if (!config.empty()) {
  1073. config_upper = cmSystemTools::UpperCase(config);
  1074. } else {
  1075. config_upper = "NOCONFIG";
  1076. }
  1077. std::string result;
  1078. const char* loc = CM_NULLPTR;
  1079. const char* imp = CM_NULLPTR;
  1080. std::string suffix;
  1081. if (this->GetType() != cmState::INTERFACE_LIBRARY &&
  1082. this->GetMappedConfig(config_upper, &loc, &imp, suffix)) {
  1083. if (!pimplib) {
  1084. if (loc) {
  1085. result = loc;
  1086. } else {
  1087. std::string impProp = "IMPORTED_LOCATION";
  1088. impProp += suffix;
  1089. if (const char* config_location = this->GetProperty(impProp)) {
  1090. result = config_location;
  1091. } else if (const char* location =
  1092. this->GetProperty("IMPORTED_LOCATION")) {
  1093. result = location;
  1094. }
  1095. }
  1096. } else {
  1097. if (imp) {
  1098. result = imp;
  1099. } else if (this->GetType() == cmState::SHARED_LIBRARY ||
  1100. this->IsExecutableWithExports()) {
  1101. std::string impProp = "IMPORTED_IMPLIB";
  1102. impProp += suffix;
  1103. if (const char* config_implib = this->GetProperty(impProp)) {
  1104. result = config_implib;
  1105. } else if (const char* implib = this->GetProperty("IMPORTED_IMPLIB")) {
  1106. result = implib;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. if (result.empty()) {
  1112. result = this->GetName();
  1113. result += "-NOTFOUND";
  1114. }
  1115. return result;
  1116. }
  1117. void cmTarget::SetPropertyDefault(const std::string& property,
  1118. const char* default_value)
  1119. {
  1120. // Compute the name of the variable holding the default value.
  1121. std::string var = "CMAKE_";
  1122. var += property;
  1123. if (const char* value = this->Makefile->GetDefinition(var)) {
  1124. this->SetProperty(property, value);
  1125. } else if (default_value) {
  1126. this->SetProperty(property, default_value);
  1127. }
  1128. }
  1129. bool cmTarget::GetMappedConfig(std::string const& desired_config,
  1130. const char** loc, const char** imp,
  1131. std::string& suffix) const
  1132. {
  1133. if (this->GetType() == cmState::INTERFACE_LIBRARY) {
  1134. // This method attempts to find a config-specific LOCATION for the
  1135. // IMPORTED library. In the case of cmState::INTERFACE_LIBRARY, there is no
  1136. // LOCATION at all, so leaving *loc and *imp unchanged is the appropriate
  1137. // and valid response.
  1138. return true;
  1139. }
  1140. // Track the configuration-specific property suffix.
  1141. suffix = "_";
  1142. suffix += desired_config;
  1143. std::vector<std::string> mappedConfigs;
  1144. {
  1145. std::string mapProp = "MAP_IMPORTED_CONFIG_";
  1146. mapProp += desired_config;
  1147. if (const char* mapValue = this->GetProperty(mapProp)) {
  1148. cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
  1149. }
  1150. }
  1151. // If we needed to find one of the mapped configurations but did not
  1152. // On a DLL platform there may be only IMPORTED_IMPLIB for a shared
  1153. // library or an executable with exports.
  1154. bool allowImp = this->HasImportLibrary();
  1155. // If a mapping was found, check its configurations.
  1156. for (std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
  1157. !*loc && !*imp && mci != mappedConfigs.end(); ++mci) {
  1158. // Look for this configuration.
  1159. std::string mcUpper = cmSystemTools::UpperCase(*mci);
  1160. std::string locProp = "IMPORTED_LOCATION_";
  1161. locProp += mcUpper;
  1162. *loc = this->GetProperty(locProp);
  1163. if (allowImp) {
  1164. std::string impProp = "IMPORTED_IMPLIB_";
  1165. impProp += mcUpper;
  1166. *imp = this->GetProperty(impProp);
  1167. }
  1168. // If it was found, use it for all properties below.
  1169. if (*loc || *imp) {
  1170. suffix = "_";
  1171. suffix += mcUpper;
  1172. }
  1173. }
  1174. // If we needed to find one of the mapped configurations but did not
  1175. // then the target is not found. The project does not want any
  1176. // other configuration.
  1177. if (!mappedConfigs.empty() && !*loc && !*imp) {
  1178. return false;
  1179. }
  1180. // If we have not yet found it then there are no mapped
  1181. // configurations. Look for an exact-match.
  1182. if (!*loc && !*imp) {
  1183. std::string locProp = "IMPORTED_LOCATION";
  1184. locProp += suffix;
  1185. *loc = this->GetProperty(locProp);
  1186. if (allowImp) {
  1187. std::string impProp = "IMPORTED_IMPLIB";
  1188. impProp += suffix;
  1189. *imp = this->GetProperty(impProp);
  1190. }
  1191. }
  1192. // If we have not yet found it then there are no mapped
  1193. // configurations and no exact match.
  1194. if (!*loc && !*imp) {
  1195. // The suffix computed above is not useful.
  1196. suffix = "";
  1197. // Look for a configuration-less location. This may be set by
  1198. // manually-written code.
  1199. *loc = this->GetProperty("IMPORTED_LOCATION");
  1200. if (allowImp) {
  1201. *imp = this->GetProperty("IMPORTED_IMPLIB");
  1202. }
  1203. }
  1204. // If we have not yet found it then the project is willing to try
  1205. // any available configuration.
  1206. if (!*loc && !*imp) {
  1207. std::vector<std::string> availableConfigs;
  1208. if (const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS")) {
  1209. cmSystemTools::ExpandListArgument(iconfigs, availableConfigs);
  1210. }
  1211. for (std::vector<std::string>::const_iterator aci =
  1212. availableConfigs.begin();
  1213. !*loc && !*imp && aci != availableConfigs.end(); ++aci) {
  1214. suffix = "_";
  1215. suffix += cmSystemTools::UpperCase(*aci);
  1216. std::string locProp = "IMPORTED_LOCATION";
  1217. locProp += suffix;
  1218. *loc = this->GetProperty(locProp);
  1219. if (allowImp) {
  1220. std::string impProp = "IMPORTED_IMPLIB";
  1221. impProp += suffix;
  1222. *imp = this->GetProperty(impProp);
  1223. }
  1224. }
  1225. }
  1226. // If we have not yet found it then the target is not available.
  1227. if (!*loc && !*imp) {
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. cmTargetInternalPointer::cmTargetInternalPointer()
  1233. {
  1234. this->Pointer = new cmTargetInternals;
  1235. }
  1236. cmTargetInternalPointer::cmTargetInternalPointer(
  1237. cmTargetInternalPointer const& r)
  1238. {
  1239. // Ideally cmTarget instances should never be copied. However until
  1240. // we can make a sweep to remove that, this copy constructor avoids
  1241. // allowing the resources (Internals) to be copied.
  1242. this->Pointer = new cmTargetInternals(*r.Pointer);
  1243. }
  1244. cmTargetInternalPointer::~cmTargetInternalPointer()
  1245. {
  1246. delete this->Pointer;
  1247. }
  1248. cmTargetInternalPointer& cmTargetInternalPointer::operator=(
  1249. cmTargetInternalPointer const& r)
  1250. {
  1251. if (this == &r) {
  1252. return *this;
  1253. } // avoid warning on HP about self check
  1254. // Ideally cmTarget instances should never be copied. However until
  1255. // we can make a sweep to remove that, this copy constructor avoids
  1256. // allowing the resources (Internals) to be copied.
  1257. cmTargetInternals* oldPointer = this->Pointer;
  1258. this->Pointer = new cmTargetInternals(*r.Pointer);
  1259. delete oldPointer;
  1260. return *this;
  1261. }