Q:

NVCC compile to ptx using CMAKE's cuda_compile_ptx

I have the simple kernel in file kernel.cu
__global__ void add1( double * pi, double c ) 
{
    *pi += c;
}

and can easily compile it to a ptx file kernel.ptx with:

nvcc -ptx kernel.cu

now, I wanted to reproduce the same behaviour using cmake with the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(cmake_ptx)
find_package(CUDA REQUIRED)
cuda_compile_ptx(
  test
  kernel.cu
)

but when I type 

cmake . && make

no ptx file is built. How can I reproduce the behavior of the above nvcc command using cmake? In particular, how to: 

add the standalone compilation of ptx files to a target (e.g. all): I noticed that when there is another cuda_add_executable inside the same CMakeFile, it also builds the ptx file, otherwise not.
name the file like the source file but with .ptx instead of .cu ending:
related to this question:
How do I change the output filename of cuda_compile_ptx in CMake?

A:

cuda_compile_ptx creates only rules to generate files, but doesn't add them to any target. You need to add custom target, that depends on ptx files:
cmake_minimum_required(VERSION 2.8)
project(cmake_ptx)
find_package(CUDA REQUIRED)
cuda_compile_ptx(
  cuda_ptx_files
  kernel.cu
)
add_custom_target(ptx ALL
    DEPENDS ${cuda_ptx_files} kernel.cu
    SOURCES kernel.cu)

Now if you run make or make ptx, it will generate the ptx files.

