cmSystemTools.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmSystemTools_h
  14. #define cmSystemTools_h
  15. #include "cmStandardIncludes.h"
  16. #include <cmsys/SystemTools.hxx>
  17. #include <cmsys/Process.h>
  18. class cmSystemToolsFileTime;
  19. /** \class cmSystemTools
  20. * \brief A collection of useful functions for CMake.
  21. *
  22. * cmSystemTools is a class that provides helper functions
  23. * for the CMake build system.
  24. */
  25. class cmSystemTools: public cmsys::SystemTools
  26. {
  27. public:
  28. typedef cmsys::SystemTools Superclass;
  29. /** Expand out any arguements in the vector that have ; separated
  30. * strings into multiple arguements. A new vector is created
  31. * containing the expanded versions of all arguments in argsIn.
  32. */
  33. static void ExpandList(std::vector<std::string> const& argsIn,
  34. std::vector<std::string>& argsOut);
  35. static void ExpandListArgument(const std::string& arg,
  36. std::vector<std::string>& argsOut,
  37. bool emptyArgs=false);
  38. /**
  39. * Look for and replace registry values in a string
  40. */
  41. static void ExpandRegistryValues(std::string& source,
  42. KeyWOW64 view = KeyWOW64_Default);
  43. /**
  44. * Platform independent escape spaces, unix uses backslash,
  45. * windows double quotes the string.
  46. */
  47. static std::string EscapeSpaces(const char* str);
  48. ///! Escape quotes in a string.
  49. static std::string EscapeQuotes(const char* str);
  50. /**
  51. * Given a string, replace any escape sequences with the corresponding
  52. * characters.
  53. */
  54. static std::string RemoveEscapes(const char*);
  55. typedef void (*ErrorCallback)(const char*, const char*, bool&, void*);
  56. /**
  57. * Set the function used by GUI's to display error messages
  58. * Function gets passed: message as a const char*,
  59. * title as a const char*, and a reference to bool that when
  60. * set to false, will disable furthur messages (cancel).
  61. */
  62. static void SetErrorCallback(ErrorCallback f, void* clientData=0);
  63. /**
  64. * Display an error message.
  65. */
  66. static void Error(const char* m, const char* m2=0,
  67. const char* m3=0, const char* m4=0);
  68. /**
  69. * Display a message.
  70. */
  71. static void Message(const char* m, const char* title=0);
  72. ///! Send a string to stdout
  73. static void Stdout(const char* s);
  74. static void Stdout(const char* s, int length);
  75. typedef void (*StdoutCallback)(const char*, int length, void*);
  76. static void SetStdoutCallback(StdoutCallback, void* clientData=0);
  77. ///! Return true if there was an error at any point.
  78. static bool GetErrorOccuredFlag()
  79. {
  80. return cmSystemTools::s_ErrorOccured ||
  81. cmSystemTools::s_FatalErrorOccured;
  82. }
  83. ///! If this is set to true, cmake stops processing commands.
  84. static void SetFatalErrorOccured()
  85. {
  86. cmSystemTools::s_FatalErrorOccured = true;
  87. }
  88. static void SetErrorOccured()
  89. {
  90. cmSystemTools::s_ErrorOccured = true;
  91. }
  92. ///! Return true if there was an error at any point.
  93. static bool GetFatalErrorOccured()
  94. {
  95. return cmSystemTools::s_FatalErrorOccured;
  96. }
  97. ///! Set the error occured flag and fatal error back to false
  98. static void ResetErrorOccuredFlag()
  99. {
  100. cmSystemTools::s_FatalErrorOccured = false;
  101. cmSystemTools::s_ErrorOccured = false;
  102. }
  103. /**
  104. * does a string indicate a true or on value ? This is not the same
  105. * as ifdef.
  106. */
  107. static bool IsOn(const char* val);
  108. /**
  109. * does a string indicate a false or off value ? Note that this is
  110. * not the same as !IsOn(...) because there are a number of
  111. * ambiguous values such as "/usr/local/bin" a path will result in
  112. * IsON and IsOff both returning false. Note that the special path
  113. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  114. */
  115. static bool IsOff(const char* val);
  116. ///! Return true if value is NOTFOUND or ends in -NOTFOUND.
  117. static bool IsNOTFOUND(const char* value);
  118. ///! Return true if the path is a framework
  119. static bool IsPathToFramework(const char* value);
  120. static bool DoesFileExistWithExtensions(
  121. const char *name,
  122. const std::vector<std::string>& sourceExts);
  123. static void Glob(const char *directory, const char *regexp,
  124. std::vector<std::string>& files);
  125. static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
  126. /**
  127. * Try to find a list of files that match the "simple" globbing
  128. * expression. At this point in time the globbing expressions have
  129. * to be in form: /directory/partial_file_name*. The * character has
  130. * to be at the end of the string and it does not support ?
  131. * []... The optional argument type specifies what kind of files you
  132. * want to find. 0 means all files, -1 means directories, 1 means
  133. * files only. This method returns true if search was succesfull.
  134. */
  135. static bool SimpleGlob(const cmStdString& glob,
  136. std::vector<cmStdString>& files,
  137. int type = 0);
  138. ///! Copy a file.
  139. static bool cmCopyFile(const char* source, const char* destination);
  140. static bool CopyFileIfDifferent(const char* source,
  141. const char* destination);
  142. ///! Compute the md5sum of a file
  143. static bool ComputeFileMD5(const char* source, char* md5out);
  144. /** Compute the md5sum of a string. */
  145. static std::string ComputeStringMD5(const char* input);
  146. /**
  147. * Run an executable command and put the stdout in output.
  148. * A temporary file is created in the binaryDir for storing the
  149. * output because windows does not have popen.
  150. *
  151. * If verbose is false, no user-viewable output from the program
  152. * being run will be generated.
  153. *
  154. * If timeout is specified, the command will be terminated after
  155. * timeout expires.
  156. */
  157. static bool RunCommand(const char* command, std::string& output,
  158. const char* directory = 0,
  159. bool verbose = true, int timeout = 0);
  160. static bool RunCommand(const char* command, std::string& output,
  161. int &retVal, const char* directory = 0,
  162. bool verbose = true, int timeout = 0);
  163. /**
  164. * Run a single executable command and put the stdout and stderr
  165. * in output.
  166. *
  167. * If verbose is false, no user-viewable output from the program
  168. * being run will be generated.
  169. *
  170. * If timeout is specified, the command will be terminated after
  171. * timeout expires. Timeout is specified in seconds.
  172. *
  173. * Argument retVal should be a pointer to the location where the
  174. * exit code will be stored. If the retVal is not specified and
  175. * the program exits with a code other than 0, then the this
  176. * function will return false.
  177. *
  178. * If the command has spaces in the path the caller MUST call
  179. * cmSystemTools::ConvertToRunCommandPath on the command before passing
  180. * it into this function or it will not work. The command must be correctly
  181. * escaped for this to with spaces.
  182. */
  183. static bool RunSingleCommand(const char* command, std::string* output = 0,
  184. int* retVal = 0, const char* dir = 0,
  185. bool verbose = true,
  186. double timeout = 0.0);
  187. /**
  188. * In this version of RunSingleCommand, command[0] should be
  189. * the command to run, and each argument to the command should
  190. * be in comand[1]...command[command.size()]
  191. */
  192. static bool RunSingleCommand(std::vector<cmStdString> const& command,
  193. std::string* output = 0,
  194. int* retVal = 0, const char* dir = 0,
  195. bool verbose = true,
  196. double timeout = 0.0);
  197. /**
  198. * Parse arguments out of a single string command
  199. */
  200. static std::vector<cmStdString> ParseArguments(const char* command);
  201. /** Parse arguments out of a windows command line string. */
  202. static void ParseWindowsCommandLine(const char* command,
  203. std::vector<std::string>& args);
  204. /** Compute an escaped version of the given argument for use in a
  205. windows shell. See kwsys/System.h.in for details. */
  206. static std::string EscapeWindowsShellArgument(const char* arg,
  207. int shell_flags);
  208. static void EnableMessages() { s_DisableMessages = false; }
  209. static void DisableMessages() { s_DisableMessages = true; }
  210. static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
  211. static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
  212. static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
  213. /**
  214. * Come constants for different file formats.
  215. */
  216. enum FileFormat {
  217. NO_FILE_FORMAT = 0,
  218. C_FILE_FORMAT,
  219. CXX_FILE_FORMAT,
  220. FORTRAN_FILE_FORMAT,
  221. JAVA_FILE_FORMAT,
  222. HEADER_FILE_FORMAT,
  223. RESOURCE_FILE_FORMAT,
  224. DEFINITION_FILE_FORMAT,
  225. STATIC_LIBRARY_FILE_FORMAT,
  226. SHARED_LIBRARY_FILE_FORMAT,
  227. MODULE_FILE_FORMAT,
  228. OBJECT_FILE_FORMAT,
  229. UNKNOWN_FILE_FORMAT
  230. };
  231. /**
  232. * Determine the file type based on the extension
  233. */
  234. static FileFormat GetFileFormat(const char* ext);
  235. /**
  236. * On Windows 9x we need a comspec (command.com) substitute to run
  237. * programs correctly. This string has to be constant available
  238. * through the running of program. This method does not create a copy.
  239. */
  240. static void SetWindows9xComspecSubstitute(const char*);
  241. static const char* GetWindows9xComspecSubstitute();
  242. /** Windows if this is true, the CreateProcess in RunCommand will
  243. * not show new consol windows when running programs.
  244. */
  245. static void SetRunCommandHideConsole(bool v){s_RunCommandHideConsole = v;}
  246. static bool GetRunCommandHideConsole(){ return s_RunCommandHideConsole;}
  247. /** Call cmSystemTools::Error with the message m, plus the
  248. * result of strerror(errno)
  249. */
  250. static void ReportLastSystemError(const char* m);
  251. /** a general output handler for cmsysProcess */
  252. static int WaitForLine(cmsysProcess* process, std::string& line,
  253. double timeout,
  254. std::vector<char>& out,
  255. std::vector<char>& err);
  256. /** Split a string on its newlines into multiple lines. Returns
  257. false only if the last line stored had no newline. */
  258. static bool Split(const char* s, std::vector<cmStdString>& l);
  259. static void SetForceUnixPaths(bool v)
  260. {
  261. s_ForceUnixPaths = v;
  262. }
  263. static bool GetForceUnixPaths()
  264. {
  265. return s_ForceUnixPaths;
  266. }
  267. // ConvertToOutputPath use s_ForceUnixPaths
  268. static std::string ConvertToOutputPath(const char* path);
  269. static void ConvertToOutputSlashes(std::string& path);
  270. // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
  271. // be used when RunCommand is called from cmake, because the
  272. // running cmake needs paths to be in its format
  273. static std::string ConvertToRunCommandPath(const char* path);
  274. //! Check if the first string ends with the second one.
  275. static bool StringEndsWith(const char* str1, const char* str2);
  276. /** compute the relative path from local to remote. local must
  277. be a directory. remote can be a file or a directory.
  278. Both remote and local must be full paths. Basically, if
  279. you are in directory local and you want to access the file in remote
  280. what is the relative path to do that. For example:
  281. /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
  282. from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
  283. */
  284. static std::string RelativePath(const char* local, const char* remote);
  285. /** Put a string into the environment
  286. of the form var=value */
  287. static bool PutEnv(const char* value);
  288. #ifdef CMAKE_BUILD_WITH_CMAKE
  289. /** Remove an environment variable */
  290. static bool UnsetEnv(const char* value);
  291. /** Get the list of all environment variables */
  292. static std::vector<std::string> GetEnvironmentVariables();
  293. /** Append multiple variables to the current environment.
  294. Return the original environment, as it was before the
  295. append. */
  296. static std::vector<std::string> AppendEnv(
  297. std::vector<std::string>* env);
  298. /** Restore the full environment to "env" - use after
  299. AppendEnv to put the environment back to the way it
  300. was. */
  301. static void RestoreEnv(const std::vector<std::string>& env);
  302. #endif
  303. /** Setup the environment to enable VS 8 IDE output. */
  304. static void EnableVSConsoleOutput();
  305. /** Create tar */
  306. static bool ListTar(const char* outFileName,
  307. std::vector<cmStdString>& files,
  308. bool gzip, bool verbose);
  309. static bool CreateTar(const char* outFileName,
  310. const std::vector<cmStdString>& files, bool gzip,
  311. bool verbose);
  312. static bool ExtractTar(const char* inFileName,
  313. const std::vector<cmStdString>& files, bool gzip,
  314. bool verbose);
  315. // This should be called first thing in main
  316. // it will keep child processes from inheriting the
  317. // stdin and stdout of this process. This is important
  318. // if you want to be able to kill child processes and
  319. // not get stuck waiting for all the output on the pipes.
  320. static void DoNotInheritStdPipes();
  321. /** Copy the file create/access/modify times from the file named by
  322. the first argument to that named by the second. */
  323. static bool CopyFileTime(const char* fromFile, const char* toFile);
  324. /** Save and restore file times. */
  325. static cmSystemToolsFileTime* FileTimeNew();
  326. static void FileTimeDelete(cmSystemToolsFileTime*);
  327. static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t);
  328. static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t);
  329. /** Find the directory containing the running executable. Save it
  330. in a global location to be queried by GetExecutableDirectory
  331. later. */
  332. static void FindExecutableDirectory(const char* argv0);
  333. /** Get the directory containing the currently running executable. */
  334. static const char* GetExecutableDirectory();
  335. #if defined(CMAKE_BUILD_WITH_CMAKE)
  336. /** Echo a message in color using KWSys's Terminal cprintf. */
  337. static void MakefileColorEcho(int color, const char* message,
  338. bool newLine, bool enabled);
  339. #endif
  340. /** Try to guess the soname of a shared library. */
  341. static bool GuessLibrarySOName(std::string const& fullPath,
  342. std::string& soname);
  343. /** Try to set the RPATH in an ELF binary. */
  344. static bool ChangeRPath(std::string const& file,
  345. std::string const& oldRPath,
  346. std::string const& newRPath,
  347. std::string* emsg = 0,
  348. bool* changed = 0);
  349. /** Try to remove the RPATH from an ELF binary. */
  350. static bool RemoveRPath(std::string const& file, std::string* emsg = 0,
  351. bool* removed = 0);
  352. /** Check whether the RPATH in an ELF binary contains the path
  353. given. */
  354. static bool CheckRPath(std::string const& file,
  355. std::string const& newRPath);
  356. private:
  357. static bool s_ForceUnixPaths;
  358. static bool s_RunCommandHideConsole;
  359. static bool s_ErrorOccured;
  360. static bool s_FatalErrorOccured;
  361. static bool s_DisableMessages;
  362. static bool s_DisableRunCommandOutput;
  363. static ErrorCallback s_ErrorCallback;
  364. static StdoutCallback s_StdoutCallback;
  365. static void* s_ErrorCallbackClientData;
  366. static void* s_StdoutCallbackClientData;
  367. static std::string s_Windows9xComspecSubstitute;
  368. };
  369. #endif