cmMakefile.h 36 KB

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