[53] | 1 | /* Command processing for GNU Make.
|
---|
[3138] | 2 | Copyright (C) 1988-2016 Free Software Foundation, Inc.
|
---|
[53] | 3 | This file is part of GNU Make.
|
---|
| 4 |
|
---|
[501] | 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
---|
[1989] | 7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 8 | version.
|
---|
[53] | 9 |
|
---|
[501] | 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
[53] | 13 |
|
---|
[501] | 14 | You should have received a copy of the GNU General Public License along with
|
---|
[1989] | 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
[53] | 16 |
|
---|
[3138] | 17 | #include "makeint.h"
|
---|
| 18 | #include "filedef.h"
|
---|
[53] | 19 | #include "dep.h"
|
---|
| 20 | #include "variable.h"
|
---|
| 21 | #include "job.h"
|
---|
| 22 | #include "commands.h"
|
---|
[501] | 23 | #ifdef WINDOWS32
|
---|
| 24 | #include <windows.h>
|
---|
| 25 | #include "w32err.h"
|
---|
| 26 | #endif
|
---|
[53] | 27 |
|
---|
| 28 | #if VMS
|
---|
[3138] | 29 | # define FILE_LIST_SEPARATOR (vms_comma_separator ? ',' : ' ')
|
---|
[53] | 30 | #else
|
---|
| 31 | # define FILE_LIST_SEPARATOR ' '
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
[3138] | 34 | #ifndef HAVE_UNISTD_H
|
---|
[900] | 35 | int getpid ();
|
---|
[53] | 36 | #endif
|
---|
| 37 | |
---|
[2596] | 38 |
|
---|
| 39 |
|
---|
| 40 | static unsigned long
|
---|
| 41 | dep_hash_1 (const void *key)
|
---|
| 42 | {
|
---|
| 43 | const struct dep *d = key;
|
---|
| 44 | return_STRING_HASH_1 (dep_name (d));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | static unsigned long
|
---|
| 48 | dep_hash_2 (const void *key)
|
---|
| 49 | {
|
---|
| 50 | const struct dep *d = key;
|
---|
| 51 | return_STRING_HASH_2 (dep_name (d));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | static int
|
---|
| 55 | dep_hash_cmp (const void *x, const void *y)
|
---|
| 56 | {
|
---|
| 57 | const struct dep *dx = x;
|
---|
| 58 | const struct dep *dy = y;
|
---|
| 59 | return strcmp (dep_name (dx), dep_name (dy));
|
---|
| 60 | }
|
---|
[53] | 61 |
|
---|
| 62 | /* Set FILE's automatic variables up. */
|
---|
[280] | 63 |
|
---|
[53] | 64 | void
|
---|
| 65 | set_file_variables (struct file *file)
|
---|
[2596] | 66 | {
|
---|
[900] | 67 | struct dep *d;
|
---|
[53] | 68 | const char *at, *percent, *star, *less;
|
---|
[3138] | 69 |
|
---|
| 70 | #ifndef NO_ARCHIVES
|
---|
| 71 | /* If the target is an archive member 'lib(member)',
|
---|
[53] | 72 | then $@ is 'lib' and $% is 'member'. */
|
---|
| 73 |
|
---|
| 74 | if (ar_name (file->name))
|
---|
| 75 | {
|
---|
[900] | 76 | unsigned int len;
|
---|
[53] | 77 | const char *cp;
|
---|
| 78 | char *p;
|
---|
[900] | 79 |
|
---|
| 80 | cp = strchr (file->name, '(');
|
---|
| 81 | p = alloca (cp - file->name + 1);
|
---|
| 82 | memcpy (p, file->name, cp - file->name);
|
---|
| 83 | p[cp - file->name] = '\0';
|
---|
| 84 | at = p;
|
---|
| 85 | len = strlen (cp + 1);
|
---|
| 86 | p = alloca (len);
|
---|
| 87 | memcpy (p, cp + 1, len - 1);
|
---|
| 88 | p[len - 1] = '\0';
|
---|
[53] | 89 | percent = p;
|
---|
| 90 | }
|
---|
[3138] | 91 | else
|
---|
[53] | 92 | #endif /* NO_ARCHIVES. */
|
---|
| 93 | {
|
---|
| 94 | at = file->name;
|
---|
| 95 | percent = "";
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /* $* is the stem from an implicit or static pattern rule. */
|
---|
| 99 | if (file->stem == 0)
|
---|
| 100 | {
|
---|
[3138] | 101 | /* In Unix make, $* is set to the target name with
|
---|
| 102 | any suffix in the .SUFFIXES list stripped off for
|
---|
[900] | 103 | explicit rules. We store this in the 'stem' member. */
|
---|
[53] | 104 | const char *name;
|
---|
| 105 | unsigned int len;
|
---|
[3138] | 106 |
|
---|
[53] | 107 | #ifndef NO_ARCHIVES
|
---|
[3138] | 108 | if (ar_name (file->name))
|
---|
| 109 | {
|
---|
| 110 | name = strchr (file->name, '(') + 1;
|
---|
| 111 | len = strlen (name) - 1;
|
---|
[53] | 112 | }
|
---|
| 113 | else
|
---|
[3138] | 114 | #endif
|
---|
| 115 | {
|
---|
| 116 | name = file->name;
|
---|
| 117 | len = strlen (name);
|
---|
[53] | 118 | }
|
---|
[900] | 119 |
|
---|
[3138] | 120 | for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
|
---|
| 121 | {
|
---|
| 122 | unsigned int slen = strlen (dep_name (d));
|
---|
| 123 | if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
|
---|
| 124 | {
|
---|
| 125 | file->stem = strcache_add_len (name, len - slen);
|
---|
| 126 | break;
|
---|
| 127 | }
|
---|
[53] | 128 | }
|
---|
[3138] | 129 | if (d == 0)
|
---|
[53] | 130 | file->stem = "";
|
---|
| 131 | }
|
---|
| 132 | star = file->stem;
|
---|
[280] | 133 |
|
---|
| 134 | /* $< is the first not order-only dependency. */
|
---|
| 135 | less = "";
|
---|
| 136 | for (d = file->deps; d != 0; d = d->next)
|
---|
| 137 | if (!d->ignore_mtime)
|
---|
[2596] | 138 | {
|
---|
| 139 | if (!d->need_2nd_expansion)
|
---|
[280] | 140 | less = dep_name (d);
|
---|
| 141 | break;
|
---|
[53] | 142 | }
|
---|
| 143 |
|
---|
| 144 | if (file->cmds == default_file->cmds)
|
---|
| 145 | /* This file got its commands from .DEFAULT.
|
---|
| 146 | In this case $< is the same as $@. */
|
---|
| 147 | less = at;
|
---|
[3138] | 148 |
|
---|
[53] | 149 | #define DEFINE_VARIABLE(name, len, value) \
|
---|
| 150 | (void) define_variable_for_file (name,len,value,o_automatic,0,file)
|
---|
| 151 |
|
---|
| 152 | /* Define the variables. */
|
---|
| 153 |
|
---|
| 154 | DEFINE_VARIABLE ("<", 1, less);
|
---|
| 155 | DEFINE_VARIABLE ("*", 1, star);
|
---|
| 156 | DEFINE_VARIABLE ("@", 1, at);
|
---|
| 157 | DEFINE_VARIABLE ("%", 1, percent);
|
---|
| 158 |
|
---|
| 159 | /* Compute the values for $^, $+, $?, and $|. */
|
---|
| 160 |
|
---|
| 161 | {
|
---|
[900] | 162 | static char *plus_value=0, *bar_value=0, *qmark_value=0;
|
---|
[53] | 163 | static unsigned int plus_max=0, bar_max=0, qmark_max=0;
|
---|
| 164 |
|
---|
| 165 | unsigned int qmark_len, plus_len, bar_len;
|
---|
| 166 | char *cp;
|
---|
| 167 | char *caret_value;
|
---|
| 168 | char *qp;
|
---|
| 169 | char *bp;
|
---|
| 170 | unsigned int len;
|
---|
[2596] | 171 |
|
---|
| 172 | struct hash_table dep_hash;
|
---|
| 173 | void **slot;
|
---|
[53] | 174 |
|
---|
| 175 | /* Compute first the value for $+, which is supposed to contain
|
---|
| 176 | duplicate dependencies as they were listed in the makefile. */
|
---|
| 177 |
|
---|
[2596] | 178 | plus_len = 0;
|
---|
[53] | 179 | bar_len = 0;
|
---|
[2596] | 180 | for (d = file->deps; d != 0; d = d->next)
|
---|
| 181 | {
|
---|
| 182 | if (!d->need_2nd_expansion)
|
---|
| 183 | {
|
---|
| 184 | if (d->ignore_mtime)
|
---|
| 185 | bar_len += strlen (dep_name (d)) + 1;
|
---|
| 186 | else
|
---|
| 187 | plus_len += strlen (dep_name (d)) + 1;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (bar_len == 0)
|
---|
| 192 | bar_len++;
|
---|
[53] | 193 |
|
---|
| 194 | if (plus_len == 0)
|
---|
| 195 | plus_len++;
|
---|
| 196 |
|
---|
[501] | 197 | if (plus_len > plus_max)
|
---|
[2596] | 198 | plus_value = xrealloc (plus_value, plus_max = plus_len);
|
---|
[53] | 199 |
|
---|
| 200 | cp = plus_value;
|
---|
[3138] | 201 |
|
---|
[53] | 202 | qmark_len = plus_len + 1; /* Will be this or less. */
|
---|
[2596] | 203 | for (d = file->deps; d != 0; d = d->next)
|
---|
[53] | 204 | if (! d->ignore_mtime && ! d->need_2nd_expansion)
|
---|
[900] | 205 | {
|
---|
[53] | 206 | const char *c = dep_name (d);
|
---|
[3138] | 207 |
|
---|
[53] | 208 | #ifndef NO_ARCHIVES
|
---|
| 209 | if (ar_name (c))
|
---|
| 210 | {
|
---|
| 211 | c = strchr (c, '(') + 1;
|
---|
| 212 | len = strlen (c) - 1;
|
---|
| 213 | }
|
---|
| 214 | else
|
---|
| 215 | #endif
|
---|
| 216 | len = strlen (c);
|
---|
[900] | 217 |
|
---|
[53] | 218 | memcpy (cp, c, len);
|
---|
| 219 | cp += len;
|
---|
[2596] | 220 | *cp++ = FILE_LIST_SEPARATOR;
|
---|
[3138] | 221 | if (! (d->changed || always_make_flag))
|
---|
[53] | 222 | qmark_len -= len + 1; /* Don't space in $? for this one. */
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /* Kill the last space and define the variable. */
|
---|
| 226 |
|
---|
| 227 | cp[cp > plus_value ? -1 : 0] = '\0';
|
---|
| 228 | DEFINE_VARIABLE ("+", 1, plus_value);
|
---|
| 229 |
|
---|
| 230 | /* Compute the values for $^, $?, and $|. */
|
---|
| 231 |
|
---|
| 232 | cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
|
---|
| 233 |
|
---|
[501] | 234 | if (qmark_len > qmark_max)
|
---|
[53] | 235 | qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
|
---|
| 236 | qp = qmark_value;
|
---|
| 237 |
|
---|
[501] | 238 | if (bar_len > bar_max)
|
---|
[53] | 239 | bar_value = xrealloc (bar_value, bar_max = bar_len);
|
---|
| 240 | bp = bar_value;
|
---|
[2596] | 241 |
|
---|
| 242 | /* Make sure that no dependencies are repeated in $^, $?, and $|. It
|
---|
| 243 | would be natural to combine the next two loops but we can't do it
|
---|
| 244 | because of a situation where we have two dep entries, the first
|
---|
| 245 | is order-only and the second is normal (see below). */
|
---|
| 246 |
|
---|
| 247 | hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
|
---|
[53] | 248 |
|
---|
| 249 | for (d = file->deps; d != 0; d = d->next)
|
---|
[2596] | 250 | {
|
---|
| 251 | if (d->need_2nd_expansion)
|
---|
[53] | 252 | continue;
|
---|
[2596] | 253 |
|
---|
| 254 | slot = hash_find_slot (&dep_hash, d);
|
---|
| 255 | if (HASH_VACANT (*slot))
|
---|
| 256 | hash_insert_at (&dep_hash, d, slot);
|
---|
| 257 | else
|
---|
| 258 | {
|
---|
| 259 | /* Check if the two prerequisites have different ignore_mtime.
|
---|
| 260 | If so then we need to "upgrade" one that is order-only. */
|
---|
| 261 |
|
---|
| 262 | struct dep* hd = (struct dep*) *slot;
|
---|
| 263 |
|
---|
| 264 | if (d->ignore_mtime != hd->ignore_mtime)
|
---|
| 265 | d->ignore_mtime = hd->ignore_mtime = 0;
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | for (d = file->deps; d != 0; d = d->next)
|
---|
| 270 | {
|
---|
| 271 | const char *c;
|
---|
| 272 |
|
---|
| 273 | if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
|
---|
| 274 | continue;
|
---|
| 275 |
|
---|
[3138] | 276 | c = dep_name (d);
|
---|
[2596] | 277 | #ifndef NO_ARCHIVES
|
---|
[3138] | 278 | if (ar_name (c))
|
---|
| 279 | {
|
---|
| 280 | c = strchr (c, '(') + 1;
|
---|
| 281 | len = strlen (c) - 1;
|
---|
| 282 | }
|
---|
[53] | 283 | else
|
---|
[3138] | 284 | #endif
|
---|
[53] | 285 | len = strlen (c);
|
---|
| 286 |
|
---|
| 287 | if (d->ignore_mtime)
|
---|
[2596] | 288 | {
|
---|
[3138] | 289 | memcpy (bp, c, len);
|
---|
| 290 | bp += len;
|
---|
| 291 | *bp++ = FILE_LIST_SEPARATOR;
|
---|
| 292 | }
|
---|
[2596] | 293 | else
|
---|
[900] | 294 | {
|
---|
[53] | 295 | memcpy (cp, c, len);
|
---|
| 296 | cp += len;
|
---|
[2596] | 297 | *cp++ = FILE_LIST_SEPARATOR;
|
---|
[53] | 298 | if (d->changed || always_make_flag)
|
---|
[900] | 299 | {
|
---|
[53] | 300 | memcpy (qp, c, len);
|
---|
| 301 | qp += len;
|
---|
| 302 | *qp++ = FILE_LIST_SEPARATOR;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
[2596] | 306 |
|
---|
| 307 | hash_free (&dep_hash, 0);
|
---|
[53] | 308 |
|
---|
| 309 | /* Kill the last spaces and define the variables. */
|
---|
| 310 |
|
---|
| 311 | cp[cp > caret_value ? -1 : 0] = '\0';
|
---|
| 312 | DEFINE_VARIABLE ("^", 1, caret_value);
|
---|
| 313 |
|
---|
| 314 | qp[qp > qmark_value ? -1 : 0] = '\0';
|
---|
| 315 | DEFINE_VARIABLE ("?", 1, qmark_value);
|
---|
| 316 |
|
---|
| 317 | bp[bp > bar_value ? -1 : 0] = '\0';
|
---|
| 318 | DEFINE_VARIABLE ("|", 1, bar_value);
|
---|
| 319 | }
|
---|
[3138] | 320 |
|
---|
[53] | 321 | #undef DEFINE_VARIABLE
|
---|
| 322 | }
|
---|
| 323 | |
---|
[3138] | 324 |
|
---|
[53] | 325 | /* Chop CMDS up into individual command lines if necessary.
|
---|
| 326 | Also set the 'lines_flags' and 'any_recurse' members. */
|
---|
| 327 |
|
---|
| 328 | void
|
---|
| 329 | chop_commands (struct commands *cmds)
|
---|
| 330 | {
|
---|
| 331 | unsigned int nlines, idx;
|
---|
| 332 | char **lines;
|
---|
| 333 |
|
---|
| 334 | /* If we don't have any commands,
|
---|
| 335 | or we already parsed them, never mind. */
|
---|
| 336 |
|
---|
| 337 | if (!cmds || cmds->command_lines != 0)
|
---|
[2596] | 338 | return;
|
---|
[53] | 339 |
|
---|
[2596] | 340 | /* Chop CMDS->commands up into lines in CMDS->command_lines. */
|
---|
[53] | 341 |
|
---|
[2596] | 342 | if (one_shell)
|
---|
| 343 | {
|
---|
| 344 | int l = strlen (cmds->commands);
|
---|
| 345 |
|
---|
| 346 | nlines = 1;
|
---|
| 347 | lines = xmalloc (nlines * sizeof (char *));
|
---|
| 348 | lines[0] = xstrdup (cmds->commands);
|
---|
| 349 |
|
---|
| 350 | /* Strip the trailing newline. */
|
---|
| 351 | if (l > 0 && lines[0][l-1] == '\n')
|
---|
| 352 | lines[0][l-1] = '\0';
|
---|
| 353 | }
|
---|
| 354 | else
|
---|
| 355 | {
|
---|
| 356 | const char *p;
|
---|
| 357 |
|
---|
| 358 | nlines = 5;
|
---|
| 359 | lines = xmalloc (nlines * sizeof (char *));
|
---|
| 360 | idx = 0;
|
---|
[53] | 361 | p = cmds->commands;
|
---|
[2596] | 362 | while (*p != '\0')
|
---|
| 363 | {
|
---|
| 364 | const char *end = p;
|
---|
| 365 | find_end:;
|
---|
| 366 | end = strchr (end, '\n');
|
---|
| 367 | if (end == 0)
|
---|
[53] | 368 | end = p + strlen (p);
|
---|
[2596] | 369 | else if (end > p && end[-1] == '\\')
|
---|
| 370 | {
|
---|
| 371 | int backslash = 1;
|
---|
| 372 | const char *b;
|
---|
| 373 | for (b = end - 2; b >= p && *b == '\\'; --b)
|
---|
| 374 | backslash = !backslash;
|
---|
| 375 | if (backslash)
|
---|
| 376 | {
|
---|
| 377 | ++end;
|
---|
[53] | 378 | goto find_end;
|
---|
[2596] | 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | if (idx == nlines)
|
---|
| 383 | {
|
---|
| 384 | nlines += 2;
|
---|
| 385 | lines = xrealloc (lines, nlines * sizeof (char *));
|
---|
| 386 | }
|
---|
| 387 | lines[idx++] = xstrndup (p, end - p);
|
---|
| 388 | p = end;
|
---|
[53] | 389 | if (*p != '\0')
|
---|
| 390 | ++p;
|
---|
[2596] | 391 | }
|
---|
[53] | 392 |
|
---|
[2596] | 393 | if (idx != nlines)
|
---|
[900] | 394 | {
|
---|
[53] | 395 | nlines = idx;
|
---|
| 396 | lines = xrealloc (lines, nlines * sizeof (char *));
|
---|
| 397 | }
|
---|
[2596] | 398 | }
|
---|
| 399 |
|
---|
[53] | 400 | /* Finally, set the corresponding CMDS->lines_flags elements and the
|
---|
[3138] | 401 | CMDS->any_recurse flag. */
|
---|
| 402 |
|
---|
| 403 | if (nlines > USHRT_MAX)
|
---|
[53] | 404 | ON (fatal, &cmds->fileinfo, _("Recipe has too many lines (%ud)"), nlines);
|
---|
| 405 |
|
---|
| 406 | cmds->ncommand_lines = nlines;
|
---|
| 407 | cmds->command_lines = lines;
|
---|
[900] | 408 |
|
---|
[2596] | 409 | cmds->any_recurse = 0;
|
---|
[53] | 410 | cmds->lines_flags = xmalloc (nlines);
|
---|
| 411 |
|
---|
[3138] | 412 | for (idx = 0; idx < nlines; ++idx)
|
---|
[2596] | 413 | {
|
---|
[53] | 414 | unsigned char flags = 0;
|
---|
[3138] | 415 | const char *p = lines[idx];
|
---|
[2596] | 416 |
|
---|
[53] | 417 | while (ISBLANK (*p) || *p == '-' || *p == '@' || *p == '+')
|
---|
| 418 | switch (*(p++))
|
---|
| 419 | {
|
---|
| 420 | case '+':
|
---|
| 421 | flags |= COMMANDS_RECURSE;
|
---|
| 422 | break;
|
---|
| 423 | case '@':
|
---|
| 424 | flags |= COMMANDS_SILENT;
|
---|
| 425 | break;
|
---|
| 426 | case '-':
|
---|
| 427 | flags |= COMMANDS_NOERROR;
|
---|
| 428 | break;
|
---|
[280] | 429 | }
|
---|
| 430 |
|
---|
| 431 | /* If no explicit '+' was given, look for MAKE variable references. */
|
---|
| 432 | if (!(flags & COMMANDS_RECURSE)
|
---|
| 433 | && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
|
---|
[53] | 434 | flags |= COMMANDS_RECURSE;
|
---|
[3138] | 435 |
|
---|
[53] | 436 | cmds->lines_flags[idx] = flags;
|
---|
| 437 | cmds->any_recurse |= flags & COMMANDS_RECURSE ? 1 : 0;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | |
---|
| 441 |
|
---|
| 442 | /* Execute the commands to remake FILE. If they are currently executing,
|
---|
| 443 | return or have already finished executing, just return. Otherwise,
|
---|
| 444 | fork off a child process to run the first command line in the sequence. */
|
---|
| 445 |
|
---|
[900] | 446 | void
|
---|
[53] | 447 | execute_file_commands (struct file *file)
|
---|
| 448 | {
|
---|
| 449 | const char *p;
|
---|
| 450 |
|
---|
| 451 | /* Don't go through all the preparations if
|
---|
[3138] | 452 | the commands are nothing but whitespace. */
|
---|
[53] | 453 |
|
---|
| 454 | for (p = file->cmds->commands; *p != '\0'; ++p)
|
---|
| 455 | if (!ISSPACE (*p) && *p != '-' && *p != '@' && *p != '+')
|
---|
| 456 | break;
|
---|
| 457 | if (*p == '\0')
|
---|
[3138] | 458 | {
|
---|
[53] | 459 | /* If there are no commands, assume everything worked. */
|
---|
| 460 | set_command_state (file, cs_running);
|
---|
| 461 | file->update_status = us_success;
|
---|
| 462 | notice_finished_file (file);
|
---|
| 463 | return;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | /* First set the automatic variables according to this file. */
|
---|
| 467 |
|
---|
| 468 | initialize_file_variables (file, 0);
|
---|
[3138] | 469 |
|
---|
| 470 | set_file_variables (file);
|
---|
| 471 |
|
---|
| 472 | /* If this is a loaded dynamic object, unload it before remaking.
|
---|
| 473 | Some systems don't support overwriting a loaded object. */
|
---|
[53] | 474 | if (file->loaded)
|
---|
| 475 | unload_file (file->name);
|
---|
| 476 |
|
---|
| 477 | /* Start the commands running. */
|
---|
| 478 | new_job (file);
|
---|
| 479 | }
|
---|
| 480 | |
---|
| 481 |
|
---|
| 482 | /* This is set while we are inside fatal_error_signal,
|
---|
| 483 | so things can avoid nonreentrant operations. */
|
---|
| 484 |
|
---|
| 485 | int handling_fatal_signal = 0;
|
---|
| 486 |
|
---|
| 487 | /* Handle fatal signals. */
|
---|
| 488 |
|
---|
| 489 | RETSIGTYPE
|
---|
| 490 | fatal_error_signal (int sig)
|
---|
| 491 | {
|
---|
| 492 | #ifdef __MSDOS__
|
---|
| 493 | extern int dos_status, dos_command_running;
|
---|
| 494 |
|
---|
| 495 | if (dos_command_running)
|
---|
| 496 | {
|
---|
| 497 | /* That was the child who got the signal, not us. */
|
---|
| 498 | dos_status |= (sig << 8);
|
---|
| 499 | return;
|
---|
| 500 | }
|
---|
| 501 | remove_intermediates (1);
|
---|
| 502 | exit (EXIT_FAILURE);
|
---|
| 503 | #else /* not __MSDOS__ */
|
---|
| 504 | #ifdef _AMIGA
|
---|
| 505 | remove_intermediates (1);
|
---|
| 506 | if (sig == SIGINT)
|
---|
[501] | 507 | fputs (_("*** Break.\n"), stderr);
|
---|
| 508 |
|
---|
| 509 | exit (10);
|
---|
| 510 | #else /* not Amiga */
|
---|
| 511 | #ifdef WINDOWS32
|
---|
| 512 | extern HANDLE main_thread;
|
---|
| 513 |
|
---|
| 514 | /* Windows creates a sperate thread for handling Ctrl+C, so we need
|
---|
| 515 | to suspend the main thread, or else we will have race conditions
|
---|
| 516 | when both threads call reap_children. */
|
---|
| 517 | if (main_thread)
|
---|
[3138] | 518 | {
|
---|
[501] | 519 | DWORD susp_count = SuspendThread (main_thread);
|
---|
[3138] | 520 |
|
---|
| 521 | if (susp_count != 0)
|
---|
[501] | 522 | fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
|
---|
[3138] | 523 | else if (susp_count == (DWORD)-1)
|
---|
| 524 | {
|
---|
| 525 | DWORD ierr = GetLastError ();
|
---|
[501] | 526 |
|
---|
| 527 | fprintf (stderr, "SuspendThread: error %ld: %s\n",
|
---|
[53] | 528 | ierr, map_windows32_error_to_string (ierr));
|
---|
| 529 | }
|
---|
| 530 | }
|
---|
| 531 | #endif
|
---|
| 532 | handling_fatal_signal = 1;
|
---|
| 533 |
|
---|
| 534 | /* Set the handling for this signal to the default.
|
---|
| 535 | It is blocked now while we run this handler. */
|
---|
| 536 | signal (sig, SIG_DFL);
|
---|
| 537 |
|
---|
| 538 | /* A termination signal won't be sent to the entire
|
---|
[900] | 539 | process group, but it means we want to kill the children. */
|
---|
[53] | 540 |
|
---|
[3138] | 541 | if (sig == SIGTERM)
|
---|
| 542 | {
|
---|
[53] | 543 | struct child *c;
|
---|
| 544 | for (c = children; c != 0; c = c->next)
|
---|
| 545 | if (!c->remote)
|
---|
| 546 | (void) kill (c->pid, SIGTERM);
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | /* If we got a signal that means the user
|
---|
| 550 | wanted to kill make, remove pending targets. */
|
---|
| 551 |
|
---|
| 552 | if (sig == SIGTERM || sig == SIGINT
|
---|
| 553 | #ifdef SIGHUP
|
---|
| 554 | || sig == SIGHUP
|
---|
| 555 | #endif
|
---|
| 556 | #ifdef SIGQUIT
|
---|
[900] | 557 | || sig == SIGQUIT
|
---|
[53] | 558 | #endif
|
---|
| 559 | )
|
---|
[3138] | 560 | {
|
---|
[53] | 561 | struct child *c;
|
---|
[3138] | 562 |
|
---|
| 563 | /* Remote children won't automatically get signals sent
|
---|
[53] | 564 | to the process group, so we must send them. */
|
---|
| 565 | for (c = children; c != 0; c = c->next)
|
---|
[3138] | 566 | if (c->remote)
|
---|
[53] | 567 | (void) remote_kill (c->pid, sig);
|
---|
| 568 |
|
---|
[3138] | 569 | for (c = children; c != 0; c = c->next)
|
---|
[53] | 570 | delete_child_targets (c);
|
---|
[3138] | 571 |
|
---|
[53] | 572 | /* Clean up the children. We don't just use the call below because
|
---|
| 573 | we don't want to print the "Waiting for children" message. */
|
---|
| 574 | while (job_slots_used > 0)
|
---|
| 575 | reap_children (1, 0);
|
---|
| 576 | }
|
---|
| 577 | else
|
---|
| 578 | /* Wait for our children to die. */
|
---|
| 579 | while (job_slots_used > 0)
|
---|
| 580 | reap_children (1, 1);
|
---|
| 581 |
|
---|
| 582 | /* Delete any non-precious intermediate files that were made. */
|
---|
| 583 |
|
---|
| 584 | remove_intermediates (1);
|
---|
| 585 |
|
---|
[3138] | 586 | #ifdef SIGQUIT
|
---|
[53] | 587 | if (sig == SIGQUIT)
|
---|
| 588 | /* We don't want to send ourselves SIGQUIT, because it will
|
---|
[280] | 589 | cause a core dump. Just exit instead. */
|
---|
[501] | 590 | exit (MAKE_TROUBLE);
|
---|
| 591 | #endif
|
---|
| 592 |
|
---|
| 593 | #ifdef WINDOWS32
|
---|
| 594 | if (main_thread)
|
---|
[280] | 595 | CloseHandle (main_thread);
|
---|
[53] | 596 | /* Cannot call W32_kill with a pid (it needs a handle). The exit
|
---|
| 597 | status of 130 emulates what happens in Bash. */
|
---|
| 598 | exit (130);
|
---|
| 599 | #else
|
---|
[280] | 600 | /* Signal the same code; this time it will really be fatal. The signal
|
---|
[53] | 601 | will be unblocked when we return and arrive then to kill us. */
|
---|
| 602 | if (kill (getpid (), sig) < 0)
|
---|
| 603 | pfatal_with_name ("kill");
|
---|
| 604 | #endif /* not WINDOWS32 */
|
---|
| 605 | #endif /* not Amiga */
|
---|
| 606 | #endif /* not __MSDOS__ */
|
---|
| 607 | }
|
---|
| 608 | |
---|
[900] | 609 |
|
---|
[53] | 610 | /* Delete FILE unless it's precious or not actually a file (phony),
|
---|
| 611 | and it has changed on disk since we last stat'd it. */
|
---|
| 612 |
|
---|
| 613 | static void
|
---|
| 614 | delete_target (struct file *file, const char *on_behalf_of)
|
---|
| 615 | {
|
---|
| 616 | struct stat st;
|
---|
| 617 | int e;
|
---|
| 618 |
|
---|
| 619 | if (file->precious || file->phony)
|
---|
| 620 | return;
|
---|
[3138] | 621 |
|
---|
| 622 | #ifndef NO_ARCHIVES
|
---|
[53] | 623 | if (ar_name (file->name))
|
---|
[3138] | 624 | {
|
---|
| 625 | time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
|
---|
| 626 | ? (time_t) -1
|
---|
| 627 | : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
|
---|
| 628 | if (ar_member_date (file->name) != file_date)
|
---|
| 629 | {
|
---|
| 630 | if (on_behalf_of)
|
---|
| 631 | OSS (error, NILF,
|
---|
| 632 | _("*** [%s] Archive member '%s' may be bogus; not deleted"),
|
---|
| 633 | on_behalf_of, file->name);
|
---|
[53] | 634 | else
|
---|
| 635 | OS (error, NILF,
|
---|
| 636 | _("*** Archive member '%s' may be bogus; not deleted"),
|
---|
| 637 | file->name);
|
---|
| 638 | }
|
---|
| 639 | return;
|
---|
| 640 | }
|
---|
| 641 | #endif
|
---|
| 642 |
|
---|
| 643 | EINTRLOOP (e, stat (file->name, &st));
|
---|
[3138] | 644 | if (e == 0
|
---|
| 645 | && S_ISREG (st.st_mode)
|
---|
[53] | 646 | && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
|
---|
[3138] | 647 | {
|
---|
[53] | 648 | if (on_behalf_of)
|
---|
[3138] | 649 | OSS (error, NILF,
|
---|
| 650 | _("*** [%s] Deleting file '%s'"), on_behalf_of, file->name);
|
---|
[53] | 651 | else
|
---|
| 652 | OS (error, NILF, _("*** Deleting file '%s'"), file->name);
|
---|
| 653 | if (unlink (file->name) < 0
|
---|
| 654 | && errno != ENOENT) /* It disappeared; so what. */
|
---|
| 655 | perror_with_name ("unlink: ", file->name);
|
---|
| 656 | }
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 |
|
---|
| 660 | /* Delete all non-precious targets of CHILD unless they were already deleted.
|
---|
| 661 | Set the flag in CHILD to say they've been deleted. */
|
---|
| 662 |
|
---|
| 663 | void
|
---|
| 664 | delete_child_targets (struct child *child)
|
---|
| 665 | {
|
---|
| 666 | struct dep *d;
|
---|
[900] | 667 |
|
---|
[53] | 668 | if (child->deleted)
|
---|
[3138] | 669 | return;
|
---|
[53] | 670 |
|
---|
| 671 | /* Delete the target file if it changed. */
|
---|
| 672 | delete_target (child->file, NULL);
|
---|
| 673 |
|
---|
| 674 | /* Also remove any non-precious targets listed in the 'also_make' member. */
|
---|
| 675 | for (d = child->file->also_make; d != 0; d = d->next)
|
---|
| 676 | delete_target (d->file, child->file->name);
|
---|
| 677 |
|
---|
| 678 | child->deleted = 1;
|
---|
[900] | 679 | }
|
---|
[53] | 680 | |
---|
[900] | 681 |
|
---|
[53] | 682 | /* Print out the commands in CMDS. */
|
---|
[1989] | 683 |
|
---|
[53] | 684 | void
|
---|
| 685 | print_commands (const struct commands *cmds)
|
---|
| 686 | {
|
---|
| 687 | const char *s;
|
---|
[3138] | 688 |
|
---|
[53] | 689 | fputs (_("# recipe to execute"), stdout);
|
---|
| 690 |
|
---|
| 691 | if (cmds->fileinfo.filenm == 0)
|
---|
| 692 | puts (_(" (built-in):"));
|
---|
| 693 | else
|
---|
[900] | 694 | printf (_(" (from '%s', line %lu):\n"),
|
---|
[3138] | 695 | cmds->fileinfo.filenm, cmds->fileinfo.lineno);
|
---|
[53] | 696 |
|
---|
[3138] | 697 | s = cmds->commands;
|
---|
| 698 | while (*s != '\0')
|
---|
| 699 | {
|
---|
| 700 | const char *end;
|
---|
| 701 | int bs;
|
---|
[53] | 702 |
|
---|
[3138] | 703 | /* Print one full logical recipe line: find a non-escaped newline. */
|
---|
| 704 | for (end = s, bs = 0; *end != '\0'; ++end)
|
---|
| 705 | {
|
---|
[1989] | 706 | if (*end == '\n' && !bs)
|
---|
[53] | 707 | break;
|
---|
[1989] | 708 |
|
---|
[53] | 709 | bs = *end == '\\' ? !bs : 0;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
|
---|
| 713 |
|
---|
| 714 | s = end + (end[0] == '\n');
|
---|
| 715 | }
|
---|
| 716 | }
|
---|