cmMakefile.h 33 KB

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