omkdepend
=========

This code is taken from the X11R6 makedepend sources.  All I have done is:

 * Add GNUWIN32 file name translation.
 * Remove all predefined symbols, include directories and other
   platform-specific behaviour.  Its behaviour should be the same on all
   platforms.  All symbols and include directories must be explicitly given on
   the command line with -D and -I flags.
 * Fix processing of numbers in #if expressions.
 * Fix processing of -D flag.
 * Fix C++ comment handling.
 * Improve #error warnings.
 * Change the output so that processing "xxx.c" or "xxx.cc" produces "xxx.d".

I disclaim all responsibility for the unclean nature of the code herein.

TJR 17/7/97.


For reference, here is a complete diff from the X11R6 source:

% diff /packages/old/X11R6/src/xc/config/makedepend .
Only in /packages/old/X11R6/src/xc/config/makedepend: Imakefile
Only in /packages/old/X11R6/src/xc/config/makedepend: cpp.ed
Only in /packages/old/X11R6/src/xc/config/makedepend: mkdepend.man
Only in .: README
Only in .: dir.mk
Only in .: gnuwin32.c
Only in .: gnuwin32.h


  *************** def.h ***************

diff /packages/old/X11R6/src/xc/config/makedepend/def.h ./def.h
29,33d28
< #include <X11/Xosdefs.h>
< #ifdef WIN32
< #include <X11/Xw32defs.h>
< #endif
< #include <X11/Xfuncproto.h>


  *************** ifparser.c ***************

diff /packages/old/X11R6/src/xc/config/makedepend/ifparser.c ./ifparser.c
102d101
< #ifdef WIN32
104,108d102
< #else
<     *valp = atoi (cp);
<     /* EMPTY */
<     for (cp++; isdigit(*cp); cp++) ;
< #endif


  *************** include.c ***************

diff /packages/old/X11R6/src/xc/config/makedepend/include.c ./include.c
103a104,106
> #ifdef WIN32
>                       sprintf(path, "%s\\%s", *pp, include);
> #else
104a108
> #endif


  *************** main.c ***************

diff /packages/old/X11R6/src/xc/config/makedepend/main.c ./main.c
28a29
> #include "gnuwin32.h"
77,79c78
< #define MAKEDEPEND
< #include "imakemdep.h"        /* from config sources */
< #undef MAKEDEPEND
---
> struct symtab predefs[] = { {NULL, NULL} };
89,90c88
< char  *objsuffix = OBJSUFFIX;
< char  *startat = "# DO NOT DELETE";
---
> char  *objsuffix = ".o";
138a137,139
> #ifdef WIN32
>       GetMounts();
> #endif
202a204,206
> #ifdef WIN32
>                       argv[0] = TranslateFileNameU2D(argv[0],0);
> #endif
211a216,217
>                   {
>                       int offset = 2;
214a221
>                               offset = 0;
216c223
<                       for (p=argv[0] + 2; *p ; p++)
---
>                       for (p=argv[0] + offset; *p ; p++)
221c228
<                       define(argv[0] + 2, &maininclist);
---
>                       define(argv[0] + offset, &maininclist);
222a230
>                   }
230a239,241
> #ifdef WIN32
>                       *(incp-1) = TranslateFileNameU2D(*(incp-1),0);
> #endif
275,294d285
<               case 's':
<                       if (endmarker) break;
<                       startat = argv[0]+2;
<                       if (*startat == '\0') {
<                               startat = *(++argv);
<                               argc--;
<                       }
<                       if (*startat != '#')
<                               fatalerr("-s flag's value should start %s\n",
<                                       "with '#'.");
<                       break;
<               case 'f':
<                       if (endmarker) break;
<                       makefile = argv[0]+2;
<                       if (*makefile == '\0') {
<                               makefile = *(++argv);
<                               argc--;
<                       }
<                       break;
< 
312,324d302
< #ifdef PREINCDIR
<           if (incp >= includedirs + MAXDIRS)
<               fatalerr("Too many -I flags.\n");
<           *incp++ = PREINCDIR;
< #endif
<           if (incp >= includedirs + MAXDIRS)
<               fatalerr("Too many -I flags.\n");
<           *incp++ = INCLUDEDIR;
< #ifdef POSTINCDIR
<           if (incp >= includedirs + MAXDIRS)
<               fatalerr("Too many -I flags.\n");
<           *incp++ = POSTINCDIR;
< #endif
331,332d308
<       redirect(startat, makefile);
< 
401a378,385
>               char *base = base_name(*fp);
>               char *depfile = (char *)malloc(strlen(base) + 3);
>               sprintf(depfile,"%s.d",base);
>               if (!freopen(depfile, "wb", stdout))
>                   fatalerr("cannot open \"%s\"\n", depfile);
>               free(depfile);
>               free(base);
>               printed = FALSE;
405c389
<               find_includes(filecontent, ip, ip, 0, FALSE);
---
>               find_includes(filecontent, ip, ip, 0, TRUE);
408a393,394
>               if (printed)
>                       printf("\n");
410,411d395
<       if (printed)
<               printf("\n");
503d486
< #ifdef WIN32
507a491
>                       p--;    /* go back to before newline */
511d494
< #endif
550c533,535
<       file = copy(file);
---
>       for(p=file+strlen(file); p>file && *p != '/' && *p != '\\'; p--) ;
>       if (p>file) p++;
>       file = copy(p);
558,639d542
< #if defined(USG) && !defined(CRAY) && !defined(SVR4)
< int rename (from, to)
<     char *from, *to;
< {
<     (void) unlink (to);
<     if (link (from, to) == 0) {
<       unlink (from);
<       return 0;
<     } else {
<       return -1;
<     }
< }
< #endif /* USGISH */
< 
< redirect(line, makefile)
<       char    *line,
<               *makefile;
< {
<       struct stat     st;
<       FILE    *fdin, *fdout;
<       char    backup[ BUFSIZ ],
<               buf[ BUFSIZ ];
<       boolean found = FALSE;
<       int     len;
< 
<       /*
<        * if makefile is "-" then let it pour onto stdout.
<        */
<       if (makefile && *makefile == '-' && *(makefile+1) == '\0')
<               return;
< 
<       /*
<        * use a default makefile is not specified.
<        */
<       if (!makefile) {
<               if (stat("Makefile", &st) == 0)
<                       makefile = "Makefile";
<               else if (stat("makefile", &st) == 0)
<                       makefile = "makefile";
<               else
<                       fatalerr("[mM]akefile is not present\n");
<       }
<       else
<           stat(makefile, &st);
<       if ((fdin = fopen(makefile, "r")) == NULL)
<               fatalerr("cannot open \"%s\"\n", makefile);
<       sprintf(backup, "%s.bak", makefile);
<       unlink(backup);
< #ifdef WIN32
<       fclose(fdin);
< #endif
<       if (rename(makefile, backup) < 0)
<               fatalerr("cannot rename %s to %s\n", makefile, backup);
< #ifdef WIN32
<       if ((fdin = fopen(backup, "r")) == NULL)
<               fatalerr("cannot open \"%s\"\n", backup);
< #endif
<       if ((fdout = freopen(makefile, "w", stdout)) == NULL)
<               fatalerr("cannot open \"%s\"\n", backup);
<       len = strlen(line);
<       while (!found && fgets(buf, BUFSIZ, fdin)) {
<               if (*buf == '#' && strncmp(line, buf, len) == 0)
<                       found = TRUE;
<               fputs(buf, fdout);
<       }
<       if (!found) {
<               if (verbose)
<               warning("Adding new delimiting line \"%s\" and dependencies...\n",
<                       line);
<               puts(line); /* same as fputs(fdout); but with newline */
<       } else if (append) {
<           while (fgets(buf, BUFSIZ, fdin)) {
<               fputs(buf, fdout);
<           }
<       }
<       fflush(fdout);
< #if defined(USGISH) || defined(_SEQUENT_)
<       chmod(makefile, st.st_mode);
< #else
<         fchmod(fileno(fdout), st.st_mode);
< #endif /* USGISH */
< }


  *************** parse.c ***************

diff /packages/old/X11R6/src/xc/config/makedepend/parse.c ./parse.c
131c131,132
<                       warning("%s: %d: %s\n", file_red->i_file,
---
>                       warning("(from %s) %s: %d: %s\n",
>                               file_red->i_file, file->i_file,


  *************** pr.c ***************

diff /packages/old/X11R6/src/xc/config/makedepend/pr.c ./pr.c
98d97
<       static int      current_len;
100a100,104
> #ifdef WIN32
>       char *transfile = TranslateFileNameD2U(ip->i_file,0);
> #else
>       char *transfile = ip->i_file;
> #endif
103,104c107
<       len = strlen(ip->i_file)+1;
<       if (current_len + len > width || file != lastfile) {
---
>       if (file != lastfile) {
106,108c109,110
<               sprintf(buf, "\n%s%s%s: %s", objprefix, base, objsuffix,
<                       ip->i_file);
<               len = current_len = strlen(buf);
---
>               sprintf(buf, "%s%s%s %s.d: %s", objprefix, base, objsuffix,
>                       base, transfile);
111,113c113
<               buf[0] = ' ';
<               strcpy(buf+1, ip->i_file);
<               current_len += len;
---
>               sprintf(buf, " \\\n %s", transfile);
115c115
<       fwrite(buf, len, 1, stdout);
---
>       fwrite(buf, strlen(buf), 1, stdout);
124c124
<       printf("\n# %s includes:", ip->i_file);
---
>       printf("\n# %s includes:", transfile);
126a127,129
> #ifdef WIN32
>       free(transfile);
> #endif
