blob: 239a6d60aaefb2f3153410ca332cf39992542570 [file] [log] [blame]
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00001//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer814ba572004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer814ba572004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines things specific to Unix implementations.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
15#define LLVM_LIB_SUPPORT_UNIX_UNIX_H
Reid Spencere925d8b2004-12-27 06:17:50 +000016
Reid Spencer814ba572004-08-25 06:20:07 +000017//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only generic UNIX code that
19//=== is guaranteed to work on all UNIX variants.
20//===----------------------------------------------------------------------===//
21
Pavel Labath757ca882016-10-24 10:59:17 +000022#include "llvm/Config/config.h" // Get autoconf configuration settings
23#include "llvm/Support/Chrono.h"
Michael J. Spencer4d2e1502010-11-29 18:29:55 +000024#include "llvm/Support/Errno.h"
Chris Lattner6ef473f2004-12-08 16:10:52 +000025#include <algorithm>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000026#include <assert.h>
Chandler Carruth802d7552012-12-04 07:12:27 +000027#include <cerrno>
28#include <cstdio>
29#include <cstdlib>
30#include <cstring>
31#include <string>
Reid Kleckner37f69de2013-07-26 16:54:23 +000032#include <sys/types.h>
Justin Bognerc7e3f3a2015-08-04 06:29:58 +000033#include <sys/wait.h>
Reid Spencer814ba572004-08-25 06:20:07 +000034
Reid Spencere925d8b2004-12-27 06:17:50 +000035#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38
Reid Spencere925d8b2004-12-27 06:17:50 +000039#ifdef HAVE_SYS_PARAM_H
40#include <sys/param.h>
Misha Brukman10468d82005-04-21 22:55:34 +000041#endif
Reid Spencere925d8b2004-12-27 06:17:50 +000042
Michael J. Spencer04b795b2012-04-23 19:00:27 +000043#ifdef HAVE_SYS_TIME_H
Reid Spencere925d8b2004-12-27 06:17:50 +000044# include <sys/time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000045#endif
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000046#include <time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000047
Rafael Espindola9ab9fe92013-10-08 16:12:58 +000048#ifdef HAVE_DLFCN_H
49# include <dlfcn.h>
50#endif
51
Eugene Leviantea877d42016-08-26 08:14:54 +000052#ifdef HAVE_FCNTL_H
53# include <fcntl.h>
54#endif
55
Reid Spencer42bcf6e2006-08-21 06:02:44 +000056/// 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 Dunbar3222b9b2009-04-20 20:50:13 +000060///
61/// If the error number can be converted to a string, it will be
62/// separated from prefix by ": ".
Dan Gohmand78c4002008-05-13 00:00:25 +000063static inline bool MakeErrMsg(
Reid Spencer42bcf6e2006-08-21 06:02:44 +000064 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
65 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000066 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000067 if (errnum == -1)
68 errnum = errno;
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000069 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
Reid Spencer879ed5a2006-08-23 07:30:48 +000070 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000071}
72
Pavel Labath757ca882016-10-24 10:59:17 +000073namespace llvm {
74namespace 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.
78inline 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.
84inline 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.
94inline 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 Smith91d3cfe2016-04-05 20:45:04 +0000106#endif