cmSystemTools.h 14 KB

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