John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 1 | #!/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 | |
| 8 | import os |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 12 | def unlinkIfExists(path): |
| 13 | try: |
| 14 | os.unlink(path) |
| 15 | except OSError: |
| 16 | pass |
| 17 | |
| 18 | def 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 Osman | d911c91 | 2020-09-15 10:54:58 -0400 | [diff] [blame^] | 27 | dst.write("\n".join(err.output.splitlines())) |
| 28 | dst.write("\n") |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 29 | return False |
| 30 | |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 31 | skslc = sys.argv[1] |
| 32 | inputs = sys.argv[2:] |
| 33 | |
| 34 | for input in inputs: |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 35 | 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 Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 52 | else: |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 53 | # 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 Stiles | b024549 | 2020-09-14 17:30:13 -0400 | [diff] [blame] | 56 | elif ext == ".sksl" or ext == ".vert": |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 57 | compile(skslc, input, target, ".glsl") |
| 58 | else: |
| 59 | print("### Unrecognized file type for " + input + ", skipped") |