| 1 | /* $Id: kDepPre.c 393 2006-01-13 00:42:10Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kDepPre - Dependency Generator using Precompiler output.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2005-2006 knut st. osmundsen <bird@innotek.de>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kBuild.
|
|---|
| 10 | *
|
|---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kBuild; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /*******************************************************************************
|
|---|
| 29 | * Header Files *
|
|---|
| 30 | *******************************************************************************/
|
|---|
| 31 | #include <stdio.h>
|
|---|
| 32 | #include <string.h>
|
|---|
| 33 | #include <ctype.h>
|
|---|
| 34 | #include "kDep.h"
|
|---|
| 35 |
|
|---|
| 36 | #ifdef HAVE_FGETC_UNLOCKED
|
|---|
| 37 | # define FGETC(s) getc_unlocked(s)
|
|---|
| 38 | #else
|
|---|
| 39 | # define FGETC(s) fgetc(s)
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 | #ifdef NEED_ISBLANK
|
|---|
| 43 | # define isblank(ch) ( (unsigned char)(ch) == ' ' || (unsigned char)(ch) == '\t' )
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Parses the output from a preprocessor of a C-style language.
|
|---|
| 51 | *
|
|---|
| 52 | * @returns 0 on success.
|
|---|
| 53 | * @returns 1 or other approriate exit code on failure.
|
|---|
| 54 | * @param pInput Input stream. (probably not seekable)
|
|---|
| 55 | */
|
|---|
| 56 | static int ParseCPrecompiler(FILE *pInput)
|
|---|
| 57 | {
|
|---|
| 58 | enum
|
|---|
| 59 | {
|
|---|
| 60 | C_DISCOVER = 0,
|
|---|
| 61 | C_SKIP_LINE,
|
|---|
| 62 | C_PARSE_FILENAME,
|
|---|
| 63 | C_EOF
|
|---|
| 64 | } enmMode = C_DISCOVER;
|
|---|
| 65 | PDEP pDep = NULL;
|
|---|
| 66 | int ch;
|
|---|
| 67 | char szBuf[8192];
|
|---|
| 68 |
|
|---|
| 69 | for (;;)
|
|---|
| 70 | {
|
|---|
| 71 | switch (enmMode)
|
|---|
| 72 | {
|
|---|
| 73 | /*
|
|---|
| 74 | * Start of line, need to look for '#[[:space]]*line <num> "file"' and '# <num> "file"'.
|
|---|
| 75 | */
|
|---|
| 76 | case C_DISCOVER:
|
|---|
| 77 | /* first find '#' */
|
|---|
| 78 | while ((ch = FGETC(pInput)) != EOF)
|
|---|
| 79 | if (!isblank(ch))
|
|---|
| 80 | break;
|
|---|
| 81 | if (ch == '#')
|
|---|
| 82 | {
|
|---|
| 83 | /* skip spaces */
|
|---|
| 84 | while ((ch = FGETC(pInput)) != EOF)
|
|---|
| 85 | if (!isblank(ch))
|
|---|
| 86 | break;
|
|---|
| 87 |
|
|---|
| 88 | /* check for "line" */
|
|---|
| 89 | if (ch == 'l')
|
|---|
| 90 | {
|
|---|
| 91 | if ( (ch = FGETC(pInput)) == 'i'
|
|---|
| 92 | && (ch = FGETC(pInput)) == 'n'
|
|---|
| 93 | && (ch = FGETC(pInput)) == 'e')
|
|---|
| 94 | {
|
|---|
| 95 | ch = FGETC(pInput);
|
|---|
| 96 | if (isblank(ch))
|
|---|
| 97 | {
|
|---|
| 98 | /* skip spaces */
|
|---|
| 99 | while ((ch = FGETC(pInput)) != EOF)
|
|---|
| 100 | if (!isblank(ch))
|
|---|
| 101 | break;
|
|---|
| 102 | }
|
|---|
| 103 | else
|
|---|
| 104 | ch = 'x';
|
|---|
| 105 | }
|
|---|
| 106 | else
|
|---|
| 107 | ch = 'x';
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* line number */
|
|---|
| 111 | if (ch >= '0' && ch <= '9')
|
|---|
| 112 | {
|
|---|
| 113 | /* skip the number following spaces */
|
|---|
| 114 | while ((ch = FGETC(pInput)) != EOF)
|
|---|
| 115 | if (!isxdigit(ch))
|
|---|
| 116 | break;
|
|---|
| 117 | if (isblank(ch))
|
|---|
| 118 | {
|
|---|
| 119 | while ((ch = FGETC(pInput)) != EOF)
|
|---|
| 120 | if (!isblank(ch))
|
|---|
| 121 | break;
|
|---|
| 122 | /* quoted filename */
|
|---|
| 123 | if (ch == '"')
|
|---|
| 124 | {
|
|---|
| 125 | enmMode = C_PARSE_FILENAME;
|
|---|
| 126 | break;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | enmMode = C_SKIP_LINE;
|
|---|
| 132 | break;
|
|---|
| 133 |
|
|---|
| 134 | /*
|
|---|
| 135 | * Skip past the end of the current line.
|
|---|
| 136 | */
|
|---|
| 137 | case C_SKIP_LINE:
|
|---|
| 138 | do
|
|---|
| 139 | {
|
|---|
| 140 | if ( ch == '\r'
|
|---|
| 141 | || ch == '\n')
|
|---|
| 142 | break;
|
|---|
| 143 | } while ((ch = FGETC(pInput)) != EOF);
|
|---|
| 144 | enmMode = C_DISCOVER;
|
|---|
| 145 | break;
|
|---|
| 146 |
|
|---|
| 147 | /*
|
|---|
| 148 | * Parse the filename.
|
|---|
| 149 | */
|
|---|
| 150 | case C_PARSE_FILENAME:
|
|---|
| 151 | {
|
|---|
| 152 | /* retreive and unescape the filename. */
|
|---|
| 153 | char *psz = &szBuf[0];
|
|---|
| 154 | while ( (ch = FGETC(pInput)) != EOF
|
|---|
| 155 | && psz < &szBuf[sizeof(szBuf) - 1])
|
|---|
| 156 | {
|
|---|
| 157 | if (ch == '\\')
|
|---|
| 158 | {
|
|---|
| 159 | ch = FGETC(pInput);
|
|---|
| 160 | switch (ch)
|
|---|
| 161 | {
|
|---|
| 162 | case '\\': ch = '/'; break;
|
|---|
| 163 | case 't': ch = '\t'; break;
|
|---|
| 164 | case 'r': ch = '\r'; break;
|
|---|
| 165 | case 'n': ch = '\n'; break;
|
|---|
| 166 | case 'b': ch = '\b'; break;
|
|---|
| 167 | default:
|
|---|
| 168 | fprintf(stderr, "warning: unknown escape char '%c'\n", ch);
|
|---|
| 169 | continue;
|
|---|
| 170 |
|
|---|
| 171 | }
|
|---|
| 172 | *psz++ = ch == '\\' ? '/' : ch;
|
|---|
| 173 | }
|
|---|
| 174 | else if (ch != '"')
|
|---|
| 175 | *psz++ = ch;
|
|---|
| 176 | else
|
|---|
| 177 | {
|
|---|
| 178 | size_t cchFilename = psz - &szBuf[0];
|
|---|
| 179 | *psz = '\0';
|
|---|
| 180 | /* compare with current dep, add & switch on mismatch. */
|
|---|
| 181 | if ( !pDep
|
|---|
| 182 | || pDep->cchFilename != cchFilename
|
|---|
| 183 | || memcmp(pDep->szFilename, szBuf, cchFilename))
|
|---|
| 184 | pDep = depAdd(szBuf, cchFilename);
|
|---|
| 185 | break;
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | enmMode = C_SKIP_LINE;
|
|---|
| 189 | break;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * Handle EOF.
|
|---|
| 194 | */
|
|---|
| 195 | case C_EOF:
|
|---|
| 196 | if (feof(pInput))
|
|---|
| 197 | return 0;
|
|---|
| 198 | enmMode = C_DISCOVER;
|
|---|
| 199 | break;
|
|---|
| 200 | }
|
|---|
| 201 | if (ch == EOF)
|
|---|
| 202 | enmMode = C_EOF;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | return 0;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | static void usage(const char *argv0)
|
|---|
| 210 | {
|
|---|
| 211 | printf("syntax: %s [-l=c] -o <output> -t <target> [-f] [-s] < - | <filename> | -e <cmdline> >\n", argv0);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | int main(int argc, char *argv[])
|
|---|
| 216 | {
|
|---|
| 217 | int i;
|
|---|
| 218 |
|
|---|
| 219 | /* Arguments. */
|
|---|
| 220 | int iExec = 0;
|
|---|
| 221 | FILE *pOutput = NULL;
|
|---|
| 222 | const char *pszOutput = NULL;
|
|---|
| 223 | FILE *pInput = NULL;
|
|---|
| 224 | const char *pszTarget = NULL;
|
|---|
| 225 | int fStubs = 0;
|
|---|
| 226 | int fFixCase = 0;
|
|---|
| 227 | /* Argument parsing. */
|
|---|
| 228 | int fInput = 0; /* set when we've found input argument. */
|
|---|
| 229 |
|
|---|
| 230 | /*
|
|---|
| 231 | * Parse arguments.
|
|---|
| 232 | */
|
|---|
| 233 | if (argc <= 1)
|
|---|
| 234 | {
|
|---|
| 235 | usage(argv[0]);
|
|---|
| 236 | return 1;
|
|---|
| 237 | }
|
|---|
| 238 | for (i = 1; i < argc; i++)
|
|---|
| 239 | {
|
|---|
| 240 | if (argv[i][0] == '-')
|
|---|
| 241 | {
|
|---|
| 242 | switch (argv[i][1])
|
|---|
| 243 | {
|
|---|
| 244 | /*
|
|---|
| 245 | * Output file.
|
|---|
| 246 | */
|
|---|
| 247 | case 'o':
|
|---|
| 248 | {
|
|---|
| 249 | pszOutput = &argv[i][2];
|
|---|
| 250 | if (pOutput)
|
|---|
| 251 | {
|
|---|
| 252 | fprintf(stderr, "%s: syntax error: only one output file!\n", argv[0]);
|
|---|
| 253 | return 1;
|
|---|
| 254 | }
|
|---|
| 255 | if (!*pszOutput)
|
|---|
| 256 | {
|
|---|
| 257 | if (++i >= argc)
|
|---|
| 258 | {
|
|---|
| 259 | fprintf(stderr, "%s: syntax error: The '-o' argument is missing the filename.\n", argv[0]);
|
|---|
| 260 | return 1;
|
|---|
| 261 | }
|
|---|
| 262 | pszOutput = argv[i];
|
|---|
| 263 | }
|
|---|
| 264 | if (pszOutput[0] == '-' && !pszOutput[1])
|
|---|
| 265 | pOutput = stdout;
|
|---|
| 266 | else
|
|---|
| 267 | pOutput = fopen(pszOutput, "w");
|
|---|
| 268 | if (!pOutput)
|
|---|
| 269 | {
|
|---|
| 270 | fprintf(stderr, "%s: error: Failed to create output file '%s'.\n", argv[0], pszOutput);
|
|---|
| 271 | return 1;
|
|---|
| 272 | }
|
|---|
| 273 | break;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /*
|
|---|
| 277 | * Language spec.
|
|---|
| 278 | */
|
|---|
| 279 | case 'l':
|
|---|
| 280 | {
|
|---|
| 281 | const char *psz = &argv[i][2];
|
|---|
| 282 | if (*psz == '=')
|
|---|
| 283 | psz++;
|
|---|
| 284 | if (!strcmp(psz, "c"))
|
|---|
| 285 | ;
|
|---|
| 286 | else
|
|---|
| 287 | {
|
|---|
| 288 | fprintf(stderr, "%s: error: The '%s' language is not supported.\n", argv[0], psz);
|
|---|
| 289 | return 1;
|
|---|
| 290 | }
|
|---|
| 291 | break;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /*
|
|---|
| 295 | * Target name.
|
|---|
| 296 | */
|
|---|
| 297 | case 't':
|
|---|
| 298 | {
|
|---|
| 299 | if (pszTarget)
|
|---|
| 300 | {
|
|---|
| 301 | fprintf(stderr, "%s: syntax error: only one target!\n", argv[0]);
|
|---|
| 302 | return 1;
|
|---|
| 303 | }
|
|---|
| 304 | pszTarget = &argv[i][2];
|
|---|
| 305 | if (!*pszTarget)
|
|---|
| 306 | {
|
|---|
| 307 | if (++i >= argc)
|
|---|
| 308 | {
|
|---|
| 309 | fprintf(stderr, "%s: syntax error: The '-t' argument is missing the target name.\n", argv[0]);
|
|---|
| 310 | return 1;
|
|---|
| 311 | }
|
|---|
| 312 | pszTarget = argv[i];
|
|---|
| 313 | }
|
|---|
| 314 | break;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /*
|
|---|
| 318 | * Exec.
|
|---|
| 319 | */
|
|---|
| 320 | case 'e':
|
|---|
| 321 | {
|
|---|
| 322 | if (++i >= argc)
|
|---|
| 323 | {
|
|---|
| 324 | fprintf(stderr, "%s: syntax error: The '-e' argument is missing the command.\n", argv[0]);
|
|---|
| 325 | return 1;
|
|---|
| 326 | }
|
|---|
| 327 | iExec = i;
|
|---|
| 328 | i = argc - 1;
|
|---|
| 329 | break;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | /*
|
|---|
| 333 | * Pipe input.
|
|---|
| 334 | */
|
|---|
| 335 | case '\0':
|
|---|
| 336 | {
|
|---|
| 337 | pInput = stdin;
|
|---|
| 338 | fInput = 1;
|
|---|
| 339 | break;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | /*
|
|---|
| 343 | * Fix case.
|
|---|
| 344 | */
|
|---|
| 345 | case 'f':
|
|---|
| 346 | {
|
|---|
| 347 | fFixCase = 1;
|
|---|
| 348 | break;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /*
|
|---|
| 352 | * Generate stubs.
|
|---|
| 353 | */
|
|---|
| 354 | case 's':
|
|---|
| 355 | {
|
|---|
| 356 | fStubs = 1;
|
|---|
| 357 | break;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | /*
|
|---|
| 361 | * Invalid argument.
|
|---|
| 362 | */
|
|---|
| 363 | default:
|
|---|
| 364 | fprintf(stderr, "%s: syntax error: Invalid argument '%s'.\n", argv[0], argv[i]);
|
|---|
| 365 | usage(argv[0]);
|
|---|
| 366 | return 1;
|
|---|
| 367 | }
|
|---|
| 368 | }
|
|---|
| 369 | else
|
|---|
| 370 | {
|
|---|
| 371 | pInput = fopen(argv[i], "r");
|
|---|
| 372 | if (!pInput)
|
|---|
| 373 | {
|
|---|
| 374 | fprintf(stderr, "%s: error: Failed to open input file '%s'.\n", argv[0], argv[i]);
|
|---|
| 375 | return 1;
|
|---|
| 376 | }
|
|---|
| 377 | fInput = 1;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /*
|
|---|
| 381 | * End of the line?
|
|---|
| 382 | */
|
|---|
| 383 | if (fInput)
|
|---|
| 384 | {
|
|---|
| 385 | if (++i < argc)
|
|---|
| 386 | {
|
|---|
| 387 | fprintf(stderr, "%s: syntax error: No arguments shall follow the input spec.\n", argv[0]);
|
|---|
| 388 | return 1;
|
|---|
| 389 | }
|
|---|
| 390 | break;
|
|---|
| 391 | }
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | /*
|
|---|
| 395 | * Got all we require?
|
|---|
| 396 | */
|
|---|
| 397 | if (!pInput && iExec <= 0)
|
|---|
| 398 | {
|
|---|
| 399 | fprintf(stderr, "%s: syntax error: No input!\n", argv[0]);
|
|---|
| 400 | return 1;
|
|---|
| 401 | }
|
|---|
| 402 | if (!pOutput)
|
|---|
| 403 | {
|
|---|
| 404 | fprintf(stderr, "%s: syntax error: No output!\n", argv[0]);
|
|---|
| 405 | return 1;
|
|---|
| 406 | }
|
|---|
| 407 | if (!pszTarget)
|
|---|
| 408 | {
|
|---|
| 409 | fprintf(stderr, "%s: syntax error: No target!\n", argv[0]);
|
|---|
| 410 | return 1;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | /*
|
|---|
| 414 | * Spawn process?
|
|---|
| 415 | */
|
|---|
| 416 | if (iExec > 0)
|
|---|
| 417 | {
|
|---|
| 418 | fprintf(stderr, "%s: -e is not yet implemented!\n", argv[0]);
|
|---|
| 419 | return 1;
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | /*
|
|---|
| 423 | * Do the parsing.
|
|---|
| 424 | */
|
|---|
| 425 | i = ParseCPrecompiler(pInput);
|
|---|
| 426 |
|
|---|
| 427 | /*
|
|---|
| 428 | * Reap child.
|
|---|
| 429 | */
|
|---|
| 430 | if (iExec > 0)
|
|---|
| 431 | {
|
|---|
| 432 | // later
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /*
|
|---|
| 436 | * Write the dependecy file.
|
|---|
| 437 | */
|
|---|
| 438 | if (!i)
|
|---|
| 439 | {
|
|---|
| 440 | depOptimize(fFixCase);
|
|---|
| 441 | fprintf(pOutput, "%s:", pszTarget);
|
|---|
| 442 | depPrint(pOutput);
|
|---|
| 443 | if (fStubs)
|
|---|
| 444 | depPrintStubs(pOutput);
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | /*
|
|---|
| 448 | * Close the output, delete output on failure.
|
|---|
| 449 | */
|
|---|
| 450 | if (!i && ferror(pOutput))
|
|---|
| 451 | {
|
|---|
| 452 | i = 1;
|
|---|
| 453 | fprintf(stderr, "%s: error: Error writing to '%s'.\n", argv[0], pszOutput);
|
|---|
| 454 | }
|
|---|
| 455 | fclose(pOutput);
|
|---|
| 456 | if (i)
|
|---|
| 457 | {
|
|---|
| 458 | if (unlink(pszOutput))
|
|---|
| 459 | fprintf(stderr, "%s: warning: failed to remove output file '%s' on failure.\n", argv[0], pszOutput);
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | return i;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|