cmMakefile.h 37 KB

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