Skip to content

Commit

Permalink
Fix protobuf build to properly include directories
Browse files Browse the repository at this point in the history
Summary:
aaronmarkham - I think this should fix the oss build.
slayton58 FYI
kmatzen FYI
Closes facebookarchive#238

Differential Revision: D4812550

Pulled By: Yangqing

fbshipit-source-id: 5703e403ef22c02e87f885bad8379fd5a8e06cdb
  • Loading branch information
Yangqing authored and facebook-github-bot committed Mar 31, 2017
1 parent 5361564 commit fa23379
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 43 deletions.
2 changes: 1 addition & 1 deletion caffe/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

file(GLOB Caffe_PROTOBUF_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")

caffe_protobuf_generate_cpp_py(${CMAKE_CURRENT_BINARY_DIR} Caffe_PROTO_SRCS Caffe_PROTO_HEADERS Caffe_PROTO_PY ${Caffe_PROTOBUF_FILES})
caffe2_protobuf_generate_cpp_py(Caffe_PROTO_SRCS Caffe_PROTO_HEADERS Caffe_PROTO_PY ${Caffe_PROTOBUF_FILES})

add_library(Caffe_PROTO OBJECT ${Caffe_PROTO_HEADERS} ${Caffe_PROTO_SRCS})
install(FILES ${Caffe_PROTO_HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/caffe/proto)
2 changes: 1 addition & 1 deletion caffe2/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

file(GLOB Caffe2_PROTOBUF_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")

caffe_protobuf_generate_cpp_py(${CMAKE_CURRENT_BINARY_DIR} Caffe2_PROTO_SRCS Caffe2_PROTO_HEADERS Caffe2_PROTO_PY ${Caffe2_PROTOBUF_FILES})
caffe2_protobuf_generate_cpp_py(Caffe2_PROTO_SRCS Caffe2_PROTO_HEADERS Caffe2_PROTO_PY ${Caffe2_PROTOBUF_FILES})

add_library(Caffe2_PROTO OBJECT ${Caffe2_PROTO_HEADERS} ${Caffe2_PROTO_SRCS})
install(FILES ${Caffe2_PROTO_HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/caffe2/proto)
53 changes: 12 additions & 41 deletions cmake/ProtoBuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,64 +45,35 @@ if (NOT (Protobuf_FOUND OR PROTOBUF_FOUND) )
message(FATAL_ERROR "Could not find Protobuf or compile local version.")
endif()

# place where to generate protobuf sources
set(proto_gen_folder "${PROJECT_BINARY_DIR}/include/caffe/proto")
include_directories("${PROJECT_BINARY_DIR}/include")

set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)

################################################################################################
# Modification of standard 'protobuf_generate_cpp()' with output dir parameter and python support
# Usage:
# 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)
# caffe2_protobuf_generate_cpp_py(<srcs_var> <hdrs_var> <python_var> <proto_files>)
function(caffe2_protobuf_generate_cpp_py srcs_var hdrs_var python_var)
if(NOT ARGN)
message(SEND_ERROR "Error: caffe_protobuf_generate_cpp_py() called without any proto files")
return()
endif()

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 _protoc_include ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protoc_include -I ${abs_path})
endif()
endforeach()
else()
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 _protoc_include ${abs_path} _contains_already)
if(${_contains_already} EQUAL -1)
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")
list(APPEND ${srcs_var} "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.cc")
list(APPEND ${hdrs_var} "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h")
list(APPEND ${python_var} "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}_pb2.py")

add_custom_command(
OUTPUT "${output_dir}/${fil_we}.pb.cc"
"${output_dir}/${fil_we}.pb.h"
"${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}
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.cc"
"${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h"
"${CMAKE_CURRENT_BINARY_DIR}/${fil_we}_pb2.py"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} -I${PROJECT_SOURCE_DIR} --cpp_out "${PROJECT_BINARY_DIR}" ${abs_fil}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} -I${PROJECT_SOURCE_DIR} --python_out "${PROJECT_BINARY_DIR}" ${abs_fil}
DEPENDS ${abs_fil}
COMMENT "Running C++/Python protocol buffer compiler on ${fil}" VERBATIM )
endforeach()
Expand Down

0 comments on commit fa23379

Please sign in to comment.