blob: e6307572d8c4d4cb8916c78f2b8dbb379492c5e6 [file] [log] [blame]
John Stilesd836f842020-09-14 10:21:44 -04001#!/usr/bin/env python
2#
3# Copyright 2020 Google LLC
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8import os
9import subprocess
10import sys
11
John Stilesea9e7ca2020-09-14 16:46:40 -040012def unlinkIfExists(path):
13 try:
14 os.unlink(path)
15 except OSError:
16 pass
17
18def compile(skslc, input, target, extension):
19 target += extension
20 try:
21 subprocess.check_output([skslc, input, target], stderr=subprocess.STDOUT)
22 return True
23
24 except subprocess.CalledProcessError as err:
25 with open(target, 'wb') as dst:
26 dst.write("### Compilation failed:\n\n")
Brian Osmand911c912020-09-15 10:54:58 -040027 dst.write("\n".join(err.output.splitlines()))
28 dst.write("\n")
John Stilesea9e7ca2020-09-14 16:46:40 -040029 return False
30
John Stilesd836f842020-09-14 10:21:44 -040031skslc = sys.argv[1]
32inputs = sys.argv[2:]
33
34for input in inputs:
John Stilesea9e7ca2020-09-14 16:46:40 -040035 noExt, ext = os.path.splitext(input)
36 head, tail = os.path.split(noExt)
37 targetDir = os.path.join(head, "golden")
38 if not os.path.isdir(targetDir):
39 os.mkdir(targetDir)
40 target = os.path.join(targetDir, tail)
41 if ext == ".fp":
42 # First, compile the CPP. If we get an error, stop here.
43 if compile(skslc, input, target, ".cpp"):
44 # Next, compile the header.
45 if compile(skslc, input, target, ".h"):
46 # Both files built successfully.
47 continue
48 else:
49 # The header generated an error; this counts as an overall failure for this test.
50 # Remove the passing CPP output since it's not relevant in a failure case.
51 unlinkIfExists(target + ".cpp")
John Stilesd836f842020-09-14 10:21:44 -040052 else:
John Stilesea9e7ca2020-09-14 16:46:40 -040053 # The CPP generated an error. We didn't actually generate a header, but there might be
54 # one from prior runs. Let's remove it for clarity.
55 unlinkIfExists(target + ".h")
John Stilesb0245492020-09-14 17:30:13 -040056 elif ext == ".sksl" or ext == ".vert":
John Stilesea9e7ca2020-09-14 16:46:40 -040057 compile(skslc, input, target, ".glsl")
58 else:
59 print("### Unrecognized file type for " + input + ", skipped")