xref: /OK3568_Linux_fs/u-boot/scripts/basic/fixdep.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * "Optimize" a list of dependencies as spit out by gcc -MD
3*4882a593Smuzhiyun  * for the kernel build
4*4882a593Smuzhiyun  * ===========================================================================
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Author       Kai Germaschewski
7*4882a593Smuzhiyun  * Copyright    2002 by Kai Germaschewski  <kai.germaschewski@gmx.de>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This software may be used and distributed according to the terms
10*4882a593Smuzhiyun  * of the GNU General Public License, incorporated herein by reference.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Introduction:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * gcc produces a very nice and correct list of dependencies which
16*4882a593Smuzhiyun  * tells make when to remake a file.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * To use this list as-is however has the drawback that virtually
19*4882a593Smuzhiyun  * every file in the kernel includes autoconf.h.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * If the user re-runs make *config, autoconf.h will be
22*4882a593Smuzhiyun  * regenerated.  make notices that and will rebuild every file which
23*4882a593Smuzhiyun  * includes autoconf.h, i.e. basically all files. This is extremely
24*4882a593Smuzhiyun  * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * So we play the same trick that "mkdep" played before. We replace
27*4882a593Smuzhiyun  * the dependency on autoconf.h by a dependency on every config
28*4882a593Smuzhiyun  * option which is mentioned in any of the listed prequisites.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * kconfig populates a tree in include/config/ with an empty file
31*4882a593Smuzhiyun  * for each config symbol and when the configuration is updated
32*4882a593Smuzhiyun  * the files representing changed config options are touched
33*4882a593Smuzhiyun  * which then let make pick up the changes and the files that use
34*4882a593Smuzhiyun  * the config symbols are rebuilt.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
37*4882a593Smuzhiyun  * which depend on "include/linux/config/his/driver.h" will be rebuilt,
38*4882a593Smuzhiyun  * so most likely only his driver ;-)
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * So to get dependencies right, there are two issues:
43*4882a593Smuzhiyun  * o if any of the files the compiler read changed, we need to rebuild
44*4882a593Smuzhiyun  * o if the command line given to the compile the file changed, we
45*4882a593Smuzhiyun  *   better rebuild as well.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * The former is handled by using the -MD output, the later by saving
48*4882a593Smuzhiyun  * the command line used to compile the old object and comparing it
49*4882a593Smuzhiyun  * to the one we would now use.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Again, also this idea is pretty old and has been discussed on
52*4882a593Smuzhiyun  * kbuild-devel a long time ago. I don't have a sensibly working
53*4882a593Smuzhiyun  * internet connection right now, so I rather don't mention names
54*4882a593Smuzhiyun  * without double checking.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * This code here has been based partially based on mkdep.c, which
57*4882a593Smuzhiyun  * says the following about its history:
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  *   Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
60*4882a593Smuzhiyun  *   This is a C version of syncdep.pl by Werner Almesberger.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * It is invoked as
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  *   fixdep <depfile> <target> <cmdline>
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * and will read the dependency file <depfile>
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * The transformed dependency snipped is written to stdout.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * It first generates a line
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  *   cmd_<target> = <cmdline>
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * and then basically copies the .<target>.d file to stdout, in the
76*4882a593Smuzhiyun  * process filtering out the dependency on autoconf.h and adding
77*4882a593Smuzhiyun  * dependencies on include/config/my/option.h for every
78*4882a593Smuzhiyun  * CONFIG_MY_OPTION encountered in any of the prequisites.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * It will also filter out all the dependencies on *.ver. We need
81*4882a593Smuzhiyun  * to make sure that the generated version checksum are globally up
82*4882a593Smuzhiyun  * to date before even starting the recursive build, so it's too late
83*4882a593Smuzhiyun  * at this point anyway.
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * The algorithm to grep for "CONFIG_..." is bit unusual, but should
86*4882a593Smuzhiyun  * be fast ;-) We don't even try to really parse the header files, but
87*4882a593Smuzhiyun  * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
88*4882a593Smuzhiyun  * be picked up as well. It's not a problem with respect to
89*4882a593Smuzhiyun  * correctness, since that can only give too many dependencies, thus
90*4882a593Smuzhiyun  * we cannot miss a rebuild. Since people tend to not mention totally
91*4882a593Smuzhiyun  * unrelated CONFIG_ options all over the place, it's not an
92*4882a593Smuzhiyun  * efficiency problem either.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * (Note: it'd be easy to port over the complete mkdep state machine,
95*4882a593Smuzhiyun  *  but I don't think the added complexity is worth it)
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto
99*4882a593Smuzhiyun  * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not
100*4882a593Smuzhiyun  * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as
101*4882a593Smuzhiyun  * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h,
102*4882a593Smuzhiyun  * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that
103*4882a593Smuzhiyun  * those files will have correct dependencies.
104*4882a593Smuzhiyun  */
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #include <sys/types.h>
107*4882a593Smuzhiyun #include <sys/stat.h>
108*4882a593Smuzhiyun #include <sys/mman.h>
109*4882a593Smuzhiyun #include <unistd.h>
110*4882a593Smuzhiyun #include <fcntl.h>
111*4882a593Smuzhiyun #include <string.h>
112*4882a593Smuzhiyun #include <stdlib.h>
113*4882a593Smuzhiyun #include <stdio.h>
114*4882a593Smuzhiyun #include <limits.h>
115*4882a593Smuzhiyun #include <ctype.h>
116*4882a593Smuzhiyun #include <arpa/inet.h>
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define INT_CONF ntohl(0x434f4e46)
119*4882a593Smuzhiyun #define INT_ONFI ntohl(0x4f4e4649)
120*4882a593Smuzhiyun #define INT_NFIG ntohl(0x4e464947)
121*4882a593Smuzhiyun #define INT_FIG_ ntohl(0x4649475f)
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun char *target;
124*4882a593Smuzhiyun char *depfile;
125*4882a593Smuzhiyun char *cmdline;
126*4882a593Smuzhiyun int is_spl_build = 0; /* hack for U-Boot */
127*4882a593Smuzhiyun 
usage(void)128*4882a593Smuzhiyun static void usage(void)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
131*4882a593Smuzhiyun 	exit(1);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun  * Print out the commandline prefixed with cmd_<target filename> :=
136*4882a593Smuzhiyun  */
print_cmdline(void)137*4882a593Smuzhiyun static void print_cmdline(void)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	printf("cmd_%s := %s\n\n", target, cmdline);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun struct item {
143*4882a593Smuzhiyun 	struct item	*next;
144*4882a593Smuzhiyun 	unsigned int	len;
145*4882a593Smuzhiyun 	unsigned int	hash;
146*4882a593Smuzhiyun 	char		name[0];
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #define HASHSZ 256
150*4882a593Smuzhiyun static struct item *hashtab[HASHSZ];
151*4882a593Smuzhiyun 
strhash(const char * str,unsigned int sz)152*4882a593Smuzhiyun static unsigned int strhash(const char *str, unsigned int sz)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	/* fnv32 hash */
155*4882a593Smuzhiyun 	unsigned int i, hash = 2166136261U;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	for (i = 0; i < sz; i++)
158*4882a593Smuzhiyun 		hash = (hash ^ str[i]) * 0x01000193;
159*4882a593Smuzhiyun 	return hash;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun  * Lookup a value in the configuration string.
164*4882a593Smuzhiyun  */
is_defined_config(const char * name,int len,unsigned int hash)165*4882a593Smuzhiyun static int is_defined_config(const char *name, int len, unsigned int hash)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct item *aux;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
170*4882a593Smuzhiyun 		if (aux->hash == hash && aux->len == len &&
171*4882a593Smuzhiyun 		    memcmp(aux->name, name, len) == 0)
172*4882a593Smuzhiyun 			return 1;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 	return 0;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun  * Add a new value to the configuration string.
179*4882a593Smuzhiyun  */
define_config(const char * name,int len,unsigned int hash)180*4882a593Smuzhiyun static void define_config(const char *name, int len, unsigned int hash)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	struct item *aux = malloc(sizeof(*aux) + len);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (!aux) {
185*4882a593Smuzhiyun 		perror("fixdep:malloc");
186*4882a593Smuzhiyun 		exit(1);
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	memcpy(aux->name, name, len);
189*4882a593Smuzhiyun 	aux->len = len;
190*4882a593Smuzhiyun 	aux->hash = hash;
191*4882a593Smuzhiyun 	aux->next = hashtab[hash % HASHSZ];
192*4882a593Smuzhiyun 	hashtab[hash % HASHSZ] = aux;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun  * Record the use of a CONFIG_* word.
197*4882a593Smuzhiyun  */
use_config(const char * m,int slen)198*4882a593Smuzhiyun static void use_config(const char *m, int slen)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	unsigned int hash = strhash(m, slen);
201*4882a593Smuzhiyun 	int c, i;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	if (is_defined_config(m, slen, hash))
204*4882a593Smuzhiyun 	    return;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	define_config(m, slen, hash);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	printf("    $(wildcard include/config/");
209*4882a593Smuzhiyun 	for (i = 0; i < slen; i++) {
210*4882a593Smuzhiyun 		c = m[i];
211*4882a593Smuzhiyun 		if (c == '_')
212*4882a593Smuzhiyun 			c = '/';
213*4882a593Smuzhiyun 		else
214*4882a593Smuzhiyun 			c = tolower(c);
215*4882a593Smuzhiyun 		putchar(c);
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 	printf(".h) \\\n");
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
parse_config_file(const char * map,size_t len)220*4882a593Smuzhiyun static void parse_config_file(const char *map, size_t len)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	const int *end = (const int *) (map + len);
223*4882a593Smuzhiyun 	/* start at +1, so that p can never be < map */
224*4882a593Smuzhiyun 	const int *m   = (const int *) map + 1;
225*4882a593Smuzhiyun 	const char *p, *q;
226*4882a593Smuzhiyun 	char tmp_buf[256] = "SPL_"; /* hack for U-Boot */
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	for (; m < end; m++) {
229*4882a593Smuzhiyun 		if (*m == INT_CONF) { p = (char *) m  ; goto conf; }
230*4882a593Smuzhiyun 		if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
231*4882a593Smuzhiyun 		if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
232*4882a593Smuzhiyun 		if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
233*4882a593Smuzhiyun 		continue;
234*4882a593Smuzhiyun 	conf:
235*4882a593Smuzhiyun 		if (p > map + len - 7)
236*4882a593Smuzhiyun 			continue;
237*4882a593Smuzhiyun 		if (memcmp(p, "CONFIG_", 7))
238*4882a593Smuzhiyun 			continue;
239*4882a593Smuzhiyun 		p += 7;
240*4882a593Smuzhiyun 		for (q = p; q < map + len; q++) {
241*4882a593Smuzhiyun 			if (!(isalnum(*q) || *q == '_'))
242*4882a593Smuzhiyun 				goto found;
243*4882a593Smuzhiyun 		}
244*4882a593Smuzhiyun 		continue;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	found:
247*4882a593Smuzhiyun 		if (!memcmp(q - 7, "_MODULE", 7))
248*4882a593Smuzhiyun 			q -= 7;
249*4882a593Smuzhiyun 		if (q - p < 0)
250*4882a593Smuzhiyun 			continue;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		/*
253*4882a593Smuzhiyun 		 * U-Boot also handles
254*4882a593Smuzhiyun 		 *   CONFIG_IS_ENABLED(...)
255*4882a593Smuzhiyun 		 *   CONFIG_IS_BUILTIN(...)
256*4882a593Smuzhiyun 		 *   CONFIG_IS_MODULE(...)
257*4882a593Smuzhiyun 		 *   CONFIG_VAL(...)
258*4882a593Smuzhiyun 		 */
259*4882a593Smuzhiyun 		if ((q - p == 10 && !memcmp(p, "IS_ENABLED(", 11)) ||
260*4882a593Smuzhiyun 		    (q - p == 10 && !memcmp(p, "IS_BUILTIN(", 11)) ||
261*4882a593Smuzhiyun 		    (q - p == 9 && !memcmp(p, "IS_MODULE(", 10)) ||
262*4882a593Smuzhiyun 		    (q - p == 3 && !memcmp(p, "VAL(", 4))) {
263*4882a593Smuzhiyun 			p = q + 1;
264*4882a593Smuzhiyun 			for (q = p; q < map + len; q++)
265*4882a593Smuzhiyun 				if (*q == ')')
266*4882a593Smuzhiyun 					goto found2;
267*4882a593Smuzhiyun 			continue;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 		found2:
270*4882a593Smuzhiyun 			if (is_spl_build) {
271*4882a593Smuzhiyun 				memcpy(tmp_buf + 4, p, q - p);
272*4882a593Smuzhiyun 				q = tmp_buf + 4 + (q - p);
273*4882a593Smuzhiyun 				p = tmp_buf;
274*4882a593Smuzhiyun 			}
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 		/* end U-Boot hack */
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 		use_config(p, q - p);
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /* test is s ends in sub */
strrcmp(char * s,char * sub)283*4882a593Smuzhiyun static int strrcmp(char *s, char *sub)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	int slen = strlen(s);
286*4882a593Smuzhiyun 	int sublen = strlen(sub);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	if (sublen > slen)
289*4882a593Smuzhiyun 		return 1;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	return memcmp(s + slen - sublen, sub, sublen);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
do_config_file(const char * filename)294*4882a593Smuzhiyun static void do_config_file(const char *filename)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	struct stat st;
297*4882a593Smuzhiyun 	int fd;
298*4882a593Smuzhiyun 	void *map;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	fd = open(filename, O_RDONLY);
301*4882a593Smuzhiyun 	if (fd < 0) {
302*4882a593Smuzhiyun 		fprintf(stderr, "fixdep: error opening config file: ");
303*4882a593Smuzhiyun 		perror(filename);
304*4882a593Smuzhiyun 		exit(2);
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 	if (fstat(fd, &st) < 0) {
307*4882a593Smuzhiyun 		fprintf(stderr, "fixdep: error fstat'ing config file: ");
308*4882a593Smuzhiyun 		perror(filename);
309*4882a593Smuzhiyun 		exit(2);
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 	if (st.st_size == 0) {
312*4882a593Smuzhiyun 		close(fd);
313*4882a593Smuzhiyun 		return;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
316*4882a593Smuzhiyun 	if ((long) map == -1) {
317*4882a593Smuzhiyun 		perror("fixdep: mmap");
318*4882a593Smuzhiyun 		close(fd);
319*4882a593Smuzhiyun 		return;
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	parse_config_file(map, st.st_size);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	munmap(map, st.st_size);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	close(fd);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun  * Important: The below generated source_foo.o and deps_foo.o variable
331*4882a593Smuzhiyun  * assignments are parsed not only by make, but also by the rather simple
332*4882a593Smuzhiyun  * parser in scripts/mod/sumversion.c.
333*4882a593Smuzhiyun  */
parse_dep_file(void * map,size_t len)334*4882a593Smuzhiyun static void parse_dep_file(void *map, size_t len)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	char *m = map;
337*4882a593Smuzhiyun 	char *end = m + len;
338*4882a593Smuzhiyun 	char *p;
339*4882a593Smuzhiyun 	char s[PATH_MAX];
340*4882a593Smuzhiyun 	int is_target;
341*4882a593Smuzhiyun 	int saw_any_target = 0;
342*4882a593Smuzhiyun 	int is_first_dep = 0;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	while (m < end) {
345*4882a593Smuzhiyun 		/* Skip any "white space" */
346*4882a593Smuzhiyun 		while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
347*4882a593Smuzhiyun 			m++;
348*4882a593Smuzhiyun 		/* Find next "white space" */
349*4882a593Smuzhiyun 		p = m;
350*4882a593Smuzhiyun 		while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
351*4882a593Smuzhiyun 			p++;
352*4882a593Smuzhiyun 		/* Is the token we found a target name? */
353*4882a593Smuzhiyun 		is_target = (*(p-1) == ':');
354*4882a593Smuzhiyun 		/* Don't write any target names into the dependency file */
355*4882a593Smuzhiyun 		if (is_target) {
356*4882a593Smuzhiyun 			/* The /next/ file is the first dependency */
357*4882a593Smuzhiyun 			is_first_dep = 1;
358*4882a593Smuzhiyun 		} else {
359*4882a593Smuzhiyun 			/* Save this token/filename */
360*4882a593Smuzhiyun 			memcpy(s, m, p-m);
361*4882a593Smuzhiyun 			s[p - m] = 0;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 			/* Ignore certain dependencies */
364*4882a593Smuzhiyun 			if (strrcmp(s, "include/generated/autoconf.h") &&
365*4882a593Smuzhiyun 			    strrcmp(s, "arch/um/include/uml-config.h") &&
366*4882a593Smuzhiyun 			    strrcmp(s, "include/linux/kconfig.h") &&
367*4882a593Smuzhiyun 			    strrcmp(s, ".ver")) {
368*4882a593Smuzhiyun 				/*
369*4882a593Smuzhiyun 				 * Do not list the source file as dependency,
370*4882a593Smuzhiyun 				 * so that kbuild is not confused if a .c file
371*4882a593Smuzhiyun 				 * is rewritten into .S or vice versa. Storing
372*4882a593Smuzhiyun 				 * it in source_* is needed for modpost to
373*4882a593Smuzhiyun 				 * compute srcversions.
374*4882a593Smuzhiyun 				 */
375*4882a593Smuzhiyun 				if (is_first_dep) {
376*4882a593Smuzhiyun 					/*
377*4882a593Smuzhiyun 					 * If processing the concatenation of
378*4882a593Smuzhiyun 					 * multiple dependency files, only
379*4882a593Smuzhiyun 					 * process the first target name, which
380*4882a593Smuzhiyun 					 * will be the original source name,
381*4882a593Smuzhiyun 					 * and ignore any other target names,
382*4882a593Smuzhiyun 					 * which will be intermediate temporary
383*4882a593Smuzhiyun 					 * files.
384*4882a593Smuzhiyun 					 */
385*4882a593Smuzhiyun 					if (!saw_any_target) {
386*4882a593Smuzhiyun 						saw_any_target = 1;
387*4882a593Smuzhiyun 						printf("source_%s := %s\n\n",
388*4882a593Smuzhiyun 							target, s);
389*4882a593Smuzhiyun 						printf("deps_%s := \\\n",
390*4882a593Smuzhiyun 							target);
391*4882a593Smuzhiyun 					}
392*4882a593Smuzhiyun 					is_first_dep = 0;
393*4882a593Smuzhiyun 				} else
394*4882a593Smuzhiyun 					printf("  %s \\\n", s);
395*4882a593Smuzhiyun 				do_config_file(s);
396*4882a593Smuzhiyun 			}
397*4882a593Smuzhiyun 		}
398*4882a593Smuzhiyun 		/*
399*4882a593Smuzhiyun 		 * Start searching for next token immediately after the first
400*4882a593Smuzhiyun 		 * "whitespace" character that follows this token.
401*4882a593Smuzhiyun 		 */
402*4882a593Smuzhiyun 		m = p + 1;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	if (!saw_any_target) {
406*4882a593Smuzhiyun 		fprintf(stderr, "fixdep: parse error; no targets found\n");
407*4882a593Smuzhiyun 		exit(1);
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	printf("\n%s: $(deps_%s)\n\n", target, target);
411*4882a593Smuzhiyun 	printf("$(deps_%s):\n", target);
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
print_deps(void)414*4882a593Smuzhiyun static void print_deps(void)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	struct stat st;
417*4882a593Smuzhiyun 	int fd;
418*4882a593Smuzhiyun 	void *map;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	fd = open(depfile, O_RDONLY);
421*4882a593Smuzhiyun 	if (fd < 0) {
422*4882a593Smuzhiyun 		fprintf(stderr, "fixdep: error opening depfile: ");
423*4882a593Smuzhiyun 		perror(depfile);
424*4882a593Smuzhiyun 		exit(2);
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 	if (fstat(fd, &st) < 0) {
427*4882a593Smuzhiyun 		fprintf(stderr, "fixdep: error fstat'ing depfile: ");
428*4882a593Smuzhiyun 		perror(depfile);
429*4882a593Smuzhiyun 		exit(2);
430*4882a593Smuzhiyun 	}
431*4882a593Smuzhiyun 	if (st.st_size == 0) {
432*4882a593Smuzhiyun 		fprintf(stderr,"fixdep: %s is empty\n",depfile);
433*4882a593Smuzhiyun 		close(fd);
434*4882a593Smuzhiyun 		return;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
437*4882a593Smuzhiyun 	if ((long) map == -1) {
438*4882a593Smuzhiyun 		perror("fixdep: mmap");
439*4882a593Smuzhiyun 		close(fd);
440*4882a593Smuzhiyun 		return;
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	parse_dep_file(map, st.st_size);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	munmap(map, st.st_size);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	close(fd);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun 
traps(void)450*4882a593Smuzhiyun static void traps(void)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
453*4882a593Smuzhiyun 	int *p = (int *)test;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	if (*p != INT_CONF) {
456*4882a593Smuzhiyun 		fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n",
457*4882a593Smuzhiyun 			*p);
458*4882a593Smuzhiyun 		exit(2);
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun 
main(int argc,char * argv[])462*4882a593Smuzhiyun int main(int argc, char *argv[])
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	traps();
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	if (argc != 4)
467*4882a593Smuzhiyun 		usage();
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	depfile = argv[1];
470*4882a593Smuzhiyun 	target = argv[2];
471*4882a593Smuzhiyun 	cmdline = argv[3];
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/* hack for U-Boot */
474*4882a593Smuzhiyun 	if (!strncmp(target, "spl/", 4) || !strncmp(target, "tpl/", 4))
475*4882a593Smuzhiyun 		is_spl_build = 1;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	print_cmdline();
478*4882a593Smuzhiyun 	print_deps();
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	return 0;
481*4882a593Smuzhiyun }
482