1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2008, 2009 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdlib.h>
25bf215546Sopenharmony_ci#include <stdio.h>
26bf215546Sopenharmony_ci#include <getopt.h>
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/** @file main.cpp
29bf215546Sopenharmony_ci *
30bf215546Sopenharmony_ci * This file is the main() routine and scaffolding for producing
31bf215546Sopenharmony_ci * builtin_compiler (which doesn't include builtins itself and is used
32bf215546Sopenharmony_ci * to generate the profile information for builtin_function.cpp), and
33bf215546Sopenharmony_ci * for glsl_compiler (which does include builtins and can be used to
34bf215546Sopenharmony_ci * offline compile GLSL code and examine the resulting GLSL IR.
35bf215546Sopenharmony_ci */
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "main/mtypes.h"
38bf215546Sopenharmony_ci#include "standalone.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic struct standalone_options options;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ciconst struct option compiler_opts[] = {
43bf215546Sopenharmony_ci   { "dump-ast", no_argument, &options.dump_ast, 1 },
44bf215546Sopenharmony_ci   { "dump-hir", no_argument, &options.dump_hir, 1 },
45bf215546Sopenharmony_ci   { "dump-lir", no_argument, &options.dump_lir, 1 },
46bf215546Sopenharmony_ci   { "dump-builder", no_argument, &options.dump_builder, 1 },
47bf215546Sopenharmony_ci   { "link",     no_argument, &options.do_link,  1 },
48bf215546Sopenharmony_ci   { "just-log", no_argument, &options.just_log, 1 },
49bf215546Sopenharmony_ci   { "lower-precision", no_argument, &options.lower_precision, 1 },
50bf215546Sopenharmony_ci   { "version",  required_argument, NULL, 'v' },
51bf215546Sopenharmony_ci   { NULL, 0, NULL, 0 }
52bf215546Sopenharmony_ci};
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci/**
55bf215546Sopenharmony_ci * \brief Print proper usage and exit with failure.
56bf215546Sopenharmony_ci */
57bf215546Sopenharmony_cistatic void
58bf215546Sopenharmony_ciusage_fail(const char *name)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   const char *header =
62bf215546Sopenharmony_ci      "usage: %s [options] <file.vert | file.tesc | file.tese | file.geom | file.frag | file.comp>\n"
63bf215546Sopenharmony_ci      "\n"
64bf215546Sopenharmony_ci      "Possible options are:\n";
65bf215546Sopenharmony_ci   printf(header, name);
66bf215546Sopenharmony_ci   for (const struct option *o = compiler_opts; o->name != 0; ++o) {
67bf215546Sopenharmony_ci      printf("    --%s", o->name);
68bf215546Sopenharmony_ci      if (o->has_arg == required_argument)
69bf215546Sopenharmony_ci         printf(" (mandatory)");
70bf215546Sopenharmony_ci      printf("\n");
71bf215546Sopenharmony_ci   }
72bf215546Sopenharmony_ci   exit(EXIT_FAILURE);
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ciint
76bf215546Sopenharmony_cimain(int argc, char * const* argv)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci   int status = EXIT_SUCCESS;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   int c;
81bf215546Sopenharmony_ci   int idx = 0;
82bf215546Sopenharmony_ci   while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) {
83bf215546Sopenharmony_ci      switch (c) {
84bf215546Sopenharmony_ci      case 'v':
85bf215546Sopenharmony_ci         options.glsl_version = strtol(optarg, NULL, 10);
86bf215546Sopenharmony_ci         break;
87bf215546Sopenharmony_ci      default:
88bf215546Sopenharmony_ci         break;
89bf215546Sopenharmony_ci      }
90bf215546Sopenharmony_ci   }
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   if (argc <= optind)
93bf215546Sopenharmony_ci      usage_fail(argv[0]);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   struct gl_shader_program *whole_program;
96bf215546Sopenharmony_ci   static struct gl_context local_ctx;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   whole_program = standalone_compile_shader(&options, argc - optind,
99bf215546Sopenharmony_ci                                             &argv[optind], &local_ctx);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   if (!whole_program)
102bf215546Sopenharmony_ci      usage_fail(argv[0]);
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   standalone_compiler_cleanup(whole_program);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   return status;
107bf215546Sopenharmony_ci}
108