1 # Finds Google Protocol Buffers library and compilers and extends
2 # the standard cmake script with version and python generation support
4 find_package( Protobuf REQUIRED )
5 list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${PROTOBUF_INCLUDE_DIR})
6 list(APPEND Caffe_LINKER_LIBS PUBLIC ${PROTOBUF_LIBRARIES})
8 # As of Ubuntu 14.04 protoc is no longer a part of libprotobuf-dev package
9 # and should be installed separately as in: sudo apt-get install protobuf-compiler
10 if(EXISTS ${PROTOBUF_PROTOC_EXECUTABLE})
11 message(STATUS "Found PROTOBUF Compiler: ${PROTOBUF_PROTOC_EXECUTABLE}")
13 message(FATAL_ERROR "Could not find PROTOBUF Compiler")
17 # fetches protobuf version
18 caffe_parse_header(${PROTOBUF_INCLUDE_DIR}/google/protobuf/stubs/common.h VERION_LINE GOOGLE_PROTOBUF_VERSION)
19 string(REGEX MATCH "([0-9])00([0-9])00([0-9])" PROTOBUF_VERSION ${GOOGLE_PROTOBUF_VERSION})
20 set(PROTOBUF_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
21 unset(GOOGLE_PROTOBUF_VERSION)
24 # place where to generate protobuf sources
25 set(proto_gen_folder "${PROJECT_BINARY_DIR}/include/caffe/proto")
26 include_directories("${PROJECT_BINARY_DIR}/include")
28 set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
30 ################################################################################################
31 # Modification of standard 'protobuf_generate_cpp()' with output dir parameter and python support
33 # caffe_protobuf_generate_cpp_py(<output_dir> <srcs_var> <hdrs_var> <python_var> <proto_files>)
34 function(caffe_protobuf_generate_cpp_py output_dir srcs_var hdrs_var python_var)
36 message(SEND_ERROR "Error: caffe_protobuf_generate_cpp_py() called without any proto files")
40 if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
41 # Create an include path for each file specified
43 get_filename_component(abs_fil ${fil} ABSOLUTE)
44 get_filename_component(abs_path ${abs_fil} PATH)
45 list(FIND _protoc_include ${abs_path} _contains_already)
46 if(${_contains_already} EQUAL -1)
47 list(APPEND _protoc_include -I ${abs_path})
51 set(_protoc_include -I ${CMAKE_CURRENT_SOURCE_DIR})
54 if(DEFINED PROTOBUF_IMPORT_DIRS)
55 foreach(dir ${PROTOBUF_IMPORT_DIRS})
56 get_filename_component(abs_path ${dir} ABSOLUTE)
57 list(FIND _protoc_include ${abs_path} _contains_already)
58 if(${_contains_already} EQUAL -1)
59 list(APPEND _protoc_include -I ${abs_path})
68 get_filename_component(abs_fil ${fil} ABSOLUTE)
69 get_filename_component(fil_we ${fil} NAME_WE)
71 list(APPEND ${srcs_var} "${output_dir}/${fil_we}.pb.cc")
72 list(APPEND ${hdrs_var} "${output_dir}/${fil_we}.pb.h")
73 list(APPEND ${python_var} "${output_dir}/${fil_we}_pb2.py")
76 OUTPUT "${output_dir}/${fil_we}.pb.cc"
77 "${output_dir}/${fil_we}.pb.h"
78 "${output_dir}/${fil_we}_pb2.py"
79 COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}"
80 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out ${output_dir} ${_protoc_include} ${abs_fil}
81 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${output_dir} ${_protoc_include} ${abs_fil}
83 COMMENT "Running C++/Python protocol buffer compiler on ${fil}" VERBATIM )
86 set_source_files_properties(${${srcs_var}} ${${hdrs_var}} ${${python_var}} PROPERTIES GENERATED TRUE)
87 set(${srcs_var} ${${srcs_var}} PARENT_SCOPE)
88 set(${hdrs_var} ${${hdrs_var}} PARENT_SCOPE)
89 set(${python_var} ${${python_var}} PARENT_SCOPE)