101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2008, 2009 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include <stdlib.h>
2501e04c3fSmrg#include <stdio.h>
2601e04c3fSmrg#include <getopt.h>
2701e04c3fSmrg
2801e04c3fSmrg/** @file main.cpp
2901e04c3fSmrg *
3001e04c3fSmrg * This file is the main() routine and scaffolding for producing
3101e04c3fSmrg * builtin_compiler (which doesn't include builtins itself and is used
3201e04c3fSmrg * to generate the profile information for builtin_function.cpp), and
3301e04c3fSmrg * for glsl_compiler (which does include builtins and can be used to
3401e04c3fSmrg * offline compile GLSL code and examine the resulting GLSL IR.
3501e04c3fSmrg */
3601e04c3fSmrg
377e102996Smaya#include "main/mtypes.h"
3801e04c3fSmrg#include "standalone.h"
3901e04c3fSmrg
4001e04c3fSmrgstatic struct standalone_options options;
4101e04c3fSmrg
4201e04c3fSmrgconst struct option compiler_opts[] = {
4301e04c3fSmrg   { "dump-ast", no_argument, &options.dump_ast, 1 },
4401e04c3fSmrg   { "dump-hir", no_argument, &options.dump_hir, 1 },
4501e04c3fSmrg   { "dump-lir", no_argument, &options.dump_lir, 1 },
4601e04c3fSmrg   { "dump-builder", no_argument, &options.dump_builder, 1 },
4701e04c3fSmrg   { "link",     no_argument, &options.do_link,  1 },
4801e04c3fSmrg   { "just-log", no_argument, &options.just_log, 1 },
497ec681f3Smrg   { "lower-precision", no_argument, &options.lower_precision, 1 },
5001e04c3fSmrg   { "version",  required_argument, NULL, 'v' },
5101e04c3fSmrg   { NULL, 0, NULL, 0 }
5201e04c3fSmrg};
5301e04c3fSmrg
5401e04c3fSmrg/**
5501e04c3fSmrg * \brief Print proper usage and exit with failure.
5601e04c3fSmrg */
5701e04c3fSmrgstatic void
5801e04c3fSmrgusage_fail(const char *name)
5901e04c3fSmrg{
6001e04c3fSmrg
6101e04c3fSmrg   const char *header =
6201e04c3fSmrg      "usage: %s [options] <file.vert | file.tesc | file.tese | file.geom | file.frag | file.comp>\n"
6301e04c3fSmrg      "\n"
6401e04c3fSmrg      "Possible options are:\n";
6501e04c3fSmrg   printf(header, name);
6601e04c3fSmrg   for (const struct option *o = compiler_opts; o->name != 0; ++o) {
6701e04c3fSmrg      printf("    --%s", o->name);
6801e04c3fSmrg      if (o->has_arg == required_argument)
6901e04c3fSmrg         printf(" (mandatory)");
7001e04c3fSmrg      printf("\n");
7101e04c3fSmrg   }
7201e04c3fSmrg   exit(EXIT_FAILURE);
7301e04c3fSmrg}
7401e04c3fSmrg
7501e04c3fSmrgint
7601e04c3fSmrgmain(int argc, char * const* argv)
7701e04c3fSmrg{
7801e04c3fSmrg   int status = EXIT_SUCCESS;
7901e04c3fSmrg
8001e04c3fSmrg   int c;
8101e04c3fSmrg   int idx = 0;
8201e04c3fSmrg   while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) {
8301e04c3fSmrg      switch (c) {
8401e04c3fSmrg      case 'v':
8501e04c3fSmrg         options.glsl_version = strtol(optarg, NULL, 10);
8601e04c3fSmrg         break;
8701e04c3fSmrg      default:
8801e04c3fSmrg         break;
8901e04c3fSmrg      }
9001e04c3fSmrg   }
9101e04c3fSmrg
9201e04c3fSmrg   if (argc <= optind)
9301e04c3fSmrg      usage_fail(argv[0]);
9401e04c3fSmrg
9501e04c3fSmrg   struct gl_shader_program *whole_program;
967e102996Smaya   static struct gl_context local_ctx;
9701e04c3fSmrg
987e102996Smaya   whole_program = standalone_compile_shader(&options, argc - optind,
997e102996Smaya                                             &argv[optind], &local_ctx);
10001e04c3fSmrg
10101e04c3fSmrg   if (!whole_program)
10201e04c3fSmrg      usage_fail(argv[0]);
10301e04c3fSmrg
10401e04c3fSmrg   standalone_compiler_cleanup(whole_program);
10501e04c3fSmrg
10601e04c3fSmrg   return status;
10701e04c3fSmrg}
108