function(_Protobuf_import)
# Let's use find_package here not to export unnecessary definitions
- find_package(Protobuf)
+ # NOTE Here we use "exact" match to avoid possible infinite loop
+ find_package(Protobuf EXACT 3.6.1)
- if(NOT PROTOBUF_FOUND)
+ if(NOT protobuf_FOUND)
set(Protobuf_FOUND FALSE PARENT_SCOPE)
return()
- endif(NOT PROTOBUF_FOUND)
-
- if(NOT TARGET protoc)
- add_executable(protoc IMPORTED)
- set_target_properties(protoc PROPERTIES IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE})
- endif(NOT TARGET protoc)
+ endif(NOT protobuf_FOUND)
if(NOT TARGET libprotobuf)
add_library(libprotobuf INTERFACE)
- target_include_directories(libprotobuf INTERFACE ${PROTOBUF_INCLUDE_DIRS})
- target_link_libraries(libprotobuf INTERFACE ${PROTOBUF_LIBRARIES})
+ target_link_libraries(libprotobuf INTERFACE protobuf::libprotobuf)
endif(NOT TARGET libprotobuf)
set(Protobuf_FOUND TRUE PARENT_SCOPE)
endfunction(_Protobuf_import)
-# TODO Allow users to download and build protobuf 3.X
+function(_Protobuf_build)
+ if(NOT BUILD_PROTOBUF)
+ return()
+ endif(NOT BUILD_PROTOBUF)
+
+ nncc_find_package(ProtobufSource QUIET)
+
+ if(NOT ProtobufSource_FOUND)
+ # Source is not available
+ return()
+ endif(NOT ProtobufSource_FOUND)
+
+ # TODO Introduce helper functions
+ set(PROTOBUF_BUILD_DIR "${CMAKE_BINARY_DIR}/externals/PROTOBUF/build")
+ set(PROTOBUF_INSTALL_DIR "${NNCC_OVERLAY_DIR}")
+
+ set(STAMP_PATH "${PROTOBUF_INSTALL_DIR}/PROTOBUF.stamp")
+ set(LOG_PATH "${PROTOBUF_INSTALL_DIR}/PROTOBUF.log")
+
+ if(EXISTS ${STAMP_PATH})
+ return()
+ endif(EXISTS ${STAMP_PATH})
+
+ message(STATUS "Build Protocol Buffer from ${ProtobufSource_DIR}")
+
+ file(MAKE_DIRECTORY ${PROTOBUF_BUILD_DIR})
+ file(MAKE_DIRECTORY ${PROTOBUF_INSTALL_DIR})
+
+ # NOTE Do NOT retry Protocol Buffer build
+ file(WRITE "${STAMP_PATH}")
+
+ execute_process(COMMAND ${CMAKE_COMMAND}
+ -DCMAKE_INSTALL_PREFIX=${PROTOBUF_INSTALL_DIR}
+ -DCMAKE_BUILD_TYPE=Release
+ -DCMAKE_CXX_FLAGS="-fPIC"
+ -Dprotobuf_BUILD_TESTS=OFF
+ -Dprotobuf_WITH_ZLIB=OFF
+ "${ProtobufSource_DIR}/cmake"
+ OUTPUT_FILE ${LOG_PATH}
+ ERROR_FILE ${LOG_PATH}
+ WORKING_DIRECTORY ${PROTOBUF_BUILD_DIR}
+ RESULT_VARIABLE CONFIGURE_EXITCODE)
+
+ if(NOT CONFIGURE_EXITCODE EQUAL 0)
+ message(STATUS "Fail to configure Protocol Buffer (check '${LOG_PATH}' for details)")
+ return()
+ endif(NOT CONFIGURE_EXITCODE EQUAL 0)
+
+ execute_process(COMMAND ${CMAKE_COMMAND} --build . -- install
+ OUTPUT_FILE ${LOG_PATH}
+ ERROR_FILE ${LOG_PATH}
+ WORKING_DIRECTORY ${PROTOBUF_BUILD_DIR}
+ RESULT_VARIABLE BUILD_AND_INSTALL_EXITCODE)
+
+ if(NOT BUILD_AND_INSTALL_EXITCODE EQUAL 0)
+ message(STATUS "Fail to build/install Protocol Buffer (check '${LOG_PATH}' for details)")
+ return()
+ endif(NOT BUILD_AND_INSTALL_EXITCODE EQUAL 0)
+
+ message(STATUS "Succeeded in building Protocol Buffer")
+endfunction(_Protobuf_build)
+
+_Protobuf_build()
_Protobuf_import()
if(Protobuf_FOUND)
--- /dev/null
+function(_ProtobufSource_import)
+ if(NOT DOWNLOAD_PROTOBUF)
+ set(ProtobufSource_FOUND FALSE PARENT_SCOPE)
+ return()
+ endif(NOT DOWNLOAD_PROTOBUF)
+
+ nncc_include(ExternalSourceTools)
+ nncc_include(OptionTools)
+
+ envoption(PROTOBUF_URL https://github.com/protocolbuffers/protobuf/archive/v3.6.1.tar.gz)
+
+ ExternalSource_Download(PROTOBUF ${PROTOBUF_URL})
+
+ set(ProtobufSource_DIR ${PROTOBUF_SOURCE_DIR} PARENT_SCOPE)
+ set(ProtobufSource_FOUND TRUE PARENT_SCOPE)
+endfunction(_ProtobufSource_import)
+
+_ProtobufSource_import()