# Copyright 2014 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/arm.gni")

# If fixed point implementation shall be used (otherwise float).
use_opus_fixed_point = ((is_android || is_chromeos ||
                         (is_ios && arm_version == 7)) && cpu_arch == "arm")

# If ARM optimizations shall be used to accelerate performance.
use_opus_arm_optimization = use_opus_fixed_point

# If OPUS Run Time CPU Detections (RTCD) shall be used.
use_opus_rtcd = ((is_android || is_chromeos) && cpu_arch == "arm")

config("opus_config") {
  include_dirs = [
    "src/include",
  ]
}

if (use_opus_rtcd) {
  action("convert_rtcd_assembler") {
    script = "convert_rtcd_assembler.py"
    outputs = [ "$target_gen_dir/celt_pitch_xcorr_arm_gnu.S" ]
    args = [
      rebase_path("//third_party/opus/src/celt/arm/arm2gnu.pl",
                  root_build_dir),
      rebase_path("//third_party/opus/src/celt/arm/celt_pitch_xcorr_arm.s",
                  root_build_dir),
      rebase_path("$target_gen_dir/celt_pitch_xcorr_arm_gnu.S",
                  root_build_dir),
    ]
  }
}

source_set("opus") {
  gypi_values = exec_script(
      "//build/gypi_to_gn.py",
      [ rebase_path("opus_srcs.gypi") ],
      "scope",
      [ "opus_srcs.gypi" ])
  sources = gypi_values.opus_common_sources

  defines = [
    "OPUS_BUILD",
    "OPUS_EXPORT=",
  ]

  include_dirs = [
    "src/celt",
    "src/silk",
  ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [ "//build/config/compiler:no_chromium_code" ]
  public_configs = [ ":opus_config" ]

  if (is_win) {
    defines += [
      "USE_ALLOCA",
      "inline=__inline",
    ]

    cflags = [
      "/wd4305",  # Disable truncation warning in celt/pitch.c .
      "/wd4334",  # Disable 32-bit shift warning in src/opus_encoder.c .
    ]
  } else {
    defines += [
      "HAVE_LRINT",
      "HAVE_LRINTF",
      "VAR_ARRAYS",
    ]
  }

  if (is_posix && !is_android) {
    # Suppress a warning given by opus_decoder.c that tells us
    # optimizations are turned off.
    cflags = [
      "-Wno-#pragma-messages",
    ]
  }

  if (use_opus_fixed_point) {
    sources += gypi_values.opus_fixed_sources

    defines += [ "FIXED_POINT" ]

    include_dirs += [
      "src/silk/fixed",
    ]
  } else {
    sources += gypi_values.opus_float_sources

    include_dirs += [
      "src/silk/float",
    ]
  }

  if (use_opus_arm_optimization) {
    sources += [
      "src/celt/arm/fixed_armv4.h",
      "src/celt/arm/fixed_armv5e.h",
      "src/celt/arm/kiss_fft_armv4.h",
      "src/celt/arm/kiss_fft_armv5e.h",
      "src/celt/pitch_arm.h",
      "src/silk/arm/macro_armv4.h",
      "src/silk/arm/macro_armv5e.h",
      "src/silk/arm/SigProc_FIX_armv4.h",
      "src/silk/arm/SigProc_FIX_armv5e.h",
    ]

    defines += [
      "OPUS_ARM_ASM",
      "OPUS_ARM_INLINE_ASM",
      "OPUS_ARM_INLINE_EDSP",
    ]

    if (use_opus_rtcd) {
      sources += [
        "src/celt/arm/arm_celt_map.c",
        "src/celt/arm/armcpu.c",
        "src/celt/arm/armcpu.h",
        "$target_gen_dir/celt_pitch_xcorr_arm_gnu.S",
      ]

      defines += [
        "OPUS_ARM_MAY_HAVE_EDSP",
        "OPUS_ARM_MAY_HAVE_MEDIA",
        "OPUS_ARM_MAY_HAVE_NEON",
        "OPUS_HAVE_RTCD",
      ]

      deps = [ ":convert_rtcd_assembler" ]
    }
  }
}

executable("opus_demo") {
  sources = [
    "src/src/opus_demo.c",
  ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [ "//build/config/compiler:no_chromium_code" ]

  include_dirs = [
    "src/celt",
    "src/silk",
  ]

  if (is_win) {
    defines = [ "inline=__inline" ]
  }
  if (is_android) {
    libs = [ "log" ]
  }
  if (is_clang) {
    cflags = [ "-Wno-absolute-value" ]
  }

  deps = [ ":opus" ]
}