cmMakefile.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <deque>
  7. #include <functional>
  8. #include <map>
  9. #include <memory>
  10. #include <set>
  11. #include <stack>
  12. #include <string>
  13. #include <unordered_map>
  14. #include <utility>
  15. #include <vector>
  16. #include <cm/optional>
  17. #include <cm/string_view>
  18. #include "cmsys/RegularExpression.hxx"
  19. #include "cm_sys_stat.h"
  20. #include "cmAlgorithms.h"
  21. #include "cmCustomCommand.h"
  22. #include "cmCustomCommandTypes.h"
  23. #include "cmListFileCache.h"
  24. #include "cmMessageType.h"
  25. #include "cmNewLineStyle.h"
  26. #include "cmPolicies.h"
  27. #include "cmSourceFileLocationKind.h"
  28. #include "cmStateSnapshot.h"
  29. #include "cmStateTypes.h"
  30. #include "cmValue.h"
  31. // IWYU does not see that 'std::unordered_map<std::string, cmTarget>'
  32. // will not compile without the complete type.
  33. #include "cmTarget.h" // IWYU pragma: keep
  34. #if !defined(CMAKE_BOOTSTRAP)
  35. # include "cmSourceGroup.h"
  36. #endif
  37. class cmCompiledGeneratorExpression;
  38. class cmCustomCommandLines;
  39. class cmExecutionStatus;
  40. class cmExpandedCommandArgument;
  41. class cmExportBuildFileGenerator;
  42. class cmFunctionBlocker;
  43. class cmGeneratorExpressionEvaluationFile;
  44. class cmGlobalGenerator;
  45. class cmInstallGenerator;
  46. class cmLocalGenerator;
  47. class cmMessenger;
  48. class cmSourceFile;
  49. class cmState;
  50. class cmTest;
  51. class cmTestGenerator;
  52. class cmVariableWatch;
  53. class cmake;
  54. /** A type-safe wrapper for a string representing a directory id. */
  55. class cmDirectoryId
  56. {
  57. public:
  58. cmDirectoryId(std::string s);
  59. std::string String;
  60. };
  61. /** \class cmMakefile
  62. * \brief Process the input CMakeLists.txt file.
  63. *
  64. * Process and store into memory the input CMakeLists.txt file.
  65. * Each CMakeLists.txt file is parsed and the commands found there
  66. * are added into the build process.
  67. */
  68. class cmMakefile
  69. {
  70. public:
  71. /* Mark a variable as used */
  72. void MarkVariableAsUsed(const std::string& var);
  73. /* return true if a variable has been initialized */
  74. bool VariableInitialized(const std::string&) const;
  75. /**
  76. * Construct an empty makefile.
  77. */
  78. cmMakefile(cmGlobalGenerator* globalGenerator,
  79. const cmStateSnapshot& snapshot);
  80. /**
  81. * Destructor.
  82. */
  83. ~cmMakefile();
  84. cmMakefile(cmMakefile const&) = delete;
  85. cmMakefile& operator=(cmMakefile const&) = delete;
  86. cmDirectoryId GetDirectoryId() const;
  87. bool ReadListFile(const std::string& filename);
  88. bool ReadListFileAsString(const std::string& content,
  89. const std::string& virtualFileName);
  90. bool ReadDependentFile(const std::string& filename,
  91. bool noPolicyScope = true);
  92. /**
  93. * Add a function blocker to this makefile
  94. */
  95. void AddFunctionBlocker(std::unique_ptr<cmFunctionBlocker> fb);
  96. /// @return whether we are processing the top CMakeLists.txt file.
  97. bool IsRootMakefile() const;
  98. /**
  99. * Remove the function blocker whose scope ends with the given command.
  100. * This returns ownership of the function blocker object.
  101. */
  102. std::unique_ptr<cmFunctionBlocker> RemoveFunctionBlocker();
  103. /**
  104. * Try running cmake and building a file. This is used for dynamically
  105. * loaded commands, not as part of the usual build process.
  106. */
  107. int TryCompile(const std::string& srcdir, const std::string& bindir,
  108. const std::string& projectName, const std::string& targetName,
  109. bool fast, int jobs,
  110. const std::vector<std::string>* cmakeArgs,
  111. std::string& output);
  112. bool GetIsSourceFileTryCompile() const;
  113. /**
  114. * Help enforce global target name uniqueness.
  115. */
  116. bool EnforceUniqueName(std::string const& name, std::string& msg,
  117. bool isCustom = false) const;
  118. class GeneratorAction
  119. {
  120. using ActionT =
  121. std::function<void(cmLocalGenerator&, const cmListFileBacktrace&)>;
  122. using CCActionT =
  123. std::function<void(cmLocalGenerator&, const cmListFileBacktrace&,
  124. std::unique_ptr<cmCustomCommand> cc)>;
  125. public:
  126. GeneratorAction(ActionT&& action)
  127. : Action(std::move(action))
  128. {
  129. }
  130. GeneratorAction(std::unique_ptr<cmCustomCommand> tcc, CCActionT&& action)
  131. : CCAction(std::move(action))
  132. , cc(std::move(tcc))
  133. {
  134. }
  135. void operator()(cmLocalGenerator& lg, const cmListFileBacktrace& lfbt);
  136. private:
  137. ActionT Action;
  138. // FIXME: Use std::variant
  139. CCActionT CCAction;
  140. std::unique_ptr<cmCustomCommand> cc;
  141. };
  142. /**
  143. * Register an action that is executed during Generate
  144. */
  145. void AddGeneratorAction(GeneratorAction&& action);
  146. /// Helper to insert the constructor GeneratorAction(args...)
  147. template <class... Args>
  148. void AddGeneratorAction(Args&&... args)
  149. {
  150. AddGeneratorAction(GeneratorAction(std::move(args)...));
  151. }
  152. /**
  153. * Perform generate actions, Library dependency analysis etc before output of
  154. * the makefile.
  155. */
  156. void Generate(cmLocalGenerator& lg);
  157. /**
  158. * Get the target for PRE_BUILD, PRE_LINK, or POST_BUILD commands.
  159. */
  160. cmTarget* GetCustomCommandTarget(const std::string& target,
  161. cmObjectLibraryCommands objLibCommands,
  162. const cmListFileBacktrace& lfbt) const;
  163. /**
  164. * Dispatch adding a custom PRE_BUILD, PRE_LINK, or POST_BUILD command to a
  165. * target.
  166. */
  167. cmTarget* AddCustomCommandToTarget(const std::string& target,
  168. cmCustomCommandType type,
  169. std::unique_ptr<cmCustomCommand> cc);
  170. /**
  171. * Called for each file with custom command.
  172. */
  173. using CommandSourceCallback = std::function<void(cmSourceFile*)>;
  174. /**
  175. * Dispatch adding a custom command to a source file.
  176. */
  177. void AddCustomCommandToOutput(
  178. std::unique_ptr<cmCustomCommand> cc,
  179. const CommandSourceCallback& callback = nullptr, bool replace = false);
  180. void AddCustomCommandOldStyle(const std::string& target,
  181. const std::vector<std::string>& outputs,
  182. const std::vector<std::string>& depends,
  183. const std::string& source,
  184. const cmCustomCommandLines& commandLines,
  185. const char* comment);
  186. void AppendCustomCommandToOutput(
  187. const std::string& output, const std::vector<std::string>& depends,
  188. const cmImplicitDependsList& implicit_depends,
  189. const cmCustomCommandLines& commandLines);
  190. /**
  191. * Add a define flag to the build.
  192. */
  193. void AddDefineFlag(std::string const& definition);
  194. void RemoveDefineFlag(std::string const& definition);
  195. void AddCompileDefinition(std::string const& definition);
  196. void AddCompileOption(std::string const& option);
  197. void AddLinkOption(std::string const& option);
  198. void AddLinkDirectory(std::string const& directory, bool before = false);
  199. /** Create a new imported target with the name and type given. */
  200. cmTarget* AddImportedTarget(const std::string& name,
  201. cmStateEnums::TargetType type, bool global);
  202. std::pair<cmTarget&, bool> CreateNewTarget(
  203. const std::string& name, cmStateEnums::TargetType type,
  204. cmTarget::PerConfig perConfig = cmTarget::PerConfig::Yes,
  205. cmTarget::Visibility vis = cmTarget::Visibility::Normal);
  206. cmTarget* AddNewTarget(cmStateEnums::TargetType type,
  207. const std::string& name);
  208. cmTarget* AddSynthesizedTarget(cmStateEnums::TargetType type,
  209. const std::string& name);
  210. /** Create a target instance for the utility. */
  211. cmTarget* AddNewUtilityTarget(const std::string& utilityName,
  212. bool excludeFromAll);
  213. /**
  214. * Add an executable to the build.
  215. */
  216. cmTarget* AddExecutable(const std::string& exename,
  217. const std::vector<std::string>& srcs,
  218. bool excludeFromAll = false);
  219. /**
  220. * Dispatch adding a utility to the build. A utility target is a command
  221. * that is run every time the target is built.
  222. */
  223. cmTarget* AddUtilityCommand(const std::string& utilityName,
  224. bool excludeFromAll,
  225. std::unique_ptr<cmCustomCommand> cc);
  226. /**
  227. * Add a subdirectory to the build.
  228. */
  229. void AddSubDirectory(const std::string& fullSrcDir,
  230. const std::string& fullBinDir, bool excludeFromAll,
  231. bool immediate, bool system);
  232. void Configure();
  233. /**
  234. * Configure a subdirectory
  235. */
  236. void ConfigureSubDirectory(cmMakefile* mf);
  237. /**
  238. * Add an include directory to the build.
  239. */
  240. void AddIncludeDirectories(const std::vector<std::string>& incs,
  241. bool before = false);
  242. /**
  243. * Add a variable definition to the build. This variable
  244. * can be used in CMake to refer to lists, directories, etc.
  245. */
  246. void AddDefinition(const std::string& name, cm::string_view value);
  247. void AddDefinition(const std::string& name, cmValue value)
  248. {
  249. this->AddDefinition(name, *value);
  250. }
  251. /**
  252. * Add bool variable definition to the build.
  253. */
  254. void AddDefinitionBool(const std::string& name, bool);
  255. //! Add a definition to this makefile and the global cmake cache.
  256. void AddCacheDefinition(const std::string& name, const char* value,
  257. const char* doc, cmStateEnums::CacheEntryType type,
  258. bool force = false);
  259. void AddCacheDefinition(const std::string& name, const std::string& value,
  260. const char* doc, cmStateEnums::CacheEntryType type,
  261. bool force = false)
  262. {
  263. this->AddCacheDefinition(name, value.c_str(), doc, type, force);
  264. }
  265. /**
  266. * Remove a variable definition from the build. This is not valid
  267. * for cache entries, and will only affect the current makefile.
  268. */
  269. void RemoveDefinition(const std::string& name);
  270. //! Remove a definition from the cache.
  271. void RemoveCacheDefinition(const std::string& name) const;
  272. /**
  273. * Specify the name of the project for this build.
  274. */
  275. void SetProjectName(std::string const& name);
  276. void InitCMAKE_CONFIGURATION_TYPES(std::string const& genDefault);
  277. /* Get the default configuration */
  278. std::string GetDefaultConfiguration() const;
  279. enum GeneratorConfigQuery
  280. {
  281. IncludeEmptyConfig, // Include "" aka noconfig
  282. ExcludeEmptyConfig, // Exclude "" aka noconfig
  283. OnlyMultiConfig,
  284. };
  285. /** Get the configurations for dependency checking. */
  286. std::vector<std::string> GetGeneratorConfigs(
  287. GeneratorConfigQuery mode) const;
  288. /**
  289. * Set the name of the library.
  290. */
  291. cmTarget* AddLibrary(const std::string& libname,
  292. cmStateEnums::TargetType type,
  293. const std::vector<std::string>& srcs,
  294. bool excludeFromAll = false);
  295. void AddAlias(const std::string& libname, const std::string& tgt,
  296. bool globallyVisible = true);
  297. //@{
  298. /**
  299. * Set, Push, Pop policy values for CMake.
  300. */
  301. bool SetPolicy(cmPolicies::PolicyID id, cmPolicies::PolicyStatus status);
  302. bool SetPolicy(const char* id, cmPolicies::PolicyStatus status);
  303. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID id,
  304. bool parent_scope = false) const;
  305. bool SetPolicyVersion(std::string const& version_min,
  306. std::string const& version_max);
  307. void RecordPolicies(cmPolicies::PolicyMap& pm) const;
  308. //@}
  309. /** Helper class to push and pop policies automatically. */
  310. class PolicyPushPop
  311. {
  312. public:
  313. PolicyPushPop(cmMakefile* m);
  314. ~PolicyPushPop();
  315. PolicyPushPop(const PolicyPushPop&) = delete;
  316. PolicyPushPop& operator=(const PolicyPushPop&) = delete;
  317. private:
  318. cmMakefile* Makefile;
  319. };
  320. friend class PolicyPushPop;
  321. /** Helper class to push and pop variables scopes automatically. */
  322. class VariablePushPop
  323. {
  324. public:
  325. VariablePushPop(cmMakefile* m);
  326. ~VariablePushPop();
  327. VariablePushPop(VariablePushPop const&) = delete;
  328. VariablePushPop& operator=(VariablePushPop const&) = delete;
  329. private:
  330. cmMakefile* Makefile;
  331. };
  332. /**
  333. * Determine if the given context, name pair has already been reported
  334. * in context of CMP0054.
  335. */
  336. bool HasCMP0054AlreadyBeenReported(const cmListFileContext& context) const;
  337. bool IgnoreErrorsCMP0061() const;
  338. std::string const& GetHomeDirectory() const;
  339. std::string const& GetHomeOutputDirectory() const;
  340. /**
  341. * Set CMAKE_SCRIPT_MODE_FILE variable when running a -P script.
  342. */
  343. void SetScriptModeFile(std::string const& scriptfile);
  344. /**
  345. * Set CMAKE_ARGC, CMAKE_ARGV0 ... variables.
  346. */
  347. void SetArgcArgv(const std::vector<std::string>& args);
  348. std::string const& GetCurrentSourceDirectory() const;
  349. std::string const& GetCurrentBinaryDirectory() const;
  350. //@}
  351. /**
  352. * Set a regular expression that include files must match
  353. * in order to be considered as part of the depend information.
  354. */
  355. void SetIncludeRegularExpression(const std::string& regex)
  356. {
  357. this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex.c_str());
  358. }
  359. const std::string& GetIncludeRegularExpression() const
  360. {
  361. return this->GetProperty("INCLUDE_REGULAR_EXPRESSION");
  362. }
  363. /**
  364. * Set a regular expression that include files that are not found
  365. * must match in order to be considered a problem.
  366. */
  367. void SetComplainRegularExpression(const std::string& regex)
  368. {
  369. this->ComplainFileRegularExpression = regex;
  370. }
  371. const std::string& GetComplainRegularExpression() const
  372. {
  373. return this->ComplainFileRegularExpression;
  374. }
  375. // -- List of targets
  376. using cmTargetMap = std::unordered_map<std::string, cmTarget>;
  377. /** Get the target map */
  378. cmTargetMap& GetTargets() { return this->Targets; }
  379. /** Get the target map - const version */
  380. cmTargetMap const& GetTargets() const { return this->Targets; }
  381. const std::vector<std::unique_ptr<cmTarget>>& GetOwnedImportedTargets() const
  382. {
  383. return this->ImportedTargetsOwned;
  384. }
  385. std::vector<cmTarget*> GetImportedTargets() const;
  386. cmTarget* FindLocalNonAliasTarget(const std::string& name) const;
  387. /** Find a target to use in place of the given name. The target
  388. returned may be imported or built within the project. */
  389. cmTarget* FindTargetToUse(const std::string& name,
  390. bool excludeAliases = false) const;
  391. bool IsAlias(const std::string& name) const;
  392. std::map<std::string, std::string> GetAliasTargets() const
  393. {
  394. return this->AliasTargets;
  395. }
  396. /**
  397. * Mark include directories as system directories.
  398. */
  399. void AddSystemIncludeDirectories(const std::set<std::string>& incs);
  400. /** Get a cmSourceFile pointer for a given source name, if the name is
  401. * not found, then a null pointer is returned.
  402. */
  403. cmSourceFile* GetSource(
  404. const std::string& sourceName,
  405. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous) const;
  406. /** Create the source file and return it. generated
  407. * indicates if it is a generated file, this is used in determining
  408. * how to create the source file instance e.g. name
  409. */
  410. cmSourceFile* CreateSource(
  411. const std::string& sourceName, bool generated = false,
  412. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
  413. /** Get a cmSourceFile pointer for a given source name, if the name is
  414. * not found, then create the source file and return it. generated
  415. * indicates if it is a generated file, this is used in determining
  416. * how to create the source file instance e.g. name
  417. */
  418. cmSourceFile* GetOrCreateSource(
  419. const std::string& sourceName, bool generated = false,
  420. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
  421. /** Get a cmSourceFile pointer for a given source name and always mark the
  422. * file as generated, if the name is not found, then create the source file
  423. * and return it.
  424. */
  425. cmSourceFile* GetOrCreateGeneratedSource(const std::string& sourceName);
  426. void AddTargetObject(std::string const& tgtName, std::string const& objFile);
  427. /**
  428. * Given a variable name, return its value (as a string).
  429. * If the variable is not found in this makefile instance, the
  430. * cache is then queried.
  431. */
  432. cmValue GetDefinition(const std::string&) const;
  433. const std::string& GetSafeDefinition(const std::string&) const;
  434. const std::string& GetRequiredDefinition(const std::string& name) const;
  435. bool IsDefinitionSet(const std::string&) const;
  436. bool IsNormalDefinitionSet(const std::string&) const;
  437. bool GetDefExpandList(const std::string& name, std::vector<std::string>& out,
  438. bool emptyArgs = false) const;
  439. /**
  440. * Get the list of all variables in the current space. If argument
  441. * cacheonly is specified and is greater than 0, then only cache
  442. * variables will be listed.
  443. */
  444. std::vector<std::string> GetDefinitions() const;
  445. /**
  446. * Test a boolean variable to see if it is true or false.
  447. * If the variable is not found in this makefile instance, the
  448. * cache is then queried.
  449. * Returns false if no entry defined.
  450. */
  451. bool IsOn(const std::string& name) const;
  452. bool IsSet(const std::string& name) const;
  453. /** Return whether the target platform is 32-bit. */
  454. bool PlatformIs32Bit() const;
  455. /** Return whether the target platform is 64-bit. */
  456. bool PlatformIs64Bit() const;
  457. /** Return whether the target platform is x32. */
  458. bool PlatformIsx32() const;
  459. /** Apple SDK Type */
  460. enum class AppleSDK
  461. {
  462. MacOS,
  463. IPhoneOS,
  464. IPhoneSimulator,
  465. AppleTVOS,
  466. AppleTVSimulator,
  467. WatchOS,
  468. WatchSimulator,
  469. };
  470. /** What SDK type points CMAKE_OSX_SYSROOT to? */
  471. AppleSDK GetAppleSDKType() const;
  472. /** Return whether the target platform is Apple iOS. */
  473. bool PlatformIsAppleEmbedded() const;
  474. /** Retrieve soname flag for the specified language if supported */
  475. const char* GetSONameFlag(const std::string& language) const;
  476. /**
  477. * Get a list of preprocessor define flags.
  478. */
  479. std::string GetDefineFlags() const { return this->DefineFlags; }
  480. /**
  481. * Make sure CMake can write this file
  482. */
  483. bool CanIWriteThisFile(std::string const& fileName) const;
  484. #if !defined(CMAKE_BOOTSTRAP)
  485. /**
  486. * Get the vector source groups.
  487. */
  488. const std::vector<cmSourceGroup>& GetSourceGroups() const
  489. {
  490. return this->SourceGroups;
  491. }
  492. /**
  493. * Get the source group
  494. */
  495. cmSourceGroup* GetSourceGroup(const std::vector<std::string>& name) const;
  496. /**
  497. * Add a root source group for consideration when adding a new source.
  498. */
  499. void AddSourceGroup(const std::string& name, const char* regex = nullptr);
  500. /**
  501. * Add a source group for consideration when adding a new source.
  502. * name is tokenized.
  503. */
  504. void AddSourceGroup(const std::vector<std::string>& name,
  505. const char* regex = nullptr);
  506. /**
  507. * Get and existing or create a new source group.
  508. */
  509. cmSourceGroup* GetOrCreateSourceGroup(
  510. const std::vector<std::string>& folders);
  511. /**
  512. * Get and existing or create a new source group.
  513. * The name will be tokenized.
  514. */
  515. cmSourceGroup* GetOrCreateSourceGroup(const std::string& name);
  516. /**
  517. * find what source group this source is in
  518. */
  519. cmSourceGroup* FindSourceGroup(const std::string& source,
  520. std::vector<cmSourceGroup>& groups) const;
  521. #endif
  522. /**
  523. * Get the vector of list files on which this makefile depends
  524. */
  525. const std::vector<std::string>& GetListFiles() const
  526. {
  527. return this->ListFiles;
  528. }
  529. //! When the file changes cmake will be re-run from the build system.
  530. void AddCMakeDependFile(const std::string& file)
  531. {
  532. this->ListFiles.push_back(file);
  533. }
  534. void AddCMakeDependFilesFromUser();
  535. std::string FormatListFileStack() const;
  536. /**
  537. * Get the current context backtrace.
  538. */
  539. cmListFileBacktrace GetBacktrace() const;
  540. /**
  541. * Get the vector of files created by this makefile
  542. */
  543. const std::vector<std::string>& GetOutputFiles() const
  544. {
  545. return this->OutputFiles;
  546. }
  547. void AddCMakeOutputFile(const std::string& file)
  548. {
  549. this->OutputFiles.push_back(file);
  550. }
  551. /**
  552. * Expand all defined variables in the string.
  553. * Defined variables come from the this->Definitions map.
  554. * They are expanded with ${var} where var is the
  555. * entry in the this->Definitions map. Also \@var\@ is
  556. * expanded to match autoconf style expansions.
  557. */
  558. const std::string& ExpandVariablesInString(std::string& source) const;
  559. const std::string& ExpandVariablesInString(
  560. std::string& source, bool escapeQuotes, bool noEscapes,
  561. bool atOnly = false, const char* filename = nullptr, long line = -1,
  562. bool removeEmpty = false, bool replaceAt = false) const;
  563. /**
  564. * Remove any remaining variables in the string. Anything with ${var} or
  565. * \@var\@ will be removed.
  566. */
  567. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  568. /**
  569. * Expand variables in the makefiles ivars such as link directories etc
  570. */
  571. void ExpandVariablesCMP0019();
  572. /**
  573. * Replace variables and #cmakedefine lines in the given string.
  574. * See cmConfigureFileCommand for details.
  575. */
  576. void ConfigureString(const std::string& input, std::string& output,
  577. bool atOnly, bool escapeQuotes) const;
  578. /**
  579. * Copy file but change lines according to ConfigureString
  580. */
  581. int ConfigureFile(const std::string& infile, const std::string& outfile,
  582. bool copyonly, bool atOnly, bool escapeQuotes,
  583. mode_t permissions = 0, cmNewLineStyle = cmNewLineStyle());
  584. enum class CommandMissingFromStack
  585. {
  586. No,
  587. Yes,
  588. };
  589. /**
  590. * Print a command's invocation
  591. */
  592. void PrintCommandTrace(
  593. cmListFileFunction const& lff, cmListFileBacktrace const& bt,
  594. CommandMissingFromStack missing = CommandMissingFromStack::No) const;
  595. /**
  596. * Set a callback that is invoked whenever ExecuteCommand is called.
  597. */
  598. void OnExecuteCommand(std::function<void()> callback);
  599. /**
  600. * Execute a single CMake command. Returns true if the command
  601. * succeeded or false if it failed.
  602. */
  603. bool ExecuteCommand(const cmListFileFunction& lff, cmExecutionStatus& status,
  604. cm::optional<std::string> deferId = {});
  605. //! Enable support for named language, if nil then all languages are
  606. /// enabled.
  607. void EnableLanguage(std::vector<std::string> const& languages,
  608. bool optional);
  609. cmState* GetState() const;
  610. /**
  611. * Get the variable watch. This is used to determine when certain variables
  612. * are accessed.
  613. */
  614. #ifndef CMAKE_BOOTSTRAP
  615. cmVariableWatch* GetVariableWatch() const;
  616. #endif
  617. //! Display progress or status message.
  618. void DisplayStatus(const std::string&, float) const;
  619. /**
  620. * Expand the given list file arguments into the full set after
  621. * variable replacement and list expansion.
  622. */
  623. bool ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  624. std::vector<std::string>& outArgs) const;
  625. bool ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  626. std::vector<cmExpandedCommandArgument>& outArgs) const;
  627. /**
  628. * Get the instance
  629. */
  630. cmake* GetCMakeInstance() const;
  631. cmMessenger* GetMessenger() const;
  632. cmGlobalGenerator* GetGlobalGenerator() const;
  633. /**
  634. * Get all the source files this makefile knows about
  635. */
  636. const std::vector<std::unique_ptr<cmSourceFile>>& GetSourceFiles() const
  637. {
  638. return this->SourceFiles;
  639. }
  640. std::vector<cmTarget*> const& GetOrderedTargets() const
  641. {
  642. return this->OrderedTargets;
  643. }
  644. //! Add a new cmTest to the list of tests for this makefile.
  645. cmTest* CreateTest(const std::string& testName);
  646. /** Get a cmTest pointer for a given test name, if the name is
  647. * not found, then a null pointer is returned.
  648. */
  649. cmTest* GetTest(const std::string& testName) const;
  650. /**
  651. * Get all tests that run under the given configuration.
  652. */
  653. void GetTests(const std::string& config, std::vector<cmTest*>& tests) const;
  654. /**
  655. * Return a location of a file in cmake or custom modules directory
  656. */
  657. std::string GetModulesFile(const std::string& name) const
  658. {
  659. bool system;
  660. std::string debugBuffer;
  661. return this->GetModulesFile(name, system, false, debugBuffer);
  662. }
  663. /**
  664. * Return a location of a file in cmake or custom modules directory
  665. */
  666. std::string GetModulesFile(const std::string& name, bool& system) const
  667. {
  668. std::string debugBuffer;
  669. return this->GetModulesFile(name, system, false, debugBuffer);
  670. }
  671. std::string GetModulesFile(const std::string& name, bool& system, bool debug,
  672. std::string& debugBuffer) const;
  673. //! Set/Get a property of this directory
  674. void SetProperty(const std::string& prop, const char* value);
  675. void SetProperty(const std::string& prop, cmValue value);
  676. void SetProperty(const std::string& prop, const std::string& value)
  677. {
  678. this->SetProperty(prop, cmValue(value));
  679. }
  680. void AppendProperty(const std::string& prop, const std::string& value,
  681. bool asString = false);
  682. cmValue GetProperty(const std::string& prop) const;
  683. cmValue GetProperty(const std::string& prop, bool chain) const;
  684. bool GetPropertyAsBool(const std::string& prop) const;
  685. std::vector<std::string> GetPropertyKeys() const;
  686. //! Initialize a makefile from its parent
  687. void InitializeFromParent(cmMakefile* parent);
  688. void AddInstallGenerator(std::unique_ptr<cmInstallGenerator> g);
  689. std::vector<std::unique_ptr<cmInstallGenerator>>& GetInstallGenerators()
  690. {
  691. return this->InstallGenerators;
  692. }
  693. const std::vector<std::unique_ptr<cmInstallGenerator>>&
  694. GetInstallGenerators() const
  695. {
  696. return this->InstallGenerators;
  697. }
  698. void AddTestGenerator(std::unique_ptr<cmTestGenerator> g);
  699. const std::vector<std::unique_ptr<cmTestGenerator>>& GetTestGenerators()
  700. const
  701. {
  702. return this->TestGenerators;
  703. }
  704. class FunctionPushPop
  705. {
  706. public:
  707. FunctionPushPop(cmMakefile* mf, std::string const& fileName,
  708. cmPolicies::PolicyMap const& pm);
  709. ~FunctionPushPop();
  710. FunctionPushPop(const FunctionPushPop&) = delete;
  711. FunctionPushPop& operator=(const FunctionPushPop&) = delete;
  712. void Quiet() { this->ReportError = false; }
  713. private:
  714. cmMakefile* Makefile;
  715. bool ReportError = true;
  716. };
  717. class MacroPushPop
  718. {
  719. public:
  720. MacroPushPop(cmMakefile* mf, std::string const& fileName,
  721. cmPolicies::PolicyMap const& pm);
  722. ~MacroPushPop();
  723. MacroPushPop(const MacroPushPop&) = delete;
  724. MacroPushPop& operator=(const MacroPushPop&) = delete;
  725. void Quiet() { this->ReportError = false; }
  726. private:
  727. cmMakefile* Makefile;
  728. bool ReportError = true;
  729. };
  730. void PushFunctionScope(std::string const& fileName,
  731. cmPolicies::PolicyMap const& pm);
  732. void PopFunctionScope(bool reportError);
  733. void PushMacroScope(std::string const& fileName,
  734. cmPolicies::PolicyMap const& pm);
  735. void PopMacroScope(bool reportError);
  736. void PushScope();
  737. void PopScope();
  738. void RaiseScope(const std::string& var, const char* value);
  739. void RaiseScope(const std::string& var, cmValue value)
  740. {
  741. this->RaiseScope(var, value.GetCStr());
  742. }
  743. void RaiseScope(const std::vector<std::string>& variables);
  744. // push and pop loop scopes
  745. void PushLoopBlockBarrier();
  746. void PopLoopBlockBarrier();
  747. bool IsImportedTargetGlobalScope() const;
  748. enum class ImportedTargetScope
  749. {
  750. Local,
  751. Global,
  752. };
  753. /** Helper class to manage whether imported packages
  754. * should be globally scoped based off the find package command
  755. */
  756. class SetGlobalTargetImportScope
  757. {
  758. public:
  759. SetGlobalTargetImportScope(cmMakefile* mk, ImportedTargetScope const scope)
  760. : Makefile(mk)
  761. {
  762. if (scope == ImportedTargetScope::Global &&
  763. !this->Makefile->IsImportedTargetGlobalScope()) {
  764. this->Makefile->CurrentImportedTargetScope = scope;
  765. this->Set = true;
  766. } else {
  767. this->Set = false;
  768. }
  769. }
  770. ~SetGlobalTargetImportScope()
  771. {
  772. if (this->Set) {
  773. this->Makefile->CurrentImportedTargetScope =
  774. ImportedTargetScope::Local;
  775. }
  776. }
  777. private:
  778. cmMakefile* Makefile;
  779. bool Set;
  780. };
  781. /** Helper class to push and pop scopes automatically. */
  782. class ScopePushPop
  783. {
  784. public:
  785. ScopePushPop(cmMakefile* m)
  786. : Makefile(m)
  787. {
  788. this->Makefile->PushScope();
  789. }
  790. ~ScopePushPop() { this->Makefile->PopScope(); }
  791. ScopePushPop(ScopePushPop const&) = delete;
  792. ScopePushPop& operator=(ScopePushPop const&) = delete;
  793. private:
  794. cmMakefile* Makefile;
  795. };
  796. void IssueMessage(MessageType t, std::string const& text) const;
  797. Message::LogLevel GetCurrentLogLevel() const;
  798. /** Set whether or not to report a CMP0000 violation. */
  799. void SetCheckCMP0000(bool b) { this->CheckCMP0000 = b; }
  800. bool CheckCMP0037(std::string const& targetName,
  801. cmStateEnums::TargetType targetType) const;
  802. cmBTStringRange GetIncludeDirectoriesEntries() const;
  803. cmBTStringRange GetCompileOptionsEntries() const;
  804. cmBTStringRange GetCompileDefinitionsEntries() const;
  805. cmBTStringRange GetLinkOptionsEntries() const;
  806. cmBTStringRange GetLinkDirectoriesEntries() const;
  807. std::set<std::string> const& GetSystemIncludeDirectories() const
  808. {
  809. return this->SystemIncludeDirectories;
  810. }
  811. bool PolicyOptionalWarningEnabled(std::string const& var) const;
  812. void PushLoopBlock();
  813. void PopLoopBlock();
  814. bool IsLoopBlock() const;
  815. void ClearMatches();
  816. void StoreMatches(cmsys::RegularExpression& re);
  817. cmStateSnapshot GetStateSnapshot() const;
  818. const char* GetDefineFlagsCMP0059() const;
  819. void EnforceDirectoryLevelRules() const;
  820. void AddEvaluationFile(
  821. const std::string& inputFile, const std::string& targetName,
  822. std::unique_ptr<cmCompiledGeneratorExpression> outputName,
  823. std::unique_ptr<cmCompiledGeneratorExpression> condition,
  824. const std::string& newLineCharacter, mode_t permissions,
  825. bool inputIsContent);
  826. const std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>&
  827. GetEvaluationFiles() const;
  828. std::vector<std::unique_ptr<cmExportBuildFileGenerator>> const&
  829. GetExportBuildFileGenerators() const;
  830. void RemoveExportBuildFileGeneratorCMP0024(cmExportBuildFileGenerator* gen);
  831. void AddExportBuildFileGenerator(
  832. std::unique_ptr<cmExportBuildFileGenerator> gen);
  833. // Maintain a stack of package roots to allow nested PACKAGE_ROOT_PATH
  834. // searches
  835. std::deque<std::vector<std::string>> FindPackageRootPathStack;
  836. class DebugFindPkgRAII
  837. {
  838. cmMakefile* Makefile;
  839. bool OldValue;
  840. public:
  841. DebugFindPkgRAII(cmMakefile* mf, std::string const& pkg);
  842. ~DebugFindPkgRAII();
  843. };
  844. bool GetDebugFindPkgMode() const;
  845. void MaybeWarnCMP0074(std::string const& pkg);
  846. void MaybeWarnUninitialized(std::string const& variable,
  847. const char* sourceFilename) const;
  848. bool IsProjectFile(const char* filename) const;
  849. int GetRecursionDepth() const;
  850. void SetRecursionDepth(int recursionDepth);
  851. std::string NewDeferId() const;
  852. bool DeferCall(std::string id, std::string fileName, cmListFileFunction lff);
  853. bool DeferCancelCall(std::string const& id);
  854. cm::optional<std::string> DeferGetCallIds() const;
  855. cm::optional<std::string> DeferGetCall(std::string const& id) const;
  856. protected:
  857. // add link libraries and directories to the target
  858. void AddGlobalLinkInformation(cmTarget& target);
  859. mutable std::set<cmListFileContext> CMP0054ReportedIds;
  860. // libraries, classes, and executables
  861. mutable cmTargetMap Targets;
  862. std::map<std::string, std::string> AliasTargets;
  863. std::vector<cmTarget*> OrderedTargets;
  864. std::vector<std::unique_ptr<cmSourceFile>> SourceFiles;
  865. // Because cmSourceFile names are compared in a fuzzy way (see
  866. // cmSourceFileLocation::Match()) we can't have a straight mapping from
  867. // filename to cmSourceFile. To make lookups more efficient we store the
  868. // Name portion of the cmSourceFileLocation and then compare on the list of
  869. // cmSourceFiles that might match that name. Note that on platforms which
  870. // have a case-insensitive filesystem we store the key in all lowercase.
  871. using SourceFileMap =
  872. std::unordered_map<std::string, std::vector<cmSourceFile*>>;
  873. SourceFileMap SourceFileSearchIndex;
  874. // For "Known" paths we can store a direct filename to cmSourceFile map
  875. std::unordered_map<std::string, cmSourceFile*> KnownFileSearchIndex;
  876. // Tests
  877. std::map<std::string, std::unique_ptr<cmTest>> Tests;
  878. // The set of include directories that are marked as system include
  879. // directories.
  880. std::set<std::string> SystemIncludeDirectories;
  881. std::vector<std::string> ListFiles;
  882. std::vector<std::string> OutputFiles;
  883. std::vector<std::unique_ptr<cmInstallGenerator>> InstallGenerators;
  884. std::vector<std::unique_ptr<cmTestGenerator>> TestGenerators;
  885. std::string ComplainFileRegularExpression;
  886. std::string DefineFlags;
  887. // Track the value of the computed DEFINITIONS property.
  888. std::string DefineFlagsOrig;
  889. #if !defined(CMAKE_BOOTSTRAP)
  890. std::vector<cmSourceGroup> SourceGroups;
  891. size_t ObjectLibrariesSourceGroupIndex;
  892. #endif
  893. cmGlobalGenerator* GlobalGenerator;
  894. bool IsFunctionBlocked(const cmListFileFunction& lff,
  895. cmExecutionStatus& status);
  896. private:
  897. cmStateSnapshot StateSnapshot;
  898. cmListFileBacktrace Backtrace;
  899. int RecursionDepth;
  900. struct DeferCommand
  901. {
  902. // Id is empty for an already-executed or canceled operation.
  903. std::string Id;
  904. std::string FilePath;
  905. cmListFileFunction Command;
  906. };
  907. struct DeferCommands
  908. {
  909. std::vector<DeferCommand> Commands;
  910. };
  911. std::unique_ptr<DeferCommands> Defer;
  912. bool DeferRunning = false;
  913. void DoGenerate(cmLocalGenerator& lg);
  914. void RunListFile(cmListFile const& listFile,
  915. const std::string& filenametoread,
  916. DeferCommands* defer = nullptr);
  917. bool ParseDefineFlag(std::string const& definition, bool remove);
  918. bool EnforceUniqueDir(const std::string& srcPath,
  919. const std::string& binPath) const;
  920. std::function<void()> ExecuteCommandCallback;
  921. using FunctionBlockerPtr = std::unique_ptr<cmFunctionBlocker>;
  922. using FunctionBlockersType =
  923. std::stack<FunctionBlockerPtr, std::vector<FunctionBlockerPtr>>;
  924. FunctionBlockersType FunctionBlockers;
  925. std::vector<FunctionBlockersType::size_type> FunctionBlockerBarriers;
  926. void PushFunctionBlockerBarrier();
  927. void PopFunctionBlockerBarrier(bool reportError = true);
  928. std::stack<int> LoopBlockCounter;
  929. mutable cmsys::RegularExpression cmDefineRegex;
  930. mutable cmsys::RegularExpression cmDefine01Regex;
  931. mutable cmsys::RegularExpression cmAtVarRegex;
  932. mutable cmsys::RegularExpression cmNamedCurly;
  933. std::vector<cmMakefile*> UnConfiguredDirectories;
  934. std::vector<std::unique_ptr<cmExportBuildFileGenerator>>
  935. ExportBuildFileGenerators;
  936. std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>
  937. EvaluationFiles;
  938. std::vector<cmExecutionStatus*> ExecutionStatusStack;
  939. friend class cmMakefileCall;
  940. friend class cmParseFileScope;
  941. std::vector<std::unique_ptr<cmTarget>> ImportedTargetsOwned;
  942. using TargetMap = std::unordered_map<std::string, cmTarget*>;
  943. TargetMap ImportedTargets;
  944. // Internal policy stack management.
  945. void PushPolicy(bool weak = false,
  946. cmPolicies::PolicyMap const& pm = cmPolicies::PolicyMap());
  947. void PopPolicy();
  948. void PopSnapshot(bool reportError = true);
  949. friend bool cmCMakePolicyCommand(std::vector<std::string> const& args,
  950. cmExecutionStatus& status);
  951. class IncludeScope;
  952. friend class IncludeScope;
  953. class ListFileScope;
  954. friend class ListFileScope;
  955. class DeferScope;
  956. friend class DeferScope;
  957. class DeferCallScope;
  958. friend class DeferCallScope;
  959. class BuildsystemFileScope;
  960. friend class BuildsystemFileScope;
  961. // CMP0053 == old
  962. MessageType ExpandVariablesInStringOld(std::string& errorstr,
  963. std::string& source,
  964. bool escapeQuotes, bool noEscapes,
  965. bool atOnly, const char* filename,
  966. long line, bool removeEmpty,
  967. bool replaceAt) const;
  968. // CMP0053 == new
  969. MessageType ExpandVariablesInStringNew(std::string& errorstr,
  970. std::string& source,
  971. bool escapeQuotes, bool noEscapes,
  972. bool atOnly, const char* filename,
  973. long line, bool replaceAt) const;
  974. bool ValidateCustomCommand(const cmCustomCommandLines& commandLines) const;
  975. void CreateGeneratedOutputs(const std::vector<std::string>& outputs);
  976. std::vector<BT<GeneratorAction>> GeneratorActions;
  977. bool GeneratorActionsInvoked = false;
  978. bool DebugFindPkg = false;
  979. bool CheckSystemVars;
  980. bool CheckCMP0000;
  981. std::set<std::string> WarnedCMP0074;
  982. bool IsSourceFileTryCompile;
  983. mutable bool SuppressSideEffects;
  984. ImportedTargetScope CurrentImportedTargetScope = ImportedTargetScope::Local;
  985. };