ENH: put the 64 bit paths first
[cmake.git] / Source / cmSystemTools.h
blobea55fbf2acc66fee65ac23472e3e12a9069a9298
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSystemTools.h,v $
5 Language: C++
6 Date: $Date: 2009-06-05 16:01:29 $
7 Version: $Revision: 1.160 $
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
35 public:
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);
48 /**
49 * Look for and replace registry values in a string
51 static void ExpandRegistryValues(std::string& source,
52 KeyWOW64 view = KeyWOW64_Default);
54 /**
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);
63 /**
64 * Given a string, replace any escape sequences with the corresponding
65 * characters.
67 static std::string RemoveEscapes(const char*);
69 typedef void (*ErrorCallback)(const char*, const char*, bool&, void*);
70 /**
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);
78 /**
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);
84 /**
85 * Display a message.
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;
123 /**
124 * does a string indicate a true or on value ? This is not the same
125 * as ifdef.
127 static bool IsOn(const char* val);
129 /**
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(
144 const char *name,
145 const std::vector<std::string>& sourceExts);
148 * Check if the given file exists in one of the parent directory of the
149 * given file or directory and if it does, return the name of the file.
150 * Toplevel specifies the top-most directory to where it will look.
152 static std::string FileExistsInParentDirectories(const char* fname,
153 const char* directory, const char* toplevel);
155 static void Glob(const char *directory, const char *regexp,
156 std::vector<std::string>& files);
157 static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
160 * Try to find a list of files that match the "simple" globbing
161 * expression. At this point in time the globbing expressions have
162 * to be in form: /directory/partial_file_name*. The * character has
163 * to be at the end of the string and it does not support ?
164 * []... The optional argument type specifies what kind of files you
165 * want to find. 0 means all files, -1 means directories, 1 means
166 * files only. This method returns true if search was succesfull.
168 static bool SimpleGlob(const cmStdString& glob,
169 std::vector<cmStdString>& files,
170 int type = 0);
172 ///! Copy a file.
173 static bool cmCopyFile(const char* source, const char* destination);
174 static bool CopyFileIfDifferent(const char* source,
175 const char* destination);
177 /** Rename a file or directory within a single disk volume (atomic
178 if possible). */
179 static bool RenameFile(const char* oldname, const char* newname);
181 ///! Compute the md5sum of a file
182 static bool ComputeFileMD5(const char* source, char* md5out);
184 /** Compute the md5sum of a string. */
185 static std::string ComputeStringMD5(const char* input);
188 * Run an executable command and put the stdout in output.
189 * A temporary file is created in the binaryDir for storing the
190 * output because windows does not have popen.
192 * If verbose is false, no user-viewable output from the program
193 * being run will be generated.
195 * If timeout is specified, the command will be terminated after
196 * timeout expires.
198 static bool RunCommand(const char* command, std::string& output,
199 const char* directory = 0,
200 bool verbose = true, int timeout = 0);
201 static bool RunCommand(const char* command, std::string& output,
202 int &retVal, const char* directory = 0,
203 bool verbose = true, int timeout = 0);
205 * Run a single executable command and put the stdout and stderr
206 * in output.
208 * If verbose is false, no user-viewable output from the program
209 * being run will be generated.
211 * If timeout is specified, the command will be terminated after
212 * timeout expires. Timeout is specified in seconds.
214 * Argument retVal should be a pointer to the location where the
215 * exit code will be stored. If the retVal is not specified and
216 * the program exits with a code other than 0, then the this
217 * function will return false.
219 * If the command has spaces in the path the caller MUST call
220 * cmSystemTools::ConvertToRunCommandPath on the command before passing
221 * it into this function or it will not work. The command must be correctly
222 * escaped for this to with spaces.
224 static bool RunSingleCommand(const char* command, std::string* output = 0,
225 int* retVal = 0, const char* dir = 0,
226 bool verbose = true,
227 double timeout = 0.0);
228 /**
229 * In this version of RunSingleCommand, command[0] should be
230 * the command to run, and each argument to the command should
231 * be in comand[1]...command[command.size()]
233 static bool RunSingleCommand(std::vector<cmStdString> const& command,
234 std::string* output = 0,
235 int* retVal = 0, const char* dir = 0,
236 bool verbose = true,
237 double timeout = 0.0);
240 * Parse arguments out of a single string command
242 static std::vector<cmStdString> ParseArguments(const char* command);
244 /** Parse arguments out of a windows command line string. */
245 static void ParseWindowsCommandLine(const char* command,
246 std::vector<std::string>& args);
248 /** Compute an escaped version of the given argument for use in a
249 windows shell. See kwsys/System.h.in for details. */
250 static std::string EscapeWindowsShellArgument(const char* arg,
251 int shell_flags);
253 static void EnableMessages() { s_DisableMessages = false; }
254 static void DisableMessages() { s_DisableMessages = true; }
255 static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
256 static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
257 static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
260 * Come constants for different file formats.
262 enum FileFormat {
263 NO_FILE_FORMAT = 0,
264 C_FILE_FORMAT,
265 CXX_FILE_FORMAT,
266 FORTRAN_FILE_FORMAT,
267 JAVA_FILE_FORMAT,
268 HEADER_FILE_FORMAT,
269 RESOURCE_FILE_FORMAT,
270 DEFINITION_FILE_FORMAT,
271 STATIC_LIBRARY_FILE_FORMAT,
272 SHARED_LIBRARY_FILE_FORMAT,
273 MODULE_FILE_FORMAT,
274 OBJECT_FILE_FORMAT,
275 UNKNOWN_FILE_FORMAT
279 * Determine the file type based on the extension
281 static FileFormat GetFileFormat(const char* ext);
284 * On Windows 9x we need a comspec (command.com) substitute to run
285 * programs correctly. This string has to be constant available
286 * through the running of program. This method does not create a copy.
288 static void SetWindows9xComspecSubstitute(const char*);
289 static const char* GetWindows9xComspecSubstitute();
291 /** Windows if this is true, the CreateProcess in RunCommand will
292 * not show new consol windows when running programs.
294 static void SetRunCommandHideConsole(bool v){s_RunCommandHideConsole = v;}
295 static bool GetRunCommandHideConsole(){ return s_RunCommandHideConsole;}
296 /** Call cmSystemTools::Error with the message m, plus the
297 * result of strerror(errno)
299 static void ReportLastSystemError(const char* m);
301 /** a general output handler for cmsysProcess */
302 static int WaitForLine(cmsysProcess* process, std::string& line,
303 double timeout,
304 std::vector<char>& out,
305 std::vector<char>& err);
307 /** Split a string on its newlines into multiple lines. Returns
308 false only if the last line stored had no newline. */
309 static bool Split(const char* s, std::vector<cmStdString>& l);
310 static void SetForceUnixPaths(bool v)
312 s_ForceUnixPaths = v;
314 static bool GetForceUnixPaths()
316 return s_ForceUnixPaths;
319 // ConvertToOutputPath use s_ForceUnixPaths
320 static std::string ConvertToOutputPath(const char* path);
321 static void ConvertToOutputSlashes(std::string& path);
323 // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
324 // be used when RunCommand is called from cmake, because the
325 // running cmake needs paths to be in its format
326 static std::string ConvertToRunCommandPath(const char* path);
327 //! Check if the first string ends with the second one.
328 static bool StringEndsWith(const char* str1, const char* str2);
330 /** compute the relative path from local to remote. local must
331 be a directory. remote can be a file or a directory.
332 Both remote and local must be full paths. Basically, if
333 you are in directory local and you want to access the file in remote
334 what is the relative path to do that. For example:
335 /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
336 from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
338 static std::string RelativePath(const char* local, const char* remote);
340 #ifdef CMAKE_BUILD_WITH_CMAKE
341 /** Remove an environment variable */
342 static bool UnsetEnv(const char* value);
344 /** Get the list of all environment variables */
345 static std::vector<std::string> GetEnvironmentVariables();
347 /** Append multiple variables to the current environment.
348 Return the original environment, as it was before the
349 append. */
350 static std::vector<std::string> AppendEnv(
351 std::vector<std::string>* env);
353 /** Restore the full environment to "env" - use after
354 AppendEnv to put the environment back to the way it
355 was. */
356 static void RestoreEnv(const std::vector<std::string>& env);
357 #endif
359 /** Setup the environment to enable VS 8 IDE output. */
360 static void EnableVSConsoleOutput();
362 /** Create tar */
363 static bool ListTar(const char* outFileName,
364 std::vector<cmStdString>& files,
365 bool gzip, bool verbose);
366 static bool CreateTar(const char* outFileName,
367 const std::vector<cmStdString>& files, bool gzip,
368 bool verbose);
369 static bool ExtractTar(const char* inFileName,
370 const std::vector<cmStdString>& files, bool gzip,
371 bool verbose);
372 // This should be called first thing in main
373 // it will keep child processes from inheriting the
374 // stdin and stdout of this process. This is important
375 // if you want to be able to kill child processes and
376 // not get stuck waiting for all the output on the pipes.
377 static void DoNotInheritStdPipes();
379 /** Copy the file create/access/modify times from the file named by
380 the first argument to that named by the second. */
381 static bool CopyFileTime(const char* fromFile, const char* toFile);
383 /** Save and restore file times. */
384 static cmSystemToolsFileTime* FileTimeNew();
385 static void FileTimeDelete(cmSystemToolsFileTime*);
386 static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t);
387 static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t);
389 /** Find the directory containing the running executable. Save it
390 in a global location to be queried by GetExecutableDirectory
391 later. */
392 static void FindExecutableDirectory(const char* argv0);
394 /** Get the directory containing the currently running executable. */
395 static const char* GetExecutableDirectory();
397 #if defined(CMAKE_BUILD_WITH_CMAKE)
398 /** Echo a message in color using KWSys's Terminal cprintf. */
399 static void MakefileColorEcho(int color, const char* message,
400 bool newLine, bool enabled);
401 #endif
403 /** Try to guess the soname of a shared library. */
404 static bool GuessLibrarySOName(std::string const& fullPath,
405 std::string& soname);
407 /** Try to set the RPATH in an ELF binary. */
408 static bool ChangeRPath(std::string const& file,
409 std::string const& oldRPath,
410 std::string const& newRPath,
411 std::string* emsg = 0,
412 bool* changed = 0);
414 /** Try to remove the RPATH from an ELF binary. */
415 static bool RemoveRPath(std::string const& file, std::string* emsg = 0,
416 bool* removed = 0);
418 /** Check whether the RPATH in an ELF binary contains the path
419 given. */
420 static bool CheckRPath(std::string const& file,
421 std::string const& newRPath);
423 private:
424 static bool s_ForceUnixPaths;
425 static bool s_RunCommandHideConsole;
426 static bool s_ErrorOccured;
427 static bool s_FatalErrorOccured;
428 static bool s_DisableMessages;
429 static bool s_DisableRunCommandOutput;
430 static ErrorCallback s_ErrorCallback;
431 static StdoutCallback s_StdoutCallback;
432 static void* s_ErrorCallbackClientData;
433 static void* s_StdoutCallbackClientData;
435 static std::string s_Windows9xComspecSubstitute;
438 #endif