| 1 | /* Basic dependency engine for GNU Make.
|
|---|
| 2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
|---|
| 3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
|
|---|
| 4 | Foundation, Inc.
|
|---|
| 5 | This file is part of GNU Make.
|
|---|
| 6 |
|
|---|
| 7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
|---|
| 8 | terms of the GNU General Public License as published by the Free Software
|
|---|
| 9 | Foundation; either version 2, or (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|---|
| 13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License along with
|
|---|
| 16 | GNU Make; see the file COPYING. If not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
|---|
| 18 |
|
|---|
| 19 | #include "make.h"
|
|---|
| 20 | #include "filedef.h"
|
|---|
| 21 | #include "job.h"
|
|---|
| 22 | #include "commands.h"
|
|---|
| 23 | #include "dep.h"
|
|---|
| 24 | #include "variable.h"
|
|---|
| 25 | #include "debug.h"
|
|---|
| 26 |
|
|---|
| 27 | #include <assert.h>
|
|---|
| 28 |
|
|---|
| 29 | #ifdef HAVE_FCNTL_H
|
|---|
| 30 | #include <fcntl.h>
|
|---|
| 31 | #else
|
|---|
| 32 | #include <sys/file.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | #ifdef VMS
|
|---|
| 36 | #include <starlet.h>
|
|---|
| 37 | #endif
|
|---|
| 38 | #ifdef WINDOWS32
|
|---|
| 39 | #include <io.h>
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 | extern int try_implicit_rule PARAMS ((struct file *file, unsigned int depth));
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /* The test for circular dependencies is based on the 'updating' bit in
|
|---|
| 46 | `struct file'. However, double colon targets have seperate `struct
|
|---|
| 47 | file's; make sure we always use the base of the double colon chain. */
|
|---|
| 48 |
|
|---|
| 49 | #define start_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
|
|---|
| 50 | ->updating = 1)
|
|---|
| 51 | #define finish_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
|
|---|
| 52 | ->updating = 0)
|
|---|
| 53 | #define is_updating(_f) (((_f)->double_colon ? (_f)->double_colon : (_f))\
|
|---|
| 54 | ->updating)
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | /* Incremented when a command is started (under -n, when one would be). */
|
|---|
| 58 | unsigned int commands_started = 0;
|
|---|
| 59 |
|
|---|
| 60 | /* Current value for pruning the scan of the goal chain (toggle 0/1). */
|
|---|
| 61 | static unsigned int considered;
|
|---|
| 62 |
|
|---|
| 63 | static int update_file PARAMS ((struct file *file, unsigned int depth));
|
|---|
| 64 | static int update_file_1 PARAMS ((struct file *file, unsigned int depth));
|
|---|
| 65 | static int check_dep PARAMS ((struct file *file, unsigned int depth, FILE_TIMESTAMP this_mtime, int *must_make_ptr));
|
|---|
| 66 | static int touch_file PARAMS ((struct file *file));
|
|---|
| 67 | static void remake_file PARAMS ((struct file *file));
|
|---|
| 68 | static FILE_TIMESTAMP name_mtime PARAMS ((char *name));
|
|---|
| 69 | static int library_search PARAMS ((char **lib, FILE_TIMESTAMP *mtime_ptr));
|
|---|
| 70 |
|
|---|
| 71 | |
|---|
| 72 |
|
|---|
| 73 | /* Remake all the goals in the `struct dep' chain GOALS. Return -1 if nothing
|
|---|
| 74 | was done, 0 if all goals were updated successfully, or 1 if a goal failed.
|
|---|
| 75 |
|
|---|
| 76 | If rebuilding_makefiles is nonzero, these goals are makefiles, so -t, -q,
|
|---|
| 77 | and -n should be disabled for them unless they were also command-line
|
|---|
| 78 | targets, and we should only make one goal at a time and return as soon as
|
|---|
| 79 | one goal whose `changed' member is nonzero is successfully made. */
|
|---|
| 80 |
|
|---|
| 81 | int
|
|---|
| 82 | update_goal_chain (struct dep *goals)
|
|---|
| 83 | {
|
|---|
| 84 | int t = touch_flag, q = question_flag, n = just_print_flag;
|
|---|
| 85 | unsigned int j = job_slots;
|
|---|
| 86 | int status = -1;
|
|---|
| 87 |
|
|---|
| 88 | #define MTIME(file) (rebuilding_makefiles ? file_mtime_no_search (file) \
|
|---|
| 89 | : file_mtime (file))
|
|---|
| 90 |
|
|---|
| 91 | /* Duplicate the chain so we can remove things from it. */
|
|---|
| 92 |
|
|---|
| 93 | goals = copy_dep_chain (goals);
|
|---|
| 94 |
|
|---|
| 95 | {
|
|---|
| 96 | /* Clear the `changed' flag of each goal in the chain.
|
|---|
| 97 | We will use the flag below to notice when any commands
|
|---|
| 98 | have actually been run for a target. When no commands
|
|---|
| 99 | have been run, we give an "up to date" diagnostic. */
|
|---|
| 100 |
|
|---|
| 101 | struct dep *g;
|
|---|
| 102 | for (g = goals; g != 0; g = g->next)
|
|---|
| 103 | g->changed = 0;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /* All files start with the considered bit 0, so the global value is 1. */
|
|---|
| 107 | considered = 1;
|
|---|
| 108 |
|
|---|
| 109 | /* Update all the goals until they are all finished. */
|
|---|
| 110 |
|
|---|
| 111 | while (goals != 0)
|
|---|
| 112 | {
|
|---|
| 113 | register struct dep *g, *lastgoal;
|
|---|
| 114 |
|
|---|
| 115 | /* Start jobs that are waiting for the load to go down. */
|
|---|
| 116 |
|
|---|
| 117 | start_waiting_jobs ();
|
|---|
| 118 |
|
|---|
| 119 | /* Wait for a child to die. */
|
|---|
| 120 |
|
|---|
| 121 | reap_children (1, 0);
|
|---|
| 122 |
|
|---|
| 123 | lastgoal = 0;
|
|---|
| 124 | g = goals;
|
|---|
| 125 | while (g != 0)
|
|---|
| 126 | {
|
|---|
| 127 | /* Iterate over all double-colon entries for this file. */
|
|---|
| 128 | struct file *file;
|
|---|
| 129 | int stop = 0, any_not_updated = 0;
|
|---|
| 130 |
|
|---|
| 131 | for (file = g->file->double_colon ? g->file->double_colon : g->file;
|
|---|
| 132 | file != NULL;
|
|---|
| 133 | file = file->prev)
|
|---|
| 134 | {
|
|---|
| 135 | unsigned int ocommands_started;
|
|---|
| 136 | int x;
|
|---|
| 137 | check_renamed (file);
|
|---|
| 138 | if (rebuilding_makefiles)
|
|---|
| 139 | {
|
|---|
| 140 | if (file->cmd_target)
|
|---|
| 141 | {
|
|---|
| 142 | touch_flag = t;
|
|---|
| 143 | question_flag = q;
|
|---|
| 144 | just_print_flag = n;
|
|---|
| 145 | }
|
|---|
| 146 | else
|
|---|
| 147 | touch_flag = question_flag = just_print_flag = 0;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /* Save the old value of `commands_started' so we can compare
|
|---|
| 151 | later. It will be incremented when any commands are
|
|---|
| 152 | actually run. */
|
|---|
| 153 | ocommands_started = commands_started;
|
|---|
| 154 |
|
|---|
| 155 | x = update_file (file, rebuilding_makefiles ? 1 : 0);
|
|---|
| 156 | check_renamed (file);
|
|---|
| 157 |
|
|---|
| 158 | /* Set the goal's `changed' flag if any commands were started
|
|---|
| 159 | by calling update_file above. We check this flag below to
|
|---|
| 160 | decide when to give an "up to date" diagnostic. */
|
|---|
| 161 | if (commands_started > ocommands_started)
|
|---|
| 162 | g->changed = 1;
|
|---|
| 163 |
|
|---|
| 164 | /* If we updated a file and STATUS was not already 1, set it to
|
|---|
| 165 | 1 if updating failed, or to 0 if updating succeeded. Leave
|
|---|
| 166 | STATUS as it is if no updating was done. */
|
|---|
| 167 |
|
|---|
| 168 | stop = 0;
|
|---|
| 169 | if ((x != 0 || file->updated) && status < 1)
|
|---|
| 170 | {
|
|---|
| 171 | if (file->update_status != 0)
|
|---|
| 172 | {
|
|---|
| 173 | /* Updating failed, or -q triggered. The STATUS value
|
|---|
| 174 | tells our caller which. */
|
|---|
| 175 | status = file->update_status;
|
|---|
| 176 | /* If -q just triggered, stop immediately. It doesn't
|
|---|
| 177 | matter how much more we run, since we already know
|
|---|
| 178 | the answer to return. */
|
|---|
| 179 | stop = (question_flag && !keep_going_flag
|
|---|
| 180 | && !rebuilding_makefiles);
|
|---|
| 181 | }
|
|---|
| 182 | else
|
|---|
| 183 | {
|
|---|
| 184 | FILE_TIMESTAMP mtime = MTIME (file);
|
|---|
| 185 | check_renamed (file);
|
|---|
| 186 |
|
|---|
| 187 | if (file->updated && g->changed &&
|
|---|
| 188 | mtime != file->mtime_before_update)
|
|---|
| 189 | {
|
|---|
| 190 | /* Updating was done. If this is a makefile and
|
|---|
| 191 | just_print_flag or question_flag is set (meaning
|
|---|
| 192 | -n or -q was given and this file was specified
|
|---|
| 193 | as a command-line target), don't change STATUS.
|
|---|
| 194 | If STATUS is changed, we will get re-exec'd, and
|
|---|
| 195 | enter an infinite loop. */
|
|---|
| 196 | if (!rebuilding_makefiles
|
|---|
| 197 | || (!just_print_flag && !question_flag))
|
|---|
| 198 | status = 0;
|
|---|
| 199 | if (rebuilding_makefiles && file->dontcare)
|
|---|
| 200 | /* This is a default makefile; stop remaking. */
|
|---|
| 201 | stop = 1;
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /* Keep track if any double-colon entry is not finished.
|
|---|
| 207 | When they are all finished, the goal is finished. */
|
|---|
| 208 | any_not_updated |= !file->updated;
|
|---|
| 209 |
|
|---|
| 210 | if (stop)
|
|---|
| 211 | break;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /* Reset FILE since it is null at the end of the loop. */
|
|---|
| 215 | file = g->file;
|
|---|
| 216 |
|
|---|
| 217 | if (stop || !any_not_updated)
|
|---|
| 218 | {
|
|---|
| 219 | /* If we have found nothing whatever to do for the goal,
|
|---|
| 220 | print a message saying nothing needs doing. */
|
|---|
| 221 |
|
|---|
| 222 | if (!rebuilding_makefiles
|
|---|
| 223 | /* If the update_status is zero, we updated successfully
|
|---|
| 224 | or not at all. G->changed will have been set above if
|
|---|
| 225 | any commands were actually started for this goal. */
|
|---|
| 226 | && file->update_status == 0 && !g->changed
|
|---|
| 227 | /* Never give a message under -s or -q. */
|
|---|
| 228 | && !silent_flag && !question_flag)
|
|---|
| 229 | message (1, ((file->phony || file->cmds == 0)
|
|---|
| 230 | ? _("Nothing to be done for `%s'.")
|
|---|
| 231 | : _("`%s' is up to date.")),
|
|---|
| 232 | file->name);
|
|---|
| 233 |
|
|---|
| 234 | /* This goal is finished. Remove it from the chain. */
|
|---|
| 235 | if (lastgoal == 0)
|
|---|
| 236 | goals = g->next;
|
|---|
| 237 | else
|
|---|
| 238 | lastgoal->next = g->next;
|
|---|
| 239 |
|
|---|
| 240 | /* Free the storage. */
|
|---|
| 241 | free ((char *) g);
|
|---|
| 242 |
|
|---|
| 243 | g = lastgoal == 0 ? goals : lastgoal->next;
|
|---|
| 244 |
|
|---|
| 245 | if (stop)
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 | else
|
|---|
| 249 | {
|
|---|
| 250 | lastgoal = g;
|
|---|
| 251 | g = g->next;
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /* If we reached the end of the dependency graph toggle the considered
|
|---|
| 256 | flag for the next pass. */
|
|---|
| 257 | if (g == 0)
|
|---|
| 258 | considered = !considered;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | if (rebuilding_makefiles)
|
|---|
| 262 | {
|
|---|
| 263 | touch_flag = t;
|
|---|
| 264 | question_flag = q;
|
|---|
| 265 | just_print_flag = n;
|
|---|
| 266 | job_slots = j;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | return status;
|
|---|
| 270 | }
|
|---|
| 271 | |
|---|
| 272 |
|
|---|
| 273 | /* If FILE is not up to date, execute the commands for it.
|
|---|
| 274 | Return 0 if successful, 1 if unsuccessful;
|
|---|
| 275 | but with some flag settings, just call `exit' if unsuccessful.
|
|---|
| 276 |
|
|---|
| 277 | DEPTH is the depth in recursions of this function.
|
|---|
| 278 | We increment it during the consideration of our dependencies,
|
|---|
| 279 | then decrement it again after finding out whether this file
|
|---|
| 280 | is out of date.
|
|---|
| 281 |
|
|---|
| 282 | If there are multiple double-colon entries for FILE,
|
|---|
| 283 | each is considered in turn. */
|
|---|
| 284 |
|
|---|
| 285 | static int
|
|---|
| 286 | update_file (struct file *file, unsigned int depth)
|
|---|
| 287 | {
|
|---|
| 288 | register int status = 0;
|
|---|
| 289 | register struct file *f;
|
|---|
| 290 |
|
|---|
| 291 | f = file->double_colon ? file->double_colon : file;
|
|---|
| 292 |
|
|---|
| 293 | /* Prune the dependency graph: if we've already been here on _this_
|
|---|
| 294 | pass through the dependency graph, we don't have to go any further.
|
|---|
| 295 | We won't reap_children until we start the next pass, so no state
|
|---|
| 296 | change is possible below here until then. */
|
|---|
| 297 | if (f->considered == considered)
|
|---|
| 298 | {
|
|---|
| 299 | DBF (DB_VERBOSE, _("Pruning file `%s'.\n"));
|
|---|
| 300 | return f->command_state == cs_finished ? f->update_status : 0;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /* This loop runs until we start commands for a double colon rule, or until
|
|---|
| 304 | the chain is exhausted. */
|
|---|
| 305 | for (; f != 0; f = f->prev)
|
|---|
| 306 | {
|
|---|
| 307 | f->considered = considered;
|
|---|
| 308 |
|
|---|
| 309 | status |= update_file_1 (f, depth);
|
|---|
| 310 | check_renamed (f);
|
|---|
| 311 |
|
|---|
| 312 | /* Clean up any alloca() used during the update. */
|
|---|
| 313 | alloca (0);
|
|---|
| 314 |
|
|---|
| 315 | /* If we got an error, don't bother with double_colon etc. */
|
|---|
| 316 | if (status != 0 && !keep_going_flag)
|
|---|
| 317 | return status;
|
|---|
| 318 |
|
|---|
| 319 | if (f->command_state == cs_running
|
|---|
| 320 | || f->command_state == cs_deps_running)
|
|---|
| 321 | {
|
|---|
| 322 | /* Don't run the other :: rules for this
|
|---|
| 323 | file until this rule is finished. */
|
|---|
| 324 | status = 0;
|
|---|
| 325 | break;
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /* Process the remaining rules in the double colon chain so they're marked
|
|---|
| 330 | considered. Start their prerequisites, too. */
|
|---|
| 331 | if (file->double_colon)
|
|---|
| 332 | for (; f != 0 ; f = f->prev)
|
|---|
| 333 | {
|
|---|
| 334 | struct dep *d;
|
|---|
| 335 |
|
|---|
| 336 | f->considered = considered;
|
|---|
| 337 |
|
|---|
| 338 | for (d = f->deps; d != 0; d = d->next)
|
|---|
| 339 | status |= update_file (d->file, depth + 1);
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | return status;
|
|---|
| 343 | }
|
|---|
| 344 | |
|---|
| 345 |
|
|---|
| 346 | /* Show a message stating the target failed to build. */
|
|---|
| 347 |
|
|---|
| 348 | static void
|
|---|
| 349 | complain (const struct file *file)
|
|---|
| 350 | {
|
|---|
| 351 | const char *msg_noparent
|
|---|
| 352 | = _("%sNo rule to make target `%s'%s");
|
|---|
| 353 | const char *msg_parent
|
|---|
| 354 | = _("%sNo rule to make target `%s', needed by `%s'%s");
|
|---|
| 355 |
|
|---|
| 356 | if (!keep_going_flag)
|
|---|
| 357 | {
|
|---|
| 358 | if (file->parent == 0)
|
|---|
| 359 | fatal (NILF, msg_noparent, "", file->name, "");
|
|---|
| 360 |
|
|---|
| 361 | fatal (NILF, msg_parent, "", file->name, file->parent->name, "");
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | if (file->parent == 0)
|
|---|
| 365 | error (NILF, msg_noparent, "*** ", file->name, ".");
|
|---|
| 366 | else
|
|---|
| 367 | error (NILF, msg_parent, "*** ", file->name, file->parent->name, ".");
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /* Consider a single `struct file' and update it as appropriate. */
|
|---|
| 371 |
|
|---|
| 372 | static int
|
|---|
| 373 | update_file_1 (struct file *file, unsigned int depth)
|
|---|
| 374 | {
|
|---|
| 375 | register FILE_TIMESTAMP this_mtime;
|
|---|
| 376 | int noexist, must_make, deps_changed;
|
|---|
| 377 | int dep_status = 0;
|
|---|
| 378 | register struct dep *d, *lastd;
|
|---|
| 379 | int running = 0;
|
|---|
| 380 |
|
|---|
| 381 | DBF (DB_VERBOSE, _("Considering target file `%s'.\n"));
|
|---|
| 382 |
|
|---|
| 383 | if (file->updated)
|
|---|
| 384 | {
|
|---|
| 385 | if (file->update_status > 0)
|
|---|
| 386 | {
|
|---|
| 387 | DBF (DB_VERBOSE,
|
|---|
| 388 | _("Recently tried and failed to update file `%s'.\n"));
|
|---|
| 389 |
|
|---|
| 390 | /* If the file we tried to make is marked dontcare then no message
|
|---|
| 391 | was printed about it when it failed during the makefile rebuild.
|
|---|
| 392 | If we're trying to build it again in the normal rebuild, print a
|
|---|
| 393 | message now. */
|
|---|
| 394 | if (file->dontcare && !rebuilding_makefiles)
|
|---|
| 395 | {
|
|---|
| 396 | file->dontcare = 0;
|
|---|
| 397 | complain (file);
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | return file->update_status;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | DBF (DB_VERBOSE, _("File `%s' was considered already.\n"));
|
|---|
| 404 | return 0;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | switch (file->command_state)
|
|---|
| 408 | {
|
|---|
| 409 | case cs_not_started:
|
|---|
| 410 | case cs_deps_running:
|
|---|
| 411 | break;
|
|---|
| 412 | case cs_running:
|
|---|
| 413 | DBF (DB_VERBOSE, _("Still updating file `%s'.\n"));
|
|---|
| 414 | return 0;
|
|---|
| 415 | case cs_finished:
|
|---|
| 416 | DBF (DB_VERBOSE, _("Finished updating file `%s'.\n"));
|
|---|
| 417 | return file->update_status;
|
|---|
| 418 | default:
|
|---|
| 419 | abort ();
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | ++depth;
|
|---|
| 423 |
|
|---|
| 424 | /* Notice recursive update of the same file. */
|
|---|
| 425 | start_updating (file);
|
|---|
| 426 |
|
|---|
| 427 | /* Looking at the file's modtime beforehand allows the possibility
|
|---|
| 428 | that its name may be changed by a VPATH search, and thus it may
|
|---|
| 429 | not need an implicit rule. If this were not done, the file
|
|---|
| 430 | might get implicit commands that apply to its initial name, only
|
|---|
| 431 | to have that name replaced with another found by VPATH search. */
|
|---|
| 432 |
|
|---|
| 433 | this_mtime = file_mtime (file);
|
|---|
| 434 | check_renamed (file);
|
|---|
| 435 | noexist = this_mtime == NONEXISTENT_MTIME;
|
|---|
| 436 | if (noexist)
|
|---|
| 437 | DBF (DB_BASIC, _("File `%s' does not exist.\n"));
|
|---|
| 438 | else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
|
|---|
| 439 | && file->low_resolution_time)
|
|---|
| 440 | {
|
|---|
| 441 | /* Avoid spurious rebuilds due to low resolution time stamps. */
|
|---|
| 442 | int ns = FILE_TIMESTAMP_NS (this_mtime);
|
|---|
| 443 | if (ns != 0)
|
|---|
| 444 | error (NILF, _("*** Warning: .LOW_RESOLUTION_TIME file `%s' has a high resolution time stamp"),
|
|---|
| 445 | file->name);
|
|---|
| 446 | this_mtime += FILE_TIMESTAMPS_PER_S - 1 - ns;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | must_make = noexist;
|
|---|
| 450 |
|
|---|
| 451 | /* If file was specified as a target with no commands,
|
|---|
| 452 | come up with some default commands. */
|
|---|
| 453 |
|
|---|
| 454 | if (!file->phony && file->cmds == 0 && !file->tried_implicit)
|
|---|
| 455 | {
|
|---|
| 456 | if (try_implicit_rule (file, depth))
|
|---|
| 457 | DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
|
|---|
| 458 | else
|
|---|
| 459 | DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
|
|---|
| 460 | file->tried_implicit = 1;
|
|---|
| 461 | }
|
|---|
| 462 | if (file->cmds == 0 && !file->is_target
|
|---|
| 463 | && default_file != 0 && default_file->cmds != 0)
|
|---|
| 464 | {
|
|---|
| 465 | DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
|
|---|
| 466 | file->cmds = default_file->cmds;
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | /* Update all non-intermediate files we depend on, if necessary,
|
|---|
| 470 | and see whether any of them is more recent than this file. */
|
|---|
| 471 |
|
|---|
| 472 | lastd = 0;
|
|---|
| 473 | d = file->deps;
|
|---|
| 474 | while (d != 0)
|
|---|
| 475 | {
|
|---|
| 476 | FILE_TIMESTAMP mtime;
|
|---|
| 477 | int maybe_make;
|
|---|
| 478 | int dontcare = 0;
|
|---|
| 479 |
|
|---|
| 480 | check_renamed (d->file);
|
|---|
| 481 |
|
|---|
| 482 | mtime = file_mtime (d->file);
|
|---|
| 483 | check_renamed (d->file);
|
|---|
| 484 |
|
|---|
| 485 | if (is_updating (d->file))
|
|---|
| 486 | {
|
|---|
| 487 | error (NILF, _("Circular %s <- %s dependency dropped."),
|
|---|
| 488 | file->name, d->file->name);
|
|---|
| 489 | /* We cannot free D here because our the caller will still have
|
|---|
| 490 | a reference to it when we were called recursively via
|
|---|
| 491 | check_dep below. */
|
|---|
| 492 | if (lastd == 0)
|
|---|
| 493 | file->deps = d->next;
|
|---|
| 494 | else
|
|---|
| 495 | lastd->next = d->next;
|
|---|
| 496 | d = d->next;
|
|---|
| 497 | continue;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | d->file->parent = file;
|
|---|
| 501 | maybe_make = must_make;
|
|---|
| 502 |
|
|---|
| 503 | /* Inherit dontcare flag from our parent. */
|
|---|
| 504 | if (rebuilding_makefiles)
|
|---|
| 505 | {
|
|---|
| 506 | dontcare = d->file->dontcare;
|
|---|
| 507 | d->file->dontcare = file->dontcare;
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 | dep_status |= check_dep (d->file, depth, this_mtime, &maybe_make);
|
|---|
| 512 |
|
|---|
| 513 | /* Restore original dontcare flag. */
|
|---|
| 514 | if (rebuilding_makefiles)
|
|---|
| 515 | d->file->dontcare = dontcare;
|
|---|
| 516 |
|
|---|
| 517 | if (! d->ignore_mtime)
|
|---|
| 518 | must_make = maybe_make;
|
|---|
| 519 |
|
|---|
| 520 | check_renamed (d->file);
|
|---|
| 521 |
|
|---|
| 522 | {
|
|---|
| 523 | register struct file *f = d->file;
|
|---|
| 524 | if (f->double_colon)
|
|---|
| 525 | f = f->double_colon;
|
|---|
| 526 | do
|
|---|
| 527 | {
|
|---|
| 528 | running |= (f->command_state == cs_running
|
|---|
| 529 | || f->command_state == cs_deps_running);
|
|---|
| 530 | f = f->prev;
|
|---|
| 531 | }
|
|---|
| 532 | while (f != 0);
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | if (dep_status != 0 && !keep_going_flag)
|
|---|
| 536 | break;
|
|---|
| 537 |
|
|---|
| 538 | if (!running)
|
|---|
| 539 | /* The prereq is considered changed if the timestamp has changed while
|
|---|
| 540 | it was built, OR it doesn't exist.
|
|---|
| 541 | This causes the Linux kernel build to break. We'll defer this
|
|---|
| 542 | fix until GNU make 3.82 to give them time to update. */
|
|---|
| 543 | d->changed = ((file_mtime (d->file) != mtime)
|
|---|
| 544 | /* || (mtime == NONEXISTENT_MTIME) */);
|
|---|
| 545 |
|
|---|
| 546 | lastd = d;
|
|---|
| 547 | d = d->next;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /* Now we know whether this target needs updating.
|
|---|
| 551 | If it does, update all the intermediate files we depend on. */
|
|---|
| 552 |
|
|---|
| 553 | if (must_make || always_make_flag)
|
|---|
| 554 | {
|
|---|
| 555 | for (d = file->deps; d != 0; d = d->next)
|
|---|
| 556 | if (d->file->intermediate)
|
|---|
| 557 | {
|
|---|
| 558 | int dontcare = 0;
|
|---|
| 559 |
|
|---|
| 560 | FILE_TIMESTAMP mtime = file_mtime (d->file);
|
|---|
| 561 | check_renamed (d->file);
|
|---|
| 562 | d->file->parent = file;
|
|---|
| 563 |
|
|---|
| 564 | /* Inherit dontcare flag from our parent. */
|
|---|
| 565 | if (rebuilding_makefiles)
|
|---|
| 566 | {
|
|---|
| 567 | dontcare = d->file->dontcare;
|
|---|
| 568 | d->file->dontcare = file->dontcare;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 | dep_status |= update_file (d->file, depth);
|
|---|
| 573 |
|
|---|
| 574 | /* Restore original dontcare flag. */
|
|---|
| 575 | if (rebuilding_makefiles)
|
|---|
| 576 | d->file->dontcare = dontcare;
|
|---|
| 577 |
|
|---|
| 578 | check_renamed (d->file);
|
|---|
| 579 |
|
|---|
| 580 | {
|
|---|
| 581 | register struct file *f = d->file;
|
|---|
| 582 | if (f->double_colon)
|
|---|
| 583 | f = f->double_colon;
|
|---|
| 584 | do
|
|---|
| 585 | {
|
|---|
| 586 | running |= (f->command_state == cs_running
|
|---|
| 587 | || f->command_state == cs_deps_running);
|
|---|
| 588 | f = f->prev;
|
|---|
| 589 | }
|
|---|
| 590 | while (f != 0);
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | if (dep_status != 0 && !keep_going_flag)
|
|---|
| 594 | break;
|
|---|
| 595 |
|
|---|
| 596 | if (!running)
|
|---|
| 597 | d->changed = ((file->phony && file->cmds != 0)
|
|---|
| 598 | || file_mtime (d->file) != mtime);
|
|---|
| 599 | }
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | finish_updating (file);
|
|---|
| 603 |
|
|---|
| 604 | DBF (DB_VERBOSE, _("Finished prerequisites of target file `%s'.\n"));
|
|---|
| 605 |
|
|---|
| 606 | if (running)
|
|---|
| 607 | {
|
|---|
| 608 | set_command_state (file, cs_deps_running);
|
|---|
| 609 | --depth;
|
|---|
| 610 | DBF (DB_VERBOSE, _("The prerequisites of `%s' are being made.\n"));
|
|---|
| 611 | return 0;
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 | /* If any dependency failed, give up now. */
|
|---|
| 615 |
|
|---|
| 616 | if (dep_status != 0)
|
|---|
| 617 | {
|
|---|
| 618 | file->update_status = dep_status;
|
|---|
| 619 | notice_finished_file (file);
|
|---|
| 620 |
|
|---|
| 621 | --depth;
|
|---|
| 622 |
|
|---|
| 623 | DBF (DB_VERBOSE, _("Giving up on target file `%s'.\n"));
|
|---|
| 624 |
|
|---|
| 625 | if (depth == 0 && keep_going_flag
|
|---|
| 626 | && !just_print_flag && !question_flag)
|
|---|
| 627 | error (NILF,
|
|---|
| 628 | _("Target `%s' not remade because of errors."), file->name);
|
|---|
| 629 |
|
|---|
| 630 | return dep_status;
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | if (file->command_state == cs_deps_running)
|
|---|
| 634 | /* The commands for some deps were running on the last iteration, but
|
|---|
| 635 | they have finished now. Reset the command_state to not_started to
|
|---|
| 636 | simplify later bookkeeping. It is important that we do this only
|
|---|
| 637 | when the prior state was cs_deps_running, because that prior state
|
|---|
| 638 | was definitely propagated to FILE's also_make's by set_command_state
|
|---|
| 639 | (called above), but in another state an also_make may have
|
|---|
| 640 | independently changed to finished state, and we would confuse that
|
|---|
| 641 | file's bookkeeping (updated, but not_started is bogus state). */
|
|---|
| 642 | set_command_state (file, cs_not_started);
|
|---|
| 643 |
|
|---|
| 644 | /* Now record which prerequisites are more
|
|---|
| 645 | recent than this file, so we can define $?. */
|
|---|
| 646 |
|
|---|
| 647 | deps_changed = 0;
|
|---|
| 648 | for (d = file->deps; d != 0; d = d->next)
|
|---|
| 649 | {
|
|---|
| 650 | FILE_TIMESTAMP d_mtime = file_mtime (d->file);
|
|---|
| 651 | check_renamed (d->file);
|
|---|
| 652 |
|
|---|
| 653 | if (! d->ignore_mtime)
|
|---|
| 654 | {
|
|---|
| 655 | #if 1
|
|---|
| 656 | /* %%% In version 4, remove this code completely to
|
|---|
| 657 | implement not remaking deps if their deps are newer
|
|---|
| 658 | than their parents. */
|
|---|
| 659 | if (d_mtime == NONEXISTENT_MTIME && !d->file->intermediate)
|
|---|
| 660 | /* We must remake if this dep does not
|
|---|
| 661 | exist and is not intermediate. */
|
|---|
| 662 | must_make = 1;
|
|---|
| 663 | #endif
|
|---|
| 664 |
|
|---|
| 665 | /* Set DEPS_CHANGED if this dep actually changed. */
|
|---|
| 666 | deps_changed |= d->changed;
|
|---|
| 667 | }
|
|---|
| 668 |
|
|---|
| 669 | /* Set D->changed if either this dep actually changed,
|
|---|
| 670 | or its dependent, FILE, is older or does not exist. */
|
|---|
| 671 | d->changed |= noexist || d_mtime > this_mtime;
|
|---|
| 672 |
|
|---|
| 673 | if (!noexist && ISDB (DB_BASIC|DB_VERBOSE))
|
|---|
| 674 | {
|
|---|
| 675 | const char *fmt = 0;
|
|---|
| 676 |
|
|---|
| 677 | if (d->ignore_mtime)
|
|---|
| 678 | {
|
|---|
| 679 | if (ISDB (DB_VERBOSE))
|
|---|
| 680 | fmt = _("Prerequisite `%s' is order-only for target `%s'.\n");
|
|---|
| 681 | }
|
|---|
| 682 | else if (d_mtime == NONEXISTENT_MTIME)
|
|---|
| 683 | {
|
|---|
| 684 | if (ISDB (DB_BASIC))
|
|---|
| 685 | fmt = _("Prerequisite `%s' of target `%s' does not exist.\n");
|
|---|
| 686 | }
|
|---|
| 687 | else if (d->changed)
|
|---|
| 688 | {
|
|---|
| 689 | if (ISDB (DB_BASIC))
|
|---|
| 690 | fmt = _("Prerequisite `%s' is newer than target `%s'.\n");
|
|---|
| 691 | }
|
|---|
| 692 | else if (ISDB (DB_VERBOSE))
|
|---|
| 693 | fmt = _("Prerequisite `%s' is older than target `%s'.\n");
|
|---|
| 694 |
|
|---|
| 695 | if (fmt)
|
|---|
| 696 | {
|
|---|
| 697 | print_spaces (depth);
|
|---|
| 698 | printf (fmt, dep_name (d), file->name);
|
|---|
| 699 | fflush (stdout);
|
|---|
| 700 | }
|
|---|
| 701 | }
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | /* Here depth returns to the value it had when we were called. */
|
|---|
| 705 | depth--;
|
|---|
| 706 |
|
|---|
| 707 | if (file->double_colon && file->deps == 0)
|
|---|
| 708 | {
|
|---|
| 709 | must_make = 1;
|
|---|
| 710 | DBF (DB_BASIC,
|
|---|
| 711 | _("Target `%s' is double-colon and has no prerequisites.\n"));
|
|---|
| 712 | }
|
|---|
| 713 | else if (!noexist && file->is_target && !deps_changed && file->cmds == 0
|
|---|
| 714 | && !always_make_flag)
|
|---|
| 715 | {
|
|---|
| 716 | must_make = 0;
|
|---|
| 717 | DBF (DB_VERBOSE,
|
|---|
| 718 | _("No commands for `%s' and no prerequisites actually changed.\n"));
|
|---|
| 719 | }
|
|---|
| 720 | else if (!must_make && file->cmds != 0 && always_make_flag)
|
|---|
| 721 | {
|
|---|
| 722 | must_make = 1;
|
|---|
| 723 | DBF (DB_VERBOSE, _("Making `%s' due to always-make flag.\n"));
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | if (!must_make)
|
|---|
| 727 | {
|
|---|
| 728 | if (ISDB (DB_VERBOSE))
|
|---|
| 729 | {
|
|---|
| 730 | print_spaces (depth);
|
|---|
| 731 | printf (_("No need to remake target `%s'"), file->name);
|
|---|
| 732 | if (!streq (file->name, file->hname))
|
|---|
| 733 | printf (_("; using VPATH name `%s'"), file->hname);
|
|---|
| 734 | puts (".");
|
|---|
| 735 | fflush (stdout);
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | notice_finished_file (file);
|
|---|
| 739 |
|
|---|
| 740 | /* Since we don't need to remake the file, convert it to use the
|
|---|
| 741 | VPATH filename if we found one. hfile will be either the
|
|---|
| 742 | local name if no VPATH or the VPATH name if one was found. */
|
|---|
| 743 |
|
|---|
| 744 | while (file)
|
|---|
| 745 | {
|
|---|
| 746 | file->name = file->hname;
|
|---|
| 747 | file = file->prev;
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | return 0;
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | DBF (DB_BASIC, _("Must remake target `%s'.\n"));
|
|---|
| 754 |
|
|---|
| 755 | /* It needs to be remade. If it's VPATH and not reset via GPATH, toss the
|
|---|
| 756 | VPATH. */
|
|---|
| 757 | if (!streq(file->name, file->hname))
|
|---|
| 758 | {
|
|---|
| 759 | DB (DB_BASIC, (_(" Ignoring VPATH name `%s'.\n"), file->hname));
|
|---|
| 760 | file->ignore_vpath = 1;
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | /* Now, take appropriate actions to remake the file. */
|
|---|
| 764 | remake_file (file);
|
|---|
| 765 |
|
|---|
| 766 | if (file->command_state != cs_finished)
|
|---|
| 767 | {
|
|---|
| 768 | DBF (DB_VERBOSE, _("Commands of `%s' are being run.\n"));
|
|---|
| 769 | return 0;
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | switch (file->update_status)
|
|---|
| 773 | {
|
|---|
| 774 | case 2:
|
|---|
| 775 | DBF (DB_BASIC, _("Failed to remake target file `%s'.\n"));
|
|---|
| 776 | break;
|
|---|
| 777 | case 0:
|
|---|
| 778 | DBF (DB_BASIC, _("Successfully remade target file `%s'.\n"));
|
|---|
| 779 | break;
|
|---|
| 780 | case 1:
|
|---|
| 781 | DBF (DB_BASIC, _("Target file `%s' needs remade under -q.\n"));
|
|---|
| 782 | break;
|
|---|
| 783 | default:
|
|---|
| 784 | assert (file->update_status >= 0 && file->update_status <= 2);
|
|---|
| 785 | break;
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | file->updated = 1;
|
|---|
| 789 | return file->update_status;
|
|---|
| 790 | }
|
|---|
| 791 | |
|---|
| 792 |
|
|---|
| 793 | /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
|
|---|
| 794 | files listed in its `also_make' member. Under -t, this function also
|
|---|
| 795 | touches FILE.
|
|---|
| 796 |
|
|---|
| 797 | On return, FILE->update_status will no longer be -1 if it was. */
|
|---|
| 798 |
|
|---|
| 799 | void
|
|---|
| 800 | notice_finished_file (struct file *file)
|
|---|
| 801 | {
|
|---|
| 802 | struct dep *d;
|
|---|
| 803 | int ran = file->command_state == cs_running;
|
|---|
| 804 | int touched = 0;
|
|---|
| 805 | DB (DB_JOBS, (_("notice_finished_file - entering: file=%p `%s' update_status=%d command_state=%d\n"), /* bird */
|
|---|
| 806 | file, file->name, file->update_status, file->command_state));
|
|---|
| 807 | file->command_state = cs_finished;
|
|---|
| 808 | file->updated = 1;
|
|---|
| 809 |
|
|---|
| 810 | #ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
|---|
| 811 | /* update not_parallel if the file was flagged for that. */
|
|---|
| 812 | if ( ran
|
|---|
| 813 | && (file->command_flags & (COMMANDS_NOTPARALLEL | COMMANDS_NO_COMMANDS))
|
|---|
| 814 | == COMMANDS_NOTPARALLEL)
|
|---|
| 815 | {
|
|---|
| 816 | DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [notice_finished_file]\n"), not_parallel,
|
|---|
| 817 | not_parallel - 1, file, file->name));
|
|---|
| 818 | assert(not_parallel >= 1);
|
|---|
| 819 | --not_parallel;
|
|---|
| 820 | }
|
|---|
| 821 | #endif
|
|---|
| 822 |
|
|---|
| 823 | if (touch_flag
|
|---|
| 824 | /* The update status will be:
|
|---|
| 825 | -1 if this target was not remade;
|
|---|
| 826 | 0 if 0 or more commands (+ or ${MAKE}) were run and won;
|
|---|
| 827 | 1 if some commands were run and lost.
|
|---|
| 828 | We touch the target if it has commands which either were not run
|
|---|
| 829 | or won when they ran (i.e. status is 0). */
|
|---|
| 830 | && file->update_status == 0)
|
|---|
| 831 | {
|
|---|
| 832 | if (file->cmds != 0 && file->cmds->any_recurse)
|
|---|
| 833 | {
|
|---|
| 834 | /* If all the command lines were recursive,
|
|---|
| 835 | we don't want to do the touching. */
|
|---|
| 836 | unsigned int i;
|
|---|
| 837 | for (i = 0; i < file->cmds->ncommand_lines; ++i)
|
|---|
| 838 | if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
|
|---|
| 839 | goto have_nonrecursing;
|
|---|
| 840 | }
|
|---|
| 841 | else
|
|---|
| 842 | {
|
|---|
| 843 | have_nonrecursing:
|
|---|
| 844 | if (file->phony)
|
|---|
| 845 | file->update_status = 0;
|
|---|
| 846 | /* According to POSIX, -t doesn't affect targets with no cmds. */
|
|---|
| 847 | else if (file->cmds != 0)
|
|---|
| 848 | {
|
|---|
| 849 | /* Should set file's modification date and do nothing else. */
|
|---|
| 850 | file->update_status = touch_file (file);
|
|---|
| 851 |
|
|---|
| 852 | /* Pretend we ran a real touch command, to suppress the
|
|---|
| 853 | "`foo' is up to date" message. */
|
|---|
| 854 | commands_started++;
|
|---|
| 855 |
|
|---|
| 856 | /* Request for the timestamp to be updated (and distributed
|
|---|
| 857 | to the double-colon entries). Simply setting ran=1 would
|
|---|
| 858 | almost have done the trick, but messes up with the also_make
|
|---|
| 859 | updating logic below. */
|
|---|
| 860 | touched = 1;
|
|---|
| 861 | }
|
|---|
| 862 | }
|
|---|
| 863 | }
|
|---|
| 864 |
|
|---|
| 865 | if (file->mtime_before_update == UNKNOWN_MTIME)
|
|---|
| 866 | file->mtime_before_update = file->last_mtime;
|
|---|
| 867 |
|
|---|
| 868 | if ((ran && !file->phony) || touched)
|
|---|
| 869 | {
|
|---|
| 870 | int i = 0;
|
|---|
| 871 |
|
|---|
| 872 | /* If -n, -t, or -q and all the commands are recursive, we ran them so
|
|---|
| 873 | really check the target's mtime again. Otherwise, assume the target
|
|---|
| 874 | would have been updated. */
|
|---|
| 875 |
|
|---|
| 876 | if (question_flag || just_print_flag || touch_flag)
|
|---|
| 877 | {
|
|---|
| 878 | for (i = file->cmds->ncommand_lines; i > 0; --i)
|
|---|
| 879 | if (! (file->cmds->lines_flags[i-1] & COMMANDS_RECURSE))
|
|---|
| 880 | break;
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | /* If there were no commands at all, it's always new. */
|
|---|
| 884 |
|
|---|
| 885 | else if (file->is_target && file->cmds == 0)
|
|---|
| 886 | i = 1;
|
|---|
| 887 |
|
|---|
| 888 | file->last_mtime = i == 0 ? UNKNOWN_MTIME : NEW_MTIME;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | if (file->double_colon)
|
|---|
| 892 | {
|
|---|
| 893 | /* If this is a double colon rule and it is the last one to be
|
|---|
| 894 | updated, propagate the change of modification time to all the
|
|---|
| 895 | double-colon entries for this file.
|
|---|
| 896 |
|
|---|
| 897 | We do it on the last update because it is important to handle
|
|---|
| 898 | individual entries as separate rules with separate timestamps
|
|---|
| 899 | while they are treated as targets and then as one rule with the
|
|---|
| 900 | unified timestamp when they are considered as a prerequisite
|
|---|
| 901 | of some target. */
|
|---|
| 902 |
|
|---|
| 903 | struct file *f;
|
|---|
| 904 | FILE_TIMESTAMP max_mtime = file->last_mtime;
|
|---|
| 905 |
|
|---|
| 906 | /* Check that all rules were updated and at the same time find
|
|---|
| 907 | the max timestamp. We assume UNKNOWN_MTIME is newer then
|
|---|
| 908 | any other value. */
|
|---|
| 909 | for (f = file->double_colon; f != 0 && f->updated; f = f->prev)
|
|---|
| 910 | if (max_mtime != UNKNOWN_MTIME
|
|---|
| 911 | && (f->last_mtime == UNKNOWN_MTIME || f->last_mtime > max_mtime))
|
|---|
| 912 | max_mtime = f->last_mtime;
|
|---|
| 913 |
|
|---|
| 914 | if (f == 0)
|
|---|
| 915 | for (f = file->double_colon; f != 0; f = f->prev)
|
|---|
| 916 | f->last_mtime = max_mtime;
|
|---|
| 917 | }
|
|---|
| 918 |
|
|---|
| 919 | if (ran && file->update_status != -1)
|
|---|
| 920 | /* We actually tried to update FILE, which has
|
|---|
| 921 | updated its also_make's as well (if it worked).
|
|---|
| 922 | If it didn't work, it wouldn't work again for them.
|
|---|
| 923 | So mark them as updated with the same status. */
|
|---|
| 924 | for (d = file->also_make; d != 0; d = d->next)
|
|---|
| 925 | {
|
|---|
| 926 | d->file->command_state = cs_finished;
|
|---|
| 927 | d->file->updated = 1;
|
|---|
| 928 | d->file->update_status = file->update_status;
|
|---|
| 929 |
|
|---|
| 930 | if (ran && !d->file->phony)
|
|---|
| 931 | /* Fetch the new modification time.
|
|---|
| 932 | We do this instead of just invalidating the cached time
|
|---|
| 933 | so that a vpath_search can happen. Otherwise, it would
|
|---|
| 934 | never be done because the target is already updated. */
|
|---|
| 935 | (void) f_mtime (d->file, 0);
|
|---|
| 936 | }
|
|---|
| 937 | else if (file->update_status == -1)
|
|---|
| 938 | /* Nothing was done for FILE, but it needed nothing done.
|
|---|
| 939 | So mark it now as "succeeded". */
|
|---|
| 940 | file->update_status = 0;
|
|---|
| 941 | }
|
|---|
| 942 | |
|---|
| 943 |
|
|---|
| 944 | /* Check whether another file (whose mtime is THIS_MTIME)
|
|---|
| 945 | needs updating on account of a dependency which is file FILE.
|
|---|
| 946 | If it does, store 1 in *MUST_MAKE_PTR.
|
|---|
| 947 | In the process, update any non-intermediate files
|
|---|
| 948 | that FILE depends on (including FILE itself).
|
|---|
| 949 | Return nonzero if any updating failed. */
|
|---|
| 950 |
|
|---|
| 951 | static int
|
|---|
| 952 | check_dep (struct file *file, unsigned int depth,
|
|---|
| 953 | FILE_TIMESTAMP this_mtime, int *must_make_ptr)
|
|---|
| 954 | {
|
|---|
| 955 | struct dep *d;
|
|---|
| 956 | int dep_status = 0;
|
|---|
| 957 |
|
|---|
| 958 | ++depth;
|
|---|
| 959 | start_updating (file);
|
|---|
| 960 |
|
|---|
| 961 | if (file->phony || !file->intermediate)
|
|---|
| 962 | {
|
|---|
| 963 | /* If this is a non-intermediate file, update it and record
|
|---|
| 964 | whether it is newer than THIS_MTIME. */
|
|---|
| 965 | FILE_TIMESTAMP mtime;
|
|---|
| 966 | dep_status = update_file (file, depth);
|
|---|
| 967 | check_renamed (file);
|
|---|
| 968 | mtime = file_mtime (file);
|
|---|
| 969 | check_renamed (file);
|
|---|
| 970 | if (mtime == NONEXISTENT_MTIME || mtime > this_mtime)
|
|---|
| 971 | *must_make_ptr = 1;
|
|---|
| 972 | }
|
|---|
| 973 | else
|
|---|
| 974 | {
|
|---|
| 975 | /* FILE is an intermediate file. */
|
|---|
| 976 | FILE_TIMESTAMP mtime;
|
|---|
| 977 |
|
|---|
| 978 | if (!file->phony && file->cmds == 0 && !file->tried_implicit)
|
|---|
| 979 | {
|
|---|
| 980 | if (try_implicit_rule (file, depth))
|
|---|
| 981 | DBF (DB_IMPLICIT, _("Found an implicit rule for `%s'.\n"));
|
|---|
| 982 | else
|
|---|
| 983 | DBF (DB_IMPLICIT, _("No implicit rule found for `%s'.\n"));
|
|---|
| 984 | file->tried_implicit = 1;
|
|---|
| 985 | }
|
|---|
| 986 | if (file->cmds == 0 && !file->is_target
|
|---|
| 987 | && default_file != 0 && default_file->cmds != 0)
|
|---|
| 988 | {
|
|---|
| 989 | DBF (DB_IMPLICIT, _("Using default commands for `%s'.\n"));
|
|---|
| 990 | file->cmds = default_file->cmds;
|
|---|
| 991 | }
|
|---|
| 992 |
|
|---|
| 993 | /* If the intermediate file actually exists
|
|---|
| 994 | and is newer, then we should remake from it. */
|
|---|
| 995 | check_renamed (file);
|
|---|
| 996 | mtime = file_mtime (file);
|
|---|
| 997 | check_renamed (file);
|
|---|
| 998 | if (mtime != NONEXISTENT_MTIME && mtime > this_mtime)
|
|---|
| 999 | *must_make_ptr = 1;
|
|---|
| 1000 | /* Otherwise, update all non-intermediate files we depend on,
|
|---|
| 1001 | if necessary, and see whether any of them is more
|
|---|
| 1002 | recent than the file on whose behalf we are checking. */
|
|---|
| 1003 | else
|
|---|
| 1004 | {
|
|---|
| 1005 | struct dep *lastd;
|
|---|
| 1006 |
|
|---|
| 1007 | lastd = 0;
|
|---|
| 1008 | d = file->deps;
|
|---|
| 1009 | while (d != 0)
|
|---|
| 1010 | {
|
|---|
| 1011 | int maybe_make;
|
|---|
| 1012 |
|
|---|
| 1013 | if (is_updating (d->file))
|
|---|
| 1014 | {
|
|---|
| 1015 | error (NILF, _("Circular %s <- %s dependency dropped."),
|
|---|
| 1016 | file->name, d->file->name);
|
|---|
| 1017 | if (lastd == 0)
|
|---|
| 1018 | {
|
|---|
| 1019 | file->deps = d->next;
|
|---|
| 1020 | free_dep (d);
|
|---|
| 1021 | d = file->deps;
|
|---|
| 1022 | }
|
|---|
| 1023 | else
|
|---|
| 1024 | {
|
|---|
| 1025 | lastd->next = d->next;
|
|---|
| 1026 | free_dep (d);
|
|---|
| 1027 | d = lastd->next;
|
|---|
| 1028 | }
|
|---|
| 1029 | continue;
|
|---|
| 1030 | }
|
|---|
| 1031 |
|
|---|
| 1032 | d->file->parent = file;
|
|---|
| 1033 | maybe_make = *must_make_ptr;
|
|---|
| 1034 | dep_status |= check_dep (d->file, depth, this_mtime,
|
|---|
| 1035 | &maybe_make);
|
|---|
| 1036 | if (! d->ignore_mtime)
|
|---|
| 1037 | *must_make_ptr = maybe_make;
|
|---|
| 1038 | check_renamed (d->file);
|
|---|
| 1039 | if (dep_status != 0 && !keep_going_flag)
|
|---|
| 1040 | break;
|
|---|
| 1041 |
|
|---|
| 1042 | if (d->file->command_state == cs_running
|
|---|
| 1043 | || d->file->command_state == cs_deps_running)
|
|---|
| 1044 | /* Record that some of FILE's deps are still being made.
|
|---|
| 1045 | This tells the upper levels to wait on processing it until
|
|---|
| 1046 | the commands are finished. */
|
|---|
| 1047 | set_command_state (file, cs_deps_running);
|
|---|
| 1048 |
|
|---|
| 1049 | lastd = d;
|
|---|
| 1050 | d = d->next;
|
|---|
| 1051 | }
|
|---|
| 1052 | }
|
|---|
| 1053 | }
|
|---|
| 1054 |
|
|---|
| 1055 | finish_updating (file);
|
|---|
| 1056 | return dep_status;
|
|---|
| 1057 | }
|
|---|
| 1058 | |
|---|
| 1059 |
|
|---|
| 1060 | /* Touch FILE. Return zero if successful, one if not. */
|
|---|
| 1061 |
|
|---|
| 1062 | #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
|
|---|
| 1063 |
|
|---|
| 1064 | static int
|
|---|
| 1065 | touch_file (struct file *file)
|
|---|
| 1066 | {
|
|---|
| 1067 | if (!silent_flag)
|
|---|
| 1068 | message (0, "touch %s", file->name);
|
|---|
| 1069 |
|
|---|
| 1070 | #ifndef NO_ARCHIVES
|
|---|
| 1071 | if (ar_name (file->name))
|
|---|
| 1072 | return ar_touch (file->name);
|
|---|
| 1073 | else
|
|---|
| 1074 | #endif
|
|---|
| 1075 | {
|
|---|
| 1076 | int fd = open (file->name, O_RDWR | O_CREAT, 0666);
|
|---|
| 1077 |
|
|---|
| 1078 | if (fd < 0)
|
|---|
| 1079 | TOUCH_ERROR ("touch: open: ");
|
|---|
| 1080 | else
|
|---|
| 1081 | {
|
|---|
| 1082 | struct stat statbuf;
|
|---|
| 1083 | char buf;
|
|---|
| 1084 | int e;
|
|---|
| 1085 |
|
|---|
| 1086 | EINTRLOOP (e, fstat (fd, &statbuf));
|
|---|
| 1087 | if (e < 0)
|
|---|
| 1088 | TOUCH_ERROR ("touch: fstat: ");
|
|---|
| 1089 | /* Rewrite character 0 same as it already is. */
|
|---|
| 1090 | if (read (fd, &buf, 1) < 0)
|
|---|
| 1091 | TOUCH_ERROR ("touch: read: ");
|
|---|
| 1092 | if (lseek (fd, 0L, 0) < 0L)
|
|---|
| 1093 | TOUCH_ERROR ("touch: lseek: ");
|
|---|
| 1094 | if (write (fd, &buf, 1) < 0)
|
|---|
| 1095 | TOUCH_ERROR ("touch: write: ");
|
|---|
| 1096 | /* If file length was 0, we just
|
|---|
| 1097 | changed it, so change it back. */
|
|---|
| 1098 | if (statbuf.st_size == 0)
|
|---|
| 1099 | {
|
|---|
| 1100 | (void) close (fd);
|
|---|
| 1101 | fd = open (file->name, O_RDWR | O_TRUNC, 0666);
|
|---|
| 1102 | if (fd < 0)
|
|---|
| 1103 | TOUCH_ERROR ("touch: open: ");
|
|---|
| 1104 | }
|
|---|
| 1105 | (void) close (fd);
|
|---|
| 1106 | }
|
|---|
| 1107 | }
|
|---|
| 1108 |
|
|---|
| 1109 | return 0;
|
|---|
| 1110 | }
|
|---|
| 1111 | |
|---|
| 1112 |
|
|---|
| 1113 | /* Having checked and updated the dependencies of FILE,
|
|---|
| 1114 | do whatever is appropriate to remake FILE itself.
|
|---|
| 1115 | Return the status from executing FILE's commands. */
|
|---|
| 1116 |
|
|---|
| 1117 | static void
|
|---|
| 1118 | remake_file (struct file *file)
|
|---|
| 1119 | {
|
|---|
| 1120 | if (file->cmds == 0)
|
|---|
| 1121 | {
|
|---|
| 1122 | if (file->phony)
|
|---|
| 1123 | /* Phony target. Pretend it succeeded. */
|
|---|
| 1124 | file->update_status = 0;
|
|---|
| 1125 | else if (file->is_target)
|
|---|
| 1126 | /* This is a nonexistent target file we cannot make.
|
|---|
| 1127 | Pretend it was successfully remade. */
|
|---|
| 1128 | file->update_status = 0;
|
|---|
| 1129 | else
|
|---|
| 1130 | {
|
|---|
| 1131 | /* This is a dependency file we cannot remake. Fail. */
|
|---|
| 1132 | if (!rebuilding_makefiles || !file->dontcare)
|
|---|
| 1133 | complain (file);
|
|---|
| 1134 | file->update_status = 2;
|
|---|
| 1135 | }
|
|---|
| 1136 | }
|
|---|
| 1137 | else
|
|---|
| 1138 | {
|
|---|
| 1139 | chop_commands (file->cmds);
|
|---|
| 1140 |
|
|---|
| 1141 | /* The normal case: start some commands. */
|
|---|
| 1142 | if (!touch_flag || file->cmds->any_recurse)
|
|---|
| 1143 | {
|
|---|
| 1144 | execute_file_commands (file);
|
|---|
| 1145 | return;
|
|---|
| 1146 | }
|
|---|
| 1147 |
|
|---|
| 1148 | /* This tells notice_finished_file it is ok to touch the file. */
|
|---|
| 1149 | file->update_status = 0;
|
|---|
| 1150 | }
|
|---|
| 1151 |
|
|---|
| 1152 | /* This does the touching under -t. */
|
|---|
| 1153 | notice_finished_file (file);
|
|---|
| 1154 | }
|
|---|
| 1155 | |
|---|
| 1156 |
|
|---|
| 1157 | /* Return the mtime of a file, given a `struct file'.
|
|---|
| 1158 | Caches the time in the struct file to avoid excess stat calls.
|
|---|
| 1159 |
|
|---|
| 1160 | If the file is not found, and SEARCH is nonzero, VPATH searching and
|
|---|
| 1161 | replacement is done. If that fails, a library (-lLIBNAME) is tried and
|
|---|
| 1162 | the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
|
|---|
| 1163 | FILE. */
|
|---|
| 1164 |
|
|---|
| 1165 | FILE_TIMESTAMP
|
|---|
| 1166 | f_mtime (struct file *file, int search)
|
|---|
| 1167 | {
|
|---|
| 1168 | FILE_TIMESTAMP mtime;
|
|---|
| 1169 |
|
|---|
| 1170 | /* File's mtime is not known; must get it from the system. */
|
|---|
| 1171 |
|
|---|
| 1172 | #ifndef NO_ARCHIVES
|
|---|
| 1173 | if (ar_name (file->name))
|
|---|
| 1174 | {
|
|---|
| 1175 | /* This file is an archive-member reference. */
|
|---|
| 1176 |
|
|---|
| 1177 | char *arname, *memname;
|
|---|
| 1178 | struct file *arfile;
|
|---|
| 1179 | int arname_used = 0;
|
|---|
| 1180 | time_t member_date;
|
|---|
| 1181 |
|
|---|
| 1182 | /* Find the archive's name. */
|
|---|
| 1183 | ar_parse_name (file->name, &arname, &memname);
|
|---|
| 1184 |
|
|---|
| 1185 | /* Find the modification time of the archive itself.
|
|---|
| 1186 | Also allow for its name to be changed via VPATH search. */
|
|---|
| 1187 | arfile = lookup_file (arname);
|
|---|
| 1188 | if (arfile == 0)
|
|---|
| 1189 | {
|
|---|
| 1190 | arfile = enter_file (arname);
|
|---|
| 1191 | arname_used = 1;
|
|---|
| 1192 | }
|
|---|
| 1193 | mtime = f_mtime (arfile, search);
|
|---|
| 1194 | check_renamed (arfile);
|
|---|
| 1195 | if (search && strcmp (arfile->hname, arname))
|
|---|
| 1196 | {
|
|---|
| 1197 | /* The archive's name has changed.
|
|---|
| 1198 | Change the archive-member reference accordingly. */
|
|---|
| 1199 |
|
|---|
| 1200 | char *name;
|
|---|
| 1201 | unsigned int arlen, memlen;
|
|---|
| 1202 |
|
|---|
| 1203 | if (!arname_used)
|
|---|
| 1204 | {
|
|---|
| 1205 | free (arname);
|
|---|
| 1206 | arname_used = 1;
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | arname = arfile->hname;
|
|---|
| 1210 | arlen = strlen (arname);
|
|---|
| 1211 | memlen = strlen (memname);
|
|---|
| 1212 |
|
|---|
| 1213 | /* free (file->name); */
|
|---|
| 1214 |
|
|---|
| 1215 | name = (char *) xmalloc (arlen + 1 + memlen + 2);
|
|---|
| 1216 | bcopy (arname, name, arlen);
|
|---|
| 1217 | name[arlen] = '(';
|
|---|
| 1218 | bcopy (memname, name + arlen + 1, memlen);
|
|---|
| 1219 | name[arlen + 1 + memlen] = ')';
|
|---|
| 1220 | name[arlen + 1 + memlen + 1] = '\0';
|
|---|
| 1221 |
|
|---|
| 1222 | /* If the archive was found with GPATH, make the change permanent;
|
|---|
| 1223 | otherwise defer it until later. */
|
|---|
| 1224 | if (arfile->name == arfile->hname)
|
|---|
| 1225 | rename_file (file, name);
|
|---|
| 1226 | else
|
|---|
| 1227 | rehash_file (file, name);
|
|---|
| 1228 | check_renamed (file);
|
|---|
| 1229 | }
|
|---|
| 1230 |
|
|---|
| 1231 | if (!arname_used)
|
|---|
| 1232 | free (arname);
|
|---|
| 1233 | free (memname);
|
|---|
| 1234 |
|
|---|
| 1235 | file->low_resolution_time = 1;
|
|---|
| 1236 |
|
|---|
| 1237 | if (mtime == NONEXISTENT_MTIME)
|
|---|
| 1238 | /* The archive doesn't exist, so its members don't exist either. */
|
|---|
| 1239 | return NONEXISTENT_MTIME;
|
|---|
| 1240 |
|
|---|
| 1241 | member_date = ar_member_date (file->hname);
|
|---|
| 1242 | mtime = (member_date == (time_t) -1
|
|---|
| 1243 | ? NONEXISTENT_MTIME
|
|---|
| 1244 | : file_timestamp_cons (file->hname, member_date, 0));
|
|---|
| 1245 | }
|
|---|
| 1246 | else
|
|---|
| 1247 | #endif
|
|---|
| 1248 | {
|
|---|
| 1249 | mtime = name_mtime (file->name);
|
|---|
| 1250 |
|
|---|
| 1251 | if (mtime == NONEXISTENT_MTIME && search && !file->ignore_vpath)
|
|---|
| 1252 | {
|
|---|
| 1253 | /* If name_mtime failed, search VPATH. */
|
|---|
| 1254 | char *name = file->name;
|
|---|
| 1255 | if (vpath_search (&name, &mtime)
|
|---|
| 1256 | /* Last resort, is it a library (-lxxx)? */
|
|---|
| 1257 | || (name[0] == '-' && name[1] == 'l'
|
|---|
| 1258 | && library_search (&name, &mtime)))
|
|---|
| 1259 | {
|
|---|
| 1260 | if (mtime != UNKNOWN_MTIME)
|
|---|
| 1261 | /* vpath_search and library_search store UNKNOWN_MTIME
|
|---|
| 1262 | if they didn't need to do a stat call for their work. */
|
|---|
| 1263 | file->last_mtime = mtime;
|
|---|
| 1264 |
|
|---|
| 1265 | /* If we found it in VPATH, see if it's in GPATH too; if so,
|
|---|
| 1266 | change the name right now; if not, defer until after the
|
|---|
| 1267 | dependencies are updated. */
|
|---|
| 1268 | if (gpath_search (name, strlen(name) - strlen(file->name) - 1))
|
|---|
| 1269 | {
|
|---|
| 1270 | rename_file (file, name);
|
|---|
| 1271 | check_renamed (file);
|
|---|
| 1272 | return file_mtime (file);
|
|---|
| 1273 | }
|
|---|
| 1274 |
|
|---|
| 1275 | rehash_file (file, name);
|
|---|
| 1276 | check_renamed (file);
|
|---|
| 1277 | /* If the result of a vpath search is -o or -W, preserve it.
|
|---|
| 1278 | Otherwise, find the mtime of the resulting file. */
|
|---|
| 1279 | if (mtime != OLD_MTIME && mtime != NEW_MTIME)
|
|---|
| 1280 | mtime = name_mtime (name);
|
|---|
| 1281 | }
|
|---|
| 1282 | }
|
|---|
| 1283 | }
|
|---|
| 1284 |
|
|---|
| 1285 | /* Files can have bogus timestamps that nothing newly made will be
|
|---|
| 1286 | "newer" than. Updating their dependents could just result in loops.
|
|---|
| 1287 | So notify the user of the anomaly with a warning.
|
|---|
| 1288 |
|
|---|
| 1289 | We only need to do this once, for now. */
|
|---|
| 1290 |
|
|---|
| 1291 | if (!clock_skew_detected
|
|---|
| 1292 | && mtime != NONEXISTENT_MTIME && mtime != NEW_MTIME
|
|---|
| 1293 | && !file->updated)
|
|---|
| 1294 | {
|
|---|
| 1295 | static FILE_TIMESTAMP adjusted_now;
|
|---|
| 1296 |
|
|---|
| 1297 | FILE_TIMESTAMP adjusted_mtime = mtime;
|
|---|
| 1298 |
|
|---|
| 1299 | #if defined(WINDOWS32) || defined(__MSDOS__)
|
|---|
| 1300 | /* Experimentation has shown that FAT filesystems can set file times
|
|---|
| 1301 | up to 3 seconds into the future! Play it safe. */
|
|---|
| 1302 |
|
|---|
| 1303 | #define FAT_ADJ_OFFSET (FILE_TIMESTAMP) 3
|
|---|
| 1304 |
|
|---|
| 1305 | FILE_TIMESTAMP adjustment = FAT_ADJ_OFFSET << FILE_TIMESTAMP_LO_BITS;
|
|---|
| 1306 | if (ORDINARY_MTIME_MIN + adjustment <= adjusted_mtime)
|
|---|
| 1307 | adjusted_mtime -= adjustment;
|
|---|
| 1308 | #elif defined(__EMX__)
|
|---|
| 1309 | /* FAT filesystems round time to the nearest even second!
|
|---|
| 1310 | Allow for any file (NTFS or FAT) to perhaps suffer from this
|
|---|
| 1311 | brain damage. */
|
|---|
| 1312 | FILE_TIMESTAMP adjustment = (((FILE_TIMESTAMP_S (adjusted_mtime) & 1) == 0
|
|---|
| 1313 | && FILE_TIMESTAMP_NS (adjusted_mtime) == 0)
|
|---|
| 1314 | ? (FILE_TIMESTAMP) 1 << FILE_TIMESTAMP_LO_BITS
|
|---|
| 1315 | : 0);
|
|---|
| 1316 | #endif
|
|---|
| 1317 |
|
|---|
| 1318 | /* If the file's time appears to be in the future, update our
|
|---|
| 1319 | concept of the present and try once more. */
|
|---|
| 1320 | if (adjusted_now < adjusted_mtime)
|
|---|
| 1321 | {
|
|---|
| 1322 | int resolution;
|
|---|
| 1323 | FILE_TIMESTAMP now = file_timestamp_now (&resolution);
|
|---|
| 1324 | adjusted_now = now + (resolution - 1);
|
|---|
| 1325 | if (adjusted_now < adjusted_mtime)
|
|---|
| 1326 | {
|
|---|
| 1327 | #ifdef NO_FLOAT
|
|---|
| 1328 | error (NILF, _("Warning: File `%s' has modification time in the future"),
|
|---|
| 1329 | file->name);
|
|---|
| 1330 | #else
|
|---|
| 1331 | double from_now =
|
|---|
| 1332 | (FILE_TIMESTAMP_S (mtime) - FILE_TIMESTAMP_S (now)
|
|---|
| 1333 | + ((FILE_TIMESTAMP_NS (mtime) - FILE_TIMESTAMP_NS (now))
|
|---|
| 1334 | / 1e9));
|
|---|
| 1335 | error (NILF, _("Warning: File `%s' has modification time %.2g s in the future"),
|
|---|
| 1336 | file->name, from_now);
|
|---|
| 1337 | #endif
|
|---|
| 1338 | clock_skew_detected = 1;
|
|---|
| 1339 | }
|
|---|
| 1340 | }
|
|---|
| 1341 | }
|
|---|
| 1342 |
|
|---|
| 1343 | /* Store the mtime into all the entries for this file. */
|
|---|
| 1344 | if (file->double_colon)
|
|---|
| 1345 | file = file->double_colon;
|
|---|
| 1346 |
|
|---|
| 1347 | do
|
|---|
| 1348 | {
|
|---|
| 1349 | /* If this file is not implicit but it is intermediate then it was
|
|---|
| 1350 | made so by the .INTERMEDIATE target. If this file has never
|
|---|
| 1351 | been built by us but was found now, it existed before make
|
|---|
| 1352 | started. So, turn off the intermediate bit so make doesn't
|
|---|
| 1353 | delete it, since it didn't create it. */
|
|---|
| 1354 | if (mtime != NONEXISTENT_MTIME && file->command_state == cs_not_started
|
|---|
| 1355 | && file->command_state == cs_not_started
|
|---|
| 1356 | && !file->tried_implicit && file->intermediate)
|
|---|
| 1357 | file->intermediate = 0;
|
|---|
| 1358 |
|
|---|
| 1359 | file->last_mtime = mtime;
|
|---|
| 1360 | file = file->prev;
|
|---|
| 1361 | }
|
|---|
| 1362 | while (file != 0);
|
|---|
| 1363 |
|
|---|
| 1364 | return mtime;
|
|---|
| 1365 | }
|
|---|
| 1366 |
|
|---|
| 1367 |
|
|---|
| 1368 | /* Return the mtime of the file or archive-member reference NAME. */
|
|---|
| 1369 |
|
|---|
| 1370 | /* First, we check with stat(). If the file does not exist, then we return
|
|---|
| 1371 | NONEXISTENT_MTIME. If it does, and the symlink check flag is set, then
|
|---|
| 1372 | examine each indirection of the symlink and find the newest mtime.
|
|---|
| 1373 | This causes one duplicate stat() when -L is being used, but the code is
|
|---|
| 1374 | much cleaner. */
|
|---|
| 1375 |
|
|---|
| 1376 | static FILE_TIMESTAMP
|
|---|
| 1377 | name_mtime (char *name)
|
|---|
| 1378 | {
|
|---|
| 1379 | FILE_TIMESTAMP mtime;
|
|---|
| 1380 | struct stat st;
|
|---|
| 1381 | int e;
|
|---|
| 1382 |
|
|---|
| 1383 | EINTRLOOP (e, stat (name, &st));
|
|---|
| 1384 | if (e == 0)
|
|---|
| 1385 | mtime = FILE_TIMESTAMP_STAT_MODTIME (name, st);
|
|---|
| 1386 | else if (errno == ENOENT || errno == ENOTDIR)
|
|---|
| 1387 | mtime = NONEXISTENT_MTIME;
|
|---|
| 1388 | else
|
|---|
| 1389 | {
|
|---|
| 1390 | perror_with_name ("stat: ", name);
|
|---|
| 1391 | return NONEXISTENT_MTIME;
|
|---|
| 1392 | }
|
|---|
| 1393 |
|
|---|
| 1394 | /* If we get here we either found it, or it doesn't exist.
|
|---|
| 1395 | If it doesn't exist see if we can use a symlink mtime instead. */
|
|---|
| 1396 |
|
|---|
| 1397 | #ifdef MAKE_SYMLINKS
|
|---|
| 1398 | #ifndef S_ISLNK
|
|---|
| 1399 | # define S_ISLNK(_m) (((_m)&S_IFMT)==S_IFLNK)
|
|---|
| 1400 | #endif
|
|---|
| 1401 | if (check_symlink_flag)
|
|---|
| 1402 | {
|
|---|
| 1403 | PATH_VAR (lpath);
|
|---|
| 1404 |
|
|---|
| 1405 | /* Check each symbolic link segment (if any). Find the latest mtime
|
|---|
| 1406 | amongst all of them (and the target file of course).
|
|---|
| 1407 | Note that we have already successfully dereferenced all the links
|
|---|
| 1408 | above. So, if we run into any error trying to lstat(), or
|
|---|
| 1409 | readlink(), or whatever, something bizarre-o happened. Just give up
|
|---|
| 1410 | and use whatever mtime we've already computed at that point. */
|
|---|
| 1411 | strcpy (lpath, name);
|
|---|
| 1412 | while (1)
|
|---|
| 1413 | {
|
|---|
| 1414 | FILE_TIMESTAMP ltime;
|
|---|
| 1415 | PATH_VAR (lbuf);
|
|---|
| 1416 | long llen;
|
|---|
| 1417 | char *p;
|
|---|
| 1418 |
|
|---|
| 1419 | EINTRLOOP (e, lstat (lpath, &st));
|
|---|
| 1420 | if (e)
|
|---|
| 1421 | {
|
|---|
| 1422 | /* Just take what we have so far. */
|
|---|
| 1423 | if (errno != ENOENT && errno != ENOTDIR)
|
|---|
| 1424 | perror_with_name ("lstat: ", lpath);
|
|---|
| 1425 | break;
|
|---|
| 1426 | }
|
|---|
| 1427 |
|
|---|
| 1428 | /* If this is not a symlink, we're done (we started with the real
|
|---|
| 1429 | file's mtime so we don't need to test it again). */
|
|---|
| 1430 | if (!S_ISLNK (st.st_mode))
|
|---|
| 1431 | break;
|
|---|
| 1432 |
|
|---|
| 1433 | /* If this mtime is newer than what we had, keep the new one. */
|
|---|
| 1434 | ltime = FILE_TIMESTAMP_STAT_MODTIME (lpath, st);
|
|---|
| 1435 | if (ltime > mtime)
|
|---|
| 1436 | mtime = ltime;
|
|---|
| 1437 |
|
|---|
| 1438 | /* Set up to check the file pointed to by this link. */
|
|---|
| 1439 | EINTRLOOP (llen, readlink (lpath, lbuf, GET_PATH_MAX));
|
|---|
| 1440 | if (llen < 0)
|
|---|
| 1441 | {
|
|---|
| 1442 | /* Eh? Just take what we have. */
|
|---|
| 1443 | perror_with_name ("readlink: ", lpath);
|
|---|
| 1444 | break;
|
|---|
| 1445 | }
|
|---|
| 1446 | lbuf[llen] = '\0';
|
|---|
| 1447 |
|
|---|
| 1448 | /* If the target is fully-qualified or the source is just a
|
|---|
| 1449 | filename, then the new path is the target. Otherwise it's the
|
|---|
| 1450 | source directory plus the target. */
|
|---|
| 1451 | if (lbuf[0] == '/' || (p = strrchr (lpath, '/')) == NULL)
|
|---|
| 1452 | strcpy (lpath, lbuf);
|
|---|
| 1453 | else if ((p - lpath) + llen + 2 > GET_PATH_MAX)
|
|---|
| 1454 | /* Eh? Path too long! Again, just go with what we have. */
|
|---|
| 1455 | break;
|
|---|
| 1456 | else
|
|---|
| 1457 | /* Create the next step in the symlink chain. */
|
|---|
| 1458 | strcpy (p+1, lbuf);
|
|---|
| 1459 | }
|
|---|
| 1460 | }
|
|---|
| 1461 | #endif
|
|---|
| 1462 |
|
|---|
| 1463 | return mtime;
|
|---|
| 1464 | }
|
|---|
| 1465 |
|
|---|
| 1466 |
|
|---|
| 1467 | /* Search for a library file specified as -lLIBNAME, searching for a
|
|---|
| 1468 | suitable library file in the system library directories and the VPATH
|
|---|
| 1469 | directories. */
|
|---|
| 1470 |
|
|---|
| 1471 | static int
|
|---|
| 1472 | library_search (char **lib, FILE_TIMESTAMP *mtime_ptr)
|
|---|
| 1473 | {
|
|---|
| 1474 | static char *dirs[] =
|
|---|
| 1475 | {
|
|---|
| 1476 | #ifndef _AMIGA
|
|---|
| 1477 | "/lib",
|
|---|
| 1478 | "/usr/lib",
|
|---|
| 1479 | #endif
|
|---|
| 1480 | #if defined(WINDOWS32) && !defined(LIBDIR)
|
|---|
| 1481 | /*
|
|---|
| 1482 | * This is completely up to the user at product install time. Just define
|
|---|
| 1483 | * a placeholder.
|
|---|
| 1484 | */
|
|---|
| 1485 | #define LIBDIR "."
|
|---|
| 1486 | #endif
|
|---|
| 1487 | LIBDIR, /* Defined by configuration. */
|
|---|
| 1488 | 0
|
|---|
| 1489 | };
|
|---|
| 1490 |
|
|---|
| 1491 | static char *libpatterns = NULL;
|
|---|
| 1492 |
|
|---|
| 1493 | char *libname = &(*lib)[2]; /* Name without the `-l'. */
|
|---|
| 1494 | FILE_TIMESTAMP mtime;
|
|---|
| 1495 |
|
|---|
| 1496 | /* Loop variables for the libpatterns value. */
|
|---|
| 1497 | char *p, *p2;
|
|---|
| 1498 | unsigned int len;
|
|---|
| 1499 |
|
|---|
| 1500 | char *file, **dp;
|
|---|
| 1501 |
|
|---|
| 1502 | /* If we don't have libpatterns, get it. */
|
|---|
| 1503 | if (!libpatterns)
|
|---|
| 1504 | {
|
|---|
| 1505 | int save = warn_undefined_variables_flag;
|
|---|
| 1506 | warn_undefined_variables_flag = 0;
|
|---|
| 1507 |
|
|---|
| 1508 | libpatterns = xstrdup (variable_expand ("$(strip $(.LIBPATTERNS))"));
|
|---|
| 1509 |
|
|---|
| 1510 | warn_undefined_variables_flag = save;
|
|---|
| 1511 | }
|
|---|
| 1512 |
|
|---|
| 1513 | /* Loop through all the patterns in .LIBPATTERNS, and search on each one. */
|
|---|
| 1514 | p2 = libpatterns;
|
|---|
| 1515 | while ((p = find_next_token (&p2, &len)) != 0)
|
|---|
| 1516 | {
|
|---|
| 1517 | static char *buf = NULL;
|
|---|
| 1518 | static unsigned int buflen = 0;
|
|---|
| 1519 | static int libdir_maxlen = -1;
|
|---|
| 1520 | char *libbuf = variable_expand ("");
|
|---|
| 1521 |
|
|---|
| 1522 | /* Expand the pattern using LIBNAME as a replacement. */
|
|---|
| 1523 | {
|
|---|
| 1524 | char c = p[len];
|
|---|
| 1525 | char *p3, *p4;
|
|---|
| 1526 |
|
|---|
| 1527 | p[len] = '\0';
|
|---|
| 1528 | p3 = find_percent (p);
|
|---|
| 1529 | if (!p3)
|
|---|
| 1530 | {
|
|---|
| 1531 | /* Give a warning if there is no pattern, then remove the
|
|---|
| 1532 | pattern so it's ignored next time. */
|
|---|
| 1533 | error (NILF, _(".LIBPATTERNS element `%s' is not a pattern"), p);
|
|---|
| 1534 | for (; len; --len, ++p)
|
|---|
| 1535 | *p = ' ';
|
|---|
| 1536 | *p = c;
|
|---|
| 1537 | continue;
|
|---|
| 1538 | }
|
|---|
| 1539 | p4 = variable_buffer_output (libbuf, p, p3-p);
|
|---|
| 1540 | p4 = variable_buffer_output (p4, libname, strlen (libname));
|
|---|
| 1541 | p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
|
|---|
| 1542 | p[len] = c;
|
|---|
| 1543 | }
|
|---|
| 1544 |
|
|---|
| 1545 | /* Look first for `libNAME.a' in the current directory. */
|
|---|
| 1546 | mtime = name_mtime (libbuf);
|
|---|
| 1547 | if (mtime != NONEXISTENT_MTIME)
|
|---|
| 1548 | {
|
|---|
| 1549 | *lib = xstrdup (libbuf);
|
|---|
| 1550 | if (mtime_ptr != 0)
|
|---|
| 1551 | *mtime_ptr = mtime;
|
|---|
| 1552 | return 1;
|
|---|
| 1553 | }
|
|---|
| 1554 |
|
|---|
| 1555 | /* Now try VPATH search on that. */
|
|---|
| 1556 |
|
|---|
| 1557 | file = libbuf;
|
|---|
| 1558 | if (vpath_search (&file, mtime_ptr))
|
|---|
| 1559 | {
|
|---|
| 1560 | *lib = file;
|
|---|
| 1561 | return 1;
|
|---|
| 1562 | }
|
|---|
| 1563 |
|
|---|
| 1564 | /* Now try the standard set of directories. */
|
|---|
| 1565 |
|
|---|
| 1566 | if (!buflen)
|
|---|
| 1567 | {
|
|---|
| 1568 | for (dp = dirs; *dp != 0; ++dp)
|
|---|
| 1569 | {
|
|---|
| 1570 | int l = strlen (*dp);
|
|---|
| 1571 | if (l > libdir_maxlen)
|
|---|
| 1572 | libdir_maxlen = l;
|
|---|
| 1573 | }
|
|---|
| 1574 | buflen = strlen (libbuf);
|
|---|
| 1575 | buf = xmalloc(libdir_maxlen + buflen + 2);
|
|---|
| 1576 | }
|
|---|
| 1577 | else if (buflen < strlen (libbuf))
|
|---|
| 1578 | {
|
|---|
| 1579 | buflen = strlen (libbuf);
|
|---|
| 1580 | buf = xrealloc (buf, libdir_maxlen + buflen + 2);
|
|---|
| 1581 | }
|
|---|
| 1582 |
|
|---|
| 1583 | for (dp = dirs; *dp != 0; ++dp)
|
|---|
| 1584 | {
|
|---|
| 1585 | sprintf (buf, "%s/%s", *dp, libbuf);
|
|---|
| 1586 | mtime = name_mtime (buf);
|
|---|
| 1587 | if (mtime != NONEXISTENT_MTIME)
|
|---|
| 1588 | {
|
|---|
| 1589 | *lib = xstrdup (buf);
|
|---|
| 1590 | if (mtime_ptr != 0)
|
|---|
| 1591 | *mtime_ptr = mtime;
|
|---|
| 1592 | return 1;
|
|---|
| 1593 | }
|
|---|
| 1594 | }
|
|---|
| 1595 | }
|
|---|
| 1596 |
|
|---|
| 1597 | return 0;
|
|---|
| 1598 | }
|
|---|