cmMakefile.h 34 KB

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