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") |
| 27 | dst.write(err.output) |
| 28 | return False |
| 29 | |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 30 | skslc = sys.argv[1] |
| 31 | inputs = sys.argv[2:] |
| 32 | |
| 33 | for input in inputs: |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 34 | 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 Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 51 | else: |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 52 | # 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 Stiles | b024549 | 2020-09-14 17:30:13 -0400 | [diff] [blame^] | 55 | elif ext == ".sksl" or ext == ".vert": |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 56 | compile(skslc, input, target, ".glsl") |
| 57 | else: |
| 58 | print("### Unrecognized file type for " + input + ", skipped") |