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

Make Linux (de)activate scripts standalone like on Windows #91

Merged
merged 12 commits into from
Aug 3, 2022
189 changes: 0 additions & 189 deletions recipe/install_nvcc.sh

This file was deleted.

110 changes: 110 additions & 0 deletions recipe/linux/activate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

# Set `CUDA_HOME` in an activation script.

# Backup environment variables (only if the variables are set)
if [[ ! -z "${CUDA_HOME+x}" ]]
then
export CUDA_HOME_CONDA_NVCC_BACKUP="${CUDA_HOME:-}"
fi

if [[ ! -z "${CUDA_PATH+x}" ]]
then
export CUDA_PATH_CONDA_NVCC_BACKUP="${CUDA_PATH:-}"
fi

if [[ ! -z "${CFLAGS+x}" ]]
then
export CFLAGS_CONDA_NVCC_BACKUP="${CFLAGS:-}"
fi

if [[ ! -z "${CPPFLAGS+x}" ]]
then
export CPPFLAGS_CONDA_NVCC_BACKUP="${CPPFLAGS:-}"
fi

if [[ ! -z "${CXXFLAGS+x}" ]]
then
export CXXFLAGS_CONDA_NVCC_BACKUP="${CXXFLAGS:-}"
fi

if [[ ! -z "${CMAKE_ARGS+x}" ]]
then
export CMAKE_ARGS_CONDA_NVCC_BACKUP="${CMAKE_ARGS:-}"
fi

# Default to using $(cuda-gdb) to specify $(CUDA_HOME).
if [[ -z "${CUDA_HOME+x}" ]]
then
CUDA_GDB_EXECUTABLE=$(which cuda-gdb || exit 0)
if [[ -n "$CUDA_GDB_EXECUTABLE" ]]
then
CUDA_HOME=$(dirname $(dirname $CUDA_GDB_EXECUTABLE))
else
echo "Cannot determine CUDA_HOME: cuda-gdb not in PATH"
return 1
fi
fi

if [[ ! -d "${CUDA_HOME}" ]]
then
echo "Directory specified in CUDA_HOME(=${CUDA_HOME}) doesn't exist"
return 1
fi

if [[ ! -f "${CUDA_HOME}/lib64/stubs/libcuda.so" ]]
then
echo "File ${CUDA_HOME}/lib64/stubs/libcuda.so doesn't exist"
return 1
fi

if [[ -z "$(${CUDA_HOME}/bin/nvcc --version | grep "Cuda compilation tools, release __PKG_VERSION__")" ]]
then
if [[ "${CONDA_BUILD}" = "1" ]]
leofang marked this conversation as resolved.
Show resolved Hide resolved
then
echo "Error: Version of installed CUDA didn't match package"
return 1
else
echo "Warning: Version of installed CUDA didn't match package"
fi
fi

export CUDA_HOME="${CUDA_HOME}"
export CFLAGS="${CFLAGS} -isystem ${CUDA_HOME}/include"
export CPPFLAGS="${CPPFLAGS} -isystem ${CUDA_HOME}/include"
export CXXFLAGS="${CXXFLAGS} -isystem ${CUDA_HOME}/include"

### CMake configurations

# CMake looks up components in CUDA_PATH, not CUDA_HOME
export CUDA_PATH="${CUDA_HOME}"
# New-style CUDA integrations in CMake
CMAKE_ARGS="${CMAKE_ARGS:-} -DCUDAToolkit_ROOT=${CUDA_HOME}"
# Old-style CUDA integrations in CMake
## See https://github.com/conda-forge/nvcc-feedstock/pull/58#issuecomment-752179349
CMAKE_ARGS+=" -DCUDA_TOOLKIT_ROOT_DIR=${CUDA_HOME}"
## Avoid https://github.com/conda-forge/openmm-feedstock/pull/44#issuecomment-753560234
## We need CUDA_HOME in _front_ of CMAKE_FIND_ROOT_PATH
CMAKE_ARGS="$(echo ${CMAKE_ARGS} | sed -E -e "s|(-DCMAKE_FIND_ROOT_PATH=)(\S+)|\1$CUDA_HOME;\2|")"
export CMAKE_ARGS="${CMAKE_ARGS}"

### /CMake configurations

# Add $(libcuda.so) shared object stub to the compiler sysroot.
# Needed for things that want to link to $(libcuda.so).
# Stub is used to avoid getting driver code linked into binaries.

if [[ ! -z "${CONDA_BUILD_SYSROOT+x}" ]]
then
mkdir -p "${CONDA_BUILD_SYSROOT}/lib"
# Make a backup of $(libcuda.so)
LIBCUDA_SO_CONDA_NVCC_BACKUP="${CONDA_BUILD_SYSROOT}/lib/libcuda.so-conda-nvcc-backup"
if [[ -f "${CONDA_BUILD_SYSROOT}/lib/libcuda.so" ]]
then
mv -f "${CONDA_BUILD_SYSROOT}/lib/libcuda.so" "${LIBCUDA_SO_CONDA_NVCC_BACKUP}"
fi
ln -s "${CUDA_HOME}/lib64/stubs/libcuda.so" "${CONDA_BUILD_SYSROOT}/lib/libcuda.so"
else
mkdir -p "${CONDA_PREFIX}/lib/stubs"
ln -sf "${CUDA_HOME}/lib64/stubs/libcuda.so" "${CONDA_PREFIX}/lib/stubs/libcuda.so"
fi
54 changes: 54 additions & 0 deletions recipe/linux/deactivate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Unset `CUDA_HOME` in a deactivation script.

# Restore environment variables (if there is anything to restore)
if [[ ! -z "${CUDA_HOME_CONDA_NVCC_BACKUP+x}" ]]
then
export CUDA_HOME="${CUDA_HOME_CONDA_NVCC_BACKUP}"
unset CUDA_HOME_CONDA_NVCC_BACKUP
fi

if [[ ! -z "${CUDA_PATH_CONDA_NVCC_BACKUP+x}" ]]
then
export CUDA_PATH="${CUDA_PATH_CONDA_NVCC_BACKUP}"
unset CUDA_PATH_CONDA_NVCC_BACKUP
fi

if [[ ! -z "${CFLAGS_CONDA_NVCC_BACKUP+x}" ]]
then
export CFLAGS="${CFLAGS_CONDA_NVCC_BACKUP}"
unset CFLAGS_CONDA_NVCC_BACKUP
fi

if [[ ! -z "${CPPFLAGS_CONDA_NVCC_BACKUP+x}" ]]
then
export CPPFLAGS="${CPPFLAGS_CONDA_NVCC_BACKUP}"
unset CPPFLAGS_CONDA_NVCC_BACKUP
fi

if [[ ! -z "${CXXFLAGS_CONDA_NVCC_BACKUP+x}" ]]
then
export CXXFLAGS="${CXXFLAGS_CONDA_NVCC_BACKUP}"
unset CXXFLAGS_CONDA_NVCC_BACKUP
fi

if [[ ! -z "${CMAKE_ARGS_CONDA_NVCC_BACKUP+x}" ]]
then
export CMAKE_ARGS="${CMAKE_ARGS_CONDA_NVCC_BACKUP}"
unset CMAKE_ARGS_CONDA_NVCC_BACKUP
fi

# Remove or restore $(libcuda.so) shared object stub from the compiler sysroot.
LIBCUDA_SO_CONDA_NVCC_BACKUP="${CONDA_BUILD_SYSROOT}/lib/libcuda.so-conda-nvcc-backup"
if [[ -f ""${LIBCUDA_SO_CONDA_NVCC_BACKUP}"" ]]
then
mv -f "${LIBCUDA_SO_CONDA_NVCC_BACKUP}" "${CONDA_BUILD_SYSROOT}/lib/libcuda.so"
else
if [[ ! -z "${CONDA_BUILD_SYSROOT+x}" ]]
then
rm -f "${CONDA_BUILD_SYSROOT}/lib/libcuda.so"
else
rm -f "${CONDA_PREFIX}/lib/stubs/libcuda.so"
fi
fi
17 changes: 17 additions & 0 deletions recipe/linux/install_nvcc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -xeuo pipefail

# Activation script
mkdir -p "${PREFIX}/etc/conda/activate.d"
sed -i "s/__PKG_VERSION__/$PKG_VERSION/g" linux/activate.sh
leofang marked this conversation as resolved.
Show resolved Hide resolved
cp $RECIPE_DIR/linux/activate.sh "${PREFIX}/etc/conda/activate.d/${PKG_NAME}_activate.sh"

# Deactivation script
mkdir -p "${PREFIX}/etc/conda/deactivate.d"
cp $RECIPE_DIR/linux/deactivate.sh "${PREFIX}/etc/conda/deactivate.d/${PKG_NAME}_deactivate.sh"

# Create `nvcc` script in `bin` so it can be easily run.
mkdir -p "${PREFIX}/bin"
cp $RECIPE_DIR/linux/nvcc.sh "${PREFIX}/bin/nvcc"
chmod +x "${PREFIX}/bin/nvcc"
9 changes: 9 additions & 0 deletions recipe/linux/nvcc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

for arg in "${@}" ; do
case ${arg} in -ccbin)
# If -ccbin argument is already provided, don't add an additional one.
exec "${CUDA_HOME}/bin/nvcc" "${@}"
esac
done
exec "${CUDA_HOME}/bin/nvcc" -ccbin "${CXX}" "${@}"
File renamed without changes.
4 changes: 2 additions & 2 deletions recipe/meta.yaml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "nvcc" %}
{% set number = 20 %}
{% set number = 21 %}

{% if cuda_compiler_version in (None, "None", True, False) %}
{% set cuda_major = 0 %}
Expand All @@ -26,7 +26,7 @@ outputs:
- name: "{{ name }}_{{ target_platform }}"
version: "{{ cuda_compiler_version }}"
number: {{ number }}
script: install_nvcc.sh # [linux]
script: linux/install_nvcc.sh # [linux]
script: windows/install_nvcc.bat # [win]
build:
ignore_run_exports:
Expand Down