Add Protobuf as an external module (#151)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 26 Apr 2018 03:48:29 +0000 (12:48 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 26 Apr 2018 03:48:29 +0000 (12:48 +0900)
This commit adds Protobuf as an external module that can be checked and
imported via nncc_find_package macro.

This commit also simplifies build scripts using this external module.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
CMakeLists.txt
cmake/packages/ProtobufConfig.cmake [new file with mode: 0644]
libs/frontend/CMakeLists.txt
libs/frontend/caffe/CMakeLists.txt

index bdf3480..29390c9 100644 (file)
@@ -34,16 +34,6 @@ if(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
   message(FATAL_ERROR "Test should be enabled to measure test coverage")
 endif(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
 
-find_package(Protobuf)
-
-if(NOT DEFINED ENABLE_CAFFE_SUPPORT)
-  set(ENABLE_CAFFE_SUPPORT ${PROTOBUF_FOUND})
-endif(NOT DEFINED ENABLE_CAFFE_SUPPORT)
-
-if(${ENABLE_CAFFE_SUPPORT} AND NOT ${PROTOBUF_FOUND})
-  message(FATAL_ERROR "Protocol buffer is required to enable Caffe frontend")
-endif(${ENABLE_CAFFE_SUPPORT} AND NOT ${PROTOBUF_FOUND})
-
 option(ENABLE_EXAMPLE_BUILD "Build examples" ON)
 option(ENABLE_CONTRIB_BUILD "Build incubating projects under contrib/" ON)
 
diff --git a/cmake/packages/ProtobufConfig.cmake b/cmake/packages/ProtobufConfig.cmake
new file mode 100644 (file)
index 0000000..58fbec6
--- /dev/null
@@ -0,0 +1,53 @@
+function(_Protobuf_import)
+  # Let's use find_package here not to export unnecessary definitions
+  find_package(Protobuf)
+
+  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)
+
+  if(NOT TARGET libprotobuf)
+    add_library(libprotobuf INTERFACE)
+    target_include_directories(libprotobuf INTERFACE ${PROTOBUF_INCLUDE_DIRS})
+    target_link_libraries(libprotobuf INTERFACE ${PROTOBUF_LIBRARIES})
+  endif(NOT TARGET libprotobuf)
+
+  set(Protobuf_FOUND TRUE PARENT_SCOPE)
+endfunction(_Protobuf_import)
+
+# TODO Allow users to download and build protobuf 3.X
+_Protobuf_import()
+
+if(Protobuf_FOUND)
+  function(Protobuf_Generate PREFIX OUTPUT_DIR PROTO_DIR)
+    get_filename_component(abs_output_dir ${OUTPUT_DIR} ABSOLUTE)
+    get_filename_component(abs_proto_dir ${PROTO_DIR} ABSOLUTE)
+
+    foreach(proto ${ARGN})
+      get_filename_component(fil "${proto}" NAME)
+      get_filename_component(dir "${proto}" DIRECTORY)
+
+      get_filename_component(fil_we "${fil}" NAME_WE)
+
+      get_filename_component(abs_fil "${abs_proto_base}/${proto}" ABSOLUTE)
+      get_filename_component(abs_dir "${abs_fil}" DIRECTORY)
+
+      list(APPEND PROTO_FILES "${abs_proto_dir}/${proto}")
+      list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb.h")
+      list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb.cc")
+    endforeach()
+
+    add_custom_command(OUTPUT ${OUTPUT_FILES}
+                       COMMAND ${CMAKE_COMMAND} -E make_directory "${abs_output_dir}"
+                       COMMAND "$<TARGET_FILE:protoc>" --cpp_out "${abs_output_dir}" -I "${abs_proto_dir}" ${PROTO_FILES})
+
+    set(${PREFIX}_SOURCES ${OUTPUT_FILES} PARENT_SCOPE)
+    set(${PREFIX}_INCLUDE_DIRS ${abs_output_dir} PARENT_SCOPE)
+  endfunction(Protobuf_Generate)
+endif(Protobuf_FOUND)
index b977ac2..1562556 100644 (file)
@@ -1,4 +1,2 @@
-if(ENABLE_CAFFE_SUPPORT)
-  add_subdirectory(caffe)
-endif(ENABLE_CAFFE_SUPPORT)
+add_subdirectory(caffe)
 add_subdirectory(tflite)
index 7930028..9aa42ba 100644 (file)
@@ -1,9 +1,28 @@
-protobuf_generate_cpp(PROTO_SRC PROTO_HDR res/caffe.proto)
+##
+## Check Prerequisites
+##
+nncc_find_package(Protobuf QUIET)
 
-add_nncc_library(nncc_frontend_caffe_core ${PROTO_SRC})
-target_include_directories(nncc_frontend_caffe_core PUBLIC ${PROTOBUF_INCLUDE_DIRS})
-target_include_directories(nncc_frontend_caffe_core PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
-target_link_libraries(nncc_frontend_caffe_core ${PROTOBUF_LIBRARIES})
+option(ENABLE_CAFFE_SUPPORT "Build Caffe frontend" ${Protobuf_FOUND})
+
+if(NOT ENABLE_CAFFE_SUPPORT)
+  return()
+endif(NOT ENABLE_CAFFE_SUPPORT)
+
+##
+## Build
+##
+nncc_find_package(Protobuf REQUIRED)
+
+set(GENERATED_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
+
+Protobuf_Generate(CAFFE_PROTO "${GENERATED_OUTPUT_DIR}"
+                              "${CMAKE_CURRENT_SOURCE_DIR}/res"
+                              "caffe.proto")
+
+add_nncc_library(nncc_frontend_caffe_core ${CAFFE_PROTO_SOURCES})
+target_include_directories(nncc_frontend_caffe_core PUBLIC ${CAFFE_PROTO_INCLUDE_DIRS})
+target_link_libraries(nncc_frontend_caffe_core libprotobuf)
 
 add_nncc_example_executable(caffe_core_io_example examples/caffe_core_io.cpp)
 nncc_example_link_libraries(caffe_core_io_example nncc_frontend_caffe_core)