Ngraph static lib (#3193)

* Fix: ITT_INCLUDE_DIR was not correctly detected

* Added NGRAPH_STATIC_LIBRARY CMake flag to optionnaly compile ngraph and link it as a static library instead of a dynamic one
- Definition of NGRAPH_STATIC_LIBRARY macro in callee and caller code to avoid dllexport/dllimport decoration
- Adding missing definitions of the explicit instantiation of FactoryRegistry<>::get
- Removed message about a non existent variable: NGRAPH_IE_STATIC_LIB_ENABLE
- Removed install export directive for ngraph in case of static library

* * Code style fix

* Added NGRAPH_STATIC_LIBRARY CMake flag to optionnaly compile ngraph and link it as a static library instead of a dynamic one
- Definition of NGRAPH_STATIC_LIBRARY macro in callee and caller code to avoid dllexport/dllimport decoration
- Adding missing definitions of the explicit instantiation of FactoryRegistry<>::get
- Removed message about a non existent variable: NGRAPH_IE_STATIC_LIB_ENABLE
- Removed install export directive for ngraph in case of static library

* - Removed redundant symbol definition
- Indented properly some comments

* - Making sure onnx is always compiled as a static library no matter what is the value of BUILD_SHARED_LIBS

* - Making sure onnx is always compiled as a static library no matter what is the value of BUILD_SHARED_LIBS

* Fixed incorrect suppression of directive

* Using BUILD_SHARED_LIBS (defaulted to ON) instead of a custom CMake option NGRAPH_STATIC_LIBRARY

* Removed useless comma

* Forcing pugixml to be static event when BUILD_SHARED_LIBS=ON globally.

* Forcing gtest for ie tests to be compiled as a static library.

* Made protobuf and gtest from ngraph always static.
Factorized all the force static logic for ngraph's externals in ngraph's CMakeLists.txt instead of external_*.cmake

* gflags is always static

* Solving cross compilation from Debian 9

* using the same pattern for other ngraph externals

Co-authored-by: emmanuelattia-philips <66060489+emmanuelattia-philips@users.noreply.github.com>
Co-authored-by: Emmanuel Attia <emmanuel.attia@philips.com>
This commit is contained in:
Ilya Lavrenov 2020-11-18 18:09:41 +03:00 committed by GitHub
parent 257b420ed6
commit 5cc7314f75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 53 additions and 22 deletions

View File

@ -50,3 +50,7 @@ ie_option (ENABLE_PROFILING_ITT "Build with ITT tracing. Optionally configure pr
ie_option (ENABLE_DOCS "build docs using Doxygen" OFF)
ie_dependent_option (ENABLE_FASTER_BUILD "Enable build features (PCH, UNITY) to speed up build time" OFF "CMAKE_VERSION VERSION_GREATER_EQUAL 3.16" OFF)
# Type of build, we add this as an explicit option to default it to ON
# FIXME: Ah this moment setting this to OFF will only build ngraph a static library
ie_option (BUILD_SHARED_LIBS "Build as a shared library" ON)

View File

@ -16,7 +16,6 @@ include(check_features_ie)
# resolving dependencies for the project
include(dependencies)
# Fuzz tests also building without ENABLE_FUZZING
include(fuzzing)

View File

@ -119,8 +119,12 @@ set (INTTYPES_FORMAT C99)
set (BUILD_TESTING OFF)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/gflags")
add_subdirectory(thirdparty/gflags)
set_target_properties(gflags_nothreads_static PROPERTIES FOLDER thirdparty)
function(add_gflags)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(thirdparty/gflags)
set_target_properties(gflags_nothreads_static PROPERTIES FOLDER thirdparty)
endfunction()
add_gflags()
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

View File

@ -103,7 +103,8 @@ ie_faster_build(${TARGET_NAME}_obj
UNITY
)
target_compile_definitions(${TARGET_NAME}_obj PRIVATE IMPLEMENT_INFERENCE_ENGINE_API)
target_compile_definitions(${TARGET_NAME}_obj PRIVATE IMPLEMENT_INFERENCE_ENGINE_API
$<TARGET_PROPERTY:ngraph::ngraph,INTERFACE_COMPILE_DEFINITIONS>)
target_include_directories(${TARGET_NAME}_obj SYSTEM PRIVATE $<TARGET_PROPERTY:ngraph::ngraph,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:pugixml,INTERFACE_INCLUDE_DIRECTORIES>)

View File

@ -43,6 +43,8 @@ target_include_directories(${TARGET_NAME}_obj PRIVATE
$<TARGET_PROPERTY:ngraph::ngraph,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:pugixml,INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_definitions(${TARGET_NAME}_obj PRIVATE $<TARGET_PROPERTY:ngraph::ngraph,INTERFACE_COMPILE_DEFINITIONS>)
# Create shared library
add_library(${TARGET_NAME} SHARED $<TARGET_OBJECTS:${TARGET_NAME}_obj>)

View File

@ -14,6 +14,7 @@ function(add_gtest_libraries)
endif()
endif ()
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(gtest)
get_target_property(gtest_include_dirs gtest INTERFACE_INCLUDE_DIRECTORIES)

View File

@ -49,6 +49,7 @@ endif()
function(ie_build_pugixml)
set(BUILD_TESTS_current ${BUILD_TESTS})
set(BUILD_TESTS OFF CACHE BOOL "Build tests" FORCE)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(pugixml)
set(BUILD_TESTS ${BUILD_TESTS_current} CACHE BOOL "Build tests" FORCE)
endfunction()

View File

@ -168,7 +168,6 @@ message(STATUS "NGRAPH_DEBUG_ENABLE: ${NGRAPH_DEBUG_ENABLE}")
message(STATUS "NGRAPH_DEPRECATED_ENABLE: ${NGRAPH_DEPRECATED_ENABLE}")
message(STATUS "NGRAPH_DYNAMIC_COMPONENTS_ENABLE: ${NGRAPH_DYNAMIC_COMPONENTS_ENABLE}")
message(STATUS "NGRAPH_EXPORT_TARGETS_ENABLE: ${NGRAPH_EXPORT_TARGETS_ENABLE}")
message(STATUS "NGRAPH_IE_STATIC_LIB_ENABLE: ${NGRAPH_IE_STATIC_LIB_ENABLE}")
message(STATUS "NGRAPH_INTERPRETER_ENABLE: ${NGRAPH_INTERPRETER_ENABLE}")
message(STATUS "NGRAPH_LIB_VERSIONING_ENABLE: ${NGRAPH_LIB_VERSIONING_ENABLE}")
message(STATUS "NGRAPH_ONNX_IMPORT_ENABLE: ${NGRAPH_ONNX_IMPORT_ENABLE}")
@ -469,6 +468,9 @@ if (NGRAPH_ONNX_IMPORT_ENABLE)
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
endif()
endif()
set(BEFORE_ONNX_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
if (NOT NGRAPH_USE_SYSTEM_PROTOBUF)
include(cmake/external_protobuf.cmake)
else()
@ -478,6 +480,8 @@ if (NGRAPH_ONNX_IMPORT_ENABLE)
if (TARGET ext_protobuf)
add_dependencies(onnx ext_protobuf)
endif()
set(BUILD_SHARED_LIBS ${BEFORE_ONNX_BUILD_SHARED_LIBS})
unset(BEFORE_ONNX_BUILD_SHARED_LIBS)
endif()
add_subdirectory(core)
@ -487,11 +491,13 @@ if (NGRAPH_EXPORT_TARGETS_ENABLE)
set(NGRAPH_TARGETS_FILE "${CMAKE_CURRENT_BINARY_DIR}/ngraphTargets.cmake")
export(TARGETS ngraph NAMESPACE ngraph:: FILE "${NGRAPH_TARGETS_FILE}")
install(EXPORT ngraphTargets
FILE ngraphTargets.cmake
NAMESPACE ngraph::
DESTINATION ${NGRAPH_COMPONENT_PREFIX}cmake
COMPONENT ngraph)
if(BUILD_SHARED_LIBS)
install(EXPORT ngraphTargets
FILE ngraphTargets.cmake
NAMESPACE ngraph::
DESTINATION ${NGRAPH_COMPONENT_PREFIX}cmake
COMPONENT ngraph)
endif()
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/share/ngraphConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/ngraphConfig.cmake
@ -510,7 +516,11 @@ endif()
add_subdirectory(frontend)
if (NGRAPH_TEST_UTIL_ENABLE)
set(BEFORE_GTEST_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
include(cmake/external_gtest.cmake)
set(BUILD_SHARED_LIBS ${BEFORE_GTEST_BUILD_SHARED_LIBS})
unset(BEFORE_GTEST_BUILD_SHARED_LIBS)
endif()
add_subdirectory(test)

View File

@ -63,15 +63,8 @@ if(NOT ext_onnx_POPULATED)
set(ONNX_CUSTOM_PROTOC_EXECUTABLE ${SYSTEM_PROTOC})
endif()
function(AddOnnxSubDirectory)
# In order to force this library to always be static
# We change the global BUILD_SHARED_LIBS inside this function scope only
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(${ext_onnx_SOURCE_DIR} ${ext_onnx_BINARY_DIR} EXCLUDE_FROM_ALL)
onnx_set_target_properties()
endfunction()
AddOnnxSubDirectory()
add_subdirectory(${ext_onnx_SOURCE_DIR} ${ext_onnx_BINARY_DIR} EXCLUDE_FROM_ALL)
onnx_set_target_properties()
else()
onnx_set_target_properties()
endif()

View File

@ -33,8 +33,9 @@ source_group("include" FILES ${PUBLIC_HEADERS})
configure_file(include/ngraph/version.in.hpp include/ngraph/version.hpp)
# Create shared library
add_library(ngraph SHARED ${LIBRARY_SRC} ${PUBLIC_HEADERS})
# Create static or shared library depending on BUILD_SHARED_LIBS
add_library(ngraph ${LIBRARY_SRC} ${PUBLIC_HEADERS})
if(COMMAND ie_faster_build)
ie_faster_build(ngraph
@ -75,6 +76,9 @@ if(NGRAPH_LIB_VERSIONING_ENABLE)
SOVERSION ${NGRAPH_API_VERSION})
endif()
target_compile_definitions(ngraph PUBLIC NGRAPH_VERSION="${NGRAPH_VERSION}")
if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(ngraph PUBLIC NGRAPH_STATIC_LIBRARY)
endif()
if (LINUX)
# nGraph links against one or more libraries (ex. LLVM) but we don't want to

View File

@ -97,4 +97,4 @@ namespace ngraph
protected:
FactoryMap m_factory_map;
};
}
} // namespace ngraph

View File

@ -25,11 +25,15 @@
#pragma warning(disable : 4275)
#endif
#ifdef NGRAPH_STATIC_LIBRARY // defined if we are building or calling NGRAPH as a static library
#define NGRAPH_API
#else
#ifdef ngraph_EXPORTS // defined if we are building the NGRAPH DLL (instead of using it)
#define NGRAPH_API NGRAPH_HELPER_DLL_EXPORT
#else
#define NGRAPH_API NGRAPH_HELPER_DLL_IMPORT
#endif // ngraph_EXPORTS
#endif // NGRAPH_STATIC_LIBRARY
#ifndef ENABLE_UNICODE_PATH_SUPPORT
#ifdef _WIN32

View File

@ -349,6 +349,10 @@ namespace ngraph
}
template class NGRAPH_API FactoryRegistry<op::util::SubGraphOp::InputDescription>;
template <>
FactoryRegistry<op::util::SubGraphOp::InputDescription>&
FactoryRegistry<op::util::SubGraphOp::InputDescription>::get();
template <>
class NGRAPH_API AttributeAdapter<std::shared_ptr<op::util::SubGraphOp::InputDescription>>
: public FactoryAttributeAdapter<op::util::SubGraphOp::InputDescription>
@ -383,6 +387,10 @@ namespace ngraph
template class NGRAPH_API FactoryRegistry<op::util::SubGraphOp::OutputDescription>;
template <>
FactoryRegistry<op::util::SubGraphOp::OutputDescription>&
FactoryRegistry<op::util::SubGraphOp::OutputDescription>::get();
template <>
class NGRAPH_API AttributeAdapter<std::shared_ptr<op::util::SubGraphOp::OutputDescription>>
: public FactoryAttributeAdapter<op::util::SubGraphOp::OutputDescription>