• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1project(libshm C CXX)
2cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
3
4set(TORCH_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../)
5
6if(NOT LIBSHM_INSTALL_LIB_SUBDIR)
7  set(LIBSHM_INSTALL_LIB_SUBDIR "lib" CACHE PATH "libshm install library directory")
8endif()
9
10add_library(shm SHARED core.cpp)
11if(HAVE_SOVERSION)
12  set_target_properties(shm PROPERTIES
13      VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
14endif()
15
16target_include_directories(shm PUBLIC
17  ${TORCH_ROOT}/torch/lib # provides "libshm/libshm.h"
18)
19
20### Torch packages supposes libraries prefix is "lib"
21set_target_properties(shm PROPERTIES
22  PREFIX "lib"
23  IMPORT_PREFIX "lib"
24  CXX_STANDARD 17)
25target_link_libraries(shm PRIVATE ${TORCH_CPU_LIB})
26
27if(UNIX AND NOT APPLE)
28  include(CheckLibraryExists)
29  find_package(Threads REQUIRED)
30  # https://github.com/libgit2/libgit2/issues/2128#issuecomment-35649830
31  check_library_exists(rt clock_gettime "time.h" NEED_LIBRT)
32  if(NEED_LIBRT)
33    target_link_libraries(shm PUBLIC rt)
34  else()
35    message(STATUS "Checking if rt requires pthread")
36    # Sometimes, rt won't be available unless you also link against
37    # pthreads.  In this case, the NEED_LIBRT test will fail, because
38    # check_library_exists isn't going to build the C file with the
39    # pthread file, and the build will fail, setting NEED_LIBRT to
40    # false (this is TOTALLY BOGUS, this situation should be an error
41    # situation, not a "oh, I guess rt is not supported", but it's
42    # not too easy to distinguish between the two situations).  So,
43    # if it fails, we try again, but this time also with a dependency
44    # on pthread.  If it succeeds this time, we know we not only need
45    # an rt dependency, but we also need pthread.
46    #
47    # BTW, this test looks for shm_open, because that's what we
48    # really care about (not clock_gettime).  I didn't change the
49    # site above though in case there was a reason we were testing
50    # against clock_gettime. In principle, the choice of symbol you
51    # test for shouldn't matter.
52    set(CMAKE_REQUIRED_LIBRARIES Threads::Threads)
53    check_library_exists(rt shm_open "sys/mman.h" NEED_RT_AND_PTHREAD)
54    unset(CMAKE_REQUIRED_LIBRARIES)
55    if(NEED_RT_AND_PTHREAD)
56      message(STATUS "Needs it, linking against pthread and rt")
57      target_link_libraries(shm PUBLIC rt Threads::Threads)
58    endif()
59  endif()
60endif()
61
62add_executable(torch_shm_manager manager.cpp)
63if(BUILD_LIBTORCHLESS)
64  target_link_libraries(torch_shm_manager PRIVATE shm ${C10_LIB})
65else()
66  # we need to link directly to c10 here otherwise we miss symbols
67  target_link_libraries(torch_shm_manager PRIVATE shm c10)
68endif()
69set_target_properties(torch_shm_manager PROPERTIES
70  INSTALL_RPATH "${_rpath_portable_origin}/../lib")
71
72install(TARGETS shm LIBRARY DESTINATION ${LIBSHM_INSTALL_LIB_SUBDIR})
73install(FILES libshm.h DESTINATION "include")
74install(TARGETS torch_shm_manager DESTINATION "bin")
75