CMakeLists.txt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. cmake_minimum_required(VERSION 3.8)
  2. project (CudaOnlyExportPTX CUDA)
  3. #Goal for this example:
  4. # How to generate PTX files instead of OBJECT files
  5. # How to reference PTX files for custom commands
  6. # How to install PTX files
  7. add_library(CudaPTX OBJECT kernelA.cu kernelB.cu)
  8. set_property(TARGET CudaPTX PROPERTY CUDA_PTX_COMPILATION ON)
  9. #Test ObjectFiles with file(GENERATE)
  10. file(GENERATE
  11. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gen_$<LOWER_CASE:$<CONFIG>/>path_to_objs.h
  12. CONTENT [[
  13. #include <vector>
  14. #include <string>
  15. #ifndef path_to_objs
  16. #define path_to_objs
  17. static std::string ptx_paths = "$<TARGET_OBJECTS:CudaPTX>";
  18. #endif
  19. ]]
  20. )
  21. #We are going to need a wrapper around bin2c for multiple reasons
  22. # 1. bin2c only converts a single file at a time
  23. # 2. bin2c has only standard out support, so we have to manually
  24. # redirect to a cmake buffer
  25. # 3. We want to pack everything into a single output file, so we
  26. # need to also pass the --name option
  27. set(output_file ${CMAKE_CURRENT_BINARY_DIR}/embedded_objs.h)
  28. get_filename_component(cuda_compiler_bin "${CMAKE_CUDA_COMPILER}" DIRECTORY)
  29. find_program(bin_to_c
  30. NAMES bin2c
  31. PATHS ${cuda_compiler_bin}
  32. )
  33. if(NOT bin_to_c)
  34. message(FATAL_ERROR
  35. "bin2c not found:\n"
  36. " CMAKE_CUDA_COMPILER='${CMAKE_CUDA_COMPILER}'\n"
  37. " cuda_compiler_bin='${cuda_compiler_bin}'\n"
  38. )
  39. endif()
  40. add_custom_command(
  41. OUTPUT "${output_file}"
  42. COMMAND ${CMAKE_COMMAND}
  43. "-DBIN_TO_C_COMMAND=${bin_to_c}"
  44. "-DOBJECTS=$<TARGET_OBJECTS:CudaPTX>"
  45. "-DOUTPUT=${output_file}"
  46. -P ${CMAKE_CURRENT_SOURCE_DIR}/bin2c_wrapper.cmake
  47. VERBATIM
  48. DEPENDS $<TARGET_OBJECTS:CudaPTX>
  49. COMMENT "Converting Object files to a C header"
  50. )
  51. add_executable(CudaOnlyExportPTX main.cu ${output_file})
  52. add_dependencies(CudaOnlyExportPTX CudaPTX)
  53. target_include_directories(CudaOnlyExportPTX PRIVATE
  54. ${CMAKE_CURRENT_BINARY_DIR} )
  55. target_compile_definitions(CudaOnlyExportPTX PRIVATE
  56. "CONFIG_TYPE=gen_$<LOWER_CASE:$<CONFIG>>")
  57. if(APPLE)
  58. # We need to add the default path to the driver (libcuda.dylib) as an rpath, so that
  59. # the static cuda runtime can find it at runtime.
  60. target_link_libraries(CudaOnlyExportPTX PRIVATE -Wl,-rpath,/usr/local/cuda/lib)
  61. endif()
  62. #Verify that we can install object targets properly
  63. install(TARGETS CudaPTX CudaOnlyExportPTX
  64. EXPORT cudaPTX
  65. RUNTIME DESTINATION bin
  66. LIBRARY DESTINATION lib
  67. OBJECTS DESTINATION objs
  68. )
  69. install(EXPORT cudaPTX DESTINATION lib/cudaPTX)