blob: fc657477dbf6863d8309330b143d89769dddbc1d [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
11
John Stiles0ed9f312020-09-16 17:46:37 -040012skslc = sys.argv[1]
John Stiles7e248712020-09-24 16:42:09 -040013lang = sys.argv[2]
14settings = sys.argv[3]
15inputs = sys.argv[4:]
John Stiles0ed9f312020-09-16 17:46:37 -040016
John Stiles886b9d42020-09-15 11:16:10 -040017def makeEmptyFile(path):
John Stilesea9e7ca2020-09-14 16:46:40 -040018 try:
John Stiles886b9d42020-09-15 11:16:10 -040019 open(path, 'wb').close()
John Stilesea9e7ca2020-09-14 16:46:40 -040020 except OSError:
21 pass
22
23def compile(skslc, input, target, extension):
24 target += extension
25 try:
John Stiles0ed9f312020-09-16 17:46:37 -040026 subprocess.check_output([skslc, input, target, settings], stderr=subprocess.STDOUT)
John Stilesea9e7ca2020-09-14 16:46:40 -040027 return True
28
29 except subprocess.CalledProcessError as err:
30 with open(target, 'wb') as dst:
31 dst.write("### Compilation failed:\n\n")
Brian Osmand911c912020-09-15 10:54:58 -040032 dst.write("\n".join(err.output.splitlines()))
33 dst.write("\n")
John Stilesea9e7ca2020-09-14 16:46:40 -040034 return False
35
John Stiles0ed9f312020-09-16 17:46:37 -040036if settings != "--settings" and settings != "--nosettings":
John Stiles7e248712020-09-24 16:42:09 -040037 sys.exit("### Expected --settings or --nosettings, got " + settings)
John Stilesd836f842020-09-14 10:21:44 -040038
39for input in inputs:
John Stilesea9e7ca2020-09-14 16:46:40 -040040 noExt, ext = os.path.splitext(input)
41 head, tail = os.path.split(noExt)
42 targetDir = os.path.join(head, "golden")
43 if not os.path.isdir(targetDir):
44 os.mkdir(targetDir)
John Stiles0ed9f312020-09-16 17:46:37 -040045
John Stilesea9e7ca2020-09-14 16:46:40 -040046 target = os.path.join(targetDir, tail)
John Stiles0ed9f312020-09-16 17:46:37 -040047 if settings == "--nosettings":
John Stiles371fde52020-09-21 17:09:52 -040048 target += "StandaloneSettings"
John Stiles0ed9f312020-09-16 17:46:37 -040049
John Stiles7e248712020-09-24 16:42:09 -040050 if lang == "--fp":
John Stilesea9e7ca2020-09-14 16:46:40 -040051 # First, compile the CPP. If we get an error, stop here.
52 if compile(skslc, input, target, ".cpp"):
53 # Next, compile the header.
54 if compile(skslc, input, target, ".h"):
55 # Both files built successfully.
56 continue
57 else:
58 # The header generated an error; this counts as an overall failure for this test.
John Stiles886b9d42020-09-15 11:16:10 -040059 # Blank out the passing CPP output since it's not relevant in a failure case.
60 makeEmptyFile(target + ".cpp")
John Stilesd836f842020-09-14 10:21:44 -040061 else:
John Stiles886b9d42020-09-15 11:16:10 -040062 # The CPP generated an error. We didn't actually generate a header at all, but Ninja
63 # expects an output file to exist or it won't reach steady-state.
64 makeEmptyFile(target + ".h")
John Stiles7e248712020-09-24 16:42:09 -040065 elif lang == "--glsl":
John Stilesea9e7ca2020-09-14 16:46:40 -040066 compile(skslc, input, target, ".glsl")
John Stilesaeae3a52020-09-25 13:35:58 -040067 elif lang == "--metal":
68 compile(skslc, input, target, ".metal")
John Stilesea9e7ca2020-09-14 16:46:40 -040069 else:
John Stilesaeae3a52020-09-25 13:35:58 -040070 sys.exit("### Expected one of: --fp --glsl --metal, got " + lang)