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 |
John Stiles | b7f5e1b | 2021-01-27 14:05:15 -0500 | [diff] [blame] | 9 | import shlex |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 10 | import subprocess |
| 11 | import sys |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 12 | import tempfile |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 13 | |
John Stiles | b7f5e1b | 2021-01-27 14:05:15 -0500 | [diff] [blame] | 14 | batchCompile = True |
| 15 | |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 16 | skslc = sys.argv[1] |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 17 | lang = sys.argv[2] |
| 18 | settings = sys.argv[3] |
John Stiles | b7f5e1b | 2021-01-27 14:05:15 -0500 | [diff] [blame] | 19 | with open(sys.argv[4], 'r') as reader: |
| 20 | inputs = shlex.split(reader.read()) |
John Stiles | f92f89b | 2021-01-12 14:05:05 -0500 | [diff] [blame] | 21 | |
John Stiles | da57fc0 | 2021-01-22 11:54:30 -0500 | [diff] [blame] | 22 | def pairwise(iterable): |
| 23 | # Iterate over an array pairwise (two elements at a time). |
| 24 | a = iter(iterable) |
| 25 | return zip(a, a) |
| 26 | |
John Stiles | f92f89b | 2021-01-12 14:05:05 -0500 | [diff] [blame] | 27 | def executeWorklist(input, worklist): |
| 28 | # Invoke skslc, passing in the worklist. |
| 29 | worklist.close() |
| 30 | try: |
| 31 | output = subprocess.check_output([skslc, worklist.name], stderr=subprocess.STDOUT) |
| 32 | except subprocess.CalledProcessError as err: |
| 33 | if err.returncode != 1: |
| 34 | print("### " + input + " skslc error:\n") |
| 35 | print("\n".join(err.output.splitlines())) |
| 36 | sys.exit(err.returncode) |
| 37 | pass # Compile errors (exit code 1) are expected and normal in test code |
| 38 | |
| 39 | # Delete the worklist file now that execution is complete. |
| 40 | os.remove(worklist.name) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 41 | |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 42 | def makeEmptyFile(path): |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 43 | try: |
John Stiles | 886b9d4 | 2020-09-15 11:16:10 -0400 | [diff] [blame] | 44 | open(path, 'wb').close() |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 45 | except OSError: |
| 46 | pass |
| 47 | |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 48 | def extensionForSpirvAsm(ext): |
Brian Osman | 99ddd2a | 2021-08-27 11:21:12 -0400 | [diff] [blame] | 49 | return ext if (ext == '.frag' or ext == '.vert') else '.frag' |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 50 | |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 51 | if settings != "--settings" and settings != "--nosettings": |
John Stiles | 7e24871 | 2020-09-24 16:42:09 -0400 | [diff] [blame] | 52 | sys.exit("### Expected --settings or --nosettings, got " + settings) |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 53 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 54 | targets = [] |
Brian Osman | bc35495 | 2021-07-09 15:39:25 -0400 | [diff] [blame] | 55 | worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w') |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 56 | |
John Stiles | da57fc0 | 2021-01-22 11:54:30 -0500 | [diff] [blame] | 57 | # The `inputs` array pairs off input files with their matching output directory, e.g.: |
| 58 | # //skia/tests/sksl/shared/test.sksl |
| 59 | # //skia/tests/sksl/shared/golden/ |
| 60 | # //skia/tests/sksl/intrinsics/abs.sksl |
| 61 | # //skia/tests/sksl/intrinsics/golden/ |
| 62 | # ... (etc) ... |
| 63 | # Here we loop over these inputs and convert them into a worklist file for skslc. |
| 64 | for input, targetDir in pairwise(inputs): |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 65 | noExt, ext = os.path.splitext(input) |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 66 | head, tail = os.path.split(noExt) |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 67 | if not os.path.isdir(targetDir): |
| 68 | os.mkdir(targetDir) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 69 | |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 70 | target = os.path.join(targetDir, tail) |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 71 | if settings == "--nosettings": |
John Stiles | 371fde5 | 2020-09-21 17:09:52 -0400 | [diff] [blame] | 72 | target += "StandaloneSettings" |
John Stiles | 0ed9f31 | 2020-09-16 17:46:37 -0400 | [diff] [blame] | 73 | |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 74 | targets.append(target) |
| 75 | |
Brian Osman | ed5181e | 2021-07-01 13:54:20 -0400 | [diff] [blame] | 76 | if lang == "--glsl": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 77 | worklist.write(input + "\n") |
| 78 | worklist.write(target + ".glsl\n") |
| 79 | worklist.write(settings + "\n\n") |
John Stiles | aeae3a5 | 2020-09-25 13:35:58 -0400 | [diff] [blame] | 80 | elif lang == "--metal": |
Brian Osman | 7b23905 | 2020-11-18 17:04:33 +0000 | [diff] [blame] | 81 | worklist.write(input + "\n") |
| 82 | worklist.write(target + ".metal\n") |
| 83 | worklist.write(settings + "\n\n") |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 84 | elif lang == "--spirv": |
| 85 | worklist.write(input + "\n") |
John Stiles | 92072a3 | 2020-11-23 15:14:35 -0500 | [diff] [blame] | 86 | worklist.write(target + ".asm" + extensionForSpirvAsm(ext) + "\n") |
John Stiles | dda1d31 | 2020-11-20 16:28:50 -0500 | [diff] [blame] | 87 | worklist.write(settings + "\n\n") |
Brian Osman | 977feec | 2020-12-22 11:28:59 -0500 | [diff] [blame] | 88 | elif lang == "--skvm": |
| 89 | worklist.write(input + "\n") |
| 90 | worklist.write(target + ".skvm\n") |
| 91 | worklist.write(settings + "\n\n") |
Brian Osman | 62b039b | 2021-02-08 13:49:53 -0500 | [diff] [blame] | 92 | elif lang == "--stage": |
| 93 | worklist.write(input + "\n") |
| 94 | worklist.write(target + ".stage\n") |
| 95 | worklist.write(settings + "\n\n") |
John Stiles | ea9e7ca | 2020-09-14 16:46:40 -0400 | [diff] [blame] | 96 | else: |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 97 | sys.exit("### Expected one of: --glsl --metal --spirv --skvm --stage --dsl, got " + lang) |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 98 | |
John Stiles | f92f89b | 2021-01-12 14:05:05 -0500 | [diff] [blame] | 99 | # Compile items one at a time. |
| 100 | if not batchCompile: |
| 101 | executeWorklist(input, worklist) |
Brian Osman | bc35495 | 2021-07-09 15:39:25 -0400 | [diff] [blame] | 102 | worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w') |
John Stiles | a1e8fe3 | 2020-11-11 17:29:28 -0500 | [diff] [blame] | 103 | |
John Stiles | f92f89b | 2021-01-12 14:05:05 -0500 | [diff] [blame] | 104 | # Compile everything all in one go. |
| 105 | if batchCompile: |
| 106 | executeWorklist("", worklist) |
| 107 | else: |
| 108 | worklist.close() |
| 109 | os.remove(worklist.name) |