fwrite: fixed handling of already buffered data.
[wine.git] / tools / makedep.c
blob5a09e5ebc2ec8a930e92ae5ea75afef9985b41b0
1 /*
2 * Generate include file dependencies
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 /* Max first-level includes per file */
28 #define MAX_INCLUDES 200
30 typedef struct _INCL_FILE
32 struct _INCL_FILE *next;
33 char *name;
34 char *filename;
35 struct _INCL_FILE *included_by; /* file that included this one */
36 int included_line; /* line where this file was included */
37 int system; /* is it a system include (#include <name>) */
38 struct _INCL_FILE *owner;
39 struct _INCL_FILE *files[MAX_INCLUDES];
40 } INCL_FILE;
42 static INCL_FILE *firstSrc;
43 static INCL_FILE *firstInclude;
45 typedef struct _INCL_PATH
47 struct _INCL_PATH *next;
48 const char *name;
49 } INCL_PATH;
51 static INCL_PATH *firstPath;
53 static const char *SrcDir = NULL;
54 static const char *OutputFileName = "Makefile";
55 static const char *Separator = "### Dependencies";
56 static const char *ProgramName;
58 static const char Usage[] =
59 "Usage: %s [options] [files]\n"
60 "Options:\n"
61 " -Idir Search for include files in directory 'dir'\n"
62 " -Cdir Search for source files in directory 'dir'\n"
63 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
64 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
67 /*******************************************************************
68 * xmalloc
70 static void *xmalloc( int size )
72 void *res;
73 if (!(res = malloc (size ? size : 1)))
75 fprintf( stderr, "%s: Virtual memory exhausted.\n", ProgramName );
76 exit(1);
78 return res;
82 /*******************************************************************
83 * xstrdup
85 static char *xstrdup( const char *str )
87 char *res = strdup( str );
88 if (!res)
90 fprintf( stderr, "%s: Virtual memory exhausted.\n", ProgramName );
91 exit(1);
93 return res;
97 /*******************************************************************
98 * is_generated
100 * Test if a given file type is generated during the make process
102 static int is_generated( const char *name )
104 static const char * const extensions[] = { ".tab.h", ".mc.rc" };
105 size_t i, len = strlen(name);
106 for (i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
108 if (len <= strlen(extensions[i])) continue;
109 if (!strcmp( name + len - strlen(extensions[i]), extensions[i] )) return 1;
111 return 0;
114 /*******************************************************************
115 * add_include_path
117 * Add a directory to the include path.
119 static void add_include_path( const char *name )
121 INCL_PATH *path = xmalloc( sizeof(*path) );
122 INCL_PATH **p = &firstPath;
123 while (*p) p = &(*p)->next;
124 *p = path;
125 path->next = NULL;
126 path->name = name;
130 /*******************************************************************
131 * add_src_file
133 * Add a source file to the list.
135 static INCL_FILE *add_src_file( const char *name )
137 INCL_FILE **p = &firstSrc;
138 INCL_FILE *file = xmalloc( sizeof(*file) );
139 memset( file, 0, sizeof(*file) );
140 file->name = xstrdup(name);
141 while (*p) p = &(*p)->next;
142 *p = file;
143 return file;
147 /*******************************************************************
148 * add_include
150 * Add an include file if it doesn't already exists.
152 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
154 INCL_FILE **p = &firstInclude;
155 int pos;
157 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
158 if (pos >= MAX_INCLUDES)
160 fprintf( stderr, "%s: %s: too many included files, please fix MAX_INCLUDES\n",
161 ProgramName, pFile->name );
162 exit(1);
165 while (*p && strcmp( name, (*p)->name )) p = &(*p)->next;
166 if (!*p)
168 *p = xmalloc( sizeof(INCL_FILE) );
169 memset( *p, 0, sizeof(INCL_FILE) );
170 (*p)->name = xstrdup(name);
171 (*p)->included_by = pFile;
172 (*p)->included_line = line;
173 (*p)->system = system || pFile->system;
175 pFile->files[pos] = *p;
176 return *p;
180 /*******************************************************************
181 * open_src_file
183 static FILE *open_src_file( INCL_FILE *pFile )
185 FILE *file;
187 if (SrcDir)
189 pFile->filename = xmalloc( strlen(SrcDir) + strlen(pFile->name) + 2 );
190 strcpy( pFile->filename, SrcDir );
191 strcat( pFile->filename, "/" );
192 strcat( pFile->filename, pFile->name );
194 else pFile->filename = xstrdup( pFile->name );
196 if (!(file = fopen( pFile->filename, "r" )))
198 perror( pFile->filename );
199 exit(1);
201 return file;
205 /*******************************************************************
206 * open_include_file
208 static FILE *open_include_file( INCL_FILE *pFile )
210 FILE *file = NULL;
211 INCL_PATH *path;
213 for (path = firstPath; path; path = path->next)
215 char *filename = xmalloc(strlen(path->name) + strlen(pFile->name) + 2);
216 strcpy( filename, path->name );
217 strcat( filename, "/" );
218 strcat( filename, pFile->name );
219 if ((file = fopen( filename, "r" )))
221 pFile->filename = filename;
222 break;
224 free( filename );
226 if (!file && pFile->system) return NULL; /* ignore system files we cannot find */
228 /* try in src file directory */
229 if (!file)
231 char *p = strrchr(pFile->included_by->name, '/');
232 if (p)
234 int l = p - pFile->included_by->name + 1;
235 char *filename = xmalloc(l + strlen(pFile->name) + 1);
236 memcpy( filename, pFile->included_by->name, l );
237 strcpy( filename + l, pFile->name );
238 if ((file = fopen( filename, "r" ))) pFile->filename = filename;
239 else free( filename );
243 if (!file)
245 if (pFile->included_by->system) return NULL; /* ignore if included by a system file */
246 if (firstPath) perror( pFile->name );
247 else fprintf( stderr, "%s: %s: File not found\n",
248 ProgramName, pFile->name );
249 while (pFile->included_by)
251 fprintf( stderr, " %s was first included from %s:%d\n",
252 pFile->name, pFile->included_by->name, pFile->included_line );
253 pFile = pFile->included_by;
255 exit(1);
257 return file;
261 /*******************************************************************
262 * parse_file
264 static void parse_file( INCL_FILE *pFile, int src )
266 char buffer[1024];
267 char *include;
268 int line = 0;
269 FILE *file;
271 if (is_generated( pFile->name ))
273 /* file is generated during make, don't try to open it */
274 pFile->filename = xstrdup( pFile->name );
275 return;
278 file = src ? open_src_file( pFile ) : open_include_file( pFile );
279 if (!file) return;
281 while (fgets( buffer, sizeof(buffer)-1, file ))
283 char quote;
284 char *p = buffer;
285 line++;
286 while (*p && isspace(*p)) p++;
287 if (*p++ != '#') continue;
288 while (*p && isspace(*p)) p++;
289 if (strncmp( p, "include", 7 )) continue;
290 p += 7;
291 while (*p && isspace(*p)) p++;
292 if (*p != '\"' && *p != '<' ) continue;
293 quote = *p++;
294 if (quote == '<') quote = '>';
295 include = p;
296 while (*p && (*p != quote)) p++;
297 if (!*p)
299 fprintf( stderr, "%s:%d: Malformed #include directive\n",
300 pFile->filename, line );
301 exit(1);
303 *p = 0;
304 add_include( pFile, include, line, (quote == '>') );
306 fclose(file);
310 /*******************************************************************
311 * output_include
313 static void output_include( FILE *file, INCL_FILE *pFile,
314 INCL_FILE *owner, int *column )
316 int i;
318 if (pFile->owner == owner) return;
319 if (!pFile->filename) return;
320 pFile->owner = owner;
321 if (*column + strlen(pFile->filename) + 1 > 70)
323 fprintf( file, " \\\n" );
324 *column = 0;
326 fprintf( file, " %s", pFile->filename );
327 *column += strlen(pFile->filename) + 1;
328 for (i = 0; i < MAX_INCLUDES; i++)
329 if (pFile->files[i]) output_include( file, pFile->files[i],
330 owner, column );
334 /*******************************************************************
335 * output_src
337 static void output_src( FILE *file, INCL_FILE *pFile, int *column )
339 char *obj = xstrdup( pFile->name );
340 char *ext = strrchr( obj, '.' );
341 if (ext && strchr( ext, '/' )) ext = NULL;
342 if (ext)
344 *ext++ = 0;
345 if (!strcmp( ext, "y" )) /* yacc file */
347 *column += fprintf( file, "y.tab.o: y.tab.c" );
349 else if (!strcmp( ext, "l" )) /* lex file */
351 *column += fprintf( file, "%s.o: %s.c", LEX_OUTPUT_ROOT, LEX_OUTPUT_ROOT );
353 else if (!strcmp( ext, "rc" )) /* resource file */
355 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
357 else if (!strcmp( ext, "mc" )) /* message file */
359 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
361 else
363 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
366 free( obj );
370 /*******************************************************************
371 * output_dependencies
373 static void output_dependencies(void)
375 INCL_FILE *pFile;
376 int i, column;
377 FILE *file = NULL;
378 char buffer[1024];
380 if (Separator && ((file = fopen( OutputFileName, "r+" ))))
382 while (fgets( buffer, sizeof(buffer), file ))
383 if (!strncmp( buffer, Separator, strlen(Separator) )) break;
384 ftruncate( fileno(file), ftell(file) );
385 fseek( file, 0L, SEEK_END );
387 if (!file)
389 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
391 perror( OutputFileName );
392 exit(1);
395 for( pFile = firstSrc; pFile; pFile = pFile->next)
397 column = 0;
398 output_src( file, pFile, &column );
399 for (i = 0; i < MAX_INCLUDES; i++)
400 if (pFile->files[i]) output_include( file, pFile->files[i],
401 pFile, &column );
402 fprintf( file, "\n" );
404 fclose(file);
408 /*******************************************************************
409 * parse_option
411 static void parse_option( const char *opt )
413 switch(opt[1])
415 case 'I':
416 if (opt[2]) add_include_path( opt + 2 );
417 break;
418 case 'C':
419 if (opt[2]) SrcDir = opt + 2;
420 else SrcDir = NULL;
421 break;
422 case 'f':
423 if (opt[2]) OutputFileName = opt + 2;
424 break;
425 case 's':
426 if (opt[2]) Separator = opt + 2;
427 else Separator = NULL;
428 break;
429 default:
430 fprintf( stderr, "Unknown option '%s'\n", opt );
431 fprintf( stderr, Usage, ProgramName );
432 exit(1);
437 /*******************************************************************
438 * main
440 int main( int argc, char *argv[] )
442 INCL_FILE *pFile;
444 ProgramName = argv[0];
445 while (argc > 1)
447 if (*argv[1] == '-') parse_option( argv[1] );
448 else
450 pFile = add_src_file( argv[1] );
451 parse_file( pFile, 1 );
453 argc--;
454 argv++;
456 for (pFile = firstInclude; pFile; pFile = pFile->next)
457 parse_file( pFile, 0 );
458 if( firstSrc ) output_dependencies();
459 return 0;