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

import("//build/config/features.gni")
import("//testing/test.gni")

# Only applied to CRC32C source and tests. (not exported)
config("crc32c_config") {
  include_dirs = [
    "config",
    "src/include",
  ]

  defines = [
    "BYTE_ORDER_BIG_ENDIAN=0",
    "CRC32C_TESTS_BUILT_WITH_GLOG=0",
  ]

  # If we ever support big-endian builds, add logic to conditionally enable
  # BYTE_ORDER_BIG_ENDIAN.

  if (target_cpu == "x86" || target_cpu == "x64") {
    defines += [
      "HAVE_MM_PREFETCH=1",
      "HAVE_SSE42=1",
    ]
  } else {
    defines += [
      "HAVE_MM_PREFETCH=0",
      "HAVE_SSE42=0",
    ]
  }
  if (is_clang || !is_win) {
    defines += [ "HAVE_BUILTIN_PREFETCH=1" ]
  } else {
    defines += [ "HAVE_BUILTIN_PREFETCH=0" ]
  }

  if (current_cpu == "arm64") {
    defines += [ "HAVE_ARM64_CRC32C=1" ]
  } else {
    defines += [ "HAVE_ARM64_CRC32C=0" ]
  }

  if (is_linux || is_chromeos) {
    defines += [
      "HAVE_STRONG_GETAUXVAL=1",
      "HAVE_WEAK_GETAUXVAL=1",
    ]
  } else if (is_android) {
    # Android added <sys/auxv.h> in API level 20.
    defines += [
      "HAVE_STRONG_GETAUXVAL=0",
      "HAVE_WEAK_GETAUXVAL=1",
    ]
  } else {
    defines += [
      "HAVE_STRONG_GETAUXVAL=0",
      "HAVE_WEAK_GETAUXVAL=0",
    ]
  }
}

source_set("crc32c") {
  sources = [
    "config/crc32c/crc32c_config.h",
    "src/include/crc32c/crc32c.h",
    "src/src/crc32c.cc",
    "src/src/crc32c_arm_linux_check.h",
    "src/src/crc32c_internal.h",
    "src/src/crc32c_portable.cc",
    "src/src/crc32c_prefetch.h",
    "src/src/crc32c_read_le.h",
    "src/src/crc32c_round_up.h",
    "src/src/crc32c_sse42_check.h",
  ]

  configs += [ ":crc32c_config" ]
  deps = [
    ":crc32c_arm64",
    ":crc32c_sse42",
  ]
}

source_set("crc32c_sse42") {
  sources = [
    "config/crc32c/crc32c_config.h",
    "src/src/crc32c_sse42.cc",
    "src/src/crc32c_sse42.h",
  ]

  configs += [ ":crc32c_config" ]
  if (target_cpu == "x86" || target_cpu == "x64") {
    if (is_win && !is_clang) {
      cflags = [ "/arch:AVX" ]
    } else {
      cflags = [ "-msse4.2" ]
    }
  }
}

source_set("crc32c_arm64") {
  sources = [
    "config/crc32c/crc32c_config.h",
    "src/src/crc32c_arm64.cc",
    "src/src/crc32c_arm64.h",
  ]

  configs += [ ":crc32c_config" ]
  if (current_cpu == "arm64") {
    cflags = [ "-march=armv8-a+crc+crypto" ]
  }
}

test("crc32c_tests") {
  sources = [
    "src/src/crc32c_arm64_unittest.cc",
    "src/src/crc32c_extend_unittests.h",
    "src/src/crc32c_portable_unittest.cc",
    "src/src/crc32c_prefetch_unittest.cc",
    "src/src/crc32c_read_le_unittest.cc",
    "src/src/crc32c_round_up_unittest.cc",
    "src/src/crc32c_sse42_unittest.cc",
    "src/src/crc32c_unittest.cc",
  ]

  configs += [ ":crc32c_config" ]
  deps = [
    ":crc32c",
    "//testing/gtest:gtest_main",
  ]
}