cmMakefile.h 39 KB

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