You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.2 KiB

if(NOT CMAKE_SCRIPT_MODE_FILE)
set(PROTOBUF_DIR ${CMAKE_CURRENT_LIST_DIR}/protobuf)
set(PROTOBUF_BUILD ${PROJECT_BINARY_DIR}/protobuf-build)
set(PROTOBUF_BINARY ${PROJECT_BINARY_DIR}/protobuf-binary)
set(PROTOBUF_SRC ${PROJECT_BINARY_DIR}/protobuf-src)
configure_file(${CMAKE_CURRENT_LIST_DIR}/protobuf-CMakeLists.cmake ${PROTOBUF_BUILD}/CMakeLists.txt)
if(NOT CMAKE_GENERATOR)
set(CMAKE_GENERATOR "Ninja")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${PROTOBUF_BUILD} )
if(result)
message(FATAL_ERROR "CMake step for protobuf failed: ${result}")
endif()
# execute_process(COMMAND ${CMAKE_COMMAND} --build .
# RESULT_VARIABLE result
# WORKING_DIRECTORY ${PROTOBUF_BUILD})
# if(result)
# message(FATAL_ERROR "Build step for protobuf failed: ${result}")
# endif()
set(PROTOC ${PROTOBUF_BINARY}/protoc)
add_custom_command(OUTPUT ${PROTOC}
COMMENT "Building protoc binary"
COMMAND ${CMAKE_COMMAND} --build .
USES_TERMINAL
WORKING_DIRECTORY ${PROTOBUF_BUILD})
endif()
# find_program(PROTOC protoc PATHS ${CMAKE_CURRENT_LIST_DIR}/protobuf/cmake/build/)
function(proto_generate_cpp PROTO_FILE PB_OUT HDR_VAR SRC_VAR)
get_filename_component(PROTO_FILE_NAME ${PROTO_FILE} NAME_WE)
get_filename_component(PROTO_FILE_PATH ${PROTO_FILE} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
get_filename_component(PROTO_FILE_DIR ${PROTO_FILE_PATH} DIRECTORY)
set(PROTO_HDR "${PB_OUT}/${PROTO_FILE_NAME}.pb.h")
set(PROTO_SRC "${PB_OUT}/${PROTO_FILE_NAME}.pb.cc")
set(${HDR_VAR} ${${HDR_VAR}} ${PROTO_HDR} PARENT_SCOPE)
set(${SRC_VAR} ${${SRC_VAR}} ${PROTO_SRC} PARENT_SCOPE)
if(NOT CMAKE_SCRIPT_MODE_FILE)
add_custom_command(OUTPUT ${PROTO_HDR} ${PROTO_SRC}
COMMAND ${PROTOC}
--cpp_out=${PB_OUT}
--proto_path ${PROTO_FILE_DIR}
${PROTO_FILE_PATH}
DEPENDS ${PROTO_FILE_PATH} ${PROTOC}
COMMENT "Generating nanopb sources for ${PROTO_FILE}")
set_property(SOURCE ${PROTO_HDR} ${PROTO_SRC} PROPERTY GENERATED TRUE)
set_property(DIRECTORY . APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${PROTO_HDR} ${PROTO_SRC})
endif()
endfunction()