# This file is part of the PyHDRL Python language bindings
# Copyright (C) 2003-2024 European Southern Observatory
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.12)
project(hdrl)

# Set policies
cmake_policy(VERSION 3.12)
# Safer library linking
cmake_policy(SET CMP0003 NEW)
# Automatically escape preprocessor definitions
cmake_policy(SET CMP0005 NEW)
# Make syntax problems errors
cmake_policy(SET CMP0010 NEW)
# Input directories must have CMakeLists.txt
cmake_policy(SET CMP0014 NEW)
# Do not interpret quoted or bracketed variables in if statments
cmake_policy(SET CMP0054 NEW)
# Usage of <PackageName>_ROOT variables
cmake_policy(SET CMP0074 NEW)
# Escape RPATH entries in intermediate scripts
cmake_policy(SET CMP0095 NEW)
# Deprecated FindPythonInterp and FindPythonLibs
# CMP0148 is only defined in cmake 3.27+
if(POLICY CMP0148)
    cmake_policy(SET CMP0148 NEW)
endif()

# Add local cmake module search path
list(INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/cmake")

find_package(PkgConfig)
find_package(CPL "7.2" REQUIRED COMPONENTS cplcore cplui cpldfs cpldrs)
find_package(HDRL "1.6" REQUIRED COMPONENTS hdrl)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED)

pybind11_add_module(hdrl src/pyhdrl.cpp
 src/dump_handler.cpp
 src/hdrlcore/error_bindings.cpp
 src/hdrlcore/error.cpp
 src/hdrlcore/errorframe.cpp
 src/hdrlcore/image_bindings.cpp
 src/hdrlcore/image.cpp
 src/hdrlcore/imagelist_bindings.cpp
 src/hdrlcore/imagelist.cpp
 src/hdrlcore/parameter.cpp
 src/hdrlcore/pycpl_types.cpp
 src/hdrldebug/types_bindings.cpp
 src/hdrldebug/types.cpp
 src/hdrlfunc/airmass_bindings.cpp
 src/hdrlfunc/airmass.cpp
 src/hdrlfunc/barycorr_bindings.cpp
 src/hdrlfunc/barycorr.cpp
 src/hdrlfunc/bpm_bindings.cpp
 src/hdrlfunc/bpm.cpp
 src/hdrlfunc/catalogue_bindings.cpp
 src/hdrlfunc/catalogue.cpp
 src/hdrlfunc/collapse_bindings.cpp
 src/hdrlfunc/collapse.cpp
 src/hdrlfunc/dar_bindings.cpp
 src/hdrlfunc/dar.cpp
 src/hdrlfunc/flat_bindings.cpp
 src/hdrlfunc/flat.cpp
 src/hdrlfunc/fpn_bindings.cpp
 src/hdrlfunc/fpn.cpp
 src/hdrlfunc/fringe_bindings.cpp
 src/hdrlfunc/fringe.cpp
 src/hdrlfunc/lacosmic_bindings.cpp
 src/hdrlfunc/lacosmic.cpp
 src/hdrlfunc/maglim_bindings.cpp
 src/hdrlfunc/maglim.cpp
 src/hdrlfunc/overscan_bindings.cpp 
 src/hdrlfunc/overscan.cpp
 src/hdrlfunc/resample_bindings.cpp
 src/hdrlfunc/resample.cpp
 src/hdrlfunc/strehl_bindings.cpp
 src/hdrlfunc/strehl.cpp
 )

target_compile_definitions(hdrl PRIVATE 
    PYHDRL_VERSION=\"${PYHDRL_VERSION}\"
    HDRL_USE_EXPERIMENTAL
    HDRL_USE_PRIVATE
    )
target_compile_options(hdrl PRIVATE
    $<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:GNU>>:-pipe -g3 -ggdb -O0 -rdynamic -fno-inline -fno-builtin -pedantic -Wextra -Wall -W -Wcast-align -Winline -Wmissing-noreturn -Wpointer-arith -Wshadow -Wsign-compare -Wundef -Wunreachable-code -Wwrite-strings -Wmissing-field-initializers -Wmissing-format-attribute -Wno-odr>
    $<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:Clang,AppleClang>>:-pipe -g3 -O0 -fno-inline -fno-builtin -pedantic -Wextra -Wall -W -Wno-c99-extensions -Wcast-align -Winline -Wimplicit-function-declaration -Wmissing-noreturn -Wincompatible-pointer-types -Wpointer-arith -Wshadow -Wsign-compare -Wundef -Wunreachable-code -Wwrite-strings -Wmissing-field-initializers -Wmissing-format-attribute -Wno-odr>
    $<$<AND:$<BOOL:${SANITIZE}>,$<CXX_COMPILER_ID:GNU,Clang,AppleClang>>:-fsanitize=${SANITIZE} -fno-omit-frame-pointer>)
target_include_directories(hdrl BEFORE
    PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src"
    PUBLIC ${HDRL_INCLUDE_DIR}
        ${CPL_INCLUDE_DIR})
target_link_options(hdrl PRIVATE
    $<$<AND:$<BOOL:${SANITIZE}>,$<CXX_COMPILER_ID:GNU,Clang,AppleClang>>:-fsanitize=${SANITIZE}>)
target_link_libraries(hdrl PRIVATE HDRL::hdrl)

set_target_properties(hdrl PROPERTIES
    C_STANDARD 17
    C_STANDARD_REQUIRED YES
    C_EXTENSIONS OFF
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS OFF
    SKIP_BUILD_RPATH TRUE)
