# App CmakeLists

if(${USE_SGX} )

    if(${SGX_MODE} )
        set(Urts_Library_Name sgx_urts)
        # set(UAE_Server_Name sgx_uae_service)
    ELSE()
        set(Urts_Library_Name sgx_urts_sim)
        # set(UAE_Server_Name sgx_uae_service_sim)
    endif()
endif()

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

SET(CMAKE_C_FLAGS " -maes  -std=gnu99 -fPIC")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

set(APP_DIR ${CMAKE_CURRENT_SOURCE_DIR})

if(${USE_SGX} )
    set(EDL_FILES_PATH ${APP_DIR}/Enclave/edls)
    # 使用唯一的变量名避免与其他子目录冲突
    set(UDF_UNTRUSTED_BRIDGE ${APP_DIR}/Enclave_u.c ${APP_DIR}/Enclave_u.h)
    add_custom_command(OUTPUT ${UDF_UNTRUSTED_BRIDGE} PRE_BUILD
        COMMAND cd ${APP_DIR}/ && rm -f Enclave_u.*  && ${SGX_EDGER8R} --untrusted ${EDL_FILES_PATH}/Enclave.edl --search-path ${EDL_FILES_PATH} --search-path ${SGX_SDK}/include 
        COMMENT "make ucode"
    )
endif()



FILE(GLOB App_C_Files 
    "*.c"
    "decider/*.c"
)
FILE(GLOB App_Cpp_Files 
    "*.cpp"
    "decider/*.cpp"
)

# set(App_Cpp_Files "${App_Cpp_Files};../../../utils/EnclaveManager.cpp;../../../utils/config.cpp;../../../utils/TimeCounter.cpp")
set(App_Cpp_Files "${App_Cpp_Files};../../../utils/config.cpp;../../../utils/TimeCounter.cpp;../../../utils/EnclaveManager.cpp")
# file(GLOB_RECURSE App_Cpp_Files "*.cpp")   #将App文件夹中的cpp文件列表赋值给APP_CPP_FILES
# file(GLOB_RECURSE App_C_Files "*.c")   #将App文件夹中的cpp文件列表赋值给APP_CPP_FILES

# 在 find_package(OpenSSL REQUIRED) 之前添加：
if(NOT DEFINED ENCRYPTSQL_OPENSSL_INCLUDE_DIR)
    message(WARNING "未找到顶层传递的 OpenSSL 路径，重新查找")
    find_package(OpenSSL REQUIRED)
    set(OPENSSL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
else()
    set(OPENSSL_INCLUDE_DIR ${ENCRYPTSQL_OPENSSL_INCLUDE_DIR})
endif()

# 添加详细的路径验证
message(STATUS "SGX 模块使用的 OpenSSL 路径: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "Validating OpenSSL path: ${OPENSSL_INCLUDE_DIR}/openssl/evp.h")
if(NOT EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/evp.h")
    message(FATAL_ERROR "OpenSSL evp.h not found at ${OPENSSL_INCLUDE_DIR}/openssl/evp.h")
endif()

execute_process(
    COMMAND ls "${OPENSSL_INCLUDE_DIR}/openssl/evp.h"
    RESULT_VARIABLE EVP_RESULT
    OUTPUT_QUIET ERROR_QUIET
)
if(NOT EVP_RESULT EQUAL 0)
    message(FATAL_ERROR "evp.h 未找到！请检查路径: ${OPENSSL_INCLUDE_DIR}/openssl/evp.h")
endif()

include_directories(SYSTEM /usr/include)  # 添加在最前面

include_directories( 
    ${SGX_SDK}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../
    ${CMAKE_CURRENT_SOURCE_DIR}/../../symmetria
    ${APP_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/decider
    # 添加当前目录作为包含路径，确保能找到生成的Enclave_u.h
    ${CMAKE_CURRENT_BINARY_DIR}
)

# 单独添加 OpenSSL
include_directories(${OPENSSL_INCLUDE_DIR}/openssl)

set(App_C_Flags " -Wno-attributescmake")#设置编译App文件夹中C文件的文件选项

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${App_Cpp_Flags}")         #将CMake中编译CPP文件中的编译选项设置成我们自己的选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${App_C_Flags}")            #将将CMake中编译C文件中的编译选项设置成我们自己的选项
# set(CMAKE_EXE_LINKER_FLAGS "-L/usr/local/lib -lcrypto  -lpthread  -L${SGX_LIBRARY_PATH} -l${Urts_Library_Name}")   
set(CMAKE_CXX_STANDARD 17)   #设置C++的std，相当于makefile文件中的-std=c++11

#  target_include_directories(${SGX_LIBRARY_PATH})
if(${SGX_MODE} ) # for sim mode
    # set(uRTS_PATH "/usr/lib/x86_64-linux-gnu")
endif()


add_library(${Udf_Lib} SHARED ${App_Cpp_Files} ${App_C_Files} ${UDF_UNTRUSTED_BRIDGE})

if(${USE_SGX}) 
    target_link_libraries(${Udf_Lib} -L${SGX_LIBRARY_PATH} -Wl,--whole-archive -lsgx_uswitchless -lsgx_uprotected_fs -Wl,--no-whole-archive   -l${Urts_Library_Name}) 
endif()

target_link_libraries(${Udf_Lib} ${SYM_Lib} ${OPENSSL_LIBRARIES}  -lpthread  ${jsoncpp_LIBRARY})

if(${USE_SGX}) 
    add_subdirectory(Enclave)
    add_subdirectory(tests)
endif()

