-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommonFunctions.cmake
135 lines (112 loc) · 4.86 KB
/
CommonFunctions.cmake
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# file manipulation functions list:
# add_dir_to_target
# add_dir_to_target_recurse
# group_files_by_directory
# dir_to_var
# dir_to_var_recurse
# add_subdirectory_with_folder
# shader functions list:
# compile_shader_glsl
# compile_shader_hlsl
#### require SPIRV_OUTPUT_DIR and DXC_PATH to be set
########### FILE MANIPULATION FUNCTIONS ###########
function(group_files_by_directory)
cmake_parse_arguments(GROUP_FILES "" "" "SRCS" ${ARGN})
foreach(FILE ${GROUP_FILES_SRCS})
# Get the directory of the source file
get_filename_component(PARENT_DIR ${FILE} DIRECTORY)
# Remove common directory prefix to make the group
string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} "" GROUP ${PARENT_DIR})
# Make sure we are using windows slashes
# string(REPLACE "/" "\\" GROUP ${GROUP})
# Group into "Source Files" and "Header Files"
# if ("${FILE}" MATCHES ".*\\.cpp")
# set(GROUP "Source Files${GROUP}")
# elseif("${FILE}" MATCHES ".*\\.h")
# set(GROUP "Header Files${GROUP}")
# endif()
source_group(${GROUP} FILES ${FILE})
endforeach()
endfunction()
function(dir_to_var PATH SOURCES)
file(GLOB TMP_SRC ${PATH}/*.cpp ${PATH}/*.c ${PATH}/*.hpp ${PATH}/*.h)
group_files_by_directory(SRCS ${TMP_SRC})
set(${SOURCES} ${TMP_SRC} PARENT_SCOPE)
endfunction()
function(dir_to_var_recurse PATH SOURCES)
file(GLOB_RECURSE TMP_SRC ${PATH}/*.cpp ${PATH}/*.c ${PATH}/*.hpp ${PATH}/*.h)
group_files_by_directory(SRCS ${TMP_SRC})
set(${SOURCES} ${TMP_SRC} PARENT_SCOPE)
endfunction()
function(add_dir_to_target TARGET PATH)
file(GLOB TMP_SRC ${PATH}/*.cpp ${PATH}/*.c)
file(GLOB TMP_HDR ${PATH}/*.hpp ${PATH}/*.h)
target_sources(${TARGET} PRIVATE ${TMP_SRC})
target_sources(${TARGET} PRIVATE ${TMP_HDR})
group_files_by_directory(SRCS ${TMP_SRC})
group_files_by_directory(SRCS ${TMP_HDR})
endfunction()
function(add_dir_to_target_recurse TARGET PATH)
file(GLOB_RECURSE TMP_SRC ${PATH}/*.cpp ${PATH}/*.c ${PATH}/*.hpp ${PATH}/*.h)
target_sources(${TARGET} PRIVATE ${TMP_SRC})
group_files_by_directory(SRCS ${TMP_SRC})
endfunction()
########### SHADER FUNCTIONS ###########
function(compile_shader_glsl shaderName stages binaryLocation)
get_filename_component(STEM ${shaderName} NAME_WLE)
get_filename_component(SHADER_PATH ${shaderName} ABSOLUTE)
get_filename_component(DIR ${shaderName} DIRECTORY)
file(RELATIVE_PATH SHADER_PATH_REL ${CMAKE_CURRENT_SOURCE_DIR} ${SHADER_PATH})
set(SPIRV "${SPIRV_OUTPUT_DIR}${DIR}/${STEM}.csd")
cmake_path(GET SPIRV PARENT_PATH SPIRV_DIR)
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory "${SPIRV_OUTPUT_DIR}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${SPIRV_DIR}"
COMMAND ${CMAKE_COMMAND} -E env DXC_PATH=${DXC_PATH} "$<TARGET_FILE_DIR:${TARGET}>/ShaderCompiler" ${SPIRV} ${SHADER_PATH} ${stages}
# COMMAND ${CMAKE_COMMAND} -E copy ${SPIRV} "$<TARGET_FILE_DIR:${TARGET}>${DIR}/${STEM}.csd"
DEPENDS ${shaderName} ShaderCompiler
)
# just a weird way to append a value to a list in the parent scope
set(TMP_LIST ${${binaryLocation}})
list(APPEND TMP_LIST ${SPIRV})
set(${binaryLocation} "${TMP_LIST}" PARENT_SCOPE)
endfunction(compile_shader_glsl)
function(compile_shader_hlsl shaderName stages binaryLocation)
get_filename_component(STEM ${shaderName} NAME_WLE)
get_filename_component(SHADER_PATH ${shaderName} ABSOLUTE)
get_filename_component(DIR ${shaderName} DIRECTORY)
file(RELATIVE_PATH SHADER_PATH_REL ${CMAKE_CURRENT_SOURCE_DIR} ${SHADER_PATH})
set(SPIRV "${SPIRV_OUTPUT_DIR}${DIR}/${STEM}.csd")
cmake_path(GET SPIRV PARENT_PATH SPIRV_DIR)
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory "${SPIRV_OUTPUT_DIR}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${SPIRV_DIR}"
COMMAND ${CMAKE_COMMAND} -E env DXC_PATH=${DXC_PATH} "$<TARGET_FILE_DIR:${TARGET}>/ShaderCompiler" ${SPIRV} ${SHADER_PATH} ${stages}
# COMMAND ${CMAKE_COMMAND} -E copy ${SPIRV} "$<TARGET_FILE_DIR:${TARGET}>${DIR}/${STEM}.csd"
DEPENDS ${shaderName} ShaderCompiler
)
# just a weird way to append a value to a list in the parent scope
set(TMP_LIST ${${binaryLocation}})
list(APPEND TMP_LIST ${SPIRV})
set(${binaryLocation} "${TMP_LIST}" PARENT_SCOPE)
endfunction(compile_shader_hlsl)
function(get_all_targets _result _dir)
get_property(_subdirs DIRECTORY "${_dir}" PROPERTY SUBDIRECTORIES)
foreach(_subdir IN LISTS _subdirs)
get_all_targets(${_result} "${_subdir}")
endforeach()
get_property(_sub_targets DIRECTORY "${_dir}" PROPERTY BUILDSYSTEM_TARGETS)
set(${_result} ${${_result}} ${_sub_targets} PARENT_SCOPE)
endfunction()
function(add_subdirectory_with_folder _folder_name _folder)
add_subdirectory(${_folder} ${ARGN})
get_all_targets(_targets "${_folder}")
foreach(_target IN LISTS _targets)
set_target_properties(
${_target}
PROPERTIES FOLDER "${_folder_name}"
)
endforeach()
endfunction()