cmMakefile.h 35 KB

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