cmMakefile.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. enum class CommandMissingFromStack
  571. {
  572. No,
  573. Yes,
  574. };
  575. /**
  576. * Print a command's invocation
  577. */
  578. void PrintCommandTrace(
  579. cmListFileFunction const& lff, cmListFileBacktrace const& bt,
  580. CommandMissingFromStack missing = CommandMissingFromStack::No) const;
  581. /**
  582. * Set a callback that is invoked whenever ExecuteCommand is called.
  583. */
  584. void OnExecuteCommand(std::function<void()> callback);
  585. /**
  586. * Execute a single CMake command. Returns true if the command
  587. * succeeded or false if it failed.
  588. */
  589. bool ExecuteCommand(const cmListFileFunction& lff, cmExecutionStatus& status,
  590. cm::optional<std::string> deferId = {});
  591. //! Enable support for named language, if nil then all languages are
  592. /// enabled.
  593. void EnableLanguage(std::vector<std::string> const& languages,
  594. bool optional);
  595. cmState* GetState() const;
  596. /**
  597. * Get the variable watch. This is used to determine when certain variables
  598. * are accessed.
  599. */
  600. #ifndef CMAKE_BOOTSTRAP
  601. cmVariableWatch* GetVariableWatch() const;
  602. #endif
  603. //! Display progress or status message.
  604. void DisplayStatus(const std::string&, float) const;
  605. /**
  606. * Expand the given list file arguments into the full set after
  607. * variable replacement and list expansion.
  608. */
  609. bool ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  610. std::vector<std::string>& outArgs) const;
  611. bool ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  612. std::vector<cmExpandedCommandArgument>& outArgs) const;
  613. /**
  614. * Get the instance
  615. */
  616. cmake* GetCMakeInstance() const;
  617. cmMessenger* GetMessenger() const;
  618. cmGlobalGenerator* GetGlobalGenerator() const;
  619. /**
  620. * Get all the source files this makefile knows about
  621. */
  622. const std::vector<std::unique_ptr<cmSourceFile>>& GetSourceFiles() const
  623. {
  624. return this->SourceFiles;
  625. }
  626. std::vector<cmTarget*> const& GetOrderedTargets() const
  627. {
  628. return this->OrderedTargets;
  629. }
  630. //! Add a new cmTest to the list of tests for this makefile.
  631. cmTest* CreateTest(const std::string& testName);
  632. /** Get a cmTest pointer for a given test name, if the name is
  633. * not found, then a null pointer is returned.
  634. */
  635. cmTest* GetTest(const std::string& testName) const;
  636. /**
  637. * Get all tests that run under the given configuration.
  638. */
  639. void GetTests(const std::string& config, std::vector<cmTest*>& tests) const;
  640. /**
  641. * Return a location of a file in cmake or custom modules directory
  642. */
  643. std::string GetModulesFile(const std::string& name) const
  644. {
  645. bool system;
  646. std::string debugBuffer;
  647. return this->GetModulesFile(name, system, false, debugBuffer);
  648. }
  649. /**
  650. * Return a location of a file in cmake or custom modules directory
  651. */
  652. std::string GetModulesFile(const std::string& name, bool& system) const
  653. {
  654. std::string debugBuffer;
  655. return this->GetModulesFile(name, system, false, debugBuffer);
  656. }
  657. std::string GetModulesFile(const std::string& name, bool& system, bool debug,
  658. std::string& debugBuffer) const;
  659. //! Set/Get a property of this directory
  660. void SetProperty(const std::string& prop, const char* value);
  661. void SetProperty(const std::string& prop, cmValue value);
  662. void SetProperty(const std::string& prop, const std::string& value)
  663. {
  664. this->SetProperty(prop, cmValue(value));
  665. }
  666. void AppendProperty(const std::string& prop, const std::string& value,
  667. bool asString = false);
  668. cmValue GetProperty(const std::string& prop) const;
  669. cmValue GetProperty(const std::string& prop, bool chain) const;
  670. bool GetPropertyAsBool(const std::string& prop) const;
  671. std::vector<std::string> GetPropertyKeys() const;
  672. void CheckProperty(const std::string& prop) const;
  673. //! Initialize a makefile from its parent
  674. void InitializeFromParent(cmMakefile* parent);
  675. void AddInstallGenerator(std::unique_ptr<cmInstallGenerator> g);
  676. std::vector<std::unique_ptr<cmInstallGenerator>>& GetInstallGenerators()
  677. {
  678. return this->InstallGenerators;
  679. }
  680. const std::vector<std::unique_ptr<cmInstallGenerator>>&
  681. GetInstallGenerators() const
  682. {
  683. return this->InstallGenerators;
  684. }
  685. void AddTestGenerator(std::unique_ptr<cmTestGenerator> g);
  686. const std::vector<std::unique_ptr<cmTestGenerator>>& GetTestGenerators()
  687. const
  688. {
  689. return this->TestGenerators;
  690. }
  691. class FunctionPushPop
  692. {
  693. public:
  694. FunctionPushPop(cmMakefile* mf, std::string const& fileName,
  695. cmPolicies::PolicyMap const& pm);
  696. ~FunctionPushPop();
  697. FunctionPushPop(const FunctionPushPop&) = delete;
  698. FunctionPushPop& operator=(const FunctionPushPop&) = delete;
  699. void Quiet() { this->ReportError = false; }
  700. private:
  701. cmMakefile* Makefile;
  702. bool ReportError = true;
  703. };
  704. class MacroPushPop
  705. {
  706. public:
  707. MacroPushPop(cmMakefile* mf, std::string const& fileName,
  708. cmPolicies::PolicyMap const& pm);
  709. ~MacroPushPop();
  710. MacroPushPop(const MacroPushPop&) = delete;
  711. MacroPushPop& operator=(const MacroPushPop&) = delete;
  712. void Quiet() { this->ReportError = false; }
  713. private:
  714. cmMakefile* Makefile;
  715. bool ReportError = true;
  716. };
  717. void PushFunctionScope(std::string const& fileName,
  718. cmPolicies::PolicyMap const& pm);
  719. void PopFunctionScope(bool reportError);
  720. void PushMacroScope(std::string const& fileName,
  721. cmPolicies::PolicyMap const& pm);
  722. void PopMacroScope(bool reportError);
  723. void PushScope();
  724. void PopScope();
  725. void RaiseScope(const std::string& var, const char* value);
  726. // push and pop loop scopes
  727. void PushLoopBlockBarrier();
  728. void PopLoopBlockBarrier();
  729. bool IsImportedTargetGlobalScope() const;
  730. enum class ImportedTargetScope
  731. {
  732. Local,
  733. Global,
  734. };
  735. /** Helper class to manage whether imported packages
  736. * should be globally scoped based off the find package command
  737. */
  738. class SetGlobalTargetImportScope
  739. {
  740. public:
  741. SetGlobalTargetImportScope(cmMakefile* mk, ImportedTargetScope const scope)
  742. : Makefile(mk)
  743. {
  744. if (scope == ImportedTargetScope::Global &&
  745. !this->Makefile->IsImportedTargetGlobalScope()) {
  746. this->Makefile->CurrentImportedTargetScope = scope;
  747. this->Set = true;
  748. } else {
  749. this->Set = false;
  750. }
  751. }
  752. ~SetGlobalTargetImportScope()
  753. {
  754. if (this->Set) {
  755. this->Makefile->CurrentImportedTargetScope =
  756. ImportedTargetScope::Local;
  757. }
  758. }
  759. private:
  760. cmMakefile* Makefile;
  761. bool Set;
  762. };
  763. /** Helper class to push and pop scopes automatically. */
  764. class ScopePushPop
  765. {
  766. public:
  767. ScopePushPop(cmMakefile* m)
  768. : Makefile(m)
  769. {
  770. this->Makefile->PushScope();
  771. }
  772. ~ScopePushPop() { this->Makefile->PopScope(); }
  773. ScopePushPop(ScopePushPop const&) = delete;
  774. ScopePushPop& operator=(ScopePushPop const&) = delete;
  775. private:
  776. cmMakefile* Makefile;
  777. };
  778. void IssueMessage(MessageType t, std::string const& text) const;
  779. /** Set whether or not to report a CMP0000 violation. */
  780. void SetCheckCMP0000(bool b) { this->CheckCMP0000 = b; }
  781. bool CheckCMP0037(std::string const& targetName,
  782. cmStateEnums::TargetType targetType) const;
  783. cmBTStringRange GetIncludeDirectoriesEntries() const;
  784. cmBTStringRange GetCompileOptionsEntries() const;
  785. cmBTStringRange GetCompileDefinitionsEntries() const;
  786. cmBTStringRange GetLinkOptionsEntries() const;
  787. cmBTStringRange GetLinkDirectoriesEntries() const;
  788. std::set<std::string> const& GetSystemIncludeDirectories() const
  789. {
  790. return this->SystemIncludeDirectories;
  791. }
  792. bool PolicyOptionalWarningEnabled(std::string const& var) const;
  793. void PushLoopBlock();
  794. void PopLoopBlock();
  795. bool IsLoopBlock() const;
  796. void ClearMatches();
  797. void StoreMatches(cmsys::RegularExpression& re);
  798. cmStateSnapshot GetStateSnapshot() const;
  799. const char* GetDefineFlagsCMP0059() const;
  800. void EnforceDirectoryLevelRules() const;
  801. void AddEvaluationFile(
  802. const std::string& inputFile, const std::string& targetName,
  803. std::unique_ptr<cmCompiledGeneratorExpression> outputName,
  804. std::unique_ptr<cmCompiledGeneratorExpression> condition,
  805. const std::string& newLineCharacter, mode_t permissions,
  806. bool inputIsContent);
  807. const std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>&
  808. GetEvaluationFiles() const;
  809. std::vector<std::unique_ptr<cmExportBuildFileGenerator>> const&
  810. GetExportBuildFileGenerators() const;
  811. void RemoveExportBuildFileGeneratorCMP0024(cmExportBuildFileGenerator* gen);
  812. void AddExportBuildFileGenerator(
  813. std::unique_ptr<cmExportBuildFileGenerator> gen);
  814. // Maintain a stack of package roots to allow nested PACKAGE_ROOT_PATH
  815. // searches
  816. std::deque<std::vector<std::string>> FindPackageRootPathStack;
  817. class DebugFindPkgRAII
  818. {
  819. cmMakefile* Makefile;
  820. bool OldValue;
  821. public:
  822. DebugFindPkgRAII(cmMakefile* mf, std::string const& pkg);
  823. ~DebugFindPkgRAII();
  824. };
  825. bool GetDebugFindPkgMode() const;
  826. void MaybeWarnCMP0074(std::string const& pkg);
  827. void MaybeWarnUninitialized(std::string const& variable,
  828. const char* sourceFilename) const;
  829. bool IsProjectFile(const char* filename) const;
  830. int GetRecursionDepth() const;
  831. void SetRecursionDepth(int recursionDepth);
  832. std::string NewDeferId() const;
  833. bool DeferCall(std::string id, std::string fileName, cmListFileFunction lff);
  834. bool DeferCancelCall(std::string const& id);
  835. cm::optional<std::string> DeferGetCallIds() const;
  836. cm::optional<std::string> DeferGetCall(std::string const& id) const;
  837. protected:
  838. // add link libraries and directories to the target
  839. void AddGlobalLinkInformation(cmTarget& target);
  840. mutable std::set<cmListFileContext> CMP0054ReportedIds;
  841. // libraries, classes, and executables
  842. mutable cmTargetMap Targets;
  843. std::map<std::string, std::string> AliasTargets;
  844. std::vector<cmTarget*> OrderedTargets;
  845. std::vector<std::unique_ptr<cmSourceFile>> SourceFiles;
  846. // Because cmSourceFile names are compared in a fuzzy way (see
  847. // cmSourceFileLocation::Match()) we can't have a straight mapping from
  848. // filename to cmSourceFile. To make lookups more efficient we store the
  849. // Name portion of the cmSourceFileLocation and then compare on the list of
  850. // cmSourceFiles that might match that name. Note that on platforms which
  851. // have a case-insensitive filesystem we store the key in all lowercase.
  852. using SourceFileMap =
  853. std::unordered_map<std::string, std::vector<cmSourceFile*>>;
  854. SourceFileMap SourceFileSearchIndex;
  855. // For "Known" paths we can store a direct filename to cmSourceFile map
  856. std::unordered_map<std::string, cmSourceFile*> KnownFileSearchIndex;
  857. // Tests
  858. std::map<std::string, std::unique_ptr<cmTest>> Tests;
  859. // The set of include directories that are marked as system include
  860. // directories.
  861. std::set<std::string> SystemIncludeDirectories;
  862. std::vector<std::string> ListFiles;
  863. std::vector<std::string> OutputFiles;
  864. std::vector<std::unique_ptr<cmInstallGenerator>> InstallGenerators;
  865. std::vector<std::unique_ptr<cmTestGenerator>> TestGenerators;
  866. std::string ComplainFileRegularExpression;
  867. std::string DefineFlags;
  868. // Track the value of the computed DEFINITIONS property.
  869. std::string DefineFlagsOrig;
  870. #if !defined(CMAKE_BOOTSTRAP)
  871. std::vector<cmSourceGroup> SourceGroups;
  872. size_t ObjectLibrariesSourceGroupIndex;
  873. #endif
  874. cmGlobalGenerator* GlobalGenerator;
  875. bool IsFunctionBlocked(const cmListFileFunction& lff,
  876. cmExecutionStatus& status);
  877. private:
  878. cmStateSnapshot StateSnapshot;
  879. cmListFileBacktrace Backtrace;
  880. int RecursionDepth;
  881. struct DeferCommand
  882. {
  883. // Id is empty for an already-executed or canceled operation.
  884. std::string Id;
  885. std::string FilePath;
  886. cmListFileFunction Command;
  887. };
  888. struct DeferCommands
  889. {
  890. std::vector<DeferCommand> Commands;
  891. };
  892. std::unique_ptr<DeferCommands> Defer;
  893. bool DeferRunning = false;
  894. void DoGenerate(cmLocalGenerator& lg);
  895. void RunListFile(cmListFile const& listFile,
  896. const std::string& filenametoread,
  897. DeferCommands* defer = nullptr);
  898. bool ParseDefineFlag(std::string const& definition, bool remove);
  899. bool EnforceUniqueDir(const std::string& srcPath,
  900. const std::string& binPath) const;
  901. std::function<void()> ExecuteCommandCallback;
  902. using FunctionBlockerPtr = std::unique_ptr<cmFunctionBlocker>;
  903. using FunctionBlockersType =
  904. std::stack<FunctionBlockerPtr, std::vector<FunctionBlockerPtr>>;
  905. FunctionBlockersType FunctionBlockers;
  906. std::vector<FunctionBlockersType::size_type> FunctionBlockerBarriers;
  907. void PushFunctionBlockerBarrier();
  908. void PopFunctionBlockerBarrier(bool reportError = true);
  909. std::stack<int> LoopBlockCounter;
  910. mutable cmsys::RegularExpression cmDefineRegex;
  911. mutable cmsys::RegularExpression cmDefine01Regex;
  912. mutable cmsys::RegularExpression cmAtVarRegex;
  913. mutable cmsys::RegularExpression cmNamedCurly;
  914. std::vector<cmMakefile*> UnConfiguredDirectories;
  915. std::vector<std::unique_ptr<cmExportBuildFileGenerator>>
  916. ExportBuildFileGenerators;
  917. std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>
  918. EvaluationFiles;
  919. std::vector<cmExecutionStatus*> ExecutionStatusStack;
  920. friend class cmMakefileCall;
  921. friend class cmParseFileScope;
  922. std::vector<std::unique_ptr<cmTarget>> ImportedTargetsOwned;
  923. using TargetMap = std::unordered_map<std::string, cmTarget*>;
  924. TargetMap ImportedTargets;
  925. // Internal policy stack management.
  926. void PushPolicy(bool weak = false,
  927. cmPolicies::PolicyMap const& pm = cmPolicies::PolicyMap());
  928. void PopPolicy();
  929. void PopSnapshot(bool reportError = true);
  930. friend bool cmCMakePolicyCommand(std::vector<std::string> const& args,
  931. cmExecutionStatus& status);
  932. class IncludeScope;
  933. friend class IncludeScope;
  934. class ListFileScope;
  935. friend class ListFileScope;
  936. class DeferScope;
  937. friend class DeferScope;
  938. class DeferCallScope;
  939. friend class DeferCallScope;
  940. class BuildsystemFileScope;
  941. friend class BuildsystemFileScope;
  942. // CMP0053 == old
  943. MessageType ExpandVariablesInStringOld(std::string& errorstr,
  944. std::string& source,
  945. bool escapeQuotes, bool noEscapes,
  946. bool atOnly, const char* filename,
  947. long line, bool removeEmpty,
  948. bool replaceAt) const;
  949. // CMP0053 == new
  950. MessageType ExpandVariablesInStringNew(std::string& errorstr,
  951. std::string& source,
  952. bool escapeQuotes, bool noEscapes,
  953. bool atOnly, const char* filename,
  954. long line, bool replaceAt) const;
  955. bool ValidateCustomCommand(const cmCustomCommandLines& commandLines) const;
  956. void CreateGeneratedOutputs(const std::vector<std::string>& outputs);
  957. std::vector<BT<GeneratorAction>> GeneratorActions;
  958. bool GeneratorActionsInvoked = false;
  959. bool DebugFindPkg = false;
  960. bool CheckSystemVars;
  961. bool CheckCMP0000;
  962. std::set<std::string> WarnedCMP0074;
  963. bool IsSourceFileTryCompile;
  964. mutable bool SuppressSideEffects;
  965. ImportedTargetScope CurrentImportedTargetScope = ImportedTargetScope::Local;
  966. };