How do I change the output filename of cuda_compile_ptx in CMake?

cmake, cuda

Solution

Ideally you should be able to specify `-o <outputName>`. However the issue is that the `CUDA_COMPILE_PTX` macro actually overrides the `-o` option with `cuda_compile_ptx_generated_${filename}.ptx`

Here are two alternate ways add CUDA compilation to your project

- You can still use `CUDA_COMPILE_PTX` but work around the issue by renaming it with `add_custom_command`: `add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/my_ptx.ptx COMMAND ${CMAKE_COMMAND} -E copy ${cuda_ptx_files} ${CMAKE_BINARY_DIR}/my_ptx.ptx DEPENDS ${cuda_ptx_files})` and use `${CMAKE_BINARY_DIR}/my_ptx.ptx` in `add_custom_target`

- There are better alternatives for compiling CUDA PTXs with CMake. One very good example of a macro do to this: https://github.com/nvpro-samples/shared_sources/blob/master/cmake/private/FindCuda.cmake. An example of how to use this macro can be found here: https://github.com/nvpro-samples/gl_cuda_interop_pingpong_st/blob/master/CMakeLists.txt.

Problem

In CMAKE with FindCUDA, given an input file `filename.cu`, the `cuda_compile_ptx` command generates output filenames of the form `cuda_compile_ptx_generated_filename.cu.ptx`, but I need the output filenames to be of the form `filename.ptx`. Is there a simple way to make this work?

Original source