blob: 36b9cae49ba09a12c2d356beb48e47930891e933 [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")
27 dst.write(err.output)
28 return False
29
John Stilesd836f842020-09-14 10:21:44 -040030skslc = sys.argv[1]
31inputs = sys.argv[2:]
32
33for input in inputs:
John Stilesea9e7ca2020-09-14 16:46:40 -040034 noExt, ext = os.path.splitext(input)
35 head, tail = os.path.split(noExt)
36 targetDir = os.path.join(head, "golden")
37 if not os.path.isdir(targetDir):
38 os.mkdir(targetDir)
39 target = os.path.join(targetDir, tail)
40 if ext == ".fp":
41 # First, compile the CPP. If we get an error, stop here.
42 if compile(skslc, input, target, ".cpp"):
43 # Next, compile the header.
44 if compile(skslc, input, target, ".h"):
45 # Both files built successfully.
46 continue
47 else:
48 # The header generated an error; this counts as an overall failure for this test.
49 # Remove the passing CPP output since it's not relevant in a failure case.
50 unlinkIfExists(target + ".cpp")
John Stilesd836f842020-09-14 10:21:44 -040051 else:
John Stilesea9e7ca2020-09-14 16:46:40 -040052 # The CPP generated an error. We didn't actually generate a header, but there might be
53 # one from prior runs. Let's remove it for clarity.
54 unlinkIfExists(target + ".h")
John Stilesb0245492020-09-14 17:30:13 -040055 elif ext == ".sksl" or ext == ".vert":
John Stilesea9e7ca2020-09-14 16:46:40 -040056 compile(skslc, input, target, ".glsl")
57 else:
58 print("### Unrecognized file type for " + input + ", skipped")