# Copyright 2019 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

add_library(pikcommon STATIC
  ${CMAKE_CURRENT_LIST_DIR}/simd/targets.cc
  ${CMAKE_CURRENT_LIST_DIR}/simd/targets.h
  ${CMAKE_CURRENT_LIST_DIR}/ac_predictions.cc
  ${CMAKE_CURRENT_LIST_DIR}/ac_predictions.h
  ${CMAKE_CURRENT_LIST_DIR}/ac_strategy.cc
  ${CMAKE_CURRENT_LIST_DIR}/ac_strategy.h
  ${CMAKE_CURRENT_LIST_DIR}/adaptive_quantization.cc
  ${CMAKE_CURRENT_LIST_DIR}/adaptive_quantization.h
  ${CMAKE_CURRENT_LIST_DIR}/adaptive_reconstruction.cc
  ${CMAKE_CURRENT_LIST_DIR}/adaptive_reconstruction.h
  ${CMAKE_CURRENT_LIST_DIR}/adaptive_reconstruction_fwd.h
  ${CMAKE_CURRENT_LIST_DIR}/alpha.cc
  ${CMAKE_CURRENT_LIST_DIR}/alpha.h
  ${CMAKE_CURRENT_LIST_DIR}/ans_common.cc
  ${CMAKE_CURRENT_LIST_DIR}/ans_common.h
  ${CMAKE_CURRENT_LIST_DIR}/ans_decode.cc
  ${CMAKE_CURRENT_LIST_DIR}/ans_decode.h
  ${CMAKE_CURRENT_LIST_DIR}/ans_encode.cc
  ${CMAKE_CURRENT_LIST_DIR}/ans_encode.h
  ${CMAKE_CURRENT_LIST_DIR}/ans_params.h
  ${CMAKE_CURRENT_LIST_DIR}/approx_cube_root.h
  ${CMAKE_CURRENT_LIST_DIR}/ar_control_field.cc
  ${CMAKE_CURRENT_LIST_DIR}/ar_control_field.h
  ${CMAKE_CURRENT_LIST_DIR}/arch_specific.cc
  ${CMAKE_CURRENT_LIST_DIR}/arch_specific.h
  ${CMAKE_CURRENT_LIST_DIR}/args.h
  ${CMAKE_CURRENT_LIST_DIR}/bit_reader.h
  ${CMAKE_CURRENT_LIST_DIR}/bits.h
  ${CMAKE_CURRENT_LIST_DIR}/block.h
  ${CMAKE_CURRENT_LIST_DIR}/block_dictionary.cc
  ${CMAKE_CURRENT_LIST_DIR}/block_dictionary.h
  ${CMAKE_CURRENT_LIST_DIR}/brotli.cc
  ${CMAKE_CURRENT_LIST_DIR}/brotli.h
  ${CMAKE_CURRENT_LIST_DIR}/butteraugli/butteraugli.cc
  ${CMAKE_CURRENT_LIST_DIR}/butteraugli/butteraugli.h
  ${CMAKE_CURRENT_LIST_DIR}/butteraugli_comparator.cc
  ${CMAKE_CURRENT_LIST_DIR}/butteraugli_comparator.h
  ${CMAKE_CURRENT_LIST_DIR}/butteraugli_distance.cc
  ${CMAKE_CURRENT_LIST_DIR}/butteraugli_distance.h
  ${CMAKE_CURRENT_LIST_DIR}/byte_order.h
  ${CMAKE_CURRENT_LIST_DIR}/cache_aligned.cc
  ${CMAKE_CURRENT_LIST_DIR}/cache_aligned.h
  ${CMAKE_CURRENT_LIST_DIR}/cluster.h
  ${CMAKE_CURRENT_LIST_DIR}/chroma_from_luma.cc
  ${CMAKE_CURRENT_LIST_DIR}/chroma_from_luma.h
  ${CMAKE_CURRENT_LIST_DIR}/chroma_from_luma_fwd.h
  ${CMAKE_CURRENT_LIST_DIR}/codec.h
  ${CMAKE_CURRENT_LIST_DIR}/codec_impl.cc
  ${CMAKE_CURRENT_LIST_DIR}/codec_png.cc
  ${CMAKE_CURRENT_LIST_DIR}/codec_png.h
  ${CMAKE_CURRENT_LIST_DIR}/codec_pnm.cc
  ${CMAKE_CURRENT_LIST_DIR}/codec_pnm.h
  ${CMAKE_CURRENT_LIST_DIR}/color_correlation.cc
  ${CMAKE_CURRENT_LIST_DIR}/color_correlation.h
  ${CMAKE_CURRENT_LIST_DIR}/color_encoding.cc
  ${CMAKE_CURRENT_LIST_DIR}/color_encoding.h
  ${CMAKE_CURRENT_LIST_DIR}/color_management.cc
  ${CMAKE_CURRENT_LIST_DIR}/color_management.h
  ${CMAKE_CURRENT_LIST_DIR}/common.h
  ${CMAKE_CURRENT_LIST_DIR}/compiler_specific.h
  ${CMAKE_CURRENT_LIST_DIR}/compressed_dc.cc
  ${CMAKE_CURRENT_LIST_DIR}/compressed_dc.h
  ${CMAKE_CURRENT_LIST_DIR}/compressed_image.cc
  ${CMAKE_CURRENT_LIST_DIR}/compressed_image.h
  ${CMAKE_CURRENT_LIST_DIR}/compressed_image_fwd.h
  ${CMAKE_CURRENT_LIST_DIR}/context_map_decode.cc
  ${CMAKE_CURRENT_LIST_DIR}/context_map_decode.h
  ${CMAKE_CURRENT_LIST_DIR}/context_map_encode.cc
  ${CMAKE_CURRENT_LIST_DIR}/context_map_encode.h
  ${CMAKE_CURRENT_LIST_DIR}/convolve.h
  ${CMAKE_CURRENT_LIST_DIR}/data_parallel.cc
  ${CMAKE_CURRENT_LIST_DIR}/data_parallel.h
  ${CMAKE_CURRENT_LIST_DIR}/dc_predictor.cc
  ${CMAKE_CURRENT_LIST_DIR}/dc_predictor.h
  ${CMAKE_CURRENT_LIST_DIR}/dct.cc
  ${CMAKE_CURRENT_LIST_DIR}/dct.h
  ${CMAKE_CURRENT_LIST_DIR}/dct_simd_4.h
  ${CMAKE_CURRENT_LIST_DIR}/dct_simd_8.h
  ${CMAKE_CURRENT_LIST_DIR}/dct_simd_any.h
  ${CMAKE_CURRENT_LIST_DIR}/dct_util.cc
  ${CMAKE_CURRENT_LIST_DIR}/dct_util.h
  ${CMAKE_CURRENT_LIST_DIR}/deconvolve.cc
  ${CMAKE_CURRENT_LIST_DIR}/deconvolve.h
  ${CMAKE_CURRENT_LIST_DIR}/descriptive_statistics.cc
  ${CMAKE_CURRENT_LIST_DIR}/descriptive_statistics.h
  ${CMAKE_CURRENT_LIST_DIR}/detect_dots.cc
  ${CMAKE_CURRENT_LIST_DIR}/detect_dots.h
  ${CMAKE_CURRENT_LIST_DIR}/entropy_coder.cc
  ${CMAKE_CURRENT_LIST_DIR}/entropy_coder.h
  ${CMAKE_CURRENT_LIST_DIR}/epf.cc
  ${CMAKE_CURRENT_LIST_DIR}/epf.h
  ${CMAKE_CURRENT_LIST_DIR}/bilinear_transform.cc
  ${CMAKE_CURRENT_LIST_DIR}/bilinear_transform.h
  ${CMAKE_CURRENT_LIST_DIR}/epf_stats.h
  ${CMAKE_CURRENT_LIST_DIR}/epf_target.cc
  ${CMAKE_CURRENT_LIST_DIR}/external_image.cc
  ${CMAKE_CURRENT_LIST_DIR}/external_image.h
  ${CMAKE_CURRENT_LIST_DIR}/fast_log.h
  ${CMAKE_CURRENT_LIST_DIR}/field_encodings.h
  ${CMAKE_CURRENT_LIST_DIR}/fields.h
  ${CMAKE_CURRENT_LIST_DIR}/file_io.h
  ${CMAKE_CURRENT_LIST_DIR}/gaborish.cc
  ${CMAKE_CURRENT_LIST_DIR}/gaborish.h
  ${CMAKE_CURRENT_LIST_DIR}/gamma_correct.h
  ${CMAKE_CURRENT_LIST_DIR}/gauss_blur.cc
  ${CMAKE_CURRENT_LIST_DIR}/gauss_blur.h
  ${CMAKE_CURRENT_LIST_DIR}/gradient_map.cc
  ${CMAKE_CURRENT_LIST_DIR}/gradient_map.h
  ${CMAKE_CURRENT_LIST_DIR}/headers.cc
  ${CMAKE_CURRENT_LIST_DIR}/headers.h
  ${CMAKE_CURRENT_LIST_DIR}/huffman_decode.cc
  ${CMAKE_CURRENT_LIST_DIR}/huffman_decode.h
  ${CMAKE_CURRENT_LIST_DIR}/huffman_encode.cc
  ${CMAKE_CURRENT_LIST_DIR}/huffman_encode.h
  ${CMAKE_CURRENT_LIST_DIR}/image.cc
  ${CMAKE_CURRENT_LIST_DIR}/image.h
  ${CMAKE_CURRENT_LIST_DIR}/image_ops.h
  ${CMAKE_CURRENT_LIST_DIR}/lehmer_code.cc
  ${CMAKE_CURRENT_LIST_DIR}/lehmer_code.h
  ${CMAKE_CURRENT_LIST_DIR}/linalg.cc
  ${CMAKE_CURRENT_LIST_DIR}/linalg.h
  ${CMAKE_CURRENT_LIST_DIR}/lossless16.cc
  ${CMAKE_CURRENT_LIST_DIR}/lossless16.h
  ${CMAKE_CURRENT_LIST_DIR}/lossless8.cc
  ${CMAKE_CURRENT_LIST_DIR}/lossless8.h
  ${CMAKE_CURRENT_LIST_DIR}/lossless_entropy.cc
  ${CMAKE_CURRENT_LIST_DIR}/lossless_entropy.h
  ${CMAKE_CURRENT_LIST_DIR}/metadata.cc
  ${CMAKE_CURRENT_LIST_DIR}/metadata.h
  ${CMAKE_CURRENT_LIST_DIR}/multipass_handler.h
  ${CMAKE_CURRENT_LIST_DIR}/noise.cc
  ${CMAKE_CURRENT_LIST_DIR}/noise.h
  ${CMAKE_CURRENT_LIST_DIR}/opsin_image.cc
  ${CMAKE_CURRENT_LIST_DIR}/opsin_image.h
  ${CMAKE_CURRENT_LIST_DIR}/opsin_inverse.cc
  ${CMAKE_CURRENT_LIST_DIR}/opsin_inverse.h
  ${CMAKE_CURRENT_LIST_DIR}/opsin_params.cc
  ${CMAKE_CURRENT_LIST_DIR}/opsin_params.h
  ${CMAKE_CURRENT_LIST_DIR}/optimize.h
  ${CMAKE_CURRENT_LIST_DIR}/os_specific.cc
  ${CMAKE_CURRENT_LIST_DIR}/os_specific.h
  ${CMAKE_CURRENT_LIST_DIR}/padded_bytes.cc
  ${CMAKE_CURRENT_LIST_DIR}/padded_bytes.h
  ${CMAKE_CURRENT_LIST_DIR}/pik.cc
  ${CMAKE_CURRENT_LIST_DIR}/pik.h
  ${CMAKE_CURRENT_LIST_DIR}/pik_info.cc
  ${CMAKE_CURRENT_LIST_DIR}/pik_info.h
  ${CMAKE_CURRENT_LIST_DIR}/pik_params.h
  ${CMAKE_CURRENT_LIST_DIR}/pik_frame.cc
  ${CMAKE_CURRENT_LIST_DIR}/pik_frame.h
  ${CMAKE_CURRENT_LIST_DIR}/profiler.h
  ${CMAKE_CURRENT_LIST_DIR}/quantizer.cc
  ${CMAKE_CURRENT_LIST_DIR}/quantizer.h
  ${CMAKE_CURRENT_LIST_DIR}/quant_weights.cc
  ${CMAKE_CURRENT_LIST_DIR}/quant_weights.h
  ${CMAKE_CURRENT_LIST_DIR}/rational_polynomial.h
  ${CMAKE_CURRENT_LIST_DIR}/resample.h
  ${CMAKE_CURRENT_LIST_DIR}/resize.h
  ${CMAKE_CURRENT_LIST_DIR}/saliency_map.cc
  ${CMAKE_CURRENT_LIST_DIR}/saliency_map.h
  ${CMAKE_CURRENT_LIST_DIR}/single_image_handler.cc
  ${CMAKE_CURRENT_LIST_DIR}/single_image_handler.h
  ${CMAKE_CURRENT_LIST_DIR}/size_coder.h
  ${CMAKE_CURRENT_LIST_DIR}/status.cc
  ${CMAKE_CURRENT_LIST_DIR}/status.h
  ${CMAKE_CURRENT_LIST_DIR}/tsc_timer.h
  ${CMAKE_CURRENT_LIST_DIR}/upscaler.cc
  ${CMAKE_CURRENT_LIST_DIR}/upscaler.h
  ${CMAKE_CURRENT_LIST_DIR}/write_bits.h
  ${CMAKE_CURRENT_LIST_DIR}/yuv_convert.cc
  ${CMAKE_CURRENT_LIST_DIR}/yuv_convert.h
  ${CMAKE_CURRENT_LIST_DIR}/yuv_opsin_convert.cc
  ${CMAKE_CURRENT_LIST_DIR}/yuv_opsin_convert.h
)

target_compile_options(pikcommon PUBLIC
  # Debug flags
  -dwarf-column-info
  -debug-info-kind=line-tables-only
  -dwarf-version=4
  -debugger-tuning=gdb

  # F_FLAGS
  -fmerge-all-constants
  -fno-builtin-fwrite
  -fno-builtin-fread
  -fno-signed-char
  -fsized-deallocation
  -fnew-alignment=8
  -fno-cxx-exceptions
  -fno-exceptions
  -fno-slp-vectorize
  -fno-vectorize

  # WARN_FLAGS
  -Wformat-security
  -Wno-char-subscripts
  -Wno-error=deprecated-declarations
  -Wno-sign-compare
  -Wno-strict-overflow
  -Wno-unused-function
  -Wthread-safety-analysis
  -Wno-unknown-warning-option
  -Wno-unused-command-line-argument
  -Wno-ignored-optimization-argument
  -Wno-ambiguous-member-template
  -Wno-pointer-sign
  -Wno-address-of-packed-member
  -Wno-enum-compare-switch
  -Wno-expansion-to-defined
  -Wno-extern-c-compat
  -Wno-gnu-alignof-expression
  -Wno-gnu-designator
  -Wno-gnu-variable-sized-type-not-at-end
  -Wno-ignored-attributes
  -Wno-ignored-qualifiers
  -Wno-inconsistent-missing-override
  -Wno-invalid-source-encoding
  -Wno-mismatched-tags
  -Wno-potentially-evaluated-expression
  -Wno-return-std-move
  -Wno-self-assign-overloaded
  -Wno-tautological-constant-compare
  -Wno-tautological-constant-in-range-compare
  -Wno-tautological-type-limit-compare
  -Wno-tautological-undefined-compare
  -Wno-tautological-unsigned-zero-compare
  -Wno-tautological-unsigned-enum-zero-compare
  -Wno-undefined-func-template
  -Wno-unknown-pragmas
  -Wno-unused-const-variable
  -Wno-unused-lambda-capture
  -Wno-unused-local-typedef
  -Wno-unused-private-field
  -Wno-private-header
  -Wfloat-overflow-conversion
  -Wfloat-zero-conversion
  -Wfor-loop-analysis
  -Wgnu-redeclared-enum
  -Wimplicit-fallthrough
  -Winfinite-recursion
  -Wliteral-conversion
  -Wself-assign
  -Wstring-conversion
  -Wtautological-overlap-compare
  -Wunused-comparison
  -Wvla
  -Wno-reserved-user-defined-literal
  -Wno-return-type-c-linkage
  -Wno-deprecated
  -Wno-invalid-offsetof
  -Wno-literal-suffix
  -Woverloaded-virtual
  -Wnon-virtual-dtor
  -Wdeprecated-increment-bool
  -Wc++11-compat
  -Wno-c++11-compat-binary-literal
  -Wc++2a-extensions
  -Wno-register
  -Wno-dynamic-exception-spec
  -Wprivate-header
  -Wno-builtin-macro-redefined

  # Machine flags
  # We don't add -pthread here since it is added automatically when depending on
  # Threads::Threads.
  -mthread-model posix

  # Language flags
  -disable-free
  -disable-llvm-verifier
  -discard-value-names
  # Note: this works only because this is the only -Xclang passed.
  -Xclang -relaxed-aliasing
  -fmath-errno
)

target_include_directories(pikcommon
    PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

target_link_libraries(pikcommon PRIVATE
  brotlicommon-static
  brotlienc-static
  brotlidec-static
  fse
  lodepng
  lcms2
  Threads::Threads
  "${CMAKE_DL_LIBS}"
)
