From d08e58465aa3bb1d60cf5e9b2f3cedd65a61049b Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sat, 9 Feb 2019 18:43:45 -0800 Subject: [PATCH] build a protoc binary to use to ensure version compat --- components/protobuf/functions.cmake | 38 ++++++++++++++++++- components/protobuf/protobuf-CMakeLists.cmake | 17 +++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 components/protobuf/protobuf-CMakeLists.cmake diff --git a/components/protobuf/functions.cmake b/components/protobuf/functions.cmake index ac626f3..f1869f5 100644 --- a/components/protobuf/functions.cmake +++ b/components/protobuf/functions.cmake @@ -1,4 +1,38 @@ -find_program(PROTOC protoc) +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) @@ -14,7 +48,7 @@ function(proto_generate_cpp PROTO_FILE PB_OUT HDR_VAR SRC_VAR) --cpp_out=${PB_OUT} --proto_path ${PROTO_FILE_DIR} ${PROTO_FILE_PATH} - DEPENDS ${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}) diff --git a/components/protobuf/protobuf-CMakeLists.cmake b/components/protobuf/protobuf-CMakeLists.cmake new file mode 100644 index 0000000..0e67047 --- /dev/null +++ b/components/protobuf/protobuf-CMakeLists.cmake @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 2.8.2) + +project(protobuf-build NONE) + +include(ExternalProject) +ExternalProject_Add(protobuf + URL "${PROTOBUF_DIR}" + SOURCE_DIR "${PROTOBUF_SRC}" + BINARY_DIR "${PROTOBUF_BINARY}" + SOURCE_SUBDIR "cmake" + CMAKE_GENERATOR "${CMAKE_GENERATOR}" + CMAKE_ARGS -Dprotobuf_BUILD_TESTS:BOOL=OFF + INSTALL_COMMAND "" + TEST_COMMAND "" + USES_TERMINAL_CONFIGURE ON + USES_TERMINAL_BUILD ON +)