# 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_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 off turns every one of the
# others off, on every configure.
option(
	VITRIO_BUILD_DEPENDENCIES
	"Build the dependencies with vitrio, rather than finding installed ones"
	ON
)
cmake_dependent_option(
	VITRIO_BUILD_BOOST "Fetch Boost and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_SPDLOG "Fetch spdlog and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_ZLIB "Fetch zlib and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_TIFF "Fetch libtiff and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_LIBURING "Fetch liburing and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_NLOHMANN_JSON "Fetch nlohmann/json and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_ZSTD "Fetch zstd and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)
cmake_dependent_option(
	VITRIO_BUILD_BLOSC "Fetch c-blosc and build it with vitrio" ON
	"VITRIO_BUILD_DEPENDENCIES" OFF
)

# 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_BUILD_BOOST)
	include(vitrio_fetch_boost)
	vitrio_fetch_boost(
		TAG boost-1.92.0
		SHA256 9bed76128d4e46755dbe818487788c6fceb6f72b378f4daa49b7e1e600d9088d
	)
else()
	find_package(Boost 1.70 REQUIRED CONFIG COMPONENTS filesystem)
endif()

if(VITRIO_BUILD_SPDLOG)
	include(vitrio_fetch_spdlog)
	vitrio_fetch_spdlog(
		COMMIT 79524ddd08a4ec981b7fea76afd08ee05f83755d
	) # v1.17.0
else()
	find_package(spdlog 1.4 REQUIRED CONFIG)
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_BUILD_ZLIB)
	include(vitrio_fetch_zlib)
	vitrio_fetch_zlib(
		COMMIT da607da739fa6047df13e66a2af6b8bec7c2a498
	) # v1.3.2
elseif(VITRIO_NEEDS_ZLIB)
	find_package(ZLIB REQUIRED)
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_BUILD_TIFF)
	include(vitrio_fetch_tiff)
	vitrio_fetch_tiff(
		COMMIT d01a94be176f5f6a87f7ee1c0b32e65416aa2b4d
	) # v4.7.2
elseif(VITRIO_ENABLE_TIFF)
	find_package(TIFF 4.5 REQUIRED)
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_BUILD_NLOHMANN_JSON)
	include(vitrio_fetch_nlohmann_json)
	vitrio_fetch_nlohmann_json(
		COMMIT 55f93686c01528224f448c19128836e7df245f72
	) # v3.12.0
elseif(VITRIO_ENABLE_ZARR)
	find_package(nlohmann_json 3.6 REQUIRED CONFIG)
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_BUILD_ZSTD)
	include(vitrio_fetch_zstd)
	vitrio_fetch_zstd(
		COMMIT f8745da6ff1ad1e7bab384bd1f9d742439278e99
	) # v1.5.7
elseif(VITRIO_ENABLE_ZARR)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(zstd REQUIRED IMPORTED_TARGET libzstd>=1.4)
	add_library(zstd::zstd ALIAS PkgConfig::zstd)
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_BUILD_BLOSC)
	include(vitrio_fetch_blosc)
	vitrio_fetch_blosc(
		ZSTD zstd::zstd
		ZLIB ZLIB::ZLIB
		COMMIT 616f4b7343a8479f7e71dd3d7025bd92c9a6bbd0
	) # v1.21.6
elseif(VITRIO_ENABLE_ZARR)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(blosc REQUIRED IMPORTED_TARGET blosc>=1.14)
	add_library(blosc::blosc ALIAS PkgConfig::blosc)
endif()

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