Support building with -fvisibility=hidden

It is important to building with -fvisibility=hidden to hide the symbols. Quote from issue 665.

Importance of builiding with hidden visibility is well described in GCC Wiki here.

tldr: It allows compiler and linker to do more optimizations, makes startup faster and binaries smaller.

In addition, there are other requirements, such as

  1. Minimization of the set of exported symbols in order to track ABI changes
  2. compliance

To this end, support for -fvisibility=hidden has been implemented and tested in our internal build. Although we have done our best to minimize changes, there are still 366 changed files with 503 additions and 480 deletions. However, the core code is simple, and I’ll post it here for discussion(excerpt).

+++ b/build_scripts/build_usd.py

+# Hide symbols
+HIDDEN_SYMBOLS = ['-DCMAKE_CXX_VISIBILITY_PRESET=hidden',
+                  '-DCMAKE_C_VISIBILITY_PRESET=hidden',
+                  '-DCMAKE_VISIBILITY_INLINES_HIDDEN=ON']
+

@@ -1801,6 +1806,8 @@ def InstallUSD(context, force, buildArgs):
...
         extraArgs.append('-DBoost_NO_SYSTEM_PATHS=True')
+
+        extraArgs += HIDDEN_SYMBOLS

+++ pxr/base/arch/export.h
#if defined(ARCH_OS_WINDOWS)
+#   define ARCH_EXPORT_TYPE
#elif defined(ARCH_COMPILER_GCC) && ARCH_COMPILER_GCC_MAJOR >= 4 || defined(ARCH_COMPILER_CLANG)
+#   define ARCH_EXPORT_TYPE __attribute__((visibility("default")))

And many changes like this

+++ b/pxr/base/gf/camera.h
@@ -29,7 +29,7 @@ class GfFrustum;
 /// This class provides a thin wrapper on the camera data model,
 /// with a small number of computations.
 ///
-class GfCamera
+class ARCH_EXPORT_TYPE GfCamera

If anyone is interested, I’ll prepare a PR.

1 Like

Ooh! Will this fix this issue which has been causing lots of trouble on macOS?

Yes, I think so but i didn’t verify it.
In my change, all types in pxr/base/gf/vec3f.h will be visible, includeing GfVec3f. It means VtArray<GfVec3f> woule be visible too and the related type_info would be weak-external so they can be merged.
In addition to GfVec3f, tfToken, I’ve also specified default visibility for about hundreds of classes, mainly for this job.

That’s great work. This bug has been causing us problems for a long time.