1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93, 1998 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 static void freerule
PARAMS ((struct rule
*rule
, struct rule
*lastrule
));
30 /* Chain of all pattern rules. */
32 struct rule
*pattern_rules
;
34 /* Pointer to last rule in the chain, so we can add onto the end. */
36 struct rule
*last_pattern_rule
;
38 /* Number of rules in the chain. */
40 unsigned int num_pattern_rules
;
42 /* Maximum number of target patterns of any pattern rule. */
44 unsigned int max_pattern_targets
;
46 /* Maximum number of dependencies of any pattern rule. */
48 unsigned int max_pattern_deps
;
50 /* Maximum length of the name of a dependencies of any pattern rule. */
52 unsigned int max_pattern_dep_length
;
54 /* Chain of all pattern-specific variables. */
56 static struct pattern_var
*pattern_vars
;
58 /* Pointer to last struct in the chain, so we can add onto the end. */
60 static struct pattern_var
*last_pattern_var
;
62 /* Pointer to structure for the file .SUFFIXES
63 whose dependencies are the suffixes to be searched. */
65 struct file
*suffix_file
;
67 /* Maximum length of a suffix. */
69 unsigned int maxsuffix
;
71 /* Compute the maximum dependency length and maximum number of
72 dependencies of all implicit rules. Also sets the subdir
73 flag for a rule when appropriate, possibly removing the rule
74 completely when appropriate. */
77 count_implicit_rule_limits ()
81 register struct rule
*rule
, *lastrule
;
83 num_pattern_rules
= max_pattern_targets
= max_pattern_deps
= 0;
84 max_pattern_dep_length
= 0;
92 unsigned int ndeps
= 0;
93 register struct dep
*dep
;
94 struct rule
*next
= rule
->next
;
95 unsigned int ntargets
;
100 while (rule
->targets
[ntargets
] != 0)
103 if (ntargets
> max_pattern_targets
)
104 max_pattern_targets
= ntargets
;
106 for (dep
= rule
->deps
; dep
!= 0; dep
= dep
->next
)
108 unsigned int len
= strlen (dep
->name
);
111 char *p
= strrchr (dep
->name
, ']');
114 p
= strrchr (dep
->name
, ':');
115 p2
= p
!= 0 ? strchr (dep
->name
, '%') : 0;
117 char *p
= strrchr (dep
->name
, '/');
118 char *p2
= p
!= 0 ? strchr (dep
->name
, '%') : 0;
122 if (len
> max_pattern_dep_length
)
123 max_pattern_dep_length
= len
;
125 if (p
!= 0 && p2
> p
)
127 /* There is a slash before the % in the dep name.
128 Extract the directory name. */
131 if (p
- dep
->name
> namelen
)
135 namelen
= p
- dep
->name
;
136 name
= (char *) xmalloc (namelen
+ 1);
138 bcopy (dep
->name
, name
, p
- dep
->name
);
139 name
[p
- dep
->name
] = '\0';
141 /* In the deps of an implicit rule the `changed' flag
142 actually indicates that the dependency is in a
143 nonexistent subdirectory. */
145 dep
->changed
= !dir_file_exists_p (name
, "");
147 if (dep
->changed
&& strchr (name
, ':') != 0)
149 if (dep
->changed
&& *name
== '/')
152 /* The name is absolute and the directory does not exist.
153 This rule can never possibly match, since this dependency
154 can never possibly exist. So just remove the rule from
156 freerule (rule
, lastrule
);
162 /* This dependency does not reside in a subdirectory. */
166 if (ndeps
> max_pattern_deps
)
167 max_pattern_deps
= ndeps
;
178 /* Create a pattern rule from a suffix rule.
179 TARGET is the target suffix; SOURCE is the source suffix.
180 CMDS are the commands.
181 If TARGET is nil, it means the target pattern should be `(%.o)'.
182 If SOURCE is nil, it means there should be no deps. */
185 convert_suffix_rule (target
, source
, cmds
)
186 char *target
, *source
;
187 struct commands
*cmds
;
189 char *targname
, *targpercent
, *depname
;
190 char **names
, **percents
;
195 /* Special case: TARGET being nil means we are defining a
196 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
199 targname
= savestring ("(%.obj)", 7);
201 targname
= savestring ("(%.o)", 5);
203 targpercent
= targname
+ 1;
207 /* Construct the target name. */
208 len
= strlen (target
);
209 targname
= xmalloc (1 + len
+ 1);
211 bcopy (target
, targname
+ 1, len
+ 1);
212 targpercent
= targname
;
215 names
= (char **) xmalloc (2 * sizeof (char *));
216 percents
= (char **) alloca (2 * sizeof (char *));
218 percents
[0] = targpercent
;
219 names
[1] = percents
[1] = 0;
225 /* Construct the dependency name. */
226 len
= strlen (source
);
227 depname
= xmalloc (1 + len
+ 1);
229 bcopy (source
, depname
+ 1, len
+ 1);
230 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
232 deps
->name
= depname
;
233 deps
->ignore_mtime
= 0;
236 create_pattern_rule (names
, percents
, 0, deps
, cmds
, 0);
239 /* Convert old-style suffix rules to pattern rules.
240 All rules for the suffixes on the .SUFFIXES list
241 are converted and added to the chain of pattern rules. */
244 convert_to_pattern ()
246 register struct dep
*d
, *d2
;
247 register struct file
*f
;
248 register char *rulename
;
249 register unsigned int slen
, s2len
;
251 /* Compute maximum length of all the suffixes. */
254 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
256 register unsigned int namelen
= strlen (dep_name (d
));
257 if (namelen
> maxsuffix
)
261 rulename
= (char *) alloca ((maxsuffix
* 2) + 1);
263 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
265 /* Make a rule that is just the suffix, with no deps or commands.
266 This rule exists solely to disqualify match-anything rules. */
267 convert_suffix_rule (dep_name (d
), (char *) 0, (struct commands
*) 0);
271 /* Record a pattern for this suffix's null-suffix rule. */
272 convert_suffix_rule ("", dep_name (d
), f
->cmds
);
274 /* Record a pattern for each of this suffix's two-suffix rules. */
275 slen
= strlen (dep_name (d
));
276 bcopy (dep_name (d
), rulename
, slen
);
277 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
279 s2len
= strlen (dep_name (d2
));
281 if (slen
== s2len
&& streq (dep_name (d
), dep_name (d2
)))
284 bcopy (dep_name (d2
), rulename
+ slen
, s2len
+ 1);
285 f
= lookup_file (rulename
);
286 if (f
== 0 || f
->cmds
== 0)
289 if (s2len
== 2 && rulename
[slen
] == '.' && rulename
[slen
+ 1] == 'a')
290 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
291 It also generates a normal `%.a: %.X' rule below. */
292 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
296 /* The suffix rule `.X.Y:' is converted
297 to the pattern rule `%.Y: %.X'. */
298 convert_suffix_rule (dep_name (d2
), dep_name (d
), f
->cmds
);
304 /* Install the pattern rule RULE (whose fields have been filled in)
305 at the end of the list (so that any rules previously defined
306 will take precedence). If this rule duplicates a previous one
307 (identical target and dependencies), the old one is replaced
308 if OVERRIDE is nonzero, otherwise this new one is thrown out.
309 When an old rule is replaced, the new one is put at the end of the
310 list. Return nonzero if RULE is used; zero if not. */
313 new_pattern_rule (rule
, override
)
314 register struct rule
*rule
;
317 register struct rule
*r
, *lastrule
;
318 register unsigned int i
, j
;
325 /* Search for an identical rule. */
327 for (r
= pattern_rules
; r
!= 0; lastrule
= r
, r
= r
->next
)
328 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
330 for (j
= 0; r
->targets
[j
] != 0; ++j
)
331 if (!streq (rule
->targets
[i
], r
->targets
[j
]))
333 if (r
->targets
[j
] == 0)
334 /* All the targets matched. */
336 register struct dep
*d
, *d2
;
337 for (d
= rule
->deps
, d2
= r
->deps
;
338 d
!= 0 && d2
!= 0; d
= d
->next
, d2
= d2
->next
)
339 if (!streq (dep_name (d
), dep_name (d2
)))
341 if (d
== 0 && d2
== 0)
343 /* All the dependencies matched. */
346 /* Remove the old rule. */
347 freerule (r
, lastrule
);
348 /* Install the new one. */
349 if (pattern_rules
== 0)
350 pattern_rules
= rule
;
352 last_pattern_rule
->next
= rule
;
353 last_pattern_rule
= rule
;
355 /* We got one. Stop looking. */
360 /* The old rule stays intact. Destroy the new one. */
361 freerule (rule
, (struct rule
*) 0);
372 /* There was no rule to replace. */
373 if (pattern_rules
== 0)
374 pattern_rules
= rule
;
376 last_pattern_rule
->next
= rule
;
377 last_pattern_rule
= rule
;
384 /* Install an implicit pattern rule based on the three text strings
385 in the structure P points to. These strings come from one of
386 the arrays of default implicit pattern rules.
387 TERMINAL specifies what the `terminal' field of the rule should be. */
390 install_pattern_rule (p
, terminal
)
394 register struct rule
*r
;
397 r
= (struct rule
*) xmalloc (sizeof (struct rule
));
399 r
->targets
= (char **) xmalloc (2 * sizeof (char *));
400 r
->suffixes
= (char **) xmalloc (2 * sizeof (char *));
401 r
->lens
= (unsigned int *) xmalloc (2 * sizeof (unsigned int));
407 r
->lens
[0] = strlen (p
->target
);
408 /* These will all be string literals, but we malloc space for
409 them anyway because somebody might want to free them later on. */
410 r
->targets
[0] = savestring (p
->target
, r
->lens
[0]);
411 r
->suffixes
[0] = find_percent (r
->targets
[0]);
412 if (r
->suffixes
[0] == 0)
413 /* Programmer-out-to-lunch error. */
419 r
->deps
= (struct dep
*) multi_glob (parse_file_seq (&ptr
, '\0',
420 sizeof (struct dep
), 1),
421 sizeof (struct dep
));
423 if (new_pattern_rule (r
, 0))
425 r
->terminal
= terminal
;
426 r
->cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
427 r
->cmds
->fileinfo
.filenm
= 0;
428 r
->cmds
->fileinfo
.lineno
= 0;
429 /* These will all be string literals, but we malloc space for them
430 anyway because somebody might want to free them later. */
431 r
->cmds
->commands
= xstrdup (p
->commands
);
432 r
->cmds
->command_lines
= 0;
437 /* Free all the storage used in RULE and take it out of the
438 pattern_rules chain. LASTRULE is the rule whose next pointer
442 freerule (rule
, lastrule
)
443 register struct rule
*rule
, *lastrule
;
445 struct rule
*next
= rule
->next
;
446 register unsigned int i
;
447 register struct dep
*dep
;
449 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
450 free (rule
->targets
[i
]);
458 /* We might leak dep->name here, but I'm not sure how to fix this: I
459 think that pointer might be shared (e.g., in the file hash?) */
464 free ((char *) rule
->targets
);
465 free ((char *) rule
->suffixes
);
466 free ((char *) rule
->lens
);
468 /* We can't free the storage for the commands because there
469 are ways that they could be in more than one place:
470 * If the commands came from a suffix rule, they could also be in
471 the `struct file's for other suffix rules or plain targets given
472 on the same makefile line.
473 * If two suffixes that together make a two-suffix rule were each
474 given twice in the .SUFFIXES list, and in the proper order, two
475 identical pattern rules would be created and the second one would
476 be discarded here, but both would contain the same `struct commands'
477 pointer from the `struct file' for the suffix rule. */
479 free ((char *) rule
);
481 if (pattern_rules
== rule
)
485 pattern_rules
= next
;
486 else if (lastrule
!= 0)
487 lastrule
->next
= next
;
488 if (last_pattern_rule
== rule
)
489 last_pattern_rule
= lastrule
;
492 /* Create a new pattern rule with the targets in the nil-terminated
493 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
494 pointers into the elements of TARGETS, where the `%'s are.
495 The new rule has dependencies DEPS and commands from COMMANDS.
496 It is a terminal rule if TERMINAL is nonzero. This rule overrides
497 identical rules with different commands if OVERRIDE is nonzero.
499 The storage for TARGETS and its elements is used and must not be freed
500 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
504 create_pattern_rule (targets
, target_percents
,
505 terminal
, deps
, commands
, override
)
506 char **targets
, **target_percents
;
509 struct commands
*commands
;
512 register struct rule
*r
= (struct rule
*) xmalloc (sizeof (struct rule
));
513 register unsigned int max_targets
, i
;
517 r
->targets
= targets
;
520 r
->lens
= (unsigned int *) xmalloc (2 * sizeof (unsigned int));
521 r
->suffixes
= (char **) xmalloc (2 * sizeof (char *));
522 for (i
= 0; targets
[i
] != 0; ++i
)
524 if (i
== max_targets
- 1)
527 r
->lens
= (unsigned int *)
528 xrealloc ((char *) r
->lens
, max_targets
* sizeof (unsigned int));
529 r
->suffixes
= (char **)
530 xrealloc ((char *) r
->suffixes
, max_targets
* sizeof (char *));
532 r
->lens
[i
] = strlen (targets
[i
]);
533 r
->suffixes
[i
] = (target_percents
== 0 ? find_percent (targets
[i
])
534 : target_percents
[i
]) + 1;
535 if (r
->suffixes
[i
] == 0)
539 if (i
< max_targets
- 1)
541 r
->lens
= (unsigned int *) xrealloc ((char *) r
->lens
,
542 (i
+ 1) * sizeof (unsigned int));
543 r
->suffixes
= (char **) xrealloc ((char *) r
->suffixes
,
544 (i
+ 1) * sizeof (char *));
547 if (new_pattern_rule (r
, override
))
548 r
->terminal
= terminal
;
551 /* Create a new pattern-specific variable struct. */
554 create_pattern_var (target
, suffix
)
555 char *target
, *suffix
;
557 register struct pattern_var
*p
= 0;
558 unsigned int len
= strlen(target
);
560 /* Look to see if this pattern already exists in the list. */
561 for (p
= pattern_vars
; p
!= NULL
; p
= p
->next
)
562 if (p
->len
== len
&& !strcmp(p
->target
, target
))
567 p
= (struct pattern_var
*) xmalloc (sizeof (struct pattern_var
));
568 if (last_pattern_var
!= 0)
569 last_pattern_var
->next
= p
;
572 last_pattern_var
= p
;
576 p
->suffix
= suffix
+ 1;
577 p
->vars
= create_new_variable_set();
583 /* Look up a target in the pattern-specific variable list. */
586 lookup_pattern_var (target
)
589 struct pattern_var
*p
;
590 unsigned int targlen
= strlen(target
);
592 for (p
= pattern_vars
; p
!= 0; p
= p
->next
)
595 unsigned int stemlen
;
597 if (p
->len
> targlen
)
598 /* It can't possibly match. */
601 /* From the lengths of the filename and the pattern parts,
602 find the stem: the part of the filename that matches the %. */
603 stem
= target
+ (p
->suffix
- p
->target
- 1);
604 stemlen
= targlen
- p
->len
+ 1;
606 /* Compare the text in the pattern before the stem, if any. */
607 if (stem
> target
&& !strneq (p
->target
, target
, stem
- target
))
610 /* Compare the text in the pattern after the stem, if any.
611 We could test simply using streq, but this way we compare the
612 first two characters immediately. This saves time in the very
613 common case where the first character matches because it is a
615 if (*p
->suffix
== stem
[stemlen
]
616 && (*p
->suffix
== '\0' || streq (&p
->suffix
[1], &stem
[stemlen
+1])))
623 /* Print the data base of rules. */
625 static void /* Useful to call from gdb. */
629 register unsigned int i
;
630 register struct dep
*d
;
632 for (i
= 0; r
->targets
[i
] != 0; ++i
)
634 fputs (r
->targets
[i
], stdout
);
635 if (r
->targets
[i
+ 1] != 0)
643 for (d
= r
->deps
; d
!= 0; d
= d
->next
)
644 printf (" %s", dep_name (d
));
648 print_commands (r
->cmds
);
652 print_rule_data_base ()
654 register unsigned int rules
, terminal
;
655 register struct rule
*r
;
657 puts (_("\n# Implicit Rules"));
659 rules
= terminal
= 0;
660 for (r
= pattern_rules
; r
!= 0; r
= r
->next
)
672 puts (_("\n# No implicit rules."));
675 printf (_("\n# %u implicit rules, %u"), rules
, terminal
);
677 printf (" (%.1f%%)", (double) terminal
/ (double) rules
* 100.0);
680 int f
= (terminal
* 1000 + 5) / rules
;
681 printf (" (%d.%d%%)", f
/10, f
%10);
684 puts (_(" terminal."));
687 if (num_pattern_rules
!= rules
)
689 /* This can happen if a fatal error was detected while reading the
690 makefiles and thus count_implicit_rule_limits wasn't called yet. */
691 if (num_pattern_rules
!= 0)
692 fatal (NILF
, _("BUG: num_pattern_rules wrong! %u != %u"),
693 num_pattern_rules
, rules
);
696 puts (_("\n# Pattern-specific variable values"));
699 struct pattern_var
*p
;
702 for (p
= pattern_vars
; p
!= 0; p
= p
->next
)
706 printf ("\n%s :\n", p
->target
);
707 print_variable_set (p
->vars
->set
, "# ");
711 puts (_("\n# No pattern-specific variable values."));
714 printf (_("\n# %u pattern-specific variable values"), rules
);