xref: /aosp_15_r20/external/skia/gn/minify_sksl.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 subprocess
10import sys
11
12sksl_minify = sys.argv[1]
13targetDir = sys.argv[2]
14modules = sys.argv[3:]
15
16# sksl-minify uses the dependency list to ensure that minified names are unique at global scope.
17dependencies = {
18    'sksl_compute': ['sksl_gpu', 'sksl_shared'],
19    'sksl_gpu': ['sksl_shared'],
20    'sksl_frag': ['sksl_gpu', 'sksl_shared'],
21    'sksl_vert': ['sksl_gpu', 'sksl_shared'],
22    'sksl_graphite_frag': ['sksl_frag', 'sksl_gpu', 'sksl_shared'],
23    'sksl_graphite_frag_es2': ['sksl_frag', 'sksl_gpu', 'sksl_shared'],
24    'sksl_graphite_vert': ['sksl_vert', 'sksl_gpu', 'sksl_shared'],
25    'sksl_graphite_vert_es2': ['sksl_vert', 'sksl_gpu', 'sksl_shared'],
26    'sksl_public': ['sksl_shared'],
27    'sksl_rt_shader': ['sksl_public', 'sksl_shared'],
28    'sksl_shared': [],
29}
30
31for module in modules:
32    try:
33        moduleDir, moduleName = os.path.split(os.path.splitext(module)[0])
34        if not os.path.isdir(targetDir):
35            os.mkdir(targetDir)
36        target = os.path.join(targetDir, moduleName)
37
38        # Determine the program kind based on the module name.
39        if "_compute" in module:
40            programKind = "--compute"
41        elif "_vert" in module:
42            programKind = "--vert"
43        else:
44            programKind = "--frag"
45
46        # Assemble the module dependency list and call sksl-minify to recreate the module in its
47        # minified form.
48        moduleList = [module]
49        if moduleName not in dependencies:
50            print("### Error compiling " + moduleName + ": dependency list must be specified")
51            exit(1)
52        for dependent in dependencies[moduleName]:
53            moduleList.append(os.path.join(moduleDir, dependent) + ".sksl")
54
55        # Generate fully-optimized and minified module data (for release/optimize-for-size builds).
56        destPath = target + ".minified.sksl"
57        args = [sksl_minify, programKind, "--stringify", destPath] + moduleList
58        subprocess.check_output(args).decode('utf-8')
59
60        # Generate unoptimized module data (used in debug, for improved readability).
61        destPath = target + ".unoptimized.sksl"
62        args = [sksl_minify, programKind, "--unoptimized", "--stringify", destPath] + moduleList
63        subprocess.check_output(args).decode('utf-8')
64
65    except subprocess.CalledProcessError as err:
66        print("### Error compiling " + module + ":")
67        print(err.output)
68        exit(1)
69