1#!/usr/bin/env python 2# 3# Copyright 2022 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 14batchMinify = False 15 16sksl_minify = sys.argv[1] 17shared_module = sys.argv[2] 18public_module = sys.argv[3] 19rt_shader_module = sys.argv[4] 20input_root_dir = sys.argv[5] 21output_root_dir = sys.argv[6] 22# The last arg is a file containing a space seperated list of filenames 23input_file = sys.argv[7] 24with open(input_file, 'r') as reader: 25 all_inputs = shlex.split(reader.read()) 26 27inputs = [] 28for file in all_inputs: 29 if (file.endswith(".rts") or file.endswith(".privrts") or file.endswith(".rtcf") or 30 file.endswith(".rtb") or file.endswith(".mfrag") or file.endswith(".mvert")): 31 inputs.append(file) 32 33def executeWorklist(input, worklist): 34 # Invoke sksl-minify, passing in the worklist. 35 worklist.close() 36 try: 37 output = subprocess.check_output([ 38 sksl_minify, worklist.name], stderr=subprocess.STDOUT).decode('utf-8', errors='ignore') 39 except subprocess.CalledProcessError as err: 40 if err.returncode != 1: 41 print("### " + input + " sksl-minify error:\n") 42 print("\n".join(err.output.decode('utf-8', errors='ignore').splitlines())) 43 sys.exit(err.returncode) 44 pass # Compile errors (exit code 1) are expected and normal in test code 45 46 # Delete the worklist file now that execution is complete. 47 os.remove(worklist.name) 48 49worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w') 50 51# Here we loop over the inputs and convert them into a worklist file for sksl-minify. 52for input in inputs: 53 # Derive the target path from the input filename and remove the extension so it can 54 # end with .minified.sksl 55 target = input.replace(input_root_dir, output_root_dir) 56 target = os.path.splitext(target)[0] 57 target_dir = os.path.dirname(target) 58 if not os.path.isdir(target_dir): 59 os.mkdir(target_dir) 60 61 noExt, ext = os.path.splitext(input) 62 head, tail = os.path.split(noExt) 63 64 if ext == '.rts': 65 worklist.write("--shader\n") 66 elif ext == '.privrts': 67 worklist.write("--privshader\n") 68 elif ext == '.rtcf': 69 worklist.write("--colorfilter\n") 70 elif ext == '.rtb': 71 worklist.write("--blender\n") 72 elif ext == '.mfrag': 73 worklist.write("--meshfrag\n") 74 elif ext == '.mvert': 75 worklist.write("--meshvert\n") 76 77 worklist.write(target + ".minified.sksl\n") 78 worklist.write(input + "\n") 79 if ext == '.privrts': 80 worklist.write(rt_shader_module + "\n") 81 worklist.write(public_module + "\n") 82 worklist.write(shared_module + "\n\n") 83 84 # Compile items one at a time. 85 if not batchMinify: 86 executeWorklist(input, worklist) 87 worklist = tempfile.NamedTemporaryFile(suffix='.worklist', delete=False, mode='w') 88 89# Compile everything all in one go. 90if batchMinify: 91 executeWorklist("", worklist) 92else: 93 worklist.close() 94 os.remove(worklist.name) 95