2013-09-12 16:07:52 +08:00
|
|
|
|
/* Output to stdout / stderr for GNU make
|
2016-02-29 01:55:20 +08:00
|
|
|
|
Copyright (C) 2013-2016 Free Software Foundation, Inc.
|
2013-09-12 16:07:52 +08:00
|
|
|
|
This file is part of GNU Make.
|
|
|
|
|
|
|
|
|
|
GNU Make is free software; you can redistribute it and/or modify it under the
|
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
|
Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
version.
|
|
|
|
|
|
|
|
|
|
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
|
|
#include "makeint.h"
|
|
|
|
|
#include "job.h"
|
|
|
|
|
|
|
|
|
|
/* GNU make no longer supports pre-ANSI89 environments. */
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
2013-09-30 01:45:25 +08:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
|
# include <fcntl.h>
|
|
|
|
|
#else
|
|
|
|
|
# include <sys/file.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-16 16:30:01 +08:00
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
# include <windows.h>
|
2013-09-30 01:45:25 +08:00
|
|
|
|
# include <io.h>
|
2013-09-16 16:30:01 +08:00
|
|
|
|
# include "sub_proc.h"
|
|
|
|
|
#endif /* WINDOWS32 */
|
|
|
|
|
|
2013-09-12 16:07:52 +08:00
|
|
|
|
struct output *output_context = NULL;
|
2013-09-19 13:15:22 +08:00
|
|
|
|
unsigned int stdio_traced = 0;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
#define OUTPUT_NONE (-1)
|
|
|
|
|
|
|
|
|
|
#define OUTPUT_ISSET(_out) ((_out)->out >= 0 || (_out)->err >= 0)
|
|
|
|
|
|
2016-03-09 12:07:14 +08:00
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2013-11-24 11:23:52 +08:00
|
|
|
|
# define STREAM_OK(_s) ((fcntl (fileno (_s), F_GETFD) != -1) || (errno != EBADF))
|
2013-09-22 05:37:59 +08:00
|
|
|
|
#else
|
2013-11-24 11:23:52 +08:00
|
|
|
|
# define STREAM_OK(_s) 1
|
2013-09-12 16:07:52 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-09-26 07:06:56 +08:00
|
|
|
|
#if defined(HAVE_UMASK)
|
|
|
|
|
# define UMASK(_m) umask (_m)
|
|
|
|
|
# define MODE_T mode_t
|
|
|
|
|
#else
|
|
|
|
|
# define UMASK(_m) 0
|
|
|
|
|
# define MODE_T int
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-12 16:07:52 +08:00
|
|
|
|
/* Write a string to the current STDOUT or STDERR. */
|
|
|
|
|
static void
|
2013-09-14 13:04:04 +08:00
|
|
|
|
_outputs (struct output *out, int is_err, const char *msg)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
2013-09-14 13:04:04 +08:00
|
|
|
|
if (! out || ! out->syncout)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
|
|
|
|
FILE *f = is_err ? stderr : stdout;
|
|
|
|
|
fputs (msg, f);
|
|
|
|
|
fflush (f);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-09-14 13:04:04 +08:00
|
|
|
|
int fd = is_err ? out->err : out->out;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
int len = strlen (msg);
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
EINTRLOOP (r, lseek (fd, 0, SEEK_END));
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
EINTRLOOP (r, write (fd, msg, len));
|
2013-09-30 09:57:22 +08:00
|
|
|
|
if (r == len || r <= 0)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
break;
|
|
|
|
|
len -= r;
|
|
|
|
|
msg += r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Write a message indicating that we've just entered or
|
|
|
|
|
left (according to ENTERING) the current directory. */
|
|
|
|
|
|
|
|
|
|
static int
|
2013-09-30 09:57:22 +08:00
|
|
|
|
log_working_directory (int entering)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
|
|
|
|
static char *buf = NULL;
|
|
|
|
|
static unsigned int len = 0;
|
|
|
|
|
unsigned int need;
|
|
|
|
|
const char *fmt;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
/* Get enough space for the longest possible output. */
|
2013-11-24 11:23:52 +08:00
|
|
|
|
need = strlen (program) + INTSTR_LENGTH + 2 + 1;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
if (starting_directory)
|
|
|
|
|
need += strlen (starting_directory);
|
|
|
|
|
|
|
|
|
|
/* Use entire sentences to give the translators a fighting chance. */
|
|
|
|
|
if (makelevel == 0)
|
|
|
|
|
if (starting_directory == 0)
|
|
|
|
|
if (entering)
|
|
|
|
|
fmt = _("%s: Entering an unknown directory\n");
|
|
|
|
|
else
|
|
|
|
|
fmt = _("%s: Leaving an unknown directory\n");
|
|
|
|
|
else
|
|
|
|
|
if (entering)
|
|
|
|
|
fmt = _("%s: Entering directory '%s'\n");
|
|
|
|
|
else
|
|
|
|
|
fmt = _("%s: Leaving directory '%s'\n");
|
|
|
|
|
else
|
|
|
|
|
if (starting_directory == 0)
|
|
|
|
|
if (entering)
|
|
|
|
|
fmt = _("%s[%u]: Entering an unknown directory\n");
|
|
|
|
|
else
|
|
|
|
|
fmt = _("%s[%u]: Leaving an unknown directory\n");
|
|
|
|
|
else
|
|
|
|
|
if (entering)
|
|
|
|
|
fmt = _("%s[%u]: Entering directory '%s'\n");
|
|
|
|
|
else
|
|
|
|
|
fmt = _("%s[%u]: Leaving directory '%s'\n");
|
|
|
|
|
|
|
|
|
|
need += strlen (fmt);
|
|
|
|
|
|
|
|
|
|
if (need > len)
|
|
|
|
|
{
|
|
|
|
|
buf = xrealloc (buf, need);
|
|
|
|
|
len = need;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
|
if (print_data_base_flag)
|
|
|
|
|
{
|
|
|
|
|
*(p++) = '#';
|
|
|
|
|
*(p++) = ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (makelevel == 0)
|
|
|
|
|
if (starting_directory == 0)
|
|
|
|
|
sprintf (p, fmt , program);
|
|
|
|
|
else
|
|
|
|
|
sprintf (p, fmt, program, starting_directory);
|
|
|
|
|
else if (starting_directory == 0)
|
|
|
|
|
sprintf (p, fmt, program, makelevel);
|
|
|
|
|
else
|
|
|
|
|
sprintf (p, fmt, program, makelevel, starting_directory);
|
|
|
|
|
|
2013-09-30 09:57:22 +08:00
|
|
|
|
_outputs (NULL, 0, buf);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2013-09-14 13:04:04 +08:00
|
|
|
|
|
|
|
|
|
/* Set a file descriptor to be in O_APPEND mode.
|
|
|
|
|
If it fails, just ignore it. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
set_append_mode (int fd)
|
|
|
|
|
{
|
|
|
|
|
#if defined(F_GETFL) && defined(F_SETFL) && defined(O_APPEND)
|
|
|
|
|
int flags = fcntl (fd, F_GETFL, 0);
|
|
|
|
|
if (flags >= 0)
|
2016-09-26 07:06:56 +08:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
EINTRLOOP(r, fcntl (fd, F_SETFL, flags | O_APPEND));
|
|
|
|
|
}
|
2013-09-14 13:04:04 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
|
2013-09-22 05:37:59 +08:00
|
|
|
|
#ifndef NO_OUTPUT_SYNC
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
/* Semaphore for use in -j mode with output_sync. */
|
|
|
|
|
static sync_handle_t sync_handle = -1;
|
|
|
|
|
|
2013-09-15 08:40:30 +08:00
|
|
|
|
#define FD_NOT_EMPTY(_f) ((_f) != OUTPUT_NONE && lseek ((_f), 0, SEEK_END) > 0)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
/* Set up the sync handle. Disables output_sync on error. */
|
|
|
|
|
static int
|
2016-05-02 03:52:58 +08:00
|
|
|
|
sync_init (void)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
2013-09-15 09:01:10 +08:00
|
|
|
|
int combined_output = 0;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
if ((!STREAM_OK (stdout) && !STREAM_OK (stderr))
|
|
|
|
|
|| (sync_handle = create_mutex ()) == -1)
|
|
|
|
|
{
|
|
|
|
|
perror_with_name ("output-sync suppressed: ", "stderr");
|
|
|
|
|
output_sync = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
combined_output = same_stream (stdout, stderr);
|
|
|
|
|
prepare_mutex_handle_string (sync_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
if (STREAM_OK (stdout))
|
|
|
|
|
{
|
|
|
|
|
struct stat stbuf_o, stbuf_e;
|
|
|
|
|
|
|
|
|
|
sync_handle = fileno (stdout);
|
|
|
|
|
combined_output = (fstat (fileno (stdout), &stbuf_o) == 0
|
|
|
|
|
&& fstat (fileno (stderr), &stbuf_e) == 0
|
|
|
|
|
&& stbuf_o.st_dev == stbuf_e.st_dev
|
|
|
|
|
&& stbuf_o.st_ino == stbuf_e.st_ino);
|
|
|
|
|
}
|
|
|
|
|
else if (STREAM_OK (stderr))
|
|
|
|
|
sync_handle = fileno (stderr);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
perror_with_name ("output-sync suppressed: ", "stderr");
|
|
|
|
|
output_sync = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return combined_output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Support routine for output_sync() */
|
|
|
|
|
static void
|
|
|
|
|
pump_from_tmp (int from, FILE *to)
|
|
|
|
|
{
|
|
|
|
|
static char buffer[8192];
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
int prev_mode;
|
|
|
|
|
|
|
|
|
|
/* "from" is opened by open_tmpfd, which does it in binary mode, so
|
|
|
|
|
we need the mode of "to" to match that. */
|
|
|
|
|
prev_mode = _setmode (fileno (to), _O_BINARY);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (lseek (from, 0, SEEK_SET) == -1)
|
|
|
|
|
perror ("lseek()");
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
EINTRLOOP (len, read (from, buffer, sizeof (buffer)));
|
|
|
|
|
if (len < 0)
|
|
|
|
|
perror ("read()");
|
|
|
|
|
if (len <= 0)
|
|
|
|
|
break;
|
|
|
|
|
if (fwrite (buffer, len, 1, to) < 1)
|
2014-05-16 02:40:27 +08:00
|
|
|
|
{
|
|
|
|
|
perror ("fwrite()");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fflush (to);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
/* Switch "to" back to its original mode, so that log messages by
|
|
|
|
|
Make have the same EOL format as without --output-sync. */
|
|
|
|
|
_setmode (fileno (to), prev_mode);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Obtain the lock for writing output. */
|
|
|
|
|
static void *
|
|
|
|
|
acquire_semaphore (void)
|
|
|
|
|
{
|
|
|
|
|
static struct flock fl;
|
|
|
|
|
|
|
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
|
fl.l_start = 0;
|
|
|
|
|
fl.l_len = 1;
|
|
|
|
|
if (fcntl (sync_handle, F_SETLKW, &fl) != -1)
|
|
|
|
|
return &fl;
|
|
|
|
|
perror ("fcntl()");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Release the lock for writing output. */
|
|
|
|
|
static void
|
|
|
|
|
release_semaphore (void *sem)
|
|
|
|
|
{
|
|
|
|
|
struct flock *flp = (struct flock *)sem;
|
|
|
|
|
flp->l_type = F_UNLCK;
|
|
|
|
|
if (fcntl (sync_handle, F_SETLKW, flp) == -1)
|
|
|
|
|
perror ("fcntl()");
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
/* Returns a file descriptor to a temporary file. The file is automatically
|
|
|
|
|
closed/deleted on exit. Don't use a FILE* stream. */
|
|
|
|
|
int
|
2016-05-02 03:52:58 +08:00
|
|
|
|
output_tmpfd (void)
|
2013-09-14 13:04:04 +08:00
|
|
|
|
{
|
2016-09-26 07:06:56 +08:00
|
|
|
|
MODE_T mask = UMASK (0077);
|
2013-09-14 13:04:04 +08:00
|
|
|
|
int fd = -1;
|
|
|
|
|
FILE *tfile = tmpfile ();
|
|
|
|
|
|
|
|
|
|
if (! tfile)
|
|
|
|
|
pfatal_with_name ("tmpfile");
|
|
|
|
|
|
|
|
|
|
/* Create a duplicate so we can close the stream. */
|
|
|
|
|
fd = dup (fileno (tfile));
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
pfatal_with_name ("dup");
|
|
|
|
|
|
|
|
|
|
fclose (tfile);
|
|
|
|
|
|
|
|
|
|
set_append_mode (fd);
|
|
|
|
|
|
2016-09-26 07:06:56 +08:00
|
|
|
|
UMASK (mask);
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Adds file descriptors to the child structure to support output_sync; one
|
|
|
|
|
for stdout and one for stderr as long as they are open. If stdout and
|
|
|
|
|
stderr share a device they can share a temp file too.
|
|
|
|
|
Will reset output_sync on error. */
|
|
|
|
|
static void
|
|
|
|
|
setup_tmpfile (struct output *out)
|
|
|
|
|
{
|
|
|
|
|
/* Is make's stdout going to the same place as stderr? */
|
|
|
|
|
static int combined_output = -1;
|
|
|
|
|
|
|
|
|
|
if (combined_output < 0)
|
|
|
|
|
combined_output = sync_init ();
|
|
|
|
|
|
|
|
|
|
if (STREAM_OK (stdout))
|
|
|
|
|
{
|
|
|
|
|
int fd = output_tmpfd ();
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
goto error;
|
|
|
|
|
CLOSE_ON_EXEC (fd);
|
|
|
|
|
out->out = fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (STREAM_OK (stderr))
|
|
|
|
|
{
|
|
|
|
|
if (out->out != OUTPUT_NONE && combined_output)
|
|
|
|
|
out->err = out->out;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int fd = output_tmpfd ();
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
goto error;
|
|
|
|
|
CLOSE_ON_EXEC (fd);
|
|
|
|
|
out->err = fd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* If we failed to create a temp file, disable output sync going forward. */
|
|
|
|
|
error:
|
|
|
|
|
output_close (out);
|
2016-04-04 13:13:43 +08:00
|
|
|
|
output_sync = OUTPUT_SYNC_NONE;
|
2013-09-14 13:04:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-12 16:07:52 +08:00
|
|
|
|
/* Synchronize the output of jobs in -j mode to keep the results of
|
|
|
|
|
each job together. This is done by holding the results in temp files,
|
|
|
|
|
one for stdout and potentially another for stderr, and only releasing
|
|
|
|
|
them to "real" stdout/stderr when a semaphore can be obtained. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
output_dump (struct output *out)
|
|
|
|
|
{
|
|
|
|
|
int outfd_not_empty = FD_NOT_EMPTY (out->out);
|
|
|
|
|
int errfd_not_empty = FD_NOT_EMPTY (out->err);
|
|
|
|
|
|
|
|
|
|
if (outfd_not_empty || errfd_not_empty)
|
|
|
|
|
{
|
2013-09-14 13:04:04 +08:00
|
|
|
|
int traced = 0;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
/* Try to acquire the semaphore. If it fails, dump the output
|
2013-09-19 13:15:22 +08:00
|
|
|
|
unsynchronized; still better than silently discarding it.
|
|
|
|
|
We want to keep this lock for as little time as possible. */
|
2013-09-12 16:07:52 +08:00
|
|
|
|
void *sem = acquire_semaphore ();
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
/* Log the working directory for this dump. */
|
|
|
|
|
if (print_directory_flag && output_sync != OUTPUT_SYNC_RECURSE)
|
2013-09-30 09:57:22 +08:00
|
|
|
|
traced = log_working_directory (1);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
if (outfd_not_empty)
|
|
|
|
|
pump_from_tmp (out->out, stdout);
|
|
|
|
|
if (errfd_not_empty && out->err != out->out)
|
|
|
|
|
pump_from_tmp (out->err, stderr);
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
if (traced)
|
2013-09-30 09:57:22 +08:00
|
|
|
|
log_working_directory (0);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
/* Exit the critical section. */
|
|
|
|
|
if (sem)
|
|
|
|
|
release_semaphore (sem);
|
|
|
|
|
|
|
|
|
|
/* Truncate and reset the output, in case we use it again. */
|
|
|
|
|
if (out->out != OUTPUT_NONE)
|
|
|
|
|
{
|
|
|
|
|
int e;
|
|
|
|
|
lseek (out->out, 0, SEEK_SET);
|
|
|
|
|
EINTRLOOP (e, ftruncate (out->out, 0));
|
|
|
|
|
}
|
|
|
|
|
if (out->err != OUTPUT_NONE && out->err != out->out)
|
|
|
|
|
{
|
|
|
|
|
int e;
|
|
|
|
|
lseek (out->err, 0, SEEK_SET);
|
|
|
|
|
EINTRLOOP (e, ftruncate (out->err, 0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-22 05:37:59 +08:00
|
|
|
|
#endif /* NO_OUTPUT_SYNC */
|
2013-09-14 13:04:04 +08:00
|
|
|
|
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
/* Provide support for temporary files. */
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
#ifndef HAVE_STDLIB_H
|
|
|
|
|
# ifdef HAVE_MKSTEMP
|
|
|
|
|
int mkstemp (char *template);
|
|
|
|
|
# else
|
|
|
|
|
char *mktemp (char *template);
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
FILE *
|
|
|
|
|
output_tmpfile (char **name, const char *template)
|
|
|
|
|
{
|
2016-09-26 07:06:56 +08:00
|
|
|
|
FILE *file;
|
2013-09-14 13:04:04 +08:00
|
|
|
|
#ifdef HAVE_FDOPEN
|
|
|
|
|
int fd;
|
|
|
|
|
#endif
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2016-09-26 07:06:56 +08:00
|
|
|
|
/* Preserve the current umask, and set a restrictive one for temp files. */
|
|
|
|
|
MODE_T mask = UMASK (0077);
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_MKSTEMP) || defined(HAVE_MKTEMP)
|
2013-09-14 13:04:04 +08:00
|
|
|
|
# define TEMPLATE_LEN strlen (template)
|
|
|
|
|
#else
|
|
|
|
|
# define TEMPLATE_LEN L_tmpnam
|
|
|
|
|
#endif
|
|
|
|
|
*name = xmalloc (TEMPLATE_LEN + 1);
|
|
|
|
|
strcpy (*name, template);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2016-09-26 07:06:56 +08:00
|
|
|
|
#if defined(HAVE_MKSTEMP) && defined(HAVE_FDOPEN)
|
2013-09-14 13:04:04 +08:00
|
|
|
|
/* It's safest to use mkstemp(), if we can. */
|
2016-09-26 07:06:56 +08:00
|
|
|
|
EINTRLOOP (fd, mkstemp (*name));
|
2013-09-14 13:04:04 +08:00
|
|
|
|
if (fd == -1)
|
2016-09-26 07:06:56 +08:00
|
|
|
|
file = NULL;
|
|
|
|
|
else
|
|
|
|
|
file = fdopen (fd, "w");
|
2013-09-14 13:04:04 +08:00
|
|
|
|
#else
|
|
|
|
|
# ifdef HAVE_MKTEMP
|
|
|
|
|
(void) mktemp (*name);
|
|
|
|
|
# else
|
|
|
|
|
(void) tmpnam (*name);
|
|
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
# ifdef HAVE_FDOPEN
|
|
|
|
|
/* Can't use mkstemp(), but guard against a race condition. */
|
2016-03-06 04:21:59 +08:00
|
|
|
|
EINTRLOOP (fd, open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600));
|
2013-09-14 13:04:04 +08:00
|
|
|
|
if (fd == -1)
|
|
|
|
|
return 0;
|
2016-09-26 07:06:56 +08:00
|
|
|
|
file = fdopen (fd, "w");
|
2013-09-14 13:04:04 +08:00
|
|
|
|
# else
|
|
|
|
|
/* Not secure, but what can we do? */
|
2016-09-26 07:06:56 +08:00
|
|
|
|
file = fopen (*name, "w");
|
2013-09-14 13:04:04 +08:00
|
|
|
|
# endif
|
|
|
|
|
#endif
|
2016-09-26 07:06:56 +08:00
|
|
|
|
|
|
|
|
|
UMASK (mask);
|
|
|
|
|
|
|
|
|
|
return file;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-15 08:40:30 +08:00
|
|
|
|
/* This code is stolen from gnulib.
|
|
|
|
|
If/when we abandon the requirement to work with K&R compilers, we can
|
|
|
|
|
remove this (and perhaps other parts of GNU make!) and migrate to using
|
|
|
|
|
gnulib directly.
|
|
|
|
|
|
|
|
|
|
This is called only through atexit(), which means die() has already been
|
|
|
|
|
invoked. So, call exit() here directly. Apparently that works...?
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Close standard output, exiting with status 'exit_failure' on failure.
|
|
|
|
|
If a program writes *anything* to stdout, that program should close
|
|
|
|
|
stdout and make sure that it succeeds before exiting. Otherwise,
|
|
|
|
|
suppose that you go to the extreme of checking the return status
|
|
|
|
|
of every function that does an explicit write to stdout. The last
|
|
|
|
|
printf can succeed in writing to the internal stream buffer, and yet
|
|
|
|
|
the fclose(stdout) could still fail (due e.g., to a disk full error)
|
|
|
|
|
when it tries to write out that buffered data. Thus, you would be
|
|
|
|
|
left with an incomplete output file and the offending program would
|
|
|
|
|
exit successfully. Even calling fflush is not always sufficient,
|
|
|
|
|
since some file systems (NFS and CODA) buffer written/flushed data
|
|
|
|
|
until an actual close call.
|
|
|
|
|
|
|
|
|
|
Besides, it's wasteful to check the return value from every call
|
|
|
|
|
that writes to stdout -- just let the internal stream state record
|
|
|
|
|
the failure. That's what the ferror test is checking below.
|
|
|
|
|
|
|
|
|
|
It's important to detect such failures and exit nonzero because many
|
|
|
|
|
tools (most notably 'make' and other build-management systems) depend
|
|
|
|
|
on being able to detect failure in other tools via their exit status. */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
close_stdout (void)
|
|
|
|
|
{
|
|
|
|
|
int prev_fail = ferror (stdout);
|
|
|
|
|
int fclose_fail = fclose (stdout);
|
|
|
|
|
|
|
|
|
|
if (prev_fail || fclose_fail)
|
|
|
|
|
{
|
|
|
|
|
if (fclose_fail)
|
2013-11-24 11:23:52 +08:00
|
|
|
|
perror_with_name (_("write error: stdout"), "");
|
2013-09-15 08:40:30 +08:00
|
|
|
|
else
|
2013-11-24 11:23:52 +08:00
|
|
|
|
O (error, NILF, _("write error: stdout"));
|
2014-08-25 04:06:15 +08:00
|
|
|
|
exit (MAKE_TROUBLE);
|
2013-09-15 08:40:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-12 16:07:52 +08:00
|
|
|
|
void
|
2013-09-14 13:04:04 +08:00
|
|
|
|
output_init (struct output *out)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
2013-09-14 13:04:04 +08:00
|
|
|
|
if (out)
|
|
|
|
|
{
|
|
|
|
|
out->out = out->err = OUTPUT_NONE;
|
|
|
|
|
out->syncout = !!output_sync;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Configure this instance of make. Be sure stdout is line-buffered. */
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_SETVBUF
|
|
|
|
|
# ifdef SETVBUF_REVERSED
|
|
|
|
|
setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
|
|
|
|
|
# else /* setvbuf not reversed. */
|
|
|
|
|
/* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
|
|
|
|
|
setvbuf (stdout, 0, _IOLBF, BUFSIZ);
|
|
|
|
|
# endif /* setvbuf reversed. */
|
|
|
|
|
#elif HAVE_SETLINEBUF
|
|
|
|
|
setlinebuf (stdout);
|
|
|
|
|
#endif /* setlinebuf missing. */
|
|
|
|
|
|
|
|
|
|
/* Force stdout/stderr into append mode. This ensures parallel jobs won't
|
|
|
|
|
lose output due to overlapping writes. */
|
|
|
|
|
set_append_mode (fileno (stdout));
|
|
|
|
|
set_append_mode (fileno (stderr));
|
2013-09-15 08:40:30 +08:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_ATEXIT
|
|
|
|
|
if (STREAM_OK (stdout))
|
|
|
|
|
atexit (close_stdout);
|
|
|
|
|
#endif
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
output_close (struct output *out)
|
|
|
|
|
{
|
|
|
|
|
if (! out)
|
|
|
|
|
{
|
|
|
|
|
if (stdio_traced)
|
2013-09-30 09:57:22 +08:00
|
|
|
|
log_working_directory (0);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-22 05:37:59 +08:00
|
|
|
|
#ifndef NO_OUTPUT_SYNC
|
2013-09-12 16:07:52 +08:00
|
|
|
|
output_dump (out);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (out->out >= 0)
|
|
|
|
|
close (out->out);
|
|
|
|
|
if (out->err >= 0 && out->err != out->out)
|
|
|
|
|
close (out->err);
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
output_init (out);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
/* We're about to generate output: be sure it's set up. */
|
2013-09-12 16:07:52 +08:00
|
|
|
|
void
|
2016-05-02 03:52:58 +08:00
|
|
|
|
output_start (void)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
2013-09-22 05:37:59 +08:00
|
|
|
|
#ifndef NO_OUTPUT_SYNC
|
2013-09-30 09:57:22 +08:00
|
|
|
|
/* If we're syncing output make sure the temporary file is set up. */
|
|
|
|
|
if (output_context && output_context->syncout)
|
|
|
|
|
if (! OUTPUT_ISSET(output_context))
|
|
|
|
|
setup_tmpfile (output_context);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
#endif
|
2013-09-14 13:04:04 +08:00
|
|
|
|
|
2013-09-30 09:57:22 +08:00
|
|
|
|
/* If we're not syncing this output per-line or per-target, make sure we emit
|
|
|
|
|
the "Entering..." message where appropriate. */
|
|
|
|
|
if (output_sync == OUTPUT_SYNC_NONE || output_sync == OUTPUT_SYNC_RECURSE)
|
|
|
|
|
if (! stdio_traced && print_directory_flag)
|
|
|
|
|
stdio_traced = log_working_directory (1);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
outputs (int is_err, const char *msg)
|
|
|
|
|
{
|
|
|
|
|
if (! msg || *msg == '\0')
|
|
|
|
|
return;
|
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
output_start ();
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-09-14 13:04:04 +08:00
|
|
|
|
_outputs (output_context, is_err, msg);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct fmtstring
|
|
|
|
|
{
|
|
|
|
|
char *buffer;
|
2013-11-24 11:23:52 +08:00
|
|
|
|
size_t size;
|
|
|
|
|
} fmtbuf = { NULL, 0 };
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
static char *
|
|
|
|
|
get_buffer (size_t need)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
2014-02-02 23:51:05 +08:00
|
|
|
|
/* Make sure we have room. NEED includes space for \0. */
|
2013-11-24 11:23:52 +08:00
|
|
|
|
if (need > fmtbuf.size)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
2013-11-24 11:23:52 +08:00
|
|
|
|
fmtbuf.size += need * 2;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
fmtbuf.buffer = xrealloc (fmtbuf.buffer, fmtbuf.size);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 23:51:05 +08:00
|
|
|
|
fmtbuf.buffer[need-1] = '\0';
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
return fmtbuf.buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print a message on stdout. */
|
|
|
|
|
|
|
|
|
|
void
|
2013-11-24 11:23:52 +08:00
|
|
|
|
message (int prefix, size_t len, const char *fmt, ...)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
|
|
|
|
va_list args;
|
2013-11-24 11:23:52 +08:00
|
|
|
|
char *p;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
len += strlen (fmt) + strlen (program) + INTSTR_LENGTH + 4 + 1 + 1;
|
|
|
|
|
p = get_buffer (len);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
|
{
|
|
|
|
|
if (makelevel == 0)
|
2013-11-24 11:23:52 +08:00
|
|
|
|
sprintf (p, "%s: ", program);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
else
|
2013-11-24 11:23:52 +08:00
|
|
|
|
sprintf (p, "%s[%u]: ", program, makelevel);
|
|
|
|
|
p += strlen (p);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2013-11-24 11:23:52 +08:00
|
|
|
|
vsprintf (p, fmt, args);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
va_end (args);
|
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
strcat (p, "\n");
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2014-02-02 23:51:05 +08:00
|
|
|
|
assert (fmtbuf.buffer[len-1] == '\0');
|
2013-09-12 16:07:52 +08:00
|
|
|
|
outputs (0, fmtbuf.buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print an error message. */
|
|
|
|
|
|
|
|
|
|
void
|
2016-05-02 07:24:20 +08:00
|
|
|
|
error (const floc *flocp, size_t len, const char *fmt, ...)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
|
|
|
|
va_list args;
|
2013-11-24 11:23:52 +08:00
|
|
|
|
char *p;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
len += (strlen (fmt) + strlen (program)
|
|
|
|
|
+ (flocp && flocp->filenm ? strlen (flocp->filenm) : 0)
|
|
|
|
|
+ INTSTR_LENGTH + 4 + 1 + 1);
|
|
|
|
|
p = get_buffer (len);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
if (flocp && flocp->filenm)
|
2016-04-11 05:12:48 +08:00
|
|
|
|
sprintf (p, "%s:%lu: ", flocp->filenm, flocp->lineno + flocp->offset);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
else if (makelevel == 0)
|
2013-11-24 11:23:52 +08:00
|
|
|
|
sprintf (p, "%s: ", program);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
else
|
2013-11-24 11:23:52 +08:00
|
|
|
|
sprintf (p, "%s[%u]: ", program, makelevel);
|
|
|
|
|
p += strlen (p);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2013-11-24 11:23:52 +08:00
|
|
|
|
vsprintf (p, fmt, args);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
va_end (args);
|
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
strcat (p, "\n");
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2014-02-02 23:51:05 +08:00
|
|
|
|
assert (fmtbuf.buffer[len-1] == '\0');
|
2013-09-12 16:07:52 +08:00
|
|
|
|
outputs (1, fmtbuf.buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print an error message and exit. */
|
|
|
|
|
|
|
|
|
|
void
|
2016-05-02 07:24:20 +08:00
|
|
|
|
fatal (const floc *flocp, size_t len, const char *fmt, ...)
|
2013-09-12 16:07:52 +08:00
|
|
|
|
{
|
|
|
|
|
va_list args;
|
2013-11-24 11:23:52 +08:00
|
|
|
|
const char *stop = _(". Stop.\n");
|
|
|
|
|
char *p;
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
len += (strlen (fmt) + strlen (program)
|
|
|
|
|
+ (flocp && flocp->filenm ? strlen (flocp->filenm) : 0)
|
|
|
|
|
+ INTSTR_LENGTH + 8 + strlen (stop) + 1);
|
|
|
|
|
p = get_buffer (len);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
if (flocp && flocp->filenm)
|
2016-04-11 05:12:48 +08:00
|
|
|
|
sprintf (p, "%s:%lu: *** ", flocp->filenm, flocp->lineno + flocp->offset);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
else if (makelevel == 0)
|
2013-11-24 11:23:52 +08:00
|
|
|
|
sprintf (p, "%s: *** ", program);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
else
|
2013-11-24 11:23:52 +08:00
|
|
|
|
sprintf (p, "%s[%u]: *** ", program, makelevel);
|
|
|
|
|
p += strlen (p);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2013-11-24 11:23:52 +08:00
|
|
|
|
vsprintf (p, fmt, args);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
va_end (args);
|
|
|
|
|
|
2013-11-24 11:23:52 +08:00
|
|
|
|
strcat (p, stop);
|
|
|
|
|
|
2014-02-02 23:51:05 +08:00
|
|
|
|
assert (fmtbuf.buffer[len-1] == '\0');
|
2013-09-12 16:07:52 +08:00
|
|
|
|
outputs (1, fmtbuf.buffer);
|
|
|
|
|
|
2014-08-25 04:06:15 +08:00
|
|
|
|
die (MAKE_FAILURE);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print an error message from errno. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
perror_with_name (const char *str, const char *name)
|
|
|
|
|
{
|
2013-11-24 11:23:52 +08:00
|
|
|
|
const char *err = strerror (errno);
|
|
|
|
|
OSSS (error, NILF, _("%s%s: %s"), str, name, err);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print an error message from errno and exit. */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
pfatal_with_name (const char *name)
|
|
|
|
|
{
|
2013-11-24 11:23:52 +08:00
|
|
|
|
const char *err = strerror (errno);
|
|
|
|
|
OSS (fatal, NILF, _("%s: %s"), name, err);
|
2013-09-12 16:07:52 +08:00
|
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
}
|