
cmake_minimum_required(VERSION 3.20...3.30)
project(pyxcp_extensions LANGUAGES C CXX)


if(POLICY CMP0135)
  cmake_policy(SET CMP0135 NEW)
endif()
cmake_policy(SET CMP0094 NEW)

find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
find_package(pybind11 CONFIG REQUIRED)

SET(CMAKE_C_STANDARD 17)
# C++23 is needed for the standard floating-point types used by the recorder.
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

message( STATUS "Found pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}: ${pybind11_INCLUDE_DIRS}")



# Note: mtune=native optimizes for build machine, may not be portable
# Removed mtune=native for better binary portability
SET(GCC_N_CLANG_BASE_OPTIONS "-Wall -Wextra -Wpedantic -Warray-bounds -fexceptions")

SET(MSVC_BASE_OPTIONS "/W3 /permissive- /EHsc /bigobj /Zc:__cplusplus /std:c++latest")



if (CMAKE_BUILD_TYPE STREQUAL "Debug")
	if (MSVC)
		SET(MSVC_BASE_OPTIONS "${MSVC_BASE_OPTIONS}  /Od /fsanitize=address /Zi")
	else()
		SET(GCC_N_CLANG_BASE_OPTIONS "${GCC_N_CLANG_BASE_OPTIONS} -Og -g3 -ggdb -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined -fsanitize=bounds")	# -fsanitize=hwaddress
	endif()
else ()
	if (MSVC)
		SET(MSVC_BASE_OPTIONS "${MSVC_BASE_OPTIONS} /Ox")
	else()
		SET(GCC_N_CLANG_BASE_OPTIONS "${GCC_N_CLANG_BASE_OPTIONS} -O3 -fomit-frame-pointer")
	endif()
 endif ()


if (CMAKE_SYSTEM_NAME STREQUAL  "Darwin")
    set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0")
	SET(GCC_N_CLANG_EXTRA_OPTIONS "-stdlib=libc++")
	message("Platform is Darwin")
elseif (CMAKE_SYSTEM_NAME STREQUAL  "Windows")
	message("Platform is WINDOWS")
	SET(MSVC_EXTRA_OPTIONS "")
elseif (CMAKE_SYSTEM_NAME STREQUAL  "Linux")
	SET(GCC_N_CLANG_EXTRA_OPTIONS "-fvisibility=hidden -g0") # -fcoroutines
	message("Platform is LINUX")
endif()


IF (CMAKE_C_COMPILER_ID STREQUAL "GNU")

ELSEIF (CMAKE_C_COMPILER_ID MATCHES "Clang")

ELSEIF (CMAKE_C_COMPILER_ID MATCHES "MSVC")

ELSE ()

ENDIF ()

IF (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

ELSEIF (CMAKE_CXX_COMPILER_ID MATCHES "Clang")

ELSEIF (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")

ELSE ()


ENDIF ()

message("Compiling C with: " ${CMAKE_C_COMPILER_ID})
message("Compiling Cpp with: " ${CMAKE_CXX_COMPILER_ID})

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(EXTENSION_INCS ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/cpp_ext)

function(configure_python_extension target package_dir)
    set(output_dir "${CMAKE_CURRENT_SOURCE_DIR}/${package_dir}")
    set_target_properties(
        ${target}
        PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY "${output_dir}"
        RUNTIME_OUTPUT_DIRECTORY "${output_dir}"
    )

    foreach(config ${CMAKE_CONFIGURATION_TYPES})
        string(TOUPPER "${config}" config_upper)
        set_target_properties(
            ${target}
            PROPERTIES
            LIBRARY_OUTPUT_DIRECTORY_${config_upper} "${output_dir}"
            RUNTIME_OUTPUT_DIRECTORY_${config_upper} "${output_dir}"
        )
    endforeach()
endfunction()

function(install_python_extension target package_dir)
    install(
        TARGETS ${target}
        LIBRARY DESTINATION ${package_dir}
        RUNTIME DESTINATION ${package_dir}
    )
endfunction()

pybind11_add_module(rekorder pyxcp/recorder/wrap.cpp pyxcp/recorder/lz4.c pyxcp/recorder/lz4hc.c)
pybind11_add_module(cpp_ext pyxcp/cpp_ext/extension_wrapper.cpp)
pybind11_add_module(stim pyxcp/daq_stim/stim_wrapper.cpp pyxcp/daq_stim/stim.cpp pyxcp/daq_stim/scheduler.cpp)
pybind11_add_module(transport_ext pyxcp/transport/transport_wrapper.cpp pyxcp/recorder/lz4.c pyxcp/recorder/lz4hc.c)

# Native asynchronous Ethernet backends share the same Python extension module.
if (WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    set(PYXCP_ETH_EXT_DEFAULT ON)
else()
    set(PYXCP_ETH_EXT_DEFAULT OFF)
endif()
option(PYXCP_BUILD_ETH_EXT "Build the native asynchronous Ethernet backend (IOCP, epoll, or kqueue)" ${PYXCP_ETH_EXT_DEFAULT})
if (PYXCP_BUILD_ETH_EXT)
    if (WIN32)
        pybind11_add_module(eth_ext pyxcp/transport/eth_ext/ethernet_wrapper.cpp pyxcp/transport/eth_ext/win/iocp.cpp pyxcp/transport/eth_ext/poolmgr.cpp pyxcp/transport/eth_ext/utils.cpp)
        target_include_directories(eth_ext PRIVATE ${EXTENSION_INCS} ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport/eth_ext ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport/eth_ext/win)
    elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
        find_package(Threads REQUIRED)
        pybind11_add_module(eth_ext pyxcp/transport/eth_ext/linux/ethernet_wrapper.cpp pyxcp/transport/eth_ext/linux/epoll.cpp pyxcp/transport/eth_ext/utils.cpp)
        target_include_directories(eth_ext PRIVATE ${EXTENSION_INCS} ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport/eth_ext ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport/eth_ext/linux)
        target_link_libraries(eth_ext PRIVATE Threads::Threads)
    elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
        find_package(Threads REQUIRED)
        pybind11_add_module(eth_ext pyxcp/transport/eth_ext/kqueue/ethernet_wrapper.cpp pyxcp/transport/eth_ext/kqueue/kqueue.cpp pyxcp/transport/eth_ext/utils.cpp)
        target_include_directories(eth_ext PRIVATE ${EXTENSION_INCS} ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport/eth_ext ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport/eth_ext/kqueue)
        target_link_libraries(eth_ext PRIVATE Threads::Threads)
    else()
        message(FATAL_ERROR "PYXCP_BUILD_ETH_EXT is supported only on Windows (IOCP), Linux (epoll), macOS, and FreeBSD (kqueue)")
    endif()
endif()

target_include_directories(rekorder PRIVATE ${EXTENSION_INCS} ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/recorder)
target_include_directories(cpp_ext PRIVATE ${EXTENSION_INCS})
target_include_directories(stim PRIVATE ${EXTENSION_INCS} ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/daq_stim)
target_include_directories(transport_ext PRIVATE ${EXTENSION_INCS} ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/transport ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/cpp_ext ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/recorder)

if (WIN32)
    target_link_libraries(cpp_ext PRIVATE Ws2_32.lib)
    target_link_libraries(transport_ext PRIVATE Ws2_32.lib)
endif()

target_compile_options(rekorder PUBLIC "-DEXTENSION_NAME=pyxcp.recorder.rekorder")
target_compile_options(cpp_ext PUBLIC "-DEXTENSION_NAME=pyxcp.cpp_ext.cpp_ext")
target_compile_options(stim PUBLIC "-DEXTENSION_NAME=pyxcp.daq_stim.stim")
target_compile_options(transport_ext PUBLIC "-DEXTENSION_NAME=pyxcp.transport.transport_ext")
if (PYXCP_BUILD_ETH_EXT)
target_compile_options(eth_ext PUBLIC "-DEXTENSION_NAME=pyxcp.transport.eth_ext")
endif()

add_executable(asamkeydll ${CMAKE_CURRENT_SOURCE_DIR}/pyxcp/asamkeydll.c)
if (CMAKE_SYSTEM_NAME STREQUAL  "Linux")
	target_link_libraries(asamkeydll PRIVATE dl)
endif()

configure_python_extension(rekorder pyxcp/recorder)
configure_python_extension(cpp_ext pyxcp/cpp_ext)
configure_python_extension(stim pyxcp/daq_stim)
configure_python_extension(transport_ext pyxcp/transport)
if (PYXCP_BUILD_ETH_EXT)
configure_python_extension(eth_ext pyxcp/transport)
endif()
configure_python_extension(asamkeydll pyxcp/cpp_ext)


if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    # CMAKE_SYSTEM_NAME STREQUAL  "Windows"
endif()

IF (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
	SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_N_CLANG_BASE_OPTIONS} ${GCC_N_CLANG_EXTRA_OPTIONS}")
	target_link_options(cpp_ext PUBLIC -flto=auto)
	target_link_options(stim PUBLIC -flto=auto)
	target_link_options(rekorder PUBLIC -flto=auto)
	target_link_options(transport_ext PUBLIC -flto=auto)
	if (PYXCP_BUILD_ETH_EXT)
	target_link_options(eth_ext PUBLIC -flto=auto)
	endif()
ELSEIF (CMAKE_C_COMPILER_ID MATCHES "MSVC")
	SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${MSVC_BASE_OPTIONS} ${MSVC_EXTRA_OPTIONS}")
ENDIF()

IF (CMAKE_SYSTEM_NAME STREQUAL  "Linux" AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
	# SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}" -fuse-ld=lld)
ENDIF()

# target_include_directories(preprocessor PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/pya2l/extensions>)
# target_link_libraries(preprocessor pybind11::headers)
# set_target_properties(preprocessor PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON CXX_VISIBILITY_PRESET ON VISIBILITY_INLINES_HIDDEN ON)

install_python_extension(rekorder pyxcp/recorder)
install_python_extension(cpp_ext pyxcp/cpp_ext)
install_python_extension(stim pyxcp/daq_stim)
install_python_extension(transport_ext pyxcp/transport)
if (PYXCP_BUILD_ETH_EXT)
install_python_extension(eth_ext pyxcp/transport)
endif()
install_python_extension(asamkeydll pyxcp/cpp_ext)
