cmSystemTools.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #if !defined(_WIN32)
  6. # include <sys/types.h>
  7. #endif
  8. #include <cstddef>
  9. #include <functional>
  10. #include <map>
  11. #include <sstream>
  12. #include <string>
  13. #include <vector>
  14. #include <cm/optional>
  15. #include <cm/string_view>
  16. #include <cm3p/uv.h>
  17. #include "cmsys/Status.hxx" // IWYU pragma: export
  18. #include "cmsys/SystemTools.hxx" // IWYU pragma: export
  19. #include "cmDuration.h"
  20. #include "cmProcessOutput.h"
  21. struct cmMessageMetadata;
  22. /** \class cmSystemTools
  23. * \brief A collection of useful functions for CMake.
  24. *
  25. * cmSystemTools is a class that provides helper functions
  26. * for the CMake build system.
  27. */
  28. class cmSystemTools : public cmsys::SystemTools
  29. {
  30. public:
  31. using Superclass = cmsys::SystemTools;
  32. using Encoding = cmProcessOutput::Encoding;
  33. /**
  34. * Return a lower case string
  35. */
  36. static std::string LowerCase(cm::string_view);
  37. static std::string LowerCase(char const* s)
  38. {
  39. return LowerCase(cm::string_view{ s });
  40. }
  41. using cmsys::SystemTools::LowerCase;
  42. /**
  43. * Return an upper case string
  44. */
  45. static std::string UpperCase(cm::string_view);
  46. static std::string UpperCase(char const* s)
  47. {
  48. return UpperCase(cm::string_view{ s });
  49. }
  50. using cmsys::SystemTools::UpperCase;
  51. /**
  52. * Look for and replace registry values in a string
  53. */
  54. static void ExpandRegistryValues(std::string& source,
  55. KeyWOW64 view = KeyWOW64_Default);
  56. /** Map help document name to file name. */
  57. static std::string HelpFileName(cm::string_view);
  58. using MessageCallback =
  59. std::function<void(std::string const&, cmMessageMetadata const&)>;
  60. /**
  61. * Set the function used by GUIs to display error messages
  62. * Function gets passed: message as a const char*,
  63. * title as a const char*.
  64. */
  65. static void SetMessageCallback(MessageCallback f);
  66. /**
  67. * Display an error message.
  68. */
  69. static void Error(std::string const& m);
  70. /**
  71. * Display a message.
  72. */
  73. static void Message(std::string const& m, char const* title = nullptr);
  74. static void Message(std::string const& m, cmMessageMetadata const& md);
  75. using OutputCallback = std::function<void(std::string const&)>;
  76. //! Send a string to stdout
  77. static void Stdout(std::string const& s);
  78. static void SetStdoutCallback(OutputCallback f);
  79. //! Send a string to stderr
  80. static void Stderr(std::string const& s);
  81. static void SetStderrCallback(OutputCallback f);
  82. using InterruptCallback = std::function<bool()>;
  83. static void SetInterruptCallback(InterruptCallback f);
  84. static bool GetInterruptFlag();
  85. //! Return true if there was an error at any point.
  86. static bool GetErrorOccurredFlag()
  87. {
  88. return cmSystemTools::s_ErrorOccurred ||
  89. cmSystemTools::s_FatalErrorOccurred || GetInterruptFlag();
  90. }
  91. //! If this is set to true, cmake stops processing commands.
  92. static void SetFatalErrorOccurred()
  93. {
  94. cmSystemTools::s_FatalErrorOccurred = true;
  95. }
  96. static void SetErrorOccurred() { cmSystemTools::s_ErrorOccurred = true; }
  97. //! Return true if there was an error at any point.
  98. static bool GetFatalErrorOccurred()
  99. {
  100. return cmSystemTools::s_FatalErrorOccurred || GetInterruptFlag();
  101. }
  102. //! Set the error occurred flag and fatal error back to false
  103. static void ResetErrorOccurredFlag()
  104. {
  105. cmSystemTools::s_FatalErrorOccurred = false;
  106. cmSystemTools::s_ErrorOccurred = false;
  107. }
  108. //! Return true if the path is a framework
  109. static bool IsPathToFramework(std::string const& path);
  110. //! Return true if the path is a xcframework
  111. static bool IsPathToXcFramework(std::string const& path);
  112. //! Return true if the path is a macOS non-framework shared library (aka
  113. //! .dylib)
  114. static bool IsPathToMacOSSharedLibrary(std::string const& path);
  115. static bool DoesFileExistWithExtensions(
  116. std::string const& name, std::vector<std::string> const& sourceExts);
  117. /**
  118. * Check if the given file exists in one of the parent directory of the
  119. * given file or directory and if it does, return the name of the file.
  120. * Toplevel specifies the top-most directory to where it will look.
  121. */
  122. static std::string FileExistsInParentDirectories(
  123. std::string const& fname, std::string const& directory,
  124. std::string const& toplevel);
  125. static void Glob(std::string const& directory, std::string const& regexp,
  126. std::vector<std::string>& files);
  127. static void GlobDirs(std::string const& fullPath,
  128. std::vector<std::string>& files);
  129. /**
  130. * Try to find a list of files that match the "simple" globbing
  131. * expression. At this point in time the globbing expressions have
  132. * to be in form: /directory/partial_file_name*. The * character has
  133. * to be at the end of the string and it does not support ?
  134. * []... The optional argument type specifies what kind of files you
  135. * want to find. 0 means all files, -1 means directories, 1 means
  136. * files only. This method returns true if search was successful.
  137. */
  138. static bool SimpleGlob(std::string const& glob,
  139. std::vector<std::string>& files, int type = 0);
  140. enum class CopyInputRecent
  141. {
  142. No,
  143. Yes,
  144. };
  145. enum class CopyResult
  146. {
  147. Success,
  148. Failure,
  149. };
  150. #if defined(_MSC_VER)
  151. /** Visual C++ does not define mode_t. */
  152. using mode_t = unsigned short;
  153. #endif
  154. /**
  155. * Make a new temporary directory. The path must end in "XXXXXX", and will
  156. * be modified to reflect the name of the directory created. This function
  157. * is similar to POSIX mkdtemp (and is implemented using the same where that
  158. * function is available).
  159. *
  160. * This function can make a full path even if none of the directories existed
  161. * prior to calling this function.
  162. *
  163. * Note that this function may modify \p path even if it does not succeed.
  164. */
  165. static cmsys::Status MakeTempDirectory(char* path,
  166. mode_t const* mode = nullptr);
  167. static cmsys::Status MakeTempDirectory(std::string& path,
  168. mode_t const* mode = nullptr);
  169. /** Copy a file. */
  170. static CopyResult CopySingleFile(std::string const& oldname,
  171. std::string const& newname, CopyWhen when,
  172. CopyInputRecent inputRecent,
  173. std::string* err = nullptr);
  174. enum class Replace
  175. {
  176. Yes,
  177. No,
  178. };
  179. enum class RenameResult
  180. {
  181. Success,
  182. NoReplace,
  183. Failure,
  184. };
  185. /** Rename a file or directory within a single disk volume (atomic
  186. if possible). */
  187. static bool RenameFile(std::string const& oldname,
  188. std::string const& newname);
  189. static RenameResult RenameFile(std::string const& oldname,
  190. std::string const& newname, Replace replace,
  191. std::string* err = nullptr);
  192. //! Rename a file if contents are different, delete the source otherwise
  193. static cmsys::Status MoveFileIfDifferent(std::string const& source,
  194. std::string const& destination);
  195. /**
  196. * According to the CreateProcessW documentation:
  197. *
  198. * To run a batch file, you must start the command interpreter; set
  199. * lpApplicationName to cmd.exe and set lpCommandLine to the following
  200. * arguments: /c plus the name of the batch file.
  201. *
  202. * Additionally, "cmd /c" does not always parse batch file names correctly
  203. * if they contain spaces, but using "cmd /c call" seems to work.
  204. *
  205. * The function is noop on platforms different from the pure WIN32 one.
  206. */
  207. static void MaybePrependCmdExe(std::vector<std::string>& cmdLine);
  208. /**
  209. * Run a single executable command
  210. *
  211. * Output is controlled with outputflag. If outputflag is OUTPUT_NONE, no
  212. * user-viewable output from the program being run will be generated.
  213. * OUTPUT_MERGE is the legacy behavior where stdout and stderr are merged
  214. * into stdout. OUTPUT_FORWARD copies the output to stdout/stderr as
  215. * it was received. OUTPUT_PASSTHROUGH passes through the original handles.
  216. *
  217. * If timeout is specified, the command will be terminated after
  218. * timeout expires. Timeout is specified in seconds.
  219. *
  220. * Argument retVal should be a pointer to the location where the
  221. * exit code will be stored. If the retVal is not specified and
  222. * the program exits with a code other than 0, then the this
  223. * function will return false.
  224. *
  225. * If the command has spaces in the path the caller MUST call
  226. * cmSystemTools::ConvertToRunCommandPath on the command before passing
  227. * it into this function or it will not work. The command must be correctly
  228. * escaped for this to with spaces.
  229. */
  230. enum OutputOption
  231. {
  232. OUTPUT_NONE = 0,
  233. OUTPUT_MERGE,
  234. OUTPUT_FORWARD,
  235. OUTPUT_PASSTHROUGH
  236. };
  237. static bool RunSingleCommand(std::string const& command,
  238. std::string* captureStdOut = nullptr,
  239. std::string* captureStdErr = nullptr,
  240. int* retVal = nullptr,
  241. char const* dir = nullptr,
  242. OutputOption outputflag = OUTPUT_MERGE,
  243. cmDuration timeout = cmDuration::zero());
  244. /**
  245. * In this version of RunSingleCommand, command[0] should be
  246. * the command to run, and each argument to the command should
  247. * be in command[1]...command[command.size()]
  248. */
  249. static bool RunSingleCommand(std::vector<std::string> const& command,
  250. std::string* captureStdOut = nullptr,
  251. std::string* captureStdErr = nullptr,
  252. int* retVal = nullptr,
  253. char const* dir = nullptr,
  254. OutputOption outputflag = OUTPUT_MERGE,
  255. cmDuration timeout = cmDuration::zero(),
  256. Encoding encoding = cmProcessOutput::Auto);
  257. static std::string PrintSingleCommand(std::vector<std::string> const&);
  258. /**
  259. * Parse arguments out of a single string command
  260. */
  261. static std::vector<std::string> ParseArguments(std::string const& command);
  262. /** Parse arguments out of a windows command line string. */
  263. static void ParseWindowsCommandLine(char const* command,
  264. std::vector<std::string>& args);
  265. /** Parse arguments out of a unix command line string. */
  266. static void ParseUnixCommandLine(char const* command,
  267. std::vector<std::string>& args);
  268. /** Split a command-line string into the parsed command and the unparsed
  269. arguments. Returns false on unfinished quoting or escaping. */
  270. static bool SplitProgramFromArgs(std::string const& command,
  271. std::string& program, std::string& args);
  272. /**
  273. * Handle response file in an argument list and return a new argument list
  274. * **/
  275. static std::vector<std::string> HandleResponseFile(
  276. std::vector<std::string>::const_iterator argBeg,
  277. std::vector<std::string>::const_iterator argEnd);
  278. static std::size_t CalculateCommandLineLengthLimit();
  279. static void DisableRunCommandOutput() { s_DisableRunCommandOutput = true; }
  280. static void EnableRunCommandOutput() { s_DisableRunCommandOutput = false; }
  281. static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
  282. enum CompareOp
  283. {
  284. OP_EQUAL = 1,
  285. OP_LESS = 2,
  286. OP_GREATER = 4,
  287. OP_LESS_EQUAL = OP_LESS | OP_EQUAL,
  288. OP_GREATER_EQUAL = OP_GREATER | OP_EQUAL
  289. };
  290. /**
  291. * Compare versions
  292. */
  293. static bool VersionCompare(CompareOp op, std::string const& lhs,
  294. std::string const& rhs);
  295. static bool VersionCompare(CompareOp op, std::string const& lhs,
  296. char const rhs[]);
  297. static bool VersionCompareEqual(std::string const& lhs,
  298. std::string const& rhs);
  299. static bool VersionCompareGreater(std::string const& lhs,
  300. std::string const& rhs);
  301. static bool VersionCompareGreaterEq(std::string const& lhs,
  302. std::string const& rhs);
  303. /**
  304. * Compare two ASCII strings using natural versioning order.
  305. * Non-numerical characters are compared directly.
  306. * Numerical characters are first globbed such that, e.g.
  307. * `test000 < test01 < test0 < test1 < test10`.
  308. * Return a value less than, equal to, or greater than zero if lhs
  309. * precedes, equals, or succeeds rhs in the defined ordering.
  310. */
  311. static int strverscmp(std::string const& lhs, std::string const& rhs);
  312. /** Windows if this is true, the CreateProcess in RunCommand will
  313. * not show new console windows when running programs.
  314. */
  315. static void SetRunCommandHideConsole(bool v) { s_RunCommandHideConsole = v; }
  316. static bool GetRunCommandHideConsole() { return s_RunCommandHideConsole; }
  317. /** Call cmSystemTools::Error with the message m, plus the
  318. * result of strerror(errno)
  319. */
  320. static void ReportLastSystemError(char const* m);
  321. enum class WaitForLineResult
  322. {
  323. None,
  324. STDOUT,
  325. STDERR,
  326. Timeout,
  327. };
  328. /** a general output handler for libuv */
  329. static WaitForLineResult WaitForLine(uv_loop_t* loop, uv_stream_t* outPipe,
  330. uv_stream_t* errPipe, std::string& line,
  331. cmDuration timeout,
  332. std::vector<char>& out,
  333. std::vector<char>& err);
  334. static void SetForceUnixPaths(bool v) { s_ForceUnixPaths = v; }
  335. static bool GetForceUnixPaths() { return s_ForceUnixPaths; }
  336. // ConvertToOutputPath use s_ForceUnixPaths
  337. static std::string ConvertToOutputPath(std::string const& path);
  338. static void ConvertToOutputSlashes(std::string& path);
  339. // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
  340. // be used when RunCommand is called from cmake, because the
  341. // running cmake needs paths to be in its format
  342. static std::string ConvertToRunCommandPath(std::string const& path);
  343. /**
  344. * For windows computes the long path for the given path,
  345. * For Unix, it is a noop
  346. */
  347. static void ConvertToLongPath(std::string& path);
  348. /** compute the relative path from local to remote. local must
  349. be a directory. remote can be a file or a directory.
  350. Both remote and local must be full paths. Basically, if
  351. you are in directory local and you want to access the file in remote
  352. what is the relative path to do that. For example:
  353. /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
  354. from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
  355. */
  356. static std::string RelativePath(std::string const& local,
  357. std::string const& remote);
  358. /**
  359. * Convert the given remote path to a relative path with respect to
  360. * the given local path. Both paths must use forward slashes and not
  361. * already be escaped or quoted.
  362. */
  363. static std::string ForceToRelativePath(std::string const& local_path,
  364. std::string const& remote_path);
  365. /**
  366. * Express the 'in' path relative to 'top' if it does not start in '../'.
  367. */
  368. static std::string RelativeIfUnder(std::string const& top,
  369. std::string const& in);
  370. static cm::optional<std::string> GetEnvVar(std::string const& var);
  371. static std::vector<std::string> GetEnvPathNormalized(std::string const& var);
  372. static std::vector<std::string> SplitEnvPath(cm::string_view in);
  373. static std::vector<std::string> SplitEnvPathNormalized(cm::string_view in);
  374. /** Convert an input path to an absolute path with no '/..' components.
  375. Backslashes in the input path are converted to forward slashes.
  376. Relative paths are interpreted w.r.t. GetLogicalWorkingDirectory.
  377. This is similar to 'realpath', but preserves symlinks that are
  378. not erased by '../' components.
  379. On Windows and macOS, the on-disk capitalization is loaded for
  380. existing paths. */
  381. static std::string ToNormalizedPathOnDisk(std::string p);
  382. #ifndef CMAKE_BOOTSTRAP
  383. /** Remove an environment variable */
  384. static bool UnsetEnv(char const* value);
  385. /** Get the list of all environment variables */
  386. static std::vector<std::string> GetEnvironmentVariables();
  387. /** Append multiple variables to the current environment. */
  388. static void AppendEnv(std::vector<std::string> const& env);
  389. /**
  390. * Helper class to represent an environment diff directly. This is to avoid
  391. * repeated in-place environment modification (i.e. via setenv/putenv), which
  392. * could be slow.
  393. */
  394. class EnvDiff
  395. {
  396. public:
  397. /** Append multiple variables to the current environment diff */
  398. void AppendEnv(std::vector<std::string> const& env);
  399. /**
  400. * Add a single variable (or remove if no = sign) to the current
  401. * environment diff.
  402. */
  403. void PutEnv(std::string const& env);
  404. /** Remove a single variable from the current environment diff. */
  405. void UnPutEnv(std::string const& env);
  406. /**
  407. * Apply an ENVIRONMENT_MODIFICATION operation to this diff. Returns
  408. * false and issues an error on parse failure.
  409. */
  410. bool ParseOperation(std::string const& envmod);
  411. /**
  412. * Apply this diff to the actual environment, optionally writing out the
  413. * modifications to a CTest-compatible measurement stream.
  414. */
  415. void ApplyToCurrentEnv(std::ostringstream* measurement = nullptr);
  416. private:
  417. std::map<std::string, cm::optional<std::string>> diff;
  418. };
  419. /** Helper class to save and restore the environment.
  420. Instantiate this class as an automatic variable on
  421. the stack. Its constructor saves a copy of the current
  422. environment and then its destructor restores the
  423. original environment. */
  424. class SaveRestoreEnvironment
  425. {
  426. public:
  427. SaveRestoreEnvironment();
  428. ~SaveRestoreEnvironment();
  429. SaveRestoreEnvironment(SaveRestoreEnvironment const&) = delete;
  430. SaveRestoreEnvironment& operator=(SaveRestoreEnvironment const&) = delete;
  431. private:
  432. std::vector<std::string> Env;
  433. };
  434. #endif
  435. /** \class ScopedEnv
  436. * \brief An RAII class to temporarily set/unset an environment variable.
  437. *
  438. * The value passed to the constructor is put into the environment. This
  439. * variable is of the form "var=value" and the original value of the "var"
  440. * environment variable is saved. When the object is destroyed, the original
  441. * value for the environment variable is restored. If the variable didn't
  442. * exist, it will be unset.
  443. */
  444. class ScopedEnv
  445. {
  446. public:
  447. ScopedEnv(cm::string_view val);
  448. ~ScopedEnv();
  449. ScopedEnv(ScopedEnv const&) = delete;
  450. ScopedEnv& operator=(ScopedEnv const&) = delete;
  451. private:
  452. std::string Key;
  453. cm::optional<std::string> Original;
  454. };
  455. /** Setup the environment to enable VS 8 IDE output. */
  456. static void EnableVSConsoleOutput();
  457. enum cmTarAction
  458. {
  459. TarActionCreate,
  460. TarActionList,
  461. TarActionExtract,
  462. TarActionNone
  463. };
  464. /** Create tar */
  465. enum cmTarCompression
  466. {
  467. TarCompressGZip,
  468. TarCompressBZip2,
  469. TarCompressLZMA,
  470. TarCompressXZ,
  471. TarCompressZstd,
  472. TarCompressNone
  473. };
  474. enum class cmTarExtractTimestamps
  475. {
  476. Yes,
  477. No
  478. };
  479. static bool ListTar(std::string const& outFileName,
  480. std::vector<std::string> const& files, bool verbose);
  481. static bool CreateTar(std::string const& outFileName,
  482. std::vector<std::string> const& files,
  483. std::string const& workingDirectory,
  484. cmTarCompression compressType, bool verbose,
  485. std::string const& mtime = std::string(),
  486. std::string const& format = std::string(),
  487. int compressionLevel = 0);
  488. static bool ExtractTar(std::string const& inFileName,
  489. std::vector<std::string> const& files,
  490. cmTarExtractTimestamps extractTimestamps,
  491. bool verbose);
  492. /** Random number generation. */
  493. static unsigned int RandomSeed();
  494. static unsigned int RandomNumber();
  495. /**
  496. * Find an executable in the system PATH, with optional extra paths.
  497. * This wraps KWSys's FindProgram to add ToNormalizedPathOnDisk.
  498. */
  499. static std::string FindProgram(
  500. std::string const& name,
  501. std::vector<std::string> const& path = std::vector<std::string>());
  502. /** Find the directory containing CMake executables. */
  503. static void FindCMakeResources(char const* argv0);
  504. /** Get the CMake resource paths, after FindCMakeResources. */
  505. static std::string const& GetCTestCommand();
  506. static std::string const& GetCPackCommand();
  507. static std::string const& GetCMakeCommand();
  508. static std::string const& GetCMakeGUICommand();
  509. static std::string const& GetCMakeCursesCommand();
  510. static std::string const& GetCMClDepsCommand();
  511. static std::string const& GetCMakeRoot();
  512. static std::string const& GetHTMLDoc();
  513. /** Get the CMake config directory **/
  514. static cm::optional<std::string> GetSystemConfigDirectory();
  515. static cm::optional<std::string> GetCMakeConfigDirectory();
  516. static std::string const& GetLogicalWorkingDirectory();
  517. /** The logical working directory may contain symlinks but must not
  518. contain any '../' path components. */
  519. static cmsys::Status SetLogicalWorkingDirectory(std::string const& lwd);
  520. /** Try to guess the soname of a shared library. */
  521. static bool GuessLibrarySOName(std::string const& fullPath,
  522. std::string& soname);
  523. /** Try to guess the install name of a shared library. */
  524. static bool GuessLibraryInstallName(std::string const& fullPath,
  525. std::string& soname);
  526. /** Try to change the RPATH in an ELF binary. */
  527. static bool ChangeRPath(std::string const& file, std::string const& oldRPath,
  528. std::string const& newRPath,
  529. bool removeEnvironmentRPath,
  530. std::string* emsg = nullptr,
  531. bool* changed = nullptr);
  532. /** Try to set the RPATH in an ELF binary. */
  533. static bool SetRPath(std::string const& file, std::string const& newRPath,
  534. std::string* emsg = nullptr, bool* changed = nullptr);
  535. /** Try to remove the RPATH from an ELF binary. */
  536. static bool RemoveRPath(std::string const& file, std::string* emsg = nullptr,
  537. bool* removed = nullptr);
  538. /** Check whether the RPATH in an ELF binary contains the path
  539. given. */
  540. static bool CheckRPath(std::string const& file, std::string const& newRPath);
  541. /** Remove a directory; repeat a few times in case of locked files. */
  542. static cmsys::Status RepeatedRemoveDirectory(std::string const& dir);
  543. /** Encode a string as a URL. */
  544. static std::string EncodeURL(std::string const& in,
  545. bool escapeSlashes = true);
  546. enum class DirCase
  547. {
  548. Sensitive,
  549. Insensitive,
  550. };
  551. /** Returns nullopt when `dir` is not a valid directory */
  552. static cm::optional<DirCase> GetDirCase(std::string const& dir);
  553. #ifdef _WIN32
  554. struct WindowsFileRetry
  555. {
  556. unsigned int Count;
  557. unsigned int Delay;
  558. };
  559. static WindowsFileRetry GetWindowsFileRetry();
  560. static WindowsFileRetry GetWindowsDirectoryRetry();
  561. struct WindowsVersion
  562. {
  563. unsigned int dwMajorVersion;
  564. unsigned int dwMinorVersion;
  565. unsigned int dwBuildNumber;
  566. };
  567. static WindowsVersion GetWindowsVersion();
  568. /** Attempt to get full path to COMSPEC, default "cmd.exe" */
  569. static std::string GetComspec();
  570. #endif
  571. /** Get the real path for a given path, removing all symlinks.
  572. This variant of GetRealPath also works on Windows but will
  573. resolve subst drives too. */
  574. static std::string GetRealPathResolvingWindowsSubst(
  575. std::string const& path, std::string* errorMessage = nullptr);
  576. /** Get the real path for a given path, removing all symlinks. */
  577. static std::string GetRealPath(std::string const& path,
  578. std::string* errorMessage = nullptr);
  579. /** Perform one-time initialization of libuv. */
  580. static void InitializeLibUV();
  581. /** Create a symbolic link if the platform supports it. Returns whether
  582. creation succeeded. */
  583. static cmsys::Status CreateSymlink(std::string const& origName,
  584. std::string const& newName);
  585. static cmsys::Status CreateSymlinkQuietly(std::string const& origName,
  586. std::string const& newName);
  587. /** Create a hard link if the platform supports it. Returns whether
  588. creation succeeded. */
  589. static cmsys::Status CreateLink(std::string const& origName,
  590. std::string const& newName);
  591. static cmsys::Status CreateLinkQuietly(std::string const& origName,
  592. std::string const& newName);
  593. /** Get the system name. */
  594. static cm::string_view GetSystemName();
  595. /** Get the system path separator character */
  596. static char GetSystemPathlistSeparator();
  597. private:
  598. static bool s_ForceUnixPaths;
  599. static bool s_RunCommandHideConsole;
  600. static bool s_ErrorOccurred;
  601. static bool s_FatalErrorOccurred;
  602. static bool s_DisableRunCommandOutput;
  603. };