chromium/third_party/crc32c/BUILD.gn

# Copyright 2017 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/features.gni")
import("//testing/test.gni")

# Only applied to CRC32C source and tests. (not exported)
config("crc32c_config") {
  visibility = [ ":*" ]

  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 (current_cpu == "x86" || current_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" ]
  }

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

source_set("crc32c") {
  sources = [
    "src/include/crc32c/crc32c.h",
    "src/src/crc32c.cc",
    "src/src/crc32c_portable.cc",
  ]

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

source_set("crc32c_sse42") {
  visibility = [ ":*" ]

  sources = [
    "src/src/crc32c_sse42.cc",
    "src/src/crc32c_sse42.h",
    "src/src/crc32c_sse42_check.h",
  ]

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

  deps = [ ":crc32c_internal_headers" ]
}

source_set("crc32c_arm64") {
  visibility = [ ":*" ]

  sources = [
    "src/src/crc32c_arm64.cc",
    "src/src/crc32c_arm64.h",
    "src/src/crc32c_arm64_check.h",
  ]

  configs += [ ":crc32c_config" ]
  if (current_cpu == "arm64") {
    if (is_clang) {
      cflags = [
        "-march=armv8-a",

        # Some builds set -march to a different value from the above.
        # The specific feature flags below enable the instructions we need
        # in these cases. See https://crbug.com/934016 for example.
        "-Xclang",
        "-target-feature",
        "-Xclang",
        "+crc",
        "-Xclang",
        "-target-feature",
        "-Xclang",
        "+crypto",
        "-Xclang",
        "-target-feature",
        "-Xclang",
        "+aes",
      ]
    } else {
      cflags = [ "-march=armv8-a+crc+crypto" ]
    }
  }

  deps = [ ":crc32c_internal_headers" ]
}

source_set("crc32c_internal_headers") {
  sources = [
    "config/crc32c/crc32c_config.h",
    "src/src/crc32c_internal.h",
    "src/src/crc32c_prefetch.h",
    "src/src/crc32c_read_le.h",
    "src/src/crc32c_round_up.h",
  ]
}

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",
    ":crc32c_arm64",
    ":crc32c_internal_headers",
    ":crc32c_sse42",
    "//testing/gtest:gtest_main",
    "//third_party/googletest:gtest",
  ]
}

test("crc32c_benchmark") {
  sources = [ "src/src/crc32c_benchmark.cc" ]
  configs += [ ":crc32c_config" ]
  deps = [
    ":crc32c",
    ":crc32c_arm64",
    ":crc32c_internal_headers",
    ":crc32c_sse42",
    "//third_party/google_benchmark",
  ]
}