Skip to content

Commit

Permalink
[NFC] Integrate clang-format to hctgen (#5732)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
llvm-beanz authored Sep 19, 2023
1 parent d4b3cb6 commit 2431f3f
Show file tree
Hide file tree
Showing 11 changed files with 8,256 additions and 3,869 deletions.
20 changes: 19 additions & 1 deletion cmake/modules/HCT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ option(HLSL_COPY_GENERATED_SOURCES "Copy generated sources if different" Off)

add_custom_target(HCTGen)

find_program(CLANG_FORMAT_EXE NAMES clang-format)

if (NOT CLANG_FORMAT_EXE)
message(WARNING "Clang-format is not available. Generating included sources is not supported.")
if (HLSL_COPY_GENERATED_SOURCES)
message(FATAL_ERROR "Generating sources requires clang-format")
endif ()
endif ()

if (WIN32 AND NOT DEFINED HLSL_AUTOCRLF)
find_program(git_executable NAMES git git.exe git.cmd)
execute_process(COMMAND ${git_executable} config --get core.autocrlf
Expand Down Expand Up @@ -49,6 +58,12 @@ function(add_hlsl_hctgen mode)
${hctdb}
${hctdb_helper})

get_filename_component(output_extension ${full_output} LAST_EXT)

if (CLANG_FORMAT_EXE AND output_extension MATCHES "\.h|\.cpp|\.inl")
set(format_cmd COMMAND ${CLANG_FORMAT_EXE} -i ${temp_output})
endif ()

set(copy_sources Off)
if(ARG_BUILD_DIR OR HLSL_COPY_GENERATED_SOURCES)
set(copy_sources On)
Expand Down Expand Up @@ -77,7 +92,9 @@ function(add_hlsl_hctgen mode)
# file, and define the verification command
if(NOT copy_sources)
set(output ${temp_output})
set(verification COMMAND ${CMAKE_COMMAND} -E compare_files ${temp_output} ${full_output})
if (CLANG_FORMAT_EXE) # Only verify sources if clang-format is available.
set(verification COMMAND ${CMAKE_COMMAND} -E compare_files ${temp_output} ${full_output})
endif()
endif()
if(WIN32 AND NOT HLSL_AUTOCRLF)
set(force_lf "--force-lf")
Expand All @@ -87,6 +104,7 @@ function(add_hlsl_hctgen mode)
COMMAND ${PYTHON_EXECUTABLE}
${hctgen} ${force_lf}
${mode} --output ${temp_output} ${input_flag}
${format_cmd}
COMMENT "Building ${ARG_OUTPUT}..."
DEPENDS ${hct_dependencies}
)
Expand Down
Loading

0 comments on commit 2431f3f

Please sign in to comment.