# 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 == "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"

# List of valid sizes generated by generate_seed_corpus.py
# N = (2^a)*(3^b)*(5^c) where a >= 5, b >=0, c >= 0 and N < 5000.
pffft_channels = [
  32,
  64,
  96,
  128,
  160,
  192,
  256,
  288,
  320,
  384,
  480,
  512,
  576,
  640,
  768,
  800,
  864,
  960,
  1024,
  1152,
  1280,
  1440,
  1536,
  1600,
  1728,
  1920,
  2048,
  2304,
  2400,
  2560,
  2592,
  2880,
  3072,
  3200,
  3456,
  3840,
  4000,
  4096,
  4320,
  4608,
  4800,
]

pffft_corpus_files = []
foreach(n, pffft_channels) {
  pffft_corpus_files += [
    "$fuzzer_testdata_dir/zeros_$n",
    "$fuzzer_testdata_dir/max_$n",
    "$fuzzer_testdata_dir/rnd_s16_$n",
  ]
}

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 = pffft_corpus_files
}

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",
  ]
}
