summaryrefslogtreecommitdiffstats
path: root/Tests/CudaOnly/ExportPTX/CMakeLists.txt
blob: ff6e77c33f2ef990b31f4ac0076a4af83dc4c41b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
cmake_minimum_required(VERSION 3.8)
project (ExportPTX CUDA)

#Goal for this example:
# How to generate PTX files instead of OBJECT files
# How to reference PTX files for custom commands
# How to install PTX files

add_library(CudaPTX OBJECT kernelA.cu kernelB.cu)
set_property(TARGET CudaPTX PROPERTY CUDA_PTX_COMPILATION ON)

#Test ObjectFiles with file(GENERATE)
file(GENERATE
     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/gen_$<LOWER_CASE:$<CONFIG>/>path_to_objs.h
     CONTENT [[

#include <vector>
#include <string>

#ifndef path_to_objs
#define path_to_objs

static std::string ptx_paths = "$<TARGET_OBJECTS:CudaPTX>";

#endif

]]
)
#We are going to need a wrapper around bin2c for multiple reasons
# 1. bin2c only converts a single file at a time
# 2. bin2c has only standard out support, so we have to manually
# redirect to a cmake buffer
# 3. We want to pack everything into a single output file, so we
# need to also pass the --name option
set(output_file ${CMAKE_CURRENT_BINARY_DIR}/embedded_objs.h)

get_filename_component(cuda_compiler_bin "${CMAKE_CUDA_COMPILER}" DIRECTORY)
find_program(bin_to_c
  NAMES bin2c
  PATHS ${cuda_compiler_bin}
  )
if(NOT bin_to_c)
  message(FATAL_ERROR
    "bin2c not found:\n"
    "  CMAKE_CUDA_COMPILER='${CMAKE_CUDA_COMPILER}'\n"
    "  cuda_compiler_bin='${cuda_compiler_bin}'\n"
    )
endif()

add_custom_command(
  OUTPUT "${output_file}"
  COMMAND ${CMAKE_COMMAND}
    "-DBIN_TO_C_COMMAND=${bin_to_c}"
    "-DOBJECTS=$<TARGET_OBJECTS:CudaPTX>"
    "-DOUTPUT=${output_file}"
    -P ${CMAKE_CURRENT_SOURCE_DIR}/bin2c_wrapper.cmake
  VERBATIM
  DEPENDS $<TARGET_OBJECTS:CudaPTX>
  COMMENT "Converting Object files to a C header"
  )

add_executable(CudaOnlyExportPTX main.cu ${output_file})
add_dependencies(CudaOnlyExportPTX CudaPTX)
target_include_directories(CudaOnlyExportPTX PRIVATE
                           ${CMAKE_CURRENT_BINARY_DIR} )
target_compile_definitions(CudaOnlyExportPTX PRIVATE
                           "CONFIG_TYPE=gen_$<LOWER_CASE:$<CONFIG>>")

if(APPLE)
  # Help the static cuda runtime find the driver (libcuda.dyllib) at runtime.
  set_property(TARGET CudaOnlyExportPTX PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
endif()

#Verify that we can install object targets properly
install(TARGETS CudaPTX CudaOnlyExportPTX
   EXPORT cudaPTX
   RUNTIME DESTINATION bin
   LIBRARY DESTINATION lib
   OBJECTS DESTINATION objs
   )
install(EXPORT cudaPTX DESTINATION lib/cudaPTX)