cmGeneratorTarget_Sources.cxx 18 KB

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