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