utils/mojoshader-compiler.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Feb 2009 03:24:03 -0500
changeset 667 8efc63f4ab9b
parent 666 aec40e1c5e99
child 668 983f396a3d71
permissions -rw-r--r--
That should be an "else if", not an "if" ...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Direct3D shaders.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 */
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <stdio.h>
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdlib.h>
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
    12
#include <string.h>
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include "mojoshader.h"
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    15
static const char **include_paths = NULL;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    16
static unsigned int include_path_count = 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    17
665
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    18
#if MOJOSHADER_DEBUG_MALLOC
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    19
static void *Malloc(int len)
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    20
{
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    21
    void *ptr = malloc(len + sizeof (int));
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    22
    int *store = (int *) ptr;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    23
    printf("malloc() %d bytes (%p)\n", len, ptr);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    24
    if (ptr == NULL) return NULL;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    25
    *store = len;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    26
    return (void *) (store + 1);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    27
} // Malloc
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    28
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    29
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    30
static void Free(void *_ptr)
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    31
{
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    32
    int *ptr = (((int *) _ptr) - 1);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    33
    int len = *ptr;
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    34
    printf("free() %d bytes (%p)\n", len, ptr);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    35
    free(ptr);
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    36
} // Free
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    37
#else
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    38
#define Malloc NULL
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    39
#define Free NULL
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
    40
#endif
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    41
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    42
static int open_include(MOJOSHADER_includeType inctype, const char *fname,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    43
                        const char *parent, const char **outdata,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    44
                        unsigned int *outbytes, MOJOSHADER_malloc m,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    45
                        MOJOSHADER_free f, void *d)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    46
{
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    47
    int i;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    48
    for (i = 0; i < include_path_count; i++)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    49
    {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    50
        const char *path = include_paths[i];
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    51
        const size_t len = strlen(path) + strlen(fname) + 2;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    52
        char *buf = (char *) m(len, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    53
        if (buf == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    54
            return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    55
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    56
        snprintf(buf, len, "%s/%s", path, fname);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    57
        FILE *io = fopen(buf, "rb");
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    58
        f(buf, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    59
        if (io == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    60
            continue;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    61
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    62
        if (fseek(io, 0, SEEK_END) != -1)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    63
        {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    64
            const long fsize = ftell(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    65
            if ((fsize == -1) || (fseek(io, 0, SEEK_SET) == -1))
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    66
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    67
                fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    68
                return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    69
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    70
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    71
            char *data = (char *) m(fsize, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    72
            if (data == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    73
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    74
                fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    75
                return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    76
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    77
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    78
            if (fread(data, fsize, 1, io) != 1)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    79
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    80
                f(data, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    81
                fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    82
                return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    83
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    84
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    85
            fclose(io);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    86
            *outdata = data;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    87
            *outbytes = (unsigned int) fsize;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    88
            return 1;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    89
        } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    90
    } // for
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    91
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    92
    return 0;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    93
} // open_include
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    94
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    95
663
e7b3b60bd3b1 Made function static.
Ryan C. Gordon <icculus@icculus.org>
parents: 662
diff changeset
    96
static void close_include(const char *data, MOJOSHADER_malloc m,
e7b3b60bd3b1 Made function static.
Ryan C. Gordon <icculus@icculus.org>
parents: 662
diff changeset
    97
                          MOJOSHADER_free f, void *d)
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    98
{
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
    99
    f((void *) data, d);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   100
} // close_include
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   101
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   102
578
6c8f73c845e7 Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 577
diff changeset
   103
static int preprocess(const char *fname, const char *buf, int len,
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   104
                      const char *outfile,
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   105
                      const MOJOSHADER_preprocessorDefine *defs,
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   106
                      unsigned int defcount)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
{
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   108
    FILE *io = outfile ? fopen(outfile, "wb") : stdout;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    if (io == NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
        printf(" ... fopen('%s') failed.\n", outfile);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        return 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    const MOJOSHADER_preprocessData *pd;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    int retval = 0;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
665
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
   118
    pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, open_include,
521d24646891 Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 664
diff changeset
   119
                               close_include, Malloc, Free, NULL);
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    if (pd->error_count > 0)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
        int i;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        for (i = 0; i < pd->error_count; i++)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
        {
577
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 556
diff changeset
   126
            printf("%s:%d: ERROR: %s\n",
bec531dd448e Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 556
diff changeset
   127
                    pd->errors[i].filename ? pd->errors[i].filename : "???",
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
                    pd->errors[i].error_position,
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
                    pd->errors[i].error);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
        } // for
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        if (pd->output != NULL)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
        {
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
            if (fwrite(pd->output, pd->output_len, 1, io) != 1)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
                printf(" ... fwrite('%s') failed.\n", outfile);
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   138
            else if ((outfile != NULL) && (fclose(io) == EOF))
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
                printf(" ... fclose('%s') failed.\n", outfile);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
            else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
                retval = 1;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
        } // if
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    } // else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    MOJOSHADER_freePreprocessData(pd);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    return retval;
556
04282775cc2c Fixed cut-and-pasted comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 555
diff changeset
   147
} // preprocess
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
int main(int argc, char **argv)
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
{
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    int retval = 1;
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   153
    const char *infile = NULL;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   154
    const char *outfile = NULL;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   155
    int i;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   157
    MOJOSHADER_preprocessorDefine *defs = NULL;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   158
    unsigned int defcount = 0;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   159
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   160
    for (i = 1; i < argc; i++)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    {
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   162
        const char *arg = argv[i];
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   163
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   164
        if (strcmp(arg, "-o") == 0)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   165
        {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   166
            if (outfile != NULL)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   167
            {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   168
                printf("multiple output files specified.\n");
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   169
                exit(1);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   170
            } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   171
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   172
            arg = argv[++i];
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   173
            if (arg == NULL)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   174
            {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   175
                printf("no filename after '-o'\n");
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   176
                exit(1);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   177
            } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   178
            outfile = arg;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   179
        } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   180
667
8efc63f4ab9b That should be an "else if", not an "if" ...
Ryan C. Gordon <icculus@icculus.org>
parents: 666
diff changeset
   181
        else if (strcmp(arg, "-I") == 0)
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   182
        {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   183
            arg = argv[++i];
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   184
            if (arg == NULL)
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   185
            {
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   186
                printf("no path after '-I'\n");
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   187
                exit(1);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   188
            } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   189
            include_paths = (const char **) realloc(include_paths,
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   190
                       (include_path_count+1) * sizeof (char *));
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   191
            include_paths[include_path_count] = arg;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   192
            include_path_count++;
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   193
        } // if
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   194
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   195
        else if (strncmp(arg, "-D", 2) == 0)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   196
        {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   197
            arg += 2;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   198
            char *ident = strdup(arg);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   199
            char *ptr = strchr(ident, '=');
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   200
            const char *val = "";
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   201
            if (ptr)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   202
            {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   203
                *ptr = '\0';
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   204
                val = ptr+1;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   205
            } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   206
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   207
            defs = (MOJOSHADER_preprocessorDefine *) realloc(defs,
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   208
                       (defcount+1) * sizeof (MOJOSHADER_preprocessorDefine));
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   209
            defs[defcount].identifier = ident;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   210
            defs[defcount].definition = val;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   211
            defcount++;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   212
        } // else if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   213
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
        {
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   216
            if (infile != NULL)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   217
            {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   218
                printf("multiple input files specified.\n");
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   219
                exit(1);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   220
            } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   221
            infile = arg;
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   222
        } // else
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   223
    } // for
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   224
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   225
    if (infile == NULL)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   226
    {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   227
        printf("no input file specified.\n");
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   228
        exit(1);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   229
    } // if
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   230
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   231
    FILE *io = fopen(infile, "rb");
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   232
    if (io == NULL)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   233
        printf(" ... fopen('%s') failed.\n", infile);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   234
    else
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   235
    {
662
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   236
        fseek(io, 0, SEEK_END);
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   237
        long fsize = ftell(io);
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   238
        fseek(io, 0, SEEK_SET);
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   239
        if (fsize == -1)
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   240
            fsize = 1000000;
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   241
        char *buf = (char *) malloc(fsize);
50ff6f3eaa4f Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents: 661
diff changeset
   242
        const int rc = fread(buf, 1, fsize, io);
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   243
        fclose(io);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   244
        if (rc == EOF)
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   245
            printf(" ... fread('%s') failed.\n", infile);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   246
        else
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   247
        {
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   248
            if (preprocess(infile, buf, rc, outfile, defs, defcount))
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   249
                retval = 0;
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
            else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
            {
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   252
                if (outfile != NULL)
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
                    remove(outfile);
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
            } // else
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   255
            free(buf);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   256
        } // else
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    } // else
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   259
    for (i = 0; i < defcount; i++)
661
a502c4999238 Fixed logic bug: used wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 660
diff changeset
   260
        free((void *) defs[i].identifier);
659
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   261
    free(defs);
e77acde263cc Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents: 588
diff changeset
   262
660
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   263
    free(include_paths);
d82a22c8ab3d Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents: 659
diff changeset
   264
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    return retval;
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
} // main
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
666
aec40e1c5e99 Fixed comment.
Ryan C. Gordon <icculus@icculus.org>
parents: 665
diff changeset
   268
// end of mojoshader-compiler.c ...
555
940821555fda Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269