cmSystemTools.h 10 KB

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