cmMakefile.h 39 KB

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