-
Notifications
You must be signed in to change notification settings - Fork 3k
Add automatic formatter for CMake scripts #14011
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
Conversation
@multiplemonomials, thank you for your changes. |
Thanks @multiplemonomials for this formatter. I'll fetch it this week to try locally. Note, most of us (CMake related) are away later this week, it might take longer to review this. |
This PR cannot be merged due to conflicts. Please rebase to resolve them. |
Dont rebase yet, we are merging now couple of CMake PRs, for the release. We should be done by tomorrow |
@multiplemonomials I'm thinking this should be able to be rebased now. @0xc0170 @hugueskamba any reason to hold back? |
This pull request has automatically been marked as stale because it has had no recent activity. @multiplemonomials, please carry out any necessary work to get the changes merged. Thank you for your contributions. |
@0xc0170 bump^ |
Sorry @multiplemonomials We are having lot of on our plate now to complete. It's on our todo to review in detail, I'll try to get to all oustanding CMake PRs this week. We should definitely adopt this! |
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.
Looks great, just one comment.
@@ -0,0 +1,22 @@ | |||
@echo off |
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.
Could we run cmake-format
using the pre-commit
tool rather than adding our own custom scripts? There is a pre-commit
hook here we could use: https://github.com/cheshirekow/cmake-format-precommit. We get a lot of useful stuff out of the box with pre-commit
. It will also allow us to add more formatting and code style checks easily in the future without the requiring us to add multiple "runner" scripts alongside them.
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.
This would be a good start to start running astyle checks as well 👍
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 would like to proceed wit this PR for the upcoming release to keep our CMake files consistent similar to the rest of coding files in the tree.
I left only couple of questions, we should write the rules are going to enforce (see my questions) - basically we should have style guideline for CMakelists. Can you capture what style this enforces ? As I haven't used this formatter, neither are familiar what "styles" are there for CMake, I would like to find out more.
How was the config for the formatter chosen (based on other project, or most used style, etc) ?
Update: the format is close to what is there as indicated earlier.
disable = False | ||
|
||
# How wide to allow formatted cmake files | ||
line_width = 80 |
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.
Can we extend this to 120 (this is what we suggest in code files as well).
../connectivity/mbedtls/include/mbedtls/ | ||
../connectivity/mbedtls/platform/inc/ | ||
../platform/mbed-trace/include/mbed-trace/ | ||
set(unittest-includes |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
INTERFACE | ||
${common_options} | ||
) | ||
target_compile_options(${target} INTERFACE ${common_options}) |
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.
What options is affecting this line and line above 32 ? is the list above formatted differently because it is over 80 characters long so would not fit on the same line?
INTERFACE | ||
$<$<COMPILE_LANGUAGE:CXX>:${cxx_compile_options}> | ||
target_compile_options( | ||
${target} |
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.
what is the difference in formatting here in this (putting target on the second line), compare to set()
here https://github.com/ARMmbed/mbed-os/pull/14011/files#diff-04b1fabd6daf85a88decfec2f6395fb7b0f18a9967b94b20a73859f90a34c713L6 (same line) ?
I am just trying to understand the rules of the formatter so I can manually do the same style :)
Shall we use this also on our CMakelists ? They should also be consistent. I am trying it locally but can't figure it out how format works, using the config from here, running
it treats includes differently than sources, different alignment there. I would expect both to be aligned the same way. I did something similar long time ago for astyle and it took lot of effort to just get it in and execute on our files. Following my latest review, the style set up in cmake-format is not any of "known styles" is it that we could refer to, there is no "standard", is it? For instance like K&R for C/C++. It is more about this is the cmake style we use, here's the tool that styles it for you, don't need to do anything. If that is the case, let's focus here on the configuration of the tool to have and we figure out the details how to get apply this to all files and do it automatically for users. Steps for adding cmake format:
Have I missed anything? |
@multiplemonomials any update on this? looks like it also needs a rebase. |
I realized that I didn't have time to deal with this properly, so a couple weeks ago I talked to @ladislas and he agreed to take over development and try to fix the remaining issues. He's going to submit a new PR when he has time. |
Yep we also need this for our project so when it's setup I'll make a PR here. Don't have any ETA for the moment though. |
Thank you both |
Summary of changes
This PR implements a basic auto-formatter for CMake scripts and runs it on all the CMake scripts in the repository. If we are going to mandate specific formatting conventions on CMake scripts, then it's great to have a way to enforce those automatically. The scripts use the cmake_format tool and require you to install it before running them.
I tried to configure the formatting config file to match the current code format as close as I could, but there are some things that can't be configured to match (especially related to how certain types of things are wrapped onto different lines). Please check over the formatted code and let me know if we need to change the file in any way.
To use the formatter, just run the script from the root directory of the repository and wait a minute or two (the tool is a bit pokey).
Note: I noticed an odd issue on my system where the Windows and Linux versions of the tool produced somewhat different output for the same files (!!!), so we might want to standardize on using it on only one OS or the other. It could also be a WSL glitch or something, though.
Impact of changes
All CMake code can be automatically formatted by the script. Formatting of some existing code will change.
Migration actions required
None
Documentation
None needed, unless we want to add instructions for running it somewhere (not sure exactly where this would go).
Pull request type
Test results
Reviewers
@hugueskamba @0xc0170