Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add options to use static Cuda libraries #1772

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/cmake/checked_find_package.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ endfunction ()
# file from the package before using a FindPackage.cmake module.
# * Optional DEBUG turns on extra debugging information related to how
# this package is found.
# * If either the optional PREFER_STATIC_LIBS argument to this macro, *or*
# CMake variable <Pkgname>_PREFER_STATIC_LIBS is true, then try to prefer
# static versions of any libraries found.
#
# N.B. This needs to be a macro, not a function, because the find modules
# will set(blah val PARENT_SCOPE) and we need that to be the global scope,
# not merely the scope for this function.
macro (checked_find_package pkgname)
cmake_parse_arguments(_pkg # prefix
# noValueKeywords:
"REQUIRED;PREFER_CONFIG;DEBUG"
"REQUIRED;PREFER_CONFIG;DEBUG;PREFER_STATIC_LIBS"
# singleValueKeywords:
"ENABLE;ISDEPOF;VERSION_MIN;VERSION_MAX;RECOMMEND_MIN;RECOMMEND_MIN_REASON"
# multiValueKeywords:
Expand Down Expand Up @@ -115,6 +118,10 @@ macro (checked_find_package pkgname)
endif ()
set (_config_status "")
if (_enable OR _pkg_REQUIRED)
set (save_lib_path ${CMAKE_FIND_LIBRARY_SUFFIXES})
if (_pkg_PREFER_STATIC_LIBS OR ${pkgname}_PREFER_STATIC_LIBS)
set (CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
endif ()
if (${pkgname}_FOUND OR ${pkgname_upper}_FOUND)
# was already found
elseif (_pkg_PREFER_CONFIG OR ALWAYS_PREFER_CONFIG)
Expand All @@ -126,6 +133,8 @@ macro (checked_find_package pkgname)
if (NOT (${pkgname}_FOUND OR ${pkgname_upper}_FOUND))
find_package (${pkgname} ${_pkg_UNPARSED_ARGUMENTS})
endif()
set (CMAKE_FIND_LIBRARY_SUFFIXES ${save_lib_path})
unset (save_lib_path)
if ((${pkgname}_FOUND OR ${pkgname_upper}_FOUND)
AND ${pkgname}_VERSION
AND (_pkg_VERSION_MIN OR _pkg_VERSION_MAX))
Expand Down
34 changes: 20 additions & 14 deletions src/cmake/externalpackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ endif ()


# CUDA setup
option (CUDA_PREFER_STATIC_LIBS "Prefer static CUDA libraries" OFF)
if (OSL_USE_OPTIX)
if (USE_LLVM_BITCODE)
if (NOT CUDA_TOOLKIT_ROOT_DIR AND NOT $ENV{CUDA_TOOLKIT_ROOT_DIR} STREQUAL "")
Expand All @@ -225,20 +226,25 @@ if (OSL_USE_OPTIX)

set (CUDA_LIB_FLAGS "--cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")

find_library(cuda_lib NAMES cudart
PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib64" "${CUDA_TOOLKIT_ROOT_DIR}/x64" "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64"
REQUIRED)
set(CUDA_LIBRARIES ${cuda_lib})

# testrender & testshade need libnvrtc
if ("${CUDA_VERSION}" VERSION_GREATER_EQUAL "10.0")
find_library(nvrtc_lib NAMES nvrtc
PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib64" "${CUDA_TOOLKIT_ROOT_DIR}/x64" "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64"
REQUIRED)
set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${nvrtc_lib})

set(CUDA_EXTRA_LIBS ${CUDA_EXTRA_LIBS} dl)
endif()
# If the user wants, try to use static libs here to putting static lib
# suffixes earlier in the suffix list. Don't forget to restore after
# so that this only applies to these library searches right here.
set (save_lib_path ${CMAKE_FIND_LIBRARY_SUFFIXES})
if (CUDA_PREFER_STATIC_LIBS)
set (CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
find_library(cudart_lib REQUIRED
NAMES cudart_static cudart
PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib64" "${CUDA_TOOLKIT_ROOT_DIR}/x64" "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64")
else ()
find_library(cudart_lib REQUIRED
NAMES cudart
PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib64" "${CUDA_TOOLKIT_ROOT_DIR}/x64" "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64")
endif ()
# Is it really a good idea to completely reset CUDA_LIBRARIES here?
set(CUDA_LIBRARIES ${cudart_lib})
set(CUDA_EXTRA_LIBS ${CUDA_EXTRA_LIBS} dl rt)
set (CMAKE_FIND_LIBRARY_SUFFIXES ${save_lib_path})
unset (save_lib_path)
endif()

# OptiX setup
Expand Down
Loading