Skip to content

Commit

Permalink
consolidate cpp & python proto genration macro to single one
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Baksheev committed Jan 7, 2015
1 parent 57b7a3b commit 1fc6f2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 88 deletions.
104 changes: 18 additions & 86 deletions cmake/ProtoBuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,131 +28,63 @@ include_directories(SYSTEM "${CMAKE_BINARY_DIR}/include")
set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)

################################################################################################
# Modification of standard 'protobuf_generate_cpp()' with support of setting output directory
# Modification of standard 'protobuf_generate_cpp()' with output dir parameter and python support
# Usage:
# caffe_protobuf_generate_cpp(<output_directory> <srcs_variable> <hdrs_variable> <proto_files_list>)
function(caffe_protobuf_generate_cpp output_dir srcs_var hdrs_var)
# caffe_protobuf_generate_cpp_py(<output_dir> <srcs_var> <hdrs_var> <python_var> <proto_files>)
function(caffe_protobuf_generate_cpp_py output_dir srcs_var hdrs_var python_var)
if(NOT ARGN)
message(SEND_ERROR "Error: caffe_protobuf_generate_cpp() called without any proto files")
message(SEND_ERROR "Error: caffe_protobuf_generate_cpp_py() called without any proto files")
return()
endif()

file(MAKE_DIRECTORY ${output_dir})

if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
# Create an include path for each file specified
foreach(fil ${ARGN})
get_filename_component(abs_fil ${fil} ABSOLUTE)
get_filename_component(abs_path ${abs_fil} PATH)
list(FIND _protobuf_include_path ${abs_path} _contains_already)
list(FIND _protoc_include ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${abs_path})
list(APPEND _protoc_include -I ${abs_path})
endif()
endforeach()
else()
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
set(_protoc_include -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if(DEFINED PROTOBUF_IMPORT_DIRS)
foreach(dir ${PROTOBUF_IMPORT_DIRS})
get_filename_component(abs_path ${dir} ABSOLUTE)
list(FIND _protobuf_include_path ${abs_path} _contains_already)
list(FIND _protoc_include ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${abs_path})
list(APPEND _protoc_include -I ${abs_path})
endif()
endforeach()
endif()

set(${srcs_var})
set(${hdrs_var})
set(${python_var})
foreach(fil ${ARGN})
get_filename_component(abs_fil ${fil} ABSOLUTE)
get_filename_component(fil_we ${fil} NAME_WE)

list(APPEND ${srcs_var} "${output_dir}/${fil_we}.pb.cc")
list(APPEND ${hdrs_var} "${output_dir}/${fil_we}.pb.h")
list(APPEND ${python_var} "${output_dir}/${fil_we}_pb2.py")

# note, the command is executed only if output file(s) doesn't exist.
# to execture the command every build need to convert it to PRE_BUILD cmd
# i.e. add target name parameter to the macro and replace
# "OUTPUT ${files}" with "TARGET ${target} PRE_BUILD"
add_custom_command(
OUTPUT "${output_dir}/${fil_we}.pb.cc"
"${output_dir}/${fil_we}.pb.h"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --cpp_out ${output_dir} ${_protobuf_include_path} ${abs_fil}
"${output_dir}/${fil_we}_pb2.py"
COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out ${output_dir} ${_protoc_include} ${abs_fil}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${output_dir} ${_protoc_include} ${abs_fil}
DEPENDS ${abs_fil}
COMMENT "Running C++ protocol buffer compiler on ${fil}"
VERBATIM )
COMMENT "Running C++/Python protocol buffer compiler on ${fil}" VERBATIM )
endforeach()

set_source_files_properties(${${srcs_var}} ${${hdrs_var}} PROPERTIES GENERATED TRUE)
set_source_files_properties(${${srcs_var}} ${${hdrs_var}} ${${python_var}} PROPERTIES GENERATED TRUE)
set(${srcs_var} ${${srcs_var}} PARENT_SCOPE)
set(${hdrs_var} ${${hdrs_var}} PARENT_SCOPE)
set(${python_var} ${${python_var}} PARENT_SCOPE)
endfunction()



################################################################################################
# Extention of protobuf scripts for python generatipon with output directory supoprt. This is
# not implemented in standard FindProtoBuf.cmake file
# Usage:
# caffe_protobuf_generate_py(<output_directory> <srcs_variable> <proto_files_list>)
function(caffe_protobuf_generate_py output_dir srcs_var)
if(NOT ARGN)
message(SEND_ERROR "Error: caffe_protobuf_generate_python() called without any proto files")
return()
endif(NOT ARGN)

file(MAKE_DIRECTORY ${output_dir})
if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
# Create an include path for each file specified
foreach(fil ${ARGN})
get_filename_component(abs_fil ${fil} ABSOLUTE)
get_filename_component(abs_path ${abs_fil} PATH)
list(FIND _protobuf_include_path ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${abs_path})
endif()
endforeach()
else()
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if(DEFINED PROTOBUF_IMPORT_DIRS)
foreach(dir ${PROTOBUF_IMPORT_DIRS})
get_filename_component(abs_path ${dir} ABSOLUTE)
list(FIND _protobuf_include_path ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${abs_path})
endif()
endforeach()
endif()

set(${srcs_var})
foreach(fil ${ARGN})
get_filename_component(abs_fil ${fil} ABSOLUTE)
get_filename_component(fil_we ${fil} NAME_WE)

list(APPEND ${srcs_var} "${output_dir}/${fil_we}_pb2.py")

# note, the command is executed only if output file(s) doesn't exist.
# to execture the command every build need to convert it to PRE_BUILD cmd
# i.e. add target name parameter to the macro and replace
# "OUTPUT ${files}" with "TARGET ${target} PRE_BUILD"
add_custom_command(
OUTPUT "${output_dir}/${fil_we}_pb2.py"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --python_out ${output_dir} ${_protobuf_include_path} ${abs_fil}
DEPENDS ${abs_fil}
COMMENT "Running Python protocol buffer compiler on ${fil}"
VERBATIM )
endforeach()

set_source_files_properties(${${srcs_var}} PROPERTIES GENERATED TRUE)
set(${srcs_var} ${${srcs_var}} PARENT_SCOPE)
endfunction()




3 changes: 1 addition & 2 deletions src/caffe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# generate protobuf sources
file(GLOB proto_files proto/*.proto)
caffe_protobuf_generate_cpp(${proto_gen_folder} proto_srcs proto_hdrs ${proto_files})
caffe_protobuf_generate_py(${proto_gen_folder} proto_python ${proto_files})
caffe_protobuf_generate_cpp_py(${proto_gen_folder} proto_srcs proto_hdrs proto_python ${proto_files})

# include python files either to force generation
add_library(proto STATIC ${proto_hdrs} ${proto_srcs} ${proto_python})
Expand Down

0 comments on commit 1fc6f2c

Please sign in to comment.