xref: /aosp_15_r20/external/pytorch/c10/cuda/CMakeLists.txt (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# Build file for the C10 CUDA.
2#
3# C10 CUDA is a minimal library, but it does depend on CUDA.
4
5include(../../cmake/public/utils.cmake)
6include(../../cmake/public/cuda.cmake)
7
8# ---[ Configure macro file.
9set(C10_CUDA_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in
10# Probably have to do this :(
11configure_file(
12    ${CMAKE_CURRENT_LIST_DIR}/impl/cuda_cmake_macros.h.in
13    ${CMAKE_BINARY_DIR}/c10/cuda/impl/cuda_cmake_macros.h)
14
15if(BUILD_LIBTORCHLESS)
16  find_library(C10_CUDA_LIB c10_cuda PATHS $ENV{LIBTORCH_LIB_PATH} NO_DEFAULT_PATH)
17else()
18  set(C10_CUDA_LIB c10_cuda)
19endif()
20
21# Note: if you want to add ANY dependency to the c10 library, make sure you
22# check with the core PyTorch developers as the dependency will be
23# transitively passed on to all libraries dependent on PyTorch.
24
25# Note: if you add a new source file/header, you will need to update
26# torch/utils/hipify/cuda_to_hip_mappings.py for new files
27# and headers you add
28set(C10_CUDA_SRCS
29    CUDAAllocatorConfig.cpp
30    CUDACachingAllocator.cpp
31    CUDADeviceAssertionHost.cpp
32    CUDAException.cpp
33    CUDAFunctions.cpp
34    CUDAMallocAsyncAllocator.cpp
35    CUDAMiscFunctions.cpp
36    CUDAStream.cpp
37    impl/CUDAGuardImpl.cpp
38    impl/CUDATest.cpp
39    driver_api.cpp
40)
41set(C10_CUDA_HEADERS
42    CUDAAllocatorConfig.h
43    CUDACachingAllocator.h
44    CUDADeviceAssertionHost.h
45    CUDAException.h
46    CUDAFunctions.h
47    CUDAGuard.h
48    CUDAMacros.h
49    CUDAMathCompat.h
50    CUDAMiscFunctions.h
51    CUDAStream.h
52    impl/CUDAGuardImpl.h
53    impl/CUDATest.h
54)
55set(CUDA_LINK_LIBRARIES_KEYWORD PRIVATE)
56
57if(NOT BUILD_LIBTORCHLESS)
58  torch_cuda_based_add_library(c10_cuda ${C10_CUDA_SRCS} ${C10_CUDA_HEADERS})
59  set(CUDA_LINK_LIBRARIES_KEYWORD)
60  # If building shared library, set dllimport/dllexport proper.
61  target_compile_options(c10_cuda PRIVATE "-DC10_CUDA_BUILD_MAIN_LIB")
62  # Enable hidden visibility if compiler supports it.
63  if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
64    target_compile_options(c10_cuda PRIVATE "-fvisibility=hidden")
65  endif()
66
67  # ---[ Dependency of c10_cuda
68  target_link_libraries(c10_cuda PUBLIC ${C10_LIB} torch::cudart)
69
70  if(NOT WIN32)
71  target_link_libraries(c10_cuda PRIVATE dl)
72  target_compile_options(c10_cuda PRIVATE "-DPYTORCH_C10_DRIVER_API_SUPPORTED")
73  endif()
74
75  target_include_directories(
76      c10_cuda PUBLIC
77      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>
78      $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
79      $<INSTALL_INTERFACE:include>)
80
81# ---[ Installation
82# Note: for now, we will put all export path into one single Caffe2Targets group
83# to deal with the cmake deployment need. Inside the Caffe2Targets set, the
84# individual libraries like libc10.so and libcaffe2.so are still self-contained.
85install(TARGETS c10_cuda EXPORT Caffe2Targets DESTINATION lib)
86
87endif()
88
89add_subdirectory(test)
90
91foreach(file ${C10_CUDA_HEADERS})
92  get_filename_component( dir ${file} DIRECTORY )
93  install( FILES ${file} DESTINATION include/c10/cuda/${dir} )
94endforeach()
95install(FILES ${CMAKE_BINARY_DIR}/c10/cuda/impl/cuda_cmake_macros.h
96  DESTINATION include/c10/cuda/impl)
97
98if(MSVC AND C10_CUDA_BUILD_SHARED_LIBS)
99  install(FILES $<TARGET_PDB_FILE:c10_cuda> DESTINATION lib OPTIONAL)
100endif()
101