cmake_minimum_required(VERSION 3.24)
cmake_policy(SET CMP0048 NEW)
project(
  ${SKBUILD_PROJECT_NAME}
  VERSION ${SKBUILD_PROJECT_VERSION}
  LANGUAGES CXX)

option(BUILD_PYTHON_BINDING "Build Python Binding" off)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 17)

include(FetchContent)

# foonathan/memory v0.7-3
FetchContent_Declare(
  foonathan_memory
  GIT_REPOSITORY https://github.com/foonathan/memory.git
  GIT_TAG v0.7-3
  GIT_SHALLOW TRUE
  OVERRIDE_FIND_PACKAGE
)

# eProsima/Fast-CDR v2.1.3
FetchContent_Declare(
  fastcdr
  GIT_REPOSITORY https://github.com/eProsima/Fast-CDR.git
  GIT_TAG v2.1.3
  GIT_SHALLOW TRUE
  OVERRIDE_FIND_PACKAGE
)

# eProsima/Fast-DDS v2.13.1 (provides fastrtps and fastdds)
FetchContent_Declare(
  fastdds
  GIT_REPOSITORY https://github.com/eProsima/Fast-DDS.git
  GIT_TAG v2.13.1
  GIT_SHALLOW TRUE
  OVERRIDE_FIND_PACKAGE
)

# nlohmann/json v3.11.2
FetchContent_Declare(
  nlohmann_json
  GIT_REPOSITORY https://github.com/nlohmann/json.git
  GIT_TAG v3.11.2
  GIT_SHALLOW TRUE
  OVERRIDE_FIND_PACKAGE
)

FetchContent_MakeAvailable(foonathan_memory fastcdr fastdds nlohmann_json)

# Detect architecture for SDK static library
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64")
  set(SDK_ARCH "aarch64")
else()
  set(SDK_ARCH "x86_64")
endif()

add_library(booster_robotics_sdk STATIC IMPORTED)
set_target_properties(booster_robotics_sdk PROPERTIES
  IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${SDK_ARCH}/libbooster_robotics_sdk.a
)
target_include_directories(booster_robotics_sdk INTERFACE ${CMAKE_SOURCE_DIR}/include)

set(EXAMPLE_TARGETS
  b1_loco_example_client
  b1_arm_sdk_example_client
  b1_7dof_arm_sdk_example_client
  b1_low_level_publisher
  b1_low_level_subscriber
  low_level_hand_data_subscriber
  b1_low_sdk_example
  b1_7dof_arm_low_sdk_example
  odometer_example
  battery_state_subscriber
)

add_executable(b1_loco_example_client example/high_level/b1_loco_example_client.cpp)
add_executable(b1_arm_sdk_example_client example/high_level/b1_arm_sdk_example.cpp)
add_executable(b1_7dof_arm_sdk_example_client example/high_level/b1_7dof_arm_sdk_example.cpp)
add_executable(b1_low_level_publisher example/low_level/low_level_publisher.cpp)
add_executable(b1_low_level_subscriber example/low_level/low_level_subscriber.cpp)
add_executable(low_level_hand_data_subscriber example/low_level/low_level_hand_data_subscriber.cpp)
add_executable(b1_low_sdk_example example/low_level/b1_low_sdk_example.cpp)
add_executable(b1_7dof_arm_low_sdk_example example/low_level/b1_7dof_arm_low_sdk_example.cpp)
add_executable(odometer_example example/low_level/odometer_example.cpp)
add_executable(battery_state_subscriber example/low_level/battery_state_subscriber.cpp)

foreach(target ${EXAMPLE_TARGETS})
  target_link_libraries(${target} PRIVATE booster_robotics_sdk fastrtps fastcdr foonathan_memory nlohmann_json::nlohmann_json)
endforeach()

if(BUILD_PYTHON_BINDING)
    find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
    message(STATUS "Python3 executable: ${Python3_EXECUTABLE}")

    execute_process(
        COMMAND ${Python3_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_paths()['purelib'])"
        OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    include_directories(${Python3_INCLUDE_DIRS})
    find_package(pybind11 CONFIG REQUIRED)

    python3_add_library(booster_robotics_sdk_python MODULE python/binding.cpp WITH_SOABI)

    target_compile_definitions(booster_robotics_sdk_python PRIVATE VERSION_INFO=${PROJECT_VERSION})

    find_program(PYBIND11_STUBGEN_EXECUTABLE pybind11-stubgen)
    if(NOT PYBIND11_STUBGEN_EXECUTABLE)
        message(FATAL_ERROR "pybind11-stubgen not found")
    endif()

    add_custom_command(
        TARGET booster_robotics_sdk_python
        POST_BUILD
        COMMAND PYTHONPATH=${CMAKE_SOURCE_DIR}/build:/${PYTHONPATH} pybind11-stubgen -o ${CMAKE_SOURCE_DIR}/build booster_robotics_sdk_python
    )

    install(TARGETS booster_robotics_sdk_python LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES})
    install(FILES ${CMAKE_SOURCE_DIR}/build/booster_robotics_sdk_python.pyi DESTINATION ${PYTHON_SITE_PACKAGES})    
endif()

