cmSystemTools.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <functional>
  7. #include <string>
  8. #include <vector>
  9. #include <cm/string_view>
  10. #include "cmsys/Process.h"
  11. #include "cmsys/Status.hxx" // IWYU pragma: export
  12. #include "cmsys/SystemTools.hxx" // IWYU pragma: export
  13. #include "cmCryptoHash.h"
  14. #include "cmDuration.h"
  15. #include "cmProcessOutput.h"
  16. struct cmMessageMetadata;
  17. /** \class cmSystemTools
  18. * \brief A collection of useful functions for CMake.
  19. *
  20. * cmSystemTools is a class that provides helper functions
  21. * for the CMake build system.
  22. */
  23. class cmSystemTools : public cmsys::SystemTools
  24. {
  25. public:
  26. using Superclass = cmsys::SystemTools;
  27. using Encoding = cmProcessOutput::Encoding;
  28. /**
  29. * Look for and replace registry values in a string
  30. */
  31. static void ExpandRegistryValues(std::string& source,
  32. KeyWOW64 view = KeyWOW64_Default);
  33. /** Map help document name to file name. */
  34. static std::string HelpFileName(cm::string_view);
  35. using MessageCallback =
  36. std::function<void(const std::string&, const cmMessageMetadata&)>;
  37. /**
  38. * Set the function used by GUIs to display error messages
  39. * Function gets passed: message as a const char*,
  40. * title as a const char*.
  41. */
  42. static void SetMessageCallback(MessageCallback f);
  43. /**
  44. * Display an error message.
  45. */
  46. static void Error(const std::string& m);
  47. /**
  48. * Display a message.
  49. */
  50. static void Message(const std::string& m, const char* title = nullptr);
  51. static void Message(const std::string& m, const cmMessageMetadata& md);
  52. using OutputCallback = std::function<void(std::string const&)>;
  53. //! Send a string to stdout
  54. static void Stdout(const std::string& s);
  55. static void SetStdoutCallback(OutputCallback f);
  56. //! Send a string to stderr
  57. static void Stderr(const std::string& s);
  58. static void SetStderrCallback(OutputCallback f);
  59. using InterruptCallback = std::function<bool()>;
  60. static void SetInterruptCallback(InterruptCallback f);
  61. static bool GetInterruptFlag();
  62. //! Return true if there was an error at any point.
  63. static bool GetErrorOccurredFlag()
  64. {
  65. return cmSystemTools::s_ErrorOccurred ||
  66. cmSystemTools::s_FatalErrorOccurred || GetInterruptFlag();
  67. }
  68. //! If this is set to true, cmake stops processing commands.
  69. static void SetFatalErrorOccurred()
  70. {
  71. cmSystemTools::s_FatalErrorOccurred = true;
  72. }
  73. static void SetErrorOccurred() { cmSystemTools::s_ErrorOccurred = true; }
  74. //! Return true if there was an error at any point.
  75. static bool GetFatalErrorOccurred()
  76. {
  77. return cmSystemTools::s_FatalErrorOccurred || GetInterruptFlag();
  78. }
  79. //! Set the error occurred flag and fatal error back to false
  80. static void ResetErrorOccurredFlag()
  81. {
  82. cmSystemTools::s_FatalErrorOccurred = false;
  83. cmSystemTools::s_ErrorOccurred = false;
  84. }
  85. //! Return true if the path is a framework
  86. static bool IsPathToFramework(const std::string& path);
  87. //! Return true if the path is a macOS non-framework shared library (aka
  88. //! .dylib)
  89. static bool IsPathToMacOSSharedLibrary(const std::string& path);
  90. static bool DoesFileExistWithExtensions(
  91. const std::string& name, const std::vector<std::string>& sourceExts);
  92. /**
  93. * Check if the given file exists in one of the parent directory of the
  94. * given file or directory and if it does, return the name of the file.
  95. * Toplevel specifies the top-most directory to where it will look.
  96. */
  97. static std::string FileExistsInParentDirectories(
  98. const std::string& fname, const std::string& directory,
  99. const std::string& toplevel);
  100. static void Glob(const std::string& directory, const std::string& regexp,
  101. std::vector<std::string>& files);
  102. static void GlobDirs(const std::string& fullPath,
  103. std::vector<std::string>& files);
  104. /**
  105. * Try to find a list of files that match the "simple" globbing
  106. * expression. At this point in time the globbing expressions have
  107. * to be in form: /directory/partial_file_name*. The * character has
  108. * to be at the end of the string and it does not support ?
  109. * []... The optional argument type specifies what kind of files you
  110. * want to find. 0 means all files, -1 means directories, 1 means
  111. * files only. This method returns true if search was successful.
  112. */
  113. static bool SimpleGlob(const std::string& glob,
  114. std::vector<std::string>& files, int type = 0);
  115. enum class CopyWhen
  116. {
  117. Always,
  118. OnlyIfDifferent,
  119. };
  120. enum class CopyResult
  121. {
  122. Success,
  123. Failure,
  124. };
  125. /** Copy a file. */
  126. static bool CopySingleFile(const std::string& oldname,
  127. const std::string& newname);
  128. static CopyResult CopySingleFile(std::string const& oldname,
  129. std::string const& newname, CopyWhen when,
  130. std::string* err = nullptr);
  131. enum class Replace
  132. {
  133. Yes,
  134. No,
  135. };
  136. enum class RenameResult
  137. {
  138. Success,
  139. NoReplace,
  140. Failure,
  141. };
  142. /** Rename a file or directory within a single disk volume (atomic
  143. if possible). */
  144. static bool RenameFile(const std::string& oldname,
  145. const std::string& newname);
  146. static RenameResult RenameFile(std::string const& oldname,
  147. std::string const& newname, Replace replace,
  148. std::string* err = nullptr);
  149. //! Rename a file if contents are different, delete the source otherwise
  150. static void MoveFileIfDifferent(const std::string& source,
  151. const std::string& destination);
  152. #ifndef CMAKE_BOOTSTRAP
  153. //! Compute the hash of a file
  154. static std::string ComputeFileHash(const std::string& source,
  155. cmCryptoHash::Algo algo);
  156. /** Compute the md5sum of a string. */
  157. static std::string ComputeStringMD5(const std::string& input);
  158. # ifdef _WIN32
  159. //! Get the SHA thumbprint for a certificate file
  160. static std::string ComputeCertificateThumbprint(const std::string& source);
  161. # endif
  162. #endif
  163. /**
  164. * Run a single executable command
  165. *
  166. * Output is controlled with outputflag. If outputflag is OUTPUT_NONE, no
  167. * user-viewable output from the program being run will be generated.
  168. * OUTPUT_MERGE is the legacy behavior where stdout and stderr are merged
  169. * into stdout. OUTPUT_FORWARD copies the output to stdout/stderr as
  170. * it was received. OUTPUT_PASSTHROUGH passes through the original handles.
  171. *
  172. * If timeout is specified, the command will be terminated after
  173. * timeout expires. Timeout is specified in seconds.
  174. *
  175. * Argument retVal should be a pointer to the location where the
  176. * exit code will be stored. If the retVal is not specified and
  177. * the program exits with a code other than 0, then the this
  178. * function will return false.
  179. *
  180. * If the command has spaces in the path the caller MUST call
  181. * cmSystemTools::ConvertToRunCommandPath on the command before passing
  182. * it into this function or it will not work. The command must be correctly
  183. * escaped for this to with spaces.
  184. */
  185. enum OutputOption
  186. {
  187. OUTPUT_NONE = 0,
  188. OUTPUT_MERGE,
  189. OUTPUT_FORWARD,
  190. OUTPUT_PASSTHROUGH
  191. };
  192. static bool RunSingleCommand(const std::string& command,
  193. std::string* captureStdOut = nullptr,
  194. std::string* captureStdErr = nullptr,
  195. int* retVal = nullptr,
  196. const char* dir = nullptr,
  197. OutputOption outputflag = OUTPUT_MERGE,
  198. cmDuration timeout = cmDuration::zero());
  199. /**
  200. * In this version of RunSingleCommand, command[0] should be
  201. * the command to run, and each argument to the command should
  202. * be in command[1]...command[command.size()]
  203. */
  204. static bool RunSingleCommand(std::vector<std::string> const& command,
  205. std::string* captureStdOut = nullptr,
  206. std::string* captureStdErr = nullptr,
  207. int* retVal = nullptr,
  208. const char* dir = nullptr,
  209. OutputOption outputflag = OUTPUT_MERGE,
  210. cmDuration timeout = cmDuration::zero(),
  211. Encoding encoding = cmProcessOutput::Auto);
  212. static std::string PrintSingleCommand(std::vector<std::string> const&);
  213. /**
  214. * Parse arguments out of a single string command
  215. */
  216. static std::vector<std::string> ParseArguments(const std::string& command);
  217. /** Parse arguments out of a windows command line string. */
  218. static void ParseWindowsCommandLine(const char* command,
  219. std::vector<std::string>& args);
  220. /** Parse arguments out of a unix command line string. */
  221. static void ParseUnixCommandLine(const char* command,
  222. std::vector<std::string>& args);
  223. /** Split a command-line string into the parsed command and the unparsed
  224. arguments. Returns false on unfinished quoting or escaping. */
  225. static bool SplitProgramFromArgs(std::string const& command,
  226. std::string& program, std::string& args);
  227. /**
  228. * Handle response file in an argument list and return a new argument list
  229. * **/
  230. static std::vector<std::string> HandleResponseFile(
  231. std::vector<std::string>::const_iterator argBeg,
  232. std::vector<std::string>::const_iterator argEnd);
  233. static size_t CalculateCommandLineLengthLimit();
  234. static void DisableRunCommandOutput() { s_DisableRunCommandOutput = true; }
  235. static void EnableRunCommandOutput() { s_DisableRunCommandOutput = false; }
  236. static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
  237. enum CompareOp
  238. {
  239. OP_EQUAL = 1,
  240. OP_LESS = 2,
  241. OP_GREATER = 4,
  242. OP_LESS_EQUAL = OP_LESS | OP_EQUAL,
  243. OP_GREATER_EQUAL = OP_GREATER | OP_EQUAL
  244. };
  245. /**
  246. * Compare versions
  247. */
  248. static bool VersionCompare(CompareOp op, const std::string& lhs,
  249. const std::string& rhs);
  250. static bool VersionCompare(CompareOp op, const std::string& lhs,
  251. const char rhs[]);
  252. static bool VersionCompareEqual(std::string const& lhs,
  253. std::string const& rhs);
  254. static bool VersionCompareGreater(std::string const& lhs,
  255. std::string const& rhs);
  256. static bool VersionCompareGreaterEq(std::string const& lhs,
  257. std::string const& rhs);
  258. /**
  259. * Compare two ASCII strings using natural versioning order.
  260. * Non-numerical characters are compared directly.
  261. * Numerical characters are first globbed such that, e.g.
  262. * `test000 < test01 < test0 < test1 < test10`.
  263. * Return a value less than, equal to, or greater than zero if lhs
  264. * precedes, equals, or succeeds rhs in the defined ordering.
  265. */
  266. static int strverscmp(std::string const& lhs, std::string const& rhs);
  267. /** Windows if this is true, the CreateProcess in RunCommand will
  268. * not show new console windows when running programs.
  269. */
  270. static void SetRunCommandHideConsole(bool v) { s_RunCommandHideConsole = v; }
  271. static bool GetRunCommandHideConsole() { return s_RunCommandHideConsole; }
  272. /** Call cmSystemTools::Error with the message m, plus the
  273. * result of strerror(errno)
  274. */
  275. static void ReportLastSystemError(const char* m);
  276. /** a general output handler for cmsysProcess */
  277. static int WaitForLine(cmsysProcess* process, std::string& line,
  278. cmDuration timeout, std::vector<char>& out,
  279. std::vector<char>& err);
  280. static void SetForceUnixPaths(bool v) { s_ForceUnixPaths = v; }
  281. static bool GetForceUnixPaths() { return s_ForceUnixPaths; }
  282. // ConvertToOutputPath use s_ForceUnixPaths
  283. static std::string ConvertToOutputPath(std::string const& path);
  284. static void ConvertToOutputSlashes(std::string& path);
  285. // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
  286. // be used when RunCommand is called from cmake, because the
  287. // running cmake needs paths to be in its format
  288. static std::string ConvertToRunCommandPath(const std::string& path);
  289. /**
  290. * For windows computes the long path for the given path,
  291. * For Unix, it is a noop
  292. */
  293. static void ConvertToLongPath(std::string& path);
  294. /** compute the relative path from local to remote. local must
  295. be a directory. remote can be a file or a directory.
  296. Both remote and local must be full paths. Basically, if
  297. you are in directory local and you want to access the file in remote
  298. what is the relative path to do that. For example:
  299. /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
  300. from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
  301. */
  302. static std::string RelativePath(std::string const& local,
  303. std::string const& remote);
  304. /**
  305. * Convert the given remote path to a relative path with respect to
  306. * the given local path. Both paths must use forward slashes and not
  307. * already be escaped or quoted.
  308. */
  309. static std::string ForceToRelativePath(std::string const& local_path,
  310. std::string const& remote_path);
  311. /**
  312. * Express the 'in' path relative to 'top' if it does not start in '../'.
  313. */
  314. static std::string RelativeIfUnder(std::string const& top,
  315. std::string const& in);
  316. #ifndef CMAKE_BOOTSTRAP
  317. /** Remove an environment variable */
  318. static bool UnsetEnv(const char* value);
  319. /** Get the list of all environment variables */
  320. static std::vector<std::string> GetEnvironmentVariables();
  321. /** Append multiple variables to the current environment. */
  322. static void AppendEnv(std::vector<std::string> const& env);
  323. /** Helper class to save and restore the environment.
  324. Instantiate this class as an automatic variable on
  325. the stack. Its constructor saves a copy of the current
  326. environment and then its destructor restores the
  327. original environment. */
  328. class SaveRestoreEnvironment
  329. {
  330. public:
  331. SaveRestoreEnvironment();
  332. ~SaveRestoreEnvironment();
  333. SaveRestoreEnvironment(SaveRestoreEnvironment const&) = delete;
  334. SaveRestoreEnvironment& operator=(SaveRestoreEnvironment const&) = delete;
  335. private:
  336. std::vector<std::string> Env;
  337. };
  338. #endif
  339. /** Setup the environment to enable VS 8 IDE output. */
  340. static void EnableVSConsoleOutput();
  341. enum cmTarAction
  342. {
  343. TarActionCreate,
  344. TarActionList,
  345. TarActionExtract,
  346. TarActionNone
  347. };
  348. /** Create tar */
  349. enum cmTarCompression
  350. {
  351. TarCompressGZip,
  352. TarCompressBZip2,
  353. TarCompressXZ,
  354. TarCompressZstd,
  355. TarCompressNone
  356. };
  357. enum class cmTarExtractTimestamps
  358. {
  359. Yes,
  360. No
  361. };
  362. static bool ListTar(const std::string& outFileName,
  363. const std::vector<std::string>& files, bool verbose);
  364. static bool CreateTar(const std::string& outFileName,
  365. const std::vector<std::string>& files,
  366. cmTarCompression compressType, bool verbose,
  367. std::string const& mtime = std::string(),
  368. std::string const& format = std::string(),
  369. int compressionLevel = 0);
  370. static bool ExtractTar(const std::string& inFileName,
  371. const std::vector<std::string>& files,
  372. cmTarExtractTimestamps extractTimestamps,
  373. bool verbose);
  374. // This should be called first thing in main
  375. // it will keep child processes from inheriting the
  376. // stdin and stdout of this process. This is important
  377. // if you want to be able to kill child processes and
  378. // not get stuck waiting for all the output on the pipes.
  379. static void DoNotInheritStdPipes();
  380. static void EnsureStdPipes();
  381. /** Random seed generation. */
  382. static unsigned int RandomSeed();
  383. /** Find the directory containing CMake executables. */
  384. static void FindCMakeResources(const char* argv0);
  385. /** Get the CMake resource paths, after FindCMakeResources. */
  386. static std::string const& GetCTestCommand();
  387. static std::string const& GetCPackCommand();
  388. static std::string const& GetCMakeCommand();
  389. static std::string const& GetCMakeGUICommand();
  390. static std::string const& GetCMakeCursesCommand();
  391. static std::string const& GetCMClDepsCommand();
  392. static std::string const& GetCMakeRoot();
  393. static std::string const& GetHTMLDoc();
  394. /** Get the CWD mapped through the KWSys translation map. */
  395. static std::string GetCurrentWorkingDirectory();
  396. /** Echo a message in color using KWSys's Terminal cprintf. */
  397. static void MakefileColorEcho(int color, const char* message, bool newLine,
  398. bool enabled);
  399. /** Try to guess the soname of a shared library. */
  400. static bool GuessLibrarySOName(std::string const& fullPath,
  401. std::string& soname);
  402. /** Try to guess the install name of a shared library. */
  403. static bool GuessLibraryInstallName(std::string const& fullPath,
  404. std::string& soname);
  405. /** Try to change the RPATH in an ELF binary. */
  406. static bool ChangeRPath(std::string const& file, std::string const& oldRPath,
  407. std::string const& newRPath,
  408. bool removeEnvironmentRPath,
  409. std::string* emsg = nullptr,
  410. bool* changed = nullptr);
  411. /** Try to set the RPATH in an ELF binary. */
  412. static bool SetRPath(std::string const& file, std::string const& newRPath,
  413. std::string* emsg = nullptr, bool* changed = nullptr);
  414. /** Try to remove the RPATH from an ELF binary. */
  415. static bool RemoveRPath(std::string const& file, std::string* emsg = nullptr,
  416. bool* removed = nullptr);
  417. /** Check whether the RPATH in an ELF binary contains the path
  418. given. */
  419. static bool CheckRPath(std::string const& file, std::string const& newRPath);
  420. /** Remove a directory; repeat a few times in case of locked files. */
  421. static bool RepeatedRemoveDirectory(const std::string& dir);
  422. /** Encode a string as a URL. */
  423. static std::string EncodeURL(std::string const& in,
  424. bool escapeSlashes = true);
  425. #ifdef _WIN32
  426. struct WindowsFileRetry
  427. {
  428. unsigned int Count;
  429. unsigned int Delay;
  430. };
  431. static WindowsFileRetry GetWindowsFileRetry();
  432. static WindowsFileRetry GetWindowsDirectoryRetry();
  433. #endif
  434. /** Get the real path for a given path, removing all symlinks.
  435. This variant of GetRealPath also works on Windows but will
  436. resolve subst drives too. */
  437. static std::string GetRealPathResolvingWindowsSubst(
  438. const std::string& path, std::string* errorMessage = nullptr);
  439. /** Perform one-time initialization of libuv. */
  440. static void InitializeLibUV();
  441. /** Create a symbolic link if the platform supports it. Returns whether
  442. creation succeeded. */
  443. static cmsys::Status CreateSymlink(std::string const& origName,
  444. std::string const& newName,
  445. std::string* errorMessage = nullptr);
  446. /** Create a hard link if the platform supports it. Returns whether
  447. creation succeeded. */
  448. static cmsys::Status CreateLink(std::string const& origName,
  449. std::string const& newName,
  450. std::string* errorMessage = nullptr);
  451. /** Get the system name. */
  452. static cm::string_view GetSystemName();
  453. /** Get the system path separator character */
  454. static char GetSystemPathlistSeparator();
  455. private:
  456. static bool s_ForceUnixPaths;
  457. static bool s_RunCommandHideConsole;
  458. static bool s_ErrorOccurred;
  459. static bool s_FatalErrorOccurred;
  460. static bool s_DisableRunCommandOutput;
  461. };