cmakecudabuildconfigurationcmake-custom-commandfatbin

How can I build a fatbin file using CMake?


I want to build a fatbin file from my .cu source file, using CMake.

I've tried this:

add_library(dummy_lib OBJECT my_src.cu)
set_property(TARGET dummy_lib PROPERTY CUDA_PTX_COMPILATION ON)
add_custom_command(
  TARGET dummy_lib POST_BUILD
  COMMAND nvcc -fatbin 
     -o "$<TARGET_FILE_BASE_NAME::dummy_lib>.fatbin" 
     "$<TARGET_FILE:dummy_lib>"
  VERBATIM)

Unfortunately - this doesn't work, since you can have a POST_BUILD dependency on an OBJECT-type library. However, if I remove the OBJECT property, then I can't use CUDA_PTX_COMPILATION ON on it...

Then I'd though I'd try the OUTPUT version of add_custom_command():

add_library(dummy_lib OBJECT my_src.cu)
set_property(TARGET dummy_lib PROPERTY CUDA_PTX_COMPILATION ON)
add_custom_command(
  OUTPUT my_src.fatbin
  COMMAND ${CMAKE_CUDA_COMPILER} -fatbin -o my_src.fatbin "$<TARGET_FILE:dummy_lib>"
  MAIN_DEPENDENCY dummy_lib
)

and this doesn't trigger any errors... but doesn't build the fatbin file either.

How do I get CMake to build my fatbin? :-(


Solution

  • Forget the PTX intermediate file, and do this instead:

    add_custom_command(
        OUTPUT my_src.fatbin
        COMMAND ${CMAKE_CUDA_COMPILER} -fatbin 
            -o ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/foo.fatbin 
            "${CMAKE_CURRENT_SOURCE_DIR}/my_src.cu"
        MAIN_DEPENDENCY my_src.cu
    )
    add_custom_target(dummy ALL DEPENDS my_src.fatbin)