add NO_EXPORT macro to unset __visibility__ attribute (#25816)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/25816

On Android we will release a small set of native APIs designed for mobile use
cases. All of needed libtorch c++ APIs are called from inside this JNI bridge:
android/pytorch_android/src/main/cpp/pytorch_jni.cpp

With NO_EXPORT set for android static library build, it will hide all
original TORCH, CAFFE2, TH/ATen APIs, which will allow linker to strip
out unused ones from mobile library when producing DSO.

If people choose to directly build libtorch DSO then it will still keep
all c++ APIs as the mobile API layer is not part of libtorch build (yet).

Test Plan:
- build libtorch statically and link into demo app;
- confirm that linker can strip out unused APIs;

Differential Revision: D17247237

Pulled By: ljk53

fbshipit-source-id: de668216b5f2130da0d6988937f98770de571c7a
This commit is contained in:
Jiakai Liu 2019-09-10 10:18:19 -07:00 committed by Facebook Github Bot
parent 8485710143
commit 6630c3f379
3 changed files with 13 additions and 0 deletions

View File

@ -293,6 +293,9 @@ endif()
# When it's disabled it builds libtorch mobile library, which contains ATen/TH ops and native support for
# TorchScript model, but doesn't contain not-yet-unified caffe2 ops;
if (INTERN_BUILD_MOBILE AND NOT BUILD_CAFFE2_MOBILE)
if (NOT BUILD_SHARED_LIBS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNO_EXPORT")
endif()
set(BUILD_PYTHON OFF)
set(BUILD_CAFFE2_OPS OFF)
set(USE_DISTRIBUTED OFF)

View File

@ -48,6 +48,11 @@
#endif // defined(__GNUC__)
#endif // _WIN32
#ifdef NO_EXPORT
#undef TH_CPP_API
#define TH_CPP_API
#endif
#define TH_API TH_EXTERNC TH_CPP_API
#ifdef _WIN32

View File

@ -64,6 +64,11 @@
#define C10_IMPORT C10_EXPORT
#endif // _WIN32
#ifdef NO_EXPORT
#undef C10_EXPORT
#define C10_EXPORT
#endif
// Definition of an adaptive XX_API macro, that depends on whether you are
// building the library itself or not, routes to XX_EXPORT and XX_IMPORT.
// Basically, you will need to do this for each shared library that you are