cmTarget.cxx 52 KB

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