Skip to content

Commit 69e2f1a

Browse files
cdluminatefacebook-github-bot
authored andcommitted
[cmake] add HAVE_SOVERSION option (default=OFF). (pytorch#37502)
Summary: This is useful for linux distributions when the ABI/API of libtorch has been changed. The default SOVERSION is set to "${TORCH_VERSION_MAJOR}.${TORCH_VERSION_MINOR}". ezyang But if the release strategy of pytorch/caffe2 involves avoiding breaking API/ABI changes to libtorch for minor/patch releases, then we can set `TORCH_SOVERSION` to simply `TORCH_VERSION_MAJOR`. Please confirm that. Pull Request resolved: pytorch#37502 Differential Revision: D21303565 Pulled By: ezyang fbshipit-source-id: 798f5ec7fc5f0431ff1a7f9e8e5d3a0d3b25bb22
1 parent 4c8636c commit 69e2f1a

File tree

8 files changed

+38
-0
lines changed

8 files changed

+38
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ cmake_dependent_option(
206206
"USE_DISTRIBUTED" OFF)
207207
option(USE_TBB "Use TBB" OFF)
208208
option(ONNX_ML "Enable traditional ONNX ML API." ON)
209+
option(HAVE_SOVERSION "Whether to add SOVERSION to the shared objects" OFF)
209210

210211
# Linux distributions do not want too many embedded sources, in that sense we
211212
# need to be able to build pytorch with an (almost) empty third_party
@@ -415,6 +416,7 @@ if(NOT TORCH_BUILD_VERSION)
415416
endif()
416417
caffe2_parse_version_str(TORCH ${TORCH_BUILD_VERSION})
417418
caffe2_parse_version_str(CAFFE2 ${TORCH_BUILD_VERSION})
419+
set(TORCH_SOVERSION "${TORCH_VERSION_MAJOR}.${TORCH_VERSION_MINOR}")
418420

419421
# ---[ CMake scripts + modules
420422
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)

c10/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ file(GLOB C10_SRCS
3838
file(GLOB_RECURSE C10_ALL_TEST_FILES test/*.cpp)
3939
file(GLOB_RECURSE C10_HEADERS *.h)
4040
add_library(c10 ${C10_SRCS} ${C10_HEADERS})
41+
if(HAVE_SOVERSION)
42+
set_target_properties(c10 PROPERTIES
43+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
44+
endif()
4145
# If building shared library, set dllimport/dllexport proper.
4246
target_compile_options(c10 PRIVATE "-DC10_BUILD_MAIN_LIB")
4347
# Enable hidden visibility if compiler supports it.

caffe2/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ endif()
522522

523523

524524
add_library(torch_cpu ${Caffe2_CPU_SRCS})
525+
if(HAVE_SOVERSION)
526+
set_target_properties(torch_cpu PROPERTIES
527+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
528+
endif()
525529
torch_compile_options(torch_cpu) # see cmake/public/utils.cmake
526530

527531
if(USE_LLVM AND LLVM_FOUND)
@@ -546,6 +550,10 @@ file(WRITE ${DUMMY_EMPTY_FILE} ${DUMMY_FILE_CONTENT})
546550
# Wrapper library for people who link against torch and expect both CPU and CUDA support
547551
# Contains "torch_cpu" and "torch_cuda"
548552
add_library(torch ${DUMMY_EMPTY_FILE})
553+
if(HAVE_SOVERSION)
554+
set_target_properties(torch PROPERTIES
555+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
556+
endif()
549557

550558
if(USE_ROCM)
551559
filter_list(__caffe2_hip_srcs_cpp Caffe2_HIP_SRCS "\\.(cu|hip)$")
@@ -1028,6 +1036,10 @@ endif()
10281036
# this we have to preload those libs in a global namespace.
10291037
if(BUILD_SHARED_LIBS)
10301038
add_library(torch_global_deps SHARED ${TORCH_SRC_DIR}/csrc/empty.c)
1039+
if(HAVE_SOVERSION)
1040+
set_target_properties(torch_global_deps PROPERTIES
1041+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
1042+
endif()
10311043
set_target_properties(torch_global_deps PROPERTIES LINKER_LANGUAGE C)
10321044
if(USE_MPI)
10331045
target_link_libraries(torch_global_deps ${MPI_CXX_LIBRARIES})

modules/detectron/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ if(BUILD_CAFFE2_OPS)
3232
install(TARGETS caffe2_detectron_ops_hip DESTINATION lib)
3333
elseif(NOT IOS_PLATFORM)
3434
add_library(caffe2_detectron_ops SHARED ${Detectron_CPU_SRCS})
35+
if(HAVE_SOVERSION)
36+
set_target_properties(caffe2_detectron_ops PROPERTIES
37+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
38+
endif()
3539
target_link_libraries(caffe2_detectron_ops torch ${OpenMP_link})
3640
install(TARGETS caffe2_detectron_ops DESTINATION lib)
3741
if(MSVC)

modules/module_test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ if(BUILD_TEST)
1111
caffe2_module_test_dynamic
1212
${CMAKE_CURRENT_SOURCE_DIR}/module_test_dynamic.cc)
1313

14+
if(HAVE_SOVERSION)
15+
set_target_properties(caffe2_module_test_dynamic PROPERTIES
16+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
17+
endif()
1418
target_link_libraries(caffe2_module_test_dynamic torch_library)
1519
install(TARGETS caffe2_module_test_dynamic DESTINATION lib)
1620
if(MSVC AND BUILD_SHARED_LIBS)

modules/observers/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ add_library(caffe2_observers
1414
"${CMAKE_CURRENT_SOURCE_DIR}/observer_config.cc"
1515
"${CMAKE_CURRENT_SOURCE_DIR}/perf_observer.cc"
1616
)
17+
if(HAVE_SOVERSION)
18+
set_target_properties(caffe2_observers PROPERTIES
19+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
20+
endif()
1721
target_link_libraries(caffe2_observers PUBLIC torch_library)
1822
target_include_directories(caffe2_observers PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
1923
target_compile_options(caffe2_observers PRIVATE "-DCAFFE2_BUILD_OBSERVER_LIB")

torch/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ add_custom_command(
291291
)
292292

293293
add_library(torch_python SHARED ${TORCH_PYTHON_SRCS})
294+
if(HAVE_SOVERSION)
295+
set_target_properties(torch_python PROPERTIES
296+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
297+
endif()
294298
add_dependencies(torch_python torch_python_stubs)
295299

296300
# Required workaround for generated sources

torch/lib/libshm/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ else()
2323
endif()
2424

2525
add_library(shm SHARED core.cpp)
26+
if(HAVE_SOVERSION)
27+
set_target_properties(shm PROPERTIES
28+
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
29+
endif()
2630

2731
target_include_directories(shm PUBLIC
2832
${CMAKE_BINARY_DIR}/aten/src # provides "ATen/TypeExtendedInterface.h" to ATen.h

0 commit comments

Comments
 (0)