cmMakefile.h 42 KB

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