blob: 96157241fad8266a2f138c2f7597d5e7deb8865b [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
9import subprocess
10import sys
Brian Osman7b239052020-11-18 17:04:33 +000011import tempfile
John Stilesd836f842020-09-14 10:21:44 -040012
John Stiles0ed9f312020-09-16 17:46:37 -040013skslc = sys.argv[1]
John Stiles7e248712020-09-24 16:42:09 -040014lang = sys.argv[2]
15settings = sys.argv[3]
16inputs = sys.argv[4:]
John Stiles0ed9f312020-09-16 17:46:37 -040017
John Stiles886b9d42020-09-15 11:16:10 -040018def makeEmptyFile(path):
John Stilesea9e7ca2020-09-14 16:46:40 -040019 try:
John Stiles886b9d42020-09-15 11:16:10 -040020 open(path, 'wb').close()
John Stilesea9e7ca2020-09-14 16:46:40 -040021 except OSError:
22 pass
23
John Stilesdda1d312020-11-20 16:28:50 -050024def extensionForSpirvAsm(ext):
John Stiles92072a32020-11-23 15:14:35 -050025 return ext if (ext == '.frag' or ext == '.vert' or ext == '.geom') else '.frag'
John Stilesdda1d312020-11-20 16:28:50 -050026
John Stiles0ed9f312020-09-16 17:46:37 -040027if settings != "--settings" and settings != "--nosettings":
John Stiles7e248712020-09-24 16:42:09 -040028 sys.exit("### Expected --settings or --nosettings, got " + settings)
John Stilesd836f842020-09-14 10:21:44 -040029
John Stilesa1e8fe32020-11-11 17:29:28 -050030targets = []
John Stiles57adba02020-11-18 13:58:19 -050031worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False)
John Stilesa1e8fe32020-11-11 17:29:28 -050032
33# Convert the list of command-line inputs into a worklist file sfor skslc.
John Stilesd836f842020-09-14 10:21:44 -040034for input in inputs:
John Stilesdda1d312020-11-20 16:28:50 -050035 noExt, ext = os.path.splitext(input)
John Stilesea9e7ca2020-09-14 16:46:40 -040036 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 Stiles0ed9f312020-09-16 17:46:37 -040040
John Stilesea9e7ca2020-09-14 16:46:40 -040041 target = os.path.join(targetDir, tail)
John Stiles0ed9f312020-09-16 17:46:37 -040042 if settings == "--nosettings":
John Stiles371fde52020-09-21 17:09:52 -040043 target += "StandaloneSettings"
John Stiles0ed9f312020-09-16 17:46:37 -040044
John Stilesa1e8fe32020-11-11 17:29:28 -050045 targets.append(target)
46
John Stiles7e248712020-09-24 16:42:09 -040047 if lang == "--fp":
Brian Osman7b239052020-11-18 17:04:33 +000048 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 Stiles7e248712020-09-24 16:42:09 -040054 elif lang == "--glsl":
Brian Osman7b239052020-11-18 17:04:33 +000055 worklist.write(input + "\n")
56 worklist.write(target + ".glsl\n")
57 worklist.write(settings + "\n\n")
John Stilesaeae3a52020-09-25 13:35:58 -040058 elif lang == "--metal":
Brian Osman7b239052020-11-18 17:04:33 +000059 worklist.write(input + "\n")
60 worklist.write(target + ".metal\n")
61 worklist.write(settings + "\n\n")
John Stilesdda1d312020-11-20 16:28:50 -050062 elif lang == "--spirv":
63 worklist.write(input + "\n")
John Stiles92072a32020-11-23 15:14:35 -050064 worklist.write(target + ".asm" + extensionForSpirvAsm(ext) + "\n")
John Stilesdda1d312020-11-20 16:28:50 -050065 worklist.write(settings + "\n\n")
Brian Osman977feec2020-12-22 11:28:59 -050066 elif lang == "--skvm":
67 worklist.write(input + "\n")
68 worklist.write(target + ".skvm\n")
69 worklist.write(settings + "\n\n")
John Stilesea9e7ca2020-09-14 16:46:40 -040070 else:
Brian Osman977feec2020-12-22 11:28:59 -050071 sys.exit("### Expected one of: --fp --glsl --metal --spirv --skvm, got " + lang)
John Stilesa1e8fe32020-11-11 17:29:28 -050072
Brian Osman7b239052020-11-18 17:04:33 +000073# Invoke skslc, passing in the worklist.
74worklist.close()
John Stilesa1e8fe32020-11-11 17:29:28 -050075try:
Brian Osman7b239052020-11-18 17:04:33 +000076 output = subprocess.check_output([skslc, worklist.name], stderr=subprocess.STDOUT)
John Stilesa1e8fe32020-11-11 17:29:28 -050077except subprocess.CalledProcessError as err:
John Stilesa446ca12020-11-19 18:54:33 -050078 if err.returncode != 1:
79 print("### skslc error:\n")
80 print("\n".join(err.output.splitlines()))
81 sys.exit(err.returncode)
82 pass # Compile errors (exit code 1) are expected and normal in test code
John Stilesa1e8fe32020-11-11 17:29:28 -050083
John Stiles57adba02020-11-18 13:58:19 -050084os.remove(worklist.name)
85
John Stilesa1e8fe32020-11-11 17:29:28 -050086# A special case cleanup pass, just for CPP and H files: if either one of these files starts with
87# `### Compilation failed`, its sibling should be replaced by an empty file. This improves clarity
88# during code review; a failure on either file means that success on the sibling is irrelevant.
89if lang == "--fp":
90 for target in targets:
91 cppFile = open(target + '.cpp', 'r')
92 hFile = open(target + '.h', 'r')
93 if cppFile.readline().startswith("### Compilation failed"):
94 # The CPP had a compilation failure. Clear the header file.
95 hFile.close()
96 makeEmptyFile(target + '.h')
97 elif hFile.readline().startswith("### Compilation failed"):
98 # The header had a compilation failure. Clear the CPP file.
99 cppFile.close()
100 makeEmptyFile(target + '.cpp')