[feature][cmake] enable build fastdeploy with examples (#145)

* [feature][cmake] enable build fastdeploy with examples

* [feature][cmake] enable build fastdeploy with examples

Co-authored-by: Jason <jiangjiajun@baidu.com>
This commit is contained in:
DefTruth
2022-08-24 11:47:40 +08:00
committed by GitHub
parent 7afbe20945
commit 23eac4106b
2 changed files with 102 additions and 0 deletions
+22
View File
@@ -55,6 +55,9 @@ option(ENABLE_FDTENSOR_FUNC "Whether to compile with function of FDTensor." OFF)
option(ENABLE_OPENCV_CUDA "Whether to enable opencv with cuda, this will allow process image with GPU." OFF)
option(ENABLE_DEBUG "Whether to enable print debug information, this may reduce performance." OFF)
# Whether to build fastdeply with vision/text/... examples, only for testings.
option(BUILD_EXAMPLES "Whether to build fastdeply with vision examples" OFF)
# config GIT_URL with github mirrors to speed up dependent repos clone
option(GIT_URL "Git URL to clone dependent repos" ${GIT_URL})
if(NOT GIT_URL)
@@ -98,6 +101,16 @@ set(HEAD_DIR "${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}")
include_directories(${HEAD_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(BUILD_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
if(ENABLE_VISION)
# ENABLE_VISION_VISUALIZE must be ON if enable vision examples.
message(STATUS "Found BUILD_EXAMPLES and ENABLE_VISION ON, so, force ENABLE_VISION_VISUALIZE ON")
set(ENABLE_VISION_VISUALIZE ON CACHE BOOL "force to enable visualize vision model result toolbox" FORCE)
else()
message(WARNING "BUILD_EXAMPLES is ON, but found ENABLE_VISION OFF, will skip vision examples.")
endif()
endif()
add_definitions(-DFASTDEPLOY_LIB)
file(GLOB_RECURSE ALL_DEPLOY_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*.cc)
file(GLOB_RECURSE FDTENSOR_FUNC_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/function/*.cc)
@@ -273,6 +286,15 @@ if(MSVC)
endif()
target_link_libraries(${LIBRARY_NAME} ${DEPEND_LIBS})
# add examples after prepare include paths for third-parties
if(BUILD_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
add_definitions(-DBUILD_EXAMPLES)
if(NOT EXECUTABLE_OUTPUT_PATH STREQUAL ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()
add_subdirectory(examples)
endif()
if (WITH_TESTING AND EXISTS ${PROJECT_SOURCE_DIR}/tests)
add_definitions(-DWITH_TESTING)
include(external/gtest.cmake)
+80
View File
@@ -0,0 +1,80 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function(get_fastdeploy_example_names NAME_SPACE CLASS_NAME CC_FILE)
get_filename_component(CPP_DIR ${CC_FILE} DIRECTORY)
get_filename_component(CLASS_DIR ${CPP_DIR} DIRECTORY)
get_filename_component(NAME_SPACE_DIR ${CLASS_DIR} DIRECTORY)
get_filename_component(_CLASS_NAME ${CLASS_DIR} NAME)
get_filename_component(_NAME_SPACE ${NAME_SPACE_DIR} NAME)
set(${NAME_SPACE} ${_NAME_SPACE} PARENT_SCOPE)
set(${CLASS_NAME} ${_CLASS_NAME} PARENT_SCOPE)
endfunction()
set(EXAMPLES_NUM 0)
function(add_fastdeploy_executable FIELD CC_FILE)
# temp target name/file var in function scope
set(TEMP_TARGET_FILE ${CC_FILE})
get_filename_component(FILE_NAME ${CC_FILE} NAME)
string(REGEX REPLACE ".cc" "" FILE_NAME ${FILE_NAME})
get_fastdeploy_example_names(NAME_SPACE CLASS_NAME ${CC_FILE})
set(TEMP_TARGET_NAME ${FIELD}_${NAME_SPACE}_${CLASS_NAME}_${FILE_NAME})
if(EXISTS ${TEMP_TARGET_FILE} AND TARGET fastdeploy)
add_executable(${TEMP_TARGET_NAME} ${TEMP_TARGET_FILE})
target_link_libraries(${TEMP_TARGET_NAME} PUBLIC fastdeploy)
math(EXPR _EXAMPLES_NUM "${EXAMPLES_NUM} + 1")
set(EXAMPLES_NUM ${_EXAMPLES_NUM} PARENT_SCOPE)
string(LENGTH ${EXAMPLES_NUM} len)
set(MESSAGE_HEAD "[${EXAMPLES_NUM}]")
if(${len} EQUAL 1)
set(MESSAGE_HEAD "[00${EXAMPLES_NUM}]")
elseif(${len} EQUAL 2)
set(MESSAGE_HEAD "[0${EXAMPLES_NUM}]")
endif()
message(STATUS " ${MESSAGE_HEAD} Added FastDeploy Executable : ${TEMP_TARGET_NAME}")
endif()
unset(TEMP_TARGET_FILE)
unset(TEMP_TARGET_NAME)
endfunction()
# vision examples
if(BUILD_EXAMPLES AND ENABLE_VISION)
if(NOT ENABLE_VISION_VISUALIZE)
message(FATAL_ERROR "ENABLE_VISION_VISUALIZE must be ON while BUILD_EXAMPLES and ENABLE_VISION both ON.")
endif()
if(EXISTS ${PROJECT_SOURCE_DIR}/examples/vision)
message(STATUS "")
message(STATUS "*************FastDeploy Vision Examples Summary**********")
file(GLOB_RECURSE ALL_VISION_EXAMPLE_SRCS ${PROJECT_SOURCE_DIR}/examples/vision/*.cc)
foreach(_CC_FILE ${ALL_VISION_EXAMPLE_SRCS})
add_fastdeploy_executable(vision ${_CC_FILE})
endforeach()
message(STATUS " [FastDeploy Executable Path] : ${EXECUTABLE_OUTPUT_PATH}")
endif()
endif()
# text examples
if(BUILD_EXAMPLES AND ENABLE_TEXT)
if(EXISTS ${PROJECT_SOURCE_DIR}/examples/text)
message(STATUS "")
message(STATUS "*************FastDeploy Text Examples Summary**********")
file(GLOB_RECURSE ALL_TEXT_EXAMPLE_SRCS ${PROJECT_SOURCE_DIR}/examples/text/*.cc)
foreach(_CC_FILE ${ALL_TEXT_EXAMPLE_SRCS})
add_fastdeploy_executable(text ${_CC_FILE})
endforeach()
message(STATUS " [FastDeploy Executable Path] : ${EXECUTABLE_OUTPUT_PATH}")
endif()
endif()
# other examples ...