cmMakefile.h 40 KB

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