xref: /aosp_15_r20/external/skia/gn/compile_sksl_tests.py (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1#!/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 shlex
10import subprocess
11import sys
12import tempfile
13
14batchCompile = True
15
16skslc = sys.argv[1]
17lang = sys.argv[2]
18settings = sys.argv[3]
19input_root_dir = sys.argv[4]
20output_root_dir = sys.argv[5]
21# The last arg is a file containing a space seperated list of filenames
22input_file = sys.argv[6]
23with open(input_file, 'r') as reader:
24    inputs = shlex.split(reader.read())
25
26def executeWorklist(input, worklist):
27    # Invoke skslc, passing in the worklist.
28    worklist.close()
29    try:
30        output = subprocess.check_output([
31            skslc, worklist.name], stderr=subprocess.STDOUT).decode('utf-8', errors='ignore')
32    except subprocess.CalledProcessError as err:
33        if err.returncode != 1:
34            print("### " + input + " skslc error:\n")
35            #print("$ lldb out/Debug/skslc -- " + worklist.name + "\n\n")
36            print("\n".join(err.output.decode('utf-8', errors='ignore').splitlines()))
37            sys.exit(err.returncode)
38        pass  # Compile errors (exit code 1) are expected and normal in test code
39
40    # Delete the worklist file now that execution is complete.
41    os.remove(worklist.name)
42
43def extensionForSpirvAsm(ext):
44    if (ext == '.compute'):
45        return '.comp'
46    return ext if (ext == '.frag' or ext == '.vert') else '.frag'
47
48if settings != "--settings" and settings != "--nosettings":
49    sys.exit("### Expected --settings or --nosettings, got " + settings)
50
51worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w')
52
53# Here we loop over the inputs and convert them into a worklist file for sksl-minify.
54for input in inputs:
55    # Derive the target path from the input filename and remove the extension so it can
56    # end with .minified.sksl
57    target = input.replace(input_root_dir, output_root_dir)
58    target = os.path.splitext(target)[0]
59    target_dir = os.path.dirname(target)
60    if not os.path.isdir(target_dir):
61        os.mkdir(target_dir)
62
63    noExt, ext = os.path.splitext(input)
64    head, tail = os.path.split(noExt)
65
66    if settings == "--nosettings":
67        target += "StandaloneSettings"
68
69    if lang == "--glsl":
70        worklist.write(input + "\n")
71        worklist.write(target + ".glsl\n")
72        worklist.write(settings + "\n\n")
73    elif lang == "--metal":
74        worklist.write(input + "\n")
75        worklist.write(target + ".metal\n")
76        worklist.write(settings + "\n\n")
77    elif lang == "--hlsl":
78        worklist.write(input + "\n")
79        worklist.write(target + ".hlsl\n")
80        worklist.write(settings + "\n\n")
81    elif lang == "--spirv":
82        worklist.write(input + "\n")
83        worklist.write(target + ".asm" + extensionForSpirvAsm(ext) + "\n")
84        worklist.write(settings + "\n\n")
85    elif lang == "--skrp":
86        worklist.write(input + "\n")
87        worklist.write(target + ".skrp\n")
88        worklist.write(settings + "\n\n")
89    elif lang == "--stage":
90        worklist.write(input + "\n")
91        worklist.write(target + ".stage\n")
92        worklist.write(settings + "\n\n")
93    elif lang == "--wgsl":
94        worklist.write(input + "\n")
95        worklist.write(target + ".wgsl\n")
96        worklist.write(settings + "\n\n")
97    else:
98        sys.exit("### Expected one of: --glsl --metal --hlsl --spirv --skrp " +
99                 "--stage --wgsl, got " + lang)
100
101    # Compile items one at a time.
102    if not batchCompile:
103        executeWorklist(input, worklist)
104        worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w')
105
106# Compile everything all in one go.
107if batchCompile:
108    executeWorklist("", worklist)
109else:
110    worklist.close()
111    os.remove(worklist.name)
112