cmSystemTools.h 11 KB

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