Added version info to 16-bit shell.dll.
[wine/multimedia.git] / tools / makedep.c
blob2a2174a5a24ec5e3e1587d51ceae6c16d5456e43
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 "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 /* Max first-level includes per file */
31 #define MAX_INCLUDES 200
33 typedef struct _INCL_FILE
35 struct _INCL_FILE *next;
36 char *name;
37 char *filename;
38 struct _INCL_FILE *included_by; /* file that included this one */
39 int included_line; /* line where this file was included */
40 int system; /* is it a system include (#include <name>) */
41 struct _INCL_FILE *owner;
42 struct _INCL_FILE *files[MAX_INCLUDES];
43 } INCL_FILE;
45 static INCL_FILE *firstSrc;
46 static INCL_FILE *firstInclude;
48 typedef struct _INCL_PATH
50 struct _INCL_PATH *next;
51 const char *name;
52 } INCL_PATH;
54 static INCL_PATH *firstPath;
56 static const char *SrcDir = NULL;
57 static const char *OutputFileName = "Makefile";
58 static const char *Separator = "### Dependencies";
59 static const char *ProgramName;
61 static const char Usage[] =
62 "Usage: %s [options] [files]\n"
63 "Options:\n"
64 " -Idir Search for include files in directory 'dir'\n"
65 " -Cdir Search for source files in directory 'dir'\n"
66 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
67 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
70 /*******************************************************************
71 * xmalloc
73 static void *xmalloc( int size )
75 void *res;
76 if (!(res = malloc (size ? size : 1)))
78 fprintf( stderr, "%s: Virtual memory exhausted.\n", ProgramName );
79 exit(1);
81 return res;
85 /*******************************************************************
86 * xstrdup
88 static char *xstrdup( const char *str )
90 char *res = strdup( str );
91 if (!res)
93 fprintf( stderr, "%s: Virtual memory exhausted.\n", ProgramName );
94 exit(1);
96 return res;
100 /*******************************************************************
101 * is_generated
103 * Test if a given file type is generated during the make process
105 static int is_generated( const char *name )
107 static const char * const extensions[] = { ".tab.h", ".mc.rc" };
108 size_t i, len = strlen(name);
109 for (i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
111 if (len <= strlen(extensions[i])) continue;
112 if (!strcmp( name + len - strlen(extensions[i]), extensions[i] )) return 1;
114 return 0;
117 /*******************************************************************
118 * add_include_path
120 * Add a directory to the include path.
122 static void add_include_path( const char *name )
124 INCL_PATH *path = xmalloc( sizeof(*path) );
125 INCL_PATH **p = &firstPath;
126 while (*p) p = &(*p)->next;
127 *p = path;
128 path->next = NULL;
129 path->name = name;
133 /*******************************************************************
134 * add_src_file
136 * Add a source file to the list.
138 static INCL_FILE *add_src_file( const char *name )
140 INCL_FILE **p = &firstSrc;
141 INCL_FILE *file = xmalloc( sizeof(*file) );
142 memset( file, 0, sizeof(*file) );
143 file->name = xstrdup(name);
144 while (*p) p = &(*p)->next;
145 *p = file;
146 return file;
150 /*******************************************************************
151 * add_include
153 * Add an include file if it doesn't already exists.
155 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
157 INCL_FILE **p = &firstInclude;
158 int pos;
160 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
161 if (pos >= MAX_INCLUDES)
163 fprintf( stderr, "%s: %s: too many included files, please fix MAX_INCLUDES\n",
164 ProgramName, pFile->name );
165 exit(1);
168 while (*p && strcmp( name, (*p)->name )) p = &(*p)->next;
169 if (!*p)
171 *p = xmalloc( sizeof(INCL_FILE) );
172 memset( *p, 0, sizeof(INCL_FILE) );
173 (*p)->name = xstrdup(name);
174 (*p)->included_by = pFile;
175 (*p)->included_line = line;
176 (*p)->system = system || pFile->system;
178 pFile->files[pos] = *p;
179 return *p;
183 /*******************************************************************
184 * open_src_file
186 static FILE *open_src_file( INCL_FILE *pFile )
188 FILE *file;
190 if (SrcDir)
192 pFile->filename = xmalloc( strlen(SrcDir) + strlen(pFile->name) + 2 );
193 strcpy( pFile->filename, SrcDir );
194 strcat( pFile->filename, "/" );
195 strcat( pFile->filename, pFile->name );
197 else pFile->filename = xstrdup( pFile->name );
199 if (!(file = fopen( pFile->filename, "r" )))
201 perror( pFile->filename );
202 exit(1);
204 return file;
208 /*******************************************************************
209 * open_include_file
211 static FILE *open_include_file( INCL_FILE *pFile )
213 FILE *file = NULL;
214 INCL_PATH *path;
216 for (path = firstPath; path; path = path->next)
218 char *filename = xmalloc(strlen(path->name) + strlen(pFile->name) + 2);
219 strcpy( filename, path->name );
220 strcat( filename, "/" );
221 strcat( filename, pFile->name );
222 if ((file = fopen( filename, "r" )))
224 pFile->filename = filename;
225 break;
227 free( filename );
229 if (!file && pFile->system) return NULL; /* ignore system files we cannot find */
231 /* try in src file directory */
232 if (!file)
234 char *p = strrchr(pFile->included_by->filename, '/');
235 if (p)
237 int l = p - pFile->included_by->filename + 1;
238 char *filename = xmalloc(l + strlen(pFile->name) + 1);
239 memcpy( filename, pFile->included_by->filename, l );
240 strcpy( filename + l, pFile->name );
241 if ((file = fopen( filename, "r" ))) pFile->filename = filename;
242 else free( filename );
246 if (!file)
248 if (pFile->included_by->system) return NULL; /* ignore if included by a system file */
249 if (firstPath) perror( pFile->name );
250 else fprintf( stderr, "%s: %s: File not found\n",
251 ProgramName, pFile->name );
252 while (pFile->included_by)
254 fprintf( stderr, " %s was first included from %s:%d\n",
255 pFile->name, pFile->included_by->name, pFile->included_line );
256 pFile = pFile->included_by;
258 exit(1);
260 return file;
264 /*******************************************************************
265 * parse_file
267 static void parse_file( INCL_FILE *pFile, int src )
269 char buffer[1024];
270 char *include;
271 int line = 0;
272 FILE *file;
274 if (is_generated( pFile->name ))
276 /* file is generated during make, don't try to open it */
277 pFile->filename = xstrdup( pFile->name );
278 return;
281 file = src ? open_src_file( pFile ) : open_include_file( pFile );
282 if (!file) return;
284 while (fgets( buffer, sizeof(buffer)-1, file ))
286 char quote;
287 char *p = buffer;
288 line++;
289 while (*p && isspace(*p)) p++;
290 if (*p++ != '#') continue;
291 while (*p && isspace(*p)) p++;
292 if (strncmp( p, "include", 7 )) continue;
293 p += 7;
294 while (*p && isspace(*p)) p++;
295 if (*p != '\"' && *p != '<' ) continue;
296 quote = *p++;
297 if (quote == '<') quote = '>';
298 include = p;
299 while (*p && (*p != quote)) p++;
300 if (!*p)
302 fprintf( stderr, "%s:%d: Malformed #include directive\n",
303 pFile->filename, line );
304 exit(1);
306 *p = 0;
307 add_include( pFile, include, line, (quote == '>') );
309 fclose(file);
313 /*******************************************************************
314 * output_include
316 static void output_include( FILE *file, INCL_FILE *pFile,
317 INCL_FILE *owner, int *column )
319 int i;
321 if (pFile->owner == owner) return;
322 if (!pFile->filename) return;
323 pFile->owner = owner;
324 if (*column + strlen(pFile->filename) + 1 > 70)
326 fprintf( file, " \\\n" );
327 *column = 0;
329 fprintf( file, " %s", pFile->filename );
330 *column += strlen(pFile->filename) + 1;
331 for (i = 0; i < MAX_INCLUDES; i++)
332 if (pFile->files[i]) output_include( file, pFile->files[i],
333 owner, column );
337 /*******************************************************************
338 * output_src
340 static void output_src( FILE *file, INCL_FILE *pFile, int *column )
342 char *obj = xstrdup( pFile->name );
343 char *ext = strrchr( obj, '.' );
344 if (ext && strchr( ext, '/' )) ext = NULL;
345 if (ext)
347 *ext++ = 0;
348 if (!strcmp( ext, "y" )) /* yacc file */
350 *column += fprintf( file, "y.tab.o: y.tab.c" );
352 else if (!strcmp( ext, "l" )) /* lex file */
354 *column += fprintf( file, "%s.o: %s.c", LEX_OUTPUT_ROOT, LEX_OUTPUT_ROOT );
356 else if (!strcmp( ext, "rc" )) /* resource file */
358 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
360 else if (!strcmp( ext, "mc" )) /* message file */
362 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
364 else
366 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
369 free( obj );
373 /*******************************************************************
374 * output_dependencies
376 static void output_dependencies(void)
378 INCL_FILE *pFile;
379 int i, column;
380 FILE *file = NULL;
381 char buffer[1024];
383 if (Separator && ((file = fopen( OutputFileName, "r+" ))))
385 while (fgets( buffer, sizeof(buffer), file ))
386 if (!strncmp( buffer, Separator, strlen(Separator) )) break;
387 ftruncate( fileno(file), ftell(file) );
388 fseek( file, 0L, SEEK_END );
390 if (!file)
392 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
394 perror( OutputFileName );
395 exit(1);
398 for( pFile = firstSrc; pFile; pFile = pFile->next)
400 column = 0;
401 output_src( file, pFile, &column );
402 for (i = 0; i < MAX_INCLUDES; i++)
403 if (pFile->files[i]) output_include( file, pFile->files[i],
404 pFile, &column );
405 fprintf( file, "\n" );
407 fclose(file);
411 /*******************************************************************
412 * parse_option
414 static void parse_option( const char *opt )
416 switch(opt[1])
418 case 'I':
419 if (opt[2]) add_include_path( opt + 2 );
420 break;
421 case 'C':
422 if (opt[2]) SrcDir = opt + 2;
423 else SrcDir = NULL;
424 break;
425 case 'f':
426 if (opt[2]) OutputFileName = opt + 2;
427 break;
428 case 's':
429 if (opt[2]) Separator = opt + 2;
430 else Separator = NULL;
431 break;
432 default:
433 fprintf( stderr, "Unknown option '%s'\n", opt );
434 fprintf( stderr, Usage, ProgramName );
435 exit(1);
440 /*******************************************************************
441 * main
443 int main( int argc, char *argv[] )
445 INCL_FILE *pFile;
447 ProgramName = argv[0];
448 while (argc > 1)
450 if (*argv[1] == '-') parse_option( argv[1] );
451 else
453 pFile = add_src_file( argv[1] );
454 parse_file( pFile, 1 );
456 argc--;
457 argv++;
459 for (pFile = firstInclude; pFile; pFile = pFile->next)
460 parse_file( pFile, 0 );
461 if( firstSrc ) output_dependencies();
462 return 0;