diff --git a/build.gradle b/build.gradle index d68916d3a..9ed7bf787 100644 --- a/build.gradle +++ b/build.gradle @@ -26,15 +26,31 @@ spotless { // formatAnnotations() // Use a different clang-format binary with -PclangFormat=clang-format-15. - def clangFormatBinary = (String) project.findProperty('clangFormat') ?: 'clang-format' - def clangOutput = new ByteArrayOutputStream() - exec { - commandLine clangFormatBinary, '--version' - standardOutput = clangOutput + // FIXME: make clang-format default in the future + // def clangFormatBinary = (String) project.findProperty('clangFormat') ?: 'clang-format' + def clangFormatBinary = (String) project.findProperty('clangFormat') + if (clangFormatBinary) { + def clangVersion = (String) project.findProperty('clangVersion') + if (!clangVersion) { + try { + // Try to read clang-format version if not specified + def clangOutput = new ByteArrayOutputStream() + exec { + ignoreExitValue = true + commandLine clangFormatBinary, '--version' + standardOutput = clangOutput + } + clangVersion = (String) (clangOutput.toString() =~ /\d+\.\d+\.\d+/)[0] + } catch (IndexOutOfBoundsException ignored) { + logger.warn("Failed to read clang-format version using '{}': no version information found", clangFormatBinary) + } catch (Exception e) { + logger.warn("Failed to read clang-format version using '{}': {}", clangFormatBinary, e.getMessage()) + } + } + if (clangVersion) { + clangFormat(clangVersion).pathToExe(clangFormatBinary).style('file') + } } - def clangVersion = (String) (clangOutput.toString() =~ /\d+\.\d+\.\d+/)[0] - // FIXME: enable clangFormat in the future. -// clangFormat(clangVersion).pathToExe(clangFormatBinary).style('file') } }