cmMakefile.h 39 KB

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