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
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:
110
cmake/generic.cmake
Normal file
110
cmake/generic.cmake
Normal 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()
|
Reference in New Issue
Block a user