cmMakefile.h 33 KB

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