Static compilation for inference plugins (#8197)

* 1. Removed explicit SHARED from libraries
2. Fixed double definition for ie_layer_validators

* Fixed SEG in unit-test: order of initialization for global vars

* Added an ability to find plugins.xml from static IE

* Fixes in unit-test

* Migrated to new macro for import / export

* Minimized number of custom dllexport

* Don't use IR v7 for static libraries

* Revert for merge

* Don't enable tests with dlopen for static libraries

* Code style

* Added condition for export

* Revert format_reader

* Removed forward decalaration with external linkage

* Fixed IE linkage on Windows

* Reverted back 2 flags

* Minimal RRTI for cpuFuncTests

* Minimal RRTI for cpuFuncTests

* Still need IR v7 reader

* Fixed build

* Fixed compilation

* clang-format fix

* Removed BUILD_AS_IS and used USE_STATIC_IE

* Enable IR v7 reader as static library

* Fixed compilation for GPU plugin

* Trying to build plugins as static library

* Plugins are able provide their own name for CreatePluginEngine function

* Fixed CPU

* Fixed comments

* Fixed ENABLE_IR_V7_READER usage

* Fixed VPU

* clang-format

* Fixes

* Fix

* Load multiple plugins at once

* Fixed interpreter undefined symbols

* Trying to dynamically register static plugins

* Reverted some ngraph changes

* Fixed cpuUnitTests compilation

* Fixed compilation

* Fixed myriad

* Fixed custom_opset tests

* Reverted linker flags

* Support both static and dynamic plugins

* Fixed compilation of myriadFuncTests

* Removed duplication

* Fixes after self-review

* Fixed linkage for preprocessing

* Fixes for Windows

* Fixes

* Fixed cmake options

* Fix

* Fix

* Fix 2
This commit is contained in:
Ilya Lavrenov
2021-10-28 11:33:56 +03:00
committed by GitHub
parent a91725cedd
commit 8fb699a42c
34 changed files with 455 additions and 130 deletions

View File

@@ -51,6 +51,12 @@ endfunction()
set(VALIDATED_LIBRARIES "" CACHE INTERNAL "")
function(_ie_add_api_validator_post_build_step)
if(NOT BUILD_SHARED_LIBS)
# since _ie_add_api_validator_post_build_step
# is currently run only on shared libraries, we have nothing to test
return()
endif()
set(UWP_API_VALIDATOR_APIS "${PROGRAMFILES}/Windows Kits/10/build/universalDDIs/x64/UniversalDDIs.xml")
set(UWP_API_VALIDATOR_EXCLUSION "${UWP_SDK_PATH}/BinaryExclusionlist.xml")

View File

@@ -0,0 +1,32 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
foreach(var IE_DEVICE_NAMES IE_PLUGINS_HPP_HEADER IE_PLUGINS_HPP_HEADER_IN)
if(NOT DEFINED ${var})
message(FATAL_ERROR "${var} is required, but not defined")
endif()
endforeach()
# configure variables
set(IE_PLUGINS_DECLARATIONS "")
set(IE_PLUGINS_MAP_DEFINITION
"std::map<std::string, InferenceEngine::CreatePluginEngineFunc *> plugins_hpp = {")
foreach(dev_name IN LISTS IE_DEVICE_NAMES)
set(_IE_CREATE_PLUGIN_FUNC "CreatePluginEngine${dev_name}")
set(IE_PLUGINS_DECLARATIONS "${IE_PLUGINS_DECLARATIONS}
IE_DEFINE_PLUGIN_CREATE_FUNCTION_DECLARATION(${_IE_CREATE_PLUGIN_FUNC})")
set(IE_PLUGINS_MAP_DEFINITION "${IE_PLUGINS_MAP_DEFINITION}
{ \"${dev_name}\", ${_IE_CREATE_PLUGIN_FUNC} },")
endforeach()
set(IE_PLUGINS_MAP_DEFINITION "${IE_PLUGINS_MAP_DEFINITION}
};\n")
message("${IE_PLUGINS_DECLARATIONS}")
message("${IE_PLUGINS_MAP_DEFINITION}")
configure_file("${IE_PLUGINS_HPP_HEADER_IN}" "${IE_PLUGINS_HPP_HEADER}" @ONLY)

View File

@@ -55,8 +55,20 @@ function(ie_add_plugin)
add_cpplint_target(${obj_lib}_cpplint FOR_TARGETS ${obj_lib})
endforeach()
add_library(${IE_PLUGIN_NAME} MODULE ${input_files})
if(BUILD_SHARED_LIBS)
set(library_type MODULE)
else()
set(library_type STATIC)
endif()
add_library(${IE_PLUGIN_NAME} ${library_type} ${input_files})
target_compile_definitions(${IE_PLUGIN_NAME} PRIVATE IMPLEMENT_INFERENCE_ENGINE_PLUGIN)
if(NOT BUILD_SHARED_LIBS)
# to distinguish functions creating plugin objects
target_compile_definitions(${IE_PLUGIN_NAME} PRIVATE
IE_CREATE_PLUGIN=CreatePluginEngine${IE_PLUGIN_DEVICE_NAME})
endif()
ie_add_vs_version_file(NAME ${IE_PLUGIN_NAME}
FILEDESCRIPTION "Inference Engine ${IE_PLUGIN_DEVICE_NAME} device plugin library")
@@ -87,23 +99,25 @@ function(ie_add_plugin)
endif()
add_dependencies(ie_plugins ${IE_PLUGIN_NAME})
if(TARGET inference_engine_preproc)
if(TARGET inference_engine_preproc AND BUILD_SHARED_LIBS)
add_dependencies(${IE_PLUGIN_NAME} inference_engine_preproc)
endif()
# fake dependencies to build in the following order:
# IE -> IE readers -> IE inference plugins -> IE-based apps
if(TARGET ir_ngraph_frontend)
add_dependencies(${IE_PLUGIN_NAME} ir_ngraph_frontend)
endif()
if(TARGET inference_engine_ir_v7_reader)
add_dependencies(${IE_PLUGIN_NAME} inference_engine_ir_v7_reader)
endif()
if(TARGET onnx_ngraph_frontend)
add_dependencies(${IE_PLUGIN_NAME} onnx_ngraph_frontend)
endif()
if(TARGET paddlepaddle_ngraph_frontend)
add_dependencies(${IE_PLUGIN_NAME} paddlepaddle_ngraph_frontend)
if(BUILD_SHARED_LIBS)
if(TARGET ir_ngraph_frontend)
add_dependencies(${IE_PLUGIN_NAME} ir_ngraph_frontend)
endif()
if(TARGET inference_engine_ir_v7_reader)
add_dependencies(${IE_PLUGIN_NAME} inference_engine_ir_v7_reader)
endif()
if(TARGET onnx_ngraph_frontend)
add_dependencies(${IE_PLUGIN_NAME} onnx_ngraph_frontend)
endif()
if(TARGET paddlepaddle_ngraph_frontend)
add_dependencies(${IE_PLUGIN_NAME} paddlepaddle_ngraph_frontend)
endif()
endif()
# install rules
@@ -137,10 +151,10 @@ function(ie_add_plugin)
endfunction()
#
# ie_register_plugins(MAIN_TARGET <main target name>
# POSSIBLE_PLUGINS <list of plugins which can be build by this repo>)
# ie_register_plugins_dynamic(MAIN_TARGET <main target name>
# POSSIBLE_PLUGINS <list of plugins which can be build by this repo>)
#
macro(ie_register_plugins)
macro(ie_register_plugins_dynamic)
set(options)
set(oneValueArgs MAIN_TARGET)
set(multiValueArgs POSSIBLE_PLUGINS)
@@ -205,3 +219,73 @@ macro(ie_register_plugins)
"Registering plugins to plugins.xml config file"
VERBATIM)
endmacro()
#
# ie_register_plugins_static(MAIN_TARGET <main target name>
# POSSIBLE_PLUGINS <list of plugins which can be build by this repo>)
#
macro(ie_register_plugins_static)
set(options)
set(oneValueArgs MAIN_TARGET)
set(multiValueArgs POSSIBLE_PLUGINS)
cmake_parse_arguments(IE_REGISTER "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(device_names)
foreach(name IN LISTS PLUGIN_FILES)
string(REPLACE ":" ";" name "${name}")
list(LENGTH name length)
if(NOT ${length} EQUAL 2)
message(FATAL_ERROR "Unexpected error, please, contact developer of this script")
endif()
list(GET name 0 device_name)
list(APPEND device_names ${device_name})
list(GET name 1 plugin_name)
target_link_libraries(${IE_REGISTER_MAIN_TARGET} PRIVATE ${plugin_name})
endforeach()
set(ie_plugins_hpp "${CMAKE_CURRENT_BINARY_DIR}/ie_plugins.hpp")
set(plugins_hpp_in "${IEDevScripts_DIR}/plugins/plugins.hpp.in")
add_custom_command(OUTPUT "${ie_plugins_hpp}"
COMMAND
"${CMAKE_COMMAND}"
-D "IE_DEVICE_NAMES=${device_names}"
-D "IE_PLUGINS_HPP_HEADER_IN=${plugins_hpp_in}"
-D "IE_PLUGINS_HPP_HEADER=${ie_plugins_hpp}"
-P "${IEDevScripts_DIR}/plugins/create_plugins_hpp.cmake"
DEPENDS
"${plugins_hpp_in}"
"${IEDevScripts_DIR}/plugins/create_plugins_hpp.cmake"
COMMENT
"Generate ie_plugins.hpp for static build"
VERBATIM)
# add dependency for object files
get_target_property(sources ${IE_REGISTER_MAIN_TARGET} SOURCES)
foreach(source IN LISTS sources)
if("${source}" MATCHES "\\$\\<TARGET_OBJECTS\\:([A-Za-z0-9_]*)\\>")
# object library
set(obj_library ${CMAKE_MATCH_1})
get_target_property(obj_sources ${obj_library} SOURCES)
list(APPEND patched_sources ${obj_sources})
else()
# usual source
list(APPEND patched_sources ${source})
endif()
endforeach()
set_source_files_properties(${patched_sources} PROPERTIES OBJECT_DEPENDS ${ie_plugins_hpp})
endmacro()
#
# ie_register_plugins(MAIN_TARGET <main target name>
# POSSIBLE_PLUGINS <list of plugins which can be build by this repo>)
#
macro(ie_register_plugins)
if(BUILD_SHARED_LIBS)
ie_register_plugins_dynamic(${ARGN})
else()
ie_register_plugins_static(${ARGN})
endif()
endmacro()

View File

@@ -0,0 +1,14 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "cpp_interfaces/interface/ie_iplugin_internal.hpp"
namespace {
@IE_PLUGINS_DECLARATIONS@
@IE_PLUGINS_MAP_DEFINITION@
} // namespace

View File

@@ -26,7 +26,7 @@ set(IE_VS_VER_COMMENTS_STR "https://docs.openvinotoolkit.org/")
# [PRODUCTVERSION_QUAD <name>])
#
function(ie_add_vs_version_file)
if(NOT WIN32)
if(NOT WIN32 OR NOT BUILD_SHARED_LIBS)
return()
endif()

View File

@@ -78,6 +78,18 @@ if (ENABLE_GNA)
endif()
endif()
if(ENABLE_TESTS OR BUILD_SHARED_LIBS)
set(ENABLE_IR_V7_READER_DEFAULT ON)
else()
set(ENABLE_IR_V7_READER_DEFAULT OFF)
endif()
ie_option (ENABLE_IR_V7_READER "Enables IR v7 reader" ${ENABLE_IR_V7_READER_DEFAULT})
ie_option (ENABLE_MULTI "Enables Multi Device Plugin" ON)
ie_option (ENABLE_HETERO "Enables Hetero Device Plugin" ON)
ie_dependent_option (ENABLE_VPU "vpu targeted plugins for inference engine" ON "NOT WINDOWS_PHONE;NOT WINDOWS_STORE" OFF)
ie_dependent_option (ENABLE_MYRIAD "myriad targeted plugin for inference engine" ON "ENABLE_VPU" OFF)