-
Notifications
You must be signed in to change notification settings - Fork 616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a 'clang-format' build target #612
Merged
cary-ilm
merged 4 commits into
AcademySoftwareFoundation:master
from
lgritz:lg-clangformat
Jan 24, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright Contributors to the OpenEXR Project. | ||
# | ||
# This module was partially adapted from OpenImageIO, also under the | ||
# same BSD-3-Clause license. | ||
|
||
########################################################################### | ||
# clang-format options | ||
# | ||
# clang-format is a source code reformatter that is part of the LLVM tools. | ||
# It can be used to check adherence to project code formatting rules and | ||
# correct any deviations. If clang-format is found on the system, a | ||
# "clang-format" build target will trigger a reformatting. | ||
# | ||
set (CLANG_FORMAT_ROOT "" CACHE PATH "clang-format executable's directory (will search if not specified") | ||
set (CLANG_FORMAT_INCLUDES "*.h" "*.cpp" | ||
CACHE STRING "Glob patterns to include for clang-format") | ||
set (CLANG_FORMAT_EXCLUDES "" | ||
CACHE STRING "Glob patterns to exclude for clang-format") | ||
|
||
# Look for clang-format. If not in the ordinary execution path, you can | ||
# hint at it with either CLANG_FORMAT_ROOT or LLVM_ROOT (as a CMake variable | ||
# or as an environment variable). | ||
find_program (CLANG_FORMAT_EXE | ||
NAMES clang-format bin/clang-format | ||
HINTS ${CLANG_FORMAT_ROOT} | ||
ENV CLANG_FORMAT_ROOT | ||
LLVM_ROOT | ||
ENV LLVM_ROOT | ||
DOC "Path to clang-format executable") | ||
|
||
# If clang-format was found, set up a custom `clang-format` target that will | ||
# reformat all source code with filenames patterns matching | ||
# CLANG_FORMAT_INCLUDES and excluding CLANG_FORMAT_EXCLUDES. | ||
if (CLANG_FORMAT_EXE) | ||
message (STATUS "clang-format found: ${CLANG_FORMAT_EXE}") | ||
# Start with the list of files to include when formatting... | ||
file (GLOB_RECURSE FILES_TO_FORMAT ${CLANG_FORMAT_INCLUDES}) | ||
# ... then process any list of excludes we are given | ||
foreach (_pat ${CLANG_FORMAT_EXCLUDES}) | ||
file (GLOB_RECURSE _excl ${_pat}) | ||
list (REMOVE_ITEM FILES_TO_FORMAT ${_excl}) | ||
endforeach () | ||
#message (STATUS "clang-format file list: ${FILES_TO_FORMAT}") | ||
file (COPY ${CMAKE_CURRENT_SOURCE_DIR}/.clang-format | ||
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) | ||
add_custom_target (clang-format | ||
COMMAND "${CLANG_FORMAT_EXE}" -i -style=file ${FILES_TO_FORMAT} ) | ||
else () | ||
message (STATUS "clang-format not found.") | ||
endif () |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, this is probably ok, although currently the filelist turns into ~26k worth of text, so some people may have issues running this on shells that have a limited command line text. If this comes up, one way to reduce that would be to make this a per-library command with a global pseudo target that depends on each of the per-target ones. Then you could also do something like
get_target_property(target_sources ${target} SOURCES)
to extract rather than having to do the GLOB_RECURSE above.Which if we start adding pybind11 as a sub-project or something may be a good idea to do (to constrain what files clang-format runs against). Up to you, don't want to slow this down getting in - just thinking of ways to improve it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I get what you're saying. How badly do you want that done now? This will probably all get some revision when we get around to really trying this out and reformatting things. My main goal for this patch was just to give the current primary developers a single simple command to reformat the codebase.