blob: 6cfe44271503bc857ed91c372350bc93a5c3530e [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 Stiles0ed9f312020-09-16 17:46:37 -040024if settings != "--settings" and settings != "--nosettings":
John Stiles7e248712020-09-24 16:42:09 -040025 sys.exit("### Expected --settings or --nosettings, got " + settings)
John Stilesd836f842020-09-14 10:21:44 -040026
John Stilesa1e8fe32020-11-11 17:29:28 -050027targets = []
John Stiles57adba02020-11-18 13:58:19 -050028worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False)
John Stilesa1e8fe32020-11-11 17:29:28 -050029
30# Convert the list of command-line inputs into a worklist file sfor skslc.
John Stilesd836f842020-09-14 10:21:44 -040031for input in inputs:
John Stilesa1e8fe32020-11-11 17:29:28 -050032 noExt, _ = os.path.splitext(input)
John Stilesea9e7ca2020-09-14 16:46:40 -040033 head, tail = os.path.split(noExt)
34 targetDir = os.path.join(head, "golden")
35 if not os.path.isdir(targetDir):
36 os.mkdir(targetDir)
John Stiles0ed9f312020-09-16 17:46:37 -040037
John Stilesea9e7ca2020-09-14 16:46:40 -040038 target = os.path.join(targetDir, tail)
John Stiles0ed9f312020-09-16 17:46:37 -040039 if settings == "--nosettings":
John Stiles371fde52020-09-21 17:09:52 -040040 target += "StandaloneSettings"
John Stiles0ed9f312020-09-16 17:46:37 -040041
John Stilesa1e8fe32020-11-11 17:29:28 -050042 targets.append(target)
43
John Stiles7e248712020-09-24 16:42:09 -040044 if lang == "--fp":
Brian Osman7b239052020-11-18 17:04:33 +000045 worklist.write(input + "\n")
46 worklist.write(target + ".cpp\n")
47 worklist.write(settings + "\n\n")
48 worklist.write(input + "\n")
49 worklist.write(target + ".h\n")
50 worklist.write(settings + "\n\n")
John Stiles7e248712020-09-24 16:42:09 -040051 elif lang == "--glsl":
Brian Osman7b239052020-11-18 17:04:33 +000052 worklist.write(input + "\n")
53 worklist.write(target + ".glsl\n")
54 worklist.write(settings + "\n\n")
John Stilesaeae3a52020-09-25 13:35:58 -040055 elif lang == "--metal":
Brian Osman7b239052020-11-18 17:04:33 +000056 worklist.write(input + "\n")
57 worklist.write(target + ".metal\n")
58 worklist.write(settings + "\n\n")
John Stilesea9e7ca2020-09-14 16:46:40 -040059 else:
John Stilesaeae3a52020-09-25 13:35:58 -040060 sys.exit("### Expected one of: --fp --glsl --metal, got " + lang)
John Stilesa1e8fe32020-11-11 17:29:28 -050061
Brian Osman7b239052020-11-18 17:04:33 +000062# Invoke skslc, passing in the worklist.
63worklist.close()
John Stilesa1e8fe32020-11-11 17:29:28 -050064try:
Brian Osman7b239052020-11-18 17:04:33 +000065 output = subprocess.check_output([skslc, worklist.name], stderr=subprocess.STDOUT)
66
John Stilesa1e8fe32020-11-11 17:29:28 -050067except subprocess.CalledProcessError as err:
Brian Osman7b239052020-11-18 17:04:33 +000068 print("### skslc error:\n")
69 print("\n".join(err.output.splitlines()))
John Stilesa1e8fe32020-11-11 17:29:28 -050070
John Stiles57adba02020-11-18 13:58:19 -050071os.remove(worklist.name)
72
John Stilesa1e8fe32020-11-11 17:29:28 -050073# A special case cleanup pass, just for CPP and H files: if either one of these files starts with
74# `### Compilation failed`, its sibling should be replaced by an empty file. This improves clarity
75# during code review; a failure on either file means that success on the sibling is irrelevant.
76if lang == "--fp":
77 for target in targets:
78 cppFile = open(target + '.cpp', 'r')
79 hFile = open(target + '.h', 'r')
80 if cppFile.readline().startswith("### Compilation failed"):
81 # The CPP had a compilation failure. Clear the header file.
82 hFile.close()
83 makeEmptyFile(target + '.h')
84 elif hFile.readline().startswith("### Compilation failed"):
85 # The header had a compilation failure. Clear the CPP file.
86 cppFile.close()
87 makeEmptyFile(target + '.cpp')