cmMakefile.h 37 KB

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