Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

multiplemonomials
Copy link
Contributor

@multiplemonomials multiplemonomials commented Dec 8, 2020

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

[X] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[X] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers

@hugueskamba @0xc0170


@mergify mergify bot added the needs: work label Dec 8, 2020
@ciarmcom ciarmcom added the release-type: patch Indentifies a PR as containing just a patch label Dec 8, 2020
@ciarmcom ciarmcom requested review from 0xc0170, hugueskamba and a team December 8, 2020 06:00
@ciarmcom
Copy link
Member

ciarmcom commented Dec 8, 2020

@multiplemonomials, thank you for your changes.
@hugueskamba @0xc0170 @ARMmbed/mbed-os-core @ARMmbed/mbed-os-tools @ARMmbed/mbed-os-hal @ARMmbed/mbed-os-test @ARMmbed/mbed-os-connectivity @ARMmbed/mbed-os-maintainers please review.

@0xc0170 0xc0170 requested review from rajkan01 and removed request for a team December 8, 2020 08:25
@0xc0170
Copy link
Contributor

0xc0170 commented Dec 8, 2020

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.

@mergify
Copy link

mergify bot commented Dec 10, 2020

This PR cannot be merged due to conflicts. Please rebase to resolve them.

@mergify mergify bot removed the needs: review label Dec 10, 2020
@0xc0170
Copy link
Contributor

0xc0170 commented Dec 10, 2020

Dont rebase yet, we are merging now couple of CMake PRs, for the release. We should be done by tomorrow

@adbridge
Copy link
Contributor

adbridge commented Jan 7, 2021

@multiplemonomials I'm thinking this should be able to be rebased now. @0xc0170 @hugueskamba any reason to hold back?

@ciarmcom ciarmcom added the stale Stale Pull Request label Jan 20, 2021
@ciarmcom
Copy link
Member

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.

@multiplemonomials
Copy link
Contributor Author

@0xc0170 bump^

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 1, 2021

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!

Copy link
Contributor

@rwalton-arm rwalton-arm left a 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
Copy link
Contributor

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.

Copy link
Contributor

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 👍

Copy link
Contributor

@0xc0170 0xc0170 left a 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
Copy link
Contributor

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.

INTERFACE
${common_options}
)
target_compile_options(${target} INTERFACE ${common_options})
Copy link
Contributor

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}
Copy link
Contributor

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 :)

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 3, 2021

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 cmake-format -c tools\cmake\scripts\cmake-format.py -i drivers/CMakeLists.txt:

target_include_directories(
    mbed-core INTERFACE . ./include ./include/drivers
                        ./include/drivers/internal
)

target_sources(
    mbed-core
    INTERFACE source/AnalogIn.cpp
              source/AnalogOut.cpp

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:

  1. Configure it for .cmake and CMakelists that are ours. We should use .codecheckignore to ignore the files we can't change.
  2. Update entire codebase to follow the style configured
  3. Add precommit hook for easy to use
  4. Add the style to our documentation

Have I missed anything?

@adbridge
Copy link
Contributor

@multiplemonomials any update on this? looks like it also needs a rebase.

@multiplemonomials
Copy link
Contributor Author

multiplemonomials commented Mar 10, 2021

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.

@mergify mergify bot removed needs: work release-type: patch Indentifies a PR as containing just a patch stale Stale Pull Request labels Mar 10, 2021
@ladislas
Copy link
Contributor

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.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 11, 2021

Thank you both

@multiplemonomials multiplemonomials deleted the cmake-autoformat branch May 17, 2023 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants