Skip to content

Commit

Permalink
build: add options to use static Cuda libraries (AcademySoftwareFound…
Browse files Browse the repository at this point in the history
…ation#1772)

* New build option `CUDA_PREFER_STATIC_LIBS` tries to use Cuda's static
  libraries if they can be found.
* Remove code related to nvrt library, it's no longer needed in OptiX 7.
* Changes in checked_find_package to allow a PREFER_STATIC_LIBS
  optional argument. Turns out we didn't use this, but I think it's
  useful functionality to keep now that I've written it.

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz committed Feb 21, 2024
1 parent db6679f commit 84c6784
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
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 @@ -183,6 +183,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 @@ -206,20 +207,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

0 comments on commit 84c6784

Please sign in to comment.