cmMakefile.h 37 KB

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