liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
proto/cpp/CMakeLists.txt
@@ -1,14 +1,56 @@
cmake_minimum_required(VERSION 3.0)
set (Target "bhome_msg")
set (TargetStatic "bhome_msg_static")
project(${Target})
find_package(Protobuf REQUIRED)
file(GLOB proto_files ../source/*.proto)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${proto_files})
#设置输出路径
SET(MESSAGE_DIR ${CMAKE_SOURCE_DIR}/proto)
if(EXISTS "${CMAKE_SOURCE_DIR}/proto" AND IS_DIRECTORY "${CMAKE_SOURCE_DIR}/proto")
        SET(PROTO_META_BASE_DIR ${MESSAGE_DIR})
else()
        file(MAKE_DIRECTORY ${MESSAGE_DIR})
        SET(PROTO_META_BASE_DIR ${MESSAGE_DIR})
endif()
add_library(${Target} SHARED ${PROTO_SRCS})
add_library(${TargetStatic} STATIC ${PROTO_SRCS})
target_link_libraries(${Target} protobuf-lite)
target_link_libraries(${TargetStatic} libprotobuf-lite.a)
#获取需要编译的proto文件
set(MSG_PROTOS
    bhome_msg.proto
    bhome_msg_api.proto
    error_msg.proto
    )
set(MESSAGE_SRC "")
set(MESSAGE_HDRS "")
foreach(msg ${MSG_PROTOS})
        message(${msg})
        get_filename_component(FIL_WE ${msg} NAME_WE)
        message(${FIL_WE})
        list(APPEND MESSAGE_SRC "${MESSAGE_DIR}/${FIL_WE}.pb.cc")
        list(APPEND MESSAGE_HDRS "${MESSAGE_DIR}/${FIL_WE}.pb.h")
        # 使用自定义命令
        execute_process(
            COMMAND ${CMAKE_SOURCE_DIR}/protobuf/protoc --cpp_out=${MESSAGE_DIR} --proto_path=${CMAKE_SOURCE_DIR}/proto/source ${msg}
            )
endforeach()
# 设置文件属性为 GENERATED
set_source_files_properties(${MESSAGE_SRC} ${MESSAGE_HDRS} PROPERTIES GENERATED TRUE)
# 添加自定义target
add_custom_target(generate_message ALL
                DEPENDS ${MESSAGE_SRC} ${MESSAGE_HDRS}
                COMMENT "generate message target"
                VERBATIM
                )
include_directories(${CMAKE_SOURCE_DIR}/protobuf/include)
add_library(${Target} STATIC ${MESSAGE_SRC})
target_link_libraries(${Target} ${CMAKE_SOURCE_DIR}/protobuf/lib/libprotobuf-lite.a)