blob: 1709c7db249a2b55151b30b264c6348552b20989 [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")
John Stilesea9e7ca2020-09-14 16:46:40 -040066 else:
John Stilesdda1d312020-11-20 16:28:50 -050067 sys.exit("### Expected one of: --fp --glsl --metal --spirv, got " + lang)
John Stilesa1e8fe32020-11-11 17:29:28 -050068
Brian Osman7b239052020-11-18 17:04:33 +000069# Invoke skslc, passing in the worklist.
70worklist.close()
John Stilesa1e8fe32020-11-11 17:29:28 -050071try:
Brian Osman7b239052020-11-18 17:04:33 +000072 output = subprocess.check_output([skslc, worklist.name], stderr=subprocess.STDOUT)
John Stilesa1e8fe32020-11-11 17:29:28 -050073except subprocess.CalledProcessError as err:
John Stilesa446ca12020-11-19 18:54:33 -050074 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 Stilesa1e8fe32020-11-11 17:29:28 -050079
John Stiles57adba02020-11-18 13:58:19 -050080os.remove(worklist.name)
81
John Stilesa1e8fe32020-11-11 17:29:28 -050082# 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.
85if 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')