# -*- coding: utf-8 -*-

# Copyright 2020 The GraphicsFuzz Project Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A tool for running a reduction on a source directory."""

import argparse
import sys
from pathlib import Path

from gfauto import (
    binaries_util,
    fuzz_glsl_test,
    fuzz_spirv_test,
    settings_util,
    test_util,
)


def main() -> None:
    parser = argparse.ArgumentParser(
        description="A tool for running a reduction on a source directory."
    )

    parser.add_argument(
        "source_dir",
        help="The source directory containing the shaders and the test.json file that describes how to run the test.",
    )

    parser.add_argument(
        "--output", help="Output directory.", default="reduction_output",
    )

    parser.add_argument(
        "--settings",
        help="Path to a settings JSON file for this instance.",
        default="settings.json",
    )

    parsed_args = parser.parse_args(sys.argv[1:])

    source_dir = Path(parsed_args.source_dir)
    output_dir = Path(parsed_args.output)
    settings = settings_util.read_or_create(Path(parsed_args.settings))

    binary_manager = binaries_util.get_default_binary_manager(settings=settings)

    test = test_util.metadata_read_from_source_dir(source_dir)

    if test.HasField("glsl"):
        fuzz_glsl_test.run_reduction(
            source_dir_to_reduce=source_dir,
            reduction_output_dir=output_dir,
            binary_manager=binary_manager,
            settings=settings,
        )
    elif test.HasField("spirv_fuzz"):
        fuzz_spirv_test.run_reduction(
            source_dir_to_reduce=source_dir,
            reduction_output_dir=output_dir,
            binary_manager=binary_manager,
            settings=settings,
        )
    else:
        raise AssertionError(f"Unknown test type: {test}")


if __name__ == "__main__":
    main()

@@ -1412,32 +1412,187 @@
 s.json%22,%0A    )%0A%0A
+    parser.add_argument(%0A        %22--literals_to_uniforms%22,%0A        action=%22store_true%22,%0A        help=%22Pass --literals-to-uniforms to glsl-reduce.%22,%0A    )%0A%0A
     parsed_args 
@@ -1625,16 +1625,16 @@
 gv%5B1:%5D)%0A
-
 %0A    sou
@@ -1785,16 +1785,82 @@
 ttings))
+%0A    literals_to_uniforms: bool = parsed_args.literals_to_uniforms
 %0A%0A    bi
@@ -1999,16 +1999,16 @@
 e_dir)%0A%0A
-
     if t
@@ -2025,24 +2025,257 @@
 ld(%22glsl%22):%0A
+        if (%0A            literals_to_uniforms%0A            and %22--literals-to-uniforms%22 not in settings.extra_graphics_fuzz_reduce_args%0A        ):%0A            settings.extra_graphics_fuzz_reduce_args.append(%22--literals-to-uniforms%22)%0A%0A
         fuzz

add --literals_to_uniforms (#1020)