Skip to content

Commit c5f2b46

Browse files
committed
cmake: don't set default visibility for static builds
define SECP256K1_CMAKE_SHARED_BUILD for all CMake shared lib builds, not just Windows. This allows us to only set default symbol visibility for shared lib builds. For static libs built with CMake, or Autotools builds for Windows, static builds will prefer to hide symbols by default. Autotools does not have the ability to set a define for non-Windows shared builds, so the best we can do there is guess. SECP256K1_NO_EXPORT_SYMBOLS can be defined as an escape-hatch in case a non-Windows CMake builder wants default visibility.
1 parent 9239447 commit c5f2b46

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ AS_UNSET(ac_cv_prog_AR)
4545
AS_UNSET(ac_cv_prog_ac_ct_AR)
4646
LT_INIT([win32-dll])
4747

48+
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DSECP256K1_AUTOTOOLS_BUILD=1"
4849
build_windows=no
4950

5051
case $host_os in

include/secp256k1.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ typedef int (*secp256k1_nonce_function)(
129129
* Attributes" in the GCC manual and the recommendations in
130130
* https://gcc.gnu.org/wiki/Visibility. */
131131
# if defined(SECP256K1_BUILD)
132-
# if defined(DLL_EXPORT) || defined(SECP256K1_DLL_EXPORT)
132+
# if defined(DLL_EXPORT) || defined(SECP256K1_CMAKE_SHARED_BUILD) || defined(SECP256K1_DLL_EXPORT)
133133
/* Building libsecp256k1 as a DLL.
134134
* 1. If using Libtool, it defines DLL_EXPORT automatically.
135-
* 2. In other cases, SECP256K1_DLL_EXPORT must be defined. */
135+
* 2. If using CMake, it defines SECP256K1_CMAKE_SHARED_BUILD automatically.
136+
* 3. In other cases, SECP256K1_DLL_EXPORT must be defined. */
136137
# define SECP256K1_API extern __declspec (dllexport)
137138
# else
138139
/* Building libsecp256k1 as a static library on Windows.
@@ -155,11 +156,16 @@ typedef int (*secp256k1_nonce_function)(
155156
/* All cases not captured by the Windows-specific logic. */
156157
# if defined(__GNUC__) && (__GNUC__ >= 4) && defined(SECP256K1_BUILD)
157158
/* Building libsecp256k1 using GCC or compatible. */
158-
# define SECP256K1_API extern __attribute__ ((visibility ("default")))
159-
# else
159+
# if !defined(SECP256K1_NO_EXPORT_SYMBOLS)
160+
# if defined(SECP256K1_CMAKE_SHARED_BUILD) || defined(SECP256K1_AUTOTOOLS_BUILD)
161+
# define SECP256K1_API extern __attribute__ ((visibility ("default")))
162+
# endif
163+
# endif
164+
# endif
165+
#endif
166+
#ifndef SECP256K1_API
160167
/* Fall back to standard C's extern. */
161168
# define SECP256K1_API extern
162-
# endif
163169
#endif
164170

165171
/* Warning attributes

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ if(SECP256K1_ASM STREQUAL "arm32")
2020
target_link_libraries(secp256k1_asm INTERFACE secp256k1_asm_arm)
2121
endif()
2222

23+
# Define our export symbol only for shared libs.
24+
set_target_properties(secp256k1 PROPERTIES DEFINE_SYMBOL SECP256K1_CMAKE_SHARED_BUILD)
2325
if(WIN32)
24-
# Define our export symbol only for shared libs.
25-
set_target_properties(secp256k1 PROPERTIES DEFINE_SYMBOL SECP256K1_DLL_EXPORT)
2626
target_compile_definitions(secp256k1 INTERFACE $<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:SECP256K1_STATIC>)
2727
endif()
2828

0 commit comments

Comments
 (0)