Integrate Protocol Buffer 3.6.1 (#2936)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 29 Jan 2019 03:46:21 +0000 (12:46 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 29 Jan 2019 03:46:21 +0000 (12:46 +0900)
* Integrate Protocol Buffer 3.6.1

This commit integrates Protocol Buffer 3.6.1 as a nncc external package.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Update control flow

* Fix incorrect indentation

CMakeLists.txt
cmake/packages/ProtobufConfig.cmake
cmake/packages/ProtobufSourceConfig.cmake [new file with mode: 0644]

index 018d47d..859c017 100644 (file)
@@ -33,6 +33,8 @@ message(STATUS "Use '${CMAKE_BUILD_TYPE}' configuration")
 ###
 ### Configuration
 ###
+option(DOWNLOAD_PROTOBUF "Download Protocol Buffer source" ON)
+option(BUILD_PROTOBUF "Locally build Protocol Buffer from the downloaded source" ON)
 option(DOWNLOAD_EIGEN "Download Eigen source" ON)
 option(DOWNLOAD_FARMHASH "Download farmhash source" ON)
 option(DOWNLOAD_GEMMLOWP "Download GEMM low precesion library source" ON)
index ad00dd4..5679614 100644 (file)
@@ -1,27 +1,84 @@
 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)
diff --git a/cmake/packages/ProtobufSourceConfig.cmake b/cmake/packages/ProtobufSourceConfig.cmake
new file mode 100644 (file)
index 0000000..fa95f39
--- /dev/null
@@ -0,0 +1,18 @@
+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()