cmMakefile.h 37 KB

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