Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Generate include file dependencies |
| 3 | * |
| 4 | * Copyright 1996 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
Alexandre Julliard | 8cbdb97 | 2003-03-20 21:08:28 +0000 | [diff] [blame] | 22 | #define NO_LIBWINE_PORT |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 23 | #include "wine/port.h" |
| 24 | |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 25 | #include <assert.h> |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 27 | #include <errno.h> |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 31 | #include <string.h> |
Dmitry Timoshkov | c63d980 | 2002-08-17 18:28:43 +0000 | [diff] [blame] | 32 | #ifdef HAVE_UNISTD_H |
| 33 | # include <unistd.h> |
| 34 | #endif |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 35 | #include "wine/list.h" |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 36 | |
| 37 | /* Max first-level includes per file */ |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 38 | #define MAX_INCLUDES 200 |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 39 | |
| 40 | typedef struct _INCL_FILE |
| 41 | { |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 42 | struct list entry; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 43 | char *name; |
| 44 | char *filename; |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 45 | char *sourcename; /* source file name for generated headers */ |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 46 | struct _INCL_FILE *included_by; /* file that included this one */ |
| 47 | int included_line; /* line where this file was included */ |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 48 | int system; /* is it a system include (#include <name>) */ |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 49 | struct _INCL_FILE *owner; |
| 50 | struct _INCL_FILE *files[MAX_INCLUDES]; |
| 51 | } INCL_FILE; |
| 52 | |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 53 | static struct list sources = LIST_INIT(sources); |
| 54 | static struct list includes = LIST_INIT(includes); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 55 | |
| 56 | typedef struct _INCL_PATH |
| 57 | { |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 58 | struct list entry; |
| 59 | const char *name; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 60 | } INCL_PATH; |
| 61 | |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 62 | static struct list paths = LIST_INIT(paths); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 63 | |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 64 | static const char *src_dir; |
| 65 | static const char *top_src_dir; |
| 66 | static const char *top_obj_dir; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 67 | static const char *OutputFileName = "Makefile"; |
| 68 | static const char *Separator = "### Dependencies"; |
| 69 | static const char *ProgramName; |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 70 | static int input_line; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 71 | |
| 72 | static const char Usage[] = |
| 73 | "Usage: %s [options] [files]\n" |
| 74 | "Options:\n" |
| 75 | " -Idir Search for include files in directory 'dir'\n" |
| 76 | " -Cdir Search for source files in directory 'dir'\n" |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 77 | " -Sdir Set the top source directory\n" |
Detlef Riekenberg | 5073aae | 2008-06-13 05:09:38 +0200 | [diff] [blame] | 78 | " -Tdir Set the top object directory\n" |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 79 | " -fxxx Store output in file 'xxx' (default: Makefile)\n" |
| 80 | " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n"; |
| 81 | |
| 82 | |
| 83 | /******************************************************************* |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 84 | * fatal_error |
| 85 | */ |
| 86 | static void fatal_error( const char *msg, ... ) |
| 87 | { |
| 88 | va_list valist; |
| 89 | va_start( valist, msg ); |
| 90 | vfprintf( stderr, msg, valist ); |
| 91 | va_end( valist ); |
| 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /******************************************************************* |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 97 | * xmalloc |
| 98 | */ |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 99 | static void *xmalloc( size_t size ) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 100 | { |
| 101 | void *res; |
| 102 | if (!(res = malloc (size ? size : 1))) |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 103 | fatal_error( "%s: Virtual memory exhausted.\n", ProgramName ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 104 | return res; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /******************************************************************* |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 109 | * xrealloc |
| 110 | */ |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 111 | static void *xrealloc (void *ptr, size_t size) |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 112 | { |
| 113 | void *res; |
| 114 | assert( size ); |
| 115 | if (!(res = realloc( ptr, size ))) |
| 116 | fatal_error( "%s: Virtual memory exhausted.\n", ProgramName ); |
| 117 | return res; |
| 118 | } |
| 119 | |
| 120 | /******************************************************************* |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 121 | * xstrdup |
| 122 | */ |
| 123 | static char *xstrdup( const char *str ) |
| 124 | { |
| 125 | char *res = strdup( str ); |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 126 | if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 127 | return res; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /******************************************************************* |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 132 | * strmake |
| 133 | */ |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 134 | static char *strmake( const char* fmt, ... ) |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 135 | { |
| 136 | int n; |
| 137 | size_t size = 100; |
| 138 | va_list ap; |
| 139 | |
| 140 | for (;;) |
| 141 | { |
| 142 | char *p = xmalloc (size); |
| 143 | va_start(ap, fmt); |
| 144 | n = vsnprintf (p, size, fmt, ap); |
| 145 | va_end(ap); |
| 146 | if (n == -1) size *= 2; |
| 147 | else if ((size_t)n >= size) size = n + 1; |
| 148 | else return p; |
| 149 | free(p); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /******************************************************************* |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 155 | * strendswith |
| 156 | */ |
| 157 | static int strendswith( const char* str, const char* end ) |
| 158 | { |
| 159 | int l = strlen(str); |
| 160 | int m = strlen(end); |
| 161 | |
| 162 | return l >= m && strcmp(str + l - m, end) == 0; |
| 163 | } |
| 164 | |
| 165 | /******************************************************************* |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 166 | * get_extension |
| 167 | */ |
| 168 | static char *get_extension( char *filename ) |
| 169 | { |
| 170 | char *ext = strrchr( filename, '.' ); |
| 171 | if (ext && strchr( ext, '/' )) ext = NULL; |
| 172 | return ext; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /******************************************************************* |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 177 | * get_line |
| 178 | */ |
| 179 | static char *get_line( FILE *file ) |
| 180 | { |
| 181 | static char *buffer; |
| 182 | static unsigned int size; |
| 183 | |
| 184 | if (!size) |
| 185 | { |
| 186 | size = 1024; |
| 187 | buffer = xmalloc( size ); |
| 188 | } |
| 189 | if (!fgets( buffer, size, file )) return NULL; |
| 190 | input_line++; |
| 191 | |
| 192 | for (;;) |
| 193 | { |
| 194 | char *p = buffer + strlen(buffer); |
| 195 | /* if line is larger than buffer, resize buffer */ |
| 196 | while (p == buffer + size - 1 && p[-1] != '\n') |
| 197 | { |
| 198 | buffer = xrealloc( buffer, size * 2 ); |
| 199 | fgets( buffer + size - 1, size + 1, file ); |
| 200 | p = buffer + strlen(buffer); |
| 201 | size *= 2; |
| 202 | } |
| 203 | if (p > buffer && p[-1] == '\n') |
| 204 | { |
| 205 | *(--p) = 0; |
| 206 | if (p > buffer && p[-1] == '\r') *(--p) = 0; |
| 207 | if (p > buffer && p[-1] == '\\') |
| 208 | { |
| 209 | *(--p) = 0; |
| 210 | /* line ends in backslash, read continuation line */ |
| 211 | fgets( p, size - (p - buffer), file ); |
| 212 | input_line++; |
| 213 | continue; |
| 214 | } |
| 215 | } |
| 216 | return buffer; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /******************************************************************* |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 221 | * add_include_path |
| 222 | * |
| 223 | * Add a directory to the include path. |
| 224 | */ |
| 225 | static void add_include_path( const char *name ) |
| 226 | { |
| 227 | INCL_PATH *path = xmalloc( sizeof(*path) ); |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 228 | list_add_tail( &paths, &path->entry ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 229 | path->name = name; |
| 230 | } |
| 231 | |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 232 | /******************************************************************* |
| 233 | * find_src_file |
| 234 | */ |
| 235 | static INCL_FILE *find_src_file( const char *name ) |
| 236 | { |
| 237 | INCL_FILE *file; |
| 238 | |
| 239 | LIST_FOR_EACH_ENTRY( file, &sources, INCL_FILE, entry ) |
| 240 | if (!strcmp( name, file->name )) return file; |
| 241 | return NULL; |
| 242 | } |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 243 | |
| 244 | /******************************************************************* |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 245 | * add_include |
| 246 | * |
| 247 | * Add an include file if it doesn't already exists. |
| 248 | */ |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 249 | static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system ) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 250 | { |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 251 | INCL_FILE *include; |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 252 | char *ext; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 253 | int pos; |
| 254 | |
| 255 | for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break; |
| 256 | if (pos >= MAX_INCLUDES) |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 257 | fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n", |
| 258 | ProgramName, pFile->name ); |
| 259 | |
| 260 | /* enforce some rules for the Wine tree */ |
| 261 | |
| 262 | if (!memcmp( name, "../", 3 )) |
| 263 | fatal_error( "%s:%d: #include directive with relative path not allowed\n", |
| 264 | pFile->filename, line ); |
| 265 | |
| 266 | if (!strcmp( name, "config.h" )) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 267 | { |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 268 | if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" )) |
| 269 | fatal_error( "%s:%d: config.h must not be included by a header file\n", |
| 270 | pFile->filename, line ); |
| 271 | if (pos) |
| 272 | fatal_error( "%s:%d: config.h must be included before anything else\n", |
| 273 | pFile->filename, line ); |
| 274 | } |
| 275 | else if (!strcmp( name, "wine/port.h" )) |
| 276 | { |
| 277 | if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" )) |
| 278 | fatal_error( "%s:%d: wine/port.h must not be included by a header file\n", |
| 279 | pFile->filename, line ); |
| 280 | if (!pos) fatal_error( "%s:%d: config.h must be included before wine/port.h\n", |
| 281 | pFile->filename, line ); |
| 282 | if (pos > 1) |
| 283 | fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n", |
| 284 | pFile->filename, line ); |
| 285 | if (strcmp( pFile->files[0]->name, "config.h" )) |
| 286 | fatal_error( "%s:%d: config.h must be included before wine/port.h\n", |
| 287 | pFile->filename, line ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 290 | LIST_FOR_EACH_ENTRY( include, &includes, INCL_FILE, entry ) |
| 291 | if (!strcmp( name, include->name )) goto found; |
| 292 | |
| 293 | include = xmalloc( sizeof(INCL_FILE) ); |
| 294 | memset( include, 0, sizeof(INCL_FILE) ); |
| 295 | include->name = xstrdup(name); |
| 296 | include->included_by = pFile; |
| 297 | include->included_line = line; |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 298 | include->system = system; |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 299 | list_add_tail( &includes, &include->entry ); |
| 300 | found: |
| 301 | pFile->files[pos] = include; |
| 302 | return include; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
| 306 | /******************************************************************* |
| 307 | * open_src_file |
| 308 | */ |
| 309 | static FILE *open_src_file( INCL_FILE *pFile ) |
| 310 | { |
| 311 | FILE *file; |
| 312 | |
Alexandre Julliard | 184c40a | 2002-12-11 01:30:14 +0000 | [diff] [blame] | 313 | /* first try name as is */ |
| 314 | if ((file = fopen( pFile->name, "r" ))) |
| 315 | { |
| 316 | pFile->filename = xstrdup( pFile->name ); |
| 317 | return file; |
| 318 | } |
| 319 | /* now try in source dir */ |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 320 | if (src_dir) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 321 | { |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 322 | pFile->filename = strmake( "%s/%s", src_dir, pFile->name ); |
Alexandre Julliard | 184c40a | 2002-12-11 01:30:14 +0000 | [diff] [blame] | 323 | file = fopen( pFile->filename, "r" ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 324 | } |
Alexandre Julliard | 184c40a | 2002-12-11 01:30:14 +0000 | [diff] [blame] | 325 | if (!file) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 326 | { |
Alexandre Julliard | 184c40a | 2002-12-11 01:30:14 +0000 | [diff] [blame] | 327 | perror( pFile->name ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 328 | exit(1); |
| 329 | } |
| 330 | return file; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /******************************************************************* |
| 335 | * open_include_file |
| 336 | */ |
| 337 | static FILE *open_include_file( INCL_FILE *pFile ) |
| 338 | { |
| 339 | FILE *file = NULL; |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 340 | char *filename, *p; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 341 | INCL_PATH *path; |
| 342 | |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 343 | errno = ENOENT; |
| 344 | |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 345 | /* check for generated bison header */ |
| 346 | |
| 347 | if (strendswith( pFile->name, ".tab.h" )) |
| 348 | { |
| 349 | if (src_dir) |
| 350 | filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name ); |
| 351 | else |
| 352 | filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name ); |
| 353 | |
| 354 | if ((file = fopen( filename, "r" ))) |
| 355 | { |
| 356 | pFile->sourcename = filename; |
| 357 | pFile->filename = xstrdup( pFile->name ); |
| 358 | /* don't bother to parse it */ |
| 359 | fclose( file ); |
| 360 | return NULL; |
| 361 | } |
| 362 | free( filename ); |
| 363 | } |
| 364 | |
| 365 | /* check for generated message resource */ |
| 366 | |
| 367 | if (strendswith( pFile->name, ".mc.rc" )) |
| 368 | { |
| 369 | if (src_dir) |
| 370 | filename = strmake( "%s/%s", src_dir, pFile->name ); |
| 371 | else |
| 372 | filename = xstrdup( pFile->name ); |
| 373 | |
| 374 | filename[strlen(filename) - 3] = 0; |
| 375 | |
| 376 | if ((file = fopen( filename, "r" ))) |
| 377 | { |
| 378 | pFile->sourcename = filename; |
| 379 | pFile->filename = xstrdup( pFile->name ); |
| 380 | /* don't bother to parse it */ |
| 381 | fclose( file ); |
| 382 | return NULL; |
| 383 | } |
| 384 | free( filename ); |
| 385 | } |
| 386 | |
| 387 | /* check for corresponding idl file in source dir */ |
| 388 | |
| 389 | if (strendswith( pFile->name, ".h" )) |
| 390 | { |
| 391 | if (src_dir) |
| 392 | filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name ); |
| 393 | else |
| 394 | filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name ); |
| 395 | |
| 396 | if ((file = fopen( filename, "r" ))) |
| 397 | { |
| 398 | pFile->sourcename = filename; |
| 399 | pFile->filename = xstrdup( pFile->name ); |
| 400 | return file; |
| 401 | } |
| 402 | free( filename ); |
| 403 | } |
| 404 | |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 405 | /* first try name as is */ |
| 406 | if ((file = fopen( pFile->name, "r" ))) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 407 | { |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 408 | pFile->filename = xstrdup( pFile->name ); |
| 409 | return file; |
| 410 | } |
| 411 | |
| 412 | /* now try in source dir */ |
| 413 | if (src_dir) |
| 414 | { |
| 415 | filename = strmake( "%s/%s", src_dir, pFile->name ); |
| 416 | if ((file = fopen( filename, "r" ))) goto found; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 417 | free( filename ); |
| 418 | } |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 419 | |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 420 | /* check for corresponding idl file in global includes */ |
| 421 | |
| 422 | if (strendswith( pFile->name, ".h" )) |
| 423 | { |
| 424 | if (top_src_dir) |
| 425 | filename = strmake( "%s/include/%.*s.idl", |
| 426 | top_src_dir, strlen(pFile->name) - 2, pFile->name ); |
| 427 | else if (top_obj_dir) |
| 428 | filename = strmake( "%s/include/%.*s.idl", |
| 429 | top_obj_dir, strlen(pFile->name) - 2, pFile->name ); |
| 430 | else |
| 431 | filename = NULL; |
| 432 | |
| 433 | if (filename && (file = fopen( filename, "r" ))) |
| 434 | { |
| 435 | pFile->sourcename = filename; |
| 436 | pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name ); |
| 437 | return file; |
| 438 | } |
| 439 | free( filename ); |
| 440 | } |
| 441 | |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 442 | /* now try in global includes */ |
| 443 | if (top_obj_dir) |
| 444 | { |
| 445 | filename = strmake( "%s/include/%s", top_obj_dir, pFile->name ); |
| 446 | if ((file = fopen( filename, "r" ))) goto found; |
| 447 | free( filename ); |
| 448 | } |
| 449 | if (top_src_dir) |
| 450 | { |
| 451 | filename = strmake( "%s/include/%s", top_src_dir, pFile->name ); |
| 452 | if ((file = fopen( filename, "r" ))) goto found; |
| 453 | free( filename ); |
| 454 | } |
| 455 | |
| 456 | /* now search in include paths */ |
| 457 | LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry ) |
| 458 | { |
| 459 | filename = strmake( "%s/%s", path->name, pFile->name ); |
| 460 | if ((file = fopen( filename, "r" ))) goto found; |
| 461 | free( filename ); |
| 462 | } |
| 463 | if (pFile->system) return NULL; /* ignore system files we cannot find */ |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 464 | |
Eric Pouech | d848c25 | 2000-12-13 21:27:26 +0000 | [diff] [blame] | 465 | /* try in src file directory */ |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 466 | if ((p = strrchr(pFile->included_by->filename, '/'))) |
Eric Pouech | d848c25 | 2000-12-13 21:27:26 +0000 | [diff] [blame] | 467 | { |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 468 | int l = p - pFile->included_by->filename + 1; |
| 469 | filename = xmalloc(l + strlen(pFile->name) + 1); |
| 470 | memcpy( filename, pFile->included_by->filename, l ); |
| 471 | strcpy( filename + l, pFile->name ); |
| 472 | if ((file = fopen( filename, "r" ))) goto found; |
| 473 | free( filename ); |
Eric Pouech | d848c25 | 2000-12-13 21:27:26 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 476 | perror( pFile->name ); |
| 477 | while (pFile->included_by) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 478 | { |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 479 | const char *parent = pFile->included_by->sourcename; |
| 480 | if (!parent) parent = pFile->included_by->name; |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 481 | fprintf( stderr, " %s was first included from %s:%d\n", |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 482 | pFile->name, parent, pFile->included_line ); |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 483 | pFile = pFile->included_by; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 484 | } |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 485 | exit(1); |
| 486 | |
| 487 | found: |
| 488 | pFile->filename = filename; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 489 | return file; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /******************************************************************* |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 494 | * parse_idl_file |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 495 | * |
| 496 | * If for_h_file is non-zero, it means we are not interested in the idl file |
| 497 | * itself, but only in the contents of the .h file that will be generated from it. |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 498 | */ |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 499 | static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file ) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 500 | { |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 501 | char *buffer, *include; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 502 | |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 503 | if (for_h_file) |
| 504 | { |
| 505 | /* generated .h file always includes these */ |
| 506 | add_include( pFile, "rpc.h", 0, 1 ); |
| 507 | add_include( pFile, "rpcndr.h", 0, 1 ); |
| 508 | } |
| 509 | |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 510 | input_line = 0; |
| 511 | while ((buffer = get_line( file ))) |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 512 | { |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 513 | char quote; |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 514 | char *p = buffer; |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 515 | while (*p && isspace(*p)) p++; |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 516 | |
| 517 | if (!strncmp( p, "import", 6 )) |
| 518 | { |
| 519 | p += 6; |
| 520 | while (*p && isspace(*p)) p++; |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 521 | if (*p != '"') continue; |
| 522 | include = ++p; |
| 523 | while (*p && (*p != '"')) p++; |
| 524 | if (!*p) fatal_error( "%s:%d: Malformed import directive\n", pFile->filename, input_line ); |
| 525 | *p = 0; |
| 526 | if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" ); |
| 527 | add_include( pFile, include, input_line, 0 ); |
| 528 | continue; |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 529 | } |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 530 | |
| 531 | if (for_h_file) /* only check for #include inside cpp_quote */ |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 532 | { |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 533 | if (strncmp( p, "cpp_quote", 9 )) continue; |
| 534 | p += 9; |
| 535 | while (*p && isspace(*p)) p++; |
| 536 | if (*p++ != '(') continue; |
| 537 | while (*p && isspace(*p)) p++; |
| 538 | if (*p++ != '"') continue; |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 539 | if (*p++ != '#') continue; |
| 540 | while (*p && isspace(*p)) p++; |
| 541 | if (strncmp( p, "include", 7 )) continue; |
| 542 | p += 7; |
| 543 | while (*p && isspace(*p)) p++; |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 544 | if (*p == '\\' && p[1] == '"') |
| 545 | { |
| 546 | p += 2; |
| 547 | quote = '"'; |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | if (*p++ != '<' ) continue; |
| 552 | quote = '>'; |
| 553 | } |
| 554 | include = p; |
| 555 | while (*p && (*p != quote)) p++; |
| 556 | if (!*p || (quote == '"' && p[-1] != '\\')) |
| 557 | fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n", |
| 558 | pFile->filename, input_line ); |
| 559 | if (quote == '"') p--; /* remove backslash */ |
| 560 | *p = 0; |
| 561 | add_include( pFile, include, input_line, (quote == '>') ); |
| 562 | continue; |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 565 | /* check for normal #include */ |
| 566 | if (*p++ != '#') continue; |
| 567 | while (*p && isspace(*p)) p++; |
| 568 | if (strncmp( p, "include", 7 )) continue; |
| 569 | p += 7; |
| 570 | while (*p && isspace(*p)) p++; |
| 571 | if (*p != '\"' && *p != '<' ) continue; |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 572 | quote = *p++; |
| 573 | if (quote == '<') quote = '>'; |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 574 | include = p; |
Alexandre Julliard | 0bcf775 | 2003-06-20 21:31:13 +0000 | [diff] [blame] | 575 | while (*p && (*p != quote)) p++; |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 576 | if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", pFile->filename, input_line ); |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 577 | *p = 0; |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 578 | add_include( pFile, include, input_line, (quote == '>') ); |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 579 | } |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 580 | } |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 581 | |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 582 | /******************************************************************* |
| 583 | * parse_c_file |
| 584 | */ |
| 585 | static void parse_c_file( INCL_FILE *pFile, FILE *file ) |
| 586 | { |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 587 | char *buffer, *include; |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 588 | |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 589 | input_line = 0; |
| 590 | while ((buffer = get_line( file ))) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 591 | { |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 592 | char quote; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 593 | char *p = buffer; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 594 | while (*p && isspace(*p)) p++; |
| 595 | if (*p++ != '#') continue; |
| 596 | while (*p && isspace(*p)) p++; |
| 597 | if (strncmp( p, "include", 7 )) continue; |
| 598 | p += 7; |
| 599 | while (*p && isspace(*p)) p++; |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 600 | if (*p != '\"' && *p != '<' ) continue; |
| 601 | quote = *p++; |
| 602 | if (quote == '<') quote = '>'; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 603 | include = p; |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 604 | while (*p && (*p != quote)) p++; |
Alexandre Julliard | ffca0d6 | 2004-04-07 04:00:16 +0000 | [diff] [blame] | 605 | if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 606 | pFile->filename, input_line ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 607 | *p = 0; |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 608 | add_include( pFile, include, input_line, (quote == '>') ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 609 | } |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | /******************************************************************* |
Alexandre Julliard | f92ef1c | 2006-12-26 15:02:03 +0100 | [diff] [blame] | 614 | * parse_rc_file |
| 615 | */ |
| 616 | static void parse_rc_file( INCL_FILE *pFile, FILE *file ) |
| 617 | { |
| 618 | char *buffer, *include; |
| 619 | |
| 620 | input_line = 0; |
| 621 | while ((buffer = get_line( file ))) |
| 622 | { |
| 623 | char quote; |
| 624 | char *p = buffer; |
| 625 | while (*p && isspace(*p)) p++; |
| 626 | |
| 627 | if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */ |
| 628 | { |
| 629 | p += 2; |
| 630 | while (*p && isspace(*p)) p++; |
| 631 | if (strncmp( p, "@makedep:", 9 )) continue; |
| 632 | p += 9; |
| 633 | while (*p && isspace(*p)) p++; |
| 634 | quote = '"'; |
| 635 | if (*p == quote) |
| 636 | { |
| 637 | include = ++p; |
| 638 | while (*p && *p != quote) p++; |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | include = p; |
| 643 | while (*p && !isspace(*p) && *p != '*') p++; |
| 644 | } |
| 645 | if (!*p) |
| 646 | fatal_error( "%s:%d: Malformed makedep comment\n", pFile->filename, input_line ); |
| 647 | *p = 0; |
| 648 | } |
| 649 | else /* check for #include */ |
| 650 | { |
| 651 | if (*p++ != '#') continue; |
| 652 | while (*p && isspace(*p)) p++; |
| 653 | if (strncmp( p, "include", 7 )) continue; |
| 654 | p += 7; |
| 655 | while (*p && isspace(*p)) p++; |
| 656 | if (*p != '\"' && *p != '<' ) continue; |
| 657 | quote = *p++; |
| 658 | if (quote == '<') quote = '>'; |
| 659 | include = p; |
| 660 | while (*p && (*p != quote)) p++; |
| 661 | if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", |
| 662 | pFile->filename, input_line ); |
| 663 | *p = 0; |
| 664 | } |
| 665 | add_include( pFile, include, input_line, (quote == '>') ); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | |
| 670 | /******************************************************************* |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 671 | * parse_generated_idl |
| 672 | */ |
| 673 | static void parse_generated_idl( INCL_FILE *source ) |
| 674 | { |
| 675 | char *header, *basename; |
| 676 | |
| 677 | basename = xstrdup( source->name ); |
| 678 | basename[strlen(basename) - 4] = 0; |
| 679 | header = strmake( "%s.h", basename ); |
| 680 | source->filename = xstrdup( source->name ); |
| 681 | |
| 682 | if (strendswith( source->name, "_c.c" )) |
| 683 | { |
| 684 | add_include( source, header, 0, 0 ); |
| 685 | } |
| 686 | else if (strendswith( source->name, "_i.c" )) |
| 687 | { |
| 688 | add_include( source, "rpc.h", 0, 1 ); |
| 689 | add_include( source, "rpcndr.h", 0, 1 ); |
| 690 | add_include( source, "guiddef.h", 0, 1 ); |
| 691 | } |
| 692 | else if (strendswith( source->name, "_p.c" )) |
| 693 | { |
Alexandre Julliard | 75c2184 | 2007-01-19 13:17:17 +0100 | [diff] [blame] | 694 | add_include( source, "objbase.h", 0, 1 ); |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 695 | add_include( source, "rpcproxy.h", 0, 1 ); |
Alexandre Julliard | d820208 | 2008-07-03 12:59:50 +0200 | [diff] [blame] | 696 | add_include( source, "wine/exception.h", 0, 1 ); |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 697 | add_include( source, header, 0, 0 ); |
| 698 | } |
| 699 | else if (strendswith( source->name, "_s.c" )) |
| 700 | { |
Alexandre Julliard | d820208 | 2008-07-03 12:59:50 +0200 | [diff] [blame] | 701 | add_include( source, "wine/exception.h", 0, 1 ); |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 702 | add_include( source, header, 0, 0 ); |
| 703 | } |
Alexandre Julliard | b996605 | 2007-12-10 14:26:25 +0100 | [diff] [blame] | 704 | else if (!strcmp( source->name, "dlldata.c" )) |
| 705 | { |
| 706 | add_include( source, "objbase.h", 0, 1 ); |
| 707 | add_include( source, "rpcproxy.h", 0, 1 ); |
| 708 | } |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 709 | |
| 710 | free( header ); |
| 711 | free( basename ); |
| 712 | } |
| 713 | |
| 714 | /******************************************************************* |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 715 | * parse_file |
| 716 | */ |
| 717 | static void parse_file( INCL_FILE *pFile, int src ) |
| 718 | { |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 719 | FILE *file; |
| 720 | |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 721 | /* special case for source files generated from idl */ |
| 722 | if (strendswith( pFile->name, "_c.c" ) || |
| 723 | strendswith( pFile->name, "_i.c" ) || |
| 724 | strendswith( pFile->name, "_p.c" ) || |
Alexandre Julliard | b996605 | 2007-12-10 14:26:25 +0100 | [diff] [blame] | 725 | strendswith( pFile->name, "_s.c" ) || |
| 726 | !strcmp( pFile->name, "dlldata.c" )) |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 727 | { |
| 728 | parse_generated_idl( pFile ); |
| 729 | return; |
| 730 | } |
| 731 | |
Alexandre Julliard | 5f4f2c0 | 2008-04-24 23:43:04 +0200 | [diff] [blame] | 732 | /* don't try to open .tlb files */ |
| 733 | if (strendswith( pFile->name, ".tlb" )) |
| 734 | { |
| 735 | pFile->filename = xstrdup( pFile->name ); |
| 736 | return; |
| 737 | } |
| 738 | |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 739 | file = src ? open_src_file( pFile ) : open_include_file( pFile ); |
| 740 | if (!file) return; |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 741 | |
| 742 | if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" )) |
| 743 | parse_idl_file( pFile, file, 1 ); |
| 744 | else if (strendswith( pFile->filename, ".idl" )) |
| 745 | parse_idl_file( pFile, file, 0 ); |
Alexandre Julliard | f92ef1c | 2006-12-26 15:02:03 +0100 | [diff] [blame] | 746 | else if (strendswith( pFile->filename, ".c" ) || |
| 747 | strendswith( pFile->filename, ".h" ) || |
| 748 | strendswith( pFile->filename, ".l" ) || |
| 749 | strendswith( pFile->filename, ".y" )) |
Alexandre Julliard | e4fca88 | 2006-09-10 22:04:42 +0200 | [diff] [blame] | 750 | parse_c_file( pFile, file ); |
Alexandre Julliard | f92ef1c | 2006-12-26 15:02:03 +0100 | [diff] [blame] | 751 | else if (strendswith( pFile->filename, ".rc" )) |
| 752 | parse_rc_file( pFile, file ); |
Alexandre Julliard | 139a4b1 | 1996-11-02 14:24:07 +0000 | [diff] [blame] | 753 | fclose(file); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | |
| 757 | /******************************************************************* |
Alexandre Julliard | b996605 | 2007-12-10 14:26:25 +0100 | [diff] [blame] | 758 | * add_src_file |
| 759 | * |
| 760 | * Add a source file to the list. |
| 761 | */ |
| 762 | static INCL_FILE *add_src_file( const char *name ) |
| 763 | { |
| 764 | INCL_FILE *file; |
| 765 | |
| 766 | if (find_src_file( name )) return NULL; /* we already have it */ |
| 767 | file = xmalloc( sizeof(*file) ); |
| 768 | memset( file, 0, sizeof(*file) ); |
| 769 | file->name = xstrdup(name); |
| 770 | list_add_tail( &sources, &file->entry ); |
| 771 | parse_file( file, 1 ); |
| 772 | return file; |
| 773 | } |
| 774 | |
| 775 | |
| 776 | /******************************************************************* |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 777 | * output_include |
| 778 | */ |
| 779 | static void output_include( FILE *file, INCL_FILE *pFile, |
| 780 | INCL_FILE *owner, int *column ) |
| 781 | { |
| 782 | int i; |
| 783 | |
| 784 | if (pFile->owner == owner) return; |
Alexandre Julliard | fed8f1c | 2002-02-15 19:57:27 +0000 | [diff] [blame] | 785 | if (!pFile->filename) return; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 786 | pFile->owner = owner; |
| 787 | if (*column + strlen(pFile->filename) + 1 > 70) |
| 788 | { |
| 789 | fprintf( file, " \\\n" ); |
| 790 | *column = 0; |
| 791 | } |
| 792 | fprintf( file, " %s", pFile->filename ); |
| 793 | *column += strlen(pFile->filename) + 1; |
| 794 | for (i = 0; i < MAX_INCLUDES; i++) |
| 795 | if (pFile->files[i]) output_include( file, pFile->files[i], |
| 796 | owner, column ); |
| 797 | } |
| 798 | |
| 799 | |
| 800 | /******************************************************************* |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 801 | * output_src |
| 802 | */ |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 803 | static int output_src( FILE *file, INCL_FILE *pFile, int *column ) |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 804 | { |
Alexandre Julliard | 2d4b33a | 2000-03-19 21:20:16 +0000 | [diff] [blame] | 805 | char *obj = xstrdup( pFile->name ); |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 806 | char *ext = get_extension( obj ); |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 807 | if (ext) |
| 808 | { |
Alexandre Julliard | 21ec006 | 2000-10-23 21:39:39 +0000 | [diff] [blame] | 809 | *ext++ = 0; |
| 810 | if (!strcmp( ext, "y" )) /* yacc file */ |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 811 | { |
Alexandre Julliard | cd91bc6 | 2005-09-28 18:17:08 +0000 | [diff] [blame] | 812 | *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj ); |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 813 | } |
Alexandre Julliard | 21ec006 | 2000-10-23 21:39:39 +0000 | [diff] [blame] | 814 | else if (!strcmp( ext, "l" )) /* lex file */ |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 815 | { |
Alexandre Julliard | 560c9ee | 2006-09-09 13:16:59 +0200 | [diff] [blame] | 816 | *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj ); |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 817 | } |
Alexandre Julliard | 21ec006 | 2000-10-23 21:39:39 +0000 | [diff] [blame] | 818 | else if (!strcmp( ext, "rc" )) /* resource file */ |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 819 | { |
Alexandre Julliard | 21ec006 | 2000-10-23 21:39:39 +0000 | [diff] [blame] | 820 | *column += fprintf( file, "%s.res: %s", obj, pFile->filename ); |
| 821 | } |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 822 | else if (!strcmp( ext, "mc" )) /* message file */ |
Alexandre Julliard | 21ec006 | 2000-10-23 21:39:39 +0000 | [diff] [blame] | 823 | { |
Alexandre Julliard | d19ad39 | 2000-11-06 05:32:59 +0000 | [diff] [blame] | 824 | *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename ); |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 825 | } |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 826 | else if (!strcmp( ext, "idl" )) /* IDL file */ |
| 827 | { |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 828 | char *name; |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 829 | int got_header = 0; |
| 830 | const char *suffix = "cips"; |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 831 | |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 832 | name = strmake( "%s.tlb", obj ); |
| 833 | if (find_src_file( name )) *column += fprintf( file, "%s", name ); |
| 834 | else |
| 835 | { |
| 836 | got_header = 1; |
| 837 | *column += fprintf( file, "%s.h", obj ); |
| 838 | } |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 839 | free( name ); |
| 840 | |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 841 | while (*suffix) |
| 842 | { |
| 843 | name = strmake( "%s_%c.c", obj, *suffix ); |
| 844 | if (find_src_file( name )) |
| 845 | { |
| 846 | if (!got_header++) *column += fprintf( file, " %s.h", obj ); |
| 847 | *column += fprintf( file, " %s", name ); |
| 848 | } |
| 849 | free( name ); |
| 850 | suffix++; |
| 851 | } |
Alexandre Julliard | 8f31f92 | 2006-10-16 17:19:07 +0200 | [diff] [blame] | 852 | *column += fprintf( file, ": %s", pFile->filename ); |
Alexandre Julliard | aa89ecc | 2003-04-11 00:38:56 +0000 | [diff] [blame] | 853 | } |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 854 | else if (!strcmp( ext, "tlb" )) |
| 855 | { |
| 856 | return 0; /* nothing to do for typelib files */ |
| 857 | } |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 858 | else |
| 859 | { |
Alexandre Julliard | 21ec006 | 2000-10-23 21:39:39 +0000 | [diff] [blame] | 860 | *column += fprintf( file, "%s.o: %s", obj, pFile->filename ); |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | free( obj ); |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 864 | return 1; |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | |
| 868 | /******************************************************************* |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 869 | * output_dependencies |
| 870 | */ |
| 871 | static void output_dependencies(void) |
| 872 | { |
| 873 | INCL_FILE *pFile; |
| 874 | int i, column; |
| 875 | FILE *file = NULL; |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 876 | char *buffer; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 877 | |
| 878 | if (Separator && ((file = fopen( OutputFileName, "r+" )))) |
| 879 | { |
Alexandre Julliard | b7ef1b2 | 2006-08-01 12:16:29 +0200 | [diff] [blame] | 880 | while ((buffer = get_line( file ))) |
Alexandre Julliard | 1b9535a | 2006-09-22 09:25:49 +0200 | [diff] [blame] | 881 | { |
| 882 | if (strncmp( buffer, Separator, strlen(Separator) )) continue; |
| 883 | ftruncate( fileno(file), ftell(file) ); |
| 884 | fseek( file, 0L, SEEK_END ); |
| 885 | break; |
| 886 | } |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 887 | } |
| 888 | if (!file) |
| 889 | { |
Dimitrie O. Paun | b631a81 | 2000-12-05 03:57:35 +0000 | [diff] [blame] | 890 | if (!(file = fopen( OutputFileName, Separator ? "a" : "w" ))) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 891 | { |
| 892 | perror( OutputFileName ); |
| 893 | exit(1); |
| 894 | } |
| 895 | } |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 896 | LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry ) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 897 | { |
Alexandre Julliard | 3db94ef | 1997-09-28 17:43:24 +0000 | [diff] [blame] | 898 | column = 0; |
Alexandre Julliard | da19122 | 2008-04-24 22:13:57 +0200 | [diff] [blame] | 899 | if (!output_src( file, pFile, &column )) continue; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 900 | for (i = 0; i < MAX_INCLUDES; i++) |
| 901 | if (pFile->files[i]) output_include( file, pFile->files[i], |
| 902 | pFile, &column ); |
| 903 | fprintf( file, "\n" ); |
| 904 | } |
Alexandre Julliard | 139a4b1 | 1996-11-02 14:24:07 +0000 | [diff] [blame] | 905 | fclose(file); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | |
| 909 | /******************************************************************* |
| 910 | * parse_option |
| 911 | */ |
| 912 | static void parse_option( const char *opt ) |
| 913 | { |
| 914 | switch(opt[1]) |
| 915 | { |
| 916 | case 'I': |
| 917 | if (opt[2]) add_include_path( opt + 2 ); |
| 918 | break; |
| 919 | case 'C': |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 920 | src_dir = opt + 2; |
| 921 | break; |
| 922 | case 'S': |
| 923 | top_src_dir = opt + 2; |
| 924 | break; |
| 925 | case 'T': |
| 926 | top_obj_dir = opt + 2; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 927 | break; |
| 928 | case 'f': |
| 929 | if (opt[2]) OutputFileName = opt + 2; |
| 930 | break; |
| 931 | case 's': |
| 932 | if (opt[2]) Separator = opt + 2; |
| 933 | else Separator = NULL; |
| 934 | break; |
| 935 | default: |
| 936 | fprintf( stderr, "Unknown option '%s'\n", opt ); |
| 937 | fprintf( stderr, Usage, ProgramName ); |
| 938 | exit(1); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | |
| 943 | /******************************************************************* |
| 944 | * main |
| 945 | */ |
| 946 | int main( int argc, char *argv[] ) |
| 947 | { |
| 948 | INCL_FILE *pFile; |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 949 | INCL_PATH *path, *next; |
| 950 | int i, j; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 951 | |
| 952 | ProgramName = argv[0]; |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 953 | |
| 954 | i = 1; |
| 955 | while (i < argc) |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 956 | { |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 957 | if (argv[i][0] == '-') |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 958 | { |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 959 | parse_option( argv[i] ); |
| 960 | for (j = i; j < argc; j++) argv[j] = argv[j+1]; |
| 961 | argc--; |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 962 | } |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 963 | else i++; |
| 964 | } |
| 965 | |
Alexandre Julliard | ded32d5 | 2006-08-01 12:37:18 +0200 | [diff] [blame] | 966 | /* ignore redundant source paths */ |
| 967 | if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL; |
| 968 | if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL; |
| 969 | |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 970 | /* get rid of absolute paths that don't point into the source dir */ |
| 971 | LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry ) |
| 972 | { |
| 973 | if (path->name[0] != '/') continue; |
| 974 | if (top_src_dir) |
| 975 | { |
Alexandre Julliard | 4fff18b | 2007-03-01 15:21:39 +0100 | [diff] [blame] | 976 | if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) && |
| 977 | path->name[strlen(top_src_dir)] == '/') continue; |
Alexandre Julliard | dea28ee | 2006-08-01 12:27:22 +0200 | [diff] [blame] | 978 | } |
| 979 | list_remove( &path->entry ); |
| 980 | free( path ); |
| 981 | } |
| 982 | |
| 983 | for (i = 1; i < argc; i++) |
| 984 | { |
Alexandre Julliard | b996605 | 2007-12-10 14:26:25 +0100 | [diff] [blame] | 985 | add_src_file( argv[i] ); |
| 986 | if (strendswith( argv[i], "_p.c" )) add_src_file( "dlldata.c" ); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 987 | } |
Alexandre Julliard | f25c4d4 | 2006-08-01 12:13:57 +0200 | [diff] [blame] | 988 | LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 ); |
Alexandre Julliard | 1b9535a | 2006-09-22 09:25:49 +0200 | [diff] [blame] | 989 | output_dependencies(); |
Alexandre Julliard | 530ee84 | 1996-10-23 16:59:13 +0000 | [diff] [blame] | 990 | return 0; |
| 991 | } |