cmSystemTools.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 cmSystemTools_h
  4. #define cmSystemTools_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmCryptoHash.h"
  7. #include "cmDuration.h"
  8. #include "cmProcessOutput.h"
  9. #include "cmsys/Process.h"
  10. #include "cmsys/SystemTools.hxx" // IWYU pragma: export
  11. #include <stddef.h>
  12. #include <string>
  13. #include <vector>
  14. class cmSystemToolsFileTime;
  15. /** \class cmSystemTools
  16. * \brief A collection of useful functions for CMake.
  17. *
  18. * cmSystemTools is a class that provides helper functions
  19. * for the CMake build system.
  20. */
  21. class cmSystemTools : public cmsys::SystemTools
  22. {
  23. public:
  24. typedef cmsys::SystemTools Superclass;
  25. typedef cmProcessOutput::Encoding Encoding;
  26. /** Expand out any arguments in the vector that have ; separated
  27. * strings into multiple arguments. A new vector is created
  28. * containing the expanded versions of all arguments in argsIn.
  29. */
  30. static void ExpandList(std::vector<std::string> const& argsIn,
  31. std::vector<std::string>& argsOut);
  32. static void ExpandListArgument(const std::string& arg,
  33. std::vector<std::string>& argsOut,
  34. bool emptyArgs = false);
  35. /**
  36. * Look for and replace registry values in a string
  37. */
  38. static void ExpandRegistryValues(std::string& source,
  39. KeyWOW64 view = KeyWOW64_Default);
  40. ///! Escape quotes in a string.
  41. static std::string EscapeQuotes(const std::string& str);
  42. /** Map help document name to file name. */
  43. static std::string HelpFileName(std::string);
  44. /**
  45. * Returns a string that has whitespace removed from the start and the end.
  46. */
  47. static std::string TrimWhitespace(const std::string& s);
  48. typedef void (*MessageCallback)(const char*, const char*, bool&, void*);
  49. /**
  50. * Set the function used by GUIs to display error messages
  51. * Function gets passed: message as a const char*,
  52. * title as a const char*, and a reference to bool that when
  53. * set to false, will disable further messages (cancel).
  54. */
  55. static void SetMessageCallback(MessageCallback f,
  56. void* clientData = nullptr);
  57. /**
  58. * Display an error message.
  59. */
  60. static void Error(const char* m, const char* m2 = nullptr,
  61. const char* m3 = nullptr, const char* m4 = nullptr);
  62. /**
  63. * Display a message.
  64. */
  65. static void Message(const char* m, const char* title = nullptr);
  66. typedef void (*OutputCallback)(const char*, size_t length, void*);
  67. ///! Send a string to stdout
  68. static void Stdout(const char* s);
  69. static void Stdout(const char* s, size_t length);
  70. static void SetStdoutCallback(OutputCallback, void* clientData = nullptr);
  71. ///! Send a string to stderr
  72. static void Stderr(const char* s);
  73. static void Stderr(const char* s, size_t length);
  74. static void SetStderrCallback(OutputCallback, void* clientData = nullptr);
  75. typedef bool (*InterruptCallback)(void*);
  76. static void SetInterruptCallback(InterruptCallback f,
  77. void* clientData = nullptr);
  78. static bool GetInterruptFlag();
  79. ///! Return true if there was an error at any point.
  80. static bool GetErrorOccuredFlag()
  81. {
  82. return cmSystemTools::s_ErrorOccured ||
  83. cmSystemTools::s_FatalErrorOccured || GetInterruptFlag();
  84. }
  85. ///! If this is set to true, cmake stops processing commands.
  86. static void SetFatalErrorOccured()
  87. {
  88. cmSystemTools::s_FatalErrorOccured = true;
  89. }
  90. static void SetErrorOccured() { cmSystemTools::s_ErrorOccured = true; }
  91. ///! Return true if there was an error at any point.
  92. static bool GetFatalErrorOccured()
  93. {
  94. return cmSystemTools::s_FatalErrorOccured || GetInterruptFlag();
  95. }
  96. ///! Set the error occurred flag and fatal error back to false
  97. static void ResetErrorOccuredFlag()
  98. {
  99. cmSystemTools::s_FatalErrorOccured = false;
  100. cmSystemTools::s_ErrorOccured = false;
  101. }
  102. /**
  103. * Does a string indicates that CMake/CPack/CTest internally
  104. * forced this value. This is not the same as On, but this
  105. * may be considered as "internally switched on".
  106. */
  107. static bool IsInternallyOn(const char* val);
  108. /**
  109. * does a string indicate a true or on value ? This is not the same
  110. * as ifdef.
  111. */
  112. static bool IsOn(const char* val);
  113. /**
  114. * does a string indicate a false or off value ? Note that this is
  115. * not the same as !IsOn(...) because there are a number of
  116. * ambiguous values such as "/usr/local/bin" a path will result in
  117. * IsON and IsOff both returning false. Note that the special path
  118. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  119. */
  120. static bool IsOff(const char* val);
  121. ///! Return true if value is NOTFOUND or ends in -NOTFOUND.
  122. static bool IsNOTFOUND(const char* value);
  123. ///! Return true if the path is a framework
  124. static bool IsPathToFramework(const char* value);
  125. static bool DoesFileExistWithExtensions(
  126. const char* name, const std::vector<std::string>& sourceExts);
  127. /**
  128. * Check if the given file exists in one of the parent directory of the
  129. * given file or directory and if it does, return the name of the file.
  130. * Toplevel specifies the top-most directory to where it will look.
  131. */
  132. static std::string FileExistsInParentDirectories(const char* fname,
  133. const char* directory,
  134. const char* toplevel);
  135. static void Glob(const std::string& directory, const std::string& regexp,
  136. std::vector<std::string>& files);
  137. static void GlobDirs(const std::string& fullPath,
  138. std::vector<std::string>& files);
  139. /**
  140. * Try to find a list of files that match the "simple" globbing
  141. * expression. At this point in time the globbing expressions have
  142. * to be in form: /directory/partial_file_name*. The * character has
  143. * to be at the end of the string and it does not support ?
  144. * []... The optional argument type specifies what kind of files you
  145. * want to find. 0 means all files, -1 means directories, 1 means
  146. * files only. This method returns true if search was successful.
  147. */
  148. static bool SimpleGlob(const std::string& glob,
  149. std::vector<std::string>& files, int type = 0);
  150. ///! Copy a file.
  151. static bool cmCopyFile(const char* source, const char* destination);
  152. static bool CopyFileIfDifferent(const char* source, const char* destination);
  153. /** Rename a file or directory within a single disk volume (atomic
  154. if possible). */
  155. static bool RenameFile(const char* oldname, const char* newname);
  156. ///! Compute the hash of a file
  157. static std::string ComputeFileHash(const std::string& source,
  158. cmCryptoHash::Algo algo);
  159. /** Compute the md5sum of a string. */
  160. static std::string ComputeStringMD5(const std::string& input);
  161. ///! Get the SHA thumbprint for a certificate file
  162. static std::string ComputeCertificateThumbprint(const std::string& source);
  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 behaviour 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 char* 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 char* 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 EnableMessages() { s_DisableMessages = false; }
  235. static void DisableMessages() { s_DisableMessages = true; }
  236. static void DisableRunCommandOutput() { s_DisableRunCommandOutput = true; }
  237. static void EnableRunCommandOutput() { s_DisableRunCommandOutput = false; }
  238. static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
  239. /**
  240. * Some constants for different file formats.
  241. */
  242. enum FileFormat
  243. {
  244. NO_FILE_FORMAT = 0,
  245. C_FILE_FORMAT,
  246. CXX_FILE_FORMAT,
  247. FORTRAN_FILE_FORMAT,
  248. JAVA_FILE_FORMAT,
  249. CUDA_FILE_FORMAT,
  250. HEADER_FILE_FORMAT,
  251. RESOURCE_FILE_FORMAT,
  252. DEFINITION_FILE_FORMAT,
  253. STATIC_LIBRARY_FILE_FORMAT,
  254. SHARED_LIBRARY_FILE_FORMAT,
  255. MODULE_FILE_FORMAT,
  256. OBJECT_FILE_FORMAT,
  257. UNKNOWN_FILE_FORMAT
  258. };
  259. enum CompareOp
  260. {
  261. OP_EQUAL = 1,
  262. OP_LESS = 2,
  263. OP_GREATER = 4,
  264. OP_LESS_EQUAL = OP_LESS | OP_EQUAL,
  265. OP_GREATER_EQUAL = OP_GREATER | OP_EQUAL
  266. };
  267. /**
  268. * Compare versions
  269. */
  270. static bool VersionCompare(CompareOp op, const char* lhs, const char* rhs);
  271. static bool VersionCompareEqual(std::string const& lhs,
  272. std::string const& rhs);
  273. static bool VersionCompareGreater(std::string const& lhs,
  274. std::string const& rhs);
  275. static bool VersionCompareGreaterEq(std::string const& lhs,
  276. std::string const& rhs);
  277. /**
  278. * Compare two ASCII strings using natural versioning order.
  279. * Non-numerical characters are compared directly.
  280. * Numerical characters are first globbed such that, e.g.
  281. * `test000 < test01 < test0 < test1 < test10`.
  282. * Return a value less than, equal to, or greater than zero if lhs
  283. * precedes, equals, or succeeds rhs in the defined ordering.
  284. */
  285. static int strverscmp(std::string const& lhs, std::string const& rhs);
  286. /**
  287. * Determine the file type based on the extension
  288. */
  289. static FileFormat GetFileFormat(const char* ext);
  290. /** Windows if this is true, the CreateProcess in RunCommand will
  291. * not show new consol windows when running programs.
  292. */
  293. static void SetRunCommandHideConsole(bool v) { s_RunCommandHideConsole = v; }
  294. static bool GetRunCommandHideConsole() { return s_RunCommandHideConsole; }
  295. /** Call cmSystemTools::Error with the message m, plus the
  296. * result of strerror(errno)
  297. */
  298. static void ReportLastSystemError(const char* m);
  299. /** a general output handler for cmsysProcess */
  300. static int WaitForLine(cmsysProcess* process, std::string& line,
  301. cmDuration timeout, std::vector<char>& out,
  302. std::vector<char>& err);
  303. /** Split a string on its newlines into multiple lines. Returns
  304. false only if the last line stored had no newline. */
  305. static bool Split(const char* s, std::vector<std::string>& l);
  306. static void SetForceUnixPaths(bool v) { s_ForceUnixPaths = v; }
  307. static bool GetForceUnixPaths() { return s_ForceUnixPaths; }
  308. // ConvertToOutputPath use s_ForceUnixPaths
  309. static std::string ConvertToOutputPath(std::string const& path);
  310. static void ConvertToOutputSlashes(std::string& path);
  311. // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
  312. // be used when RunCommand is called from cmake, because the
  313. // running cmake needs paths to be in its format
  314. static std::string ConvertToRunCommandPath(const char* path);
  315. /** compute the relative path from local to remote. local must
  316. be a directory. remote can be a file or a directory.
  317. Both remote and local must be full paths. Basically, if
  318. you are in directory local and you want to access the file in remote
  319. what is the relative path to do that. For example:
  320. /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
  321. from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
  322. */
  323. static std::string RelativePath(std::string const& local,
  324. std::string const& remote);
  325. /** Joins two paths while collapsing x/../ parts
  326. * For example CollapseCombinedPath("a/b/c", "../../d") results in "a/d"
  327. */
  328. static std::string CollapseCombinedPath(std::string const& dir,
  329. std::string const& file);
  330. #ifdef CMAKE_BUILD_WITH_CMAKE
  331. /** Remove an environment variable */
  332. static bool UnsetEnv(const char* value);
  333. /** Get the list of all environment variables */
  334. static std::vector<std::string> GetEnvironmentVariables();
  335. /** Append multiple variables to the current environment. */
  336. static void AppendEnv(std::vector<std::string> const& env);
  337. /** Helper class to save and restore the environment.
  338. Instantiate this class as an automatic variable on
  339. the stack. Its constructor saves a copy of the current
  340. environment and then its destructor restores the
  341. original environment. */
  342. class SaveRestoreEnvironment
  343. {
  344. CM_DISABLE_COPY(SaveRestoreEnvironment)
  345. public:
  346. SaveRestoreEnvironment();
  347. ~SaveRestoreEnvironment();
  348. private:
  349. std::vector<std::string> Env;
  350. };
  351. #endif
  352. /** Setup the environment to enable VS 8 IDE output. */
  353. static void EnableVSConsoleOutput();
  354. /** Create tar */
  355. enum cmTarCompression
  356. {
  357. TarCompressGZip,
  358. TarCompressBZip2,
  359. TarCompressXZ,
  360. TarCompressNone
  361. };
  362. static bool ListTar(const char* outFileName, bool verbose);
  363. static bool CreateTar(const char* outFileName,
  364. const std::vector<std::string>& files,
  365. cmTarCompression compressType, bool verbose,
  366. std::string const& mtime = std::string(),
  367. std::string const& format = std::string());
  368. static bool ExtractTar(const char* inFileName, bool verbose);
  369. // This should be called first thing in main
  370. // it will keep child processes from inheriting the
  371. // stdin and stdout of this process. This is important
  372. // if you want to be able to kill child processes and
  373. // not get stuck waiting for all the output on the pipes.
  374. static void DoNotInheritStdPipes();
  375. /** Copy the file create/access/modify times from the file named by
  376. the first argument to that named by the second. */
  377. static bool CopyFileTime(const char* fromFile, const char* toFile);
  378. /** Save and restore file times. */
  379. static cmSystemToolsFileTime* FileTimeNew();
  380. static void FileTimeDelete(cmSystemToolsFileTime*);
  381. static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t);
  382. static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t);
  383. /** Random seed generation. */
  384. static unsigned int RandomSeed();
  385. /** Find the directory containing CMake executables. */
  386. static void FindCMakeResources(const char* argv0);
  387. /** Get the CMake resource paths, after FindCMakeResources. */
  388. static std::string const& GetCTestCommand();
  389. static std::string const& GetCPackCommand();
  390. static std::string const& GetCMakeCommand();
  391. static std::string const& GetCMakeGUICommand();
  392. static std::string const& GetCMakeCursesCommand();
  393. static std::string const& GetCMClDepsCommand();
  394. static std::string const& GetCMakeRoot();
  395. /** Echo a message in color using KWSys's Terminal cprintf. */
  396. static void MakefileColorEcho(int color, const char* message, bool newLine,
  397. bool enabled);
  398. /** Try to guess the soname of a shared library. */
  399. static bool GuessLibrarySOName(std::string const& fullPath,
  400. std::string& soname);
  401. /** Try to guess the install name of a shared library. */
  402. static bool GuessLibraryInstallName(std::string const& fullPath,
  403. std::string& soname);
  404. /** Try to set the RPATH in an ELF binary. */
  405. static bool ChangeRPath(std::string const& file, std::string const& oldRPath,
  406. std::string const& newRPath,
  407. std::string* emsg = nullptr,
  408. bool* changed = nullptr);
  409. /** Try to remove the RPATH from an ELF binary. */
  410. static bool RemoveRPath(std::string const& file, std::string* emsg = nullptr,
  411. bool* removed = nullptr);
  412. /** Check whether the RPATH in an ELF binary contains the path
  413. given. */
  414. static bool CheckRPath(std::string const& file, std::string const& newRPath);
  415. /** Remove a directory; repeat a few times in case of locked files. */
  416. static bool RepeatedRemoveDirectory(const char* dir);
  417. /** Tokenize a string */
  418. static std::vector<std::string> tokenize(const std::string& str,
  419. const std::string& sep);
  420. /** Convert string to long. Expected that the whole string is an integer */
  421. static bool StringToLong(const char* str, long* value);
  422. static bool StringToULong(const char* str, unsigned long* value);
  423. #ifdef _WIN32
  424. struct WindowsFileRetry
  425. {
  426. unsigned int Count;
  427. unsigned int Delay;
  428. };
  429. static WindowsFileRetry GetWindowsFileRetry();
  430. /** Get the real path for a given path, removing all symlinks. */
  431. static std::string GetRealPath(const std::string& path,
  432. std::string* errorMessage = 0);
  433. #endif
  434. /** Perform one-time initialization of libuv. */
  435. static void InitializeLibUV();
  436. private:
  437. static bool s_ForceUnixPaths;
  438. static bool s_RunCommandHideConsole;
  439. static bool s_ErrorOccured;
  440. static bool s_FatalErrorOccured;
  441. static bool s_DisableMessages;
  442. static bool s_DisableRunCommandOutput;
  443. static MessageCallback s_MessageCallback;
  444. static OutputCallback s_StdoutCallback;
  445. static OutputCallback s_StderrCallback;
  446. static InterruptCallback s_InterruptCallback;
  447. static void* s_MessageCallbackClientData;
  448. static void* s_StdoutCallbackClientData;
  449. static void* s_StderrCallbackClientData;
  450. static void* s_InterruptCallbackClientData;
  451. };
  452. #endif