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 /* Pointer to structure for the file .SUFFIXES
55 whose dependencies are the suffixes to be searched. */
57 struct file
*suffix_file
;
59 /* Maximum length of a suffix. */
61 unsigned int maxsuffix
;
63 /* Compute the maximum dependency length and maximum number of
64 dependencies of all implicit rules. Also sets the subdir
65 flag for a rule when appropriate, possibly removing the rule
66 completely when appropriate. */
69 count_implicit_rule_limits (void)
73 register struct rule
*rule
, *lastrule
;
75 num_pattern_rules
= max_pattern_targets
= max_pattern_deps
= 0;
76 max_pattern_dep_length
= 0;
84 unsigned int ndeps
= 0;
85 register struct dep
*dep
;
86 struct rule
*next
= rule
->next
;
87 unsigned int ntargets
;
92 while (rule
->targets
[ntargets
] != 0)
95 if (ntargets
> max_pattern_targets
)
96 max_pattern_targets
= ntargets
;
98 for (dep
= rule
->deps
; dep
!= 0; dep
= dep
->next
)
100 unsigned int len
= strlen (dep
->name
);
103 char *p
= strrchr (dep
->name
, ']');
106 p
= strrchr (dep
->name
, ':');
107 p2
= p
!= 0 ? strchr (dep
->name
, '%') : 0;
109 char *p
= strrchr (dep
->name
, '/');
110 char *p2
= p
!= 0 ? strchr (dep
->name
, '%') : 0;
114 if (len
> max_pattern_dep_length
)
115 max_pattern_dep_length
= len
;
117 if (p
!= 0 && p2
> p
)
119 /* There is a slash before the % in the dep name.
120 Extract the directory name. */
123 if (p
- dep
->name
> namelen
)
127 namelen
= p
- dep
->name
;
128 name
= (char *) xmalloc (namelen
+ 1);
130 bcopy (dep
->name
, name
, p
- dep
->name
);
131 name
[p
- dep
->name
] = '\0';
133 /* In the deps of an implicit rule the `changed' flag
134 actually indicates that the dependency is in a
135 nonexistent subdirectory. */
137 dep
->changed
= !dir_file_exists_p (name
, "");
140 /* This dependency does not reside in a subdirectory. */
144 if (ndeps
> max_pattern_deps
)
145 max_pattern_deps
= ndeps
;
155 /* Create a pattern rule from a suffix rule.
156 TARGET is the target suffix; SOURCE is the source suffix.
157 CMDS are the commands.
158 If TARGET is nil, it means the target pattern should be `(%.o)'.
159 If SOURCE is nil, it means there should be no deps. */
162 convert_suffix_rule (char *target
, char *source
, struct commands
*cmds
)
164 char *targname
, *targpercent
, *depname
;
165 char **names
, **percents
;
170 /* Special case: TARGET being nil means we are defining a
171 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
174 targname
= savestring ("(%.obj)", 7);
176 targname
= savestring ("(%.o)", 5);
178 targpercent
= targname
+ 1;
182 /* Construct the target name. */
183 len
= strlen (target
);
184 targname
= xmalloc (1 + len
+ 1);
186 bcopy (target
, targname
+ 1, len
+ 1);
187 targpercent
= targname
;
190 names
= (char **) xmalloc (2 * sizeof (char *));
191 percents
= (char **) alloca (2 * sizeof (char *));
193 percents
[0] = targpercent
;
194 names
[1] = percents
[1] = 0;
200 /* Construct the dependency name. */
201 len
= strlen (source
);
202 depname
= xmalloc (1 + len
+ 1);
204 bcopy (source
, depname
+ 1, len
+ 1);
205 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
207 deps
->name
= depname
;
208 deps
->ignore_mtime
= 0;
209 deps
->staticpattern
= 0;
210 deps
->need_2nd_expansion
= 0;
213 create_pattern_rule (names
, percents
, 0, deps
, cmds
, 0);
216 /* Convert old-style suffix rules to pattern rules.
217 All rules for the suffixes on the .SUFFIXES list
218 are converted and added to the chain of pattern rules. */
221 convert_to_pattern (void)
223 register struct dep
*d
, *d2
;
224 register struct file
*f
;
225 register char *rulename
;
226 register unsigned int slen
, s2len
;
228 /* Compute maximum length of all the suffixes. */
231 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
233 register unsigned int namelen
= strlen (dep_name (d
));
234 if (namelen
> maxsuffix
)
238 rulename
= (char *) alloca ((maxsuffix
* 2) + 1);
240 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
242 /* Make a rule that is just the suffix, with no deps or commands.
243 This rule exists solely to disqualify match-anything rules. */
244 convert_suffix_rule (dep_name (d
), (char *) 0, (struct commands
*) 0);
248 /* Record a pattern for this suffix's null-suffix rule. */
249 convert_suffix_rule ("", dep_name (d
), f
->cmds
);
251 /* Record a pattern for each of this suffix's two-suffix rules. */
252 slen
= strlen (dep_name (d
));
253 bcopy (dep_name (d
), rulename
, slen
);
254 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
256 s2len
= strlen (dep_name (d2
));
258 if (slen
== s2len
&& streq (dep_name (d
), dep_name (d2
)))
261 bcopy (dep_name (d2
), rulename
+ slen
, s2len
+ 1);
262 f
= lookup_file (rulename
);
263 if (f
== 0 || f
->cmds
== 0)
266 if (s2len
== 2 && rulename
[slen
] == '.' && rulename
[slen
+ 1] == 'a')
267 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
268 It also generates a normal `%.a: %.X' rule below. */
269 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
273 /* The suffix rule `.X.Y:' is converted
274 to the pattern rule `%.Y: %.X'. */
275 convert_suffix_rule (dep_name (d2
), dep_name (d
), f
->cmds
);
281 /* Install the pattern rule RULE (whose fields have been filled in)
282 at the end of the list (so that any rules previously defined
283 will take precedence). If this rule duplicates a previous one
284 (identical target and dependencies), the old one is replaced
285 if OVERRIDE is nonzero, otherwise this new one is thrown out.
286 When an old rule is replaced, the new one is put at the end of the
287 list. Return nonzero if RULE is used; zero if not. */
290 new_pattern_rule (struct rule
*rule
, int override
)
292 register struct rule
*r
, *lastrule
;
293 register unsigned int i
, j
;
300 /* Search for an identical rule. */
302 for (r
= pattern_rules
; r
!= 0; lastrule
= r
, r
= r
->next
)
303 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
305 for (j
= 0; r
->targets
[j
] != 0; ++j
)
306 if (!streq (rule
->targets
[i
], r
->targets
[j
]))
308 if (r
->targets
[j
] == 0)
309 /* All the targets matched. */
311 register struct dep
*d
, *d2
;
312 for (d
= rule
->deps
, d2
= r
->deps
;
313 d
!= 0 && d2
!= 0; d
= d
->next
, d2
= d2
->next
)
314 if (!streq (dep_name (d
), dep_name (d2
)))
316 if (d
== 0 && d2
== 0)
318 /* All the dependencies matched. */
321 /* Remove the old rule. */
322 freerule (r
, lastrule
);
323 /* Install the new one. */
324 if (pattern_rules
== 0)
325 pattern_rules
= rule
;
327 last_pattern_rule
->next
= rule
;
328 last_pattern_rule
= rule
;
330 /* We got one. Stop looking. */
335 /* The old rule stays intact. Destroy the new one. */
336 freerule (rule
, (struct rule
*) 0);
347 /* There was no rule to replace. */
348 if (pattern_rules
== 0)
349 pattern_rules
= rule
;
351 last_pattern_rule
->next
= rule
;
352 last_pattern_rule
= rule
;
359 /* Install an implicit pattern rule based on the three text strings
360 in the structure P points to. These strings come from one of
361 the arrays of default implicit pattern rules.
362 TERMINAL specifies what the `terminal' field of the rule should be. */
365 install_pattern_rule (struct pspec
*p
, int terminal
)
367 register struct rule
*r
;
370 r
= (struct rule
*) xmalloc (sizeof (struct rule
));
372 r
->targets
= (char **) xmalloc (2 * sizeof (char *));
373 r
->suffixes
= (char **) xmalloc (2 * sizeof (char *));
374 r
->lens
= (unsigned int *) xmalloc (2 * sizeof (unsigned int));
380 r
->lens
[0] = strlen (p
->target
);
381 /* These will all be string literals, but we malloc space for
382 them anyway because somebody might want to free them later on. */
383 r
->targets
[0] = savestring (p
->target
, r
->lens
[0]);
384 r
->suffixes
[0] = find_percent (r
->targets
[0]);
385 if (r
->suffixes
[0] == 0)
386 /* Programmer-out-to-lunch error. */
392 r
->deps
= (struct dep
*) multi_glob (parse_file_seq (&ptr
, '\0',
393 sizeof (struct dep
), 1),
394 sizeof (struct dep
));
396 if (new_pattern_rule (r
, 0))
398 r
->terminal
= terminal
;
399 r
->cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
400 r
->cmds
->fileinfo
.filenm
= 0;
401 r
->cmds
->fileinfo
.lineno
= 0;
402 /* These will all be string literals, but we malloc space for them
403 anyway because somebody might want to free them later. */
404 r
->cmds
->commands
= xstrdup (p
->commands
);
405 r
->cmds
->command_lines
= 0;
410 /* Free all the storage used in RULE and take it out of the
411 pattern_rules chain. LASTRULE is the rule whose next pointer
415 freerule (struct rule
*rule
, struct rule
*lastrule
)
417 struct rule
*next
= rule
->next
;
418 register unsigned int i
;
419 register struct dep
*dep
;
421 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
422 free (rule
->targets
[i
]);
430 /* We might leak dep->name here, but I'm not sure how to fix this: I
431 think that pointer might be shared (e.g., in the file hash?) */
436 free ((char *) rule
->targets
);
437 free ((char *) rule
->suffixes
);
438 free ((char *) rule
->lens
);
440 /* We can't free the storage for the commands because there
441 are ways that they could be in more than one place:
442 * If the commands came from a suffix rule, they could also be in
443 the `struct file's for other suffix rules or plain targets given
444 on the same makefile line.
445 * If two suffixes that together make a two-suffix rule were each
446 given twice in the .SUFFIXES list, and in the proper order, two
447 identical pattern rules would be created and the second one would
448 be discarded here, but both would contain the same `struct commands'
449 pointer from the `struct file' for the suffix rule. */
451 free ((char *) rule
);
453 if (pattern_rules
== rule
)
457 pattern_rules
= next
;
458 else if (lastrule
!= 0)
459 lastrule
->next
= next
;
460 if (last_pattern_rule
== rule
)
461 last_pattern_rule
= lastrule
;
464 /* Create a new pattern rule with the targets in the nil-terminated
465 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
466 pointers into the elements of TARGETS, where the `%'s are.
467 The new rule has dependencies DEPS and commands from COMMANDS.
468 It is a terminal rule if TERMINAL is nonzero. This rule overrides
469 identical rules with different commands if OVERRIDE is nonzero.
471 The storage for TARGETS and its elements is used and must not be freed
472 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
476 create_pattern_rule (char **targets
, char **target_percents
,
477 int terminal
, struct dep
*deps
,
478 struct commands
*commands
, int override
)
480 unsigned int max_targets
, i
;
481 struct rule
*r
= (struct rule
*) xmalloc (sizeof (struct rule
));
485 r
->targets
= targets
;
488 r
->lens
= (unsigned int *) xmalloc (2 * sizeof (unsigned int));
489 r
->suffixes
= (char **) xmalloc (2 * sizeof (char *));
490 for (i
= 0; targets
[i
] != 0; ++i
)
492 if (i
== max_targets
- 1)
495 r
->lens
= (unsigned int *)
496 xrealloc ((char *) r
->lens
, max_targets
* sizeof (unsigned int));
497 r
->suffixes
= (char **)
498 xrealloc ((char *) r
->suffixes
, max_targets
* sizeof (char *));
500 r
->lens
[i
] = strlen (targets
[i
]);
501 r
->suffixes
[i
] = (target_percents
== 0 ? find_percent (targets
[i
])
502 : target_percents
[i
]) + 1;
503 if (r
->suffixes
[i
] == 0)
507 if (i
< max_targets
- 1)
509 r
->lens
= (unsigned int *) xrealloc ((char *) r
->lens
,
510 (i
+ 1) * sizeof (unsigned int));
511 r
->suffixes
= (char **) xrealloc ((char *) r
->suffixes
,
512 (i
+ 1) * sizeof (char *));
515 if (new_pattern_rule (r
, override
))
516 r
->terminal
= terminal
;
519 /* Print the data base of rules. */
521 static void /* Useful to call from gdb. */
522 print_rule (struct rule
*r
)
524 register unsigned int i
;
525 register struct dep
*d
;
527 for (i
= 0; r
->targets
[i
] != 0; ++i
)
529 fputs (r
->targets
[i
], stdout
);
530 if (r
->targets
[i
+ 1] != 0)
538 for (d
= r
->deps
; d
!= 0; d
= d
->next
)
539 printf (" %s", dep_name (d
));
543 print_commands (r
->cmds
);
547 print_rule_data_base (void)
549 register unsigned int rules
, terminal
;
550 register struct rule
*r
;
552 puts (_("\n# Implicit Rules"));
554 rules
= terminal
= 0;
555 for (r
= pattern_rules
; r
!= 0; r
= r
->next
)
567 puts (_("\n# No implicit rules."));
570 printf (_("\n# %u implicit rules, %u"), rules
, terminal
);
572 printf (" (%.1f%%)", (double) terminal
/ (double) rules
* 100.0);
575 int f
= (terminal
* 1000 + 5) / rules
;
576 printf (" (%d.%d%%)", f
/10, f
%10);
579 puts (_(" terminal."));
582 if (num_pattern_rules
!= rules
)
584 /* This can happen if a fatal error was detected while reading the
585 makefiles and thus count_implicit_rule_limits wasn't called yet. */
586 if (num_pattern_rules
!= 0)
587 fatal (NILF
, _("BUG: num_pattern_rules wrong! %u != %u"),
588 num_pattern_rules
, rules
);