Skip to content

Commit 2431f3f

Browse files
authored
[NFC] Integrate clang-format to hctgen (#5732)
This integrates formatting generated sources with clang-format with the hctgen generation process. This change makes a few small adjustments to how the build-time header generation works. This change, disables automated build-time generation for C++ sources if you don't have clang-format available on your system. CMake can automatically detect clang-format installed as part of Visual Studio, or based on your PATH. You can also explicitly set `CLANG_FORMAT_EXE` when configuring to manually override. **Developers on macOS** will need to install clang-format manually and place it on their path to generate DXC's generated sources. If clang-format cannot be found and `HLSL_COPY_GENERATED_SOURCES` is On a fatal error will be reported at configuration time. This change _does not_ make clang-format a requirement to build DXC, but it does make it a requirement if you are modifying the generated sources.
1 parent d4b3cb6 commit 2431f3f

File tree

11 files changed

+8256
-3869
lines changed

11 files changed

+8256
-3869
lines changed

cmake/modules/HCT.cmake

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ option(HLSL_COPY_GENERATED_SOURCES "Copy generated sources if different" Off)
22

33
add_custom_target(HCTGen)
44

5+
find_program(CLANG_FORMAT_EXE NAMES clang-format)
6+
7+
if (NOT CLANG_FORMAT_EXE)
8+
message(WARNING "Clang-format is not available. Generating included sources is not supported.")
9+
if (HLSL_COPY_GENERATED_SOURCES)
10+
message(FATAL_ERROR "Generating sources requires clang-format")
11+
endif ()
12+
endif ()
13+
514
if (WIN32 AND NOT DEFINED HLSL_AUTOCRLF)
615
find_program(git_executable NAMES git git.exe git.cmd)
716
execute_process(COMMAND ${git_executable} config --get core.autocrlf
@@ -49,6 +58,12 @@ function(add_hlsl_hctgen mode)
4958
${hctdb}
5059
${hctdb_helper})
5160

61+
get_filename_component(output_extension ${full_output} LAST_EXT)
62+
63+
if (CLANG_FORMAT_EXE AND output_extension MATCHES "\.h|\.cpp|\.inl")
64+
set(format_cmd COMMAND ${CLANG_FORMAT_EXE} -i ${temp_output})
65+
endif ()
66+
5267
set(copy_sources Off)
5368
if(ARG_BUILD_DIR OR HLSL_COPY_GENERATED_SOURCES)
5469
set(copy_sources On)
@@ -77,7 +92,9 @@ function(add_hlsl_hctgen mode)
7792
# file, and define the verification command
7893
if(NOT copy_sources)
7994
set(output ${temp_output})
80-
set(verification COMMAND ${CMAKE_COMMAND} -E compare_files ${temp_output} ${full_output})
95+
if (CLANG_FORMAT_EXE) # Only verify sources if clang-format is available.
96+
set(verification COMMAND ${CMAKE_COMMAND} -E compare_files ${temp_output} ${full_output})
97+
endif()
8198
endif()
8299
if(WIN32 AND NOT HLSL_AUTOCRLF)
83100
set(force_lf "--force-lf")
@@ -87,6 +104,7 @@ function(add_hlsl_hctgen mode)
87104
COMMAND ${PYTHON_EXECUTABLE}
88105
${hctgen} ${force_lf}
89106
${mode} --output ${temp_output} ${input_flag}
107+
${format_cmd}
90108
COMMENT "Building ${ARG_OUTPUT}..."
91109
DEPENDS ${hct_dependencies}
92110
)

0 commit comments

Comments
 (0)