Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines things specific to Unix implementations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H |
| 15 | #define LLVM_LIB_SUPPORT_UNIX_UNIX_H |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 16 | |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 19 | //=== is guaranteed to work on all UNIX variants. |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 22 | #include "llvm/Config/config.h" // Get autoconf configuration settings |
| 23 | #include "llvm/Support/Chrono.h" |
Michael J. Spencer | 4d2e150 | 2010-11-29 18:29:55 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Errno.h" |
Chris Lattner | 6ef473f | 2004-12-08 16:10:52 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 26 | #include <assert.h> |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 27 | #include <cerrno> |
| 28 | #include <cstdio> |
| 29 | #include <cstdlib> |
| 30 | #include <cstring> |
| 31 | #include <string> |
Reid Kleckner | 37f69de | 2013-07-26 16:54:23 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
Justin Bogner | c7e3f3a | 2015-08-04 06:29:58 +0000 | [diff] [blame] | 33 | #include <sys/wait.h> |
Reid Spencer | 814ba57 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 34 | |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 35 | #ifdef HAVE_UNISTD_H |
| 36 | #include <unistd.h> |
| 37 | #endif |
| 38 | |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 39 | #ifdef HAVE_SYS_PARAM_H |
| 40 | #include <sys/param.h> |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 41 | #endif |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 42 | |
Michael J. Spencer | 04b795b | 2012-04-23 19:00:27 +0000 | [diff] [blame] | 43 | #ifdef HAVE_SYS_TIME_H |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 44 | # include <sys/time.h> |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 45 | #endif |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 46 | #include <time.h> |
Reid Spencer | e925d8b | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | 9ab9fe9 | 2013-10-08 16:12:58 +0000 | [diff] [blame] | 48 | #ifdef HAVE_DLFCN_H |
| 49 | # include <dlfcn.h> |
| 50 | #endif |
| 51 | |
Eugene Leviant | ea877d4 | 2016-08-26 08:14:54 +0000 | [diff] [blame] | 52 | #ifdef HAVE_FCNTL_H |
| 53 | # include <fcntl.h> |
| 54 | #endif |
| 55 | |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 56 | /// This function builds an error message into \p ErrMsg using the \p prefix |
| 57 | /// string and the Unix error number given by \p errnum. If errnum is -1, the |
| 58 | /// default then the value of errno is used. |
| 59 | /// @brief Make an error message |
Daniel Dunbar | 3222b9b | 2009-04-20 20:50:13 +0000 | [diff] [blame] | 60 | /// |
| 61 | /// If the error number can be converted to a string, it will be |
| 62 | /// separated from prefix by ": ". |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 63 | static inline bool MakeErrMsg( |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 64 | std::string* ErrMsg, const std::string& prefix, int errnum = -1) { |
| 65 | if (!ErrMsg) |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 66 | return true; |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 67 | if (errnum == -1) |
| 68 | errnum = errno; |
Jeffrey Yasskin | 1a93330 | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 69 | *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum); |
Reid Spencer | 879ed5a | 2006-08-23 07:30:48 +0000 | [diff] [blame] | 70 | return true; |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 73 | namespace llvm { |
| 74 | namespace sys { |
| 75 | |
| 76 | /// Convert a struct timeval to a duration. Note that timeval can be used both |
| 77 | /// as a time point and a duration. Be sure to check what the input represents. |
| 78 | inline std::chrono::microseconds toDuration(const struct timeval &TV) { |
| 79 | return std::chrono::seconds(TV.tv_sec) + |
| 80 | std::chrono::microseconds(TV.tv_usec); |
| 81 | } |
| 82 | |
| 83 | /// Convert a time point to struct timespec. |
| 84 | inline struct timespec toTimeSpec(TimePoint<> TP) { |
| 85 | using namespace std::chrono; |
| 86 | |
| 87 | struct timespec RetVal; |
| 88 | RetVal.tv_sec = toTimeT(TP); |
| 89 | RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count(); |
| 90 | return RetVal; |
| 91 | } |
| 92 | |
| 93 | /// Convert a time point to struct timeval. |
| 94 | inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) { |
| 95 | using namespace std::chrono; |
| 96 | |
| 97 | struct timeval RetVal; |
| 98 | RetVal.tv_sec = toTimeT(TP); |
| 99 | RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count(); |
| 100 | return RetVal; |
| 101 | } |
| 102 | |
| 103 | } // namespace sys |
| 104 | } // namespace llvm |
| 105 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 106 | #endif |