cmMakefile.h 36 KB

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