Legacy protobuf is annoying.

Took 4 minutes
This commit is contained in:
Lukas Brübach 2025-10-04 21:31:54 +02:00
parent fdf08780b3
commit e5f23f1906

View file

@ -165,49 +165,62 @@ set(PROTO_FILES
session_event.proto
)
# Common output directory for generated files
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pb)
set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pb")
if(${Protobuf_VERSION} VERSION_LESS "3.21.0.0")
message(STATUS "Using Protobuf Legacy Mode")
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pb)
set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pb")
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${PROTO_BINARY_DIR}) # pb/ folder
include_directories(${PROTO_BINARY_DIR}) # expose pb/ folder
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
# Generate protobuf cpp/h files directly into pb/
set(PROTO_SRCS)
set(PROTO_HDRS)
foreach (f ${PROTO_FILES})
get_filename_component(fname ${f} NAME_WE)
set(src_cpp ${PROTO_BINARY_DIR}/${fname}.pb.cc)
set(src_h ${PROTO_BINARY_DIR}/${fname}.pb.h)
# Move files at build time using a custom command
foreach (f ${PROTO_SRCS} ${PROTO_HDRS})
get_filename_component(fname ${f} NAME)
add_custom_command(
TARGET cockatrice_protocol POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${f} ${PROTO_BINARY_DIR}/${fname}
OUTPUT ${src_cpp} ${src_h}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
--cpp_out=${PROTO_BINARY_DIR}
-I ${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/${f}
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/${f}
COMMENT "Generating ${fname}.pb.cc / .pb.h"
)
list(APPEND PROTO_SRCS ${src_cpp})
list(APPEND PROTO_HDRS ${src_h})
endforeach ()
add_library(cockatrice_protocol ${PROTO_SRCS} ${PROTO_HDRS})
set(cockatrice_protocol_LIBS ${PROTOBUF_LIBRARIES})
if(UNIX)
set(cockatrice_protocol_LIBS ${cockatrice_protocol_LIBS} -lpthread)
endif(UNIX)
target_link_libraries(cockatrice_protocol ${cockatrice_protocol_LIBS})
target_include_directories(cockatrice_protocol PUBLIC ${PROTO_BINARY_DIR})
# ubuntu uses an outdated package for protobuf, 3.1.0 is required
if(${Protobuf_VERSION} VERSION_LESS "3.1.0")
# remove unused parameter and misleading indentation warnings when compiling to avoid errors
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-unused-parameter -Wno-misleading-indentation")
message(WARNING "Older protobuf version found (${Protobuf_VERSION} < 3.1.0), "
"disabled the warnings 'unused-parameter' and 'misleading-indentation' for protobuf generated code "
"to avoid compilation errors."
"disabled the warnings 'unused-parameter' and 'misleading-indentation' for protobuf generated code "
"to avoid compilation errors."
)
endif()
else()
add_library(cockatrice_protocol ${PROTO_FILES})
target_link_libraries(cockatrice_protocol PUBLIC protobuf::libprotobuf)
set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/pb")
target_include_directories(cockatrice_protocol PUBLIC "${PROTOBUF_INCLUDE_DIRS}" PUBLIC ${CMAKE_CURRENT_BINARY_DIR} PUBLIC ${PROTO_BINARY_DIR})
protobuf_generate(
TARGET cockatrice_protocol IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}" PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
TARGET cockatrice_protocol IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}" PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
)
endif()