编辑 | blame | 历史 | 原始文档
cmake_minimum_required(VERSION 3.0)

set (Target "bhome_msg")
project(${Target})

#设置输出路径
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()


#获取需要编译的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)