Advertisement
Guest User

Protobuf.cmake

a guest
Oct 26th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CMake 4.01 KB | None | 0 0
  1. # Finds Google Protocol Buffers library and compilers and extends
  2. # the standard cmake script with version and python generation support
  3.  
  4. SET(PROTOBUF_LIBRARY "/home/fei/mor0146/caffe/protobuf-3.0.0/src/.libs/libprotobuf.so")
  5. SET(PROTOBUF_INCLUDE_DIRECTORY "/home/fei/mor0146/caffe/protobuf-3.0.0/src/google/protobuf")
  6. SET(PROTOBUF_PROTOC_EXECUTABLE "/home/fei/mor0146/caffe/protobuf-3.0.0/src/protoc")
  7.  
  8. find_package( Protobuf REQUIRED )
  9. list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${PROTOBUF_INCLUDE_DIR})
  10. list(APPEND Caffe_LINKER_LIBS PUBLIC ${PROTOBUF_LIBRARIES})
  11.  
  12. # As of Ubuntu 14.04 protoc is no longer a part of libprotobuf-dev package
  13. # and should be installed separately as in: sudo apt-get install protobuf-compiler
  14. if(EXISTS ${PROTOBUF_PROTOC_EXECUTABLE})
  15.   message(STATUS "Found PROTOBUF Compiler: ${PROTOBUF_PROTOC_EXECUTABLE}")
  16. else()
  17.   message(FATAL_ERROR "Could not find PROTOBUF Compiler")
  18. endif()
  19.  
  20. if(PROTOBUF_FOUND)
  21.   # fetches protobuf version
  22.   caffe_parse_header(${PROTOBUF_INCLUDE_DIR}/google/protobuf/stubs/common.h VERION_LINE GOOGLE_PROTOBUF_VERSION)
  23.   string(REGEX MATCH "([0-9])00([0-9])00([0-9])" PROTOBUF_VERSION ${GOOGLE_PROTOBUF_VERSION})
  24.   set(PROTOBUF_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  25.   unset(GOOGLE_PROTOBUF_VERSION)
  26. endif()
  27.  
  28. # place where to generate protobuf sources
  29. set(proto_gen_folder "${PROJECT_BINARY_DIR}/include/caffe/proto")
  30. include_directories("${PROJECT_BINARY_DIR}/include")
  31.  
  32. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  33.  
  34. ################################################################################################
  35. # Modification of standard 'protobuf_generate_cpp()' with output dir parameter and python support
  36. # Usage:
  37. #   caffe_protobuf_generate_cpp_py(<output_dir> <srcs_var> <hdrs_var> <python_var> <proto_files>)
  38. function(caffe_protobuf_generate_cpp_py output_dir srcs_var hdrs_var python_var)
  39.   if(NOT ARGN)
  40.     message(SEND_ERROR "Error: caffe_protobuf_generate_cpp_py() called without any proto files")
  41.     return()
  42.   endif()
  43.  
  44.   if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  45.     # Create an include path for each file specified
  46.     foreach(fil ${ARGN})
  47.       get_filename_component(abs_fil ${fil} ABSOLUTE)
  48.       get_filename_component(abs_path ${abs_fil} PATH)
  49.       list(FIND _protoc_include ${abs_path} _contains_already)
  50.       if(${_contains_already} EQUAL -1)
  51.         list(APPEND _protoc_include -I ${abs_path})
  52.       endif()
  53.     endforeach()
  54.   else()
  55.     set(_protoc_include -I ${CMAKE_CURRENT_SOURCE_DIR})
  56.   endif()
  57.  
  58.   if(DEFINED PROTOBUF_IMPORT_DIRS)
  59.     foreach(dir ${PROTOBUF_IMPORT_DIRS})
  60.       get_filename_component(abs_path ${dir} ABSOLUTE)
  61.       list(FIND _protoc_include ${abs_path} _contains_already)
  62.       if(${_contains_already} EQUAL -1)
  63.         list(APPEND _protoc_include -I ${abs_path})
  64.       endif()
  65.     endforeach()
  66.   endif()
  67.  
  68.   set(${srcs_var})
  69.   set(${hdrs_var})
  70.   set(${python_var})
  71.   foreach(fil ${ARGN})
  72.     get_filename_component(abs_fil ${fil} ABSOLUTE)
  73.     get_filename_component(fil_we ${fil} NAME_WE)
  74.  
  75.     list(APPEND ${srcs_var} "${output_dir}/${fil_we}.pb.cc")
  76.     list(APPEND ${hdrs_var} "${output_dir}/${fil_we}.pb.h")
  77.     list(APPEND ${python_var} "${output_dir}/${fil_we}_pb2.py")
  78.  
  79.     add_custom_command(
  80.       OUTPUT "${output_dir}/${fil_we}.pb.cc"
  81.              "${output_dir}/${fil_we}.pb.h"
  82.              "${output_dir}/${fil_we}_pb2.py"
  83.       COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}"
  84.       COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out    ${output_dir} ${_protoc_include} ${abs_fil}
  85.       COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${output_dir} ${_protoc_include} ${abs_fil}
  86.       DEPENDS ${abs_fil}
  87.       COMMENT "Running C++/Python protocol buffer compiler on ${fil}" VERBATIM )
  88.   endforeach()
  89.  
  90.   set_source_files_properties(${${srcs_var}} ${${hdrs_var}} ${${python_var}} PROPERTIES GENERATED TRUE)
  91.   set(${srcs_var} ${${srcs_var}} PARENT_SCOPE)
  92.   set(${hdrs_var} ${${hdrs_var}} PARENT_SCOPE)
  93.   set(${python_var} ${${python_var}} PARENT_SCOPE)
  94. endfunction()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement