You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
18 KiB

  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. /* clang-format off */
  4. #include "cmGeneratorTarget.h"
  5. /* clang-format on */
  6. #include <cstddef>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <sstream>
  11. #include <string>
  12. #include <type_traits>
  13. #include <unordered_set>
  14. #include <utility>
  15. #include <vector>
  16. #include <cm/string_view>
  17. #include <cmext/algorithm>
  18. #include <cmext/string_view>
  19. #include "cmsys/RegularExpression.hxx"
  20. #include "cmAlgorithms.h"
  21. #include "cmEvaluatedTargetProperty.h"
  22. #include "cmFileSet.h"
  23. #include "cmGeneratorExpression.h"
  24. #include "cmGeneratorExpressionDAGChecker.h"
  25. #include "cmGlobalGenerator.h"
  26. #include "cmLinkItem.h"
  27. #include "cmList.h"
  28. #include "cmListFileCache.h"
  29. #include "cmLocalGenerator.h"
  30. #include "cmMakefile.h"
  31. #include "cmMessageType.h"
  32. #include "cmSourceFile.h"
  33. #include "cmSourceFileLocation.h"
  34. #include "cmSourceGroup.h"
  35. #include "cmStateTypes.h"
  36. #include "cmSystemTools.h"
  37. #include "cmTarget.h"
  38. #include "cmValue.h"
  39. #include "cmake.h"
  40. namespace {
  41. using UseTo = cmGeneratorTarget::UseTo;
  42. void AddObjectEntries(cmGeneratorTarget const* headTarget,
  43. std::string const& config,
  44. cmGeneratorExpressionDAGChecker* dagChecker,
  45. EvaluatedTargetPropertyEntries& entries)
  46. {
  47. if (cmLinkImplementationLibraries const* impl =
  48. headTarget->GetLinkImplementationLibraries(config, UseTo::Link)) {
  49. entries.HadContextSensitiveCondition = impl->HadContextSensitiveCondition;
  50. for (cmLinkImplItem const& lib : impl->Libraries) {
  51. if (lib.Target &&
  52. lib.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  53. std::string uniqueName =
  54. headTarget->GetGlobalGenerator()->IndexGeneratorTargetUniquely(
  55. lib.Target);
  56. std::string genex = "$<TARGET_OBJECTS:" + std::move(uniqueName) + ">";
  57. cmGeneratorExpression ge(*headTarget->Makefile->GetCMakeInstance(),
  58. lib.Backtrace);
  59. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  60. cge->SetEvaluateForBuildsystem(true);
  61. EvaluatedTargetPropertyEntry ee(lib, lib.Backtrace);
  62. cmExpandList(cge->Evaluate(headTarget->GetLocalGenerator(), config,
  63. headTarget, dagChecker),
  64. ee.Values);
  65. if (cge->GetHadContextSensitiveCondition()) {
  66. ee.ContextDependent = true;
  67. }
  68. entries.Entries.emplace_back(std::move(ee));
  69. }
  70. }
  71. }
  72. }
  73. void addFileSetEntry(cmGeneratorTarget const* headTarget,
  74. std::string const& config,
  75. cmGeneratorExpressionDAGChecker* dagChecker,
  76. cmFileSet const* fileSet,
  77. EvaluatedTargetPropertyEntries& entries)
  78. {
  79. auto dirCges = fileSet->CompileDirectoryEntries();
  80. auto dirs = fileSet->EvaluateDirectoryEntries(
  81. dirCges, headTarget->GetLocalGenerator(), config, headTarget, dagChecker);
  82. bool contextSensitiveDirs = false;
  83. for (auto const& dirCge : dirCges) {
  84. if (dirCge->GetHadContextSensitiveCondition()) {
  85. contextSensitiveDirs = true;
  86. break;
  87. }
  88. }
  89. cmake* cm = headTarget->GetLocalGenerator()->GetCMakeInstance();
  90. for (auto& entryCge : fileSet->CompileFileEntries()) {
  91. auto targetPropEntry =
  92. cmGeneratorTarget::TargetPropertyEntry::CreateFileSet(
  93. dirs, contextSensitiveDirs, std::move(entryCge), fileSet);
  94. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  95. headTarget, config, "", dagChecker, *targetPropEntry));
  96. EvaluatedTargetPropertyEntry const& entry = entries.Entries.back();
  97. for (auto const& file : entry.Values) {
  98. auto* sf = headTarget->Makefile->GetOrCreateSource(file);
  99. if (fileSet->GetType() == "HEADERS"_s) {
  100. sf->SetProperty("HEADER_FILE_ONLY", "TRUE");
  101. }
  102. #ifndef CMAKE_BOOTSTRAP
  103. std::string e;
  104. std::string w;
  105. auto path = sf->ResolveFullPath(&e, &w);
  106. if (!w.empty()) {
  107. cm->IssueMessage(MessageType::AUTHOR_WARNING, w, entry.Backtrace);
  108. }
  109. if (path.empty()) {
  110. if (!e.empty()) {
  111. cm->IssueMessage(MessageType::FATAL_ERROR, e, entry.Backtrace);
  112. }
  113. return;
  114. }
  115. bool found = false;
  116. for (auto const& sg : headTarget->Makefile->GetSourceGroups()) {
  117. if (sg.MatchChildrenFiles(path)) {
  118. found = true;
  119. break;
  120. }
  121. }
  122. if (!found) {
  123. if (fileSet->GetType() == "HEADERS"_s) {
  124. headTarget->Makefile->GetOrCreateSourceGroup("Header Files")
  125. ->AddGroupFile(path);
  126. }
  127. }
  128. #endif
  129. }
  130. }
  131. }
  132. void AddFileSetEntries(cmGeneratorTarget const* headTarget,
  133. std::string const& config,
  134. cmGeneratorExpressionDAGChecker* dagChecker,
  135. EvaluatedTargetPropertyEntries& entries)
  136. {
  137. for (auto const& entry : headTarget->Target->GetHeaderSetsEntries()) {
  138. for (auto const& name : cmList{ entry.Value }) {
  139. auto const* headerSet = headTarget->Target->GetFileSet(name);
  140. addFileSetEntry(headTarget, config, dagChecker, headerSet, entries);
  141. }
  142. }
  143. for (auto const& entry : headTarget->Target->GetCxxModuleSetsEntries()) {
  144. for (auto const& name : cmList{ entry.Value }) {
  145. auto const* cxxModuleSet = headTarget->Target->GetFileSet(name);
  146. addFileSetEntry(headTarget, config, dagChecker, cxxModuleSet, entries);
  147. }
  148. }
  149. }
  150. bool processSources(cmGeneratorTarget const* tgt,
  151. EvaluatedTargetPropertyEntries& entries,
  152. std::vector<BT<std::string>>& srcs,
  153. std::unordered_set<std::string>& uniqueSrcs,
  154. bool debugSources)
  155. {
  156. cmMakefile* mf = tgt->Target->GetMakefile();
  157. bool contextDependent = entries.HadContextSensitiveCondition;
  158. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  159. if (entry.ContextDependent) {
  160. contextDependent = true;
  161. }
  162. cmLinkImplItem const& item = entry.LinkImplItem;
  163. std::string const& targetName = item.AsStr();
  164. for (std::string& src : entry.Values) {
  165. cmSourceFile* sf = mf->GetOrCreateSource(src);
  166. std::string e;
  167. std::string w;
  168. std::string fullPath = sf->ResolveFullPath(&e, &w);
  169. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  170. if (!w.empty()) {
  171. cm->IssueMessage(MessageType::AUTHOR_WARNING, w, entry.Backtrace);
  172. }
  173. if (fullPath.empty()) {
  174. if (!e.empty()) {
  175. cm->IssueMessage(MessageType::FATAL_ERROR, e, entry.Backtrace);
  176. }
  177. return contextDependent;
  178. }
  179. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src)) {
  180. std::ostringstream err;
  181. if (!targetName.empty()) {
  182. err << "Target \"" << targetName
  183. << "\" contains relative path in its INTERFACE_SOURCES:\n \""
  184. << src << "\"";
  185. } else {
  186. err << "Found relative path while evaluating sources of \""
  187. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  188. }
  189. tgt->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  190. err.str());
  191. return contextDependent;
  192. }
  193. src = fullPath;
  194. }
  195. std::string usedSources;
  196. for (std::string const& src : entry.Values) {
  197. if (uniqueSrcs.insert(src).second) {
  198. srcs.emplace_back(src, entry.Backtrace);
  199. if (debugSources) {
  200. usedSources += " * " + src + "\n";
  201. }
  202. }
  203. }
  204. if (!usedSources.empty()) {
  205. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  206. MessageType::LOG,
  207. std::string("Used sources for target ") + tgt->GetName() + ":\n" +
  208. usedSources,
  209. entry.Backtrace);
  210. }
  211. }
  212. return contextDependent;
  213. }
  214. }
  215. std::vector<BT<std::string>> cmGeneratorTarget::GetSourceFilePaths(
  216. std::string const& config) const
  217. {
  218. std::vector<BT<std::string>> files;
  219. cmList debugProperties{ this->Makefile->GetDefinition(
  220. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  221. bool debugSources =
  222. !this->DebugSourcesDone && cm::contains(debugProperties, "SOURCES");
  223. this->DebugSourcesDone = true;
  224. cmGeneratorExpressionDAGChecker dagChecker{
  225. this, "SOURCES", nullptr, nullptr, this->LocalGenerator, config,
  226. };
  227. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  228. this, config, std::string(), &dagChecker, this->SourceEntries);
  229. std::unordered_set<std::string> uniqueSrcs;
  230. bool contextDependentDirectSources =
  231. processSources(this, entries, files, uniqueSrcs, debugSources);
  232. // Collect INTERFACE_SOURCES of all direct link-dependencies.
  233. EvaluatedTargetPropertyEntries linkInterfaceSourcesEntries;
  234. AddInterfaceEntries(this, config, "INTERFACE_SOURCES", std::string(),
  235. &dagChecker, linkInterfaceSourcesEntries,
  236. IncludeRuntimeInterface::No, UseTo::Compile);
  237. bool contextDependentInterfaceSources = processSources(
  238. this, linkInterfaceSourcesEntries, files, uniqueSrcs, debugSources);
  239. // Collect TARGET_OBJECTS of direct object link-dependencies.
  240. bool contextDependentObjects = false;
  241. if (this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  242. EvaluatedTargetPropertyEntries linkObjectsEntries;
  243. AddObjectEntries(this, config, &dagChecker, linkObjectsEntries);
  244. contextDependentObjects = processSources(this, linkObjectsEntries, files,
  245. uniqueSrcs, debugSources);
  246. // Note that for imported targets or multi-config generators supporting
  247. // cross-config builds the paths to the object files must be per-config,
  248. // so contextDependentObjects will be true here even if object libraries
  249. // are specified without per-config generator expressions.
  250. }
  251. // Collect this target's file sets.
  252. EvaluatedTargetPropertyEntries fileSetEntries;
  253. AddFileSetEntries(this, config, &dagChecker, fileSetEntries);
  254. bool contextDependentFileSets =
  255. processSources(this, fileSetEntries, files, uniqueSrcs, debugSources);
  256. // Determine if sources are context-dependent or not.
  257. if (!contextDependentDirectSources && !contextDependentInterfaceSources &&
  258. !contextDependentObjects && !contextDependentFileSets) {
  259. this->SourcesAreContextDependent = Tribool::False;
  260. } else {
  261. this->SourcesAreContextDependent = Tribool::True;
  262. }
  263. return files;
  264. }
  265. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  266. std::string const& config) const
  267. {
  268. std::vector<BT<cmSourceFile*>> tmp = this->GetSourceFiles(config);
  269. files.reserve(tmp.size());
  270. for (BT<cmSourceFile*>& v : tmp) {
  271. files.push_back(v.Value);
  272. }
  273. }
  274. std::vector<BT<cmSourceFile*>> cmGeneratorTarget::GetSourceFiles(
  275. std::string const& config) const
  276. {
  277. std::vector<BT<cmSourceFile*>> files;
  278. KindedSources const& kinded = this->GetKindedSources(config);
  279. files.reserve(kinded.Sources.size());
  280. for (SourceAndKind const& si : kinded.Sources) {
  281. files.push_back(si.Source);
  282. }
  283. return files;
  284. }
  285. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  286. std::vector<cmSourceFile*>& files, std::string const& config) const
  287. {
  288. std::vector<BT<cmSourceFile*>> tmp =
  289. this->GetSourceFilesWithoutObjectLibraries(config);
  290. files.reserve(tmp.size());
  291. for (BT<cmSourceFile*>& v : tmp) {
  292. files.push_back(v.Value);
  293. }
  294. }
  295. std::vector<BT<cmSourceFile*>>
  296. cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  297. std::string const& config) const
  298. {
  299. std::vector<BT<cmSourceFile*>> files;
  300. KindedSources const& kinded = this->GetKindedSources(config);
  301. files.reserve(kinded.Sources.size());
  302. for (SourceAndKind const& si : kinded.Sources) {
  303. if (si.Source.Value->GetObjectLibrary().empty()) {
  304. files.push_back(si.Source);
  305. }
  306. }
  307. return files;
  308. }
  309. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  310. std::string const& config) const
  311. {
  312. // If we already processed one configuration and found no dependency
  313. // on configuration then always use the one result.
  314. if (this->SourcesAreContextDependent == Tribool::False) {
  315. return this->KindedSourcesMap.begin()->second;
  316. }
  317. // Lookup any existing link implementation for this configuration.
  318. std::string const key = cmSystemTools::UpperCase(config);
  319. auto it = this->KindedSourcesMap.find(key);
  320. if (it != this->KindedSourcesMap.end()) {
  321. if (!it->second.Initialized) {
  322. std::ostringstream e;
  323. e << "The SOURCES of \"" << this->GetName()
  324. << "\" use a generator expression that depends on the "
  325. "SOURCES themselves.";
  326. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  327. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  328. static KindedSources empty;
  329. return empty;
  330. }
  331. return it->second;
  332. }
  333. // Add an entry to the map for this configuration.
  334. KindedSources& files = this->KindedSourcesMap[key];
  335. this->ComputeKindedSources(files, config);
  336. files.Initialized = true;
  337. return files;
  338. }
  339. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  340. std::string const& config) const
  341. {
  342. // Get the source file paths by string.
  343. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  344. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  345. std::vector<cmSourceFile*> badObjLib;
  346. std::set<cmSourceFile*> emitted;
  347. for (BT<std::string> const& s : srcs) {
  348. // Create each source at most once.
  349. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  350. if (!emitted.insert(sf).second) {
  351. continue;
  352. }
  353. // Compute the kind (classification) of this source file.
  354. SourceKind kind;
  355. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  356. cmFileSet const* fs = this->GetFileSetForSource(config, sf);
  357. if (sf->GetCustomCommand()) {
  358. kind = SourceKindCustomCommand;
  359. } else if (!this->Target->IsNormal() && !this->Target->IsImported() &&
  360. fs && (fs->GetType() == "CXX_MODULES"_s)) {
  361. kind = SourceKindCxxModuleSource;
  362. } else if (this->Target->GetType() == cmStateEnums::UTILITY ||
  363. this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY
  364. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  365. // NOLINTNEXTLINE(bugprone-branch-clone)
  366. ) {
  367. kind = SourceKindExtra;
  368. } else if (this->IsSourceFilePartOfUnityBatch(sf->ResolveFullPath())) {
  369. kind = SourceKindUnityBatched;
  370. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  371. // NOLINTNEXTLINE(bugprone-branch-clone)
  372. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  373. kind = SourceKindHeader;
  374. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  375. kind = SourceKindExternalObject;
  376. } else if (!sf->GetOrDetermineLanguage().empty()) {
  377. kind = SourceKindObjectSource;
  378. } else if (ext == "def") {
  379. kind = SourceKindModuleDefinition;
  380. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  381. badObjLib.push_back(sf);
  382. }
  383. } else if (ext == "idl") {
  384. kind = SourceKindIDL;
  385. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  386. badObjLib.push_back(sf);
  387. }
  388. } else if (ext == "resx") {
  389. kind = SourceKindResx;
  390. } else if (ext == "appxmanifest") {
  391. kind = SourceKindAppManifest;
  392. } else if (ext == "manifest") {
  393. if (sf->GetPropertyAsBool("VS_DEPLOYMENT_CONTENT")) {
  394. kind = SourceKindExtra;
  395. } else {
  396. kind = SourceKindManifest;
  397. }
  398. } else if (ext == "pfx") {
  399. kind = SourceKindCertificate;
  400. } else if (ext == "xaml") {
  401. kind = SourceKindXaml;
  402. } else if (header_regex.find(sf->ResolveFullPath())) {
  403. kind = SourceKindHeader;
  404. } else {
  405. kind = SourceKindExtra;
  406. }
  407. // Save this classified source file in the result vector.
  408. files.Sources.push_back({ BT<cmSourceFile*>(sf, s.Backtrace), kind });
  409. }
  410. if (!badObjLib.empty()) {
  411. std::ostringstream e;
  412. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  413. for (cmSourceFile* i : badObjLib) {
  414. e << " " << i->GetLocation().GetName() << "\n";
  415. }
  416. e << "but may contain only sources that compile, header files, and "
  417. "other files that would not affect linking of a normal library.";
  418. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  419. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  420. }
  421. }
  422. std::vector<cmGeneratorTarget::AllConfigSource> const&
  423. cmGeneratorTarget::GetAllConfigSources() const
  424. {
  425. if (this->AllConfigSources.empty()) {
  426. this->ComputeAllConfigSources();
  427. }
  428. return this->AllConfigSources;
  429. }
  430. void cmGeneratorTarget::ComputeAllConfigSources() const
  431. {
  432. std::vector<std::string> configs =
  433. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  434. std::map<cmSourceFile const*, size_t> index;
  435. for (size_t ci = 0; ci < configs.size(); ++ci) {
  436. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  437. for (SourceAndKind const& src : sources.Sources) {
  438. auto mi = index.find(src.Source.Value);
  439. if (mi == index.end()) {
  440. AllConfigSource acs;
  441. acs.Source = src.Source.Value;
  442. acs.Kind = src.Kind;
  443. this->AllConfigSources.push_back(std::move(acs));
  444. std::map<cmSourceFile const*, size_t>::value_type entry(
  445. src.Source.Value, this->AllConfigSources.size() - 1);
  446. mi = index.insert(entry).first;
  447. }
  448. this->AllConfigSources[mi->second].Configs.push_back(ci);
  449. }
  450. }
  451. }
  452. std::vector<cmGeneratorTarget::AllConfigSource>
  453. cmGeneratorTarget::GetAllConfigSources(SourceKind kind) const
  454. {
  455. std::vector<AllConfigSource> result;
  456. for (AllConfigSource const& source : this->GetAllConfigSources()) {
  457. if (source.Kind == kind) {
  458. result.push_back(source);
  459. }
  460. }
  461. return result;
  462. }
  463. std::set<std::string> cmGeneratorTarget::GetAllConfigCompileLanguages() const
  464. {
  465. std::set<std::string> languages;
  466. std::vector<AllConfigSource> const& sources = this->GetAllConfigSources();
  467. for (AllConfigSource const& si : sources) {
  468. std::string const& lang = si.Source->GetOrDetermineLanguage();
  469. if (!lang.empty()) {
  470. languages.emplace(lang);
  471. }
  472. }
  473. return languages;
  474. }