cmTarget.cxx 52 KB

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