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 |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 11 | import tempfile |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 12 | |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 13 | skslc = sys.argv[1] |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 14 | lang = sys.argv[2] |
| 15 | settings = sys.argv[3] |
| 16 | inputs = sys.argv[4:] |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 17 | |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 18 | def makeEmptyFile(path): |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 19 | try: |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 20 | open(path, 'wb').close() |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 21 | except OSError: |
| 22 | pass |
| 23 | |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 24 | def extensionForSpirvAsm(ext): |
John Stiles | 92072a3 | 2020-11-23 15:14:35 -0500 | [diff] [blame] | 25 | return ext if (ext == '.frag' or ext == '.vert' or ext == '.geom') else '.frag' |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 26 | |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 27 | if settings != "--settings" and settings != "--nosettings": |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 28 | sys.exit("### Expected --settings or --nosettings, got " + settings) |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 29 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 30 | targets = [] |
John Stiles | 57adba0 | 2020-11-18 13:58:19 -0500 | [diff] [blame] | 31 | worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 32 | |
| 33 | # Convert the list of command-line inputs into a worklist file sfor skslc. |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 34 | for input in inputs: |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 35 | noExt, ext = os.path.splitext(input) |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 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) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 40 | |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 41 | target = os.path.join(targetDir, tail) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 42 | if settings == "--nosettings": |
John Stiles | 371fde5 | 2020-09-21 17:09:52 -0400 | [diff] [blame] | 43 | target += "StandaloneSettings" |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 44 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 45 | targets.append(target) |
| 46 | |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 47 | if lang == "--fp": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 48 | worklist.write(input + "\n") |
| 49 | worklist.write(target + ".cpp\n") |
| 50 | worklist.write(settings + "\n\n") |
| 51 | worklist.write(input + "\n") |
| 52 | worklist.write(target + ".h\n") |
| 53 | worklist.write(settings + "\n\n") |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 54 | elif lang == "--glsl": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 55 | worklist.write(input + "\n") |
| 56 | worklist.write(target + ".glsl\n") |
| 57 | worklist.write(settings + "\n\n") |
John Stiles | aeae3a5 | 2020-09-25 13:35:58 -0400 | [diff] [blame] | 58 | elif lang == "--metal": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 59 | worklist.write(input + "\n") |
| 60 | worklist.write(target + ".metal\n") |
| 61 | worklist.write(settings + "\n\n") |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 62 | elif lang == "--spirv": |
| 63 | worklist.write(input + "\n") |
John Stiles | 92072a3 | 2020-11-23 15:14:35 -0500 | [diff] [blame] | 64 | worklist.write(target + ".asm" + extensionForSpirvAsm(ext) + "\n") |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 65 | worklist.write(settings + "\n\n") |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 66 | else: |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 67 | sys.exit("### Expected one of: --fp --glsl --metal --spirv, got " + lang) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 68 | |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 69 | # Invoke skslc, passing in the worklist. |
| 70 | worklist.close() |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 71 | try: |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 72 | output = subprocess.check_output([skslc, worklist.name], stderr=subprocess.STDOUT) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 73 | except subprocess.CalledProcessError as err: |
John Stiles | a446ca1 | 2020-11-19 18:54:33 -0500 | [diff] [blame] | 74 | if err.returncode != 1: |
| 75 | print("### skslc error:\n") |
| 76 | print("\n".join(err.output.splitlines())) |
| 77 | sys.exit(err.returncode) |
| 78 | pass # Compile errors (exit code 1) are expected and normal in test code |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 79 | |
John Stiles | 57adba0 | 2020-11-18 13:58:19 -0500 | [diff] [blame] | 80 | os.remove(worklist.name) |
| 81 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 82 | # A special case cleanup pass, just for CPP and H files: if either one of these files starts with |
| 83 | # `### Compilation failed`, its sibling should be replaced by an empty file. This improves clarity |
| 84 | # during code review; a failure on either file means that success on the sibling is irrelevant. |
| 85 | if lang == "--fp": |
| 86 | for target in targets: |
| 87 | cppFile = open(target + '.cpp', 'r') |
| 88 | hFile = open(target + '.h', 'r') |
| 89 | if cppFile.readline().startswith("### Compilation failed"): |
| 90 | # The CPP had a compilation failure. Clear the header file. |
| 91 | hFile.close() |
| 92 | makeEmptyFile(target + '.h') |
| 93 | elif hFile.readline().startswith("### Compilation failed"): |
| 94 | # The header had a compilation failure. Clear the CPP file. |
| 95 | cppFile.close() |
| 96 | makeEmptyFile(target + '.cpp') |