Skip to content

Commit

Permalink
onnx proto symbols visibility clean-up
Browse files Browse the repository at this point in the history
Signed-off-by: mbencer <mateusz.bencer@intel.com>
  • Loading branch information
mbencer committed Mar 30, 2021
1 parent 478124d commit ef56321
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
45 changes: 36 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,44 @@ if (MSVC)
else()
set(EXTRA_FLAGS "")
endif()
if (BUILD_SHARED_LIBS OR ONNX_BUILD_MAIN_LIB)
set(ONNX_API_DEFINE "-DONNX_API=__declspec(dllexport)")
endif()

# onnx_proto symbols visibility
if(MSVC)
if(ONNX_BUILD_MAIN_LIB)
# ONNX_BUILD_MAIN_LIB can be be set if onnx_proto is static,
# but it is linked into a shared library which wants
# to export the ONNX APIs and classes.
target_compile_definitions(onnx_proto PUBLIC "ONNX_API=__declspec(dllexport)")

elseif(BUILD_SHARED_LIBS)
# users of onnx_proto import symbols
target_compile_definitions(onnx_proto INTERFACE "ONNX_API=__declspec(dllimport)")
# onnx_proto exports symbols
target_compile_definitions(onnx_proto PRIVATE "ONNX_API=__declspec(dllexport)")

else()
set(ONNX_API_DEFINE "-DONNX_API=")
# Windows static libs don't need symbols visibility decorators
target_compile_definitions(onnx_proto PUBLIC "ONNX_API=")
endif()
else()
# On non-Windows, hide all symbols we don't need
set(ONNX_API_DEFINE "-DONNX_API=__attribute__\(\(__visibility__\(\"default\"\)\)\)")
set_target_properties(onnx_proto PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(onnx_proto PROPERTIES VISIBILITY_INLINES_HIDDEN 1)
# On non-Windows, set default visibility if built as shared or exporting symbols forced
if(ONNX_BUILD_MAIN_LIB OR BUILD_SHARED_LIBS)
target_compile_definitions(onnx_proto PUBLIC "ONNX_API=__attribute__\(\(__visibility__\(\"default\"\)\)\)")
set_target_properties(onnx_proto PROPERTIES
CXX_VISIBILITY_PRESET default
C_VISIBILITY_PRESET default
VISIBILITY_INLINES_HIDDEN OFF)

else() # hide symbols for static lib case
target_compile_definitions(onnx_proto PUBLIC "ONNX_API=__attribute__\(\(__visibility__\(\"hidden\"\)\)\)")
set_target_properties(onnx_proto PROPERTIES
CXX_VISIBILITY_PRESET hidden
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)

endif()
endif()
target_compile_definitions(onnx_proto PRIVATE ${ONNX_API_DEFINE})

if(ONNX_USE_LITE_PROTO)
if(TARGET protobuf::libprotobuf-lite)
Expand All @@ -409,7 +435,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
# So, create a object library
add_library(onnx OBJECT ${ONNX_SRCS})
else()
add_library(onnx ${ONNX_SRCS})
# onnx target doesn't export symbols and should be built as static lib
add_library(onnx STATIC ${ONNX_SRCS})
endif()

target_include_directories(onnx PUBLIC
Expand Down
42 changes: 0 additions & 42 deletions onnx/onnx_pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,6 @@
#ifndef ONNX_ONNX_PB_H
#define ONNX_ONNX_PB_H

// Defines ONNX_EXPORT and ONNX_IMPORT. On Windows, this corresponds to
// different declarations (dllexport and dllimport). On Linux/Mac, it just
// resolves to the same "default visibility" setting.
#if defined(_MSC_VER)
#if defined(ONNX_BUILD_SHARED_LIBS) || defined(ONNX_BUILD_MAIN_LIB)
#define ONNX_EXPORT __declspec(dllexport)
#define ONNX_IMPORT __declspec(dllimport)
#else
#define ONNX_EXPORT
#define ONNX_IMPORT
#endif
#else
#if defined(__GNUC__)
#define ONNX_EXPORT __attribute__((__visibility__("default")))
#else
#define ONNX_EXPORT
#endif
#define ONNX_IMPORT ONNX_EXPORT
#endif

// ONNX_API is a macro that, depends on whether you are building the
// main ONNX library or not, resolves to either ONNX_EXPORT or
// ONNX_IMPORT.
//
// This is used in e.g. ONNX's protobuf files: when building the main library,
// it is defined as ONNX_EXPORT to fix a Windows global-variable-in-dll
// issue, and for anyone dependent on ONNX it will be defined as
// ONNX_IMPORT. ONNX_BUILD_MAIN_LIB can also be set when being built
// statically if ONNX is being linked into a shared library that wants
// to export the ONNX APIs and classes.
//
// More details on Windows dllimport / dllexport can be found at
// https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx
//
// This solution is similar to
// https://github.com/pytorch/pytorch/blob/master/caffe2/core/common.h
#if defined(ONNX_BUILD_SHARED_LIBS) || defined(ONNX_BUILD_MAIN_LIB)
#define ONNX_API ONNX_EXPORT
#else
#define ONNX_API ONNX_IMPORT
#endif

#ifdef ONNX_ML
#include "onnx/onnx-ml.pb.h"
#else
Expand Down

0 comments on commit ef56321

Please sign in to comment.