Skip to content

Commit

Permalink
Make symbols hidden by default (#120)
Browse files Browse the repository at this point in the history
* Make symbols hidden by default and export the templated types for OS that need that
* Add cmake configure control to disable the behavior on non DLL / windows MSVC builds
* misc cleanup in header includes, extra types in ImathForward that are no longer part of the library

Signed-off-by: Kimball Thurston <[email protected]>
  • Loading branch information
kdt3rd authored Mar 15, 2021
1 parent 21e2dc0 commit 6d14064
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 72 deletions.
5 changes: 5 additions & 0 deletions config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
if (IMATH_ENABLE_LARGE_STACK)
set(IMATH_HAVE_LARGE_STACK ON)
endif()
if (IMATH_USE_DEFAULT_VISIBILITY)
set(IMATH_ENABLE_API_VISIBILITY OFF)
else()
set(IMATH_ENABLE_API_VISIBILITY ON)
endif()

configure_file(ImathConfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/ImathConfig.h)

Expand Down
18 changes: 17 additions & 1 deletion config/ImathConfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/////////////////////

//
// Define and set to 1 if the target system has support for large
// Define if the target system has support for large
// stack sizes.
//
#cmakedefine IMATH_HAVE_LARGE_STACK
Expand Down Expand Up @@ -113,4 +113,20 @@
# define IMATH_DEPRECATED(msg) /* unsupported on this platform */
#endif

// Whether the user configured the library to have symbol visibility
// tagged
#cmakedefine IMATH_ENABLE_API_VISIBILITY

// MSVC does not do the same visibility attributes, and when we are
// compiling a static library we won't be in DLL mode, but just don't
// define these and the export headers will work out
#if ! defined(_MSC_VER) && defined(IMATH_ENABLE_API_VISIBILITY)
# define IMATH_PUBLIC_SYMBOL_ATTRIBUTE __attribute__ ((__visibility__ ("default")))
# define IMATH_PRIVATE_SYMBOL_ATTRIBUTE __attribute__ ((__visibility__ ("hidden")))
// clang differs from gcc and has type visibility which is needed for enums and templates
# if __has_attribute(__type_visibility__)
# define IMATH_PUBLIC_TYPE_VISIBILITY_ATTRIBUTE __attribute__ ((__type_visibility__ ("default")))
# endif
#endif

#endif // INCLUDED_IMATH_CONFIG_H
2 changes: 2 additions & 0 deletions config/ImathSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ if(NOT "${CMAKE_PROJECT_NAME}" STREQUAL "${PROJECT_NAME}")
message(STATUS "Imath is configuring as a cmake sub project")
endif()

option(IMATH_USE_DEFAULT_VISIBILITY "Makes the compile use default visibility (by default compiles tidy, hidden-by-default)" OFF)

# This is primarily for the halfFunction code that enables a stack
# object (if you enable this) that contains a LUT of the function
option(IMATH_ENABLE_LARGE_STACK "Enables code to take advantage of large stack support" OFF)
Expand Down
11 changes: 10 additions & 1 deletion config/LibraryDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ function(IMATH_DEFINE_LIBRARY libname)
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)
)
if (NOT IMATH_USE_DEFAULT_VISIBILITY)
set_target_properties(${objlib} PROPERTIES
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
else()
target_compile_definitions(${objlib} PUBLIC IMATH_USE_DEFAULT_VISIBILITY)
endif()
if (_imath_extra_flags)
target_compile_options(${objlib} PUBLIC ${_imath_extra_flags})
endif()
Expand Down
8 changes: 5 additions & 3 deletions src/Imath/ImathBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#ifndef INCLUDED_IMATHBOX_H
#define INCLUDED_IMATHBOX_H

#include "ImathExport.h"
#include "ImathNamespace.h"

#include "ImathVec.h"

IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
Expand All @@ -32,7 +34,7 @@ IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
/// preferably, this returns a constant expression, typically 2 or 3.
///

template <class V> class Box
template <class V> class IMATH_EXPORT_TEMPLATE_TYPE Box
{
public:

Expand Down Expand Up @@ -351,7 +353,7 @@ template <typename V> class Box;
/// public.
///

template <class T> class Box<Vec2<T>>
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box<Vec2<T>>
{
public:

Expand Down Expand Up @@ -620,7 +622,7 @@ Box<Vec2<T>>::majorAxis() const noexcept
/// The Box<Vec3> template represents a 3D bounding box defined by
/// minimum and maximum values of type Vec3.
///
template <class T> class Box<Vec3<T>>
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box<Vec3<T>>
{
public:

Expand Down
3 changes: 2 additions & 1 deletion src/Imath/ImathBoxAlgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#ifndef INCLUDED_IMATHBOXALGO_H
#define INCLUDED_IMATHBOXALGO_H

#include "ImathNamespace.h"

#include "ImathBox.h"
#include "ImathLineAlgo.h"
#include "ImathMatrix.h"
#include "ImathNamespace.h"
#include "ImathPlane.h"

IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
Expand Down
6 changes: 4 additions & 2 deletions src/Imath/ImathColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#ifndef INCLUDED_IMATHCOLOR_H
#define INCLUDED_IMATHCOLOR_H

#include "ImathExport.h"
#include "ImathNamespace.h"

#include "ImathVec.h"
#include "half.h"

Expand All @@ -25,7 +27,7 @@ IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
/// Note: because Color3 inherits from Vec3, its member fields are
/// called `x`, `y`, and `z`.

template <class T> class Color3 : public Vec3<T>
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Color3 : public Vec3<T>
{
public:

Expand Down Expand Up @@ -111,7 +113,7 @@ template <class T> class Color3 : public Vec3<T>
/// can represent either rgb or hsv color values.
///

template <class T> class Color4
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Color4
{
public:

Expand Down
5 changes: 3 additions & 2 deletions src/Imath/ImathColorAlgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#ifndef INCLUDED_IMATHCOLORALGO_H
#define INCLUDED_IMATHCOLORALGO_H

#include "ImathColor.h"
#include "ImathNamespace.h"
#include "ImathExport.h"

#include "ImathColor.h"
#include "ImathMath.h"
#include "ImathNamespace.h"

IMATH_INTERNAL_NAMESPACE_HEADER_ENTER

Expand Down
12 changes: 7 additions & 5 deletions src/Imath/ImathEuler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#ifndef INCLUDED_IMATHEULER_H
#define INCLUDED_IMATHEULER_H

#include "ImathExport.h"
#include "ImathNamespace.h"

#include "ImathMath.h"
#include "ImathMatrix.h"
#include "ImathNamespace.h"
#include "ImathQuat.h"
#include "ImathVec.h"

Expand Down Expand Up @@ -114,7 +116,7 @@ IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
/// The units of the rotation angles are assumed to be radians.
///

template <class T> class Euler : public Vec3<T>
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Euler : public Vec3<T>
{
public:
using Vec3<T>::x;
Expand All @@ -125,7 +127,7 @@ template <class T> class Euler : public Vec3<T>
///
/// All 24 possible orderings
///
enum Order
enum IMATH_EXPORT_ENUM Order
{
XYZ = 0x0101, // "usual" orderings
XZY = 0x0001,
Expand Down Expand Up @@ -175,7 +177,7 @@ template <class T> class Euler : public Vec3<T>
///
/// Axes
///
enum Axis
enum IMATH_EXPORT_ENUM Axis
{
X = 0,
Y = 1,
Expand All @@ -186,7 +188,7 @@ template <class T> class Euler : public Vec3<T>
/// Layout
///

enum InputLayout
enum IMATH_EXPORT_ENUM InputLayout
{
XYZLayout,
IJKLayout
Expand Down
67 changes: 59 additions & 8 deletions src/Imath/ImathExport.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,66 @@
// Copyright Contributors to the OpenEXR Project.
//

#ifndef INCLUDED_IMATHEXPORT_H
#define INCLUDED_IMATHEXPORT_H

#include "ImathConfig.h"

/// \defgroup ExportMacros Macros to manage symbol visibility
///
/// There is more information about the motivation for these macros
/// documented in the OpenEXR source tree
/// (https://github.com/AcademySoftwareFoundation/openexr) under
/// docs/SymbolVisibility.md
///
/// Imath only needs a couple of the possible macros outlined in the
/// above document, and due to it largely being inline only, does not
/// have much to do.
///
/// @{
#if defined(IMATH_DLL)
# if defined(IMATH_EXPORTS)
# define IMATH_EXPORT __declspec(dllexport)
# define IMATH_EXPORT_CONST extern __declspec(dllexport)
# else
# define IMATH_EXPORT __declspec(dllimport)
# define IMATH_EXPORT_CONST extern __declspec(dllimport)
# endif

// when building Imath as a DLL for Windows, we have to control the
// typical DLL export / import things. Luckily, the typeinfo is all
// automatic there, so only have to deal with symbols, except Windows
// has some weirdness with DLLs and extern const, so we have to
// provide a macro to handle that.

# if defined(IMATH_EXPORTS)
# define IMATH_EXPORT __declspec(dllexport)
# define IMATH_EXPORT_CONST extern __declspec(dllexport)
# else
# define IMATH_EXPORT __declspec(dllimport)
# define IMATH_EXPORT_CONST extern __declspec(dllimport)
# endif

// DLLs don't support these types of visibility controls, just leave them as empty
# define IMATH_EXPORT_TYPE
# define IMATH_EXPORT_ENUM
# define IMATH_EXPORT_TEMPLATE_TYPE

#else

# ifdef IMATH_PUBLIC_SYMBOL_ATTRIBUTE
# define IMATH_EXPORT IMATH_PUBLIC_SYMBOL_ATTRIBUTE
# define IMATH_EXPORT_CONST extern const IMATH_PUBLIC_SYMBOL_ATTRIBUTE
# else
# define IMATH_EXPORT
# define IMATH_EXPORT_CONST extern const
#endif
# endif

# ifdef IMATH_PUBLIC_TYPE_VISIBILITY_ATTRIBUTE
# define IMATH_EXPORT_ENUM IMATH_PUBLIC_TYPE_VISIBILITY_ATTRIBUTE
# define IMATH_EXPORT_TEMPLATE_TYPE IMATH_PUBLIC_TYPE_VISIBILITY_ATTRIBUTE
# define IMATH_EXPORT_TYPE IMATH_PUBLIC_TYPE_VISIBILITY_ATTRIBUTE
# else
# define IMATH_EXPORT_ENUM
# define IMATH_EXPORT_TEMPLATE_TYPE IMATH_EXPORT
# define IMATH_EXPORT_TYPE IMATH_EXPORT
# endif

#endif // IMATH_DLL

/// @}

#endif // INCLUDED_IMATHEXPORT_H
44 changes: 21 additions & 23 deletions src/Imath/ImathForward.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define INCLUDED_IMATHFORWARD_H

#include "ImathNamespace.h"
#include "ImathExport.h"

IMATH_INTERNAL_NAMESPACE_HEADER_ENTER

Expand All @@ -16,29 +17,26 @@ IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
// Basic template type declarations.
//

template <class T> class Box;
template <class T> class Color3;
template <class T> class Color4;
template <class T> class Euler;
template <class T> class Frustum;
template <class T> class FrustumTest;
template <class T> class Interval;
template <class T> class Line3;
template <class T> class Matrix33;
template <class T> class Matrix44;
template <class T> class Plane3;
template <class T> class Quat;
template <class T> class Shear6;
template <class T> class Sphere3;
template <class T> class TMatrix;
template <class T> class TMatrixBase;
template <class T> class TMatrixData;
template <class T> class Vec2;
template <class T> class Vec3;
template <class T> class Vec4;

class Rand32;
class Rand48;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Color3;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Color4;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Euler;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Frustum;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE FrustumTest;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Interval;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Line3;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix33;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix44;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Plane3;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Quat;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Shear6;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Sphere3;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Vec2;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Vec3;
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Vec4;

class IMATH_EXPORT_TYPE Rand32;
class IMATH_EXPORT_TYPE Rand48;

/// @endcond

Expand Down
6 changes: 4 additions & 2 deletions src/Imath/ImathFrustum.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
#ifndef INCLUDED_IMATHFRUSTUM_H
#define INCLUDED_IMATHFRUSTUM_H

#include "ImathExport.h"
#include "ImathNamespace.h"

#include "ImathFun.h"
#include "ImathLine.h"
#include "ImathMatrix.h"
#include "ImathNamespace.h"
#include "ImathPlane.h"
#include "ImathVec.h"

Expand All @@ -34,7 +36,7 @@ IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
/// compiler, so we use nearPlane/farPlane instead to avoid
/// issues.

template <class T> class Frustum
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Frustum
{
public:

Expand Down
6 changes: 4 additions & 2 deletions src/Imath/ImathFrustumTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#ifndef INCLUDED_IMATHFRUSTUMTEST_H
#define INCLUDED_IMATHFRUSTUMTEST_H

#include "ImathExport.h"
#include "ImathNamespace.h"

#include "ImathBox.h"
#include "ImathFrustum.h"
#include "ImathMatrix.h"
#include "ImathNamespace.h"
#include "ImathSphere.h"
#include "ImathVec.h"

Expand Down Expand Up @@ -80,7 +82,7 @@ IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
/// form, with the X components grouped into an X vector, etc.
///

template <class T> class FrustumTest
template <class T> class IMATH_EXPORT_TEMPLATE_TYPE FrustumTest
{
public:
/// @{
Expand Down
Loading

0 comments on commit 6d14064

Please sign in to comment.