blob: e16a226a8eafe6505b03affc68fa0bfe27db91ab [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +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
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Config/config.h" // Get autoconf configuration settings
Michael J. Spencer4d2e1502010-11-29 18:29:55 +000023#include "llvm/Support/Errno.h"
Chris Lattner6ef473f2004-12-08 16:10:52 +000024#include <algorithm>
Reid Kleckner37f69de2013-07-26 16:54:23 +000025#include <assert.h>
Chandler Carruth802d7552012-12-04 07:12:27 +000026#include <cerrno>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30#include <string>
Reid Kleckner37f69de2013-07-26 16:54:23 +000031#include <sys/types.h>
Reid Spencer814ba572004-08-25 06:20:07 +000032
Reid Spencere925d8b2004-12-27 06:17:50 +000033#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36
Reid Spencere925d8b2004-12-27 06:17:50 +000037#ifdef HAVE_SYS_PARAM_H
38#include <sys/param.h>
Misha Brukman10468d82005-04-21 22:55:34 +000039#endif
Reid Spencere925d8b2004-12-27 06:17:50 +000040
Michael J. Spencer04b795b2012-04-23 19:00:27 +000041#ifdef HAVE_SYS_TIME_H
Reid Spencere925d8b2004-12-27 06:17:50 +000042# include <sys/time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000043#endif
Michael J. Spencer04b795b2012-04-23 19:00:27 +000044#include <time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000045
46#ifdef HAVE_SYS_WAIT_H
47# include <sys/wait.h>
48#endif
49
Rafael Espindola9ab9fe92013-10-08 16:12:58 +000050#ifdef HAVE_DLFCN_H
51# include <dlfcn.h>
52#endif
53
Reid Spencere925d8b2004-12-27 06:17:50 +000054#ifndef WEXITSTATUS
55# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
56#endif
57
58#ifndef WIFEXITED
59# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
60#endif
61
Reid Spencer42bcf6e2006-08-21 06:02:44 +000062/// This function builds an error message into \p ErrMsg using the \p prefix
63/// string and the Unix error number given by \p errnum. If errnum is -1, the
64/// default then the value of errno is used.
65/// @brief Make an error message
Daniel Dunbar3222b9b2009-04-20 20:50:13 +000066///
67/// If the error number can be converted to a string, it will be
68/// separated from prefix by ": ".
Dan Gohmand78c4002008-05-13 00:00:25 +000069static inline bool MakeErrMsg(
Reid Spencer42bcf6e2006-08-21 06:02:44 +000070 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
71 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000072 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000073 if (errnum == -1)
74 errnum = errno;
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000075 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
Reid Spencer879ed5a2006-08-23 07:30:48 +000076 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000077}
78
Reid Spencere925d8b2004-12-27 06:17:50 +000079#endif