Q:

Permissions error with CMake and CUDA

I'm trying to build a project with a static library using CUDA along with C++ code. It needs to link with an external library (moderngpu). The CUDA and C++ code each independently work fine, and have been previously compiled with make. However, when trying to build the code with CMake (through CLion, if it matters), I get the following error:  
Error running 'main': Cannot run program "/home/mnemonic/Programs/myProject/bin/src/CMakeFiles/hvm.dir/cmake_device_link.o" (in directory "/home/mnemonic/Programs/myProject/bin/src/CMakeFiles/hvm.dir"): error=13, Permission denied

I've been trying to solve this for days, but haven't been able to make any progress. It doesn't make sense that I'm getting a permissions error at all, and the .o file it's complaining about doesn't run when I set it to executable. My best guess is that it's some sort of linking issue. Any help would be appreciated.
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.9 FATAL_ERROR)
PROJECT(myProject LANGUAGES CXX CUDA)

FIND_PACKAGE(CUDA 8.0 REQUIRED)

SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-Wall -Wextra -Wno-deprecated -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op \
-Wmissing-declarations -Wredundant-decls -Wshadow -Woverloaded-virtual -Wpedantic \
-ffast-math \
")
SET(CMAKE_CUDA_COMPILER /usr/local/cuda-8.0/bin/nvcc)
SET(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} \
    -Xcompiler -pthread,-fno-strict-aliasing,-g,-O2,-g,-fwrapv,-O3,-Wall,-fPIC \
    -gencode arch=compute_61,code=sm_61 \
")

ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(lib)

src/CMakeLists.txt
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} source)

ADD_EXECUTABLE(stuff ${source})

TARGET_LINK_LIBRARIES(stuff LINK_PUBLIC thing thingKernels)

lib/CMakeLists.txt
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(
    /usr/local/src/moderngpu/include
    /usr/local/cuda-8.0/include
)

ADD_LIBRARY(thing STATIC
    ${CMAKE_CURRENT_SOURCE_DIR}/thing.cpp
)

ADD_LIBRARY(thingKernels STATIC
    ../include/cuda/thing.cu
    /usr/local/src/moderngpu/src/mgpucontext.cu
    /usr/local/src/moderngpu/src/mgpuutil.cpp
)

TARGET_INCLUDE_DIRECTORIES(thing PUBLIC ../include ${CMAKE_CURRENT_SOURCE_DIR})
TARGET_INCLUDE_DIRECTORIES(thingKernels PUBLIC ../include ${CMAKE_CURRENT_SOURCE_DIR})

For reference, this is the working Makefile
ifndef CUDA_VERSION
    CUDA_VERSION = 8.0
endif
ifndef COMPUTE_CAPABILITY
    COMPUTE_CAPABILITY = 61
endif

CPP_COMPILER = g++
CPP_FLAGS = -pthread -fno-strict-aliasing -g -O2 -g -fwrapv -O3 -Wall -fPIC

CUDA_COMPILER = nvcc -m64
CUDA_FLAGS = -Xcompiler -pthread,-fno-strict-aliasing,-g,-O2,-g,-fwrapv,-O3,-Wall,-fPIC
CUDA_FLAGS += -gencode arch=compute_$(COMPUTE_CAPABILITY),code=sm_$(COMPUTE_CAPABILITY)

CUDA_INCLUDE = /usr/local/cuda-$(CUDA_VERSION)/include
MGPU_INCLUDE = /usr/local/src/moderngpu/include
INCLUDE_FLAGS = -I$(CUDA_INCLUDE) -I$(MGPU_INCLUDE)

CUDA_LIB = /usr/local/cuda-$(CUDA_VERSION)/lib64
LINK_FLAGS = -L$(CUDA_LIB)

MGPU_SRC = /usr/local/src/moderngpu/src

TARGETS = thing

all: $(TARGETS)

thing: thing.co | mgpuutil.o mgpucontext.o
    $(CPP_COMPILER) $(CPP_FLAGS) $< $(LINK_FLAGS) -lcudart -o $@ $|

%.co: %.cu
    $(CUDA_COMPILER) $(CUDA_FLAGS) $(INCLUDE_FLAGS) $(LINK_FLAGS) -o $@ -c $<

mgpucontext.o:  $(MGPU_SRC)/mgpucontext.cu
    $(CUDA_COMPILER) $(CUDA_ARGS) $(INCLUDE_FLAGS) -o $@ -c $<

mgpuutil.o:  $(MGPU_SRC)/mgpuutil.cpp
    $(CUDA_COMPILER) $(CUDA_ARGS) $(INCLUDE_FLAGS) -o $@ -c $<

clean:
    rm -f *.o *.co $(TARGETS)

A:

I wrote the plugin CLion CUDA Run Patcher  to provide a temporary fix for this bug in CLion until JetBrains provides a proper fix as tracked in CPP-10292.
The plugin was necessary because CLion ignored any target names from CMake including those specified by target property OUTPUT_NAME.
It's likely this problem won't be fixed until CLion has full CUDA support as tracked in CPP-866.

