-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
117 lines (101 loc) · 4.4 KB
/
Copy pathCMakeLists.txt
File metadata and controls
117 lines (101 loc) · 4.4 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright Contributors to the Open Shading Language project.
# SPDX-License-Identifier: BSD-3-Clause
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
cmake_minimum_required(VERSION 3.19) # Same as top-level CMakeLists.txt
project(examplecuda LANGUAGES CXX)
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE "Release")
endif ()
# Is there a better way to get the location of the main OSL CMakeLists.txt?
set(OSL_LIST_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../../")
# We have to make the path absolute or CMake will fail to find the file...
get_filename_component(OSL_LIST_DIR ${OSL_LIST_DIR} ABSOLUTE)
set(CMAKE_MODULE_PATH "${OSL_LIST_DIR}/src/cmake;${OSL_LIST_DIR}/src/cmake/modules")
include(check_is_enabled)
include(checked_find_package)
find_package(OSL REQUIRED)
find_package(CUDAToolkit REQUIRED)
checked_find_package(LLVM 7.0 REQUIRED)
checked_find_package(Imath 3.1 REQUIRED)
checked_find_package(OpenImageIO 2.4 REQUIRED)
checked_find_package(OptiX REQUIRED)
set(CUDA_TOOLKIT_ROOT_DIR ${CUDAToolkit_ROOT_DIR})
set(CUDA_INCLUDE_DIRS ${CUDAToolkit_INCLUDE_DIRS})
set(CUDA_NVCC_EXECUTABLE ${CUDAToolkit_NVCC_EXECUTABLE})
set(CUDA_LIBRARIES CUDA::cudart)
# Make the build area layout look like we expect
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# TODO: what are the path suffixes on other platforms?
find_library(CUDA_nvrtc_LIBRARY nvrtc HINTS ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64)
find_library(CUDA_cuda_LIBRARY cuda HINTS ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib/stubs lib64/stubs)
if (TARGET CUDA::nvrtc)
set(CUDA_nvrtc_LIBRARY CUDA::nvrtc)
endif ()
if (TARGET CUDA::cuda_driver)
set(CUDA_cuda_LIBRARY CUDA::cuda_driver)
endif ()
# TODO: move to sm_60?
set(CUDA_TARGET_ARCH sm_35)
set (CMAKE_CXX_STANDARD 17 CACHE STRING
"C++ standard to build with (17, 20, etc.)")
# Compile a CUDA source file to PTX with NVCC. (Formerly provided by FindCUDA.cmake)
function(cuda_compile_ptx out_var cuda_src)
cmake_parse_arguments(_ccp "" "" "OPTIONS" ${ARGN})
get_filename_component(cuda_src_we ${cuda_src} NAME_WE)
set(cuda_ptx "${CMAKE_CURRENT_BINARY_DIR}/${cuda_src_we}.ptx")
add_custom_command(OUTPUT ${cuda_ptx}
COMMAND ${CUDA_NVCC_EXECUTABLE}
${_ccp_OPTIONS}
-ptx
${cuda_src}
-o ${cuda_ptx}
MAIN_DEPENDENCY ${cuda_src}
DEPENDS ${cuda_src}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
set(${out_var} ${cuda_ptx} PARENT_SCOPE)
endfunction()
# Compile our "renderer" to PTX
cuda_compile_ptx(CUDA_PTX_FILES cuda_grid_renderer.cu
OPTIONS --gpu-architecture=${CUDA_TARGET_ARCH} --use_fast_math -dc
--std=c++${CMAKE_CXX_STANDARD}
--expt-relaxed-constexpr
-I${OSL_INCLUDES}
-I${IMATH_INCLUDES}
-I${OpenImageIO_INCLUDES}
-I${OPTIX_INCLUDES}
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/cuda_grid_renderer.ptx
COMMAND ${CMAKE_COMMAND} -E rename ${CUDA_PTX_FILES} ${CMAKE_BINARY_DIR}/cuda_grid_renderer.ptx
DEPENDS ${CUDA_PTX_FILES}
)
add_custom_target(cuda_grid_renderer_ptx ALL
DEPENDS ${CMAKE_BINARY_DIR}/cuda_grid_renderer.ptx cuda_grid_renderer.cu
SOURCES cuda_grid_renderer.cu
)
# Compile the rend_lib shadeops to PTX
cuda_compile_ptx(CUDA_PTX_FILES rend_lib.cu
OPTIONS --gpu-architecture=${CUDA_TARGET_ARCH} --use_fast_math -dc
--std=c++${CMAKE_CXX_STANDARD}
--expt-relaxed-constexpr
-I${OSL_INCLUDES}
-I${IMATH_INCLUDES}
-I${OpenImageIO_INCLUDES}
-I${OPTIX_INCLUDES}
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/rend_lib.ptx
COMMAND ${CMAKE_COMMAND} -E rename ${CUDA_PTX_FILES} ${CMAKE_BINARY_DIR}/rend_lib.ptx
DEPENDS ${CUDA_PTX_FILES}
)
add_custom_target(rend_lib_ptx ALL
DEPENDS ${CMAKE_BINARY_DIR}/rend_lib.ptx rend_lib.cu
SOURCES rend_lib.cu
)
# Compile and link the main executable
add_executable(example-cuda example-cuda.cpp cuda_grid_renderer.cpp)
target_link_libraries(example-cuda PRIVATE OSL::oslexec OSL::oslquery ${CUDA_LIBRARIES} ${CUDA_nvrtc_LIBRARY} ${CUDA_cuda_LIBRARY})
target_include_directories(example-cuda PRIVATE ${CUDA_INCLUDE_DIRS})
set_property(TARGET example-cuda PROPERTY CXX_STANDARD 17)
install(TARGETS example-cuda DESTINATION ${CMAKE_CURRENT_SOURCE_DIR})