Skip to content

Commit d2e4e18

Browse files
committed
cmake: Fix library ABI versioning
This change emulates Libtool to make sure Libtool and CMake agree on the ABI version.
1 parent f6bef03 commit d2e4e18

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/CMakeLists.txt

+36-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,44 @@ set_target_properties(secp256k1_precomputed PROPERTIES POSITION_INDEPENDENT_CODE
3434
target_include_directories(secp256k1 PUBLIC
3535
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
3636
)
37+
38+
# This emulates Libtool to make sure Libtool and CMake agree on the ABI version,
39+
# see below "Calculate the version variables" in build-aux/ltmain.sh.
40+
math(EXPR ${PROJECT_NAME}_soversion "${${PROJECT_NAME}_LIB_VERSION_CURRENT} - ${${PROJECT_NAME}_LIB_VERSION_AGE}")
3741
set_target_properties(secp256k1 PROPERTIES
38-
VERSION "${${PROJECT_NAME}_LIB_VERSION_CURRENT}.${${PROJECT_NAME}_LIB_VERSION_AGE}.${${PROJECT_NAME}_LIB_VERSION_REVISION}"
39-
SOVERSION "${${PROJECT_NAME}_LIB_VERSION_CURRENT}"
42+
SOVERSION ${${PROJECT_NAME}_soversion}
4043
)
44+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
45+
set_target_properties(secp256k1 PROPERTIES
46+
VERSION ${${PROJECT_NAME}_soversion}.${${PROJECT_NAME}_LIB_VERSION_AGE}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
47+
)
48+
elseif(APPLE)
49+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
50+
math(EXPR ${PROJECT_NAME}_compatibility_version "${${PROJECT_NAME}_LIB_VERSION_CURRENT} + 1")
51+
set_target_properties(secp256k1 PROPERTIES
52+
MACHO_COMPATIBILITY_VERSION ${${PROJECT_NAME}_compatibility_version}
53+
MACHO_CURRENT_VERSION ${${PROJECT_NAME}_compatibility_version}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
54+
)
55+
unset(${PROJECT_NAME}_compatibility_version)
56+
elseif(BUILD_SHARED_LIBS)
57+
message(WARNING
58+
"The 'compatibility version' and 'current version' values of the DYLIB "
59+
"will diverge from the values set by the GNU Libtool. To ensure "
60+
"compatibility, it is recommended to upgrade CMake to at least version 3.17."
61+
)
62+
endif()
63+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
64+
if(MSVC)
65+
set_target_properties(secp256k1 PROPERTIES
66+
PREFIX "lib"
67+
)
68+
endif()
69+
set_target_properties(secp256k1 PROPERTIES
70+
ARCHIVE_OUTPUT_NAME "secp256k1"
71+
RUNTIME_OUTPUT_NAME "secp256k1-${${PROJECT_NAME}_soversion}"
72+
)
73+
endif()
74+
unset(${PROJECT_NAME}_soversion)
4175

4276
if(SECP256K1_BUILD_BENCHMARK)
4377
add_executable(bench bench.c)

0 commit comments

Comments
 (0)