cmMakefile.h 35 KB

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