cmake_minimum_required(VERSION 3.5)
cmake_policy(VERSION ${CMAKE_VERSION})
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)

set(DEV_DIR_REL_PATH ../../../..)
set(DEV_DIR_PATH ${DEV_DIR_REL_PATH} CACHE STRING "Dev directory")
get_filename_component(DEV_DIR_PATH ${DEV_DIR_PATH} ABSOLUTE)

set(PKG_NAME Demo)
set(SUBPKG_NAME Demo)
set(TARGET_NAME	Py)
set(TARGET_TYPE "PYBIND11")

set(${TARGET_NAME}_VMAJOR 1)
set(${TARGET_NAME}_VMINOR 0)
set(${TARGET_NAME}_VPATCH 0)

set(CONFIG "Release")

project(${TARGET_NAME} VERSION ${${TARGET_NAME}_VMAJOR}.${${TARGET_NAME}_VMINOR}.${${TARGET_NAME}_VPATCH})

set(SUBPKG_DIR_NAME	${SUBPKG_NAME})
set(SUBPKG_PATH		"${DEV_DIR_PATH}/${PKG_NAME}/${SUBPKG_DIR_NAME}")

set(LIB_PATH		"${SUBPKG_PATH}/libs")
set(BIN_PATH		"${SUBPKG_PATH}/bins")
set(SRC_PATH		"${SUBPKG_PATH}/src")
set(INCLUDE_PATH	"${SUBPKG_PATH}/includes")

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX ${SUBPKG_PATH} CACHE PATH "Installation directory" FORCE)
endif()

set(INSTALL_LIB_PATH		libs)
set(INSTALL_BIN_PATH		bins)
set(INSTALL_INCLUDE_PATH	includes)

set(MY_CMAKEPACKAGE_PATH	"${DEV_DIR_PATH}/CMakePackages")

list(APPEND CMAKE_PREFIX_PATH "${MY_CMAKEPACKAGE_PATH}")

add_subdirectory(${DEV_DIR_PATH}/third_party/pybind11 ${CMAKE_BINARY_DIR}/pybind11)

file(GLOB_RECURSE SRCS_C "${SRC_PATH}/${TARGET_NAME}/*.c")
file(GLOB_RECURSE SRCS_CPP "${SRC_PATH}/${TARGET_NAME}/*.cpp")
LIST(APPEND SRCS ${SRCS_C} ${SRCS_CPP})

pybind11_add_module(${TARGET_NAME} MODULE ${SRCS})

target_compile_definitions(${TARGET_NAME} PRIVATE PYTHON_IMPORT_NAME=${TARGET_NAME}_${CONFIG})

set_target_properties(${TARGET_NAME} PROPERTIES NO_SYSTEM_FROM_IMPORTED false)

set(CMAKE_BUILD_TYPE ${CONFIG})

set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)

target_compile_features(${TARGET_NAME} PUBLIC cxx_std_20)
set_target_properties(${TARGET_NAME} PROPERTIES CXX_EXTENSIONS OFF)

set_target_properties(${TARGET_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)

set (MY_FLAGS -DNDEBUG -O2 -Wall -Wextra $<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast> -pedantic -Wshadow $<$<AND:$<STREQUAL:${CMAKE_CXX_COMPILER_ID},GNU>,$<VERSION_GREATER_EQUAL:${CMAKE_CXX_COMPILER_VERSION},14>>:-Wnrvo> -fstrict-aliasing  -Wstrict-aliasing=1 -Wconversion -Wsign-conversion -Werror=narrowing -Warith-conversion)

include(CheckIPOSupported)
check_ipo_supported(RESULT IPO_SUPPORTED)
if(IPO_SUPPORTED)
	set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

target_compile_options(${TARGET_NAME} PRIVATE ${MY_FLAGS})

set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${LIB_PATH}/${CONFIG})
set_target_properties(${TARGET_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${LIB_PATH}/${CONFIG})
set_target_properties(${TARGET_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIB_PATH}/${CONFIG})
set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME ${TARGET_NAME}_${CONFIG})

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  ${TARGET_NAME}ConfigVersion.cmake
  VERSION "${${TARGET_NAME}_VMAJOR}.${${TARGET_NAME}_VMINOR}.${${TARGET_NAME}_VPATCH}"
  COMPATIBILITY AnyNewerVersion
)

set_target_properties(${TARGET_NAME} PROPERTIES VERSION ${${TARGET_NAME}_VMAJOR}.${${TARGET_NAME}_VMINOR}.${${TARGET_NAME}_VPATCH} SOVERSION ${${TARGET_NAME}_VMAJOR})

configure_package_config_file(
  "${DEV_DIR_REL_PATH}/CMake/ModuleConfig.cmake.in"
  ${TARGET_NAME}Config.cmake
  INSTALL_DESTINATION ${MY_CMAKEPACKAGE_PATH}
)

set(MY_PYTHONWRAPPERS_DIR_NAME	"MyPythonWrappers")
set(MY_PYTHONWRAPPERS_DIR_PATH	"${DEV_DIR_PATH}/${MY_PYTHONWRAPPERS_DIR_NAME}")

install(FILES ${LIB_PATH}/${CONFIG}/${TARGET_NAME}_${CONFIG}${PYTHON_MODULE_EXTENSION}
DESTINATION ${MY_PYTHONWRAPPERS_DIR_PATH})
