cmTarget.cxx 53 KB

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