# SPDX-License-Identifier: LGPL-2.1-or-later

cmake_minimum_required(VERSION 3.18)

# Without this CMAKE_MSVC_DEBUG_INFORMATION_FORMAT is silently ignored.
if(POLICY CMP0141)
	cmake_policy(SET CMP0141 NEW)
endif()

# What a fetched archive holds takes the time it is extracted at, so that a
# new archive always rebuilds what came from it.
if(POLICY CMP0135)
	cmake_policy(SET CMP0135 NEW)
endif()

file(READ ${CMAKE_CURRENT_SOURCE_DIR}/VERSION PROJECT_VERSION)
string(STRIP "${PROJECT_VERSION}" PROJECT_VERSION)

project(
	vitrio
	VERSION ${PROJECT_VERSION}
	DESCRIPTION
		"High-throughput image I/O for Cryo-Electron Microscopy (CryoEM)"
	LANGUAGES CXX
)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

include(CTest)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(CMakeDependentOption)
include(GenerateExportHeader)
include(vitrio_enable_warnings)

option(VITRIO_INSTALL "Generate vitrio's install rules" ON)
option(
	VITRIO_ENABLE_TIFF 
	"Read and write TIFF and EER files, which needs libtiff"
	ON
)
option(
	VITRIO_ENABLE_ZARR
	"Read ZARR arrays, which needs nlohmann/json, c-blosc, zstd and zlib"
	ON
)
option(
	VITRIO_ENABLE_GZIP
	"Read MRC files compressed with gzip, such as .map.gz, which needs zlib"
	ON
)
cmake_dependent_option(
	VITRIO_ENABLE_IO_URING
	"Read the values of raw image files through io_uring where the kernel \
offers it, which needs liburing"
	ON
	"CMAKE_SYSTEM_NAME STREQUAL Linux" OFF
)
option(
	VITRIO_ENABLE_SIMD
	"Convert half precision values with the vector units of the processor, \
which needs Google Highway"
	ON
)
set(
	VITRIO_SIMD_DISPATCH DYNAMIC CACHE STRING
	"DYNAMIC compiles the vector loops once per instruction set they are \
asked for and picks one as the library runs; STATIC compiles them once, for \
the instruction set the compiler flags allow"
)
set_property(CACHE VITRIO_SIMD_DISPATCH PROPERTY STRINGS DYNAMIC STATIC)
if(NOT VITRIO_SIMD_DISPATCH MATCHES "^(DYNAMIC|STATIC)$")
	message(
		FATAL_ERROR
		"VITRIO_SIMD_DISPATCH is ${VITRIO_SIMD_DISPATCH}; it is DYNAMIC or STATIC"
	)
endif()
include(vitrio_simd_targets)
vitrio_simd_default_targets(VITRIO_SIMD_DEFAULT_TARGETS)
set(
	VITRIO_SIMD_TARGETS "${VITRIO_SIMD_DEFAULT_TARGETS}" CACHE STRING
	"The instruction sets the vector loops are compiled for beyond the one \
the compiler flags allow, by Highway's names without HWY_, such as AVX2;AVX3. \
Read only with VITRIO_SIMD_DISPATCH=DYNAMIC"
)
# Checked here, so that a name outside the table stops the configure before
# anything is fetched.
vitrio_simd_disabled_targets(
	VITRIO_SIMD_DISABLED_TARGETS
	${VITRIO_SIMD_TARGETS}
)
option(VITRIO_BUILD_PYTHON "Build the Python package, which needs nanobind" OFF)
option(
	VITRIO_PYTHON_STABLE_ABI
	"Build the extension of the Python package against the stable ABI, so \
that one build serves Python 3.12 and newer. Needs CMake 3.26"
	OFF
)
# Only while BUILD_TESTING, the switch of CTest, is on: tests built without
# it could not be registered with CTest.
cmake_dependent_option(
	VITRIO_BUILD_TESTING "Build vitrio's tests" ON
	"BUILD_TESTING" OFF
)
option(
	VITRIO_BUILD_EXAMPLES
	"Build the C++ examples of examples/cpp"
	OFF
)
option(
	VITRIO_BUILD_BENCHMARKS
	"Build the C++ benchmarks of benchmarks/cpp, which need Google Benchmark"
	OFF
)
option(
	VITRIO_BUILD_DOCS
	"Add the targets that build the documentation of the API, which needs \
Doxygen, and with VITRIO_BUILD_PYTHON that of the Python package, which \
needs Sphinx"
	OFF
)
option(
	VITRIO_REGISTER_TESTS_PER_BINARY
	"Register one test per test executable instead of one per Catch2 case"
	OFF
)
option(
	VITRIO_CHECK_HEADERS
	"Compile every public header on its own under each C++ standard the \
compiler supports"
	OFF
)

# Every dependency is private and none appears in a public header, so building
# one here or finding an installed one changes nothing for a consumer of the
# installed library. Turning the global switch on turns every one of the
# others on, on every configure.
option(
	VITRIO_USE_SYSTEM_DEPENDENCIES
	"Find every dependency installed, rather than fetching it and building it \
with vitrio"
	OFF
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_BOOST
	"Find an installed Boost rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_SPDLOG
	"Find an installed spdlog rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_ZLIB
	"Find an installed zlib rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_TIFF
	"Find an installed libtiff rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_LIBURING
	"Find an installed liburing rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_NLOHMANN_JSON
	"Find an installed nlohmann/json rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_ZSTD
	"Find an installed zstd rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_BLOSC
	"Find an installed c-blosc rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)
cmake_dependent_option(
	VITRIO_USE_SYSTEM_HIGHWAY
	"Find an installed Google Highway rather than fetching it"
	OFF
	"NOT VITRIO_USE_SYSTEM_DEPENDENCIES"
	ON
)

# The library and its public headers are C++14. The standard is required
# rather than preferred, so that nothing newer gets in unnoticed.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Windows looks for a library beside the executable that needs it, and has no
# other way to be told where one is. So the library, the libraries it needs
# in turn and the test executables are all built into one directory.
if(WIN32 AND NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
endif()

find_package(Threads REQUIRED)

if(VITRIO_USE_SYSTEM_BOOST)
	find_package(Boost 1.70 REQUIRED CONFIG COMPONENTS filesystem)
else()
	include(vitrio_fetch_boost)
	vitrio_fetch_boost(
		TAG boost-1.92.0
		SHA256 9bed76128d4e46755dbe818487788c6fceb6f72b378f4daa49b7e1e600d9088d
	)
endif()

if(VITRIO_USE_SYSTEM_SPDLOG)
	find_package(spdlog 1.4 REQUIRED CONFIG)
else()
	include(vitrio_fetch_spdlog)
	vitrio_fetch_spdlog(
		COMMIT 79524ddd08a4ec981b7fea76afd08ee05f83755d
	) # v1.17.0
endif()

# zlib is libtiff's, decodes the gzip and zlib chunks of ZARR arrays and
# inflates the MRC files compressed with gzip. libtiff and c-blosc look it up
# themselves, so zlib comes first: built here, it is the one they find.
if(VITRIO_ENABLE_TIFF OR VITRIO_ENABLE_ZARR OR VITRIO_ENABLE_GZIP)
	set(VITRIO_NEEDS_ZLIB ON)
else()
	set(VITRIO_NEEDS_ZLIB OFF)
endif()

if(VITRIO_NEEDS_ZLIB AND VITRIO_USE_SYSTEM_ZLIB)
	find_package(ZLIB REQUIRED)
elseif(VITRIO_NEEDS_ZLIB)
	include(vitrio_fetch_zlib)
	vitrio_fetch_zlib(
		COMMIT da607da739fa6047df13e66a2af6b8bec7c2a498
	) # v1.3.2
endif()

# The handlers a file is opened with, which keep its errors apart from those
# of every other file, arrived in libtiff 4.5.
if(VITRIO_ENABLE_TIFF AND VITRIO_USE_SYSTEM_TIFF)
	find_package(TIFF 4.5 REQUIRED)
elseif(VITRIO_ENABLE_TIFF)
	include(vitrio_fetch_tiff)
	vitrio_fetch_tiff(
		COMMIT d01a94be176f5f6a87f7ee1c0b32e65416aa2b4d
	) # v4.7.2
endif()

# Only the ZARR format needs nlohmann/json, zstd and c-blosc. contains(),
# which tells a key that is absent, arrived in nlohmann/json 3.6.
if(VITRIO_ENABLE_ZARR AND VITRIO_USE_SYSTEM_NLOHMANN_JSON)
	find_package(nlohmann_json 3.6 REQUIRED CONFIG)
elseif(VITRIO_ENABLE_ZARR)
	include(vitrio_fetch_nlohmann_json)
	vitrio_fetch_nlohmann_json(
		COMMIT 55f93686c01528224f448c19128836e7df245f72
	) # v3.12.0
endif()

# zstd is found through pkg-config, since the targets its CMake package names
# changed between releases. Built here, it is the one c-blosc links, so that
# its functions are defined once.
if(VITRIO_ENABLE_ZARR AND VITRIO_USE_SYSTEM_ZSTD)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(zstd REQUIRED IMPORTED_TARGET libzstd>=1.4)
	add_library(zstd::zstd ALIAS PkgConfig::zstd)
elseif(VITRIO_ENABLE_ZARR)
	include(vitrio_fetch_zstd)
	vitrio_fetch_zstd(
		COMMIT f8745da6ff1ad1e7bab384bd1f9d742439278e99
	) # v1.5.7
endif()

# c-blosc installs a file for pkg-config rather than a CMake package. Built
# here, it takes the zstd and zlib above rather than copies of its own.
if(VITRIO_ENABLE_ZARR AND VITRIO_USE_SYSTEM_BLOSC)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(blosc REQUIRED IMPORTED_TARGET blosc>=1.14)
	add_library(blosc::blosc ALIAS PkgConfig::blosc)
elseif(VITRIO_ENABLE_ZARR)
	include(vitrio_fetch_blosc)
	vitrio_fetch_blosc(
		ZSTD zstd::zstd
		ZLIB ZLIB::ZLIB
		COMMIT 616f4b7343a8479f7e71dd3d7025bd92c9a6bbd0
	) # v1.21.6
endif()

# liburing installs a file for pkg-config rather than a CMake package.
if(VITRIO_ENABLE_IO_URING AND VITRIO_USE_SYSTEM_LIBURING)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(liburing REQUIRED IMPORTED_TARGET liburing>=2.0)
	add_library(liburing::liburing ALIAS PkgConfig::liburing)
elseif(VITRIO_ENABLE_IO_URING)
	include(vitrio_fetch_liburing)
	vitrio_fetch_liburing(
		COMMIT d41bf9220ec39277ff235379e9089d9e0fd6c2a5
	) # liburing-2.15
endif()

# Highway installs a CMake package, hwy, with the target hwy::hwy. The build
# may be asked for any of a table of its targets, and 1.2.0 is the first
# release that names every one of them.
if(VITRIO_ENABLE_SIMD AND VITRIO_USE_SYSTEM_HIGHWAY)
	find_package(hwy 1.2.0 REQUIRED CONFIG)
elseif(VITRIO_ENABLE_SIMD)
	include(vitrio_fetch_highway)
	vitrio_fetch_highway(
		COMMIT 2607d3b5b0113992fe84d3848859eae13b3b52c1
	) # 1.4.0
endif()

# nanobind ships its CMake package inside its Python package, so the
# interpreter that has it is asked where that is.
if(VITRIO_BUILD_PYTHON)
	set(VITRIO_PYTHON_COMPONENTS Interpreter Development.Module)
	if(VITRIO_PYTHON_STABLE_ABI)
		list(APPEND VITRIO_PYTHON_COMPONENTS Development.SABIModule)
	endif()

	find_package(
		Python 3.10 REQUIRED
		COMPONENTS ${VITRIO_PYTHON_COMPONENTS}
	)

	if(NOT nanobind_ROOT AND NOT nanobind_DIR)
		execute_process(
			COMMAND ${Python_EXECUTABLE} -m nanobind --cmake_dir
			OUTPUT_VARIABLE nanobind_ROOT
			OUTPUT_STRIP_TRAILING_WHITESPACE
			ERROR_QUIET
		)
	endif()

	find_package(nanobind 3.0 REQUIRED CONFIG)
endif()

add_subdirectory(src)

if(VITRIO_BUILD_PYTHON)
	add_subdirectory(python)
endif()

if(VITRIO_BUILD_TESTING)
	add_subdirectory(tests)
endif()

if(VITRIO_BUILD_EXAMPLES)
	add_subdirectory(examples)
endif()

if(VITRIO_BUILD_BENCHMARKS)
	add_subdirectory(benchmarks)
endif()

if(VITRIO_CHECK_HEADERS)
	add_subdirectory(tests/headers)
endif()

if(VITRIO_BUILD_DOCS)
	add_subdirectory(docs)
endif()
