Skip implicit link info for multiple OS X archs
[cmake.git] / Source / cmSystemTools.h
blob764c8c876c54c395db746bb8dbf9f14ff572b2d8
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmSystemTools.h,v $
5 Language: C++
6 Date: $Date: 2009-07-13 20:22:48 $
7 Version: $Revision: 1.162 $
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 typedef void (*ErrorCallback)(const char*, const char*, bool&, void*);
64 /**
65 * Set the function used by GUI's to display error messages
66 * Function gets passed: message as a const char*,
67 * title as a const char*, and a reference to bool that when
68 * set to false, will disable furthur messages (cancel).
70 static void SetErrorCallback(ErrorCallback f, void* clientData=0);
72 /**
73 * Display an error message.
75 static void Error(const char* m, const char* m2=0,
76 const char* m3=0, const char* m4=0);
78 /**
79 * Display a message.
81 static void Message(const char* m, const char* title=0);
83 ///! Send a string to stdout
84 static void Stdout(const char* s);
85 static void Stdout(const char* s, int length);
86 typedef void (*StdoutCallback)(const char*, int length, void*);
87 static void SetStdoutCallback(StdoutCallback, void* clientData=0);
89 ///! Return true if there was an error at any point.
90 static bool GetErrorOccuredFlag()
92 return cmSystemTools::s_ErrorOccured ||
93 cmSystemTools::s_FatalErrorOccured;
95 ///! If this is set to true, cmake stops processing commands.
96 static void SetFatalErrorOccured()
98 cmSystemTools::s_FatalErrorOccured = true;
100 static void SetErrorOccured()
102 cmSystemTools::s_ErrorOccured = true;
104 ///! Return true if there was an error at any point.
105 static bool GetFatalErrorOccured()
107 return cmSystemTools::s_FatalErrorOccured;
110 ///! Set the error occured flag and fatal error back to false
111 static void ResetErrorOccuredFlag()
113 cmSystemTools::s_FatalErrorOccured = false;
114 cmSystemTools::s_ErrorOccured = false;
117 /**
118 * does a string indicate a true or on value ? This is not the same
119 * as ifdef.
121 static bool IsOn(const char* val);
123 /**
124 * does a string indicate a false or off value ? Note that this is
125 * not the same as !IsOn(...) because there are a number of
126 * ambiguous values such as "/usr/local/bin" a path will result in
127 * IsON and IsOff both returning false. Note that the special path
128 * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
130 static bool IsOff(const char* val);
132 ///! Return true if value is NOTFOUND or ends in -NOTFOUND.
133 static bool IsNOTFOUND(const char* value);
134 ///! Return true if the path is a framework
135 static bool IsPathToFramework(const char* value);
137 static bool DoesFileExistWithExtensions(
138 const char *name,
139 const std::vector<std::string>& sourceExts);
142 * Check if the given file exists in one of the parent directory of the
143 * given file or directory and if it does, return the name of the file.
144 * Toplevel specifies the top-most directory to where it will look.
146 static std::string FileExistsInParentDirectories(const char* fname,
147 const char* directory, const char* toplevel);
149 static void Glob(const char *directory, const char *regexp,
150 std::vector<std::string>& files);
151 static void GlobDirs(const char *fullPath, std::vector<std::string>& files);
154 * Try to find a list of files that match the "simple" globbing
155 * expression. At this point in time the globbing expressions have
156 * to be in form: /directory/partial_file_name*. The * character has
157 * to be at the end of the string and it does not support ?
158 * []... The optional argument type specifies what kind of files you
159 * want to find. 0 means all files, -1 means directories, 1 means
160 * files only. This method returns true if search was succesfull.
162 static bool SimpleGlob(const cmStdString& glob,
163 std::vector<cmStdString>& files,
164 int type = 0);
166 ///! Copy a file.
167 static bool cmCopyFile(const char* source, const char* destination);
168 static bool CopyFileIfDifferent(const char* source,
169 const char* destination);
171 /** Rename a file or directory within a single disk volume (atomic
172 if possible). */
173 static bool RenameFile(const char* oldname, const char* newname);
175 ///! Compute the md5sum of a file
176 static bool ComputeFileMD5(const char* source, char* md5out);
178 /** Compute the md5sum of a string. */
179 static std::string ComputeStringMD5(const char* input);
182 * Run an executable command and put the stdout in output.
183 * A temporary file is created in the binaryDir for storing the
184 * output because windows does not have popen.
186 * If verbose is false, no user-viewable output from the program
187 * being run will be generated.
189 * If timeout is specified, the command will be terminated after
190 * timeout expires.
192 static bool RunCommand(const char* command, std::string& output,
193 const char* directory = 0,
194 bool verbose = true, int timeout = 0);
195 static bool RunCommand(const char* command, std::string& output,
196 int &retVal, const char* directory = 0,
197 bool verbose = true, int timeout = 0);
199 * Run a single executable command and put the stdout and stderr
200 * in output.
202 * If verbose is false, no user-viewable output from the program
203 * being run will be generated.
205 * If timeout is specified, the command will be terminated after
206 * timeout expires. Timeout is specified in seconds.
208 * Argument retVal should be a pointer to the location where the
209 * exit code will be stored. If the retVal is not specified and
210 * the program exits with a code other than 0, then the this
211 * function will return false.
213 * If the command has spaces in the path the caller MUST call
214 * cmSystemTools::ConvertToRunCommandPath on the command before passing
215 * it into this function or it will not work. The command must be correctly
216 * escaped for this to with spaces.
218 static bool RunSingleCommand(const char* command, std::string* output = 0,
219 int* retVal = 0, const char* dir = 0,
220 bool verbose = true,
221 double timeout = 0.0);
222 /**
223 * In this version of RunSingleCommand, command[0] should be
224 * the command to run, and each argument to the command should
225 * be in comand[1]...command[command.size()]
227 static bool RunSingleCommand(std::vector<cmStdString> const& command,
228 std::string* output = 0,
229 int* retVal = 0, const char* dir = 0,
230 bool verbose = true,
231 double timeout = 0.0);
234 * Parse arguments out of a single string command
236 static std::vector<cmStdString> ParseArguments(const char* command);
238 /** Parse arguments out of a windows command line string. */
239 static void ParseWindowsCommandLine(const char* command,
240 std::vector<std::string>& args);
242 /** Parse arguments out of a unix command line string. */
243 static void ParseUnixCommandLine(const char* command,
244 std::vector<std::string>& args);
246 /** Compute an escaped version of the given argument for use in a
247 windows shell. See kwsys/System.h.in for details. */
248 static std::string EscapeWindowsShellArgument(const char* arg,
249 int shell_flags);
251 static void EnableMessages() { s_DisableMessages = false; }
252 static void DisableMessages() { s_DisableMessages = true; }
253 static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }
254 static void EnableRunCommandOutput() {s_DisableRunCommandOutput = false; }
255 static bool GetRunCommandOutput() { return s_DisableRunCommandOutput; }
258 * Come constants for different file formats.
260 enum FileFormat {
261 NO_FILE_FORMAT = 0,
262 C_FILE_FORMAT,
263 CXX_FILE_FORMAT,
264 FORTRAN_FILE_FORMAT,
265 JAVA_FILE_FORMAT,
266 HEADER_FILE_FORMAT,
267 RESOURCE_FILE_FORMAT,
268 DEFINITION_FILE_FORMAT,
269 STATIC_LIBRARY_FILE_FORMAT,
270 SHARED_LIBRARY_FILE_FORMAT,
271 MODULE_FILE_FORMAT,
272 OBJECT_FILE_FORMAT,
273 UNKNOWN_FILE_FORMAT
277 * Determine the file type based on the extension
279 static FileFormat GetFileFormat(const char* ext);
282 * On Windows 9x we need a comspec (command.com) substitute to run
283 * programs correctly. This string has to be constant available
284 * through the running of program. This method does not create a copy.
286 static void SetWindows9xComspecSubstitute(const char*);
287 static const char* GetWindows9xComspecSubstitute();
289 /** Windows if this is true, the CreateProcess in RunCommand will
290 * not show new consol windows when running programs.
292 static void SetRunCommandHideConsole(bool v){s_RunCommandHideConsole = v;}
293 static bool GetRunCommandHideConsole(){ return s_RunCommandHideConsole;}
294 /** Call cmSystemTools::Error with the message m, plus the
295 * result of strerror(errno)
297 static void ReportLastSystemError(const char* m);
299 /** a general output handler for cmsysProcess */
300 static int WaitForLine(cmsysProcess* process, std::string& line,
301 double timeout,
302 std::vector<char>& out,
303 std::vector<char>& err);
305 /** Split a string on its newlines into multiple lines. Returns
306 false only if the last line stored had no newline. */
307 static bool Split(const char* s, std::vector<cmStdString>& l);
308 static void SetForceUnixPaths(bool v)
310 s_ForceUnixPaths = v;
312 static bool GetForceUnixPaths()
314 return s_ForceUnixPaths;
317 // ConvertToOutputPath use s_ForceUnixPaths
318 static std::string ConvertToOutputPath(const char* path);
319 static void ConvertToOutputSlashes(std::string& path);
321 // ConvertToRunCommandPath does not use s_ForceUnixPaths and should
322 // be used when RunCommand is called from cmake, because the
323 // running cmake needs paths to be in its format
324 static std::string ConvertToRunCommandPath(const char* path);
325 //! Check if the first string ends with the second one.
326 static bool StringEndsWith(const char* str1, const char* str2);
328 /** compute the relative path from local to remote. local must
329 be a directory. remote can be a file or a directory.
330 Both remote and local must be full paths. Basically, if
331 you are in directory local and you want to access the file in remote
332 what is the relative path to do that. For example:
333 /a/b/c/d to /a/b/c1/d1 -> ../../c1/d1
334 from /usr/src to /usr/src/test/blah/foo.cpp -> test/blah/foo.cpp
336 static std::string RelativePath(const char* local, const char* remote);
338 #ifdef CMAKE_BUILD_WITH_CMAKE
339 /** Remove an environment variable */
340 static bool UnsetEnv(const char* value);
342 /** Get the list of all environment variables */
343 static std::vector<std::string> GetEnvironmentVariables();
345 /** Append multiple variables to the current environment.
346 Return the original environment, as it was before the
347 append. */
348 static std::vector<std::string> AppendEnv(
349 std::vector<std::string>* env);
351 /** Restore the full environment to "env" - use after
352 AppendEnv to put the environment back to the way it
353 was. */
354 static void RestoreEnv(const std::vector<std::string>& env);
355 #endif
357 /** Setup the environment to enable VS 8 IDE output. */
358 static void EnableVSConsoleOutput();
360 /** Create tar */
361 static bool ListTar(const char* outFileName,
362 std::vector<cmStdString>& files,
363 bool gzip, bool verbose);
364 static bool CreateTar(const char* outFileName,
365 const std::vector<cmStdString>& files, bool gzip,
366 bool verbose);
367 static bool ExtractTar(const char* inFileName,
368 const std::vector<cmStdString>& files, bool gzip,
369 bool verbose);
370 // This should be called first thing in main
371 // it will keep child processes from inheriting the
372 // stdin and stdout of this process. This is important
373 // if you want to be able to kill child processes and
374 // not get stuck waiting for all the output on the pipes.
375 static void DoNotInheritStdPipes();
377 /** Copy the file create/access/modify times from the file named by
378 the first argument to that named by the second. */
379 static bool CopyFileTime(const char* fromFile, const char* toFile);
381 /** Save and restore file times. */
382 static cmSystemToolsFileTime* FileTimeNew();
383 static void FileTimeDelete(cmSystemToolsFileTime*);
384 static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t);
385 static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t);
387 /** Find the directory containing the running executable. Save it
388 in a global location to be queried by GetExecutableDirectory
389 later. */
390 static void FindExecutableDirectory(const char* argv0);
392 /** Get the directory containing the currently running executable. */
393 static const char* GetExecutableDirectory();
395 #if defined(CMAKE_BUILD_WITH_CMAKE)
396 /** Echo a message in color using KWSys's Terminal cprintf. */
397 static void MakefileColorEcho(int color, const char* message,
398 bool newLine, bool enabled);
399 #endif
401 /** Try to guess the soname of a shared library. */
402 static bool GuessLibrarySOName(std::string const& fullPath,
403 std::string& soname);
405 /** Try to set the RPATH in an ELF binary. */
406 static bool ChangeRPath(std::string const& file,
407 std::string const& oldRPath,
408 std::string const& newRPath,
409 std::string* emsg = 0,
410 bool* changed = 0);
412 /** Try to remove the RPATH from an ELF binary. */
413 static bool RemoveRPath(std::string const& file, std::string* emsg = 0,
414 bool* removed = 0);
416 /** Check whether the RPATH in an ELF binary contains the path
417 given. */
418 static bool CheckRPath(std::string const& file,
419 std::string const& newRPath);
421 private:
422 static bool s_ForceUnixPaths;
423 static bool s_RunCommandHideConsole;
424 static bool s_ErrorOccured;
425 static bool s_FatalErrorOccured;
426 static bool s_DisableMessages;
427 static bool s_DisableRunCommandOutput;
428 static ErrorCallback s_ErrorCallback;
429 static StdoutCallback s_StdoutCallback;
430 static void* s_ErrorCallbackClientData;
431 static void* s_StdoutCallbackClientData;
433 static std::string s_Windows9xComspecSubstitute;
436 #endif