cmSystemTools.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #ifndef cmSystemTools_h
  33. #define cmSystemTools_h
  34. #include "cmStandardIncludes.h"
  35. /** \class cmSystemTools
  36. * \brief A collection of useful functions for CMake.
  37. *
  38. * cmSystemTools is a class that provides helper functions
  39. * for the CMake build system.
  40. */
  41. class cmSystemTools
  42. {
  43. public:
  44. /**
  45. * Make a new directory if it is not there. This function
  46. * can make a full path even if none of the directories existed
  47. * prior to calling this function.
  48. */
  49. static bool MakeDirectory(const char* path);
  50. /**
  51. * Replace replace all occurances of the string in
  52. * the source string.
  53. */
  54. static void ReplaceString(std::string& source,
  55. const char* replace,
  56. const char* with);
  57. /**
  58. * Return a string equivalent to the input string, but with all " " replaced
  59. * with "\ " to escape the spaces.
  60. */
  61. static std::string EscapeSpaces(const char*);
  62. /**
  63. * Replace Windows file system slashes with Unix-style slashes.
  64. */
  65. static void ConvertToUnixSlashes(std::string& path);
  66. ///! Return true if a file exists in the current directory.
  67. static bool FileExists(const char* filename);
  68. /**
  69. * Return the number of times the given expression occurs in the file
  70. * specified by the concatenation of dir/file.
  71. */
  72. static int Grep(const char* dir, const char* file, const char* expression);
  73. /**
  74. * Convert a path containing a cygwin drive specifier to its natural
  75. * equivalent.
  76. */
  77. static void ConvertCygwinPath(std::string& pathname);
  78. /**
  79. * Read a CMake command (or function) from an input file. This
  80. * returns the name of the function and a list of its
  81. * arguments.
  82. */
  83. static bool ParseFunction(std::ifstream&,
  84. std::string& name,
  85. std::vector<std::string>& arguments);
  86. /**
  87. * Extract white-space separated arguments from a string.
  88. * Double quoted strings are accepted with spaces.
  89. * This is called by ParseFunction.
  90. */
  91. static void GetArguments(std::string& line,
  92. std::vector<std::string>& arguments);
  93. /**
  94. * Add the paths from the environment variable PATH to the
  95. * string vector passed in.
  96. */
  97. static void GetPath(std::vector<std::string>& path);
  98. /**
  99. * Get the file extension (including ".") needed for an executable
  100. * on the current platform ("" for unix, ".exe" for Windows).
  101. */
  102. static const char* GetExecutableExtension();
  103. /**
  104. * Display an error message.
  105. */
  106. static void Error(const char* m, const char* m2=0,
  107. const char* m3=0, const char* m4=0);
  108. ///! Return true if there was an error at any point.
  109. static bool GetErrorOccuredFlag()
  110. {
  111. return cmSystemTools::s_ErrorOccured;
  112. }
  113. /**
  114. * Copy the source file to the destination file only
  115. * if the two files differ.
  116. */
  117. static void CopyFileIfDifferent(const char* source,
  118. const char* destination);
  119. ///! Compare the contents of two files. Return true if different.
  120. static bool FilesDiffer(const char* source,
  121. const char* destination);
  122. ///! Copy a file.
  123. static void cmCopyFile(const char* source,
  124. const char* destination);
  125. ///! Remove a file.
  126. static void RemoveFile(const char* source);
  127. /**
  128. * does a string indicate a true or on value ? This is not the same
  129. * as ifdef.
  130. */
  131. static bool IsOn(const char* val);
  132. /**
  133. * does a string indicate a false or off value ? Note that this is
  134. * not the same as !IsOn(...) because there are a number of
  135. * ambiguous values such as "/usr/local/bin" a path will result in
  136. * IsON and IsOff both returning false. Note that the special path
  137. * NOTFOUND will cause IsOff to return true.
  138. */
  139. static bool IsOff(const char* val);
  140. static std::string FindProgram(const char* name,
  141. const std::vector<std::string>& = std::vector<std::string>());
  142. static bool FileIsDirectory(const char* name);
  143. static std::string GetCurrentWorkingDirectory();
  144. static std::string GetProgramPath(const char*);
  145. static void SplitProgramPath(const char* in_name, std::string& dir, std::string& file);
  146. static std::string CollapseFullPath(const char*);
  147. static long int ModifiedTime(const char* filename);
  148. /**
  149. * Run an executable command and put the stdout in output.
  150. * A temporary file is created in the binaryDir for storing the
  151. * output because windows does not have popen.
  152. */
  153. static bool RunCommand(const char* command,
  154. std::string& output);
  155. ///! Generate a temporary file name
  156. static std::string TemporaryFileName();
  157. private:
  158. static bool s_ErrorOccured;
  159. };
  160. #endif