blob: fbb83662602aa5f0d1ab7f623cb02ff827e4d648 [file] [log] [blame]
John Stilesd836f842020-09-14 10:21:44 -04001#!/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
8import os
John Stilesb7f5e1b2021-01-27 14:05:15 -05009import shlex
John Stilesd836f842020-09-14 10:21:44 -040010import subprocess
11import sys
Brian Osman7b239052020-11-18 17:04:33 +000012import tempfile
John Stilesd836f842020-09-14 10:21:44 -040013
John Stilesb7f5e1b2021-01-27 14:05:15 -050014batchCompile = True
15
John Stiles0ed9f312020-09-16 17:46:37 -040016skslc = sys.argv[1]
John Stiles7e248712020-09-24 16:42:09 -040017lang = sys.argv[2]
18settings = sys.argv[3]
John Stilesb7f5e1b2021-01-27 14:05:15 -050019with open(sys.argv[4], 'r') as reader:
20 inputs = shlex.split(reader.read())
John Stilesf92f89b2021-01-12 14:05:05 -050021
John Stilesda57fc02021-01-22 11:54:30 -050022def pairwise(iterable):
23 # Iterate over an array pairwise (two elements at a time).
24 a = iter(iterable)
25 return zip(a, a)
26
John Stilesf92f89b2021-01-12 14:05:05 -050027def 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 Stiles0ed9f312020-09-16 17:46:37 -040041
John Stiles886b9d42020-09-15 11:16:10 -040042def makeEmptyFile(path):
John Stilesea9e7ca2020-09-14 16:46:40 -040043 try:
John Stiles886b9d42020-09-15 11:16:10 -040044 open(path, 'wb').close()
John Stilesea9e7ca2020-09-14 16:46:40 -040045 except OSError:
46 pass
47
John Stilesdda1d312020-11-20 16:28:50 -050048def extensionForSpirvAsm(ext):
Brian Osman99ddd2a2021-08-27 11:21:12 -040049 return ext if (ext == '.frag' or ext == '.vert') else '.frag'
John Stilesdda1d312020-11-20 16:28:50 -050050
John Stiles0ed9f312020-09-16 17:46:37 -040051if settings != "--settings" and settings != "--nosettings":
John Stiles7e248712020-09-24 16:42:09 -040052 sys.exit("### Expected --settings or --nosettings, got " + settings)
John Stilesd836f842020-09-14 10:21:44 -040053
John Stilesa1e8fe32020-11-11 17:29:28 -050054targets = []
Brian Osmanbc354952021-07-09 15:39:25 -040055worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w')
John Stilesa1e8fe32020-11-11 17:29:28 -050056
John Stilesda57fc02021-01-22 11:54:30 -050057# 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.
64for input, targetDir in pairwise(inputs):
John Stilesdda1d312020-11-20 16:28:50 -050065 noExt, ext = os.path.splitext(input)
John Stilesea9e7ca2020-09-14 16:46:40 -040066 head, tail = os.path.split(noExt)
John Stilesea9e7ca2020-09-14 16:46:40 -040067 if not os.path.isdir(targetDir):
68 os.mkdir(targetDir)
John Stiles0ed9f312020-09-16 17:46:37 -040069
John Stilesea9e7ca2020-09-14 16:46:40 -040070 target = os.path.join(targetDir, tail)
John Stiles0ed9f312020-09-16 17:46:37 -040071 if settings == "--nosettings":
John Stiles371fde52020-09-21 17:09:52 -040072 target += "StandaloneSettings"
John Stiles0ed9f312020-09-16 17:46:37 -040073
John Stilesa1e8fe32020-11-11 17:29:28 -050074 targets.append(target)
75
Brian Osmaned5181e2021-07-01 13:54:20 -040076 if lang == "--glsl":
Brian Osman7b239052020-11-18 17:04:33 +000077 worklist.write(input + "\n")
78 worklist.write(target + ".glsl\n")
79 worklist.write(settings + "\n\n")
John Stilesaeae3a52020-09-25 13:35:58 -040080 elif lang == "--metal":
Brian Osman7b239052020-11-18 17:04:33 +000081 worklist.write(input + "\n")
82 worklist.write(target + ".metal\n")
83 worklist.write(settings + "\n\n")
John Stilesdda1d312020-11-20 16:28:50 -050084 elif lang == "--spirv":
85 worklist.write(input + "\n")
John Stiles92072a32020-11-23 15:14:35 -050086 worklist.write(target + ".asm" + extensionForSpirvAsm(ext) + "\n")
John Stilesdda1d312020-11-20 16:28:50 -050087 worklist.write(settings + "\n\n")
Brian Osman977feec2020-12-22 11:28:59 -050088 elif lang == "--skvm":
89 worklist.write(input + "\n")
90 worklist.write(target + ".skvm\n")
91 worklist.write(settings + "\n\n")
Brian Osman62b039b2021-02-08 13:49:53 -050092 elif lang == "--stage":
93 worklist.write(input + "\n")
94 worklist.write(target + ".stage\n")
95 worklist.write(settings + "\n\n")
John Stilesea9e7ca2020-09-14 16:46:40 -040096 else:
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -040097 sys.exit("### Expected one of: --glsl --metal --spirv --skvm --stage --dsl, got " + lang)
John Stilesa1e8fe32020-11-11 17:29:28 -050098
John Stilesf92f89b2021-01-12 14:05:05 -050099 # Compile items one at a time.
100 if not batchCompile:
101 executeWorklist(input, worklist)
Brian Osmanbc354952021-07-09 15:39:25 -0400102 worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w')
John Stilesa1e8fe32020-11-11 17:29:28 -0500103
John Stilesf92f89b2021-01-12 14:05:05 -0500104# Compile everything all in one go.
105if batchCompile:
106 executeWorklist("", worklist)
107else:
108 worklist.close()
109 os.remove(worklist.name)