44# This source code is licensed under the BSD-style license found in the
55# LICENSE file in the root directory of this source tree.
66
7- cmake_minimum_required (VERSION 3.22 FATAL_ERROR)
7+ cmake_minimum_required (VERSION 3.24 FATAL_ERROR)
88project (kineto VERSION 0.1 LANGUAGES CXX C)
99
1010#install libraries into correct locations on all platforms
@@ -52,6 +52,7 @@ endif()
5252# Set LIBKINETO_NOCUPTI to explicitly disable CUPTI
5353# Otherwise, CUPTI is disabled if not found
5454option (LIBKINETO_NOCUPTI "Disable CUPTI" OFF )
55+ option (USE_CUPTI_SO "Use CUPTI as a shared library" ON )
5556
5657find_package (CUDAToolkit)
5758if (NOT LIBKINETO_NOCUPTI)
@@ -63,24 +64,65 @@ if(NOT LIBKINETO_NOCUPTI)
6364 NO_DEFAULT_PATH)
6465 endif ()
6566
66- if (NOT CUDA_cupti_LIBRARY)
67- find_library (CUDA_cupti_LIBRARY cupti PATHS
68- "${CUDAToolkit_LIBRARY_ROOT} "
69- "${CUDAToolkit_LIBRARY_ROOT} /extras/CUPTI/lib64"
70- "${CUDAToolkit_LIBRARY_ROOT} /lib"
71- "${CUDAToolkit_LIBRARY_ROOT} /lib64"
72- NO_DEFAULT_PATH)
67+ if (USE_CUPTI_SO)
68+ if (NOT CUDA_cupti_LIBRARY)
69+ find_library (CUDA_cupti_LIBRARY cupti PATHS
70+ "${CUDAToolkit_LIBRARY_ROOT} "
71+ "${CUDAToolkit_LIBRARY_ROOT} /extras/CUPTI/lib64"
72+ "${CUDAToolkit_LIBRARY_ROOT} /lib"
73+ "${CUDAToolkit_LIBRARY_ROOT} /lib64"
74+ NO_DEFAULT_PATH)
75+ endif ()
76+ set (cupti_LIBRARY ${CUDA_cupti_LIBRARY} )
77+ else ()
78+ if (NOT CUDA_cupti_static_LIBRARY)
79+ find_library (CUDA_cupti_static_LIBRARY cupti_static PATHS
80+ "${CUDAToolkit_LIBRARY_ROOT} "
81+ "${CUDAToolkit_LIBRARY_ROOT} /extras/CUPTI/lib64"
82+ "${CUDAToolkit_LIBRARY_ROOT} /lib"
83+ "${CUDAToolkit_LIBRARY_ROOT} /lib64"
84+ NO_DEFAULT_PATH)
85+ endif ()
86+ set (cupti_LIBRARY ${CUDA_cupti_static_LIBRARY} )
7387 endif ()
7488
75- if (CUDA_cupti_LIBRARY AND CUPTI_INCLUDE_DIR)
89+ if (cupti_LIBRARY AND CUPTI_INCLUDE_DIR)
7690 message (STATUS " CUPTI_INCLUDE_DIR = ${CUPTI_INCLUDE_DIR} " )
7791 message (STATUS " CUDA_cupti_LIBRARY = ${CUDA_cupti_LIBRARY} " )
92+ message (STATUS " CUDA_cupti_static_LIBRARY = ${CUDA_cupti_static_LIBRARY} " )
7893 message (STATUS "Found CUPTI" )
79- if (NOT TARGET CUDA::cupti)
80- add_library (CUDA::cupti INTERFACE IMPORTED )
81- target_link_libraries (CUDA::cupti INTERFACE "${CUDA_cupti_LIBRARY} " )
94+ if (USE_CUPTI_SO)
95+ set (cupti_target CUDA::cupti)
96+ else ()
97+ set (cupti_target CUDA::cupti_static)
98+ endif ()
99+ if (NOT TARGET ${cupti_target} )
100+ add_library (${cupti_target} INTERFACE IMPORTED )
101+ target_link_libraries (${cupti_target} INTERFACE "${cupti_LIBRARY} " )
82102 endif ()
83- target_include_directories (CUDA::cupti INTERFACE "${CUPTI_INCLUDE_DIR} " )
103+ target_include_directories (${cupti_target} INTERFACE "${CUPTI_INCLUDE_DIR} " )
104+
105+ if (NOT USE_CUPTI_SO)
106+ include (CheckCXXSourceRuns)
107+ set (CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "dl" "pthread" $<LINK_LIBRARY:WHOLE_ARCHIVE,${cupti_LIBRARY} >)
108+
109+ check_cxx_source_runs("#include <stdexcept>
110+ int main() {
111+ try {
112+ throw std::runtime_error(\" error\" );
113+ } catch (...) {
114+ return 0;
115+ }
116+ return 1;
117+ }" EXCEPTIONS_WORK)
118+ set (CMAKE_REQUIRED_LINK_OPTIONS "" )
119+ if (NOT EXCEPTIONS_WORK)
120+ message (FATAL_ERROR
121+ "Detected that statically linking against CUPTI causes exceptions to stop working. "
122+ "See https://github.com/pytorch/pytorch/issues/57744 for more details. " )
123+ endif ()
124+ endif ()
125+
84126 else ()
85127 set (LIBKINETO_NOCUPTI ON CACHE BOOL "" FORCE)
86128 message (STATUS "Could not find CUPTI library" )
@@ -247,8 +289,8 @@ if(NOT LIBKINETO_NOROCTRACER)
247289endif ()
248290
249291if (NOT LIBKINETO_NOCUPTI)
250- target_link_libraries (kineto PUBLIC CUDA::cupti CUDA::cudart CUDA::cuda_driver)
251- target_link_libraries (kineto_base PUBLIC CUDA::cupti CUDA::cudart CUDA::cuda_driver)
292+ target_link_libraries (kineto PUBLIC ${cupti_target} CUDA::cudart CUDA::cuda_driver)
293+ target_link_libraries (kineto_base PUBLIC ${cupti_target} CUDA::cudart CUDA::cuda_driver)
252294endif ()
253295if (TARGET CUDA::nvperf_host)
254296 target_link_libraries (kineto_base PRIVATE CUDA::nvperf_host)
0 commit comments