cmMakefile.h 35 KB

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