chromium/third_party/openh264/BUILD.gn

# Copyright 2015 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/chromeos/ui_mode.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//third_party/nasm/nasm_assemble.gni")
import("//third_party/openh264/openh264_sources.gni")

# Config shared by all openh264 targets.
config("config") {
  cflags = []
  defines = []

  if (is_chromeos && current_cpu == "arm") {
    # HAVE_NEON and __chromeos__ are needed for enabling NEON on ChromeOS
    # devices.
    defines += [
      "HAVE_NEON",
      "__chromeos__",
    ]
  }

  # GCC and clang flags. MSVS (is_win && !is_clang) does not use cflags.
  if (!is_win || is_clang) {
    cflags += [
      "-Wno-format",
      "-Wno-format-security",
      "-Wno-header-hygiene",
      "-Wno-unused-function",
      "-Wno-unused-value",
    ]
  }

  # Platform-specific defines.
  if (is_android) {
    # Android NDK is necessary for its cpufeatures and this define is what
    # OpenH264 code uses to check if it should be used.
    defines += [ "ANDROID_NDK" ]
  }
}

# MSAN builds are flaky with assembler. crbug.com/685168

use_assembler = (is_win || is_mac || is_linux || is_chromeos) &&
                (current_cpu == "x86" || current_cpu == "x64") && !is_msan

# This IF statement will make the targets visible only on specific builds,
# which will lead to failures on other platforms if accidentally invoked.
if (use_assembler) {
  asm_defines = []
  if (!is_component_build) {
    if (is_apple) {
      asm_defines += [ "WELS_PRIVATE_EXTERN=private_extern" ]
    } else if (is_linux || is_chromeos || is_android || is_fuchsia) {
      asm_defines += [ "WELS_PRIVATE_EXTERN=hidden" ]
    }
  }

  nasm_assemble("openh264_common_asm") {
    include_dirs = openh264_common_include_dirs
    sources = openh264_common_sources_asm_x86
    defines = asm_defines
    if (current_cpu == "x86") {
      defines += [
        "X86_32",
        "X86_32_PICASM",
      ]
    } else {  # x64
      if (is_mac) {
        defines += [
          "PREFIX",
          "UNIX64",
        ]
      } else if (is_win) {
        defines += [ "WIN64" ]
      } else if (is_linux || is_chromeos) {
        defines += [ "UNIX64" ]
      }
    }
  }

  nasm_assemble("openh264_processing_asm") {
    include_dirs = openh264_processing_include_dirs
    include_dirs += [ "./src/codec/common/x86" ]
    sources = openh264_processing_sources_asm_x86
    defines = asm_defines
    if (current_cpu == "x86") {
      defines += [
        "X86_32",
        "X86_32_PICASM",
      ]
    } else {  # x64
      if (is_mac) {
        defines += [
          "PREFIX",
          "UNIX64",
        ]
      } else if (is_win) {
        defines += [ "WIN64" ]
      } else if (is_linux || is_chromeos) {
        defines += [ "UNIX64" ]
      }
    }
  }

  nasm_assemble("openh264_encoder_asm") {
    include_dirs = openh264_encoder_include_dirs
    include_dirs += [ "./src/codec/common/x86" ]
    sources = openh264_encoder_sources_asm_x86
    defines = asm_defines
    if (current_cpu == "x86") {
      defines += [
        "X86_32",
        "X86_32_PICASM",
      ]
    } else {  # x64
      if (is_mac) {
        defines += [
          "PREFIX",
          "UNIX64",
        ]
      } else if (is_win) {
        defines += [ "WIN64" ]
      } else if (is_linux || is_chromeos) {
        defines += [ "UNIX64" ]
      }
    }
  }
}  # if (is_win || is_linux || is_chromeos)

source_set("common") {
  sources = openh264_common_sources
  if (is_chromeos && current_cpu == "arm") {
    sources += openh264_common_sources_asm_arm
  }
  include_dirs = openh264_common_include_dirs

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [ "//build/config/compiler:no_chromium_code" ]
  configs += [ ":config" ]
  deps = []
  if (use_assembler) {
    defines = [ "X86_ASM" ]
    deps += [ ":openh264_common_asm" ]
  }
  if (is_android) {
    deps += [
      # Defines "android_get/setCpu..." functions. The original OpenH264 build
      # files replaces these using macros for "wels_..." versions of the same
      # functions. We do not have access to these and use the <cpu-features.h>
      # ones instead.
      "//third_party/cpu_features:ndk_compat",
    ]
  }
}

source_set("processing") {
  sources = openh264_processing_sources
  if (is_chromeos && current_cpu == "arm") {
    sources += openh264_processing_sources_asm_arm
  }
  include_dirs = openh264_processing_include_dirs

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [ "//build/config/compiler:no_chromium_code" ]
  configs += [ ":config" ]
  deps = [ ":common" ]
  if (use_assembler) {
    defines = [ "X86_ASM" ]
    deps += [ ":openh264_processing_asm" ]
  }
}

source_set("encoder") {
  sources = openh264_encoder_sources
  if (is_chromeos && current_cpu == "arm") {
    sources += openh264_encoder_sources_asm_arm
  }
  include_dirs = openh264_encoder_include_dirs

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

  # TODO: Remove after fixing always-true condition
  # third_party/openh264/src/codec/encoder/core/src/encoder_ext.cpp:142.
  if (is_clang) {
    configs -= [ "//build/config/clang:extra_warnings" ]
  }
  deps = [
    ":common",
    ":processing",
  ]
  if (use_assembler) {
    defines = [ "X86_ASM" ]
    deps += [ ":openh264_encoder_asm" ]
  }
}

source_set("decoder") {
  testonly = true

  sources = openh264_decoder_sources
  include_dirs = openh264_decoder_include_dirs

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

  deps = [
    ":common",
    ":processing",
  ]
}