cmMakefile.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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* AddNewTarget(cmStateEnums::TargetType type,
  206. const std::string& name);
  207. /** Create a target instance for the utility. */
  208. cmTarget* AddNewUtilityTarget(const std::string& utilityName,
  209. bool excludeFromAll);
  210. /**
  211. * Add an executable to the build.
  212. */
  213. cmTarget* AddExecutable(const std::string& exename,
  214. const std::vector<std::string>& srcs,
  215. bool excludeFromAll = false);
  216. /**
  217. * Dispatch adding a utility to the build. A utility target is a command
  218. * that is run every time the target is built.
  219. */
  220. cmTarget* AddUtilityCommand(const std::string& utilityName,
  221. bool excludeFromAll,
  222. std::unique_ptr<cmCustomCommand> cc);
  223. /**
  224. * Add a subdirectory to the build.
  225. */
  226. void AddSubDirectory(const std::string& fullSrcDir,
  227. const std::string& fullBinDir, bool excludeFromAll,
  228. bool immediate);
  229. void Configure();
  230. /**
  231. * Configure a subdirectory
  232. */
  233. void ConfigureSubDirectory(cmMakefile* mf);
  234. /**
  235. * Add an include directory to the build.
  236. */
  237. void AddIncludeDirectories(const std::vector<std::string>& incs,
  238. bool before = false);
  239. /**
  240. * Add a variable definition to the build. This variable
  241. * can be used in CMake to refer to lists, directories, etc.
  242. */
  243. void AddDefinition(const std::string& name, cm::string_view value);
  244. void AddDefinition(const std::string& name, cmValue value)
  245. {
  246. this->AddDefinition(name, *value);
  247. }
  248. /**
  249. * Add bool variable definition to the build.
  250. */
  251. void AddDefinitionBool(const std::string& name, bool);
  252. //! Add a definition to this makefile and the global cmake cache.
  253. void AddCacheDefinition(const std::string& name, const char* value,
  254. const char* doc, cmStateEnums::CacheEntryType type,
  255. bool force = false);
  256. void AddCacheDefinition(const std::string& name, const std::string& value,
  257. const char* doc, cmStateEnums::CacheEntryType type,
  258. bool force = false)
  259. {
  260. this->AddCacheDefinition(name, value.c_str(), doc, type, force);
  261. }
  262. /**
  263. * Remove a variable definition from the build. This is not valid
  264. * for cache entries, and will only affect the current makefile.
  265. */
  266. void RemoveDefinition(const std::string& name);
  267. //! Remove a definition from the cache.
  268. void RemoveCacheDefinition(const std::string& name) const;
  269. /**
  270. * Specify the name of the project for this build.
  271. */
  272. void SetProjectName(std::string const& name);
  273. void InitCMAKE_CONFIGURATION_TYPES(std::string const& genDefault);
  274. /* Get the default configuration */
  275. std::string GetDefaultConfiguration() const;
  276. enum GeneratorConfigQuery
  277. {
  278. IncludeEmptyConfig, // Include "" aka noconfig
  279. ExcludeEmptyConfig, // Exclude "" aka noconfig
  280. OnlyMultiConfig,
  281. };
  282. /** Get the configurations for dependency checking. */
  283. std::vector<std::string> GetGeneratorConfigs(
  284. GeneratorConfigQuery mode) const;
  285. /**
  286. * Set the name of the library.
  287. */
  288. cmTarget* AddLibrary(const std::string& libname,
  289. cmStateEnums::TargetType type,
  290. const std::vector<std::string>& srcs,
  291. bool excludeFromAll = false);
  292. void AddAlias(const std::string& libname, const std::string& tgt,
  293. bool globallyVisible = true);
  294. //@{
  295. /**
  296. * Set, Push, Pop policy values for CMake.
  297. */
  298. bool SetPolicy(cmPolicies::PolicyID id, cmPolicies::PolicyStatus status);
  299. bool SetPolicy(const char* id, cmPolicies::PolicyStatus status);
  300. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID id,
  301. bool parent_scope = false) const;
  302. bool SetPolicyVersion(std::string const& version_min,
  303. std::string const& version_max);
  304. void RecordPolicies(cmPolicies::PolicyMap& pm) const;
  305. //@}
  306. /** Helper class to push and pop policies automatically. */
  307. class PolicyPushPop
  308. {
  309. public:
  310. PolicyPushPop(cmMakefile* m);
  311. ~PolicyPushPop();
  312. PolicyPushPop(const PolicyPushPop&) = delete;
  313. PolicyPushPop& operator=(const PolicyPushPop&) = delete;
  314. private:
  315. cmMakefile* Makefile;
  316. };
  317. friend class PolicyPushPop;
  318. /**
  319. * Determine if the given context, name pair has already been reported
  320. * in context of CMP0054.
  321. */
  322. bool HasCMP0054AlreadyBeenReported(const cmListFileContext& context) const;
  323. bool IgnoreErrorsCMP0061() const;
  324. std::string const& GetHomeDirectory() const;
  325. std::string const& GetHomeOutputDirectory() const;
  326. /**
  327. * Set CMAKE_SCRIPT_MODE_FILE variable when running a -P script.
  328. */
  329. void SetScriptModeFile(std::string const& scriptfile);
  330. /**
  331. * Set CMAKE_ARGC, CMAKE_ARGV0 ... variables.
  332. */
  333. void SetArgcArgv(const std::vector<std::string>& args);
  334. std::string const& GetCurrentSourceDirectory() const;
  335. std::string const& GetCurrentBinaryDirectory() const;
  336. //@}
  337. /**
  338. * Set a regular expression that include files must match
  339. * in order to be considered as part of the depend information.
  340. */
  341. void SetIncludeRegularExpression(const std::string& regex)
  342. {
  343. this->SetProperty("INCLUDE_REGULAR_EXPRESSION", regex.c_str());
  344. }
  345. const std::string& GetIncludeRegularExpression() const
  346. {
  347. return this->GetProperty("INCLUDE_REGULAR_EXPRESSION");
  348. }
  349. /**
  350. * Set a regular expression that include files that are not found
  351. * must match in order to be considered a problem.
  352. */
  353. void SetComplainRegularExpression(const std::string& regex)
  354. {
  355. this->ComplainFileRegularExpression = regex;
  356. }
  357. const std::string& GetComplainRegularExpression() const
  358. {
  359. return this->ComplainFileRegularExpression;
  360. }
  361. // -- List of targets
  362. using cmTargetMap = std::unordered_map<std::string, cmTarget>;
  363. /** Get the target map */
  364. cmTargetMap& GetTargets() { return this->Targets; }
  365. /** Get the target map - const version */
  366. cmTargetMap const& GetTargets() const { return this->Targets; }
  367. const std::vector<std::unique_ptr<cmTarget>>& GetOwnedImportedTargets() const
  368. {
  369. return this->ImportedTargetsOwned;
  370. }
  371. std::vector<cmTarget*> GetImportedTargets() const;
  372. cmTarget* FindLocalNonAliasTarget(const std::string& name) const;
  373. /** Find a target to use in place of the given name. The target
  374. returned may be imported or built within the project. */
  375. cmTarget* FindTargetToUse(const std::string& name,
  376. bool excludeAliases = false) const;
  377. bool IsAlias(const std::string& name) const;
  378. std::map<std::string, std::string> GetAliasTargets() const
  379. {
  380. return this->AliasTargets;
  381. }
  382. /**
  383. * Mark include directories as system directories.
  384. */
  385. void AddSystemIncludeDirectories(const std::set<std::string>& incs);
  386. /** Get a cmSourceFile pointer for a given source name, if the name is
  387. * not found, then a null pointer is returned.
  388. */
  389. cmSourceFile* GetSource(
  390. const std::string& sourceName,
  391. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous) const;
  392. /** Create the source file and return it. generated
  393. * indicates if it is a generated file, this is used in determining
  394. * how to create the source file instance e.g. name
  395. */
  396. cmSourceFile* CreateSource(
  397. const std::string& sourceName, bool generated = false,
  398. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
  399. /** Get a cmSourceFile pointer for a given source name, if the name is
  400. * not found, then create the source file and return it. generated
  401. * indicates if it is a generated file, this is used in determining
  402. * how to create the source file instance e.g. name
  403. */
  404. cmSourceFile* GetOrCreateSource(
  405. const std::string& sourceName, bool generated = false,
  406. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
  407. /** Get a cmSourceFile pointer for a given source name and always mark the
  408. * file as generated, if the name is not found, then create the source file
  409. * and return it.
  410. */
  411. cmSourceFile* GetOrCreateGeneratedSource(const std::string& sourceName);
  412. void AddTargetObject(std::string const& tgtName, std::string const& objFile);
  413. /**
  414. * Given a variable name, return its value (as a string).
  415. * If the variable is not found in this makefile instance, the
  416. * cache is then queried.
  417. */
  418. cmValue GetDefinition(const std::string&) const;
  419. const std::string& GetSafeDefinition(const std::string&) const;
  420. const std::string& GetRequiredDefinition(const std::string& name) const;
  421. bool IsDefinitionSet(const std::string&) const;
  422. bool IsNormalDefinitionSet(const std::string&) const;
  423. bool GetDefExpandList(const std::string& name, std::vector<std::string>& out,
  424. bool emptyArgs = false) const;
  425. /**
  426. * Get the list of all variables in the current space. If argument
  427. * cacheonly is specified and is greater than 0, then only cache
  428. * variables will be listed.
  429. */
  430. std::vector<std::string> GetDefinitions() const;
  431. /**
  432. * Test a boolean variable to see if it is true or false.
  433. * If the variable is not found in this makefile instance, the
  434. * cache is then queried.
  435. * Returns false if no entry defined.
  436. */
  437. bool IsOn(const std::string& name) const;
  438. bool IsSet(const std::string& name) const;
  439. /** Return whether the target platform is 32-bit. */
  440. bool PlatformIs32Bit() const;
  441. /** Return whether the target platform is 64-bit. */
  442. bool PlatformIs64Bit() const;
  443. /** Return whether the target platform is x32. */
  444. bool PlatformIsx32() const;
  445. /** Apple SDK Type */
  446. enum class AppleSDK
  447. {
  448. MacOS,
  449. IPhoneOS,
  450. IPhoneSimulator,
  451. AppleTVOS,
  452. AppleTVSimulator,
  453. WatchOS,
  454. WatchSimulator,
  455. };
  456. /** What SDK type points CMAKE_OSX_SYSROOT to? */
  457. AppleSDK GetAppleSDKType() const;
  458. /** Return whether the target platform is Apple iOS. */
  459. bool PlatformIsAppleEmbedded() const;
  460. /** Retrieve soname flag for the specified language if supported */
  461. const char* GetSONameFlag(const std::string& language) const;
  462. /**
  463. * Get a list of preprocessor define flags.
  464. */
  465. std::string GetDefineFlags() const { return this->DefineFlags; }
  466. /**
  467. * Make sure CMake can write this file
  468. */
  469. bool CanIWriteThisFile(std::string const& fileName) const;
  470. #if !defined(CMAKE_BOOTSTRAP)
  471. /**
  472. * Get the vector source groups.
  473. */
  474. const std::vector<cmSourceGroup>& GetSourceGroups() const
  475. {
  476. return this->SourceGroups;
  477. }
  478. /**
  479. * Get the source group
  480. */
  481. cmSourceGroup* GetSourceGroup(const std::vector<std::string>& name) const;
  482. /**
  483. * Add a root source group for consideration when adding a new source.
  484. */
  485. void AddSourceGroup(const std::string& name, const char* regex = nullptr);
  486. /**
  487. * Add a source group for consideration when adding a new source.
  488. * name is tokenized.
  489. */
  490. void AddSourceGroup(const std::vector<std::string>& name,
  491. const char* regex = nullptr);
  492. /**
  493. * Get and existing or create a new source group.
  494. */
  495. cmSourceGroup* GetOrCreateSourceGroup(
  496. const std::vector<std::string>& folders);
  497. /**
  498. * Get and existing or create a new source group.
  499. * The name will be tokenized.
  500. */
  501. cmSourceGroup* GetOrCreateSourceGroup(const std::string& name);
  502. /**
  503. * find what source group this source is in
  504. */
  505. cmSourceGroup* FindSourceGroup(const std::string& source,
  506. std::vector<cmSourceGroup>& groups) const;
  507. #endif
  508. /**
  509. * Get the vector of list files on which this makefile depends
  510. */
  511. const std::vector<std::string>& GetListFiles() const
  512. {
  513. return this->ListFiles;
  514. }
  515. //! When the file changes cmake will be re-run from the build system.
  516. void AddCMakeDependFile(const std::string& file)
  517. {
  518. this->ListFiles.push_back(file);
  519. }
  520. void AddCMakeDependFilesFromUser();
  521. std::string FormatListFileStack() const;
  522. /**
  523. * Get the current context backtrace.
  524. */
  525. cmListFileBacktrace GetBacktrace() const;
  526. /**
  527. * Get the vector of files created by this makefile
  528. */
  529. const std::vector<std::string>& GetOutputFiles() const
  530. {
  531. return this->OutputFiles;
  532. }
  533. void AddCMakeOutputFile(const std::string& file)
  534. {
  535. this->OutputFiles.push_back(file);
  536. }
  537. /**
  538. * Expand all defined variables in the string.
  539. * Defined variables come from the this->Definitions map.
  540. * They are expanded with ${var} where var is the
  541. * entry in the this->Definitions map. Also \@var\@ is
  542. * expanded to match autoconf style expansions.
  543. */
  544. const std::string& ExpandVariablesInString(std::string& source) const;
  545. const std::string& ExpandVariablesInString(
  546. std::string& source, bool escapeQuotes, bool noEscapes,
  547. bool atOnly = false, const char* filename = nullptr, long line = -1,
  548. bool removeEmpty = false, bool replaceAt = false) const;
  549. /**
  550. * Remove any remaining variables in the string. Anything with ${var} or
  551. * \@var\@ will be removed.
  552. */
  553. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  554. /**
  555. * Expand variables in the makefiles ivars such as link directories etc
  556. */
  557. void ExpandVariablesCMP0019();
  558. /**
  559. * Replace variables and #cmakedefine lines in the given string.
  560. * See cmConfigureFileCommand for details.
  561. */
  562. void ConfigureString(const std::string& input, std::string& output,
  563. bool atOnly, bool escapeQuotes) const;
  564. /**
  565. * Copy file but change lines according to ConfigureString
  566. */
  567. int ConfigureFile(const std::string& infile, const std::string& outfile,
  568. bool copyonly, bool atOnly, bool escapeQuotes,
  569. mode_t permissions = 0, cmNewLineStyle = cmNewLineStyle());
  570. /**
  571. * Print a command's invocation
  572. */
  573. void PrintCommandTrace(cmListFileFunction const& lff,
  574. cm::optional<std::string> const& deferId = {}) const;
  575. /**
  576. * Set a callback that is invoked whenever ExecuteCommand is called.
  577. */
  578. void OnExecuteCommand(std::function<void()> callback);
  579. /**
  580. * Execute a single CMake command. Returns true if the command
  581. * succeeded or false if it failed.
  582. */
  583. bool ExecuteCommand(const cmListFileFunction& lff, cmExecutionStatus& status,
  584. cm::optional<std::string> deferId = {});
  585. //! Enable support for named language, if nil then all languages are
  586. /// enabled.
  587. void EnableLanguage(std::vector<std::string> const& languages,
  588. bool optional);
  589. cmState* GetState() const;
  590. /**
  591. * Get the variable watch. This is used to determine when certain variables
  592. * are accessed.
  593. */
  594. #ifndef CMAKE_BOOTSTRAP
  595. cmVariableWatch* GetVariableWatch() const;
  596. #endif
  597. //! Display progress or status message.
  598. void DisplayStatus(const std::string&, float) const;
  599. /**
  600. * Expand the given list file arguments into the full set after
  601. * variable replacement and list expansion.
  602. */
  603. bool ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  604. std::vector<std::string>& outArgs) const;
  605. bool ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  606. std::vector<cmExpandedCommandArgument>& outArgs) const;
  607. /**
  608. * Get the instance
  609. */
  610. cmake* GetCMakeInstance() const;
  611. cmMessenger* GetMessenger() const;
  612. cmGlobalGenerator* GetGlobalGenerator() const;
  613. /**
  614. * Get all the source files this makefile knows about
  615. */
  616. const std::vector<std::unique_ptr<cmSourceFile>>& GetSourceFiles() const
  617. {
  618. return this->SourceFiles;
  619. }
  620. std::vector<cmTarget*> const& GetOrderedTargets() const
  621. {
  622. return this->OrderedTargets;
  623. }
  624. //! Add a new cmTest to the list of tests for this makefile.
  625. cmTest* CreateTest(const std::string& testName);
  626. /** Get a cmTest pointer for a given test name, if the name is
  627. * not found, then a null pointer is returned.
  628. */
  629. cmTest* GetTest(const std::string& testName) const;
  630. /**
  631. * Get all tests that run under the given configuration.
  632. */
  633. void GetTests(const std::string& config, std::vector<cmTest*>& tests) const;
  634. /**
  635. * Return a location of a file in cmake or custom modules directory
  636. */
  637. std::string GetModulesFile(const std::string& name) const
  638. {
  639. bool system;
  640. std::string debugBuffer;
  641. return this->GetModulesFile(name, system, false, debugBuffer);
  642. }
  643. /**
  644. * Return a location of a file in cmake or custom modules directory
  645. */
  646. std::string GetModulesFile(const std::string& name, bool& system) const
  647. {
  648. std::string debugBuffer;
  649. return this->GetModulesFile(name, system, false, debugBuffer);
  650. }
  651. std::string GetModulesFile(const std::string& name, bool& system, bool debug,
  652. std::string& debugBuffer) const;
  653. //! Set/Get a property of this directory
  654. void SetProperty(const std::string& prop, const char* value);
  655. void SetProperty(const std::string& prop, cmValue value);
  656. void SetProperty(const std::string& prop, const std::string& value)
  657. {
  658. this->SetProperty(prop, cmValue(value));
  659. }
  660. void AppendProperty(const std::string& prop, const std::string& value,
  661. bool asString = false);
  662. cmValue GetProperty(const std::string& prop) const;
  663. cmValue GetProperty(const std::string& prop, bool chain) const;
  664. bool GetPropertyAsBool(const std::string& prop) const;
  665. std::vector<std::string> GetPropertyKeys() const;
  666. void CheckProperty(const std::string& prop) const;
  667. //! Initialize a makefile from its parent
  668. void InitializeFromParent(cmMakefile* parent);
  669. void AddInstallGenerator(std::unique_ptr<cmInstallGenerator> g);
  670. std::vector<std::unique_ptr<cmInstallGenerator>>& GetInstallGenerators()
  671. {
  672. return this->InstallGenerators;
  673. }
  674. const std::vector<std::unique_ptr<cmInstallGenerator>>&
  675. GetInstallGenerators() const
  676. {
  677. return this->InstallGenerators;
  678. }
  679. void AddTestGenerator(std::unique_ptr<cmTestGenerator> g);
  680. const std::vector<std::unique_ptr<cmTestGenerator>>& GetTestGenerators()
  681. const
  682. {
  683. return this->TestGenerators;
  684. }
  685. class FunctionPushPop
  686. {
  687. public:
  688. FunctionPushPop(cmMakefile* mf, std::string const& fileName,
  689. cmPolicies::PolicyMap const& pm);
  690. ~FunctionPushPop();
  691. FunctionPushPop(const FunctionPushPop&) = delete;
  692. FunctionPushPop& operator=(const FunctionPushPop&) = delete;
  693. void Quiet() { this->ReportError = false; }
  694. private:
  695. cmMakefile* Makefile;
  696. bool ReportError;
  697. };
  698. class MacroPushPop
  699. {
  700. public:
  701. MacroPushPop(cmMakefile* mf, std::string const& fileName,
  702. cmPolicies::PolicyMap const& pm);
  703. ~MacroPushPop();
  704. MacroPushPop(const MacroPushPop&) = delete;
  705. MacroPushPop& operator=(const MacroPushPop&) = delete;
  706. void Quiet() { this->ReportError = false; }
  707. private:
  708. cmMakefile* Makefile;
  709. bool ReportError;
  710. };
  711. void PushFunctionScope(std::string const& fileName,
  712. cmPolicies::PolicyMap const& pm);
  713. void PopFunctionScope(bool reportError);
  714. void PushMacroScope(std::string const& fileName,
  715. cmPolicies::PolicyMap const& pm);
  716. void PopMacroScope(bool reportError);
  717. void PushScope();
  718. void PopScope();
  719. void RaiseScope(const std::string& var, const char* value);
  720. // push and pop loop scopes
  721. void PushLoopBlockBarrier();
  722. void PopLoopBlockBarrier();
  723. /** Helper class to push and pop scopes automatically. */
  724. class ScopePushPop
  725. {
  726. public:
  727. ScopePushPop(cmMakefile* m)
  728. : Makefile(m)
  729. {
  730. this->Makefile->PushScope();
  731. }
  732. ~ScopePushPop() { this->Makefile->PopScope(); }
  733. ScopePushPop(ScopePushPop const&) = delete;
  734. ScopePushPop& operator=(ScopePushPop const&) = delete;
  735. private:
  736. cmMakefile* Makefile;
  737. };
  738. void IssueMessage(MessageType t, std::string const& text) const;
  739. /** Set whether or not to report a CMP0000 violation. */
  740. void SetCheckCMP0000(bool b) { this->CheckCMP0000 = b; }
  741. bool CheckCMP0037(std::string const& targetName,
  742. cmStateEnums::TargetType targetType) const;
  743. cmBTStringRange GetIncludeDirectoriesEntries() const;
  744. cmBTStringRange GetCompileOptionsEntries() const;
  745. cmBTStringRange GetCompileDefinitionsEntries() const;
  746. cmBTStringRange GetLinkOptionsEntries() const;
  747. cmBTStringRange GetLinkDirectoriesEntries() const;
  748. std::set<std::string> const& GetSystemIncludeDirectories() const
  749. {
  750. return this->SystemIncludeDirectories;
  751. }
  752. bool PolicyOptionalWarningEnabled(std::string const& var) const;
  753. void PushLoopBlock();
  754. void PopLoopBlock();
  755. bool IsLoopBlock() const;
  756. void ClearMatches();
  757. void StoreMatches(cmsys::RegularExpression& re);
  758. cmStateSnapshot GetStateSnapshot() const;
  759. const char* GetDefineFlagsCMP0059() const;
  760. void EnforceDirectoryLevelRules() const;
  761. void AddEvaluationFile(
  762. const std::string& inputFile, const std::string& targetName,
  763. std::unique_ptr<cmCompiledGeneratorExpression> outputName,
  764. std::unique_ptr<cmCompiledGeneratorExpression> condition,
  765. const std::string& newLineCharacter, mode_t permissions,
  766. bool inputIsContent);
  767. const std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>&
  768. GetEvaluationFiles() const;
  769. std::vector<std::unique_ptr<cmExportBuildFileGenerator>> const&
  770. GetExportBuildFileGenerators() const;
  771. void RemoveExportBuildFileGeneratorCMP0024(cmExportBuildFileGenerator* gen);
  772. void AddExportBuildFileGenerator(
  773. std::unique_ptr<cmExportBuildFileGenerator> gen);
  774. // Maintain a stack of package roots to allow nested PACKAGE_ROOT_PATH
  775. // searches
  776. std::deque<std::vector<std::string>> FindPackageRootPathStack;
  777. void MaybeWarnCMP0074(std::string const& pkg);
  778. void MaybeWarnUninitialized(std::string const& variable,
  779. const char* sourceFilename) const;
  780. bool IsProjectFile(const char* filename) const;
  781. int GetRecursionDepth() const;
  782. void SetRecursionDepth(int recursionDepth);
  783. std::string NewDeferId() const;
  784. bool DeferCall(std::string id, std::string fileName, cmListFileFunction lff);
  785. bool DeferCancelCall(std::string const& id);
  786. cm::optional<std::string> DeferGetCallIds() const;
  787. cm::optional<std::string> DeferGetCall(std::string const& id) const;
  788. protected:
  789. // add link libraries and directories to the target
  790. void AddGlobalLinkInformation(cmTarget& target);
  791. mutable std::set<cmListFileContext> CMP0054ReportedIds;
  792. // libraries, classes, and executables
  793. mutable cmTargetMap Targets;
  794. std::map<std::string, std::string> AliasTargets;
  795. std::vector<cmTarget*> OrderedTargets;
  796. std::vector<std::unique_ptr<cmSourceFile>> SourceFiles;
  797. // Because cmSourceFile names are compared in a fuzzy way (see
  798. // cmSourceFileLocation::Match()) we can't have a straight mapping from
  799. // filename to cmSourceFile. To make lookups more efficient we store the
  800. // Name portion of the cmSourceFileLocation and then compare on the list of
  801. // cmSourceFiles that might match that name. Note that on platforms which
  802. // have a case-insensitive filesystem we store the key in all lowercase.
  803. using SourceFileMap =
  804. std::unordered_map<std::string, std::vector<cmSourceFile*>>;
  805. SourceFileMap SourceFileSearchIndex;
  806. // For "Known" paths we can store a direct filename to cmSourceFile map
  807. std::unordered_map<std::string, cmSourceFile*> KnownFileSearchIndex;
  808. // Tests
  809. std::map<std::string, std::unique_ptr<cmTest>> Tests;
  810. // The set of include directories that are marked as system include
  811. // directories.
  812. std::set<std::string> SystemIncludeDirectories;
  813. std::vector<std::string> ListFiles;
  814. std::vector<std::string> OutputFiles;
  815. std::vector<std::unique_ptr<cmInstallGenerator>> InstallGenerators;
  816. std::vector<std::unique_ptr<cmTestGenerator>> TestGenerators;
  817. std::string ComplainFileRegularExpression;
  818. std::string DefineFlags;
  819. // Track the value of the computed DEFINITIONS property.
  820. std::string DefineFlagsOrig;
  821. #if !defined(CMAKE_BOOTSTRAP)
  822. std::vector<cmSourceGroup> SourceGroups;
  823. size_t ObjectLibrariesSourceGroupIndex;
  824. #endif
  825. cmGlobalGenerator* GlobalGenerator;
  826. bool IsFunctionBlocked(const cmListFileFunction& lff,
  827. cmExecutionStatus& status);
  828. private:
  829. cmStateSnapshot StateSnapshot;
  830. cmListFileBacktrace Backtrace;
  831. int RecursionDepth;
  832. struct DeferCommand
  833. {
  834. // Id is empty for an already-executed or canceled operation.
  835. std::string Id;
  836. std::string FilePath;
  837. cmListFileFunction Command;
  838. };
  839. struct DeferCommands
  840. {
  841. std::vector<DeferCommand> Commands;
  842. };
  843. std::unique_ptr<DeferCommands> Defer;
  844. bool DeferRunning = false;
  845. void DoGenerate(cmLocalGenerator& lg);
  846. void RunListFile(cmListFile const& listFile,
  847. const std::string& filenametoread,
  848. DeferCommands* defer = nullptr);
  849. bool ParseDefineFlag(std::string const& definition, bool remove);
  850. bool EnforceUniqueDir(const std::string& srcPath,
  851. const std::string& binPath) const;
  852. std::function<void()> ExecuteCommandCallback;
  853. using FunctionBlockerPtr = std::unique_ptr<cmFunctionBlocker>;
  854. using FunctionBlockersType =
  855. std::stack<FunctionBlockerPtr, std::vector<FunctionBlockerPtr>>;
  856. FunctionBlockersType FunctionBlockers;
  857. std::vector<FunctionBlockersType::size_type> FunctionBlockerBarriers;
  858. void PushFunctionBlockerBarrier();
  859. void PopFunctionBlockerBarrier(bool reportError = true);
  860. std::stack<int> LoopBlockCounter;
  861. mutable cmsys::RegularExpression cmDefineRegex;
  862. mutable cmsys::RegularExpression cmDefine01Regex;
  863. mutable cmsys::RegularExpression cmAtVarRegex;
  864. mutable cmsys::RegularExpression cmNamedCurly;
  865. std::vector<cmMakefile*> UnConfiguredDirectories;
  866. std::vector<std::unique_ptr<cmExportBuildFileGenerator>>
  867. ExportBuildFileGenerators;
  868. std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>
  869. EvaluationFiles;
  870. std::vector<cmExecutionStatus*> ExecutionStatusStack;
  871. friend class cmMakefileCall;
  872. friend class cmParseFileScope;
  873. std::vector<std::unique_ptr<cmTarget>> ImportedTargetsOwned;
  874. using TargetMap = std::unordered_map<std::string, cmTarget*>;
  875. TargetMap ImportedTargets;
  876. // Internal policy stack management.
  877. void PushPolicy(bool weak = false,
  878. cmPolicies::PolicyMap const& pm = cmPolicies::PolicyMap());
  879. void PopPolicy();
  880. void PopSnapshot(bool reportError = true);
  881. friend bool cmCMakePolicyCommand(std::vector<std::string> const& args,
  882. cmExecutionStatus& status);
  883. class IncludeScope;
  884. friend class IncludeScope;
  885. class ListFileScope;
  886. friend class ListFileScope;
  887. class DeferScope;
  888. friend class DeferScope;
  889. class DeferCallScope;
  890. friend class DeferCallScope;
  891. class BuildsystemFileScope;
  892. friend class BuildsystemFileScope;
  893. // CMP0053 == old
  894. MessageType ExpandVariablesInStringOld(std::string& errorstr,
  895. std::string& source,
  896. bool escapeQuotes, bool noEscapes,
  897. bool atOnly, const char* filename,
  898. long line, bool removeEmpty,
  899. bool replaceAt) const;
  900. // CMP0053 == new
  901. MessageType ExpandVariablesInStringNew(std::string& errorstr,
  902. std::string& source,
  903. bool escapeQuotes, bool noEscapes,
  904. bool atOnly, const char* filename,
  905. long line, bool replaceAt) const;
  906. bool ValidateCustomCommand(const cmCustomCommandLines& commandLines) const;
  907. void CreateGeneratedOutputs(const std::vector<std::string>& outputs);
  908. std::vector<BT<GeneratorAction>> GeneratorActions;
  909. bool GeneratorActionsInvoked = false;
  910. bool CheckSystemVars;
  911. bool CheckCMP0000;
  912. std::set<std::string> WarnedCMP0074;
  913. bool IsSourceFileTryCompile;
  914. mutable bool SuppressSideEffects;
  915. };