1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSystemTools.h,v $
6 Date: $Date: 2008-08-14 13:53:26 $
7 Version: $Revision: 1.155 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #ifndef cmSystemTools_h
18 #define cmSystemTools_h
20 #include "cmStandardIncludes.h"
22 #include <cmsys/SystemTools.hxx>
23 #include <cmsys/Process.h>
25 class cmSystemToolsFileTime
;
27 /** \class cmSystemTools
28 * \brief A collection of useful functions for CMake.
30 * cmSystemTools is a class that provides helper functions
31 * for the CMake build system.
33 class cmSystemTools
: public cmsys::SystemTools
36 typedef cmsys::SystemTools Superclass
;
38 /** Expand out any arguements in the vector that have ; separated
39 * strings into multiple arguements. A new vector is created
40 * containing the expanded versions of all arguments in argsIn.
42 static void ExpandList(std::vector
<std::string
> const& argsIn
,
43 std::vector
<std::string
>& argsOut
);
44 static void ExpandListArgument(const std::string
& arg
,
45 std::vector
<std::string
>& argsOut
,
46 bool emptyArgs
=false);
49 * Look for and replace registry values in a string
51 static void ExpandRegistryValues(std::string
& source
,
52 KeyWOW64 view
= KeyWOW64_Default
);
55 * Platform independent escape spaces, unix uses backslash,
56 * windows double quotes the string.
58 static std::string
EscapeSpaces(const char* str
);
60 ///! Escape quotes in a string.
61 static std::string
EscapeQuotes(const char* str
);
64 * Given a string, replace any escape sequences with the corresponding
67 static std::string
RemoveEscapes(const char*);
69 typedef void (*ErrorCallback
)(const char*, const char*, bool&, void*);
71 * Set the function used by GUI's to display error messages
72 * Function gets passed: message as a const char*,
73 * title as a const char*, and a reference to bool that when
74 * set to false, will disable furthur messages (cancel).
76 static void SetErrorCallback(ErrorCallback f
, void* clientData
=0);
79 * Display an error message.
81 static void Error(const char* m
, const char* m2
=0,
82 const char* m3
=0, const char* m4
=0);
87 static void Message(const char* m
, const char* title
=0);
89 ///! Send a string to stdout
90 static void Stdout(const char* s
);
91 static void Stdout(const char* s
, int length
);
92 typedef void (*StdoutCallback
)(const char*, int length
, void*);
93 static void SetStdoutCallback(StdoutCallback
, void* clientData
=0);
95 ///! Return true if there was an error at any point.
96 static bool GetErrorOccuredFlag()
98 return cmSystemTools::s_ErrorOccured
||
99 cmSystemTools::s_FatalErrorOccured
;
101 ///! If this is set to true, cmake stops processing commands.
102 static void SetFatalErrorOccured()
104 cmSystemTools::s_FatalErrorOccured
= true;
106 static void SetErrorOccured()
108 cmSystemTools::s_ErrorOccured
= true;
110 ///! Return true if there was an error at any point.
111 static bool GetFatalErrorOccured()
113 return cmSystemTools::s_FatalErrorOccured
;
116 ///! Set the error occured flag and fatal error back to false
117 static void ResetErrorOccuredFlag()
119 cmSystemTools::s_FatalErrorOccured
= false;
120 cmSystemTools::s_ErrorOccured
= false;
124 * does a string indicate a true or on value ? This is not the same
127 static bool IsOn(const char* val
);
130 * does a string indicate a false or off value ? Note that this is
131 * not the same as !IsOn(...) because there are a number of
132 * ambiguous values such as "/usr/local/bin" a path will result in
133 * IsON and IsOff both returning false. Note that the special path
134 * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
136 static bool IsOff(const char* val
);
138 ///! Return true if value is NOTFOUND or ends in -NOTFOUND.
139 static bool IsNOTFOUND(const char* value
);
140 ///! Return true if the path is a framework
141 static bool IsPathToFramework(const char* value
);
143 static bool DoesFileExistWithExtensions(
145 const std::vector
<std::string
>& sourceExts
);
147 static void Glob(const char *directory
, const char *regexp
,
148 std::vector
<std::string
>& files
);
149 static void GlobDirs(const char *fullPath
, std::vector
<std::string
>& files
);
152 * Try to find a list of files that match the "simple" globbing
153 * expression. At this point in time the globbing expressions have
154 * to be in form: /directory/partial_file_name*. The * character has
155 * to be at the end of the string and it does not support ?
156 * []... The optional argument type specifies what kind of files you
157 * want to find. 0 means all files, -1 means directories, 1 means
158 * files only. This method returns true if search was succesfull.
160 static bool SimpleGlob(const cmStdString
& glob
,
161 std::vector
<cmStdString
>& files
,
165 static bool cmCopyFile(const char* source
, const char* destination
);
166 static bool CopyFileIfDifferent(const char* source
,
167 const char* destination
);
169 ///! Compute the md5sum of a file
170 static bool ComputeFileMD5(const char* source
, char* md5out
);
172 /** Compute the md5sum of a string. */
173 static std::string
ComputeStringMD5(const char* input
);
176 * Run an executable command and put the stdout in output.
177 * A temporary file is created in the binaryDir for storing the
178 * output because windows does not have popen.
180 * If verbose is false, no user-viewable output from the program
181 * being run will be generated.
183 * If timeout is specified, the command will be terminated after
186 static bool RunCommand(const char* command
, std::string
& output
,
187 const char* directory
= 0,
188 bool verbose
= true, int timeout
= 0);
189 static bool RunCommand(const char* command
, std::string
& output
,
190 int &retVal
, const char* directory
= 0,
191 bool verbose
= true, int timeout
= 0);
193 * Run a single executable command and put the stdout and stderr
196 * If verbose is false, no user-viewable output from the program
197 * being run will be generated.
199 * If timeout is specified, the command will be terminated after
200 * timeout expires. Timeout is specified in seconds.
202 * Argument retVal should be a pointer to the location where the
203 * exit code will be stored. If the retVal is not specified and
204 * the program exits with a code other than 0, then the this
205 * function will return false.
207 * If the command has spaces in the path the caller MUST call
208 * cmSystemTools::ConvertToRunCommandPath on the command before passing
209 * it into this function or it will not work. The command must be correctly
210 * escaped for this to with spaces.
212 static bool RunSingleCommand(const char* command
, std::string
* output
= 0,
213 int* retVal
= 0, const char* dir
= 0,
215 double timeout
= 0.0);
217 * In this version of RunSingleCommand, command[0] should be
218 * the command to run, and each argument to the command should
219 * be in comand[1]...command[command.size()]
221 static bool RunSingleCommand(std::vector
<cmStdString
> const& command
,
222 std::string
* output
= 0,
223 int* retVal
= 0, const char* dir
= 0,
225 double timeout
= 0.0);
228 * Parse arguments out of a single string command
230 static std::vector
<cmStdString
> ParseArguments(const char* command
);
232 /** Parse arguments out of a windows command line string. */
233 static void ParseWindowsCommandLine(const char* command
,
234 std::vector
<std::string
>& args
);
236 /** Compute an escaped version of the given argument for use in a
237 windows shell. See kwsys/System.h.in for details. */
238 static std::string
EscapeWindowsShellArgument(const char* arg
,
241 static void EnableMessages() { s_DisableMessages
= false; }
242 static void DisableMessages() { s_DisableMessages
= true; }
243 static void DisableRunCommandOutput() {s_DisableRunCommandOutput
= true; }
244 static void EnableRunCommandOutput() {s_DisableRunCommandOutput
= false; }
245 static bool GetRunCommandOutput() { return s_DisableRunCommandOutput
; }
248 * Come constants for different file formats.
257 RESOURCE_FILE_FORMAT
,
258 DEFINITION_FILE_FORMAT
,
259 STATIC_LIBRARY_FILE_FORMAT
,
260 SHARED_LIBRARY_FILE_FORMAT
,
267 * Determine the file type based on the extension
269 static FileFormat
GetFileFormat(const char* ext
);
272 * On Windows 9x we need a comspec (command.com) substitute to run
273 * programs correctly. This string has to be constant available
274 * through the running of program. This method does not create a copy.
276 static void SetWindows9xComspecSubstitute(const char*);
277 static const char* GetWindows9xComspecSubstitute();
279 /** Windows if this is true, the CreateProcess in RunCommand will
280 * not show new consol windows when running programs.
282 static void SetRunCommandHideConsole(bool v
){s_RunCommandHideConsole
= v
;}
283 static bool GetRunCommandHideConsole(){ return s_RunCommandHideConsole
;}
284 /** Call cmSystemTools::Error with the message m, plus the
285 * result of strerror(errno)
287 static void ReportLastSystemError(const char* m
);
289 /** a general output handler for cmsysProcess */
290 static int WaitForLine(cmsysProcess
* process
, std::string
& line
,
292 std::vector
<char>& out
,
293 std::vector
<char>& err
);
295 /** Split a string on its newlines into multiple lines. Returns
296 false only if the last line stored had no newline. */
297 static bool Split(const char* s
, std::vector
<cmStdString
>& l
);
298 static void SetForceUnixPaths(bool v
)
300 s_ForceUnixPaths
= v
;
302 static bool GetForceUnixPaths()
304 return s_ForceUnixPaths
;
307 // ConvertToOutputPath use s_ForceUnixPaths
308 static std::string
ConvertToOutputPath(const char* path
);
309 static void ConvertToOutputSlashes(std::string
& path
);
311 // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
312 // be used when RunCommand is called from cmake, because the
313 // running cmake needs paths to be in its format
314 static std::string
ConvertToRunCommandPath(const char* path
);
315 //! Check if the first string ends with the second one.
316 static bool StringEndsWith(const char* str1
, const char* str2
);
318 /** compute the relative path from local to remote. local must
319 be a directory. remote can be a file or a directory.
320 Both remote and local must be full paths. Basically, if
321 you are in directory local and you want to access the file in remote
322 what is the relative path to do that. For example:
323 /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
324 from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
326 static std::string
RelativePath(const char* local
, const char* remote
);
328 /** Put a string into the environment
329 of the form var=value */
330 static bool PutEnv(const char* value
);
332 #ifdef CMAKE_BUILD_WITH_CMAKE
333 /** Remove an environment variable */
334 static bool UnsetEnv(const char* value
);
336 /** Get the list of all environment variables */
337 static std::vector
<std::string
> GetEnvironmentVariables();
340 /** Setup the environment to enable VS 8 IDE output. */
341 static void EnableVSConsoleOutput();
343 /** Make string XML safe */
344 static std::string
MakeXMLSafe(const char* str
);
347 static bool ListTar(const char* outFileName
,
348 std::vector
<cmStdString
>& files
,
349 bool gzip
, bool verbose
);
350 static bool CreateTar(const char* outFileName
,
351 const std::vector
<cmStdString
>& files
, bool gzip
,
353 static bool ExtractTar(const char* inFileName
,
354 const std::vector
<cmStdString
>& files
, bool gzip
,
356 // This should be called first thing in main
357 // it will keep child processes from inheriting the
358 // stdin and stdout of this process. This is important
359 // if you want to be able to kill child processes and
360 // not get stuck waiting for all the output on the pipes.
361 static void DoNotInheritStdPipes();
363 /** Copy the file create/access/modify times from the file named by
364 the first argument to that named by the second. */
365 static bool CopyFileTime(const char* fromFile
, const char* toFile
);
367 /** Save and restore file times. */
368 static cmSystemToolsFileTime
* FileTimeNew();
369 static void FileTimeDelete(cmSystemToolsFileTime
*);
370 static bool FileTimeGet(const char* fname
, cmSystemToolsFileTime
* t
);
371 static bool FileTimeSet(const char* fname
, cmSystemToolsFileTime
* t
);
373 /** Find the directory containing the running executable. Save it
374 in a global location to be queried by GetExecutableDirectory
376 static void FindExecutableDirectory(const char* argv0
);
378 /** Get the directory containing the currently running executable. */
379 static const char* GetExecutableDirectory();
381 #if defined(CMAKE_BUILD_WITH_CMAKE)
382 /** Echo a message in color using KWSys's Terminal cprintf. */
383 static void MakefileColorEcho(int color
, const char* message
,
384 bool newLine
, bool enabled
);
387 /** Try to guess the soname of a shared library. */
388 static bool GuessLibrarySOName(std::string
const& fullPath
,
389 std::string
& soname
);
391 /** Try to set the RPATH in an ELF binary. */
392 static bool ChangeRPath(std::string
const& file
,
393 std::string
const& oldRPath
,
394 std::string
const& newRPath
,
395 std::string
* emsg
= 0,
398 /** Try to remove the RPATH from an ELF binary. */
399 static bool RemoveRPath(std::string
const& file
, std::string
* emsg
= 0,
402 /** Check whether the RPATH in an ELF binary contains the path
404 static bool CheckRPath(std::string
const& file
,
405 std::string
const& newRPath
);
408 static bool s_ForceUnixPaths
;
409 static bool s_RunCommandHideConsole
;
410 static bool s_ErrorOccured
;
411 static bool s_FatalErrorOccured
;
412 static bool s_DisableMessages
;
413 static bool s_DisableRunCommandOutput
;
414 static ErrorCallback s_ErrorCallback
;
415 static StdoutCallback s_StdoutCallback
;
416 static void* s_ErrorCallbackClientData
;
417 static void* s_StdoutCallbackClientData
;
419 static std::string s_Windows9xComspecSubstitute
;