From cee18efa2559e97fadb26ef73a429fceb815b13a Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Thu, 8 Jul 2021 14:14:33 +0100
Subject: [PATCH] ENH: New CUDA_STATIC option, which allows static linking to
 the CUDA runtime/toolkit

---
 config/buildSettings.mk | 23 +++++++++++++++++++++--
 config/vars.mk          | 30 ++++++++++++++++--------------
 2 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/config/buildSettings.mk b/config/buildSettings.mk
index 7946dae..3f9ea14 100644
--- a/config/buildSettings.mk
+++ b/config/buildSettings.mk
@@ -121,8 +121,9 @@ endif
 ifneq ($(shell which nvcc), )
 
   # NVCC, CUDA_HOME, CUDA_VER, GENCODEFLAGS,
-  # NVCCFLAGS, and NVCCLDFLAGS may be
-  # overridden by the environment.
+  # NVCCFLAGS, NVCCLDFLAGS, and CUDA_STATIC
+  # may be specified/overridden by the
+  # environment.
 
   # We specifically use variable names "CUDA_VER"
   # and "CUDA_HOME" because they will be set in
@@ -147,4 +148,22 @@ ifneq ($(shell which nvcc), )
                  -L${CUDA_HOME}/lib64       \
                  -L${CUDA_HOME}/lib/stubs   \
                  -L${CUDA_HOME}/lib64/stubs
+
+  # Link CUDA libraries statically, if compilation
+  # was invoked with "make CUDA_STATIC=1".
+  ifdef CUDA_STATIC
+    # The cuda/cudart libs are handled by nvcc. Other
+    # components of the CUDA toolkit are provided as
+    # both dynamic and static libraries. "_CUDALIBS"
+    # is what is used to construct the final NVCCLDFLAGS
+    # (see vars.mk)
+    # Other CUDA toolkit components will be added here
+    # on an as-needed basis.
+    NVCCFLAGS += --cudart=static
+    _CUDALIBS  = $(subst -lcublas,-lcublas_static -lcublasLt_static -lculibos, \
+                 $(subst -lcurand,-lcurand_static,${CUDALIBS}))
+  else
+    NVCCFLAGS += --cudart=shared
+	_CUDALIBS  = ${CUDALIBS}
+  endif
 endif
diff --git a/config/vars.mk b/config/vars.mk
index b8d2286..0d1d972 100644
--- a/config/vars.mk
+++ b/config/vars.mk
@@ -13,19 +13,21 @@ PROJNAME =
 # Project-specific compiler/linker flags - these
 # can be overridden in the project Makefile.
 
-USRLDFLAGS      = # Project-specific linker flags
-USRINCFLAGS     = # Project-specific include directories
-USRCFLAGS       = # Project-specific compiler flags for C projects
-USRCXXFLAGS     = # Project-specific compiler flags for C++ projects
-USRCPPFLAGS     = # Project-specific preprocessor flags
-LIBS            = # Project-specific libraries to link against for C
-                  # and C++ projects - these are incorporated into the
-                  # final LDFLAGS, below.
-USRNVCCFLAGS    = # Project-specific compiler flags for CUDA projects
-USRNVCCLDFLAGS  = # Project-specific linker flags for CUDA projects
-CUDALIBS        = # Project-specific libraries to link against for CUDA
-                  # projects - these are incorporated into the final
-                  # NVCCLDFLAGS, below.
+USRLDFLAGS      = # Linker flags
+USRINCFLAGS     = # Include directories
+USRCFLAGS       = # Compiler flags for C projects
+USRCXXFLAGS     = # Compiler flags for C++ projects
+USRCPPFLAGS     = # Preprocessor flags
+LIBS            = # Libraries to link against for C and C++ projects -
+                  # these are incorporated into the final LDFLAGS, below.
+USRNVCCFLAGS    = # Compiler flags for CUDA projects
+USRNVCCLDFLAGS  = # Linker flags for CUDA projects
+CUDALIBS        = # CUDA libraries to link against (e.g. curand, cublas, etc) -
+                  # these are incorporated into the final NVCCLDFLAGS, below.
+                  # -lcuda and -lcudart are automatically added, so do not
+                  # need to be explicitly listed. Dynamic library names should
+                  # be used here - they will be transformed to their static
+                  # versions if CUDA_STATIC is set (see buildSettings.mk).
 
 # Project outputs - these variables control
 # what gets installed from a project. They
@@ -121,4 +123,4 @@ NVCCLDFLAGS += ${ARCHNVCCLDFLAGS}                  \
                ${USRNVCCLDFLAGS}                   \
                --compiler-options "${USRLDFLAGS}"  \
                -L. -L${DEVLIBDIR} -L${LIBDIR}      \
-                ${LIBS} ${CUDALIBS} ${ARCHLIBS}
+                ${LIBS} ${_CUDALIBS} ${ARCHLIBS}
-- 
GitLab