cmMakefile.h 33 KB

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