Automatic CMake formatting and linting with ament_gersemi

Automatic CMake formatting and linting with ament_gersemi

ROS has CMake code style checking provided by the ament_lint_cmake package using cmakelint. This is great but cmake-lint doesn’t fix the issues it finds, and we like automation here in the ROS community.

I’m here to introduce a tool called ament_gersemi that can do CMake code style checking and automatically apply fixes. It is based on the gersemi tool and I think it could be useful to others out there! It has all of the configurability and limitations that gersemi has, but it will at least try to format your CMake files for you.

I have provided a default .gersemirc configuration file, but I would be open to feedback about what that default should be.

  • Does it work with ament_lint_cmake?
    • In my experience, yes. Feel free to point out any cases where the tools conflict.
    • You shouldn’t need ament_lint_cmake if ament_gersemi works well because it also does checking w/o formatting.
  • Why isn’t this released into the ROS distro?
    • It’s probably not mature enough yet.
    • gersemi is a pip-only dependency, and packages that depend on pip-only packages cannot be released.
  • Can it sort my find_package/add_library sources/<insert cmake command here> for me?
    • Maybe. gersemi allows extensions, so we might be able to add some extensions for this.
Original Formatted
cmake_minimum_required(VERSION 3.10)
project(gps_msgs)

set(CMAKE_CXX_STANDARD 14)

set(MSG_DEPS std_msgs)

find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs REQUIRED)

set(MSG_FILES
  msg/GPSFix.msg
  msg/GPSStatus.msg
)

rosidl_generate_interfaces(${PROJECT_NAME}
  ${MSG_FILES}
  DEPENDENCIES ${MSG_DEPS}
)

ament_export_dependencies(rosidl_default_runtime)
ament_package()

install(
  FILES ros1_ros2_mapping.yaml
  DESTINATION share/${PROJECT_NAME}
)

cmake_minimum_required(VERSION 3.10)
project(gps_msgs)

set(CMAKE_CXX_STANDARD 14)

set(MSG_DEPS std_msgs)

find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(std_msgs REQUIRED)

set(
  MSG_FILES
  msg/GPSFix.msg
  msg/GPSStatus.msg
)

rosidl_generate_interfaces(${PROJECT_NAME}
  ${MSG_FILES}
  DEPENDENCIES ${MSG_DEPS}
)

ament_export_dependencies(rosidl_default_runtime)
ament_package()

install(FILES ros1_ros2_mapping.yaml DESTINATION share/${PROJECT_NAME})

2 Likes