d16dac586819af9ca21bbefc9644af726e224522
[mesa.git] / src / glsl / apps / compile.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33 #include "../cl/sl_cl_parse.h"
34
35
36 int
37 main(int argc,
38 char *argv[])
39 {
40 FILE *in;
41 long size;
42 char *inbuf;
43 struct sl_pp_purify_options options;
44 char errmsg[100] = "";
45 unsigned int errline = 0;
46 struct sl_pp_context *context;
47 struct sl_pp_token_info *tokens;
48 unsigned int version;
49 unsigned int tokens_eaten;
50 struct sl_pp_token_info *outtokens;
51 FILE *out;
52 unsigned int i, j;
53 unsigned char *outbytes;
54 unsigned int cboutbytes;
55 unsigned int shader_type;
56
57 if (argc != 4) {
58 return 1;
59 }
60
61 if (!strcmp(argv[1], "fragment")) {
62 shader_type = 1;
63 } else if (!strcmp(argv[1], "vertex")) {
64 shader_type = 2;
65 } else {
66 return 1;
67 }
68
69 in = fopen(argv[2], "rb");
70 if (!in) {
71 return 1;
72 }
73
74 fseek(in, 0, SEEK_END);
75 size = ftell(in);
76 fseek(in, 0, SEEK_SET);
77
78 out = fopen(argv[3], "w");
79 if (!out) {
80 fclose(in);
81 return 1;
82 }
83
84 inbuf = malloc(size + 1);
85 if (!inbuf) {
86 fprintf(out, "$OOMERROR\n");
87
88 fclose(out);
89 fclose(in);
90 return 1;
91 }
92
93 if (fread(inbuf, 1, size, in) != size) {
94 fprintf(out, "$READERROR\n");
95
96 free(inbuf);
97 fclose(out);
98 fclose(in);
99 return 1;
100 }
101 inbuf[size] = '\0';
102
103 fclose(in);
104
105 memset(&options, 0, sizeof(options));
106
107 context = sl_pp_context_create();
108 if (!context) {
109 fprintf(out, "$CONTEXERROR\n");
110
111 free(inbuf);
112 fclose(out);
113 return 1;
114 }
115
116 if (sl_pp_tokenise(context, inbuf, &options, &tokens)) {
117 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
118
119 sl_pp_context_destroy(context);
120 free(inbuf);
121 fclose(out);
122 return 1;
123 }
124
125 free(inbuf);
126
127 if (sl_pp_version(context, tokens, &version, &tokens_eaten)) {
128 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
129
130 sl_pp_context_destroy(context);
131 free(tokens);
132 fclose(out);
133 return -1;
134 }
135
136 if (sl_pp_process(context, &tokens[tokens_eaten], &outtokens)) {
137 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
138
139 sl_pp_context_destroy(context);
140 free(tokens);
141 fclose(out);
142 return -1;
143 }
144
145 free(tokens);
146
147 for (i = j = 0; outtokens[i].token != SL_PP_EOF; i++) {
148 switch (outtokens[i].token) {
149 case SL_PP_NEWLINE:
150 case SL_PP_EXTENSION_REQUIRE:
151 case SL_PP_EXTENSION_ENABLE:
152 case SL_PP_EXTENSION_WARN:
153 case SL_PP_EXTENSION_DISABLE:
154 case SL_PP_LINE:
155 break;
156 default:
157 outtokens[j++] = outtokens[i];
158 }
159 }
160 outtokens[j] = outtokens[i];
161
162 if (sl_cl_compile(context, outtokens, shader_type, 1, &outbytes, &cboutbytes, errmsg, sizeof(errmsg)) == 0) {
163 unsigned int i;
164 unsigned int line = 0;
165
166 fprintf(out, "\n/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */");
167 fprintf(out, "\n/* %s */", argv[2]);
168 fprintf(out, "\n\n");
169
170 for (i = 0; i < cboutbytes; i++) {
171 unsigned int a;
172
173 if (outbytes[i] < 10) {
174 a = 1;
175 } else if (outbytes[i] < 100) {
176 a = 2;
177 } else {
178 a = 3;
179 }
180 if (i < cboutbytes - 1) {
181 a++;
182 }
183 if (line + a >= 100) {
184 fprintf (out, "\n");
185 line = 0;
186 }
187 line += a;
188 fprintf (out, "%u", outbytes[i]);
189 if (i < cboutbytes - 1) {
190 fprintf (out, ",");
191 }
192 }
193 fprintf (out, "\n");
194 free(outbytes);
195 } else {
196 fprintf(out, "$SYNTAXERROR: `%s'\n", errmsg);
197 return -1;
198 }
199
200 sl_pp_context_destroy(context);
201 free(outtokens);
202 fclose(out);
203
204 return 0;
205 }