# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/arm.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//testing/test.gni")

config("common_config") {
  cflags = [ "-Wno-shadow" ]

  if (is_win) {
    defines = [
      # Required to use math constants from math.h.
      "_USE_MATH_DEFINES",
    ]
  }

  # PFFFT doesn't support SIMD on some cpus, so build a scalar version.
  if ((current_cpu == "arm" && !arm_use_neon) || current_cpu == "mipsel" ||
      current_cpu == "mips64el" || current_cpu == "ppc64" ||
      current_cpu == "riscv64" || current_cpu == "s390x" ||
      current_cpu == "loong64") {
    defines = [ "PFFFT_SIMD_DISABLE" ]
  }
}

static_library("pffft") {
  configs += [ ":common_config" ]
  sources = [
    "src/pffft.c",
    "src/pffft.h",
  ]
}

# Fuzzing.

group("fuzzers") {
  testonly = true
  deps = [
    ":pffft_complex_fuzzer",
    ":pffft_real_fuzzer",
  ]
}

fuzzer_testdata_dir = "$target_gen_dir/testdata"

action("generate_pffft_fuzzer_corpus") {
  script = "generate_seed_corpus.py"
  sources = [ "generate_seed_corpus.py" ]
  args = [ rebase_path(fuzzer_testdata_dir, root_build_dir) ]
  outputs = [ fuzzer_testdata_dir ]
}

fuzzer_test("pffft_complex_fuzzer") {
  sources = [ "pffft_fuzzer.cc" ]
  cflags = [ "-DTRANSFORM_COMPLEX" ]
  deps = [ ":pffft" ]
  seed_corpus = fuzzer_testdata_dir
  seed_corpus_deps = [ ":generate_pffft_fuzzer_corpus" ]
}

fuzzer_test("pffft_real_fuzzer") {
  sources = [ "pffft_fuzzer.cc" ]
  cflags = [ "-DTRANSFORM_REAL" ]
  deps = [ ":pffft" ]
  seed_corpus = fuzzer_testdata_dir
  seed_corpus_deps = [ ":generate_pffft_fuzzer_corpus" ]
}

# Unit tests and benchmark.

# This target must be used only for testing and benchmark purposes.
static_library("fftpack") {
  testonly = true
  configs += [ ":common_config" ]
  sources = [
    "src/fftpack.c",
    "src/fftpack.h",
  ]
  visibility = [ ":*" ]
}

config("pffft_benchmark_internal_config") {
  cflags = [
    # test_pffft.c contains an `exit(1)` following a `break` statement.
    "-Wno-unreachable-code",
  ]
}

executable("pffft_benchmark") {
  testonly = true
  configs += [
    ":common_config",
    ":pffft_benchmark_internal_config",
  ]
  sources = [ "src/test_pffft.c" ]
  deps = [
    ":fftpack",
    ":pffft",
  ]
}

test("pffft_unittest") {
  testonly = true
  sources = [ "pffft_unittest.cc" ]
  deps = [
    ":fftpack",
    ":pffft",
    "//testing/gtest",
    "//testing/gtest:gtest_main",
  ]
}
