TarsCloud_TarsCpp/cmake/BuildTarsCpp.cmake

128 lines
4.8 KiB
CMake
Raw Normal View History

2020-01-29 15:52:39 +08:00
2020-02-02 15:42:56 +08:00
macro(build_tars_server MODULE DEPS)
2020-01-29 15:52:39 +08:00
2020-02-25 10:14:27 +08:00
project(${MODULE})
2020-01-29 15:52:39 +08:00
include_directories(./)
FILE(GLOB_RECURSE DIR_SRCS "*.cpp")
2020-01-29 15:52:39 +08:00
2021-08-23 15:35:24 +08:00
FILE(GLOB_RECURSE TARS_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.tars")
FILE(GLOB_RECURSE PB_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
2020-01-29 15:52:39 +08:00
set(TARS_LIST_DEPENDS)
2021-07-23 10:08:42 +08:00
set(PB_LIST_DEPENDS)
2020-01-29 15:52:39 +08:00
if (TARS_LIST)
set(CLEAN_LIST)
foreach (TARS_SRC ${TARS_LIST})
get_filename_component(NAME_WE ${TARS_SRC} NAME_WE)
2021-08-23 15:35:24 +08:00
get_filename_component(PATH ${TARS_SRC} PATH)
2020-01-29 15:52:39 +08:00
set(TARS_H ${NAME_WE}.h)
2021-08-23 15:35:24 +08:00
set(CUR_TARS_GEN ${PATH}/${TARS_H})
2020-01-29 15:52:39 +08:00
LIST(APPEND TARS_LIST_DEPENDS ${CUR_TARS_GEN})
add_custom_command(OUTPUT ${CUR_TARS_GEN}
2021-08-23 15:35:24 +08:00
WORKING_DIRECTORY ${PATH}
DEPENDS ${TARS2CPP} ${TARS_SRC}
2020-01-29 15:52:39 +08:00
COMMAND ${TARS2CPP} ${TARS_SRC}
COMMENT "${TARS2CPP} ${TARS_SRC}")
2021-08-23 15:35:24 +08:00
list(APPEND CLEAN_LIST ${PATH}/${TARS_H})
2020-01-29 15:52:39 +08:00
endforeach ()
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}")
set(TARS_TARGET "TARS_${MODULE}")
2020-02-07 13:50:04 +08:00
add_custom_target(${TARS_TARGET} ALL DEPENDS ${TARS_LIST_DEPENDS} tars2cpp)
2020-01-29 15:52:39 +08:00
add_executable(${MODULE} ${DIR_SRCS})
add_dependencies(${MODULE} ${TARS_TARGET})
2021-07-23 10:08:42 +08:00
elseif(PB_LIST)
set(CLEAN_LIST)
set(_PROTOBUF_PROTOC ${CMAKE_BINARY_DIR}/src/protobuf/bin/protoc)
foreach (PB_SRC ${PB_LIST})
get_filename_component(NAME_WE ${PB_SRC} NAME_WE)
2021-08-23 15:35:24 +08:00
get_filename_component(PATH ${PB_SRC} PATH)
2021-07-23 10:08:42 +08:00
set(PB_H ${NAME_WE}.pb.h)
set(PB_CC ${NAME_WE}.pb.cc)
2020-01-29 15:52:39 +08:00
2021-08-23 15:35:24 +08:00
set(CUR_PB_GEN ${PATH}/${PB_H} ${PATH}/${PB_CC})
2021-07-23 10:08:42 +08:00
LIST(APPEND PB_LIST_DEPENDS ${CUR_PB_GEN})
add_custom_command(OUTPUT ${CUR_PB_GEN}
2021-08-23 15:35:24 +08:00
WORKING_DIRECTORY ${PATH}
2021-07-23 10:08:42 +08:00
DEPENDS ${PROTO2TARS} ${_PROTOBUF_PROTOC}
2021-08-23 15:35:24 +08:00
COMMAND ${_PROTOBUF_PROTOC} -I "${PATH}"
"${PB_SRC}" --cpp_out "${PATH}"
COMMENT "${_PROTOBUF_PROTOC} ${PB_SRC} ${PATH} ${CUR_PB_GEN}")
2021-07-23 10:08:42 +08:00
2021-08-23 15:35:24 +08:00
list(APPEND CLEAN_LIST ${PATH}/${PB_H} ${PATH}/${PB_CC})
2021-07-23 10:08:42 +08:00
endforeach ()
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}")
set(TARS_TARGET "TARS_${MODULE}")
add_custom_target(${TARS_TARGET} ALL DEPENDS ${PB_LIST_DEPENDS})
add_executable(${MODULE} ${CLEAN_LIST} ${DIR_SRCS})
add_dependencies(${MODULE} ${TARS_TARGET})
else()
2020-01-29 15:52:39 +08:00
add_executable(${MODULE} ${DIR_SRCS})
2021-07-23 10:08:42 +08:00
endif()
2020-01-29 15:52:39 +08:00
if("${DEPS}" STREQUAL "")
add_dependencies(${MODULE} tarsservant tarsutil)
else()
string(REPLACE " " ";" DEP_LIST ${DEPS})
add_dependencies(${MODULE} ${DEP_LIST} tarsservant tarsutil)
endif()
2020-01-29 15:52:39 +08:00
target_link_libraries(${MODULE} tarsservant tarsutil)
if(TARS_SSL)
target_link_libraries(${MODULE} tarsservant tarsutil ${LIB_SSL} ${LIB_CRYPTO})
if(WIN32)
target_link_libraries(${MODULE} Crypt32)
endif()
endif()
if(TARS_HTTP2)
2021-07-23 10:08:42 +08:00
target_link_libraries(${MODULE} ${LIB_HTTP2} ${LIB_PROTOBUF})
endif()
2020-04-06 21:09:28 +08:00
if(TARS_GPERF)
target_link_libraries(${MODULE} ${LIB_GPERF})
endif(TARS_GPERF)
SET(MODULE-TGZ "${CMAKE_BINARY_DIR}/${MODULE}.tgz")
SET(RUN_DEPLOY_COMMAND_FILE "${PROJECT_BINARY_DIR}/run-deploy-${MODULE}.cmake")
FILE(WRITE ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/deploy/${MODULE})\n")
IF(WIN32)
2020-02-07 13:50:04 +08:00
# message(${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_BUILD_TYPE}/${MODULE}.exe)
FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_BUILD_TYPE}/${MODULE}.exe ${PROJECT_BINARY_DIR}/deploy/${MODULE}/)\n")
FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/deploy/ \n COMMAND ${CMAKE_COMMAND} -E tar czfv ${MODULE-TGZ} ${MODULE})\n")
ELSE()
FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${MODULE} ${PROJECT_BINARY_DIR}/deploy/${MODULE}/)\n")
FILE(APPEND ${RUN_DEPLOY_COMMAND_FILE} "EXECUTE_PROCESS(WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/deploy/ \n COMMAND ${CMAKE_COMMAND} -E tar czfv ${MODULE-TGZ} ${MODULE})\n")
ENDIF()
#执行命令
add_custom_command(OUTPUT ${MODULE-TGZ}
DEPENDS ${MODULE}
COMMAND ${CMAKE_COMMAND} -P ${RUN_DEPLOY_COMMAND_FILE}
COMMENT "call ${RUN_DEPLOY_COMMAND_FILE}")
add_custom_target(${MODULE}-tar DEPENDS ${MODULE-TGZ})
2020-01-29 21:31:27 +08:00
endmacro()
2020-02-20 22:00:32 +08:00
#-----------------------------------------------------------------------