1 /* @(#)update.c 1.145 21/08/11 Copyright 1985, 88, 91, 1995-2021 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static UConst char sccsid[] =
5 "@(#)update.c 1.145 21/08/11 Copyright 1985, 88, 91, 1995-2021 J. Schilling";
6 #endif
7 /*
8 * Make program
9 * Macro handling / Dependency Update
10 *
11 * Copyright (c) 1985, 88, 91, 1995-2021 by J. Schilling
12 */
13 /*
14 * The contents of this file are subject to the terms of the
15 * Common Development and Distribution License, Version 1.0 only
16 * (the "License"). You may not use this file except in compliance
17 * with the License.
18 *
19 * See the file CDDL.Schily.txt in this distribution for details.
20 * A copy of the CDDL is also available via the Internet at
21 * http://www.opensource.org/licenses/cddl1.txt
22 *
23 * When distributing Covered Code, include this CDDL HEADER in each
24 * file and include the License file CDDL.Schily.txt from this distribution.
25 */
26
27 #include <schily/stdio.h>
28 #include <schily/types.h>
29 #include <schily/standard.h>
30 #include <schily/stdlib.h> /* for free() */
31 #include <schily/string.h>
32 #include <schily/unistd.h>
33 #include <schily/schily.h>
34 #include <schily/libport.h>
35 #include "make.h"
36 #include "job.h"
37
38 EXPORT obj_t *default_tgt; /* Current 'make' arg or default tgt */
39 EXPORT BOOL found_make; /* Did we expand the $(MAKE) macro? */
40
41 #define RTYPE_NONE -1 /* Undefined type (used to init) */
42 #define RTYPE_DEFAULT 0 /* Rule from .DEFAULT: target */
43 #define RTYPE_SSUFFIX 1 /* Simple suffix rule */
44 #define RTYPE_SUFFIX 2 /* Single suffix rule */
45 #define RTYPE_DSUFFIX 3 /* Double suffix rule */
46 #define RTYPE_PATTERN 4 /* Pattern matching rule */
47
48 #define RTYPE_NEEDFREE 0x1000 /* cmd_t * list needs to be free'd */
49
50 #define rule_type(t) ((t) & ~RTYPE_NEEDFREE)
51
52 EXPORT void initchars __PR((void));
53 EXPORT char *filename __PR((const char * name));
54 LOCAL void copy_dir __PR((char * name, char *dir, size_t dsize));
55 LOCAL char *get_suffix __PR((char *name, char *suffix));
56 LOCAL void copy_base __PR((char *name, char *dir, size_t dsize, char *suffix));
57 EXPORT BOOL isprecious __PR((obj_t * obj));
58 EXPORT BOOL isphony __PR((obj_t * obj));
59 LOCAL patr_t *_pattern_rule __PR((patr_t * prule, char *name));
60 LOCAL obj_t *pattern_rule __PR((obj_t * obj));
61 LOCAL obj_t *suffix_rule __PR((obj_t * obj, int *rtypep));
62 LOCAL void suffix_warn __PR((obj_t * obj));
63 LOCAL obj_t *ssuffix_rule __PR((obj_t * obj));
64 LOCAL obj_t *default_rule __PR((obj_t * obj, int *rtypep));
65 EXPORT list_t *list_nth __PR((list_t * list, int n));
66 EXPORT char *build_path __PR((int level, char *name, size_t namelen,
67 char *path, size_t psize));
68 LOCAL void etoolong __PR((char *topic, char *name));
69 LOCAL void grant_gbuf __PR((int size));
70 LOCAL void sub_put __PR((char *chunk, int size));
71 LOCAL void sub_c_put __PR((int c));
72 LOCAL void sub_s_put __PR((char *chunk));
73 LOCAL BOOL sub_arg __PR((int n, list_t * depends, obj_t * target));
74 EXPORT char *substitute __PR((char *cmd, obj_t * obj, obj_t * source, char *suffix));
75 LOCAL char *subst __PR((char *cmd, obj_t * obj, obj_t * source, char *suffix, list_t * depends));
76 LOCAL char *dynmac __PR((char *cmd, obj_t * obj, obj_t * source, char *suffix, list_t * depends, BOOL domod));
77 LOCAL void warn_implicit __PR((obj_t *obj, char *mac, char *exp));
78 LOCAL void warn_displaced __PR((obj_t *obj, char *mac, char *exp));
79 LOCAL void extr_filenames __PR((char *names));
80 LOCAL void extr_dirnames __PR((char *names));
81 LOCAL char *exp_var __PR((char end, char *cmd, obj_t * obj, obj_t * source, char *suffix, list_t *depends));
82 LOCAL char *rstr __PR((char * s1, char * s2));
83 LOCAL BOOL patsub __PR((char *name, char *f1, char *f2, char *t1, char *t2));
84 LOCAL void patmsub __PR((char *name, char *f1, char *f2, char *t1, char *t2));
85 LOCAL void parsepat __PR((char *pat, char **fp1, char **fp2, char **tp1, char **tp2));
86 EXPORT char *shout __PR((char *cmd));
87 LOCAL char *shsub __PR((list_t * l, obj_t * obj, obj_t * source, char *suffix, list_t * depends));
88 LOCAL void exp_name __PR((char * name, obj_t * obj, obj_t * source, char *suffix, list_t * depends, char *));
89 LOCAL void dcolon_time __PR((obj_t *obj));
90 LOCAL date_t searchobj __PR((obj_t * obj, int maxlevel, int mode));
91 LOCAL obj_t *patr_src __PR((char *name, patr_t * prule, int *rtypep, char ** suffixp, cmd_t ** pcmd, int dlev));
92 LOCAL obj_t *suff_src __PR((char *name, obj_t * rule, int *rtypep, char ** suffixp, cmd_t ** pcmd, int dlev));
93 LOCAL obj_t *one_suff_src __PR((char *name, char *suffix, cmd_t **pcmd, int dlev));
94 LOCAL obj_t *ssuff_src __PR((char *name, obj_t * rule, int *rtypep, char ** suffixp, cmd_t ** pcmd, int dlev));
95 LOCAL obj_t *findsrc __PR((obj_t *obj, obj_t * rule, int *rtypep, char ** suffixp, cmd_t ** pcmd, int dlev));
96 LOCAL date_t default_cmd __PR((obj_t * obj, char *depname, date_t deptime, int deplevel, BOOL must_exist, int dlev));
97 LOCAL date_t make __PR((obj_t * obj, BOOL must_exist, int dlev));
98 EXPORT BOOL domake __PR((char *name));
99 EXPORT BOOL omake __PR((obj_t * obj, BOOL must_exist));
100 EXPORT BOOL xmake __PR((char *name, BOOL must_exist));
101
102 char chartype[256];
103
104 /*
105 * Optimise character classification
106 */
107 EXPORT void
initchars()108 initchars()
109 {
110 char *p;
111 int c;
112
113 p = "@*<0123456789r^?";
114
115 while ((c = *p++) != '\0') {
116 chartype[c] |= DYNCHAR;
117 }
118
119 p = "0123456789";
120
121 while ((c = *p++) != '\0') {
122 chartype[c] |= NUMBER;
123 }
124 }
125
126 /*
127 * Return last pathname component.
128 */
129 EXPORT char *
filename(name)130 filename(name)
131 register const char *name;
132 {
133 register const char *fname;
134
135 for (fname = name; *name; )
136 if (*name++ == SLASH)
137 fname = name;
138 return ((char *)fname);
139 }
140
141 /*
142 * Copy directory component of pathname.
143 */
144 LOCAL void
copy_dir(name,dir,dsize)145 copy_dir(name, dir, dsize)
146 register char *name;
147 register char *dir;
148 register size_t dsize;
149 {
150 register char *p = filename(name);
151 char *ns = name;
152
153 if (XDebug > 0)
154 error("copy_dir(name:'%s', dir:'%s', dsize: %zd) fn: '%s' \n",
155 name, dir, dsize, p);
156 *dir = '\0';
157 if (p == name) {
158 if (dsize < 2)
159 etoolong("copy directory name", ns);
160 *dir++ = '.';
161 *dir = '\0';
162 } else {
163 if (++dsize == 0) { /* unsigned overflow */
164 dsize--;
165 while (name < p && dsize-- > 0)
166 *dir++ = *name++;
167 dsize++;
168 } else {
169 while (name < p && --dsize > 0)
170 *dir++ = *name++;
171 }
172 if (dsize == 0)
173 etoolong("copy directory name", ns);
174
175 *--dir = '\0'; /* POSIX wants the '/' to be removed */
176 /* This will make dir(/filename) */
177 /* not usable... */
178 *dir = '\0';
179 }
180 }
181
182 /*
183 * Return part after '.' of last pathname component.
184 */
185 LOCAL char *
get_suffix(name,suffix)186 get_suffix(name, suffix)
187 char *name;
188 char *suffix;
189 {
190 register char *p;
191 register char *suff = (char *)NULL;
192
193 if (suffix != NULL) {
194 p = filename(name);
195 suff = rstr(p, suffix);
196 if (suff == (char *)NULL) /* No suffix: return end of string */
197 suff = &p[strlen(p)];
198 return (suff);
199 }
200
201 for (p = filename(name); *p; p++)
202 if (*p == '.')
203 suff = p;
204 if (suff == (char *)NULL) /* No suffix: return end of string */
205 suff = p;
206 return (suff);
207 }
208
209 /*
210 * Copy namebase (everything before '.').
211 */
212 LOCAL void
copy_base(name,dir,dsize,suffix)213 copy_base(name, dir, dsize, suffix)
214 register char *name;
215 register char *dir;
216 register size_t dsize;
217 char *suffix;
218 {
219 register char *p = get_suffix(name, suffix);
220 char *ns = name;
221
222 if (++dsize == 0) { /* unsigned overflow */
223 dsize--;
224 while (name < p && dsize-- > 0)
225 *dir++ = *name++;
226 dsize++;
227 } else {
228 while (name < p && --dsize > 0)
229 *dir++ = *name++;
230 }
231 if (dsize == 0)
232 etoolong("copy base name", ns);
233
234 *dir = '\0';
235 }
236
237 /*
238 * Return TRUE if 'obj' is in the list of targets that should not be removed
239 * or of the list of dependencies for .PRECIOUS is empty.
240 */
241 EXPORT BOOL
isprecious(obj)242 isprecious(obj)
243 obj_t *obj;
244 {
245 list_t *l;
246
247 if (Precious == (obj_t *)NULL)
248 return (FALSE);
249
250 if (Precious->o_list == NULL)
251 return (TRUE);
252
253 for (l = Precious->o_list; l; l = l->l_next)
254 if (obj == l->l_obj)
255 return (TRUE);
256 return (FALSE);
257 }
258
259 /*
260 * Return TRUE if 'obj' is in the list of targets that should not be checked
261 * aginst existing files. A .PHONY target is assumed to be never up to date,
262 * it is not removed in case a signal is received.
263 */
264 EXPORT BOOL
isphony(obj)265 isphony(obj)
266 obj_t *obj;
267 {
268 list_t *l;
269
270 if (Phony == (obj_t *)NULL)
271 return (FALSE);
272
273 for (l = Phony->o_list; l; l = l->l_next)
274 if (obj == l->l_obj)
275 return (TRUE);
276 return (FALSE);
277 }
278
279 /*
280 * Find pattern rule for 'name' starting at 'prule' in rules list.
281 */
282 LOCAL patr_t *
_pattern_rule(prule,name)283 _pattern_rule(prule, name)
284 register patr_t *prule;
285 register char *name;
286 {
287 register char *p;
288
289 if (prule == NULL)
290 return ((patr_t *)0);
291
292 if (Debug > 1)
293 printf("Searching pattern rule for: %s \n", name);
294
295 for (; prule != NULL; prule = prule->p_next) {
296 register char *np;
297
298 /*
299 * XXX NeXT Step has a buggy strstr(); returns NULL if p == ""
300 */
301 p = (char *)prule->p_tgt_prefix;
302
303 if (*p != '\0' && strstr(name, p) != name)
304 continue; /* no matching prefix */
305
306 np = name + prule->p_tgt_pfxlen; /* strip matching prefix */
307
308 if ((p = rstr(np, (char *)prule->p_tgt_suffix)) == NULL)
309 continue;
310
311 #ifdef DEBUG
312 if (Debug > 1) {
313 register cmd_t *cmd;
314 register list_t *l;
315
316 printf("name: %s (%s %% %s):%s ", name,
317 prule->p_tgt_prefix, prule->p_tgt_suffix,
318 (prule->p_flags & PF_TERM) ? ":":"");
319
320 for (l = prule->p_list; l; l = l->l_next)
321 printf(" %s", l->l_obj->o_name);
322 printf("\n");
323
324 for (cmd = prule->p_cmd; cmd; cmd = cmd->c_next) {
325 printf("\t%s\n", cmd->c_line);
326 }
327 }
328 #endif
329 break;
330 }
331 return (prule);
332 }
333
334 /*
335 * Find pattern rule for 'obj' ... not yet ready.
336 */
337 LOCAL obj_t *
pattern_rule(obj)338 pattern_rule(obj)
339 obj_t *obj;
340 {
341 /*
342 * XXX Hack for now (cast to obj_t *), should return prule.
343 */
344 return ((obj_t *)_pattern_rule(Patrules, obj->o_name));
345 }
346
347 /*
348 * Find a POSIX suffix rule.
349 *
350 * Check if obj has a file name with a default dependency for the
351 * corresponding source and a rule to compile it.
352 */
353 LOCAL obj_t *
suffix_rule(obj,rtypep)354 suffix_rule(obj, rtypep)
355 register obj_t *obj;
356 int *rtypep;
357 {
358 list_t *l;
359 list_t *l2;
360 obj_t *o;
361 char *suffix;
362 char rulename[TYPICAL_NAMEMAX]; /* Space for two suffixes */
363 char *rule = rulename;
364 char *rp = NULL;
365 int rlen = sizeof (rulename);
366 BOOL found_suffix = FALSE;
367
368 if (Suffixes == NULL)
369 return ((obj_t *)0);
370
371 if (Debug > 1)
372 printf("Searching double suffix rule for: %s \n", obj->o_name);
373
374 for (l = Suffixes; l; l = l->l_next) {
375 suffix = l->l_obj->o_name;
376 if (rstr(obj->o_name, suffix)) { /* may be a suffix */
377
378 found_suffix = TRUE;
379
380 for (l2 = Suffixes; l2; l2 = l2->l_next) {
381 again:
382 if (snprintf(rule, rlen, "%s%s",
383 l2->l_obj->o_name,
384 suffix) >= rlen) {
385 /*
386 * Expand rule name space.
387 */
388 rlen = strlen(suffix) +
389 l2->l_obj->o_namelen + 16;
390 rule = rp = ___realloc(rp, rlen,
391 "suffix rule name");
392 goto again;
393 }
394 if ((o = objlook(rule, FALSE)) != NULL && o->o_type == COLON) {
395 *rtypep = RTYPE_DSUFFIX;
396 if (rp)
397 free(rp);
398 return (o);
399 }
400 }
401 }
402 }
403 if (rp)
404 free(rp);
405 if (found_suffix)
406 return ((obj_t *) 0);
407
408 if (Debug > 1)
409 printf("Searching single suffix rule for: %s \n", obj->o_name);
410
411 for (l2 = Suffixes; l2; l2 = l2->l_next) {
412 rule = l2->l_obj->o_name;
413 if ((o = objlook(rule, FALSE)) != NULL && o->o_type == COLON) {
414 *rtypep = RTYPE_SUFFIX;
415 return (o);
416 }
417 }
418 return ((obj_t *) 0);
419 }
420
421 LOCAL void
suffix_warn(obj)422 suffix_warn(obj)
423 obj_t *obj;
424 {
425 list_t *l;
426
427 if (obj->o_list == NULL)
428 return;
429
430 errmsgno(EX_BAD,
431 "WARNING: suffix rule '%s' has superfluous dependency: '",
432 obj->o_name);
433
434 for (l = obj->o_list; l; l = l->l_next) {
435 error("%s%s",
436 l->l_obj->o_name,
437 l->l_next?" ":"");
438 }
439 error("'.\n");
440 }
441
442 /*
443 * Find a simple suffix rule.
444 *
445 * Check if obj has a file name with a default dependency for the
446 * corresponding source and a rule to compile it.
447 */
448 LOCAL obj_t *
ssuffix_rule(obj)449 ssuffix_rule(obj)
450 register obj_t *obj;
451 {
452 register obj_t *rule;
453 char *ext;
454
455 if (!SSuffrules)
456 return ((obj_t *)0);
457
458 if (Debug > 1)
459 printf("Searching simple-suffix rule for: %s \n", obj->o_name);
460
461 ext = get_suffix(obj->o_name, (char *)0); /* Use '.' (dot) suffix only*/
462
463 if (ext[0] == '\0') {
464 if (obj->o_list == (list_t *)NULL) {
465 ext = "\"\""; /* obj has no suffix: use "" */
466 } else {
467 return ((obj_t *)NULL); /* obj has dependency list */
468 }
469 }
470 rule = ssufflook(ext, FALSE);
471 if (rule == (obj_t *)NULL || /* no default rules known */
472 rule->o_list == (list_t *)NULL || /* no source suffix list */
473 rule->o_cmd == (cmd_t *)NULL) /* no commands defined */
474 return ((obj_t *)NULL);
475 return (rule);
476 }
477
478 /*
479 * Check if a default rules exists for the target.
480 */
481 LOCAL obj_t *
default_rule(obj,rtypep)482 default_rule(obj, rtypep)
483 obj_t *obj;
484 int *rtypep;
485 {
486 obj_t *rule;
487
488 #ifdef NO_SLASH_IMPLICIT
489 if (strchr(obj->o_name, SLASH) != NULL) {
490 if (Debug > 3)
491 error("%s has slash, no implicit dependency searched.\n",
492 obj->o_name);
493 /*
494 * XXX We need to check if this is a good idea.
495 */
496 rule = (obj_t *)NULL;
497 }
498 #endif
499 rule = pattern_rule(obj);
500 if (rule) {
501 *rtypep = RTYPE_PATTERN;
502 return (rule);
503 }
504
505 rule = suffix_rule(obj, rtypep);
506 if (rule) {
507 return (rule);
508 }
509
510 rule = ssuffix_rule(obj);
511 if (rule) {
512 *rtypep = RTYPE_SSUFFIX;
513 return (rule);
514 }
515
516 /*
517 * XXX Must exits wichtig ??
518 */
519 *rtypep = RTYPE_DEFAULT;
520 rule = Deflt; /* .DEFAULT: */
521
522 if (rule == (obj_t *)NULL || rule->o_cmd == (cmd_t *)NULL)
523 return ((obj_t *)NULL);
524 return ((obj_t *)rule->o_cmd); /* XXX Hack for now, should return drule */
525 }
526
527 /*
528 * Return the nth element of a list.
529 */
530 EXPORT list_t *
list_nth(list,n)531 list_nth(list, n)
532 register list_t *list;
533 int n;
534 {
535 if (n < 0)
536 return ((list_t *)NULL);
537
538 for (; list; list = list->l_next)
539 if (--n < 0)
540 return (list);
541 return ((list_t *)NULL);
542 }
543
544 /*
545 * Create a new file name from name and the n'th directory in SearchList.
546 * SearchList lists sourcedirs before objdirs, starting with
547 * n = 0 for '.' and n = 1 for ObjDir.
548 *
549 * Returns:
550 * NULL No search path at "level"
551 * name level points to empty dirname
552 * path New path in space provided py "path"
553 * other Allocated new path
554 */
555 EXPORT char *
build_path(level,name,namelen,path,psize)556 build_path(level, name, namelen, path, psize)
557 int level;
558 char *name;
559 size_t namelen;
560 char *path;
561 size_t psize;
562 {
563 list_t *lp;
564 char *dir_name = (char *)NULL;
565 register int n = level;
566
567 if (n <= 1) {
568 if (level == OBJLEVEL) {
569 dir_name = ObjDir;
570 namelen += slashlen + ObjDirlen;
571 }
572 } else if (level != MAXLEVEL) {
573 if ((lp = list_nth(SearchList, n - 2)) == (list_t *)NULL)
574 return ((char *)NULL);
575 dir_name = lp->l_obj->o_name;
576 namelen += slashlen + lp->l_obj->o_namelen;
577 }
578 if (dir_name == (char *)NULL)
579 return (name);
580
581 if (namelen >= psize) {
582 psize = namelen + 1;
583 path = ___realloc(NULL, psize, "build path name");
584 }
585 n = snprintf(path, psize, "%s%s%s", dir_name, slash, name);
586 if (n >= psize)
587 etoolong("build path name", name);
588
589 return (path);
590 }
591
592 LOCAL void
etoolong(topic,name)593 etoolong(topic, name)
594 char *topic;
595 char *name;
596 {
597 comerrno(EX_BAD, "String too long, could not %s for '%s'.\n",
598 topic, name);
599 /* NOTREACHED */
600 }
601
602 /*
603 * The growable buffer (gbuf) defines a string with the following layout
604 * "xxxxxxxxxxxxxxxCxxxxxxxxxxxxxxx________"
605 * ^ ^ ^ ^
606 * | | | |
607 * gbuf textp sub_ptr gbufend
608 * textp points to a string that is currently been worked on,
609 * sub_ptr is the write pointer.
610 */
611 static char *sub_ptr = (char *)NULL;
612
613 /*
614 * Check whether a pointer is inside the growable buffer (gbuf)
615 *
616 * ison_gbuf(p) depends on the global variables "gbuf" and "gbufend"
617 * wason_gbuf(p) depens on local copies in "sb" and "sbe" that hold
618 * old values.
619 */
620 #define ison_gbuf(p) ((p) >= gbuf && (p) < gbufend)
621 #define wason_gbuf(p) ((p) >= sb && (p) < sbe)
622
623 LOCAL void
grant_gbuf(size)624 grant_gbuf(size)
625 int size;
626 {
627 /*
628 * In order to simplify code we add the space needed for the null byte.
629 */
630 size++;
631
632 while (sub_ptr + size >= gbufend)
633 sub_ptr = growgbuf(sub_ptr);
634 }
635
636 /*
637 * Put a string bounded by size into the growable buffer.
638 */
639 LOCAL void
sub_put(chunk,size)640 sub_put(chunk, size)
641 char *chunk;
642 int size;
643 {
644 char *sb = gbuf;
645 char *sbe = gbufend;
646
647 grant_gbuf(size);
648 if (sb != gbuf && wason_gbuf(chunk)) {
649 chunk = gbuf + (chunk - sb);
650 }
651 movebytes(chunk, sub_ptr, size);
652 sub_ptr += size;
653 *sub_ptr = '\0'; /* Null terminate the gbuf string */
654 }
655
656 /*
657 * Put a single character into the growable buffer.
658 */
659 LOCAL void
sub_c_put(c)660 sub_c_put(c)
661 int c;
662 {
663 grant_gbuf(1);
664 *sub_ptr++ = c & 0xFF;
665 *sub_ptr = '\0'; /* Null terminate the gbuf string */
666 }
667
668 /*
669 * Put a string bounded by strlen() into the growable buffer.
670 */
671 LOCAL void
sub_s_put(chunk)672 sub_s_put(chunk)
673 char *chunk;
674 {
675 sub_put(chunk, strlen(chunk));
676 }
677
678 /*
679 * Put one arg into the growable buffer.
680 *
681 * It target is nonzero, check in addition if the target
682 * depends on the date of the currently selected obj too.
683 *
684 * Return FALSE if no more list elements are available.
685 */
686 LOCAL BOOL
sub_arg(n,depends,target)687 sub_arg(n, depends, target)
688 int n;
689 list_t *depends;
690 obj_t *target;
691 {
692 register obj_t *obj;
693 char arg[TYPICAL_NAMEMAX];
694 char *argp;
695
696 if ((depends = list_nth(depends, n)) == (list_t *)NULL)
697 return (FALSE);
698
699 /*
700 * $0 is not available if no implicit source is present!
701 * Just skip it.
702 */
703 if ((obj =
704 depends->l_obj) == (obj_t *)NULL)
705 return (TRUE);
706
707 /*
708 * It the target does not yet exist, target->o_date is set
709 * to RECURSETIME. We need to make sure that newtime in the
710 * dependencies (here obj) is considered > target->o_date.
711 */
712 if (target != NULL &&
713 VALIDTIME(target->o_date) && target->o_date > obj->o_date) {
714 return (TRUE);
715 }
716 if ((argp = build_path(obj->o_level, obj->o_name, obj->o_namelen,
717 arg, sizeof (arg))) != NULL) {
718 sub_s_put(argp);
719 if (argp != obj->o_name && argp != arg) {
720 free(argp);
721 }
722 } else {
723 sub_s_put(obj->o_name);
724 }
725 return (TRUE);
726 }
727
728 /*
729 * Do macro substitution. Substitution is done in the growable buffer buf.
730 * The buffer is used as stack to allow recursive substitution.
731 */
732 EXPORT char *
substitute(cmd,obj,source,suffix)733 substitute(cmd, obj, source, suffix)
734 register char *cmd; /* The text to substitute */
735 obj_t *obj; /* The current target or NullObj */
736 obj_t *source; /* Implicit source */
737 char *suffix; /* Suffix for implicit source */
738 {
739 list_t depends;
740
741 found_make = FALSE; /* we did not expand $(MAKE) */
742 depends.l_obj = source; /* define implicit source $< */
743 depends.l_next = obj->o_list;
744
745 sub_ptr = gbuf; /* Growable string stack buf */
746 return (subst(cmd, obj, source, suffix, &depends));
747 }
748
749 static int depth = 0; /* Keep track of recursion */
750
751 /*
752 * Substitute macros.
753 */
754 /* source wird eigentlich nicht gebraucht */
755 LOCAL char *
subst(cmd,obj,source,suffix,depends)756 subst(cmd, obj, source, suffix, depends)
757 register char *cmd; /* The text to substitute */
758 obj_t *obj; /* The current target or NullObj */
759 obj_t *source; /* Implicit source */
760 char *suffix; /* Suffix for implicit source */
761 list_t *depends;
762 {
763 char *sp = sub_ptr;
764 char *sb = gbuf;
765 register char *p;
766 char name[2];
767
768 if (++depth > 100)
769 comerrno(EX_BAD, "Recursion in macro '%s'.\n", cmd);
770
771 name[1] = '\0'; /* Null terminate */
772 while ((p = strchr(cmd, '$')) != NULL) {
773 sub_put(cmd, p - cmd); /* Skip invariant test */
774 cmd = ++p; /* Point past $ */
775 switch (*cmd++) {
776
777 default:
778 if (chartype[*(Uchar *)p] & DYNCHAR) {
779 cmd = dynmac(p, obj, source, suffix, depends, FALSE);
780 continue;
781 }
782 name[0] = cmd[-1];
783 exp_name(name, obj, source, suffix, depends, Nullstr);
784 break;
785 case '\0':
786 *sub_ptr = '\0';
787 /*
788 * No need to update 'sb' as we exit here...
789 */
790 if (sb != gbuf) {
791 sp = gbuf + (sp - sb);
792 sb = gbuf;
793 }
794 comerrno(EX_BAD,
795 "Fatal error '$' at end of string '%s$'\n",
796 sp);
797 /* NOTREACHED */
798 case '$':
799 if (in_varassign)
800 sub_c_put('$'); /* Inhibit $$ expansion */
801 sub_c_put('$');
802 break;
803 case '(':
804 cmd = exp_var(')', cmd, obj, source, suffix, depends);
805 break;
806 case '{':
807 cmd = exp_var('}', cmd, obj, source, suffix, depends);
808 break;
809 }
810 }
811 sub_s_put(cmd);
812 *sub_ptr = '\0';
813 depth--;
814 if (sb != gbuf) {
815 sp = gbuf + (sp - sb);
816 sb = gbuf;
817 }
818 return (sp);
819 }
820
821 /*
822 * Substitute dynamic macros.
823 */
824 LOCAL char *
dynmac(cmd,obj,source,suffix,depends,domod)825 dynmac(cmd, obj, source, suffix, depends, domod)
826 char *cmd;
827 obj_t *obj;
828 obj_t *source;
829 char *suffix;
830 list_t *depends;
831 BOOL domod;
832 {
833 int num;
834 char _base[TYPICAL_NAMEMAX];
835 char *base = _base;
836 char *bp = NULL;
837 size_t blen = sizeof (_base);
838 char *sp = sub_ptr;
839 char *sb = gbuf;
840 char *sp1;
841 char *sb1;
842 register char *p = cmd;
843 char *ocmd;
844
845 switch (*cmd++) {
846
847 default:
848 return (cmd);
849
850 case '@':
851 if (obj->o_flags & F_DCOLON) /* Is a ::@ target */
852 sub_s_put(obj->o_node->o_name); /* $@ -> full target name */
853 else
854 sub_s_put(obj->o_name); /* $@ -> full target name */
855 break;
856 case '*':
857 if (obj->o_namelen >= blen) {
858 blen = obj->o_namelen + 1;
859 base = bp = ___realloc(bp, blen, "base name");
860 }
861 #ifdef used_to_be_in_former_versions
862 copy_base(filename(obj->o_name), base, blen, suffix);
863 #endif
864 if (ObjDir || SearchList) { /* May be removed in 2010 */
865
866 if (obj->o_name != filename(obj->o_name) &&
867 objlook("VPATH", FALSE) == NULL)
868 error(
869 "WARNING: Old: convert $* from '%s' -> '%s'\n",
870 obj->o_name, filename(obj->o_name));
871 }
872 if (obj->o_flags & F_PATRULE) {
873 patr_t *pr = (patr_t *)obj->o_node;
874
875 copy_base(obj->o_name+pr->p_tgt_pfxlen, base, blen,
876 pr->p_tgt_suffix);
877 } else {
878 copy_base(obj->o_name, base, blen, suffix);
879 if (suffix == NULL && !nowarn("$*"))
880 warn_implicit(obj, "$*", base);
881 }
882 sub_s_put(base); /* $* -> target name base */
883 break;
884 case '<':
885 if (depends->l_obj == NULL && !nowarn("$<"))
886 warn_implicit(obj, "$<", "");
887 sub_arg(0, depends, (obj_t *)0); /* $< -> implicit source */
888 break;
889 case '0': case '1': case '2': case '3': case '4':
890 case '5': case '6': case '7': case '8': case '9':
891 if (depends->l_obj == NULL && obj->o_type == 0 && !nowarn("$#"))
892 warn_displaced(obj, "$#", "");
893 cmd = astoi(p, &num); /* $1 -> first dependency */
894 sub_arg(num, depends, (obj_t *)0); /* $0 -> implicit source */
895 break;
896 case 'r':
897 /*
898 * $r0 -> all dependencies + implicit source
899 * $r1 -> all dependencies
900 */
901 if (depends->l_obj == NULL && obj->o_type == 0 &&
902 !nowarn("$r#"))
903 warn_displaced(obj, "$r#", "");
904 sp1 = sub_ptr;
905 sb1 = gbuf;
906 ocmd = cmd;
907 cmd = astoi(cmd, &num);
908 if (num < 0 || cmd == ocmd) {
909 /*
910 * sub_arg() deals with negative "num" values, but
911 * while (num++) could nearly take forever.
912 */
913 errmsgno(EX_BAD,
914 "WARNING: illegal substitution: '$r%s'\n", ocmd);
915 break;
916 }
917 while (sub_arg(num++, depends, (obj_t *)0)) {
918 if (sb1 != gbuf)
919 sp1 = gbuf + (sp1 - sb1);
920 if (sp1 != sub_ptr) /* Add only if nonempty arg */
921 sub_c_put(' ');
922 sp1 = sub_ptr;
923 sb1 = gbuf;
924 }
925 if (sb != gbuf)
926 sp = gbuf + (sp - sb);
927 sb = gbuf;
928 if (sp != sub_ptr && sub_ptr[-1] == ' ')
929 sub_ptr--; /* Kill last space */
930 break;
931 case '^':
932 sp1 = sub_ptr;
933 sb1 = gbuf;
934 num = 1; /* $^ -> all dependencies */
935 if (depends->l_obj != NULL) /* For implicit rules */
936 num = 0; /* include implicit source */
937 while (sub_arg(num++, depends, (obj_t *)0)) {
938 if (sb1 != gbuf)
939 sp1 = gbuf + (sp1 - sb1);
940 if (sp1 != sub_ptr) /* Add only if nonempty arg */
941 sub_c_put(' ');
942 sp1 = sub_ptr;
943 sb1 = gbuf;
944 }
945 if (sb != gbuf)
946 sp = gbuf + (sp - sb);
947 sb = gbuf;
948 if (sp != sub_ptr && sub_ptr[-1] == ' ')
949 sub_ptr--; /* Kill last space */
950 break;
951 case '?':
952 sp1 = sub_ptr;
953 sb1 = gbuf;
954 num = 1; /* $? -> outdated dependencies*/
955 if (depends->l_obj != NULL) /* For implicit rules */
956 num = 0; /* include implicit source */
957 while (sub_arg(num++, depends, obj)) {
958 if (sb1 != gbuf)
959 sp1 = gbuf + (sp1 - sb1);
960 if (sp1 != sub_ptr) /* Add only if nonempty arg */
961 sub_c_put(' ');
962 sp1 = sub_ptr;
963 sb1 = gbuf;
964 }
965 if (sb != gbuf)
966 sp = gbuf + (sp - sb);
967 sb = gbuf;
968 if (sp != sub_ptr && sub_ptr[-1] == ' ')
969 sub_ptr--; /* Kill last space */
970 break;
971 }
972 *sub_ptr = '\0';
973 if (bp)
974 free(bp);
975 if (!domod)
976 return (cmd);
977 if (sb != gbuf)
978 sp = gbuf + (sp - sb);
979 sb = gbuf;
980 if (*cmd == 'F') {
981 extr_filenames(sp); /* 'sp' must be in gbuf */
982 return (++cmd);
983 } else if (*cmd == 'D') {
984 extr_dirnames(sp); /* 'sp' must be in gbuf */
985 return (++cmd);
986 }
987 return (cmd);
988 }
989
990 LOCAL void
warn_implicit(obj,mac,exp)991 warn_implicit(obj, mac, exp)
992 obj_t *obj;
993 char *mac;
994 char *exp;
995 {
996 errmsgno(EX_BAD,
997 "WARNING: requesting implicit dynmac '%s' for explicit target '%s'\n",
998 mac, obj->o_name);
999 errmsgno(EX_BAD,
1000 "WARNING: expanding implicit dynmac '%s' to '%s'\n",
1001 mac, exp);
1002 errmsgno(EX_BAD,
1003 "WARNING: Current working directory: '%s', Makefile '%s'\n",
1004 curwdir(), MakeFileNames[obj->o_fileindex]);
1005 }
1006
1007 LOCAL void
warn_displaced(obj,mac,exp)1008 warn_displaced(obj, mac, exp)
1009 obj_t *obj;
1010 char *mac;
1011 char *exp;
1012 {
1013 errmsgno(EX_BAD,
1014 "WARNING: requesting dynmac '%s' in target definition\n",
1015 mac);
1016 errmsgno(EX_BAD,
1017 "WARNING: expanding implicit dynmac '%s' to '%s'\n",
1018 mac, exp);
1019 errmsgno(EX_BAD,
1020 "WARNING: Current working directory: '%s', Makefile '%s'\n",
1021 curwdir(), mfname);
1022 }
1023
1024 /*
1025 * Extract the filename parts from a string that contains a list of names.
1026 *
1027 * The parameter 'names' is expected to be on the 'growable buffer'.
1028 * If we ever need to use extr_filenames() otherwise, we need to add a boolean
1029 * parameter that tells extr_filenames() whether 'names' needs to be corrected
1030 * if 'gbuf' did change or not.
1031 */
1032 LOCAL void
extr_filenames(names)1033 extr_filenames(names)
1034 char *names;
1035 {
1036 char *p;
1037 char *np;
1038 char *s;
1039 char *sp;
1040 char *sb;
1041 char *sbe;
1042
1043 sp = ++sub_ptr;
1044 sb = gbuf;
1045 sbe = gbufend;
1046 /*
1047 * Make sure that gbuf has enough room for the copy.
1048 * Getting the size with sub_ptr - names only works if "names"
1049 * is on gbuf.
1050 */
1051 if (wason_gbuf(names))
1052 grant_gbuf(sub_ptr - names);
1053 if (sb != gbuf) {
1054 sp = gbuf + (sp - sb);
1055 if (wason_gbuf(names))
1056 names = gbuf + (names - sb);
1057 sb = gbuf;
1058 sbe = gbufend;
1059 }
1060 for (np = p = names, s = sp; np && *np; p = np) {
1061 np = strchr(np, ' ');
1062 if (np)
1063 *np++ = '\0';
1064 p = filename(p);
1065
1066 /*
1067 * Make sure the next string fits on gbuf.
1068 */
1069 sub_ptr = s; /* needed by grant_gbuf() */
1070 grant_gbuf(strlen(p)+2); /* add null-byte and space */
1071 if (sb != gbuf) {
1072 sp = gbuf + (sp - sb);
1073 s = gbuf + (s - sb);
1074 if (wason_gbuf(names)) {
1075 names = gbuf + (names - sb);
1076 p = gbuf + (p - sb);
1077 if (np)
1078 np = gbuf + (np - sb);
1079 }
1080 sb = gbuf;
1081 sbe = gbufend;
1082 }
1083
1084 while (*p)
1085 *s++ = *p++;
1086 if (np)
1087 *s++ = ' ';
1088 }
1089 *s = '\0';
1090 /*
1091 * Now copy down from upper part of gbuf.
1092 */
1093 for (s = names, p = sp; *p; )
1094 *s++ = *p++;
1095 *s = '\0';
1096 if (wason_gbuf(names))
1097 sub_ptr = s;
1098 }
1099
1100 /*
1101 * Extract the directory parts from a string that contains a list of names.
1102 *
1103 * The parameter 'names' is expected to be on the 'growable buffer'.
1104 * If we ever need to use extr_dirnames() otherwise, we need to add a boolean
1105 * parameter that tells extr_dirnames() whether 'names' needs to be corrected
1106 * if 'gbuf' did change or not.
1107 */
1108 LOCAL void
extr_dirnames(names)1109 extr_dirnames(names)
1110 char *names;
1111 {
1112 char *p;
1113 char *np;
1114 char *s;
1115 char *sp;
1116 char *sb;
1117 char *sbe;
1118 char _base[TYPICAL_NAMEMAX];
1119 char *base = _base;
1120 char *bp = NULL;
1121 size_t blen = sizeof (_base);
1122 size_t len;
1123
1124 sp = ++sub_ptr;
1125 sb = gbuf;
1126 sbe = gbufend;
1127 /*
1128 * Make sure that gbuf has enough room for the copy.
1129 * Getting the size with sub_ptr - names only works if "names"
1130 * is on gbuf.
1131 */
1132 if (wason_gbuf(names))
1133 grant_gbuf(sub_ptr - names);
1134 if (sb != gbuf) {
1135 sp = gbuf + (sp - sb);
1136 if (wason_gbuf(names))
1137 names = gbuf + (names - sb);
1138 sb = gbuf;
1139 sbe = gbufend;
1140 }
1141 for (np = p = names, s = sp; np && *np; p = np) {
1142 np = strchr(np, ' ');
1143 if (np) {
1144 *np++ = '\0';
1145 len = np - p;
1146 } else {
1147 len = strlen(p) + 1;
1148 }
1149
1150 /*
1151 * Make sure the next string fits on gbuf.
1152 */
1153 sub_ptr = s; /* needed by grant_gbuf() */
1154 grant_gbuf(len+1); /* add null-byte and space */
1155 if (sb != gbuf) {
1156 sp = gbuf + (sp - sb);
1157 s = gbuf + (s - sb);
1158 if (wason_gbuf(names)) {
1159 names = gbuf + (names - sb);
1160 p = gbuf + (p - sb);
1161 if (np)
1162 np = gbuf + (np - sb);
1163 }
1164 sb = gbuf;
1165 sbe = gbufend;
1166 }
1167 if (len > blen) {
1168 blen = len + 32; /* Add some reserve */
1169 base = bp = ___realloc(bp, blen, "dir base");
1170 }
1171 copy_dir(p, base, blen);
1172 p = base;
1173 while (*p)
1174 *s++ = *p++;
1175 if (np)
1176 *s++ = ' ';
1177 }
1178 *s = '\0';
1179 if (bp)
1180 free(bp);
1181 /*
1182 * Now copy down from upper part of gbuf.
1183 */
1184 for (s = names, p = sp; *p; )
1185 *s++ = *p++;
1186 *s = '\0';
1187 if (wason_gbuf(names))
1188 sub_ptr = s;
1189 }
1190
1191 #define white(c) ((c) == ' ' || (c) == '\t')
1192
1193 /*
1194 * Expand a macro.
1195 * The input text may either be a macro name or a suffix/pattern rule.
1196 * As the replacement may be a suffix rule or a pattern rule too,
1197 * we first must get the basic name the macro refers to.
1198 */
1199 #ifdef PROTOTYPES
1200 LOCAL char *
exp_var(register char end,char * cmd,obj_t * obj,obj_t * source,char * suffix,list_t * depends)1201 exp_var(
1202 register char end, /* End character, ')' or '}' */
1203 char *cmd, /* Macro name or macro rule */
1204 obj_t *obj, /* The current target or NullObj */
1205 obj_t *source, /* Implicit source */
1206 char *suffix, /* Suffix for implicit source */
1207 list_t *depends)
1208 #else
1209 LOCAL char *
1210 exp_var(end, cmd, obj, source, suffix, depends)
1211 register char end;
1212 char *cmd;
1213 obj_t *obj;
1214 obj_t *source;
1215 char *suffix;
1216 list_t *depends;
1217 #endif
1218 {
1219 char _name[TYPICAL_NAMEMAX];
1220 char *name = _name;
1221 char *nep = &_name[sizeof (_name) - 2];
1222 char *np = NULL;
1223 size_t nlen = sizeof (_name);
1224 char _pat[TYPICAL_NAMEMAX];
1225 char *pat = _pat;
1226 char *pep = &_pat[sizeof (_pat) - 2];
1227 char *pp = NULL;
1228 size_t plen = sizeof (_pat);
1229 register char beg = cmd[-1];
1230 register char *s = cmd;
1231 register char *rname = name;
1232 register char ch;
1233 register int nestlevel = 0;
1234 BOOL funccall = FALSE;
1235
1236 /*error("exp_var(end: '%c' cmd: '%.50s') [beg: '%c']\n", end, cmd, beg);*/
1237 pat[0] = '\0';
1238 while ((ch = *s) != '\0') {
1239 if (nestlevel == 0) {
1240 /*
1241 * Only stop looking for the end of the macro name in
1242 * case we are at the initial nestlevel. Collect as
1243 * much as possible for a recursive macro name
1244 * expansion below.
1245 */
1246 if (ch == ':' || white(ch))
1247 break;
1248 }
1249 if (ch == beg)
1250 nestlevel++;
1251 if (ch == end)
1252 nestlevel--;
1253 if (nestlevel < 0) {
1254 /*printf("name: %s\n", name);*/
1255 break;
1256 }
1257 if (rname >= nep) {
1258 nlen += TYPICAL_NAMEMAX*2;
1259 name = ___realloc(np, nlen, "macro name");
1260 if (np == NULL) {
1261 /*
1262 * Copy old content.
1263 * First null-terminate the current buffer.
1264 * We did originally use sizeof (_name) for
1265 * strlcpy(), but clang likes to be foxier
1266 * than it is and writes a warning.
1267 */
1268 *rname = '\0';
1269 strlcpy(name, _name, nlen);
1270 rname = name + (rname - _name);
1271 } else {
1272 rname = name + (rname - np);
1273 }
1274 np = name;
1275 nep = &name[nlen - 2];
1276 }
1277 *rname++ = *s++;
1278 }
1279 *rname = '\0';
1280
1281 if (ch == ' ')
1282 funccall = TRUE;
1283
1284 if (*s != end && *s != ':' && *s != ' ') {
1285 comerrno(EX_BAD, "Missing '%c' in macro call '%s'\n", end, name);
1286 /* NOTREACHED */
1287 /* return (cmd);*/
1288 }
1289
1290 if (*s == ':' || white(*s)) {
1291 rname = pat;
1292 if (funccall) {
1293 while (*s && white(*s))
1294 s++;
1295 } else {
1296 s++;
1297 }
1298 nestlevel = 0; /* Paranoia */
1299 while ((ch = *s) != '\0') {
1300 if (ch == beg)
1301 nestlevel++;
1302 if (ch == end)
1303 nestlevel--;
1304 if (nestlevel < 0) {
1305 /*printf("name: %s\n", name);*/
1306 break;
1307 }
1308 if (rname >= pep) {
1309 plen += TYPICAL_NAMEMAX*2;
1310 pat = ___realloc(pp, plen, "macro pattern");
1311 if (pp == NULL) {
1312 /*
1313 * Copy old content.
1314 * First null-terminate the
1315 * current buffer.
1316 * We did originally use sizeof (_pat)
1317 * for strlcpy(), but clang likes to be
1318 * foxier than it is and writes a
1319 * warning.
1320 */
1321 *rname = '\0';
1322 strlcpy(pat, _pat, plen);
1323 rname = pat + (rname - _pat);
1324 } else {
1325 rname = pat + (rname - pp);
1326 }
1327 pp = pat;
1328 pep = &pat[plen - 2];
1329 }
1330 *rname++ = *s++;
1331 }
1332 *rname = '\0';
1333
1334 if (nestlevel >= 0)
1335 comerrno(EX_BAD, "Missing '%c' in macro call '%s%c%s'\n",
1336 end, name, funccall?' ':':', pat);
1337 }
1338 if (*s)
1339 s++;
1340
1341 if (name[0] == 'M' && streql(name, "MAKE"))
1342 found_make = TRUE;
1343
1344 /*
1345 * If the name of the macro contains a '$', recursively expand it.
1346 * We need to check if we should rather expand anything between the
1347 * brackets (e.g {...}) however, this may fail to expand long lists.
1348 * See also comment in exp_name() regarding USE_SUBPAT
1349 */
1350 if (strchr(name, '$')) {
1351 char *sp = sub_ptr;
1352 char *sb = gbuf;
1353 char *s2;
1354
1355 *sub_ptr++ = '\0';
1356
1357 s2 = subst(name, obj, source, suffix, depends);
1358 if (sb != gbuf)
1359 sp = gbuf + (sp - sb);
1360 sb = gbuf;
1361 sub_ptr = sp;
1362 if (*s2) {
1363 if (strlcpy(name, s2, nlen) >= nlen) {
1364 nlen = strlen(s2) +1;
1365 name = np = ___realloc(np, nlen, "macro name");
1366 if (strlcpy(name, s2, nlen) >= nlen)
1367 etoolong("copy macro content", s2);
1368 }
1369 }
1370 }
1371
1372 if (funccall) {
1373 /*
1374 * GNU type macro functions will go here.
1375 */
1376 goto out;
1377 }
1378 /*printf("name: '%s' pat: '%s'\n", name, pat);*/
1379 exp_name(name, obj, source, suffix, depends, pat);
1380 out:
1381 if (np)
1382 free(np);
1383 if (pp)
1384 free(pp);
1385 return (s);
1386 }
1387
1388 /*
1389 * Check if s1 ends in strings s2
1390 */
1391 LOCAL char *
rstr(s1,s2)1392 rstr(s1, s2)
1393 char *s1;
1394 char *s2;
1395 {
1396 int l1;
1397 int l2;
1398
1399 l1 = strlen(s1);
1400 l2 = strlen(s2);
1401 if (l2 > l1)
1402 return ((char *)NULL);
1403
1404 if (streql(&s1[l1 - l2], s2))
1405 return (&s1[l1 - l2]);
1406 return ((char *)NULL);
1407 }
1408
1409 /*
1410 * Substitute a pattern:
1411 * 1) select the part of 'name' that is surrounded by f1 & f2
1412 * 2a) output the selected part from 'name' and t1 (suffix)
1413 * 2b) output t2 -
1414 * if t2 contains '%' substitute the selected part of 'name'
1415 *
1416 * The parameter 'name' is expected to be on the 'growable buffer'.
1417 * If we ever need to use patsub() otherwise, we need to add a boolean
1418 * parameter that tells patsub() whether 'name' needs to be corrected
1419 * if 'gbuf' did change or not.
1420 */
1421 LOCAL BOOL
patsub(name,f1,f2,t1,t2)1422 patsub(name, f1, f2, t1, t2)
1423 char *name;
1424 char *f1;
1425 char *f2;
1426 char *t1;
1427 char *t2;
1428 {
1429 int l;
1430 char *p;
1431 char *sb = gbuf;
1432 char *sbe = gbufend;
1433
1434 /* printf("name: '%s' f1: '%s' f2: '%s' t1: '%s' t2: '%s'\n", name, f1, f2, t1, t2);*/
1435
1436 /*
1437 * Make sure $(VAR:%=PREFIX/%) does not expand if $VAR is empty.
1438 */
1439 if (*name == '\0' && *f1 == '\0' && *f2 == '\0')
1440 return (FALSE);
1441 /*
1442 * XXX NeXT Step has a buggy strstr(); returns NULL if f1 == ""
1443 * XXX f1 is guaranteed to be != NULL
1444 */
1445 if (*f1 != '\0' && strstr(name, f1) != name)
1446 return (FALSE); /* no matching prefix */
1447
1448 name += strlen(f1); /* strip matching prefix */
1449
1450 if ((p = rstr(name, f2)) == NULL)
1451 return (FALSE); /* no matching suffix */
1452
1453 l = p - name;
1454 if (t1 != NULL) { /* This is a suffix rule */
1455 sub_put(name, l); /* 'name' maybe on gbuf... */
1456 if (sb != gbuf) {
1457 if (wason_gbuf(name))
1458 name = gbuf + (name - sb);
1459 sb = gbuf;
1460 sbe = gbufend;
1461 }
1462 p = t1;
1463 } else { /* This is a pattern rule */
1464 p = t2;
1465 }
1466 while (*p) {
1467 /*
1468 * Expand '%' only in case of a patten macro expansion.
1469 */
1470 if (*p == '%' && t2 != NULL) {
1471 p++;
1472 sub_put(name, l); /* 'name' is on gbuf... */
1473 } else {
1474 sub_put(p++, 1);
1475 }
1476 if (sb != gbuf) {
1477 if (wason_gbuf(name))
1478 name = gbuf + (name - sb);
1479 if (wason_gbuf(p))
1480 p = gbuf + (p - sb);
1481 sb = gbuf;
1482 sbe = gbufend;
1483 }
1484 }
1485 return (TRUE);
1486 }
1487
1488 #ifdef needed
1489 /*
1490 * Do pattern substitution in a macro that may only contain one word.
1491 *
1492 * The parameter 'name' is expected to be on the 'growable buffer'.
1493 * If we ever need to use patssub() otherwise, we need to add a boolean
1494 * parameter that tells patssub() whether 'name' needs to be corrected
1495 * if 'gbuf' did change or not.
1496 */
1497 LOCAL void
patssub(name,f1,f2,t1,t2)1498 patssub(name, f1, f2, t1, t2)
1499 char *name;
1500 char *f1;
1501 char *f2;
1502 char *t1;
1503 char *t2;
1504 {
1505 char *osp = name;
1506 char *sp = sub_ptr;
1507 char *sb = gbuf;
1508
1509 *sub_ptr++ = '\0';
1510
1511 if (!patsub(name, f1, f2, t1, t2)) {
1512 sub_ptr = sp;
1513 return;
1514 }
1515 *sub_ptr = '\0';
1516 sub_ptr = osp;
1517 if (sb != gbuf) {
1518 sp = gbuf + (sp - sb);
1519 sub_ptr = gbuf + (sub_ptr - sb);
1520 sb = gbuf;
1521 }
1522 sub_s_put(&sp[1]);
1523 }
1524 #endif
1525
1526 /*
1527 * Do pattern substitution in a macro that may contain multiple words.
1528 * Substitution is applied separately to each word.
1529 *
1530 * The parameter 'name' is expected to be on the 'growable buffer'.
1531 * If we ever need to use patmsub() otherwise, we need to add a boolean
1532 * parameter that tells patmsub() whether 'name' needs to be corrected
1533 * if 'gbuf' did change or not.
1534 */
1535 LOCAL void
patmsub(name,f1,f2,t1,t2)1536 patmsub(name, f1, f2, t1, t2)
1537 char *name;
1538 char *f1;
1539 char *f2;
1540 char *t1;
1541 char *t2;
1542 {
1543 char *osp = name;
1544 char *sp = sub_ptr;
1545 char *sb = gbuf;
1546 char *sbe = gbufend;
1547 char *b;
1548 char c;
1549
1550 *sub_ptr++ = '\0';
1551
1552 do {
1553 b = name;
1554 while (*b != '\0' && !white(*b))
1555 b++;
1556 if ((c = *b) != '\0')
1557 *b++ = '\0';
1558 else
1559 b = NULL;
1560
1561 /*error("name '%s'\n", name);*/
1562 if (!patsub(name, f1, f2, t1, t2)) {
1563 sub_s_put(name); /* 'name' maybe on gbuf... */
1564 }
1565 if (sb != gbuf) {
1566 sp = gbuf + (sp - sb);
1567 osp = gbuf + (osp - sb);
1568 if (wason_gbuf(name)) {
1569 name = gbuf + (name - sb);
1570 if (b != NULL)
1571 b = gbuf + (b - sb);
1572 }
1573 sb = gbuf;
1574 sbe = gbufend;
1575 }
1576
1577 if (b) {
1578 sub_c_put(c);
1579 if (sb != gbuf) {
1580 sp = gbuf + (sp - sb);
1581 osp = gbuf + (osp - sb);
1582 if (wason_gbuf(b))
1583 b = gbuf + (b - sb);
1584 sb = gbuf;
1585 sbe = gbufend;
1586 }
1587 while (*b != '\0' && white(*b)) {
1588 sub_put(b++, 1);
1589 if (sb != gbuf) {
1590 sp = gbuf + (sp - sb);
1591 osp = gbuf + (osp - sb);
1592 if (wason_gbuf(b))
1593 b = gbuf + (b - sb);
1594 sb = gbuf;
1595 sbe = gbufend;
1596 }
1597 }
1598 }
1599 name = b;
1600 } while (b);
1601 *sub_ptr = '\0';
1602 sub_ptr = osp;
1603 if (sb != gbuf) {
1604 sp = gbuf + (sp - sb);
1605 sub_ptr = gbuf + (sub_ptr - sb);
1606 sb = gbuf;
1607 sbe = gbufend;
1608 }
1609 sub_s_put(&sp[1]);
1610 }
1611
1612 /*
1613 * Parse a pattern and divide pattern into parts.
1614 *
1615 * Check if this is a suffix replacement macro or a pattern repacement macro.
1616 *
1617 * If this is a suffix macro (suf1=suf2), tp2 will point to a NULL pointer,
1618 * if this is a pattern macro (pref1%suf1=pref1%suf2) tp1 will point to NULL.
1619 */
1620 LOCAL void
parsepat(pat,fp1,fp2,tp1,tp2)1621 parsepat(pat, fp1, fp2, tp1, tp2)
1622 char *pat;
1623 char **fp1;
1624 char **fp2;
1625 char **tp1;
1626 char **tp2;
1627 {
1628 char *f1;
1629 char *f2;
1630 char *t1;
1631 char *t2;
1632
1633 t1 = strchr(pat, '=');
1634 if (t1 == NULL)
1635 comerrno(EX_BAD, "'=' missing in macro substitution.\n");
1636 *t1++ = '\0';
1637
1638 f2 = strchr(pat, '%'); /* Find '%' in from patttern */
1639 if (f2 != NULL) {
1640 *f2++ = '\0';
1641 f1 = pat;
1642 } else {
1643 f2 = pat;
1644 f1 = Nullstr;
1645 }
1646 if (f1 == pat) { /* This is a pattern macro */
1647 t2 = t1;
1648 t1 = NULL;
1649 } else { /* This is a suffix macro */
1650 t2 = NULL;
1651 }
1652 *fp1 = f1;
1653 *fp2 = f2;
1654 *tp1 = t1;
1655 *tp2 = t2;
1656 /* printf("f1: '%s' f2: '%s' t1: '%s' t2: '%s'\n", f1, f2, t1, t2);*/
1657 }
1658
1659 #if !defined(popen) && !defined(HAVE_POPEN) && defined(HAVE__POPEN)
1660 # define popen _popen
1661 # define HAVE_POPEN
1662 #endif
1663 #if !defined(pclose) && !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
1664 # define pclose _pclose
1665 # define HAVE_PCLOSE
1666 #endif
1667 /*
1668 * Call shell command and capture the outpout in the growable buffer.
1669 */
1670 EXPORT char *
shout(cmd)1671 shout(cmd)
1672 char *cmd;
1673 {
1674 FILE *f;
1675 int c;
1676 char *sptr = sub_ptr;
1677 char *sbuf = gbuf;
1678
1679 /*
1680 * getshvar() does not know about sub_ptr and thus cannot initialize
1681 * it. As it first overwrites gbuf and then calls us with name == gbuf,
1682 * we can do the needed initialization for sub_c_put() here.
1683 */
1684 if (sub_ptr == NULL || cmd == gbuf)
1685 sptr = sub_ptr = gbuf;
1686
1687 f = popen(cmd, "r");
1688 if (f == NULL)
1689 comerr("Cannot popen '%s'.\n", cmd);
1690
1691 while ((c = getc(f)) != EOF) {
1692 if (c == '\t' || c == '\n')
1693 c = ' ';
1694 sub_c_put(c);
1695 }
1696 if ((c = pclose(f)) != 0)
1697 comerr("Shell returns %d from command line.\n", c);
1698 *sub_ptr = '\0';
1699 if (sbuf != gbuf)
1700 sptr = gbuf + (sptr - sbuf);
1701 sbuf = gbuf;
1702 return (sptr);
1703 }
1704
1705 /*
1706 * Substitute a shell command output
1707 */
1708 LOCAL char *
shsub(l,obj,source,suffix,depends)1709 shsub(l, obj, source, suffix, depends)
1710 register list_t *l;
1711 obj_t *obj;
1712 obj_t *source;
1713 char *suffix;
1714 list_t *depends;
1715 {
1716 char *sptr = sub_ptr;
1717 char *sbuf = gbuf;
1718
1719 if (sub_ptr == NULL)
1720 sptr = sub_ptr = gbuf;
1721
1722 for (;;) {
1723 subst(l->l_obj->o_name, obj, source, suffix, depends);
1724 if ((l = l->l_next) != NULL)
1725 sub_c_put(' ');
1726 else
1727 break;
1728 }
1729 *sub_ptr = '\0';
1730 if (sbuf != gbuf)
1731 sptr = gbuf + (sptr - sbuf);
1732 sub_ptr = sptr;
1733 sbuf = gbuf;
1734
1735 return (shout(sptr));
1736 }
1737
1738 /*
1739 * Expand a macro for which the name is explicitly known.
1740 */
1741 LOCAL void
exp_name(name,obj,source,suffix,depends,pat)1742 exp_name(name, obj, source, suffix, depends, pat)
1743 char *name; /* The macro name */
1744 obj_t *obj; /* The current target or NullObj */
1745 obj_t *source; /* Implicit source */
1746 char *suffix; /* Suffix for implicit source */
1747 list_t *depends;
1748 char *pat; /* Replacement pattern */
1749 {
1750 register list_t *l = NULL;
1751 obj_t *o = NULL;
1752 BOOL ispat;
1753 char epat[TYPICAL_NAMEMAX*2]; /* Was NAMEMAX */
1754 char *epa = epat;
1755 char *ep = NULL;
1756 char *f1, *f2;
1757 char *t1, *t2;
1758 char *sp;
1759 char *sb;
1760
1761 if ((chartype[*(Uchar *)name] & DYNCHAR) == 0 ||
1762 (*name == 'r' && (chartype[((Uchar *)name)[1]] & NUMBER) == 0)) {
1763 /*
1764 * Allow dynamic macros to appear in $() or ${} too.
1765 */
1766 o = objlook(name, FALSE);
1767 if (o == (obj_t *)NULL) {
1768 /*
1769 * Check for $O -> ObjDir (pseudo dyn) macro.
1770 * $O is a pseudo dyn macro as we allow to overwrite it
1771 * in order get full POSIX make compliance.
1772 */
1773 if (name[0] == 'O' && name[1] == '\0') {
1774 /*
1775 * If $O has not been overwritten, use ObjDir.
1776 */
1777 if (ObjDir == NULL) {
1778 sp = sub_ptr;
1779 sb = gbuf;
1780 sub_c_put('.');
1781 *sub_ptr = '\0';
1782 if (pat[0] != '\0') {
1783 char *p;
1784 p = subst(pat, obj, source,
1785 suffix, depends);
1786 if ((sub_ptr - p) >=
1787 sizeof (epat)) {
1788 epa = ep = ___realloc(ep,
1789 sub_ptr - p + 1,
1790 "pattern content");
1791 }
1792 strcpy(epa, p);
1793 /*
1794 * Free space from subpat()
1795 */
1796 sub_ptr = p;
1797 parsepat(epa, &f1, &f2, &t1, &t2);
1798 if (sb != gbuf)
1799 sp = gbuf + (sp - sb);
1800 sb = gbuf;
1801 /*
1802 * patmsub() expects first
1803 * parameter to be in 'gbuf'
1804 */
1805 patmsub(sp, f1, f2, t1, t2);
1806 }
1807 } else {
1808 exp_name(".OBJDIR", obj, source, suffix, depends, pat);
1809 }
1810 }
1811 if (ep)
1812 free(ep);
1813 return;
1814 }
1815 if ((l = o->o_list) == (list_t *)NULL)
1816 return;
1817 }
1818 if (streql(pat, "sh")) {
1819 /*
1820 * Allow SunPRO make compatible shell expansion as $(NAME:sh)
1821 */
1822 if (o != NULL) {
1823 shsub(o->o_list, obj, source, suffix, depends);
1824 } else {
1825 /*
1826 * Process dynamic macros that appear in $() or ${}.
1827 */
1828 sp = sub_ptr;
1829 sb = gbuf;
1830 dynmac(name, obj, source, suffix, depends, TRUE);
1831 /*error("expanded1: '%s'\n", sp);*/
1832 if (sb != gbuf)
1833 sp = gbuf + (sp - sb);
1834 sub_ptr = sp;
1835 sb = gbuf;
1836 (void) shout(sp);
1837 }
1838 return;
1839 }
1840 ispat = pat[0] != '\0';
1841 if (ispat) {
1842 char *p = subst(pat, obj, source, suffix, depends);
1843
1844 if ((sub_ptr - p) >= sizeof (epat)) {
1845 epa = ep = ___realloc(ep, sub_ptr - p + 1,
1846 "pattern content");
1847 }
1848 strcpy(epa, p);
1849 sub_ptr = p; /* Free space from subpat() */
1850 parsepat(epa, &f1, &f2, &t1, &t2);
1851 }
1852
1853 if (o == NULL) {
1854 /*
1855 * Process dynamic macros that appear in $() or ${}.
1856 */
1857 sp = sub_ptr;
1858 sb = gbuf;
1859 dynmac(name, obj, source, suffix, depends, TRUE);
1860 /*error("expanded1: '%s'\n", sp);*/
1861 if (ispat) {
1862 if (sb != gbuf)
1863 sp = gbuf + (sp - sb);
1864 sb = gbuf;
1865 /*
1866 * patmsub() expects first parameter to be in 'gbuf'
1867 */
1868 patmsub(sp, f1, f2, t1, t2);
1869 /*error("expanded2: '%s'\n", sp);*/
1870 }
1871 if (ep)
1872 free(ep);
1873 return;
1874 }
1875 for (;;) {
1876 /*
1877 * Since the default value for in_parser is FALSE and since
1878 * this is only changed at parse time, the use of the
1879 * global variable in_parser does not cause problems with
1880 * parallel execution at update time.
1881 *
1882 * ::= type macros are only expanded from inside the parser
1883 * and not expanded for the parser when in_parser is <= 0.
1884 * in_parser == -1 when this is the left or right side of
1885 * a : dependency.
1886 */
1887 if (o->o_type == GNU_ASSIGN && in_parser != TRUE) {
1888 sub_s_put(l->l_obj->o_name);
1889 } else {
1890 sp = sub_ptr;
1891 sb = gbuf;
1892 subst(l->l_obj->o_name, obj, source, suffix, depends);
1893 /*error("expanded1: '%s'\n", sp);*/
1894 if (ispat) {
1895 if (sb != gbuf)
1896 sp = gbuf + (sp - sb);
1897 sb = gbuf;
1898 /*
1899 * patmsub() expects first parameter to be in 'gbuf'
1900 */
1901 patmsub(sp, f1, f2, t1, t2);
1902 /*error("expanded2: '%s'\n", sp);*/
1903 }
1904 }
1905 if ((l = l->l_next) != NULL)
1906 sub_c_put(' ');
1907 else
1908 break;
1909 /*error("expanded3: '%s'\n", sp);*/
1910 }
1911 if (ep)
1912 free(ep);
1913 }
1914
1915 /*
1916 * Copy over the time and level from the intermediate :: object
1917 * to the main base object.
1918 */
1919 LOCAL void
dcolon_time(obj)1920 dcolon_time(obj)
1921 register obj_t *obj;
1922 {
1923 #ifdef __really_needed__
1924 if (VALIDTIME(obj->o_node->o_date))
1925 return;
1926 #endif
1927
1928 #ifdef Do_not_use_o_node
1929 name = strchr(obj->o_name, '@');
1930 if (name == NULL)
1931 return;
1932 o = objlook(++name, FALSE);
1933 #endif
1934
1935 obj->o_node->o_date = obj->o_date;
1936 obj->o_node->o_level = obj->o_level;
1937 }
1938
1939 /*
1940 * Find a file in .SEARCHLIST
1941 * .SEARCHLIST is a list of of src/obj dir pairs
1942 * Before .SEARCHLIST is searched, look for "." and .OBJDIR
1943 */
1944 LOCAL date_t
searchobj(obj,maxlevel,mode)1945 searchobj(obj, maxlevel, mode)
1946 register obj_t *obj;
1947 int maxlevel;
1948 int mode;
1949 {
1950 char name[TYPICAL_NAMEMAX];
1951 char *namep = NULL;
1952 char *oname = NULL;
1953 register int level = MAXLEVEL;
1954 date_t filedate;
1955
1956
1957 name[0] = '\0'; /* for clean debug print */
1958
1959 if ((maxlevel & 1) == 0) /* Source has lower level */
1960 maxlevel += 1; /* include OBJ directory level */
1961
1962 if (obj->o_date == PHONYTIME) {
1963 filedate = PHONYTIME;
1964
1965 } else if (isphony(obj)) {
1966 obj->o_date = filedate = PHONYTIME;
1967
1968 #ifdef NO_SEARCHOBJ_SLASH_IN_NAME
1969 /*
1970 * Earlier versions of smake did allow slashes anywhere in path names
1971 * and being searched for. A common practice in VPATH usage requires us
1972 * to allow slashes inside a path name again.
1973 */
1974 } else if (strchr(obj->o_name, SLASH) != (char *)NULL) {
1975 #else
1976 /*
1977 * If an object file name starts with a slash, is is an absolute path
1978 * name so do not search.
1979 */
1980 } else if (obj->o_name[0] == SLASH) {
1981 #endif
1982 /*
1983 * Do not search for pathnames.
1984 */
1985 filedate = gftime(obj->o_name);
1986 } else for (level = 0, filedate = NOTIME; level <= maxlevel; level++) {
1987 if (level & 1 ? mode & SOBJ : mode & SSRC) {
1988 oname = obj->o_name;
1989 if ((obj->o_flags & F_DCOLON) != 0) {
1990 oname = obj->o_node->o_name;
1991 if ((namep = build_path(level,
1992 obj->o_node->o_name,
1993 obj->o_node->o_namelen,
1994 name, sizeof (name))) == NULL)
1995 break;
1996 } else if ((namep = build_path(level, obj->o_name,
1997 obj->o_namelen,
1998 name, sizeof (name))) == NULL)
1999 break;
2000 if ((filedate = gftime(namep)) != NOTIME)
2001 break;
2002 if (level == WDLEVEL && ObjDir == (char *)NULL) {
2003 /*
2004 * Working dir has just been searched don't
2005 * look again if no .OBJDIR has been defined.
2006 */
2007 level++;
2008 }
2009 }
2010 }
2011 if (filedate == NOTIME || filedate == PHONYTIME)
2012 level = MAXLEVEL;
2013
2014 obj->o_date = filedate;
2015 obj->o_level = level;
2016 if (obj->o_flags & F_DCOLON)
2017 dcolon_time(obj); /* Copy from intermediate to main obj */
2018
2019 if (Debug > 2) {
2020 error("search(%s, %d, %s) = %s %s %d\n",
2021 obj->o_name, maxlevel, searchtype(mode),
2022 prtime(filedate), namep, level);
2023 }
2024 if (namep != NULL && namep != oname && namep != name) {
2025 free(namep);
2026 }
2027 if (filedate == PHONYTIME) {
2028 return (NOTIME);
2029 }
2030 return (filedate);
2031 }
2032
2033 /*
2034 * Find a source file for a given object file, use pattern matching rules.
2035 */
2036 LOCAL obj_t *
patr_src(name,prule,rtypep,suffixp,pcmd,dlev)2037 patr_src(name, prule, rtypep, suffixp, pcmd, dlev)
2038 char *name;
2039 patr_t *prule;
2040 int *rtypep;
2041 char **suffixp;
2042 cmd_t **pcmd;
2043 int dlev;
2044 {
2045 char *xname;
2046 register obj_t *source;
2047 obj_t newsource;
2048 char _sourcename[TYPICAL_NAMEMAX];
2049 char *sourcename = _sourcename;
2050 char *sp = NULL;
2051 size_t slen = sizeof (_sourcename);
2052 char _pat[TYPICAL_NAMEMAX];
2053 char *pat = _pat;
2054 char *pp = NULL;
2055 size_t plen = sizeof (_pat);
2056 char *p;
2057 size_t len;
2058 obj_t *obj;
2059
2060 if (prule->p_list == NULL) {
2061 source = NullObj;
2062 goto found;
2063 }
2064 if ((prule->p_list->l_obj->o_flags & F_PERCENT) == 0)
2065 errmsgno(EX_BAD,
2066 "WARNING: Non-percent pattern dependencies not yet supported.\n");
2067 if (prule->p_list->l_next)
2068 errmsgno(EX_BAD,
2069 "WARNING: More than one pattern dependency not yet supported.\n");
2070
2071 again:
2072 *suffixp = (char *)prule->p_tgt_suffix;
2073
2074 p = (char *)prule->p_tgt_prefix;
2075 xname = name + prule->p_tgt_pfxlen; /* strip matching prefix */
2076 /*error("name: '%s'\n", xname);*/
2077 p = rstr(xname, (char *)prule->p_tgt_suffix);
2078 /*error("p: '%s'\n", p);*/
2079 len = p - xname + 1;
2080 if (len > plen) {
2081 plen = len;
2082 pat = pp = ___realloc(pp, plen, "pattern content");
2083 }
2084 strlcpy(pat, xname, len);
2085 /*error("pat: '%s' len %d strlen %d\n", pat, len, strlen(pat));*/
2086 /*
2087 * "len" contains the Null Byte
2088 */
2089 if ((len = prule->p_src_pfxlen + len + prule->p_src_suflen) > slen) {
2090 slen = len;
2091 sourcename = sp = ___realloc(sp, slen, "pattern source name");
2092 }
2093 if ((len = snprintf(sourcename, slen, "%s%s%s",
2094 prule->p_src_prefix, pat,
2095 prule->p_src_suffix)) >= slen) {
2096 etoolong("copy pattern source name", sourcename);
2097 /* NOTREACHED */
2098 }
2099 /*error("sourcename: '%s'\n", sourcename);*/
2100
2101 newsource.o_name = sourcename;
2102 newsource.o_namelen = len;
2103 newsource.o_date = NOTIME;
2104 newsource.o_type = 0;
2105 newsource.o_flags = 0;
2106 newsource.o_level = MAXLEVEL;
2107 newsource.o_fileindex = 0;
2108 newsource.o_list = (list_t *)NULL;
2109 newsource.o_cmd = (cmd_t *)NULL;
2110 newsource.o_node = (obj_t *)NULL;
2111
2112 if (prule->p_flags & PF_TERM)
2113 newsource.o_flags |= F_TERM;
2114
2115 if ((source = objlook(sourcename, FALSE)) != (obj_t *)NULL) {
2116 make(source, TRUE, dlev); /* source found, update it */
2117 /* XXX make() return value ??? */
2118
2119 } else if (make(&newsource, FALSE,
2120 dlev) != NOTIME) { /* found or made source */
2121 source = objlook(sourcename, TRUE);
2122 source->o_date = newsource.o_date;
2123 source->o_level = newsource.o_level;
2124 if (source->o_flags & F_DCOLON)
2125 dcolon_time(source);
2126
2127 } else {
2128 /*
2129 * This is a replicated search for additional matches for the
2130 * same obj.
2131 */
2132 prule = _pattern_rule(prule->p_next, name);
2133 if (prule != NULL)
2134 goto again;
2135
2136 *pcmd = (cmd_t *)NULL;
2137 source = (obj_t *)0;
2138 goto out;
2139 }
2140
2141 found:
2142 /*
2143 * If we do not create missing nodes here, Pattern Matching Rules like:
2144 * %.1:
2145 * command
2146 * will cause smake to dump core.
2147 */
2148 obj = objlook(name, TRUE);
2149 obj->o_node = (obj_t *)prule;
2150 obj->o_flags |= F_PATRULE;
2151 *pcmd = prule->p_cmd;
2152 out:
2153 if (sp)
2154 free(sp);
2155 if (pp)
2156 free(pp);
2157 return (source);
2158 }
2159
2160 /*
2161 * Find a source file for a given object file, use POSIX suffix rules.
2162 * Loop over all possible source names for all possible target suffixes.
2163 */
2164 LOCAL obj_t *
suff_src(name,rule,rtypep,suffixp,pcmd,dlev)2165 suff_src(name, rule, rtypep, suffixp, pcmd, dlev)
2166 char *name;
2167 obj_t *rule;
2168 int *rtypep;
2169 char **suffixp;
2170 cmd_t **pcmd;
2171 int dlev;
2172 {
2173 register list_t *suf; /* List of possible suffixes */
2174 register obj_t *source;
2175 register char *suffix = NULL;
2176 BOOL found_suffix = FALSE;
2177
2178 for (suf = Suffixes; suf; suf = suf->l_next) {
2179 suffix = suf->l_obj->o_name;
2180 if (rstr(name, suffix) == NULL) /* may not be a suffix */
2181 continue;
2182
2183 found_suffix = TRUE;
2184 *suffixp = suffix;
2185 if ((source = one_suff_src(name, suffix, pcmd, dlev)) != NULL)
2186 return (source);
2187 }
2188 if (found_suffix) {
2189 *pcmd = (cmd_t *)NULL;
2190 return ((obj_t *)0);
2191 }
2192 /*
2193 * XXX We should never come here.
2194 */
2195 errmsgno(EX_BAD, "XXX Expected double suffix rule but found none.\n");
2196 return (one_suff_src(name, Nullstr, pcmd, dlev));
2197 }
2198
2199 /*
2200 * Find a source file for a given object file, use POSIX suffix rules.
2201 * Loop over all possible source names for one given target suffix.
2202 */
2203 LOCAL obj_t *
one_suff_src(name,suffix,pcmd,dlev)2204 one_suff_src(name, suffix, pcmd, dlev)
2205 char *name;
2206 char *suffix;
2207 cmd_t **pcmd;
2208 int dlev;
2209 {
2210 register list_t *suf; /* List of possible suffixes */
2211 register obj_t *o;
2212 register obj_t *source;
2213 obj_t newsource;
2214 char _sourcename[TYPICAL_NAMEMAX];
2215 char *sourcename = _sourcename;
2216 char *sp = NULL;
2217 size_t slen = sizeof (_sourcename);
2218 char *endbase;
2219 char _rulename[TYPICAL_NAMEMAX];
2220 char *rulename = _rulename;
2221 char *rp = NULL;
2222 size_t rlen = sizeof (_rulename);
2223 size_t endlen;
2224 size_t sourcelen;
2225 size_t len;
2226
2227 if ((len = strlen(name)) >= slen) {
2228 slen = len + 16; /* Add space for '\0' and suf */
2229 sourcename = sp = ___realloc(sp, slen,
2230 "suffix source name");
2231 }
2232 copy_base(name, sourcename, slen, suffix);
2233 endbase = sourcename + strlen(sourcename);
2234 endlen = slen - (endbase - sourcename);
2235
2236 newsource.o_name = sourcename;
2237 newsource.o_namelen = sourcelen = strlen(sourcename);
2238 newsource.o_date = NOTIME;
2239 newsource.o_type = 0;
2240 newsource.o_flags = 0;
2241 newsource.o_level = MAXLEVEL;
2242 newsource.o_fileindex = 0;
2243 newsource.o_list = (list_t *)NULL;
2244 newsource.o_cmd = (cmd_t *)NULL;
2245 newsource.o_node = (obj_t *)NULL;
2246
2247 for (suf = Suffixes; suf; suf = suf->l_next) {
2248 again:
2249 if (snprintf(rulename, rlen,
2250 "%s%s",
2251 suf->l_obj->o_name,
2252 suffix) >= rlen) {
2253 /*
2254 * Expand rule name space.
2255 */
2256 rlen = strlen(suffix) + suf->l_obj->o_namelen + 16;
2257 rulename = rp = ___realloc(rp, rlen, "suffix rule name");
2258 goto again;
2259
2260 }
2261
2262 if ((o = objlook(rulename, FALSE)) == NULL || o->o_type != COLON)
2263 continue;
2264
2265 if (o->o_list)
2266 suffix_warn(o);
2267
2268 if (Debug > 1)
2269 error("Trying %s suffix rule '%s' for: %s (suffix: '%s')\n",
2270 *suffix?"double":"single", rulename, name, suffix);
2271
2272 if (suf->l_obj->o_namelen >= endlen) {
2273 slen = sourcelen + suf->l_obj->o_namelen + 1;
2274 sourcename = ___realloc(sp, slen,
2275 "suffix source name");
2276 if (sp == NULL) {
2277 /*
2278 * Copy old content.
2279 * First null-terminate the current buffer.
2280 */
2281 _sourcename[sizeof (_sourcename) - 1] = '\0';
2282 strlcpy(sourcename, _sourcename, sourcelen+1);
2283 }
2284 newsource.o_name = sp = sourcename;
2285 endbase = sourcename + sourcelen;
2286 endlen = slen - (endbase - sourcename);
2287 }
2288 if (strlcpy(endbase, suf->l_obj->o_name, endlen) >= endlen)
2289 etoolong("build suffix source name", sourcename);
2290 newsource.o_namelen = sourcelen + suf->l_obj->o_namelen;
2291 if ((source = objlook(sourcename, FALSE)) != (obj_t *)NULL) {
2292 make(source, TRUE, dlev); /* source known, update it */
2293 /* XXX make() return value ??? */
2294
2295 *pcmd = o->o_cmd;
2296 if (sp)
2297 free(sp);
2298 if (rp)
2299 free(rp);
2300 return (source);
2301 }
2302 if (make(&newsource, FALSE,
2303 dlev) != NOTIME) { /* found/made source */
2304 source = objlook(sourcename, TRUE);
2305 source->o_date = newsource.o_date;
2306 source->o_level = newsource.o_level;
2307 if (source->o_flags & F_DCOLON)
2308 dcolon_time(source);
2309
2310 *pcmd = o->o_cmd;
2311 if (sp)
2312 free(sp);
2313 if (rp)
2314 free(rp);
2315 return (source);
2316 }
2317 }
2318 if (sp)
2319 free(sp);
2320 if (rp)
2321 free(rp);
2322 *pcmd = (cmd_t *)NULL;
2323 return ((obj_t *)0);
2324 }
2325
2326 /*
2327 * Find a source file for a given object file, use simple suffix rules.
2328 */
2329 LOCAL obj_t *
ssuff_src(name,rule,rtypep,suffixp,pcmd,dlev)2330 ssuff_src(name, rule, rtypep, suffixp, pcmd, dlev)
2331 char *name;
2332 obj_t *rule;
2333 int *rtypep;
2334 char **suffixp;
2335 cmd_t **pcmd;
2336 int dlev;
2337 {
2338 register list_t *suf = rule->o_list; /* List of possible suffixes */
2339 register cmd_t *cmd = rule->o_cmd; /* List of implicit commands */
2340 cmd_t *ncmd; /* for allocated new command */
2341 register obj_t *source;
2342 obj_t newsource;
2343 char _sourcename[TYPICAL_NAMEMAX];
2344 char *sourcename = _sourcename;
2345 char *sp = NULL;
2346 size_t slen = sizeof (_sourcename);
2347 char *endbase;
2348 size_t endlen;
2349 size_t sourcelen;
2350 size_t len;
2351
2352 *suffixp = get_suffix(name, (char *)0); /* Use '.' (dot) suffix only */
2353
2354 if ((len = strlen(name)) >= slen) {
2355 slen = len + 16; /* Add space for '\0' and suf */
2356 sourcename = sp = ___realloc(sp, slen,
2357 "simple suffix source name");
2358 }
2359 copy_base(name, sourcename, slen, (char *)NULL);
2360 endbase = sourcename + strlen(sourcename);
2361 endlen = slen - (endbase - sourcename);
2362
2363 newsource.o_name = sourcename;
2364 newsource.o_namelen = sourcelen = strlen(sourcename);
2365 newsource.o_date = NOTIME;
2366 newsource.o_type = 0;
2367 newsource.o_flags = 0;
2368 newsource.o_level = MAXLEVEL;
2369 newsource.o_fileindex = 0;
2370 newsource.o_list = (list_t *)NULL;
2371 newsource.o_cmd = (cmd_t *)NULL;
2372 newsource.o_node = (obj_t *)NULL;
2373
2374 do {
2375 if (suf->l_obj->o_namelen >= endlen) {
2376 slen = sourcelen + suf->l_obj->o_namelen + 1;
2377 sourcename = ___realloc(sp, slen,
2378 "simple suffix source name");
2379 if (sp == NULL) {
2380 /*
2381 * Copy old content.
2382 * First null-terminate the current buffer.
2383 */
2384 _sourcename[sizeof (_sourcename) - 1] = '\0';
2385 strlcpy(sourcename, _sourcename, sourcelen+1);
2386 }
2387 newsource.o_name = sp = sourcename;
2388 endbase = sourcename + sourcelen;
2389 endlen = slen - (endbase - sourcename);
2390 }
2391 if (strlcpy(endbase, suf->l_obj->o_name, endlen) >= endlen)
2392 etoolong("build simple suffix source name", sourcename);
2393 newsource.o_namelen = sourcelen + suf->l_obj->o_namelen;
2394 if ((source = objlook(sourcename, FALSE)) != (obj_t *)NULL) {
2395 make(source, TRUE, dlev); /* source known, update it */
2396 /* XXX make() return value ??? */
2397 break;
2398 }
2399 if (make(&newsource, FALSE,
2400 dlev) != NOTIME) { /* found/made source */
2401 source = objlook(sourcename, TRUE);
2402 source->o_date = newsource.o_date;
2403 source->o_level = newsource.o_level;
2404 if (source->o_flags & F_DCOLON)
2405 dcolon_time(source);
2406
2407 break;
2408 }
2409 } while ((suf = suf->l_next) != (list_t *)NULL &&
2410 (cmd = cmd->c_next) != (cmd_t *)NULL);
2411 if (sp)
2412 free(sp);
2413 if (source) {
2414 ncmd = (cmd_t *)fastalloc(sizeof (cmd_t));
2415 ncmd->c_next = (cmd_t *)NULL;
2416 ncmd->c_line = cmd->c_line;
2417 *pcmd = ncmd;
2418 *rtypep |= RTYPE_NEEDFREE;
2419 } else {
2420 *pcmd = (cmd_t *)NULL;
2421 }
2422 return (source);
2423 }
2424
2425 LOCAL obj_t *BadObj;
2426 /*
2427 * Find a source file for a given object file.
2428 */
2429 LOCAL obj_t *
findsrc(obj,rule,rtypep,suffixp,pcmd,dlev)2430 findsrc(obj, rule, rtypep, suffixp, pcmd, dlev)
2431 obj_t *obj;
2432 obj_t *rule;
2433 int *rtypep;
2434 char **suffixp;
2435 cmd_t **pcmd;
2436 int dlev;
2437 {
2438 int rtype = *rtypep;
2439
2440 if (BadObj == NULL) {
2441 BadObj = objlook("BAD OBJECT", TRUE);
2442 BadObj->o_date = BADTIME;
2443 BadObj->o_type = ':';
2444 }
2445 *suffixp = (char *)NULL;
2446 *pcmd = (cmd_t *)NULL;
2447
2448 if (obj->o_flags & F_TERM)
2449 return ((obj_t *)0);
2450
2451 switch (rtype) {
2452
2453 case RTYPE_PATTERN:
2454 return (patr_src(obj->o_name, (patr_t *)rule,
2455 rtypep, suffixp, pcmd, dlev));
2456
2457 case RTYPE_DSUFFIX:
2458 return (suff_src(obj->o_name, rule,
2459 rtypep, suffixp, pcmd, dlev));
2460
2461 case RTYPE_SUFFIX:
2462 *suffixp = Nullstr;
2463 return (one_suff_src(obj->o_name, Nullstr, pcmd, dlev));
2464
2465 case RTYPE_SSUFFIX:
2466 return (ssuff_src(obj->o_name, rule,
2467 rtypep, suffixp, pcmd, dlev));
2468
2469 case RTYPE_DEFAULT:
2470 *pcmd = (cmd_t *)rule;
2471 return (obj); /* This causes $< to be == $@ */
2472
2473 default:
2474 break;
2475 }
2476 errmsgno(EX_BAD, "Impossible default rule type %X for '%s'.\n",
2477 rtype,
2478 obj->o_name);
2479 return (BadObj);
2480 }
2481
2482 /*
2483 * Process a target with no explicit command list.
2484 */
2485 LOCAL date_t
default_cmd(obj,depname,deptime,deplevel,must_exist,dlev)2486 default_cmd(obj, depname, deptime, deplevel, must_exist, dlev)
2487 register obj_t *obj;
2488 char *depname;
2489 date_t deptime;
2490 int deplevel;
2491 BOOL must_exist;
2492 int dlev;
2493 {
2494 obj_t *rule = (obj_t *)NULL;
2495 obj_t *source;
2496 cmd_t *cmd;
2497 cmd_t *cp;
2498 char *suffix;
2499 int rtype = RTYPE_NONE;
2500
2501
2502 if (dlev <= 0) {
2503 errmsgno(EX_BAD,
2504 "Recursion in default rule for '%s'.\n",
2505 obj->o_name);
2506 return (RECURSETIME);
2507 }
2508
2509 if (((obj->o_flags & F_TERM) == 0) &&
2510 ((rule = default_rule(obj, &rtype)) == (obj_t *)NULL)) {
2511 /*
2512 * Found no default dependency rule for this target.
2513 * This is most likely a placeholder target where no
2514 * related file exists. As we did not found rules,
2515 * we do not run commands and thus do not propagate deptime.
2516 */
2517 if (obj->o_list != (list_t *)NULL) {
2518 obj->o_level = deplevel;
2519 return (NOTIME);
2520 #ifdef notdef
2521 /*
2522 * This caused "all is up to date" messages.
2523 */
2524 return (deptime);
2525 #endif
2526 }
2527 /*
2528 * Found no rule to make target.
2529 */
2530 if (obj == default_tgt) {
2531 /*
2532 * For intermediate placeholder targets like FORCE:
2533 * that have no prerequisites, do not create an error.
2534 */
2535 if (obj->o_list == (list_t *)NULL)
2536 return (NOTIME);
2537 errmsgno(EX_BAD, "'%s' does not say how to make '%s'.\n",
2538 MakeFileNames[obj->o_fileindex], obj->o_name);
2539 return (BADTIME);
2540 }
2541 /*
2542 * Check if it is a source.
2543 */
2544 if (searchobj(obj, MAXLEVEL, SSRC) != NOTIME)
2545 return (obj->o_date);
2546 if (!must_exist)
2547 return (NOTIME);
2548 /*
2549 * For intermediate placeholder targets like FORCE:
2550 * that have no prerequisites, do not create an error.
2551 */
2552 if (obj->o_list == (list_t *)NULL)
2553 return (NOTIME);
2554
2555 errmsgno(EX_BAD, "'%s' does not exist.\n", obj->o_name);
2556 return (BADTIME);
2557 }
2558 /*
2559 * findsrc() returns NULL if (obj->o_flags & F_TERM) != 0.
2560 * This prevents problems with rule == NULL from default_rule().
2561 */
2562 if ((source =
2563 findsrc(obj, rule, &rtype, &suffix, &cmd,
2564 dlev)) == (obj_t *)NULL) {
2565
2566 /* Probably a obj file name. */
2567 if (searchobj(obj, MAXLEVEL, ObjSearch) != NOTIME)
2568 return (obj->o_date);
2569
2570 if (Debug > 2)
2571 error("Cannot find source for: %s->time: %s %s(type: %X)\n",
2572 obj->o_name, prtime(obj->o_date),
2573 must_exist?"but must ":"", obj->o_type);
2574 if (!must_exist)
2575 return (NOTIME);
2576 /*
2577 * A target must not exist, a dependency must.
2578 */
2579 if (basetype(obj->o_type) == COLON)
2580 return (NOTIME);
2581 if (!No_Warn)
2582 errmsgno(EX_BAD,
2583 "Can't find any source for '%s'.\n",
2584 obj->o_name);
2585 if (NSflag)
2586 return (NOTIME); /* -N specified */
2587 return (BADTIME);
2588 }
2589
2590 if (source->o_date == BADTIME)
2591 goto badtime;
2592
2593 if (source->o_date > deptime) { /* target depends on this source too */
2594 depname = source->o_name;
2595 deptime = source->o_date;
2596 }
2597 if (source->o_level < deplevel)
2598 deplevel = source->o_level;
2599 if (deptime == BADTIME) /* Errors occured cannot make source.*/
2600 goto badtime;
2601
2602 if (deptime >= newtime || /* made some targets */
2603 !searchobj(obj, deplevel, SALL) || /* found no obj file */
2604 deptime > obj->o_date) { /* target is out of date */
2605
2606 if (Debug > 0)
2607 error("Default: %s is out of date to: %s%s\n",
2608 obj->o_name, depname,
2609 isphony(obj)?" and .PHONY:":"");
2610 if (Tflag) {
2611 if (!isphony(obj) && !touch_file(obj->o_name))
2612 goto badtime;
2613 }
2614 /*
2615 * Sun make only calls '+' command with -t when in POSIX mode.
2616 */
2617 for (cp = cmd; cp; cp = cp->c_next) {
2618 if (docmd(substitute(cp->c_line, obj, source, suffix), obj)) {
2619 goto badtime;
2620 }
2621 }
2622 obj->o_date = newtime;
2623 obj->o_level = WDLEVEL;
2624 if (obj->o_flags & F_DCOLON)
2625 dcolon_time(obj);
2626
2627 }
2628 if ((rtype & RTYPE_NEEDFREE) != 0 && cmd != NULL)
2629 fastfree((char *)cmd, sizeof (*cmd));
2630
2631 /*
2632 * A file is only subject to automatic movement to the object directory
2633 * if there is no slash in the path name. move_tgt() will not move such
2634 * a file and we thus do not check such files here as well.
2635 */
2636 if (!Tflag && ObjDir != (char *)NULL &&
2637 obj->o_level == WDLEVEL && strchr(obj->o_name, SLASH) == NULL) {
2638 int m; /* for move status */
2639 int a = 0; /* == 1 if file at src level */
2640
2641 if (Debug > 3) {
2642 list_t *l = list_nth(SearchList, source->o_level-2);
2643
2644 error("%s: obj->o_level: %d %s: source->o_level: %d '%s'\n",
2645 obj->o_name, obj->o_level,
2646 source->o_name, source->o_level,
2647 l ? l->l_obj->o_name:Nullstr);
2648 }
2649 obj->o_level = source->o_level;
2650 if ((source->o_level & 1) == 0) /* Source has lower level */
2651 a = 1; /* add one if source level */
2652 obj->o_level += a; /* use corresponding ObjDir*/
2653
2654 if ((m = move_tgt(obj)) == 0) { /* move target into ObjDir */
2655 obj->o_level = WDLEVEL;
2656 return (BADTIME); /* move failed */
2657 } else if (m == TRUE) { /* OK, but did not move */
2658 obj->o_level -= a; /* Restore original level */
2659 } else if (m == (TRUE+1) &&
2660 obj->o_date == newtime) {
2661 /*
2662 * The target may have been created as $(ARCHDIR)/$@
2663 * and we thus could not find the target at WDLEVEL.
2664 * Search for it in the list of search directories.
2665 * Use the correct o_level if we could find it via
2666 * searchobj().
2667 */
2668 searchobj(obj, deplevel, SALL);
2669 obj->o_date = newtime;
2670 }
2671 /* obj->o_level = OBJLEVEL;*/
2672 }
2673 return (obj->o_date);
2674 badtime:
2675 if ((rtype & RTYPE_NEEDFREE) != 0 && cmd != NULL)
2676 fastfree((char *)cmd, sizeof (*cmd));
2677
2678 return (BADTIME);
2679 }
2680
2681 /*
2682 * Do all commands that are required to make the current target up to date.
2683 *
2684 * If any step fails, return BADTIME. This will stop dependent commands but
2685 * all other commands, not related to the failed target may be made.
2686 *
2687 * If must_exist is FALSE, return value 0 if no source file exists.
2688 * In this case, findsrc will try other suffixes in the implicit rules.
2689 */
2690 LOCAL date_t
make(obj,must_exist,dlev)2691 make(obj, must_exist, dlev)
2692 register obj_t *obj;
2693 BOOL must_exist;
2694 int dlev;
2695 {
2696 char *depname = 0;
2697 date_t deptime = NOTIME;
2698 date_t mtime;
2699 short deplevel = MAXLEVEL;
2700 register list_t *l;
2701 register obj_t *dep;
2702 register cmd_t *cmd;
2703
2704 if (Debug > 1)
2705 error("%sing %s\n", must_exist?"Mak":"Check", obj->o_name);
2706
2707 if (obj->o_date != 0) {
2708 if (Debug > 2)
2709 error("make: %s->time: %s\n",
2710 obj->o_name, prtime(obj->o_date));
2711 /*
2712 * We have been here before!
2713 */
2714 if (obj->o_date == RECURSETIME)
2715 comerrno(EX_BAD, "Recursion in dependencies for '%s'.\n",
2716 obj->o_name);
2717 return (obj->o_date);
2718 }
2719 obj->o_date = RECURSETIME; /* mark obj "we have been here" */
2720 if (obj->o_flags & F_DCOLON)
2721 dcolon_time(obj);
2722
2723 /*
2724 * Loop through the list of dependencies for obj, but do not try
2725 * to "make" the names that appear as content of a NAME=val type macro.
2726 */
2727 if (obj->o_type != EQUAL)
2728 for (l = obj->o_list; l; l = l->l_next) {
2729 dep = l->l_obj;
2730 mtime = make(dep, TRUE, dlev);
2731 if (!Kflag && mtime == BADTIME) {
2732 deptime = BADTIME;
2733 break;
2734 }
2735
2736
2737 if (dep->o_date > deptime) {
2738 /*
2739 * Remember the date of the newest
2740 * object in the list.
2741 */
2742 depname = dep->o_name;
2743 deptime = dep->o_date;
2744 }
2745 if (dep->o_level < deplevel) {
2746 /*
2747 * Remember the highest index in the search path
2748 * for all objects in the list.
2749 */
2750 deplevel = dep->o_level;
2751 }
2752 }
2753 if (deptime == BADTIME) { /* Errors occured cannot make target.*/
2754 obj->o_date = BADTIME;
2755 if (obj->o_flags & F_DCOLON)
2756 dcolon_time(obj);
2757
2758 return (obj->o_date);
2759 }
2760
2761 if ((obj->o_type != DCOLON) && /* If final :: target */
2762 (obj->o_flags & F_DCOLON) == 0 && /* If no intermediate target */
2763 obj->o_cmd == (cmd_t *)NULL) { /* Find default commands */
2764 obj->o_date = default_cmd(obj, depname, deptime,
2765 deplevel, must_exist,
2766 dlev < 0?16:dlev-1);
2767 if (obj->o_flags & F_DCOLON)
2768 dcolon_time(obj);
2769 /*
2770 * Fake success for targets listed in the makefile that have
2771 * no explicit commands no explicit dependencies (prerequisites)
2772 * and where we could not find implicit dependencies.
2773 * These intermediate placeholder targets look similar to FORCE:
2774 */
2775 if (obj->o_list == NULL && obj->o_date == NOTIME && must_exist)
2776 obj->o_date = newtime;
2777
2778 if (obj->o_flags & F_DCOLON)
2779 dcolon_time(obj);
2780
2781 return (obj->o_date);
2782 }
2783
2784 /*
2785 * There is an explicit command list for the current target.
2786 */
2787 if (Debug > 2)
2788 error("deptime: %s\n", prtime(deptime));
2789 if (deptime >= newtime || /* made some targets */
2790 !searchobj(obj, deplevel, ObjSearch) || /* found no obj file */
2791 deptime > obj->o_date) { /* tgt is out of date*/
2792
2793 if (Debug > 0)
2794 error("Make: %s is out of date to: %s%s\n",
2795 obj->o_name, depname,
2796 isphony(obj)?" and .PHONY:":"");
2797 if (Tflag) {
2798 if (!isphony(obj) && !touch_file(obj->o_name)) {
2799
2800 obj->o_date = BADTIME;
2801 if (obj->o_flags & F_DCOLON)
2802 dcolon_time(obj);
2803
2804 return (obj->o_date);
2805 }
2806 }
2807 /*
2808 * Sun make only calls '+' command with -t when in POSIX mode.
2809 */
2810 for (cmd = obj->o_cmd; cmd; cmd = cmd->c_next) {
2811 if (docmd(substitute(cmd->c_line, obj, (obj_t *)NULL, (char *)NULL),
2812 obj)) {
2813
2814 obj->o_date = BADTIME;
2815 if (obj->o_flags & F_DCOLON)
2816 dcolon_time(obj);
2817 return (obj->o_date);
2818 }
2819 }
2820 if (obj->o_level == MAXLEVEL)
2821 searchobj(obj, deplevel, SALL);
2822 obj->o_date = newtime;
2823 if (obj->o_flags & F_DCOLON)
2824 dcolon_time(obj);
2825 }
2826 return (obj->o_date);
2827 }
2828
2829 /*
2830 * This is the interface function that is to be called from main()
2831 * If called with a NULL pointer, it checks for the default target first,
2832 * otherwise the named target is made.
2833 */
2834 EXPORT BOOL
domake(name)2835 domake(name)
2836 char *name;
2837 {
2838 date_t mtime;
2839
2840 if (name) {
2841 default_tgt = objlook(name, TRUE);
2842 if (Debug > 0)
2843 error("Target: %s\n", name);
2844 } else if (default_tgt) {
2845 name = default_tgt->o_name;
2846 if (Debug > 0)
2847 error("DTarget: %s\n", name);
2848 }
2849 if (default_tgt) {
2850 mtime = make(default_tgt, TRUE, -1);
2851 if (mtime == BADTIME) {
2852 errmsgno(EX_BAD, "Couldn't make '%s'.\n", name);
2853 if (Debug > 0)
2854 error("Current working directory: %s\n", curwdir());
2855 return (FALSE);
2856 }
2857 if (mtime != NOTIME && mtime != newtime) {
2858 /*
2859 * Nothing to do.
2860 */
2861 if (!Qflag)
2862 errmsgno(EX_BAD, "'%s' is up to date.\n", name);
2863 return (TRUE);
2864 }
2865 if (Qflag)
2866 return (FALSE);
2867 return (TRUE);
2868 }
2869 errmsgno(EX_BAD, "No default Command defined.\n");
2870 usage(1);
2871 return (FALSE); /* keep lint happy */
2872 }
2873
2874 /*
2875 * Try to make target 'obj'.
2876 * Return TRUE, if target could be made.
2877 *
2878 * omake and xmake are used to make intermediate targets with no direct
2879 * dependency (e.g. .INIT and included files).
2880 */
2881 EXPORT BOOL
omake(obj,must_exist)2882 omake(obj, must_exist)
2883 obj_t *obj;
2884 BOOL must_exist;
2885 {
2886 date_t mtime;
2887
2888 if (obj == (obj_t *)NULL)
2889 return (TRUE);
2890
2891 if (Debug > 2)
2892 error("xmake: %s->time: %s\n",
2893 obj->o_name, prtime(obj->o_date));
2894 mtime = make(obj, must_exist, -1);
2895
2896 if (Debug > 2)
2897 error("xmake: %s\n", prtime(mtime));
2898 if (mtime == BADTIME) {
2899 /* errmsgno(EX_BAD, "Couldn't make '%s'.\n", obj->o_name);*/
2900 return (FALSE);
2901 }
2902 return (TRUE);
2903 }
2904
2905 /*
2906 * First do an objlook(name), then make it using omake.
2907 */
2908 EXPORT BOOL
xmake(name,must_exist)2909 xmake(name, must_exist)
2910 char *name;
2911 BOOL must_exist;
2912 {
2913 obj_t *o;
2914
2915 o = objlook(name, TRUE);
2916 return (omake(o, must_exist));
2917 }
2918