1 /* Data base of default implicit rules for GNU Make.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4 
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9 
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 
17 #include "makeint.h"
18 
19 #include <assert.h>
20 
21 #include "filedef.h"
22 #include "variable.h"
23 #include "rule.h"
24 #include "dep.h"
25 #include "job.h"
26 #include "commands.h"
27 
28 /* This is the default list of suffixes for suffix rules.
29    '.s' must come last, so that a '.o' file will be made from
30    a '.c' or '.p' or ... file rather than from a .s file.  */
31 
32 static char default_suffixes[]
33   = ".out .a .ln .o .c .cc .C .cpp .p .f .F .m .r .y .l .ym .yl .s .S \
34 .mod .sym .def .h .info .dvi .tex .texinfo .texi .txinfo \
35 .w .ch .web .sh .elc .el";
36 
37 static struct pspec default_pattern_rules[] =
38   {
39     { "(%)", "%",
40         "$(AR) $(ARFLAGS) $@ $<" },
41     /* The X.out rules are only in BSD's default set because
42        BSD Make has no null-suffix rules, so 'foo.out' and
43        'foo' are the same thing.  */
44     { "%.out", "%",
45         "@rm -f $@ \n cp $< $@" },
46 
47     /* Syntax is "ctangle foo.w foo.ch foo.c".  */
48     { "%.c", "%.w %.ch",
49         "$(CTANGLE) $^ $@" },
50     { "%.tex", "%.w %.ch",
51         "$(CWEAVE) $^ $@" },
52 
53     { 0, 0, 0 }
54   };
55 
56 static struct pspec default_terminal_rules[] =
57   {
58     /* RCS.  */
59     { "%", "%,v",
60         "$(CHECKOUT,v)" },
61     { "%", "RCS/%,v",
62         "$(CHECKOUT,v)" },
63     { "%", "RCS/%",
64         "$(CHECKOUT,v)" },
65 
66     /* SCCS.  */
67     { "%", "s.%",
68         "$(GET) $(GFLAGS) $(SCCS_OUTPUT_OPTION) $<" },
69     { "%", "SCCS/s.%",
70         "$(GET) $(GFLAGS) $(SCCS_OUTPUT_OPTION) $<" },
71     { 0, 0, 0 }
72   };
73 
74 static const char *default_suffix_rules[] =
75   {
76     ".o",
77     "$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@",
78     ".s",
79     "$(LINK.s) $^ $(LOADLIBES) $(LDLIBS) -o $@",
80     ".S",
81     "$(LINK.S) $^ $(LOADLIBES) $(LDLIBS) -o $@",
82     ".c",
83     "$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@",
84     ".cc",
85     "$(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@",
86     ".C",
87     "$(LINK.C) $^ $(LOADLIBES) $(LDLIBS) -o $@",
88     ".cpp",
89     "$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@",
90     ".f",
91     "$(LINK.f) $^ $(LOADLIBES) $(LDLIBS) -o $@",
92     ".m",
93     "$(LINK.m) $^ $(LOADLIBES) $(LDLIBS) -o $@",
94     ".p",
95     "$(LINK.p) $^ $(LOADLIBES) $(LDLIBS) -o $@",
96     ".F",
97     "$(LINK.F) $^ $(LOADLIBES) $(LDLIBS) -o $@",
98     ".r",
99     "$(LINK.r) $^ $(LOADLIBES) $(LDLIBS) -o $@",
100     ".mod",
101     "$(COMPILE.mod) -o $@ -e $@ $^",
102 
103     ".def.sym",
104     "$(COMPILE.def) -o $@ $<",
105 
106     ".sh",
107     "cat $< >$@ \n chmod a+x $@",
108 
109     ".s.o",
110     "$(COMPILE.s) -o $@ $<",
111     ".S.o",
112     "$(COMPILE.S) -o $@ $<",
113     ".c.o",
114     "$(COMPILE.c) $(OUTPUT_OPTION) $<",
115     ".cc.o",
116     "$(COMPILE.cc) $(OUTPUT_OPTION) $<",
117     ".C.o",
118     "$(COMPILE.C) $(OUTPUT_OPTION) $<",
119     ".cpp.o",
120     "$(COMPILE.cpp) $(OUTPUT_OPTION) $<",
121     ".f.o",
122     "$(COMPILE.f) $(OUTPUT_OPTION) $<",
123     ".m.o",
124     "$(COMPILE.m) $(OUTPUT_OPTION) $<",
125     ".p.o",
126     "$(COMPILE.p) $(OUTPUT_OPTION) $<",
127     ".F.o",
128     "$(COMPILE.F) $(OUTPUT_OPTION) $<",
129     ".r.o",
130     "$(COMPILE.r) $(OUTPUT_OPTION) $<",
131     ".mod.o",
132     "$(COMPILE.mod) -o $@ $<",
133 
134     ".c.ln",
135     "$(LINT.c) -C$* $<",
136     ".y.ln",
137     "$(YACC.y) $< \n $(LINT.c) -C$* y_tab.c \n $(RM) y_tab.c",
138     ".l.ln",
139     "@$(RM) $*.c\n $(LEX.l) $< > $*.c\n$(LINT.c) -i $*.c -o $@\n $(RM) $*.c",
140 
141     ".y.c",
142     "$(YACC.y) $< \n mv -f y.tab.c $@",
143     ".l.c",
144     "@$(RM) $@ \n $(LEX.l) $< > $@",
145     ".ym.m",
146     "$(YACC.m) $< \n mv -f y.tab.c $@",
147     ".lm.m",
148     "@$(RM) $@ \n $(LEX.m) $< > $@",
149 
150     ".F.f",
151     "$(PREPROCESS.F) $(OUTPUT_OPTION) $<",
152     ".r.f",
153     "$(PREPROCESS.r) $(OUTPUT_OPTION) $<",
154 
155     /* This might actually make lex.yy.c if there's no %R% directive in $*.l,
156        but in that case why were you trying to make $*.r anyway?  */
157     ".l.r",
158     "$(LEX.l) $< > $@ \n mv -f lex.yy.r $@",
159 
160     ".S.s",
161     "$(PREPROCESS.S) $< > $@",
162 
163     ".texinfo.info",
164     "$(MAKEINFO) $(MAKEINFO_FLAGS) $< -o $@",
165 
166     ".texi.info",
167     "$(MAKEINFO) $(MAKEINFO_FLAGS) $< -o $@",
168 
169     ".txinfo.info",
170     "$(MAKEINFO) $(MAKEINFO_FLAGS) $< -o $@",
171 
172     ".tex.dvi",
173     "$(TEX) $<",
174 
175     ".texinfo.dvi",
176     "$(TEXI2DVI) $(TEXI2DVI_FLAGS) $<",
177 
178     ".texi.dvi",
179     "$(TEXI2DVI) $(TEXI2DVI_FLAGS) $<",
180 
181     ".txinfo.dvi",
182     "$(TEXI2DVI) $(TEXI2DVI_FLAGS) $<",
183 
184     ".w.c",
185     "$(CTANGLE) $< - $@",       /* The '-' says there is no '.ch' file.  */
186 
187     ".web.p",
188     "$(TANGLE) $<",
189 
190     ".w.tex",
191     "$(CWEAVE) $< - $@",        /* The '-' says there is no '.ch' file.  */
192 
193     ".web.tex",
194     "$(WEAVE) $<",
195 
196     0, 0,
197   };
198 
199 static const char *default_variables[] =
200   {
201     "AR", "ar",
202     "ARFLAGS", "rv",
203     "AS", "as",
204 #ifdef GCC_IS_NATIVE
205     "CC", "gcc",
206     "CXX", "gcc",
207     "OBJC", "gcc",
208 #else
209     "CC", "cc",
210     "CXX", "g++",
211     "OBJC", "cc",
212 #endif
213 
214     /* This expands to $(CO) $(COFLAGS) $< $@ if $@ does not exist,
215        and to the empty string if $@ does exist.  */
216     "CHECKOUT,v", "+$(if $(wildcard $@),,$(CO) $(COFLAGS) $< $@)",
217     "CO", "co",
218     "COFLAGS", "",
219 
220     "CPP", "$(CC) -E",
221 #ifdef  CRAY
222     "CF77PPFLAGS", "-P",
223     "CF77PP", "/lib/cpp",
224     "CFT", "cft77",
225     "CF", "cf77",
226     "FC", "$(CF)",
227 #else   /* Not CRAY.  */
228 #ifdef  _IBMR2
229     "FC", "xlf",
230 #else
231 #ifdef  __convex__
232     "FC", "fc",
233 #else
234     "FC", "f77",
235 #endif /* __convex__ */
236 #endif /* _IBMR2 */
237     /* System V uses these, so explicit rules using them should work.
238        However, there is no way to make implicit rules use them and FC.  */
239     "F77", "$(FC)",
240     "F77FLAGS", "$(FFLAGS)",
241 #endif  /* Cray.  */
242     "GET", SCCS_GET,
243     "LD", "ld",
244 #ifdef GCC_IS_NATIVE
245     "LEX", "flex",
246 #else
247     "LEX", "lex",
248 #endif
249     "LINT", "lint",
250     "M2C", "m2c",
251 #ifdef  pyr
252     "PC", "pascal",
253 #else
254 #ifdef  CRAY
255     "PC", "PASCAL",
256     "SEGLDR", "segldr",
257 #else
258     "PC", "pc",
259 #endif  /* CRAY.  */
260 #endif  /* pyr.  */
261 #ifdef GCC_IS_NATIVE
262     "YACC", "bison -y",
263 #else
264     "YACC", "yacc",     /* Or "bison -y"  */
265 #endif
266     "MAKEINFO", "makeinfo",
267     "TEX", "tex",
268     "TEXI2DVI", "texi2dvi",
269     "WEAVE", "weave",
270     "CWEAVE", "cweave",
271     "TANGLE", "tangle",
272     "CTANGLE", "ctangle",
273 
274     "RM", "rm -f",
275 
276     "LINK.o", "$(CC) $(LDFLAGS) $(TARGET_ARCH)",
277     "COMPILE.c", "$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c",
278     "LINK.c", "$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
279     "COMPILE.m", "$(OBJC) $(OBJCFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c",
280     "LINK.m", "$(OBJC) $(OBJCFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
281     "COMPILE.cc", "$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c",
282 #ifndef HAVE_CASE_INSENSITIVE_FS
283     /* On case-insensitive filesystems, treat *.C files as *.c files,
284        to avoid erroneously compiling C sources as C++, which will
285        probably fail.  */
286     "COMPILE.C", "$(COMPILE.cc)",
287 #else
288     "COMPILE.C", "$(COMPILE.c)",
289 #endif
290     "COMPILE.cpp", "$(COMPILE.cc)",
291     "LINK.cc", "$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
292 #ifndef HAVE_CASE_INSENSITIVE_FS
293     "LINK.C", "$(LINK.cc)",
294 #else
295     "LINK.C", "$(LINK.c)",
296 #endif
297     "LINK.cpp", "$(LINK.cc)",
298     "YACC.y", "$(YACC) $(YFLAGS)",
299     "LEX.l", "$(LEX) $(LFLAGS) -t",
300     "YACC.m", "$(YACC) $(YFLAGS)",
301     "LEX.m", "$(LEX) $(LFLAGS) -t",
302     "COMPILE.f", "$(FC) $(FFLAGS) $(TARGET_ARCH) -c",
303     "LINK.f", "$(FC) $(FFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
304     "COMPILE.F", "$(FC) $(FFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c",
305     "LINK.F", "$(FC) $(FFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
306     "COMPILE.r", "$(FC) $(FFLAGS) $(RFLAGS) $(TARGET_ARCH) -c",
307     "LINK.r", "$(FC) $(FFLAGS) $(RFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
308     "COMPILE.def", "$(M2C) $(M2FLAGS) $(DEFFLAGS) $(TARGET_ARCH)",
309     "COMPILE.mod", "$(M2C) $(M2FLAGS) $(MODFLAGS) $(TARGET_ARCH)",
310     "COMPILE.p", "$(PC) $(PFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c",
311     "LINK.p", "$(PC) $(PFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)",
312     "LINK.s", "$(CC) $(ASFLAGS) $(LDFLAGS) $(TARGET_MACH)",
313     "COMPILE.s", "$(AS) $(ASFLAGS) $(TARGET_MACH)",
314     "LINK.S", "$(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_MACH)",
315     "COMPILE.S", "$(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_MACH) -c",
316     "PREPROCESS.S", "$(CC) -E $(CPPFLAGS)",
317     "PREPROCESS.F", "$(FC) $(FFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -F",
318     "PREPROCESS.r", "$(FC) $(FFLAGS) $(RFLAGS) $(TARGET_ARCH) -F",
319     "LINT.c", "$(LINT) $(LINTFLAGS) $(CPPFLAGS) $(TARGET_ARCH)",
320 
321 #ifndef NO_MINUS_C_MINUS_O
322     "OUTPUT_OPTION", "-o $@",
323 #endif
324 
325 #ifdef  SCCS_GET_MINUS_G
326     "SCCS_OUTPUT_OPTION", "-G$@",
327 #endif
328 
329 #if defined(__APPLE__)
330     ".LIBPATTERNS", "lib%.dylib lib%.a",
331 #elif defined(__CYGWIN__) || defined(WINDOWS32)
332     ".LIBPATTERNS", "lib%.dll.a %.dll.a lib%.a %.lib lib%.dll %.dll",
333 #else
334     ".LIBPATTERNS", "lib%.so lib%.a",
335 #endif
336 
337     /* Make this assignment to avoid undefined variable warnings.  */
338     "GNUMAKEFLAGS", "",
339     0, 0
340   };
341 
342 /* Set up the default .SUFFIXES list.  */
343 
344 void
set_default_suffixes(void)345 set_default_suffixes (void)
346 {
347   suffix_file = enter_file (strcache_add (".SUFFIXES"));
348   suffix_file->builtin = 1;
349 
350   if (no_builtin_rules_flag)
351     define_variable_cname ("SUFFIXES", "", o_default, 0);
352   else
353     {
354       struct dep *d;
355       const char *p = default_suffixes;
356       suffix_file->deps = enter_prereqs (PARSE_SIMPLE_SEQ ((char **)&p, struct dep),
357                                          NULL);
358       for (d = suffix_file->deps; d; d = d->next)
359         d->file->builtin = 1;
360 
361       define_variable_cname ("SUFFIXES", default_suffixes, o_default, 0);
362     }
363 }
364 
365 /* Enter the default suffix rules as file rules.  This used to be done in
366    install_default_implicit_rules, but that loses because we want the
367    suffix rules installed before reading makefiles, and the pattern rules
368    installed after.  */
369 
370 void
install_default_suffix_rules(void)371 install_default_suffix_rules (void)
372 {
373   const char **s;
374 
375   if (no_builtin_rules_flag)
376     return;
377 
378   for (s = default_suffix_rules; *s != 0; s += 2)
379     {
380       struct file *f = enter_file (strcache_add (s[0]));
381       /* This function should run before any makefile is parsed.  */
382       assert (f->cmds == 0);
383       f->cmds = xmalloc (sizeof (struct commands));
384       f->cmds->fileinfo.filenm = 0;
385       f->cmds->commands = xstrdup (s[1]);
386       f->cmds->command_lines = 0;
387       f->cmds->recipe_prefix = RECIPEPREFIX_DEFAULT;
388       f->builtin = 1;
389     }
390 }
391 
392 
393 /* Install the default pattern rules.  */
394 
395 void
install_default_implicit_rules(void)396 install_default_implicit_rules (void)
397 {
398   struct pspec *p;
399 
400   if (no_builtin_rules_flag)
401     return;
402 
403   for (p = default_pattern_rules; p->target != 0; ++p)
404     install_pattern_rule (p, 0);
405 
406   for (p = default_terminal_rules; p->target != 0; ++p)
407     install_pattern_rule (p, 1);
408 }
409 
410 void
define_default_variables(void)411 define_default_variables (void)
412 {
413   const char **s;
414 
415   if (no_builtin_variables_flag)
416     return;
417 
418   for (s = default_variables; *s != 0; s += 2)
419     define_variable (s[0], strlen (s[0]), s[1], o_default, 1);
420 }
421 
422 void
undefine_default_variables(void)423 undefine_default_variables (void)
424 {
425   const char **s;
426 
427   for (s = default_variables; *s != 0; s += 2)
428     undefine_variable_global (s[0], strlen (s[0]), o_default);
429 }
430