cmMakefile.h 34 KB

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