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 | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 12 | skslc = sys.argv[1] |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 13 | lang = sys.argv[2] |
| 14 | settings = sys.argv[3] |
| 15 | inputs = sys.argv[4:] |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 16 | |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 17 | def makeEmptyFile(path): |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 18 | try: |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 19 | open(path, 'wb').close() |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 20 | except OSError: |
| 21 | pass |
| 22 | |
| 23 | def compile(skslc, input, target, extension): |
| 24 | target += extension |
| 25 | try: |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 26 | subprocess.check_output([skslc, input, target, settings], stderr=subprocess.STDOUT) |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 27 | return True |
| 28 | |
| 29 | except subprocess.CalledProcessError as err: |
| 30 | with open(target, 'wb') as dst: |
| 31 | dst.write("### Compilation failed:\n\n") |
Brian Osman | d911c91 | 2020-09-15 10:54:58 -0400 | [diff] [blame] | 32 | dst.write("\n".join(err.output.splitlines())) |
| 33 | dst.write("\n") |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 34 | return False |
| 35 | |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 36 | if settings != "--settings" and settings != "--nosettings": |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 37 | sys.exit("### Expected --settings or --nosettings, got " + settings) |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 38 | |
| 39 | for input in inputs: |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 40 | noExt, ext = os.path.splitext(input) |
| 41 | head, tail = os.path.split(noExt) |
| 42 | targetDir = os.path.join(head, "golden") |
| 43 | if not os.path.isdir(targetDir): |
| 44 | os.mkdir(targetDir) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 45 | |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 46 | target = os.path.join(targetDir, tail) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 47 | if settings == "--nosettings": |
John Stiles | 371fde5 | 2020-09-21 17:09:52 -0400 | [diff] [blame] | 48 | target += "StandaloneSettings" |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 49 | |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 50 | if lang == "--fp": |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 51 | # First, compile the CPP. If we get an error, stop here. |
| 52 | if compile(skslc, input, target, ".cpp"): |
| 53 | # Next, compile the header. |
| 54 | if compile(skslc, input, target, ".h"): |
| 55 | # Both files built successfully. |
| 56 | continue |
| 57 | else: |
| 58 | # The header generated an error; this counts as an overall failure for this test. |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 59 | # Blank out the passing CPP output since it's not relevant in a failure case. |
| 60 | makeEmptyFile(target + ".cpp") |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 61 | else: |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 62 | # The CPP generated an error. We didn't actually generate a header at all, but Ninja |
| 63 | # expects an output file to exist or it won't reach steady-state. |
| 64 | makeEmptyFile(target + ".h") |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 65 | elif lang == "--glsl": |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 66 | compile(skslc, input, target, ".glsl") |
John Stiles | aeae3a5 | 2020-09-25 13:35:58 -0400 | [diff] [blame] | 67 | elif lang == "--metal": |
| 68 | compile(skslc, input, target, ".metal") |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 69 | else: |
John Stiles | aeae3a5 | 2020-09-25 13:35:58 -0400 | [diff] [blame] | 70 | sys.exit("### Expected one of: --fp --glsl --metal, got " + lang) |