cmMakefile.h 37 KB

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