feat(cmake): 增加一些常用工具
All checks were successful
sm-rpc / build (Debug, arm-linux-gnueabihf) (push) Successful in 1m11s
sm-rpc / build (Debug, aarch64-linux-gnu) (push) Successful in 1m25s
sm-rpc / build (Debug, host.gcc) (push) Successful in 1m2s
sm-rpc / build (Debug, mipsel-linux-gnu) (push) Successful in 1m25s
sm-rpc / build (Release, aarch64-linux-gnu) (push) Successful in 1m32s
sm-rpc / build (Release, arm-linux-gnueabihf) (push) Successful in 1m39s
sm-rpc / build (Release, host.gcc) (push) Successful in 1m22s
sm-rpc / build (Release, mipsel-linux-gnu) (push) Successful in 1m32s

This commit is contained in:
tqcq
2025-08-19 18:59:22 +08:00
parent ea9d7b5f8c
commit 3d395b45f0
5 changed files with 116 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ project(demo LANGUAGES C CXX)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
endif()
include(cmake/generic.cmake)
add_subdirectory(third_party/oatpp)
@@ -11,8 +12,5 @@ add_subdirectory(third_party/googletest)
add_subdirectory(third_party/benchmark)
enable_testing()
add_executable(add_test src/add_test.cc)
target_link_libraries(add_test PRIVATE GTest::gtest_main)
add_executable(add_benchmark src/add_benchmark.cc)
target_link_libraries(add_benchmark PRIVATE benchmark::benchmark)
cc_test(calc_test SRCS src/calc_test.cc DEPS)
cc_benchmark(calc_benchmark SRCS src/calc_benchmark.cc DEPS)

110
cmake/generic.cmake Normal file
View File

@@ -0,0 +1,110 @@
function(cc_library TARGET_NAME)
set(options STATIC static SHARED shared INTERFACE interface)
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS INCS)
# 解析参数三种参数 前缀为cc_library_XXX
cmake_parse_arguments(cc_library "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
if(WIN32)
# add prefix `lib`, suffix `.lib` -> libxxx.lib in windows
set(${TARGET_NAME}_LIB_NAME
"${CMAKE_STATIC_LIBRARY_PREFIX}${TARGET_NAME}${CMAKE_STATIC_LIBRARY_PREFIX}"
CACHE STRING "output library name for target ${TARGET_NAME}")
endif()
if(cc_library_SRCS)
if(cc_library_SHARED OR cc_library_shared)
add_library(${TARGET_NAME} SHARED ${cc_library_SRCS})
elseif(cc_library_INTERFACE OR cc_library_interface)
# add_library(${TARGET_NAME} SHARED ${cc_library_SRCS})
else()
add_library(${TARGET_NAME} STATIC ${cc_library_SRCS})
endif()
if(cc_library_INCS)
target_include_directories(${TARGET_NAME} ${cc_library_DEPS})
endif()
if(cc_library_DEPS)
target_link_libraries(${TARGET_NAME} ${cc_library_DEPS})
endif()
endif()
endfunction()
function(cc_executable TARGET_NAME)
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS INCS)
cmake_parse_arguments(cc_executable "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
if(cc_executable_SRCS)
list(LENGTH cc_executable_SRCS len_src)
if("${len_src}" GREATER_EQUAL 1)
add_executable(${TARGET_NAME} ${cc_executable_SRCS})
else()
add_executable(${TARGET_NAME} "")
endif()
endif()
if(cc_executable_INCS)
target_include_directories(${TARGET_NAME} ${cc_executable_INCS})
endif()
if(cc_executable_DEPS)
target_link_libraries(${TARGET_NAME} ${cc_executable_DEPS})
endif()
endfunction()
function(cc_test TARGET_NAME)
if((NOT TARGET GTest::gtest_main) OR (NOT TARGET GTest::gtest))
message("Not found gtest")
return()
endif()
set(options NO_MAIN)
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS INCS ARGS)
cmake_parse_arguments(cc_test "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
# message("TARGET ${TARGET_NAME}")
cc_executable(
${TARGET_NAME}
SRCS
${cc_test_SRCS}
DEPS
${cc_test_DEPS}
INCS
${cc_test_INCS})
target_link_libraries(${TARGET_NAME} ${cc_test_DEPS} PRIVATE GTest::gtest)
if(NOT cc_test_NO_MAIN)
target_link_libraries(${TARGET_NAME} PRIVATE benchmark::benchmark_main)
endif()
add_test(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME})
endfunction()
function(cc_benchmark TARGET_NAME)
if(NOT TARGET benchmark::benchmark)
message("Not found benchmark")
return()
endif()
set(options NO_MAIN)
set(oneValueArgs "")
set(multiValueArgs SRCS DEPS INCS ARGS)
cmake_parse_arguments(cc_benchmark "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
# message("TARGET ${TARGET_NAME}")
cc_executable(
${TARGET_NAME}
SRCS
${cc_benchmark_SRCS}
DEPS
${cc_benchmark_DEPS}
INCS
${cc_benchmark_INCS})
target_link_libraries(${TARGET_NAME} ${cc_benchmark_DEPS}
PRIVATE benchmark::benchmark)
if(NOT cc_benchmark_NO_MAIN)
target_link_libraries(${TARGET_NAME} PRIVATE benchmark::benchmark_main)
endif()
endfunction()

View File

@@ -1,12 +1,10 @@
#include "add.h"
#include "calc.h"
#include <benchmark/benchmark.h>
static void
BM_Add(benchmark::State &state)
{
Add(1, 2);
while (state.KeepRunning()) { Add(1, 2); }
}
BENCHMARK(BM_Add);
BENCHMARK_MAIN();

View File

@@ -1,4 +1,4 @@
#include "add.h"
#include "calc.h"
#include <gtest/gtest.h>
TEST(Add, Base)