# Copyright 2023 The Dawn & Tint Authors
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import("//build_overrides/build.gni")

import("../../scripts/tint_overrides_with_defaults.gni")

if (tint_has_protobuf) {
  import("//third_party/protobuf/proto_library.gni")
}

###############################################################################
# OS defines
###############################################################################
tint_build_is_win = is_win
tint_build_is_mac = is_mac
tint_build_is_linux = is_linux

###############################################################################
# Tint library target
###############################################################################
template("libtint_source_set") {
  source_set(target_name) {
    forward_variables_from(invoker, "*", [ "configs" ])

    if (!defined(invoker.deps)) {
      deps = []
    }

    if (defined(invoker.configs)) {
      configs += invoker.configs
    }

    configs += [ "${tint_src_dir}:tint_common_config" ]

    if (build_with_chromium) {
      configs -= [ "//build/config/compiler:chromium_code" ]
      configs += [ "//build/config/compiler:no_chromium_code" ]
    }

    if (!defined(invoker.public_configs)) {
      public_configs = []
    }

    public_configs += [ "${tint_src_dir}:tint_public_config" ]
  }
}

###############################################################################
# Tint protobuf library target
###############################################################################
template("tint_proto_library") {
  if (!tint_has_protobuf) {
    error("Tint needs protobuf to build a proto library")
  }
  proto_library(target_name) {
    forward_variables_from(invoker, "*", [ "configs" ])
    proto_in_dir = "${tint_root_dir}"
    generate_cc = true
    generate_python = false
    use_protobuf_full = true
  }
}

###############################################################################
# Executables - only built when tint_build_cmds is enabled
###############################################################################
if (tint_build_cmds) {
  template("tint_executable") {
    executable(target_name) {
      forward_variables_from(invoker, "*")
    }
  }
}

###############################################################################
# Unit tests - only built when tint_build_unittests is enabled
###############################################################################
template("tint_unittests_source_set") {
  if (tint_build_unittests) {
    source_set(target_name) {
      forward_variables_from(invoker, "*", [ "configs" ])

      testonly = true

      if (defined(invoker.configs)) {
        configs += invoker.configs
      }

      configs += [ "${tint_src_dir}:tint_unittests_config" ]

      if (build_with_chromium) {
        configs -= [ "//build/config/compiler:chromium_code" ]
        configs += [ "//build/config/compiler:no_chromium_code" ]
      }

      if (!defined(invoker.deps)) {
        deps = []
      }

      deps += [ "${tint_src_dir}:gmock_and_gtest" ]
    }
  }
}

###############################################################################
# Fuzzers - only built when tint_has_fuzzers is enabled
###############################################################################
if (tint_has_fuzzers) {
  import("//testing/libfuzzer/fuzzer_test.gni")
  fuzzer_corpus_wgsl_dir = "${root_gen_dir}/fuzzers/wgsl_corpus"
  fuzzer_corpus_wgsl_stamp = "${fuzzer_corpus_wgsl_dir}.stamp"
  fuzzer_corpus_ir_dir = "${root_gen_dir}/fuzzers/ir_corpus"
  fuzzer_corpus_ir_stamp = "${fuzzer_corpus_ir_dir}.stamp"

  template("tint_fuzz_source_set") {
    source_set(target_name) {
      forward_variables_from(invoker, "*", [ "configs" ])

      testonly = true

      if (!defined(invoker.deps)) {
        deps = []
      }

      if (defined(invoker.configs)) {
        configs += invoker.configs
      }

      configs += [ "${tint_src_dir}:tint_common_config" ]

      if (build_with_chromium) {
        configs -= [ "//build/config/compiler:chromium_code" ]
        configs += [ "//build/config/compiler:no_chromium_code" ]
      }

      if (!defined(invoker.public_configs)) {
        public_configs = []
      }

      public_configs += [ "${tint_src_dir}:tint_public_config" ]
    }
  }

  template("tint_fuzzer_test") {
    # fuzzer_test.gni doesn't handle targets that use 'output_name' correctly.
    # It will build an executable with the requested name, but the ClusterFuzz
    # data files (corpus, dictionary, options, owners, runtime_deps) will all
    # use the target name for their output file names, and these will not be
    # used by ClusterFuzz.
    # To work around this, if an 'output_name' is specified, pass the
    # 'output_name' to fuzzer_test() as the target name, then make a proxy
    # 'target_name' group that depends on the fuzzer target.
    if (defined(invoker.output_name)) {
      fuzzer_name = invoker.output_name
    } else {
      fuzzer_name = target_name
    }

    fuzzer_test(fuzzer_name) {
      forward_variables_from(invoker, "*", [ "output_name" ])
      exclude_main = false

      if (target_name == "wgsl") {
        dict = "dictionary.txt"
        libfuzzer_options = [
          # TODO(crbug.com/tint/2223): Remove this to fuzz unicode?
          "only_ascii=1",
          "max_len=10000",
        ]
        seed_corpus = fuzzer_corpus_wgsl_dir
        seed_corpus_deps = [ "${tint_src_dir}:tint_generate_wgsl_corpus" ]
      } else if (target_name == "ir") {
        # The IR corpus takes a long time to generate and may not
        # actually be correctly formatted (crbug.com/345377541 &
        # crbug.com/343218481).
        # Disabling generating it by default until these issues
        # are resolved.
        # The corpus can still be generated by explicitly
        # invoking tint_generate_ir_corpus
        #
        # if (tint_build_cmds) {
        #   seed_corpus = fuzzer_corpus_ir_dir
        #   seed_corpus_deps = [ "${tint_src_dir}:tint_generate_ir_corpus" ]
        # }
      } else {
        assert(false, "unsupported tint fuzzer target")
      }
    }

    if (fuzzer_name != target_name) {
      group(target_name) {
        testonly = true
        deps = [ ":${fuzzer_name}" ]
      }
    }
  }
} else {
  template("tint_fuzz_source_set") {
    not_needed(invoker, "*")
    not_needed("*")
  }
  template("tint_fuzzer_test") {
    not_needed(invoker, "*")
    not_needed("*")
  }
}
