cmMakefile.h 35 KB

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