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