2 * kmscon - Generate Shader Files
4 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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 NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * 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.
29 * This takes as arguments shaders and creates a C-source file which
30 * contains these shaders as constants.
37 static char *read_file(const char *path, size_t *size)
43 ffile = fopen(path, "rb");
45 fprintf(stderr, "genshader: cannot open %s: %m\n", path);
49 if (fseek(ffile, 0, SEEK_END) != 0) {
50 fprintf(stderr, "genshader: cannot seek %s: %m\n", path);
56 fprintf(stderr, "genshader: cannot tell %s: %m\n", path);
61 fprintf(stderr, "genshader: empty file %s\n", path);
67 buf = malloc(len + 1);
69 fprintf(stderr, "genshader: memory allocation failed\n");
73 if (len != fread(buf, 1, len, ffile)) {
74 fprintf(stderr, "genshader: cannot read %s: %m\n", path);
85 static const char *get_basename(const char *path)
89 res = strrchr(path, '/');
96 static void write_seq(FILE *out, const char *src, size_t len)
100 for (i = 0; i < len; ++i) {
101 if (src[i] == '\n') {
102 fwrite("\\n\"\n\"", 5, 1, out);
103 } else if (src[i] == '"') {
104 fwrite("\\\"", 2, 1, out);
106 fwrite(&src[i], 1, 1, out);
111 static void write_name(FILE *out, const char *name)
116 for (i = 0; i < len; ++i) {
117 if ((name[i] >= 'A' && name[i] <= 'Z') ||
118 (name[i] >= 'a' && name[i] <= 'z') ||
119 (name[i] >= '0' && name[i] <= '9'))
120 fwrite(&name[i], 1, 1, out);
122 fwrite("_", 1, 1, out);
126 static void write_single_file(FILE *out, const char *path)
128 static const char c1[] = "const char *gl_";
129 static const char c2[] = " = \"";
130 static const char c3[] = "\";\n";
135 name = get_basename(path);
136 content = read_file(path, &len);
138 fwrite(c1, sizeof(c1) - 1, 1, out);
139 write_name(out, name);
140 fwrite(c2, sizeof(c2) - 1, 1, out);
141 write_seq(out, content, len);
142 fwrite(c3, sizeof(c3) - 1, 1, out);
147 int main(int argc, char *argv[])
151 static const char c0[] = "/* This file was generated "
152 "by genshader.c */\n";
155 fprintf(stderr, "genshader: use ./genshader <outputfile> [<shader-files> ...]\n");
159 out = fopen(argv[1], "wb");
161 fprintf(stderr, "genshader: cannot open %s: %m\n", argv[1]);
165 fwrite(c0, sizeof(c0) - 1, 1, out);
166 for (i = 2; i < argc; ++i) {
167 write_single_file(out, argv[i]);