CMakeLists.txt 1.01 KB
Newer Older
wangdawei's avatar
wangdawei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
find_package(Protobuf 3.0.0 REQUIRED)
#设置输出路径
message(${Protobuf_PROTOC_EXECUTABLE})
message(${Protobuf_PROTOC_LIBRARY})
message(${Protobuf_LIBRARIES})
#设置protoc的搜索路径
LIST(APPEND PROTO_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR})

#获取需要编译的proto文件
file(GLOB_RECURSE MSG_PROTOS ${CMAKE_CURRENT_SOURCE_DIR}/*/*.proto)
set(MESSAGE_SRC "")
set(MESSAGE_HDRS "")

set(DST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/proto_pb)
execute_process( COMMAND ${CMAKE_COMMAND} -E make_directory ${DST_DIR})
foreach(msg ${MSG_PROTOS})
        get_filename_component(FIL_WE ${msg} NAME_WE)

        # 生成源码
        execute_process(
            COMMAND ${Protobuf_PROTOC_EXECUTABLE} ${PROTO_FLAGS} --cpp_out=${DST_DIR} ${msg}
            )
endforeach()

message(${DST_DIR})
file(GLOB_RECURSE PROTO_PB_SRC ${DST_DIR}/*.cc)

add_library(proto_pb SHARED ${PROTO_PB_SRC})
target_include_directories(proto_pb
    PUBLIC
    ${DST_DIR}
    ${Protobuf_INCLUDE_DIR})

target_link_libraries(proto_pb
    ${Protobuf_LIBRARIES})