| 1 |
/* |
| 2 |
* Copyright (C) 2018 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "GlslMinify.h" |
| 18 |
|
| 19 |
#include <utils/Path.h> |
| 20 |
|
| 21 |
#include <getopt/getopt.h> |
| 22 |
|
| 23 |
#include <fstream> |
| 24 |
#include <iostream> |
| 25 |
#include <string> |
| 26 |
|
| 27 |
using namespace std; |
| 28 |
using namespace utils; |
| 29 |
using namespace glslminifier; |
| 30 |
|
| 31 |
static bool g_writeToStdOut = true; |
| 32 |
static const char* g_outputFile = ""; |
| 33 |
static const char* g_inputFile = ""; |
| 34 |
GlslMinifyOptions g_optimizationLevel = GlslMinifyOptions::ALL; |
| 35 |
static bool g_outputLineDirectives = false; |
| 36 |
static std::string g_lineDirectiveName; |
| 37 |
|
| 38 |
static const char* USAGE = R"TXT( |
| 39 |
GLSLMINIFIER minifies GLSL shader code by removing comments, blank lines and indentation. |
| 40 |
|
| 41 |
Usage: |
| 42 |
GLSLMINIFIER [options] <input file> |
| 43 |
|
| 44 |
Options: |
| 45 |
--help, -h |
| 46 |
Print this message. |
| 47 |
--license, -L |
| 48 |
Print copyright and license information. |
| 49 |
--output, -o |
| 50 |
Specify path to output file. If none provided, writes to stdout. |
| 51 |
--optimization, -O [none] |
| 52 |
Set the level of optimization. "none" performs a simple passthrough. |
| 53 |
--line, -l [name] |
| 54 |
Insert a #line directive on the first line of the shader with the given name. |
| 55 |
For example, --line foobar.h will insert the following: |
| 56 |
#if defined(GL_GOOGLE_cpp_style_line_directive) |
| 57 |
#line 0 "foobar.h" |
| 58 |
#endif |
| 59 |
This option is meant to be used with -Onone optimization. |
| 60 |
|
| 61 |
Example: |
| 62 |
GLSLMINIFIER -o output.fs.min input.fs |
| 63 |
> Output file: output.fs.min |
| 64 |
)TXT"; |
| 65 |
|
| 66 |
static void printUsage(const char* name) { |
| 67 |
std::string execName(Path(name).getName()); |
| 68 |
const std::string from("GLSLMINIFIER"); |
| 69 |
std::string usage(USAGE); |
| 70 |
for (size_t pos = usage.find(from); pos != std::string::npos; pos = usage.find(from, pos)) { |
| 71 |
usage.replace(pos, from.length(), execName); |
| 72 |
} |
| 73 |
puts(usage.c_str()); |
| 74 |
} |
| 75 |
|
| 76 |
static void license() { |
| 77 |
static const char *license[] = { |
| 78 |
#include "licenses/licenses.inc" |
| 79 |
nullptr |
| 80 |
}; |
| 81 |
|
| 82 |
const char **p = &license[0]; |
| 83 |
while (*p) |
| 84 |
std::cout << *p++ << std::endl; |
| 85 |
} |
| 86 |
|
| 87 |
static int handleArguments(int argc, char* argv[]) { |
| 88 |
static constexpr const char* OPTSTR = "hLo:O:l:"; |
| 89 |
static const struct option OPTIONS[] = { |
| 90 |
{ "help", no_argument, nullptr, 'h' }, |
| 91 |
{ "license", no_argument, nullptr, 'L' }, |
| 92 |
{ "output", required_argument, nullptr, 'o' }, |
| 93 |
{ "optimization", required_argument, nullptr, 'O' }, |
| 94 |
{ "line", required_argument, nullptr, 'l' }, |
| 95 |
{ nullptr, 0, nullptr, 0 } // termination of the option list |
| 96 |
}; |
| 97 |
|
| 98 |
int opt; |
| 99 |
int optionIndex = 0; |
| 100 |
|
| 101 |
while ((opt = getopt_long(argc, argv, OPTSTR, OPTIONS, &optionIndex)) >= 0) { |
| 102 |
std::string arg(optarg ? optarg : ""); |
| 103 |
switch (opt) { |
| 104 |
default: |
| 105 |
case 'h': |
| 106 |
printUsage(argv[0]); |
| 107 |
exit(0); |
| 108 |
case 'L': |
| 109 |
license(); |
| 110 |
exit(0); |
| 111 |
case 'o': |
| 112 |
g_outputFile = optarg; |
| 113 |
g_writeToStdOut = false; |
| 114 |
break; |
| 115 |
case 'O': |
| 116 |
if (arg == "none") { |
| 117 |
g_optimizationLevel = GlslMinifyOptions::NONE; |
| 118 |
} else { |
| 119 |
std::cerr << "Warning: unknown optimization level." << std::endl; |
| 120 |
} |
| 121 |
break; |
| 122 |
case 'l': |
| 123 |
g_outputLineDirectives = true; |
| 124 |
g_lineDirectiveName = arg; |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return optind; |
| 130 |
} |
| 131 |
|
| 132 |
int main(int argc, char* argv[]) { |
| 133 |
const int optionIndex = handleArguments(argc, argv); |
| 134 |
const int numArgs = argc - optionIndex; |
| 135 |
if (numArgs < 1) { |
| 136 |
printUsage(argv[0]); |
| 137 |
return 1; |
| 138 |
} |
| 139 |
if (numArgs > 1) { |
| 140 |
cerr << "Only one input file should be specified on the command line." << endl; |
| 141 |
return 1; |
| 142 |
} |
| 143 |
g_inputFile = argv[optionIndex]; |
| 144 |
|
| 145 |
// Read the contents of the input file. |
| 146 |
ifstream inStream(g_inputFile, ios::binary); |
| 147 |
if (!inStream) { |
| 148 |
cerr << "Unable to read " << g_inputFile << endl; |
| 149 |
exit(1); |
| 150 |
} |
| 151 |
string inputStr((istreambuf_iterator<char>(inStream)), istreambuf_iterator<char>()); |
| 152 |
|
| 153 |
// Minify the GLSL. |
| 154 |
string result = minifyGlsl(inputStr, g_optimizationLevel); |
| 155 |
|
| 156 |
// Add a #line directive at the beginning of the file, if requested. |
| 157 |
if (g_outputLineDirectives) { |
| 158 |
// We must check for support for the line directive, otherwise drivers may complain if they |
| 159 |
// don't support it. |
| 160 |
std::string lineDirective = |
| 161 |
std::string("#if defined(GL_GOOGLE_cpp_style_line_directive)\n") + |
| 162 |
"#line 0 \"" + g_lineDirectiveName + "\"\n" + |
| 163 |
"#endif\n"; |
| 164 |
result.insert(0, lineDirective); |
| 165 |
} |
| 166 |
|
| 167 |
if (g_writeToStdOut) { |
| 168 |
cout << result; |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
// Create the output path if it doesn't exist already. |
| 173 |
const Path outputDir = Path(g_outputFile).getParent(); |
| 174 |
if (!outputDir.exists()) { |
| 175 |
outputDir.mkdirRecursive(); |
| 176 |
} |
| 177 |
|
| 178 |
// Open the output file for writing. |
| 179 |
ofstream outStream(g_outputFile, ios::binary); |
| 180 |
if (!outStream) { |
| 181 |
cerr << "Unable to open " << g_outputFile << endl; |
| 182 |
exit(1); |
| 183 |
} |
| 184 |
outStream << result; |
| 185 |
return 0; |
| 186 |
} |