cmake_minimum_required(VERSION 3.1)
project(astcenc VERSION 2.1 LANGUAGES CXX)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

find_package(tinyexr REQUIRED)
find_package(Threads REQUIRED)

find_path(stb_INCLUDE_DIR NAMES stb.h PATH_SUFFIXES stb)
find_library(stb_LIBRARY NAMES stb)
add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${stb_INCLUDE_DIR})
target_link_libraries(stb INTERFACE ${stb_LIBRARY})

add_library(astcenc_common INTERFACE)
target_compile_definitions(astcenc_common INTERFACE
	ASTCENC_ISA_INVARIANCE=1
	ASTCENC_VECALIGN=16
)
target_compile_options(astcenc_common INTERFACE
	-faligned-new -fno-strict-aliasing -Wall -Wextra -Wpedantic -Wno-unused-parameter -Wno-double-promotion -Wno-psabi
)

add_library(astcenc SHARED
    Source/astcenc_averages_and_directions.cpp
    Source/astcenc_block_sizes2.cpp
    Source/astcenc_color_quantize.cpp
    Source/astcenc_color_unquantize.cpp
    Source/astcenc_compress_symbolic.cpp
    Source/astcenc_compute_variance.cpp
    Source/astcenc_decompress_symbolic.cpp
    Source/astcenc_encoding_choice_error.cpp
    Source/astcenc_entry.cpp
    Source/astcenc_find_best_partitioning.cpp
    Source/astcenc_ideal_endpoints_and_weights.cpp
    Source/astcenc_image.cpp
    Source/astcenc_integer_sequence.cpp
    Source/astcenc_kmeans_partitioning.cpp
    Source/astcenc_mathlib.cpp
    Source/astcenc_mathlib_softfloat.cpp
    Source/astcenc_partition_tables.cpp
    Source/astcenc_percentile_tables.cpp
    Source/astcenc_pick_best_endpoint_format.cpp
    Source/astcenc_platform_isa_detection.cpp
    Source/astcenc_quantization.cpp
    Source/astcenc_symbolic_physical.cpp
    Source/astcenc_weight_align.cpp
    Source/astcenc_weight_quant_xfer_tables.cpp
)
target_include_directories(astcenc PUBLIC
	$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_features(astcenc PUBLIC cxx_auto_type)
set_target_properties(astcenc PROPERTIES
	VERSION ${PROJECT_VERSION}
	SOVERSION 0d
	CXX_VISIBILITY_PRESET "hidden"
	VISIBILITY_INLINES_HIDDEN ON
	INTERPROCEDURAL_OPTIMIZATION ON
)
target_link_libraries(astcenc PRIVATE astcenc_common)
add_library(astcenc::astcenc ALIAS astcenc)

add_executable(astcenccli
    Source/astcenccli_error_metrics.cpp
    Source/astcenccli_image.cpp
    Source/astcenccli_image_load_store.cpp
    Source/astcenccli_platform_dependents.cpp
    Source/astcenccli_toplevel.cpp
    Source/astcenccli_toplevel_help.cpp
    Source/astcenc_mathlib.cpp
    Source/astcenc_mathlib_softfloat.cpp
)
target_link_libraries(astcenccli PRIVATE astcenc tinyexr::tinyexr stb Threads::Threads astcenc_common)
set_target_properties(astcenccli PROPERTIES OUTPUT_NAME "astcenc")

configure_package_config_file(debian/extra/config.cmake.in astcenc-config.cmake
	INSTALL_DESTINATION ${CMAKE_INSTAL_LIBDIR}/cmake/astcenc
	NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
install(TARGETS astcenc EXPORT astcenc-targets DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS astcenccli DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES Source/astcenc.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT astcenc-targets NAMESPACE astcenc:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/astcenc)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/astcenc-config.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/astcenc)

