xref: /aosp_15_r20/external/ot-br-posix/src/proto/CMakeLists.txt (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker# Config brew protobuf version for Mac, see .github/workflows/macOS.yml
2*4a64e381SAndroid Build Coastguard Workerif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
3*4a64e381SAndroid Build Coastguard Worker    set(Protobuf_PREFIX_PATH
4*4a64e381SAndroid Build Coastguard Worker        "/usr/local/opt/protobuf@21/include"
5*4a64e381SAndroid Build Coastguard Worker        "/usr/local/opt/protobuf@21/lib"
6*4a64e381SAndroid Build Coastguard Worker        "/usr/local/opt/protobuf@21/bin")
7*4a64e381SAndroid Build Coastguard Worker    list(APPEND CMAKE_PREFIX_PATH "${Protobuf_PREFIX_PATH}")
8*4a64e381SAndroid Build Coastguard Workerendif()
9*4a64e381SAndroid Build Coastguard Workerfind_package(Protobuf REQUIRED)
10*4a64e381SAndroid Build Coastguard Worker
11*4a64e381SAndroid Build Coastguard Worker# Protobuf library which >= 4.22 requires to link the absl
12*4a64e381SAndroid Build Coastguard Workerif ("${Protobuf_VERSION}" VERSION_GREATER_EQUAL 4.22)
13*4a64e381SAndroid Build Coastguard Worker    find_package(absl REQUIRED)
14*4a64e381SAndroid Build Coastguard Worker    set(ABSL_LIBS absl::log_internal_check_op)
15*4a64e381SAndroid Build Coastguard Workerendif()
16*4a64e381SAndroid Build Coastguard Worker
17*4a64e381SAndroid Build Coastguard Worker# Set up the output path.
18*4a64e381SAndroid Build Coastguard Workerset(PROTO_GEN_DIR ${PROJECT_SOURCE_DIR}/build/src/proto)
19*4a64e381SAndroid Build Coastguard Workerif(NOT (EXISTS "${PROTO_GEN_DIR}" AND IS_DIRECTORY "${PROTO_GEN_DIR}"))
20*4a64e381SAndroid Build Coastguard Worker    file(MAKE_DIRECTORY ${PROTO_GEN_DIR})
21*4a64e381SAndroid Build Coastguard Workerendif()
22*4a64e381SAndroid Build Coastguard Worker
23*4a64e381SAndroid Build Coastguard Worker# Retrieve all proto files.
24*4a64e381SAndroid Build Coastguard Workerfile(GLOB_RECURSE MSG_PROTOS ${CMAKE_CURRENT_SOURCE_DIR}/*.proto)
25*4a64e381SAndroid Build Coastguard Workerset(PROTO_SRC "")
26*4a64e381SAndroid Build Coastguard Workerset(PROTO_HDRS "")
27*4a64e381SAndroid Build Coastguard Worker
28*4a64e381SAndroid Build Coastguard Workerforeach(msg ${MSG_PROTOS})
29*4a64e381SAndroid Build Coastguard Worker    get_filename_component(FIL_WE ${msg} NAME_WE)
30*4a64e381SAndroid Build Coastguard Worker
31*4a64e381SAndroid Build Coastguard Worker    list(APPEND PROTO_SRC "${PROTO_GEN_DIR}/${FIL_WE}.pb.cc")
32*4a64e381SAndroid Build Coastguard Worker    list(APPEND PROTO_HDRS "${PROTO_GEN_DIR}/${FIL_WE}.pb.h")
33*4a64e381SAndroid Build Coastguard Worker
34*4a64e381SAndroid Build Coastguard Worker    # Define protoc command.
35*4a64e381SAndroid Build Coastguard Worker    add_custom_command(
36*4a64e381SAndroid Build Coastguard Worker        OUTPUT "${PROTO_GEN_DIR}/${FIL_WE}.pb.cc"
37*4a64e381SAndroid Build Coastguard Worker                "${PROTO_GEN_DIR}/${FIL_WE}.pb.h"
38*4a64e381SAndroid Build Coastguard Worker        COMMAND ${Protobuf_PROTOC_EXECUTABLE}
39*4a64e381SAndroid Build Coastguard Worker        ARGS --proto_path ${CMAKE_CURRENT_SOURCE_DIR}
40*4a64e381SAndroid Build Coastguard Worker            --cpp_out  ${PROTO_GEN_DIR}
41*4a64e381SAndroid Build Coastguard Worker            ${msg}
42*4a64e381SAndroid Build Coastguard Worker        DEPENDS ${msg}
43*4a64e381SAndroid Build Coastguard Worker        COMMENT "Running C++ protocol buffer compiler on ${msg}"
44*4a64e381SAndroid Build Coastguard Worker        VERBATIM
45*4a64e381SAndroid Build Coastguard Worker    )
46*4a64e381SAndroid Build Coastguard Workerendforeach()
47*4a64e381SAndroid Build Coastguard Worker
48*4a64e381SAndroid Build Coastguard Worker# Set the protoc output files as GENERATED.
49*4a64e381SAndroid Build Coastguard Workerset_source_files_properties(${PROTO_SRC} ${PROTO_HDRS}
50*4a64e381SAndroid Build Coastguard Worker    PROPERTIES GENERATED TRUE
51*4a64e381SAndroid Build Coastguard Worker    # Add flag to fix the issue https://github.com/protocolbuffers/protobuf/issues/7140
52*4a64e381SAndroid Build Coastguard Worker    COMPILE_FLAGS -Wno-array-bounds)
53*4a64e381SAndroid Build Coastguard Worker
54*4a64e381SAndroid Build Coastguard Worker# Add custom targets so that proto files will be generated only when changed.
55*4a64e381SAndroid Build Coastguard Workeradd_custom_target(generate_message ALL
56*4a64e381SAndroid Build Coastguard Worker    DEPENDS ${PROTO_SRC} ${PROTO_HDRS}
57*4a64e381SAndroid Build Coastguard Worker    COMMENT "generate message target"
58*4a64e381SAndroid Build Coastguard Worker    VERBATIM
59*4a64e381SAndroid Build Coastguard Worker)
60*4a64e381SAndroid Build Coastguard Worker
61*4a64e381SAndroid Build Coastguard Workeradd_library(otbr-proto STATIC
62*4a64e381SAndroid Build Coastguard Worker    ${PROTO_SRC}
63*4a64e381SAndroid Build Coastguard Worker    ${PROTO_HDRS}
64*4a64e381SAndroid Build Coastguard Worker)
65*4a64e381SAndroid Build Coastguard Worker
66*4a64e381SAndroid Build Coastguard Workerfind_package(Protobuf REQUIRED)
67*4a64e381SAndroid Build Coastguard Worker
68*4a64e381SAndroid Build Coastguard Workertarget_link_libraries(otbr-proto PUBLIC
69*4a64e381SAndroid Build Coastguard Worker    protobuf::libprotobuf-lite
70*4a64e381SAndroid Build Coastguard Worker    ${ABSL_LIBS}
71*4a64e381SAndroid Build Coastguard Worker)
72*4a64e381SAndroid Build Coastguard Worker
73*4a64e381SAndroid Build Coastguard Workertarget_include_directories(otbr-proto PUBLIC
74*4a64e381SAndroid Build Coastguard Worker    ${PROJECT_SOURCE_DIR}/build/src
75*4a64e381SAndroid Build Coastguard Worker)
76