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 | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 24 | if settings != "--settings" and settings != "--nosettings": |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 25 | sys.exit("### Expected --settings or --nosettings, got " + settings) |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 26 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 27 | targets = [] |
John Stiles | 57adba0 | 2020-11-18 13:58:19 -0500 | [diff] [blame] | 28 | worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 29 | |
| 30 | # 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] | 31 | for input in inputs: |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 32 | noExt, _ = os.path.splitext(input) |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 33 | head, tail = os.path.split(noExt) |
| 34 | targetDir = os.path.join(head, "golden") |
| 35 | if not os.path.isdir(targetDir): |
| 36 | os.mkdir(targetDir) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 37 | |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 38 | target = os.path.join(targetDir, tail) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 39 | if settings == "--nosettings": |
John Stiles | 371fde5 | 2020-09-21 17:09:52 -0400 | [diff] [blame] | 40 | target += "StandaloneSettings" |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 41 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 42 | targets.append(target) |
| 43 | |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 44 | if lang == "--fp": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 45 | worklist.write(input + "\n") |
| 46 | worklist.write(target + ".cpp\n") |
| 47 | worklist.write(settings + "\n\n") |
| 48 | worklist.write(input + "\n") |
| 49 | worklist.write(target + ".h\n") |
| 50 | worklist.write(settings + "\n\n") |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 51 | elif lang == "--glsl": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 52 | worklist.write(input + "\n") |
| 53 | worklist.write(target + ".glsl\n") |
| 54 | worklist.write(settings + "\n\n") |
John Stiles | aeae3a5 | 2020-09-25 13:35:58 -0400 | [diff] [blame] | 55 | elif lang == "--metal": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 56 | worklist.write(input + "\n") |
| 57 | worklist.write(target + ".metal\n") |
| 58 | worklist.write(settings + "\n\n") |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 59 | else: |
John Stiles | aeae3a5 | 2020-09-25 13:35:58 -0400 | [diff] [blame] | 60 | sys.exit("### Expected one of: --fp --glsl --metal, got " + lang) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 61 | |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 62 | # Invoke skslc, passing in the worklist. |
| 63 | worklist.close() |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 64 | try: |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 65 | output = subprocess.check_output([skslc, worklist.name], stderr=subprocess.STDOUT) |
| 66 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 67 | except subprocess.CalledProcessError as err: |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 68 | print("### skslc error:\n") |
| 69 | print("\n".join(err.output.splitlines())) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 70 | |
John Stiles | 57adba0 | 2020-11-18 13:58:19 -0500 | [diff] [blame] | 71 | os.remove(worklist.name) |
| 72 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 73 | # A special case cleanup pass, just for CPP and H files: if either one of these files starts with |
| 74 | # `### Compilation failed`, its sibling should be replaced by an empty file. This improves clarity |
| 75 | # during code review; a failure on either file means that success on the sibling is irrelevant. |
| 76 | if lang == "--fp": |
| 77 | for target in targets: |
| 78 | cppFile = open(target + '.cpp', 'r') |
| 79 | hFile = open(target + '.h', 'r') |
| 80 | if cppFile.readline().startswith("### Compilation failed"): |
| 81 | # The CPP had a compilation failure. Clear the header file. |
| 82 | hFile.close() |
| 83 | makeEmptyFile(target + '.h') |
| 84 | elif hFile.readline().startswith("### Compilation failed"): |
| 85 | # The header had a compilation failure. Clear the CPP file. |
| 86 | cppFile.close() |
| 87 | makeEmptyFile(target + '.cpp') |