1 /* Pattern and suffix rule internals for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 static void freerule ();
28 /* Chain of all pattern rules. */
30 struct rule
*pattern_rules
;
32 /* Pointer to last rule in the chain, so we can add onto the end. */
34 struct rule
*last_pattern_rule
;
36 /* Number of rules in the chain. */
38 unsigned int num_pattern_rules
;
40 /* Maximum number of target patterns of any pattern rule. */
42 unsigned int max_pattern_targets
;
44 /* Maximum number of dependencies of any pattern rule. */
46 unsigned int max_pattern_deps
;
48 /* Maximum length of the name of a dependencies of any pattern rule. */
50 unsigned int max_pattern_dep_length
;
52 /* Pointer to structure for the file .SUFFIXES
53 whose dependencies are the suffixes to be searched. */
55 struct file
*suffix_file
;
57 /* Maximum length of a suffix. */
59 unsigned int maxsuffix
;
61 /* Compute the maximum dependency length and maximum number of
62 dependencies of all implicit rules. Also sets the subdir
63 flag for a rule when appropriate, possibly removing the rule
64 completely when appropriate. */
67 count_implicit_rule_limits ()
71 register struct rule
*rule
, *lastrule
;
73 num_pattern_rules
= max_pattern_targets
= max_pattern_deps
= 0;
74 max_pattern_dep_length
= 0;
82 unsigned int ndeps
= 0;
83 register struct dep
*dep
;
84 struct rule
*next
= rule
->next
;
85 unsigned int ntargets
;
90 while (rule
->targets
[ntargets
] != 0)
93 if (ntargets
> max_pattern_targets
)
94 max_pattern_targets
= ntargets
;
96 for (dep
= rule
->deps
; dep
!= 0; dep
= dep
->next
)
98 unsigned int len
= strlen (dep
->name
);
99 char *p
= rindex (dep
->name
, '/');
100 char *p2
= p
!= 0 ? index (dep
->name
, '%') : 0;
104 if (len
> max_pattern_dep_length
)
105 max_pattern_dep_length
= len
;
107 if (p
!= 0 && p2
> p
)
109 /* There is a slash before the % in the dep name.
110 Extract the directory name. */
113 if (p
- dep
->name
> namelen
)
117 namelen
= p
- dep
->name
;
118 name
= (char *) xmalloc (namelen
+ 1);
120 bcopy (dep
->name
, name
, p
- dep
->name
);
121 name
[p
- dep
->name
] = '\0';
123 /* In the deps of an implicit rule the `changed' flag
124 actually indicates that the dependency is in a
125 nonexistent subdirectory. */
127 dep
->changed
= !dir_file_exists_p (name
, "");
128 if (dep
->changed
&& *name
== '/')
130 /* The name is absolute and the directory does not exist.
131 This rule can never possibly match, since this dependency
132 can never possibly exist. So just remove the rule from
134 freerule (rule
, lastrule
);
140 /* This dependency does not reside in a subdirectory. */
144 if (ndeps
> max_pattern_deps
)
145 max_pattern_deps
= ndeps
;
156 /* Create a pattern rule from a suffix rule.
157 TARGET is the target suffix; SOURCE is the source suffix.
158 CMDS are the commands.
159 If TARGET is nil, it means the target pattern should be `(%.o)'.
160 If SOURCE is nil, it means there should be no deps. */
163 convert_suffix_rule (target
, source
, cmds
)
164 char *target
, *source
;
165 struct commands
*cmds
;
167 char *targname
, *targpercent
, *depname
;
168 char **names
, **percents
;
173 /* Special case: TARGET being nil means we are defining a
174 `.X.a' suffix rule; the target pattern is always `(%.o)'. */
176 targname
= savestring ("(%.o)", 5);
177 targpercent
= targname
+ 1;
181 /* Construct the target name. */
182 len
= strlen (target
);
183 targname
= xmalloc (1 + len
+ 1);
185 bcopy (target
, targname
+ 1, len
+ 1);
186 targpercent
= targname
;
189 names
= (char **) xmalloc (2 * sizeof (char *));
190 percents
= (char **) alloca (2 * sizeof (char *));
192 percents
[0] = targpercent
;
193 names
[1] = percents
[1] = 0;
199 /* Construct the dependency name. */
200 len
= strlen (source
);
201 depname
= xmalloc (1 + len
+ 1);
203 bcopy (source
, depname
+ 1, len
+ 1);
204 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
206 deps
->name
= depname
;
209 create_pattern_rule (names
, percents
, 0, deps
, cmds
, 0);
212 /* Convert old-style suffix rules to pattern rules.
213 All rules for the suffixes on the .SUFFIXES list
214 are converted and added to the chain of pattern rules. */
217 convert_to_pattern ()
219 register struct dep
*d
, *d2
;
220 register struct file
*f
;
221 register char *rulename
;
222 register unsigned int slen
, s2len
;
224 /* Compute maximum length of all the suffixes. */
227 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
229 register unsigned int namelen
= strlen (dep_name (d
));
230 if (namelen
> maxsuffix
)
234 rulename
= (char *) alloca ((maxsuffix
* 2) + 1);
236 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
238 /* Make a rule that is just the suffix, with no deps or commands.
239 This rule exists solely to disqualify match-anything rules. */
240 convert_suffix_rule (dep_name (d
), (char *) 0, (struct commands
*) 0);
244 /* Record a pattern for this suffix's null-suffix rule. */
245 convert_suffix_rule ("", dep_name (d
), f
->cmds
);
247 /* Record a pattern for each of this suffix's two-suffix rules. */
248 slen
= strlen (dep_name (d
));
249 bcopy (dep_name (d
), rulename
, slen
);
250 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
252 s2len
= strlen (dep_name (d2
));
254 if (slen
== s2len
&& streq (dep_name (d
), dep_name (d2
)))
257 bcopy (dep_name (d2
), rulename
+ slen
, s2len
+ 1);
258 f
= lookup_file (rulename
);
259 if (f
== 0 || f
->cmds
== 0)
262 if (s2len
== 2 && rulename
[slen
] == '.' && rulename
[slen
+ 1] == 'a')
263 /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
264 It also generates a normal `%.a: %.X' rule below. */
265 convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'. */
269 /* The suffix rule `.X.Y:' is converted
270 to the pattern rule `%.Y: %.X'. */
271 convert_suffix_rule (dep_name (d2
), dep_name (d
), f
->cmds
);
277 /* Install the pattern rule RULE (whose fields have been filled in)
278 at the end of the list (so that any rules previously defined
279 will take precedence). If this rule duplicates a previous one
280 (identical target and dependencies), the old one is replaced
281 if OVERRIDE is nonzero, otherwise this new one is thrown out.
282 When an old rule is replaced, the new one is put at the end of the
283 list. Return nonzero if RULE is used; zero if not. */
286 new_pattern_rule (rule
, override
)
287 register struct rule
*rule
;
290 register struct rule
*r
, *lastrule
;
291 register unsigned int i
, j
;
298 /* Search for an identical rule. */
300 for (r
= pattern_rules
; r
!= 0; lastrule
= r
, r
= r
->next
)
301 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
303 for (j
= 0; r
->targets
[j
] != 0; ++j
)
304 if (!streq (rule
->targets
[i
], r
->targets
[j
]))
306 if (r
->targets
[j
] == 0)
307 /* All the targets matched. */
309 register struct dep
*d
, *d2
;
310 for (d
= rule
->deps
, d2
= r
->deps
;
311 d
!= 0 && d2
!= 0; d
= d
->next
, d2
= d2
->next
)
312 if (!streq (dep_name (d
), dep_name (d2
)))
314 if (d
== 0 && d2
== 0)
315 /* All the dependencies matched. */
318 /* Remove the old rule. */
319 freerule (r
, lastrule
);
320 /* Install the new one. */
321 if (pattern_rules
== 0)
322 pattern_rules
= rule
;
324 last_pattern_rule
->next
= rule
;
325 last_pattern_rule
= rule
;
327 /* We got one. Stop looking. */
332 /* The old rule stays intact. Destroy the new one. */
333 freerule (rule
, (struct rule
*) 0);
343 /* There was no rule to replace. */
344 if (pattern_rules
== 0)
345 pattern_rules
= rule
;
347 last_pattern_rule
->next
= rule
;
348 last_pattern_rule
= rule
;
355 /* Install an implicit pattern rule based on the three text strings
356 in the structure P points to. These strings come from one of
357 the arrays of default implicit pattern rules.
358 TERMINAL specifies what the `terminal' field of the rule should be. */
361 install_pattern_rule (p
, terminal
)
365 register struct rule
*r
;
368 r
= (struct rule
*) xmalloc (sizeof (struct rule
));
370 r
->targets
= (char **) xmalloc (2 * sizeof (char *));
371 r
->suffixes
= (char **) xmalloc (2 * sizeof (char *));
372 r
->lens
= (unsigned int *) xmalloc (2 * sizeof (unsigned int));
378 r
->lens
[0] = strlen (p
->target
);
379 /* These will all be string literals, but we malloc space for
380 them anyway because somebody might want to free them later on. */
381 r
->targets
[0] = savestring (p
->target
, r
->lens
[0]);
382 r
->suffixes
[0] = find_percent (r
->targets
[0]);
383 if (r
->suffixes
[0] == 0)
384 /* Programmer-out-to-lunch error. */
390 r
->deps
= (struct dep
*) multi_glob (parse_file_seq (&ptr
, '\0',
391 sizeof (struct dep
), 1),
392 sizeof (struct dep
));
394 if (new_pattern_rule (r
, 0))
396 r
->terminal
= terminal
;
397 r
->cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
398 r
->cmds
->filename
= 0;
400 /* These will all be string literals, but we malloc space for them
401 anyway because somebody might want to free them later. */
402 r
->cmds
->commands
= savestring (p
->commands
, strlen (p
->commands
));
403 r
->cmds
->command_lines
= 0;
408 /* Free all the storage used in RULE and take it out of the
409 pattern_rules chain. LASTRULE is the rule whose next pointer
413 freerule (rule
, lastrule
)
414 register struct rule
*rule
, *lastrule
;
416 struct rule
*next
= rule
->next
;
417 register unsigned int i
;
419 for (i
= 0; rule
->targets
[i
] != 0; ++i
)
420 free (rule
->targets
[i
]);
422 free ((char *) rule
->targets
);
423 free ((char *) rule
->suffixes
);
424 free ((char *) rule
->lens
);
426 /* We can't free the storage for the commands because there
427 are ways that they could be in more than one place:
428 * If the commands came from a suffix rule, they could also be in
429 the `struct file's for other suffix rules or plain targets given
430 on the same makefile line.
431 * If two suffixes that together make a two-suffix rule were each
432 given twice in the .SUFFIXES list, and in the proper order, two
433 identical pattern rules would be created and the second one would
434 be discarded here, but both would contain the same `struct commands'
435 pointer from the `struct file' for the suffix rule. */
437 free ((char *) rule
);
439 if (pattern_rules
== rule
)
443 pattern_rules
= next
;
444 else if (lastrule
!= 0)
445 lastrule
->next
= next
;
446 if (last_pattern_rule
== rule
)
447 last_pattern_rule
= lastrule
;
450 /* Create a new pattern rule with the targets in the nil-terminated
451 array TARGETS. If TARGET_PERCENTS is not nil, it is an array of
452 pointers into the elements of TARGETS, where the `%'s are.
453 The new rule has dependencies DEPS and commands from COMMANDS.
454 It is a terminal rule if TERMINAL is nonzero. This rule overrides
455 identical rules with different commands if OVERRIDE is nonzero.
457 The storage for TARGETS and its elements is used and must not be freed
458 until the rule is destroyed. The storage for TARGET_PERCENTS is not used;
462 create_pattern_rule (targets
, target_percents
,
463 terminal
, deps
, commands
, override
)
464 char **targets
, **target_percents
;
467 struct commands
*commands
;
470 register struct rule
*r
= (struct rule
*) xmalloc (sizeof (struct rule
));
471 register unsigned int max_targets
, i
;
475 r
->targets
= targets
;
478 r
->lens
= (unsigned int *) xmalloc (2 * sizeof (unsigned int));
479 r
->suffixes
= (char **) xmalloc (2 * sizeof (char *));
480 for (i
= 0; targets
[i
] != 0; ++i
)
482 if (i
== max_targets
- 1)
485 r
->lens
= (unsigned int *)
486 xrealloc ((char *) r
->lens
, max_targets
* sizeof (unsigned int));
487 r
->suffixes
= (char **)
488 xrealloc ((char *) r
->suffixes
, max_targets
* sizeof (char *));
490 r
->lens
[i
] = strlen (targets
[i
]);
491 r
->suffixes
[i
] = (target_percents
== 0 ? find_percent (targets
[i
])
492 : target_percents
[i
]) + 1;
493 if (r
->suffixes
[i
] == 0)
497 if (i
< max_targets
- 1)
499 r
->lens
= (unsigned int *) xrealloc ((char *) r
->lens
,
500 (i
+ 1) * sizeof (unsigned int));
501 r
->suffixes
= (char **) xrealloc ((char *) r
->suffixes
,
502 (i
+ 1) * sizeof (char *));
505 if (new_pattern_rule (r
, override
))
506 r
->terminal
= terminal
;
509 /* Print the data base of rules. */
511 static void /* Useful to call from gdb. */
515 register unsigned int i
;
516 register struct dep
*d
;
518 for (i
= 0; r
->targets
[i
] != 0; ++i
)
520 fputs (r
->targets
[i
], stdout
);
521 if (r
->targets
[i
+ 1] != 0)
529 for (d
= r
->deps
; d
!= 0; d
= d
->next
)
530 printf (" %s", dep_name (d
));
534 print_commands (r
->cmds
);
538 print_rule_data_base ()
540 register unsigned int rules
, terminal
;
541 register struct rule
*r
;
543 puts ("\n# Implicit Rules");
545 rules
= terminal
= 0;
546 for (r
= pattern_rules
; r
!= 0; r
= r
->next
)
558 puts ("\n# No implicit rules.");
561 printf ("\n# %u implicit rules, %u", rules
, terminal
);
563 printf (" (%.1f%%)", (double) terminal
/ (double) rules
* 100.0);
568 if (num_pattern_rules
!= rules
)
569 fatal ("BUG: num_pattern_rules wrong! %u != %u",
570 num_pattern_rules
, rules
);