cmMakefile.h 34 KB

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