cmSystemTools.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. /** \class cmSystemTools
  17. * \brief A collection of useful functions for CMake.
  18. *
  19. * cmSystemTools is a class that provides helper functions
  20. * for the CMake build system.
  21. */
  22. class cmSystemTools
  23. {
  24. public:
  25. /**
  26. * Replace symbols in str that are not valid in C identifiers as
  27. * defined by the 1999 standard, ie. anything except [A-Za-z0-9_].
  28. * They are replaced with `_' and if the first character is a digit
  29. * then an underscore is prepended. Note that this can produce
  30. * identifiers that the standard reserves (_[A-Z].* and __.*).
  31. */
  32. static std::string MakeCindentifier(const char* s);
  33. /**
  34. * Make a new directory if it is not there. This function
  35. * can make a full path even if none of the directories existed
  36. * prior to calling this function.
  37. */
  38. static bool MakeDirectory(const char* path);
  39. /**
  40. * Get current time as a double. On certain platforms this will
  41. * return higher resolution than seconds:
  42. * (1) gettimeofday() -- resolution in microseconds
  43. * (2) ftime() -- resolution in milliseconds
  44. * (3) time() -- resolution in seconds
  45. */
  46. static double GetTime();
  47. /**
  48. * Replace replace all occurances of the string in
  49. * the source string.
  50. */
  51. static void ReplaceString(std::string& source,
  52. const char* replace,
  53. const char* with);
  54. /** Expand out any arguements in the vector that have ; separated
  55. * strings into multiple arguements. A new vector is created
  56. * containing the expanded versions of all arguments in argsIn.
  57. */
  58. static void ExpandList(std::vector<std::string> const& argsIn,
  59. std::vector<std::string>& argsOut);
  60. static void ExpandListArgument(const std::string& arg,
  61. std::vector<std::string>& argsOut);
  62. /**
  63. * Read a registry value
  64. */
  65. static bool ReadRegistryValue(const char *key, std::string &value);
  66. /**
  67. * Write a registry value
  68. */
  69. static bool WriteRegistryValue(const char *key, const char *value);
  70. /**
  71. * Delete a registry value
  72. */
  73. static bool DeleteRegistryValue(const char *key);
  74. /**
  75. * Look for and replace registry values in a string
  76. */
  77. static void ExpandRegistryValues(std::string& source);
  78. /**
  79. * Return a capitalized string (i.e the first letter is uppercased, all other
  80. * are lowercased).
  81. */
  82. static std::string Capitalized(const std::string&);
  83. /**
  84. * Return a lower case string
  85. */
  86. static std::string LowerCase(const std::string&);
  87. /**
  88. * Return a lower case string
  89. */
  90. static std::string UpperCase(const std::string&);
  91. /**
  92. * Replace Windows file system slashes with Unix-style slashes.
  93. */
  94. static void ConvertToUnixSlashes(std::string& path);
  95. /**
  96. * Platform independent escape spaces, unix uses backslash,
  97. * windows double quotes the string.
  98. */
  99. static std::string EscapeSpaces(const char* str);
  100. ///! Escape quotes in a string.
  101. static std::string EscapeQuotes(const char* str);
  102. /**
  103. * For windows this calles ConvertToWindowsOutputPath and for unix
  104. * it calls ConvertToUnixOutputPath
  105. */
  106. static std::string ConvertToOutputPath(const char*);
  107. ///! Return true if a file exists in the current directory.
  108. static bool FileExists(const char* filename);
  109. /**
  110. * Given a string, replace any escape sequences with the corresponding
  111. * characters.
  112. */
  113. static std::string RemoveEscapes(const char*);
  114. /**
  115. * Add the paths from the environment variable PATH to the
  116. * string vector passed in.
  117. */
  118. static void GetPath(std::vector<std::string>& path);
  119. /**
  120. * Get the file extension (including ".") needed for an executable
  121. * on the current platform ("" for unix, ".exe" for Windows).
  122. */
  123. static const char* GetExecutableExtension();
  124. typedef void (*ErrorCallback)(const char*, const char*, bool&, void*);
  125. /**
  126. * Set the function used by GUI's to display error messages
  127. * Function gets passed: message as a const char*,
  128. * title as a const char*, and a reference to bool that when
  129. * set to false, will disable furthur messages (cancel).
  130. */
  131. static void SetErrorCallback(ErrorCallback f, void* clientData=0);
  132. /**
  133. * Display an error message.
  134. */
  135. static void Error(const char* m, const char* m2=0,
  136. const char* m3=0, const char* m4=0);
  137. /**
  138. * Display a message.
  139. */
  140. static void Message(const char* m, const char* title=0);
  141. ///! Return true if there was an error at any point.
  142. static bool GetErrorOccuredFlag()
  143. {
  144. return cmSystemTools::s_ErrorOccured || cmSystemTools::s_FatalErrorOccured;
  145. }
  146. ///! If this is set to true, cmake stops processing commands.
  147. static void SetFatalErrorOccured()
  148. {
  149. cmSystemTools::s_FatalErrorOccured = true;
  150. }
  151. ///! Return true if there was an error at any point.
  152. static bool GetFatalErrorOccured()
  153. {
  154. return cmSystemTools::s_FatalErrorOccured;
  155. }
  156. ///! Set the error occured flag and fatal error back to false
  157. static void ResetErrorOccuredFlag()
  158. {
  159. cmSystemTools::s_FatalErrorOccured = false;
  160. cmSystemTools::s_ErrorOccured = false;
  161. }
  162. /**
  163. * Copy the source file to the destination file only
  164. * if the two files differ.
  165. */
  166. static bool CopyFileIfDifferent(const char* source,
  167. const char* destination);
  168. ///! Compare the contents of two files. Return true if different.
  169. static bool FilesDiffer(const char* source,
  170. const char* destination);
  171. ///! return true if the two files are the same file
  172. static bool SameFile(const char* file1, const char* file2);
  173. ///! Copy a file.
  174. static void cmCopyFile(const char* source,
  175. const char* destination);
  176. ///! Remove a file.
  177. static bool RemoveFile(const char* source);
  178. /**
  179. * does a string indicate a true or on value ? This is not the same
  180. * as ifdef.
  181. */
  182. static bool IsOn(const char* val);
  183. /**
  184. * does a string indicate a false or off value ? Note that this is
  185. * not the same as !IsOn(...) because there are a number of
  186. * ambiguous values such as "/usr/local/bin" a path will result in
  187. * IsON and IsOff both returning false. Note that the special path
  188. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  189. */
  190. static bool IsOff(const char* val);
  191. ///! Return true if value is NOTFOUND or ends in -NOTFOUND.
  192. static bool IsNOTFOUND(const char* value);
  193. ///! Find a file in the system PATH, with optional extra paths.
  194. static std::string FindFile(const char* name,
  195. const std::vector<std::string>& path= std::vector<std::string>());
  196. ///! Find an executable in the system PATH, with optional extra paths.
  197. static std::string FindProgram(const char* name,
  198. const std::vector<std::string>& path = std::vector<std::string>(),
  199. bool no_system_path = false);
  200. ///! Find a library in the system PATH, with optional extra paths.
  201. static std::string FindLibrary(const char* name,
  202. const std::vector<std::string>& path);
  203. ///! return true if the file is a directory.
  204. static bool FileIsDirectory(const char* name);
  205. static void Glob(const char *directory, const char *regexp,
  206. std::vector<std::string>& files);
  207. static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
  208. /**
  209. * Try to find a list of files that match the "simple" globbing
  210. * expression. At this point in time the globbing expressions have
  211. * to be in form: /directory/partial_file_name*. The * character has
  212. * to be at the end of the string and it does not support ?
  213. * []... The optional argument type specifies what kind of files you
  214. * want to find. 0 means all files, -1 means directories, 1 means
  215. * files only. This method returns true if search was succesfull.
  216. */
  217. static bool SimpleGlob(const std::string& glob, std::vector<std::string>& files,
  218. int type = 0);
  219. static std::string GetCurrentWorkingDirectory();
  220. static std::string GetProgramPath(const char*);
  221. static void SplitProgramPath(const char* in_name,
  222. std::string& dir,
  223. std::string& file,
  224. bool errorReport = true);
  225. static std::string CollapseFullPath(const char* in_relative);
  226. static std::string CollapseFullPath(const char* in_relative,
  227. const char* in_base);
  228. ///! return path of a full filename (no trailing slashes).
  229. static std::string GetFilenamePath(const std::string&);
  230. ///! return file name of a full filename (i.e. file name without path).
  231. static std::string GetFilenameName(const std::string&);
  232. ///! Split a program from its arguments and handle spaces in the paths.
  233. static void SplitProgramFromArgs(const char* path,
  234. std::string& program, std::string& args);
  235. ///! return file extension of a full filename (dot included).
  236. static std::string GetFilenameExtension(const std::string&);
  237. ///! return file extension of a full filename (dot included).
  238. static std::string GetFilenameLastExtension(const std::string&);
  239. ///! return file name without extension of a full filename.
  240. static std::string GetFilenameWithoutExtension(const std::string&);
  241. ///! return file name without its last (shortest) extension.
  242. static std::string GetFilenameWithoutLastExtension(const std::string&);
  243. /** Return whether the path represents a full path (not relative). */
  244. static bool FileIsFullPath(const char*);
  245. static long int ModifiedTime(const char* filename);
  246. /**
  247. * Run an executable command and put the stdout in output.
  248. * A temporary file is created in the binaryDir for storing the
  249. * output because windows does not have popen.
  250. *
  251. * If verbose is false, no user-viewable output from the program
  252. * being run will be generated.
  253. *
  254. * If timeout is specified, the command will be terminated after
  255. * timeout expires.
  256. */
  257. static bool RunCommand(const char* command, std::string& output,
  258. const char* directory = 0,
  259. bool verbose = true, int timeout = 0);
  260. static bool RunCommand(const char* command, std::string& output,
  261. int &retVal, const char* directory = 0,
  262. bool verbose = true, int timeout = 0);
  263. ///! for windows return the short path for the given path, unix just a pass through
  264. static bool GetShortPath(const char* path, std::string& result);
  265. ///! change directory the the directory specified
  266. static int ChangeDirectory(const char* dir);
  267. static void EnableMessages() { s_DisableMessages = false; }
  268. static void DisableMessages() { s_DisableMessages = true; }
  269. static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
  270. static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
  271. static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
  272. /** Split a string on its newlines into multiple lines. Returns
  273. false only if the last line stored had no newline. */
  274. static bool Split(const char* s, std::vector<cmStdString>& l);
  275. /**
  276. * Come constants for different file formats.
  277. */
  278. enum FileFormat {
  279. NO_FILE_FORMAT = 0,
  280. C_FILE_FORMAT,
  281. CXX_FILE_FORMAT,
  282. JAVA_FILE_FORMAT,
  283. HEADER_FILE_FORMAT,
  284. RESOURCE_FILE_FORMAT,
  285. DEFINITION_FILE_FORMAT,
  286. STATIC_LIBRARY_FILE_FORMAT,
  287. SHARED_LIBRARY_FILE_FORMAT,
  288. MODULE_FILE_FORMAT,
  289. OBJECT_FILE_FORMAT,
  290. UNKNOWN_FILE_FORMAT
  291. };
  292. /**
  293. * Determine the file type based on the extension
  294. */
  295. static FileFormat GetFileFormat(const char* ext);
  296. static std::string GetCurrentDateTime(const char* format);
  297. /**
  298. * On Windows 9x we need a comspec (command.com) substitute to run
  299. * programs correctly. This string has to be constant available
  300. * through the running of program. This method does not create a copy.
  301. */
  302. static void SetWindows9xComspecSubstitute(const char*);
  303. static const char* GetWindows9xComspecSubstitute();
  304. /** Windows if this is true, the CreateProcess in RunCommand will
  305. * not show new consol windows when running programs.
  306. */
  307. static void SetRunCommandHideConsole(bool v){s_RunCommandHideConsole = v;}
  308. static bool GetRunCommandHideConsole(){ return s_RunCommandHideConsole;}
  309. /** Call cmSystemTools::Error with the message m, plus the
  310. * result of strerror(errno)
  311. */
  312. static void ReportLastSystemError(const char* m);
  313. /** When building DEBUG with MSVC, this enables a hook that prevents
  314. * error dialogs from popping up if the program is being run from
  315. * DART.
  316. */
  317. static void EnableMSVCDebugHook();
  318. /**
  319. * Read line from file. Make sure to get everything. Due to a buggy stream
  320. * library on the HP and another on Mac OSX, we need this very carefully
  321. * written version of getline. Returns true if any data were read before the
  322. * end-of-file was reached.
  323. */
  324. static bool GetLineFromStream(std::istream& istr, std::string& line);
  325. protected:
  326. // these two functions can be called from ConvertToOutputPath
  327. /**
  328. * Convert the path to a string that can be used in a unix makefile.
  329. * double slashes are removed, and spaces are escaped.
  330. */
  331. static std::string ConvertToUnixOutputPath(const char*);
  332. /**
  333. * Convert the path to string that can be used in a windows project or
  334. * makefile. Double slashes are removed if they are not at the start of
  335. * the string, the slashes are converted to windows style backslashes, and
  336. * if there are spaces in the string it is double quoted.
  337. */
  338. static std::string ConvertToWindowsOutputPath(const char*);
  339. private:
  340. static bool s_RunCommandHideConsole;
  341. static bool s_ErrorOccured;
  342. static bool s_FatalErrorOccured;
  343. static bool s_DisableMessages;
  344. static bool s_DisableRunCommandOutput;
  345. static ErrorCallback s_ErrorCallback;
  346. static void* s_ErrorCallbackClientData;
  347. static std::string s_Windows9xComspecSubstitute;
  348. };
  349. #endif