Debian Codegraph @

tools/mkdep.c

raw
1 /* Copyright 2016 Google Inc.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License. */
14
15 /* Generate make dependencies using C preprocessor.
16 Usage:
17 mkdep cc cc_args_to_invoke_preprocessor... file.{[csh],cc} ...
18 Output is written to stdout. */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24
25 #if defined(_WIN32)
26 #define popen _popen
27 #define pclose _pclose
28 #endif
29
30 /* Comparison routine to sort strings with qsort(). */
31 static int cmpstr (const void *va, const void *vb) {
32 const char **a = (const char **)va;
33 const char **b = (const char **)vb;
34 return (strcmp (*a, *b));
35 }
36
37 /* Return whether nul-terminated string s[] starts with any of the
38 nul-terminated strings given as arguments after s,
39 and termated with a NULL argument. */
40 static int has_prefix (const char *s, ...) {
41 va_list ap;
42 const char *pref;
43 va_start (ap, s);
44 pref = va_arg (ap, const char *);
45 while (pref != NULL && strncmp (s, pref, strlen (pref)) != 0) {
46 pref = va_arg (ap, const char *);
47 }
48 va_end (ap);
49 return (pref != NULL);
50 }
51
52 /* Return whether nul-terminated string s[] ends with any of the
53 nul-terminated strings given as arguments after s,
54 and termated with a NULL argument. */
55 static int has_suffix (const char *s, ...) {
56 va_list ap;
57 int slen = strlen (s);
58 const char *suf;
59 va_start (ap, s);
60 suf = va_arg (ap, const char *);
61 while (suf != NULL &&
62 (slen < (int) strlen (suf) ||
63 strcmp (&s[slen-strlen (suf)], suf) != 0)) {
64 suf = va_arg (ap, const char *);
65 }
66 va_end (ap);
67 return (suf != NULL);
68 }
69
70 /* Return whether nul-termainted string s[] ends with
71 a suffix that designates a source file. */
72 static int has_prog_suffix (const char *s) {
73 return (has_suffix (s, ".c", ".h", ".s", ".cc",
74 ".C", ".H", ".S", ".CC",
75 (char *) NULL));
76 }
77
78 /* Return the index in nul-terminated string s[] of the longest suffix that
79 contains none of the bytes in nul-terminated string sep[]. */
80 static int pos_suffix_not_containing (const char *s, const char *sep) {
81 int pos = strlen (s);
82 while (pos != 0 && strchr (sep, s[pos-1] & 0xff) == NULL) {
83 pos--;
84 }
85 return (pos);
86 }
87
88 /* Process a source file with nul-terminated file name file[],
89 writing to ofp the source dependencies obtained by
90 running the file through the C preprocessor using the
91 nul-terminated command prefix cmd_prefix[]. */
92 static void process_file (FILE *ofp, const char *cmd_prefix, const char *file) {
93 char line[2048];
94 int cmdlen = strlen (cmd_prefix) + strlen (file) + 10;
95 char *cmd = (char *) malloc (cmdlen);
96 FILE *ifp;
97 int status;
98 int i;
99 int n;
100 int basename = pos_suffix_not_containing (file, "/\\");
101 int ext = pos_suffix_not_containing (file, ".");
102 const char *last;
103 int list_max = 64;
104 char **list = (char **) malloc (sizeof (list[0]) * list_max);
105
106 snprintf (cmd, cmdlen, "%s %s", cmd_prefix, file);
107 ifp = popen (cmd, "r");
108 if (ifp == NULL) {
109 fprintf (stderr, "error starting command: \"%s\": ", cmd);
110 perror ("popen");
111 exit (2);
112 }
113 n = 0;
114 while (fgets (line, sizeof (line), ifp) != NULL) {
115 if (has_prefix (line, "#line 1 \"", "# 1 \"", (char *) NULL)) {
116 char *start = strchr (line, '"');
117 char *end = strchr (start+1, '"');
118 if (end != NULL) {
119 end[1] = 0;
120 if (start[0] == '"' && end[0] == '"' &&
121 strchr (start, ' ') == NULL) {
122 /* No need for quotes. */
123 start++;
124 end[0] = 0;
125 }
126 }
127 if (has_prog_suffix (start)) {
128 if (n == list_max) {
129 list_max *= 2;
130 list = (char **) realloc (list,
131 sizeof (list[0]) * list_max);
132 }
133 list[n++] = strdup (start);
134 }
135 }
136 }
137 if (basename < ext) {
138 fprintf (ofp, "%.*so:", ext - basename, &file[basename]);
139 } else {
140 abort (); /* can't happen: file ends in a prog suffix */
141 }
142 qsort (list, n, sizeof (list[0]), &cmpstr);
143 last = "";
144 for (i = 0; i != n; i++) {
145 if (strcmp (last, list[i]) != 0) {
146 fprintf (ofp, " \\\n %s", list[i]);
147 last = list[i];
148 }
149 }
150 fprintf (ofp, "\n");
151 for (i = 0; i != n; i++) {
152 free (list[i]);
153 }
154 if (ferror (ifp)) {
155 fprintf (stderr, "error reading from command: ");
156 perror (cmd);
157 exit (2);
158 }
159 status = pclose (ifp);
160 if (status != 0) {
161 fprintf (stderr,
162 "mkdep: command \"%s\" returned error %x\n",
163 cmd, status);
164 exit (2);
165 }
166 free (cmd);
167 free (list);
168 }
169
170 int main (int argc, char *argv[]) {
171 char cmd_prefix[2048];
172 int cmd_len = 0;
173 char cpp_cmd_prefix[2048];
174 int cpp_cmd_len = 0;
175 int argn;
176 FILE *ofp = stdout;
177 /* Build up command line using arguments
178 that don't end in .[chs] .cc */
179 for (argn = 1; argn != argc; argn++) {
180 const char *arg = argv[argn];
181 if (!has_prog_suffix (arg)) {
182 if (has_prefix (arg, "-c++=", NULL)) {
183 snprintf (&cpp_cmd_prefix[cpp_cmd_len],
184 sizeof (cpp_cmd_prefix) - cpp_cmd_len,
185 "%s ", arg+5);
186 cpp_cmd_len += strlen (
187 &cpp_cmd_prefix[cpp_cmd_len]);
188 } else {
189 snprintf (&cmd_prefix[cmd_len],
190 sizeof (cmd_prefix) - cmd_len,
191 "%s ", arg);
192 cmd_len += strlen (&cmd_prefix[cmd_len]);
193 snprintf (&cpp_cmd_prefix[cpp_cmd_len],
194 sizeof (cpp_cmd_prefix) - cpp_cmd_len,
195 "%s ", arg);
196 cpp_cmd_len += strlen (
197 &cpp_cmd_prefix[cpp_cmd_len]);
198 }
199 }
200 }
201 if (cmd_len >= (int) sizeof (cmd_prefix) - 1) {
202 fprintf (stderr, "mkdep: command line is too long\n");
203 exit (2);
204 }
205 if (cpp_cmd_len >= (int) sizeof (cpp_cmd_prefix) - 1) {
206 fprintf (stderr, "mkdep: command line is too long\n");
207 exit (2);
208 }
209 cmd_prefix[cmd_len] = 0;
210 cpp_cmd_prefix[cpp_cmd_len] = 0;
211
212 /* Run the compiler for each non-flag file containing a dot,
213 generating Makefile dependencies for that file by running
214 it through the preprocessor. */
215 for (argn = 1; argn != argc; argn++) {
216 if (has_prog_suffix (argv[argn])) {
217 if (has_suffix (argv[argn], ".cc", ".CC", NULL)) {
218 process_file (ofp, cpp_cmd_prefix, argv[argn]);
219 } else {
220 process_file (ofp, cmd_prefix, argv[argn]);
221 }
222 }
223 }
224 return (0);
225 }