Skip to content

Commit e56d605

Browse files
mshabuninvpisarev
authored andcommitted
Do not build protobuf without dnn (opencv#10689)
* Do not build protobuf if dnn is disabled * Added BUILD_LIST cmake option to the cache * Moved protobuf to the top level * Fixed static build * Fixed world build * fixup! Fixed world build
1 parent 36222c9 commit e56d605

8 files changed

+113
-67
lines changed

3rdparty/protobuf/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
project(libprotobuf)
2-
32
include(CheckIncludeFiles)
43

54
if(HAVE_PTHREAD)
@@ -136,8 +135,7 @@ append_if_exist(Protobuf_SRCS
136135
)
137136

138137
add_library(libprotobuf STATIC ${Protobuf_SRCS})
139-
ocv_include_directories(${PROTOBUF_ROOT}/src)
140-
138+
target_include_directories(libprotobuf SYSTEM PUBLIC $<BUILD_INTERFACE:${PROTOBUF_ROOT}/src>)
141139
set_target_properties(libprotobuf
142140
PROPERTIES
143141
FOLDER "3rdparty"
@@ -146,6 +144,9 @@ set_target_properties(libprotobuf
146144
ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
147145
)
148146

147+
get_protobuf_version(Protobuf_VERSION "${PROTOBUF_ROOT}/src")
148+
set(Protobuf_VERSION ${Protobuf_VERSION} CACHE INTERNAL "" FORCE)
149+
149150
if(NOT BUILD_SHARED_LIBS)
150151
ocv_install_target(libprotobuf EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
151152
endif()

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ ocv_clear_vars(OpenCVModules_TARGETS)
126126

127127
include(cmake/OpenCVDownload.cmake)
128128

129+
set(BUILD_LIST "" CACHE STRING "Build only listed modules (comma-separated, e.g. 'videoio,dnn,ts')")
130+
129131
# ----------------------------------------------------------------------------
130132
# Break in case of popular CMake configuration mistakes
131133
# ----------------------------------------------------------------------------
@@ -625,6 +627,7 @@ include(cmake/OpenCVFindLibsGUI.cmake)
625627
include(cmake/OpenCVFindLibsVideo.cmake)
626628
include(cmake/OpenCVFindLibsPerf.cmake)
627629
include(cmake/OpenCVFindLAPACK.cmake)
630+
include(cmake/OpenCVFindProtobuf.cmake)
628631

629632
# ----------------------------------------------------------------------------
630633
# Detect other 3rd-party libraries/tools
@@ -1359,6 +1362,10 @@ endif()
13591362

13601363
status(" Custom HAL:" OpenCV_USED_HAL THEN "YES (${OpenCV_USED_HAL})" ELSE "NO")
13611364

1365+
foreach(s ${CUSTOM_STATUS})
1366+
status(${CUSTOM_STATUS_${s}})
1367+
endforeach()
1368+
13621369
if(WITH_CUDA OR HAVE_CUDA)
13631370
ocv_build_features_string(cuda_features
13641371
IF HAVE_CUFFT THEN "CUFFT"

cmake/OpenCVFindLibProtobuf.cmake

-33
This file was deleted.

cmake/OpenCVFindProtobuf.cmake

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# If protobuf is found - libprotobuf target is available
2+
3+
ocv_option(WITH_PROTOBUF "Enable libprotobuf" ON)
4+
ocv_option(BUILD_PROTOBUF "Force to build libprotobuf from sources" ON)
5+
ocv_option(PROTOBUF_UPDATE_FILES "Force rebuilding .proto files (protoc should be available)" OFF)
6+
7+
set(HAVE_PROTOBUF FALSE)
8+
9+
if(NOT WITH_PROTOBUF)
10+
return()
11+
endif()
12+
13+
function(get_protobuf_version version include)
14+
file(STRINGS "${include}/google/protobuf/stubs/common.h" ver REGEX "#define GOOGLE_PROTOBUF_VERSION [0-9]+")
15+
string(REGEX MATCHALL "[0-9]+" ver ${ver})
16+
math(EXPR major "${ver} / 1000000")
17+
math(EXPR minor "${ver} / 1000 % 1000")
18+
math(EXPR patch "${ver} % 1000")
19+
set(${version} "${major}.${minor}.${patch}" PARENT_SCOPE)
20+
endfunction()
21+
22+
if(BUILD_PROTOBUF)
23+
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/protobuf")
24+
set(HAVE_PROTOBUF TRUE)
25+
else()
26+
unset(Protobuf_VERSION CACHE)
27+
find_package(Protobuf QUIET)
28+
29+
# Backwards compatibility
30+
# Define camel case versions of input variables
31+
foreach(UPPER
32+
PROTOBUF_FOUND
33+
PROTOBUF_LIBRARY
34+
PROTOBUF_INCLUDE_DIR
35+
PROTOBUF_VERSION
36+
)
37+
if (DEFINED ${UPPER})
38+
string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
39+
if (NOT DEFINED ${Camel})
40+
set(${Camel} ${${UPPER}})
41+
endif()
42+
endif()
43+
endforeach()
44+
# end of compatibility block
45+
46+
if(Protobuf_FOUND)
47+
if(TARGET protobuf::libprotobuf)
48+
add_library(libprotobuf INTERFACE)
49+
target_link_libraries(libprotobuf INTERFACE protobuf::libprotobuf)
50+
else()
51+
add_library(libprotobuf UNKNOWN IMPORTED)
52+
set_target_properties(libprotobuf PROPERTIES
53+
IMPORTED_LOCATION "${Protobuf_LIBRARY}"
54+
INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}"
55+
)
56+
get_protobuf_version(Protobuf_VERSION "${Protobuf_INCLUDE_DIR}")
57+
endif()
58+
set(HAVE_PROTOBUF TRUE)
59+
endif()
60+
endif()
61+
62+
if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP)
63+
find_package(Protobuf QUIET)
64+
if(NOT COMMAND PROTOBUF_GENERATE_CPP)
65+
message(FATAL_ERROR "PROTOBUF_GENERATE_CPP command is not available")
66+
endif()
67+
endif()
68+
69+
if(HAVE_PROTOBUF)
70+
list(APPEND CUSTOM_STATUS protobuf)
71+
list(APPEND CUSTOM_STATUS_protobuf " Protobuf:"
72+
BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})"
73+
ELSE "${Protobuf_LIBRARY} (${Protobuf_VERSION})")
74+
endif()

cmake/OpenCVModule.cmake

+8-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,14 @@ macro(ocv_module_include_directories)
653653
"${OPENCV_MODULE_${the_module}_LOCATION}/src"
654654
"${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
655655
)
656-
ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
656+
foreach(arg ${ARGN})
657+
if(IS_ABSOLUTE "${arg}")
658+
list(APPEND incs "${arg}")
659+
else()
660+
list(APPEND incs "${OPENCV_MODULE_${the_module}_LOCATION}/${arg}")
661+
endif()
662+
endforeach()
663+
ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${incs})
657664
endmacro()
658665

659666

modules/dnn/CMakeLists.txt

+11-25
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ if(WINRT)
22
ocv_module_disable(dnn)
33
endif()
44

5-
if(DEFINED BUILD_opencv_dnn AND NOT BUILD_opencv_dnn)
6-
return()
7-
endif()
8-
9-
include(${OpenCV_SOURCE_DIR}/cmake/OpenCVFindLibProtobuf.cmake)
10-
if(NOT Protobuf_FOUND)
5+
if(NOT HAVE_PROTOBUF)
116
ocv_module_disable(opencv_dnn)
127
endif()
138

@@ -21,8 +16,6 @@ ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses -Wmaybe-uninit
2116
)
2217
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4701 /wd4100)
2318

24-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/ocl4dnn/include ${OPENCL_INCLUDE_DIRS})
25-
2619
if(MSVC)
2720
add_definitions( -D_CRT_SECURE_NO_WARNINGS=1 )
2821
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146
@@ -45,8 +38,9 @@ if(ANDROID)
4538
add_definitions(-DDISABLE_POSIX_MEMALIGN -DTH_DISABLE_HEAP_TRACKING)
4639
endif()
4740

48-
#supress warnings in autogenerated caffe.pb.* files
4941
add_definitions(-DHAVE_PROTOBUF=1)
42+
43+
#supress warnings in autogenerated caffe.pb.* files
5044
ocv_warnings_disable(CMAKE_CXX_FLAGS
5145
-Wunused-parameter -Wundef -Wignored-qualifiers -Wno-enum-compare
5246
-Wdeprecated-declarations
@@ -59,26 +53,18 @@ ocv_warnings_disable(CMAKE_CXX_FLAGS
5953
)
6054

6155
if(PROTOBUF_UPDATE_FILES)
62-
file(GLOB proto_files ${CMAKE_CURRENT_SOURCE_DIR}/src/tensorflow/*.proto)
63-
list(APPEND proto_files ${CMAKE_CURRENT_SOURCE_DIR}/src/caffe/opencv-caffe.proto)
56+
file(GLOB proto_files "${CMAKE_CURRENT_SOURCE_DIR}/src/tensorflow/*.proto" "${CMAKE_CURRENT_SOURCE_DIR}/src/caffe/opencv-caffe.proto")
6457
set(PROTOBUF_GENERATE_CPP_APPEND_PATH ON) # required for tensorflow
65-
PROTOBUF_GENERATE_CPP(Protobuf_HDRS Protobuf_SRCS ${proto_files})
58+
protobuf_generate_cpp(fw_srcs fw_hdrs ${proto_files})
6659
else()
67-
file(GLOB fw_srcs ${CMAKE_CURRENT_SOURCE_DIR}/misc/tensorflow/*.cc)
68-
file(GLOB fw_hdrs ${CMAKE_CURRENT_SOURCE_DIR}/misc/tensorflow/*.h)
69-
list(APPEND fw_srcs ${CMAKE_CURRENT_SOURCE_DIR}/misc/caffe/opencv-caffe.pb.cc)
70-
list(APPEND fw_hdrs ${CMAKE_CURRENT_SOURCE_DIR}/misc/caffe/opencv-caffe.pb.h)
71-
list(APPEND Protobuf_SRCS ${fw_srcs})
72-
list(APPEND Protobuf_HDRS ${fw_hdrs})
73-
list(APPEND Protobuf_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/misc/caffe)
74-
list(APPEND Protobuf_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/misc/tensorflow)
60+
file(GLOB fw_srcs "${CMAKE_CURRENT_SOURCE_DIR}/misc/tensorflow/*.cc" "${CMAKE_CURRENT_SOURCE_DIR}/misc/caffe/opencv-caffe.pb.cc")
61+
file(GLOB fw_hdrs "${CMAKE_CURRENT_SOURCE_DIR}/misc/tensorflow/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/misc/caffe/opencv-caffe.pb.h")
62+
set(fw_inc "misc/caffe" "misc/tensorflow")
7563
endif()
7664

77-
ocv_source_group("Src\\protobuf" FILES ${Protobuf_SRCS} ${Protobuf_HDRS})
78-
ocv_module_include_directories(include ${Protobuf_INCLUDE_DIRS})
79-
80-
ocv_glob_module_sources(${Protobuf_SRCS} ${Protobuf_HDRS} ${CBLAS_H_PROXY_PATH})
81-
ocv_create_module(${Protobuf_LIBRARIES} ${LAPACK_LIBRARIES})
65+
ocv_module_include_directories(${fw_inc} src/ocl4dnn/include ${OPENCL_INCLUDE_DIRS})
66+
ocv_glob_module_sources(SOURCES ${fw_srcs})
67+
ocv_create_module(libprotobuf ${LAPACK_LIBRARIES})
8268
ocv_add_samples()
8369
ocv_add_accuracy_tests()
8470
ocv_add_perf_tests()

modules/dnn/src/caffe/caffe_shrinker.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ void shrinkCaffeModel(const String& src, const String& dst, const std::vector<St
5454
blob->set_raw_data_type(caffe::FLOAT16);
5555
}
5656
}
57+
#if GOOGLE_PROTOBUF_VERSION < 3005000
58+
size_t msgSize = saturate_cast<size_t>(net.ByteSize());
59+
#else
5760
size_t msgSize = net.ByteSizeLong();
61+
#endif
5862
std::vector<uint8_t> output(msgSize);
5963
net.SerializeWithCachedSizesToArray(&output[0]);
6064

modules/world/CMakeLists.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ endif()
3838

3939
ocv_add_module(world opencv_core)
4040

41-
set(headers_list "HEADERS")
42-
set(sources_list "SOURCES")
41+
set(headers_list)
42+
set(sources_list)
4343
set(link_deps "")
4444
foreach(m ${OPENCV_MODULE_${the_module}_DEPS} opencv_world)
4545
if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
46-
set(headers_list "${headers_list};${OPENCV_MODULE_${m}_HEADERS}")
47-
set(sources_list "${sources_list};${OPENCV_MODULE_${m}_SOURCES}")
46+
list(APPEND headers_list ${OPENCV_MODULE_${m}_HEADERS})
47+
list(APPEND sources_list ${OPENCV_MODULE_${m}_SOURCES})
4848
endif()
4949
if(NOT " ${OPENCV_MODULE_${m}_LINK_DEPS}" STREQUAL " ")
5050
list(APPEND link_deps ${OPENCV_MODULE_${m}_LINK_DEPS})
5151
endif()
5252
endforeach()
5353

54-
ocv_glob_module_sources(${headers_list} ${sources_list})
54+
ocv_glob_module_sources(HEADERS ${headers_list} SOURCES ${sources_list})
5555

5656
ocv_module_include_directories()
5757

0 commit comments

Comments
 (0)