# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.

project(nvsipl_query)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -frtti")

set(SIPL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../include")
set(SIPL_QUERY_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../include/query/include")

include_directories(
    ${SIPL_INCLUDE_DIR}
    ${SIPL_QUERY_INCLUDE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
)

find_library(NVSIPL_QUERY_LIB nvsipl_query
    HINTS /usr/lib/aarch64-linux-gnu/nvidia/
)

find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
    pkg_check_modules(JSONCPP jsoncpp)
endif()

if(NVSIPL_QUERY_LIB)
    add_executable(${PROJECT_NAME} main.cpp)

    target_link_libraries(${PROJECT_NAME}
        ${NVSIPL_QUERY_LIB}
        stdc++
    )

    if(JSONCPP_FOUND)
        target_include_directories(${PROJECT_NAME} PRIVATE ${JSONCPP_INCLUDE_DIRS})
        target_link_libraries(${PROJECT_NAME} ${JSONCPP_LIBRARIES})
        target_compile_definitions(${PROJECT_NAME} PRIVATE HAS_JSONCPP=1)
    else()
        message(STATUS "jsoncpp not found — --output-json will not be available")
    endif()

    install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION /usr/sbin
        COMPONENT applications
    )
else()
    message(WARNING "nvsipl_query library not found. Skipping ${PROJECT_NAME}.")
endif()
