chromium/third_party/libpng/BUILD.gn

# Copyright 2014 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("//build/config/mips.gni")
import("//build/config/loongarch64.gni")

if (build_with_chromium) {
  import("//testing/libfuzzer/fuzzer_test.gni")
}

config("libpng_config") {
  include_dirs = [ "." ]

  defines = []

  if (is_win) {
    if (is_component_build) {
      defines += [
        "PNG_USE_DLL",
        "PNG_NO_MODULEDEF",
      ]
    }
  }
}

# Must be in a config because of how GN orders flags (otherwise -Wall will
# appear after this, and turn it back on).
config("clang_warnings") {
  if (is_clang) {
    cflags = [
      # libpng checks that the width is not greater than PNG_SIZE_MAX.
      # On platforms where size_t is 64-bits, this comparison will always
      # be false.
      "-Wno-tautological-constant-out-of-range-compare",
    ]
  }
}

# Cannot be a static_library in component builds
source_set("libpng_sources") {
  sources = [
    "png.c",
    "png.h",
    "pngconf.h",
    "pngdebug.h",
    "pngerror.c",
    "pngget.c",
    "pnginfo.h",
    "pnglibconf.h",
    "pngmem.c",
    "pngpread.c",
    "pngpriv.h",
    "pngread.c",
    "pngrio.c",
    "pngrtran.c",
    "pngrutil.c",
    "pngset.c",
    "pngstruct.h",
    "pngtrans.c",
    "pngwio.c",
    "pngwrite.c",
    "pngwtran.c",
    "pngwutil.c",
  ]

  defines = [ "PNG_SET_OPTION_SUPPORTED" ]
  cflags = []

  if (current_cpu == "x86" || current_cpu == "x64") {
    sources += [
      "intel/filter_sse2_intrinsics.c",
      "intel/intel_init.c",
    ]
    defines += [ "PNG_INTEL_SSE_OPT=1" ]
  } else if ((current_cpu == "arm" || current_cpu == "arm64") && arm_use_neon) {
    sources += [
      "arm/arm_init.c",
      "arm/filter_neon_intrinsics.c",
      "arm/palette_neon_intrinsics.c",
    ]
    defines += [
      "PNG_ARM_NEON_OPT=2",
      "PNG_ARM_NEON_IMPLEMENTATION=1",
    ]
    configs -= [ "//build/config/compiler:default_optimization" ]
    configs += [ "//build/config/compiler:optimize_speed" ]
  } else if (current_cpu == "mips64el" && mips_use_msa) {
    sources += [
      "mips/filter_mmi_inline_assembly.c",
      "mips/filter_msa_intrinsics.c",
      "mips/mips_init.c",
    ]
  } else if (current_cpu == "loong64" && loongarch64_use_lsx) {
    sources += [
      "loongarch/filter_lsx_intrinsics.c",
      "loongarch/loongarch_lsx_init.c",
    ]
  }

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

  if (is_win) {
    # Unary minus applied to unsigned type.
    cflags += [ "/wd4146" ]
  }

  if (is_win && is_component_build) {
    defines += [ "PNG_BUILD_DLL" ]
  }

  public_configs = [ ":libpng_config" ]

  public_deps = [ "//third_party/zlib" ]

  configs += [ ":clang_warnings" ]
}

if (is_win) {
  component("libpng") {
    public_deps = [ ":libpng_sources" ]
  }
} else {
  group("libpng") {
    public_deps = [ ":libpng_sources" ]
  }
}

if (build_with_chromium) {
  libpng_ossfuzz_seed_corpuses = [
    "//components/test/data/viz",
    "//third_party/blink/web_tests/images/png-suite/samples",
    "//third_party/blink/web_tests/images/resources/pngfuzz",
  ]

  fuzzer_test("libpng_read_fuzzer") {
    sources = [ "contrib/oss-fuzz/libpng_read_fuzzer.cc" ]
    deps = [ ":libpng" ]
    dict = "contrib/oss-fuzz/png.dict"
    seed_corpuses = libpng_ossfuzz_seed_corpuses
  }
}