Skip to content

Commit aa8c67f

Browse files
committed
WIP: visibility example for cmake
1 parent c498779 commit aa8c67f

File tree

3 files changed

+36
-69
lines changed

3 files changed

+36
-69
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
function(add_example name)
22
set(target_name ${name}_example)
33
add_executable(${target_name} ${name}.c)
4-
target_include_directories(${target_name} PRIVATE
5-
${PROJECT_SOURCE_DIR}/include
6-
)
7-
target_link_libraries(${target_name}
8-
secp256k1
4+
target_link_libraries(${target_name} PRIVATE
5+
libsecp256k1::secp256k1
96
$<$<PLATFORM_ID:Windows>:bcrypt>
107
)
118
set(test_name ${name}_example)

include/secp256k1.h

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,45 +121,21 @@ typedef int (*secp256k1_nonce_function)(
121121
#endif
122122

123123
/* Symbol visibility. */
124-
#if defined(_WIN32)
125-
/* GCC for Windows (e.g., MinGW) accepts the __declspec syntax
126-
* for MSVC compatibility. A __declspec declaration implies (but is not
127-
* exactly equivalent to) __attribute__ ((visibility("default"))), and so we
128-
* actually want __declspec even on GCC, see "Microsoft Windows Function
129-
* Attributes" in the GCC manual and the recommendations in
130-
* https://gcc.gnu.org/wiki/Visibility. */
131-
# if defined(SECP256K1_BUILD)
132-
# if defined(DLL_EXPORT) || defined(SECP256K1_DLL_EXPORT)
133-
/* Building libsecp256k1 as a DLL.
134-
* 1. If using Libtool, it defines DLL_EXPORT automatically.
135-
* 2. In other cases, SECP256K1_DLL_EXPORT must be defined. */
136-
# define SECP256K1_API extern __declspec (dllexport)
137-
# else
138-
/* Building libsecp256k1 as a static library on Windows.
139-
* No declspec is needed, and so we would want the non-Windows-specific
140-
* logic below take care of this case. However, this may result in setting
141-
* __attribute__ ((visibility("default"))), which is supposed to be a noop
142-
* on Windows but may trigger warnings when compiling with -flto due to a
143-
* bug in GCC, see
144-
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116478 . */
145-
# define SECP256K1_API extern
146-
# endif
147-
/* The user must define SECP256K1_STATIC when consuming libsecp256k1 as a static
148-
* library on Windows. */
149-
# elif !defined(SECP256K1_STATIC)
150-
/* Consuming libsecp256k1 as a DLL. */
151-
# define SECP256K1_API extern __declspec (dllimport)
152-
# endif
124+
#if defined(_WIN32) || defined(__CYGWIN__)
125+
# define SECP256K1_EXPORT __declspec(dllexport)
126+
# define SECP256K1_IMPORT __declspec(dllimport)
127+
#else
128+
# define SECP256K1_EXPORT __attribute__((visibility("default")))
129+
# define SECP256K1_IMPORT __attribute__((visibility("default")))
153130
#endif
154131
#ifndef SECP256K1_API
155-
/* All cases not captured by the Windows-specific logic. */
156-
# if defined(__GNUC__) && (__GNUC__ >= 4) && defined(SECP256K1_BUILD)
157-
/* Building libsecp256k1 using GCC or compatible. */
158-
# define SECP256K1_API extern __attribute__ ((visibility ("default")))
159-
# else
160-
/* Fall back to standard C's extern. */
161-
# define SECP256K1_API extern
162-
# endif
132+
# if defined(SECP256K1_STATIC)
133+
# define SECP256K1_API extern
134+
# elif defined(SECP256K1_BUILD)
135+
# define SECP256K1_API extern SECP256K1_EXPORT
136+
# else
137+
# define SECP256K1_API extern SECP256K1_IMPORT
138+
# endif
163139
#endif
164140

165141
/* Warning attributes

src/CMakeLists.txt

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
add_library(secp256k1)
22

3+
# Allow projects in the same source tree to use this as if it had been imported.
4+
# The prefix has to match the NAMESPACE in `install(EXPORT)` further down.
5+
add_library(${PROJECT_NAME}::secp256k1 ALIAS secp256k1)
6+
37
set_property(TARGET secp256k1 PROPERTY PUBLIC_HEADER
48
${PROJECT_SOURCE_DIR}/include/secp256k1.h
59
${PROJECT_SOURCE_DIR}/include/secp256k1_preallocated.h
@@ -54,40 +58,24 @@ add_library(secp256k1_precomputed OBJECT EXCLUDE_FROM_ALL
5458
# from being exported.
5559
target_sources(secp256k1 PRIVATE secp256k1.c $<TARGET_OBJECTS:secp256k1_precomputed>)
5660

57-
# Create a helper lib that parent projects can use to link secp256k1 into a
58-
# static lib.
59-
add_library(secp256k1_objs INTERFACE)
60-
target_sources(secp256k1_objs INTERFACE $<TARGET_OBJECTS:secp256k1> $<TARGET_OBJECTS:secp256k1_precomputed>)
61-
62-
add_library(secp256k1_asm INTERFACE)
6361
if(SECP256K1_ASM STREQUAL "arm32")
64-
add_library(secp256k1_asm_arm OBJECT EXCLUDE_FROM_ALL)
65-
target_sources(secp256k1_asm_arm PUBLIC
66-
asm/field_10x26_arm.s
67-
)
68-
target_sources(secp256k1 PRIVATE $<TARGET_OBJECTS:secp256k1_asm_arm>)
69-
target_sources(secp256k1_objs INTERFACE $<TARGET_OBJECTS:secp256k1_asm_arm>)
70-
target_link_libraries(secp256k1_asm INTERFACE secp256k1_asm_arm)
62+
add_library(secp256k1_asm OBJECT EXCLUDE_FROM_ALL asm/field_10x26_arm.s)
63+
else()
64+
add_library(secp256k1_asm INTERFACE)
7165
endif()
7266

73-
if(WIN32)
74-
# Define our export symbol only for shared libs.
75-
set_target_properties(secp256k1 PROPERTIES DEFINE_SYMBOL SECP256K1_DLL_EXPORT)
76-
target_compile_definitions(secp256k1 INTERFACE $<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:SECP256K1_STATIC>)
77-
endif()
67+
# When building a static libary, SECP256K1_STATIC must be defined both for itself and downstream.
68+
# Note that the generator expression is evaluated in the context of the consuming target!
69+
target_compile_definitions(secp256k1 PUBLIC $<$<STREQUAL:$<TARGET_PROPERTY:secp256k1,TYPE>,STATIC_LIBRARY>:SECP256K1_STATIC>)
70+
set_target_properties(secp256k1 PROPERTIES C_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
7871

7972
# Object libs don't know if they're being built for a shared or static lib.
8073
# Grab the PIC property from secp256k1 which knows.
8174
get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE)
8275
set_target_properties(secp256k1_precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
8376

84-
# Add the include path for parent projects so that they don't have to manually add it.
85-
target_include_directories(secp256k1 INTERFACE
86-
$<BUILD_INTERFACE:$<$<NOT:$<BOOL:${PROJECT_IS_TOP_LEVEL}>>:${PROJECT_SOURCE_DIR}/include>>
87-
)
88-
set_target_properties(secp256k1_objs PROPERTIES
89-
INTERFACE_INCLUDE_DIRECTORIES "$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>"
90-
)
77+
# Add the include path for projects in the same source tree.
78+
target_include_directories(secp256k1 INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
9179

9280
# This emulates Libtool to make sure Libtool and CMake agree on the ABI version,
9381
# see below "Calculate the version variables" in build-aux/ltmain.sh.
@@ -123,18 +111,21 @@ if(SECP256K1_BUILD_BENCHMARK)
123111
add_executable(bench bench.c)
124112
target_link_libraries(bench secp256k1)
125113
add_executable(bench_internal bench_internal.c)
114+
target_compile_definitions(bench_internal PRIVATE $<TARGET_PROPERTY:secp256k1,INTERFACE_COMPILE_DEFINITIONS>)
126115
target_link_libraries(bench_internal secp256k1_precomputed secp256k1_asm)
127116
add_executable(bench_ecmult bench_ecmult.c)
117+
target_compile_definitions(bench_ecmult PRIVATE $<TARGET_PROPERTY:secp256k1,INTERFACE_COMPILE_DEFINITIONS>)
128118
target_link_libraries(bench_ecmult secp256k1_precomputed secp256k1_asm)
129119
endif()
130120

131121
if(SECP256K1_BUILD_TESTS)
132122
add_executable(noverify_tests tests.c)
123+
target_compile_definitions(noverify_tests PRIVATE $<TARGET_PROPERTY:secp256k1,INTERFACE_COMPILE_DEFINITIONS>)
133124
target_link_libraries(noverify_tests secp256k1_precomputed secp256k1_asm)
134125
add_test(NAME secp256k1_noverify_tests COMMAND noverify_tests)
135126
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
136127
add_executable(tests tests.c)
137-
target_compile_definitions(tests PRIVATE VERIFY)
128+
target_compile_definitions(tests PRIVATE VERIFY $<TARGET_PROPERTY:secp256k1,INTERFACE_COMPILE_DEFINITIONS>)
138129
target_link_libraries(tests secp256k1_precomputed secp256k1_asm)
139130
add_test(NAME secp256k1_tests COMMAND tests)
140131
endif()
@@ -144,7 +135,10 @@ if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
144135
# Note: do not include secp256k1_precomputed in exhaustive_tests (it uses runtime-generated tables).
145136
add_executable(exhaustive_tests tests_exhaustive.c)
146137
target_link_libraries(exhaustive_tests secp256k1_asm)
147-
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
138+
target_compile_definitions(exhaustive_tests PRIVATE
139+
$<$<NOT:$<CONFIG:Coverage>>:VERIFY>
140+
$<TARGET_PROPERTY:secp256k1,INTERFACE_COMPILE_DEFINITIONS>
141+
)
148142
add_test(NAME secp256k1_exhaustive_tests COMMAND exhaustive_tests)
149143
endif()
150144

0 commit comments

Comments
 (0)