# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------

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

set(seccomp_download_dependent FALSE)
if (IS_DIRECTORY "${CANN_3RD_LIB_PATH}/../tools/minios/arm64/include/libseccomp")
    set(LIB_SECCOMP_INCLUDE_PATH "${CANN_3RD_LIB_PATH}/../tools/minios/arm64/include/libseccomp")
elseif (EXISTS "${CANN_3RD_LIB_PATH}/lib_cache/libseccomp-2.5.4/include/seccomp.h")
    message("seccomp use opensource cache.")
    set(LIB_SECCOMP_INCLUDE_PATH "${CANN_3RD_LIB_PATH}/lib_cache/libseccomp-2.5.4/include")
elseif (EXISTS "/usr/local/include/seccomp.h")
    set(LIB_SECCOMP_INCLUDE_PATH "/usr/local/include")
else()
    set(REQ_URL "${CANN_3RD_LIB_PATH}/libseccomp-2.5.4/libseccomp-2.5.4.tar.gz")
    if(EXISTS ${REQ_URL})
        message(STATUS "[UDF seccomp] ${REQ_URL} found in local")
    elseif(EXISTS "${CANN_3RD_LIB_PATH}/libseccomp-2.5.4")
        message(STATUS "[UDF seccomp] ${CANN_3RD_LIB_PATH}/libseccomp-2.5.4 found in local")
        set(REQ_URL "${CANN_3RD_LIB_PATH}/libseccomp-2.5.4")
    else()
        message(STATUS "[UDF seccomp] libseccomp-2.5.4 not found, download.")
        set(REQ_URL "https://gitcode.com/cann-src-third-party/libseccomp/releases/download/v2.5.4/libseccomp-2.5.4.tar.gz")
    endif()
    include(ExternalProject)
    ExternalProject_Add(external_seccomp
            URL               ${REQ_URL}
            TLS_VERIFY        OFF
            DOWNLOAD_DIR      ${CMAKE_BINARY_DIR}/downloads
            PREFIX            third_party
            CONFIGURE_COMMAND bash -c "cd <SOURCE_DIR> && test -f include/seccomp.h || (./autogen.sh && ./configure --prefix=<INSTALL_DIR>)"
            INSTALL_COMMAND   ""
            BUILD_COMMAND     ""
            )

    ExternalProject_Get_Property(external_seccomp SOURCE_DIR)
    set(LIB_SECCOMP_INCLUDE_PATH ${SOURCE_DIR}/include)
    set(seccomp_download_dependent TRUE)
endif()

add_library(seccomp_headers INTERFACE)
set_target_properties(seccomp_headers PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${LIB_SECCOMP_INCLUDE_PATH}"
        )
if (seccomp_download_dependent)
    add_dependencies(seccomp_headers external_seccomp)
endif()

include(CMakePrintHelpers)
cmake_print_properties(TARGETS seccomp_headers
        PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
        )