author | Ryan C. Gordon <icculus@icculus.org> |
Thu, 19 Feb 2009 03:36:49 -0500 | |
changeset 668 | 983f396a3d71 |
parent 667 | 8efc63f4ab9b |
child 669 | fd14a7278a95 |
permissions | -rw-r--r-- |
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 |
|
668 | 42 |
|
43 |
static void fail(const char *err) |
|
44 |
{ |
|
45 |
printf("%s.\n", err); |
|
46 |
exit(1); |
|
47 |
} // fail |
|
48 |
||
49 |
||
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
55 |
int i; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
56 |
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
|
57 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
return 0; |
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 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
continue; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
69 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
70 |
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
|
71 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
75 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
76 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
77 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
78 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
79 |
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
|
80 |
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
|
81 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
82 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
83 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
84 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
85 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
86 |
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
|
87 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
88 |
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
|
89 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
90 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
91 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
92 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
93 |
fclose(io); |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
94 |
*outdata = data; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
95 |
*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
|
96 |
return 1; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
97 |
} // if |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
98 |
} // for |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
99 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
100 |
return 0; |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
101 |
} // open_include |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
102 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
103 |
|
663 | 104 |
static void close_include(const char *data, MOJOSHADER_malloc m, |
105 |
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
|
106 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
107 |
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
|
108 |
} // close_include |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
109 |
|
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
110 |
|
578
6c8f73c845e7
Allow app to specify a base filename for error messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
577
diff
changeset
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
unsigned int defcount) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
115 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
116 |
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
|
117 |
if (io == NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
118 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
119 |
printf(" ... fopen('%s') failed.\n", outfile); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
120 |
return 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
121 |
} // if |
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 |
const MOJOSHADER_preprocessData *pd; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
124 |
int retval = 0; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
125 |
|
665
521d24646891
Added allocator debug output.
Ryan C. Gordon <icculus@icculus.org>
parents:
664
diff
changeset
|
126 |
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
|
127 |
close_include, Malloc, Free, NULL); |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
128 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
129 |
if (pd->error_count > 0) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
130 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
131 |
int i; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
132 |
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
|
133 |
{ |
577
bec531dd448e
Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
556
diff
changeset
|
134 |
printf("%s:%d: ERROR: %s\n", |
bec531dd448e
Report filenames in error messages.
Ryan C. Gordon <icculus@icculus.org>
parents:
556
diff
changeset
|
135 |
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
|
136 |
pd->errors[i].error_position, |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
137 |
pd->errors[i].error); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
138 |
} // for |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
139 |
} // if |
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 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
142 |
if (pd->output != NULL) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
143 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
144 |
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
|
145 |
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
|
146 |
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
|
147 |
printf(" ... fclose('%s') failed.\n", outfile); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
148 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
149 |
retval = 1; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
150 |
} // if |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
151 |
} // else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
152 |
MOJOSHADER_freePreprocessData(pd); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
153 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
154 |
return retval; |
556
04282775cc2c
Fixed cut-and-pasted comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
555
diff
changeset
|
155 |
} // preprocess |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
156 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
157 |
|
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
158 |
int main(int argc, char **argv) |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
159 |
{ |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
int i; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
164 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
165 |
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
|
166 |
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
|
167 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
168 |
for (i = 1; i < argc; i++) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
169 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
170 |
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
|
171 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
172 |
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
|
173 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
174 |
if (outfile != NULL) |
668 | 175 |
fail("multiple output files specified"); |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
176 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
177 |
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
|
178 |
if (arg == NULL) |
668 | 179 |
fail("no filename after '-o'"); |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
180 |
outfile = arg; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
181 |
} // if |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
182 |
|
667
8efc63f4ab9b
That should be an "else if", not an "if" ...
Ryan C. Gordon <icculus@icculus.org>
parents:
666
diff
changeset
|
183 |
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
|
184 |
{ |
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
185 |
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
|
186 |
if (arg == NULL) |
668 | 187 |
fail("no path after '-I'"); |
188 |
||
660
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) |
668 | 217 |
fail("multiple input files specified."); |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
218 |
infile = arg; |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
219 |
} // else |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
220 |
} // for |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
221 |
|
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
222 |
if (infile == NULL) |
668 | 223 |
fail("no input file specified."); |
659
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 |
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
|
226 |
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
|
227 |
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
|
228 |
else |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
229 |
{ |
662
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
230 |
fseek(io, 0, SEEK_END); |
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
231 |
long fsize = ftell(io); |
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
232 |
fseek(io, 0, SEEK_SET); |
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
233 |
if (fsize == -1) |
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
234 |
fsize = 1000000; |
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
235 |
char *buf = (char *) malloc(fsize); |
50ff6f3eaa4f
Allocate just enough to read the input file.
Ryan C. Gordon <icculus@icculus.org>
parents:
661
diff
changeset
|
236 |
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
|
237 |
fclose(io); |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
else |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
241 |
{ |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
242 |
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
|
243 |
retval = 0; |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
244 |
else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
245 |
{ |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
246 |
if (outfile != NULL) |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
247 |
remove(outfile); |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
248 |
} // else |
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
249 |
free(buf); |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
250 |
} // else |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
251 |
} // else |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
252 |
|
659
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
253 |
for (i = 0; i < defcount; i++) |
661
a502c4999238
Fixed logic bug: used wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents:
660
diff
changeset
|
254 |
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
|
255 |
free(defs); |
e77acde263cc
Added command line stuff to preprocess.c, including predefined macro support.
Ryan C. Gordon <icculus@icculus.org>
parents:
588
diff
changeset
|
256 |
|
660
d82a22c8ab3d
Allow preprocess.c to specify include paths on the command line.
Ryan C. Gordon <icculus@icculus.org>
parents:
659
diff
changeset
|
257 |
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
|
258 |
|
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
259 |
return retval; |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
260 |
} // main |
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
261 |
|
666 | 262 |
// end of mojoshader-compiler.c ... |
555
940821555fda
Initial work on preprocessor. Not yet complete!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
263 |