blob: eb07b4d0323e584df810e60e20667a64b3ceb512 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "stdio.h"
9#include <fstream>
10#include "SkSLCompiler.h"
11
ethannicholas5961bc92016-10-12 06:39:56 -070012bool endsWith(const std::string& s, const std::string& ending) {
13 if (s.length() >= ending.length()) {
14 return (0 == s.compare(s.length() - ending.length(), ending.length(), ending));
15 }
16 return false;
17}
18
ethannicholasb3058bd2016-07-01 08:22:01 -070019/**
20 * Very simple standalone executable to facilitate testing.
21 */
22int main(int argc, const char** argv) {
23 if (argc != 3) {
24 printf("usage: skslc <input> <output>\n");
25 exit(1);
26 }
27 SkSL::Program::Kind kind;
28 size_t len = strlen(argv[1]);
29 if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".vert")) {
30 kind = SkSL::Program::kVertex_Kind;
31 } else if (len > 5 && !strcmp(argv[1] + strlen(argv[1]) - 5, ".frag")) {
32 kind = SkSL::Program::kFragment_Kind;
33 } else {
34 printf("input filename must end in '.vert' or '.frag'\n");
35 exit(1);
36 }
37
38 std::ifstream in(argv[1]);
39 std::string text((std::istreambuf_iterator<char>(in)),
40 std::istreambuf_iterator<char>());
41 if (in.rdstate()) {
42 printf("error reading '%s'\n", argv[1]);
43 exit(2);
44 }
ethannicholas5961bc92016-10-12 06:39:56 -070045 std::string name(argv[2]);
46 if (endsWith(name, ".spirv")) {
47 std::ofstream out(argv[2], std::ofstream::binary);
48 SkSL::Compiler compiler;
49 if (!compiler.toSPIRV(kind, text, out)) {
50 printf("%s", compiler.errorText().c_str());
51 exit(3);
52 }
53 if (out.rdstate()) {
54 printf("error writing '%s'\n", argv[2]);
55 exit(4);
56 }
57 } else if (endsWith(name, ".glsl")) {
58 std::ofstream out(argv[2], std::ofstream::binary);
59 SkSL::Compiler compiler;
ethannicholasddb37d62016-10-20 09:54:00 -070060 if (!compiler.toGLSL(kind, text, SkSL::GLCaps(), out)) {
ethannicholas5961bc92016-10-12 06:39:56 -070061 printf("%s", compiler.errorText().c_str());
62 exit(3);
63 }
64 if (out.rdstate()) {
65 printf("error writing '%s'\n", argv[2]);
66 exit(4);
67 }
68 } else {
69 printf("expected output filename to end with '.spirv' or '.glsl'");
ethannicholasb3058bd2016-07-01 08:22:01 -070070 }
71}