Skip to content

Commit 1cdcd0f

Browse files
committed
Always link protobuf-lite for mobile inference.
1 parent 0295b00 commit 1cdcd0f

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

CMakeLists.txt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ if(ANDROID OR IOS)
8282
"Disable AVX when cross-compiling for Android and iOS" FORCE)
8383
set(WITH_PYTHON OFF CACHE STRING
8484
"Disable PYTHON when cross-compiling for Android and iOS" FORCE)
85+
set(WITH_SWIG_PY OFF CACHE STRING
86+
"Disable SWIG_PY when cross-compiling for Android and iOS" FORCE)
8587
set(WITH_RDMA OFF CACHE STRING
8688
"Disable RDMA when cross-compiling for Android and iOS" FORCE)
8789
set(WITH_MKL OFF CACHE STRING
@@ -99,12 +101,12 @@ if(ANDROID OR IOS)
99101
endif()
100102

101103
set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING
102-
"A path setting third party libraries download & build directories.")
104+
"A path setting third party libraries download & build directories.")
103105

104106
if (WITH_C_API AND WITH_PYTHON)
105-
message(WARNING "It is suggest not embedded a python interpreter in Paddle "
106-
"when using C-API. It will give an unpredictable behavior when using a "
107-
"different Python interpreter from compiling.")
107+
message(WARNING "It is suggest not embedded a python interpreter in Paddle "
108+
"when using C-API. It will give an unpredictable behavior when using a "
109+
"different Python interpreter from compiling.")
108110
endif()
109111

110112
if(MOBILE_INFERENCE)
@@ -153,6 +155,10 @@ include(flags) # set paddle compile flags
153155
include(version) # set PADDLE_VERSION
154156
include(coveralls) # set code coverage
155157

158+
if(WITH_GPU)
159+
include(cuda)
160+
endif(WITH_GPU)
161+
156162

157163
include_directories("${PADDLE_SOURCE_DIR}")
158164
include_directories("${PADDLE_SOURCE_DIR}/paddle/cuda/include")
@@ -164,14 +170,14 @@ set(EXTERNAL_LIBS
164170
${GFLAGS_LIBRARIES}
165171
${GLOG_LIBRARIES}
166172
${CBLAS_LIBRARIES}
167-
${PROTOBUF_LIBRARY}
168-
${ZLIB_LIBRARIES}
169173
${PYTHON_LIBRARIES}
170174
)
171175

172-
if(WITH_GPU)
173-
include(cuda)
174-
endif(WITH_GPU)
176+
if(MOBILE_INFERENCE)
177+
list(APPEND EXTERNAL_LIBS ${PROTOBUF_LITE_LIBRARY})
178+
else()
179+
list(APPEND EXTERNAL_LIBS ${PROTOBUF_LIBRARY} ${ZLIB_LIBRARIES})
180+
endif()
175181

176182
if(WITH_MKLML)
177183
list(APPEND EXTERNAL_LIBS ${MKLML_IOMP_LIB})

cmake/external/protobuf.cmake

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ macro(PROMPT_PROTOBUF_LIB)
9494
SET(protobuf_DEPS ${ARGN})
9595

9696
MESSAGE(STATUS "Protobuf protoc executable: ${PROTOBUF_PROTOC_EXECUTABLE}")
97-
MESSAGE(STATUS "Protobuf library: ${PROTOBUF_LIBRARY}")
97+
MESSAGE(STATUS "Protobuf-lite library: ${PROTOBUF_LITE_LIBRARY}")
9898
MESSAGE(STATUS "Protobuf version: ${PROTOBUF_VERSION}")
9999
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
100100

101101
# Assuming that all the protobuf libraries are of the same type.
102-
IF(${PROTOBUF_LIBRARY} MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
102+
IF(${PROTOBUF_LITE_LIBRARY} MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
103103
SET(protobuf_LIBTYPE STATIC)
104-
ELSEIF(${PROTOBUF_LIBRARY} MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}$")
104+
ELSEIF(${PROTOBUF_LITE_LIBRARY} MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}$")
105105
SET(protobuf_LIBTYPE SHARED)
106106
ELSE()
107107
MESSAGE(FATAL_ERROR "Unknown library type: ${PROTOBUF_LIBRARY}")
@@ -128,7 +128,11 @@ macro(PROMPT_PROTOBUF_LIB)
128128
ADD_DEPENDENCIES(protoc ${dep})
129129
ENDFOREACH()
130130

131-
LIST(APPEND external_project_dependencies protobuf)
131+
IF(MOBILE_INFERENCE)
132+
LIST(APPEND external_project_dependencies protobuf_lite)
133+
ELSE()
134+
LIST(APPEND external_project_dependencies protobuf)
135+
ENDIF()
132136
RETURN()
133137
endmacro()
134138
macro(SET_PROTOBUF_VERSION)
@@ -252,10 +256,14 @@ IF(NOT PROTOBUF_FOUND)
252256

253257
IF(WITH_C_API)
254258
INSTALL(DIRECTORY ${PROTOBUF_INCLUDE_DIR} DESTINATION third_party/protobuf)
255-
IF(ANDROID)
256-
INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib/${ANDROID_ABI})
259+
IF(MOBILE_INFERENCE)
260+
IF(ANDROID)
261+
INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib/${ANDROID_ABI})
262+
ELSE()
263+
INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib)
264+
ENDIF()
257265
ELSE()
258-
INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib)
266+
INSTALL(FILES ${PROTOBUF_LIBRARY} DESTINATION third_party/protobuf/lib)
259267
ENDIF()
260268
ENDIF()
261269

cmake/generic.cmake

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,10 @@ function(go_test TARGET_NAME)
389389
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
390390
endfunction(go_test)
391391

392+
392393
# Modification of standard 'protobuf_generate_cpp()' with protobuf-lite support
393394
# Usage:
394395
# paddle_protobuf_generate_cpp(<proto_srcs> <proto_hdrs> <proto_files>)
395-
396396
function(paddle_protobuf_generate_cpp SRCS HDRS)
397397
if(NOT ARGN)
398398
message(SEND_ERROR "Error: paddle_protobuf_generate_cpp() called without any proto files")
@@ -402,10 +402,10 @@ function(paddle_protobuf_generate_cpp SRCS HDRS)
402402
set(${SRCS})
403403
set(${HDRS})
404404

405-
if (MOBILE_INFERENCE)
406-
set(EXTRA_FLAG "lite:")
405+
if(MOBILE_INFERENCE)
406+
set(EXTRA_FLAG "lite:")
407407
else()
408-
set(EXTRA_FLAG "")
408+
set(EXTRA_FLAG "")
409409
endif()
410410

411411
foreach(FIL ${ARGN})
@@ -423,8 +423,8 @@ function(paddle_protobuf_generate_cpp SRCS HDRS)
423423

424424
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}"
425425
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
426-
-I${CMAKE_CURRENT_SOURCE_DIR}
427-
--cpp_out "${EXTRA_FLAG}${CMAKE_CURRENT_BINARY_DIR}" ${ABS_FIL}
426+
-I${CMAKE_CURRENT_SOURCE_DIR}
427+
--cpp_out "${EXTRA_FLAG}${CMAKE_CURRENT_BINARY_DIR}" ${ABS_FIL}
428428
DEPENDS ${ABS_FIL} protoc
429429
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
430430
VERBATIM )
@@ -435,15 +435,20 @@ function(paddle_protobuf_generate_cpp SRCS HDRS)
435435
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
436436
endfunction()
437437

438-
439438
function(proto_library TARGET_NAME)
440439
set(oneValueArgs "")
441440
set(multiValueArgs SRCS DEPS)
442441
cmake_parse_arguments(proto_library "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
443442
set(proto_srcs)
444443
set(proto_hdrs)
445444
paddle_protobuf_generate_cpp(proto_srcs proto_hdrs ${proto_library_SRCS})
446-
cc_library(${TARGET_NAME} SRCS ${proto_srcs} DEPS ${proto_library_DEPS} protobuf)
445+
if(MOBILE_INFERENCE)
446+
list(APPEND proto_library_DEPS protobuf_lite)
447+
else()
448+
list(APPEND proto_library_DEPS protobuf)
449+
endif()
450+
list(REMOVE_DUPLICATES proto_library_DEPS)
451+
cc_library(${TARGET_NAME} SRCS ${proto_srcs} DEPS ${proto_library_DEPS})
447452
endfunction()
448453

449454
function(py_proto_compile TARGET_NAME)

proto/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if (MOBILE_INFERENCE)
1+
if(MOBILE_INFERENCE)
22
file(GLOB proto_filenames . ModelConfig.proto ParameterConfig.proto
33
TrainerConfig.proto DataConfig.proto)
44
else()

0 commit comments

Comments
 (0)