liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)