extern_interface/CMakeLists.txt

64 lines
2.0 KiB
CMake
Raw Normal View History

cmake_minimum_required(VERSION 3.15)
project(extern_interface CXX)
set(CMAKE_CXX_STANDARD 11)
if (NOT CMAKE_CROSSCOMPILING)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
endif()
if (UNIX)
set(CMAKE_CXX_FLAGS "-std=c++17 -fPIC -Wall ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "-g -O2 ${CMAKE_CXX_FLAGS}")
elseif (WIN32)
# windows platform
#add_definitions(-D_CRT_SECURE_NO_WARNINGS)
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd /EHsc")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT /EHsc")
endif()
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
endif()
if (CMAKE_CROSSCOMPILING)
message(STATUS "Cross compiling ...")
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
include_directories(./include)
aux_source_directory(./src SRC_LIST)
add_library(extern_interface_static STATIC ${SRC_LIST})
add_library(extern_interface SHARED ${SRC_LIST})
# 设置测试文件和公共源文件
set(TESTS_DIR "${CMAKE_SOURCE_DIR}/tests")
set(TEST_COMMON_SOURCE "${TESTS_DIR}/c_testcase.cpp")
# 查找所有以 test_ 开头的文件
file(GLOB_RECURSE TEST_FILES "${TESTS_DIR}/test_*.cpp")
# 循环遍历每个测试文件并创建可执行文件
foreach(TEST_FILE ${TEST_FILES})
# 获取文件名(不带路径和扩展名)
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)
# 创建可执行文件
add_executable(${TEST_NAME} ${TEST_FILE} ${TEST_COMMON_SOURCE} ${SRC_LIST})
# 将可执行文件路径添加到列表中
list(APPEND TEST_EXECUTABLES "${EXECUTABLE_OUTPUT_PATH}/${TEST_NAME}")
endforeach()
enable_testing()
add_test(NAME test
COMMAND ${CMAKE_SOURCE_DIR}/scripts/unittest.py ${TEST_EXECUTABLES}
)