Path: blob/21.2-virgl/src/compiler/glsl/glcpp/glcpp.c
4547 views
/*1* Copyright © 2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#include <stdio.h>24#include <string.h>25#include <errno.h>26#include <getopt.h>2728#include "glcpp.h"29#include "main/mtypes.h"30#include "main/shaderobj.h"31#include "util/strtod.h"3233extern int glcpp_parser_debug;3435void36_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,37struct gl_shader *sh)38{39(void) ctx;40*ptr = sh;41}4243/* Read from fp until EOF and return a string of everything read.44*/45static char *46load_text_fp (void *ctx, FILE *fp)47{48#define CHUNK 409649char *text = NULL;50size_t text_size = 0;51size_t total_read = 0;52size_t bytes;5354while (1) {55if (total_read + CHUNK + 1 > text_size) {56text_size = text_size ? text_size * 2 : CHUNK + 1;57text = reralloc_size (ctx, text, text_size);58if (text == NULL) {59fprintf (stderr, "Out of memory\n");60return NULL;61}62}63bytes = fread (text + total_read, 1, CHUNK, fp);64total_read += bytes;6566if (bytes < CHUNK) {67break;68}69}7071text[total_read] = '\0';7273return text;74}7576static char *77load_text_file(void *ctx, const char *filename)78{79char *text;80FILE *fp;8182if (filename == NULL || strcmp (filename, "-") == 0)83return load_text_fp (ctx, stdin);8485fp = fopen (filename, "r");86if (fp == NULL) {87fprintf (stderr, "Failed to open file %s: %s\n",88filename, strerror (errno));89return NULL;90}9192text = load_text_fp (ctx, fp);9394fclose(fp);9596return text;97}9899/* Initialize only those things that glcpp cares about.100*/101static void102init_fake_gl_context (struct gl_context *gl_ctx)103{104memset(gl_ctx, 0, sizeof(*gl_ctx));105gl_ctx->API = API_OPENGL_COMPAT;106gl_ctx->Const.DisableGLSLLineContinuations = false;107}108109static void110usage (void)111{112fprintf (stderr,113"Usage: glcpp [OPTIONS] [--] [<filename>]\n"114"\n"115"Pre-process the given filename (stdin if no filename given).\n"116"The following options are supported:\n"117" --disable-line-continuations Do not interpret lines ending with a\n"118" backslash ('\\') as a line continuation.\n");119}120121enum {122DISABLE_LINE_CONTINUATIONS_OPT = CHAR_MAX + 1123};124125static const struct option126long_options[] = {127{"disable-line-continuations", no_argument, 0, DISABLE_LINE_CONTINUATIONS_OPT },128{"debug", no_argument, 0, 'd'},129{0, 0, 0, 0 }130};131132int133main (int argc, char *argv[])134{135char *filename = NULL;136void *ctx = ralloc(NULL, void*);137char *info_log = ralloc_strdup(ctx, "");138const char *shader;139int ret;140struct gl_context gl_ctx;141int c;142143init_fake_gl_context (&gl_ctx);144145while ((c = getopt_long(argc, argv, "d", long_options, NULL)) != -1) {146switch (c) {147case DISABLE_LINE_CONTINUATIONS_OPT:148gl_ctx.Const.DisableGLSLLineContinuations = true;149break;150case 'd':151glcpp_parser_debug = 1;152break;153default:154usage ();155exit (1);156}157}158159if (optind + 1 < argc) {160printf ("Unexpected argument: %s\n", argv[optind+1]);161usage ();162exit (1);163}164if (optind < argc) {165filename = argv[optind];166}167168shader = load_text_file (ctx, filename);169if (shader == NULL)170return 1;171172_mesa_locale_init();173174ret = glcpp_preprocess(ctx, &shader, &info_log, NULL, NULL, &gl_ctx);175176printf("%s", shader);177fprintf(stderr, "%s", info_log);178179ralloc_free(ctx);180181return ret;182}183184185