cmMakefile.h 35 KB

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