Skip to content

Commit

Permalink
CMake (MetroRobots#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
DLu authored Dec 8, 2023
1 parent d390021 commit 78a374f
Show file tree
Hide file tree
Showing 5 changed files with 837 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ros_glint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .core import get_linters

from .glinters import package_xml, ros_interfaces
from .glinters import package_xml, cmake, ros_interfaces

__all__ = ['get_linters', 'package_xml', 'ros_interfaces']
__all__ = ['get_linters', 'package_xml', 'cmake', 'ros_interfaces']
184 changes: 184 additions & 0 deletions src/ros_glint/cmake_ordering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
from ros_introspect.components.cmake import ROS_TESTING_FLAGS, is_testing_group
from stylish_cmake_parser import Command, CommandGroup
from enum import IntEnum

BUILD_TARGET_COMMANDS = ['qt5_wrap_cpp', 'add_library', 'add_executable', 'add_rostest',
'target_include_directories', 'add_dependencies', 'target_link_libraries',
'set_target_properties', 'ament_target_dependencies']
TEST_COMMANDS = [('group', variable) for variable in ROS_TESTING_FLAGS] + \
['catkin_download_test_data',
'roslint_cpp', 'roslint_python', 'roslint_add_test',
'catkin_add_nosetests', 'catkin_add_gtest', 'ament_add_gtest', 'add_rostest_gtest',
'ament_lint_auto_find_test_dependencies']
INSTALL_COMMANDS = ['ament_export_targets', 'install', 'catkin_install_python', 'ament_python_install_module',
'ament_python_install_package', 'ament_export_include_directories', 'ament_export_libraries',
'ament_export_dependencies', 'pluginlib_export_plugin_description_file']

BASE_ORDERING = ['cmake_minimum_required', 'project',
('group', 'CMAKE_C_STANDARD'), ('group', 'CMAKE_CXX_STANDARD'), ('group', 'CMAKE_COMPILER_IS_GNUCXX'),
('set', 'CMAKE'),
'set_directory_properties', 'add_compile_options', 'find_package', 'pkg_check_modules',
'moveit_build_options',
('set', 'OTHER'),
'catkin_generate_virtualenv', 'catkin_python_setup', 'add_definitions',
'add_message_files', 'add_service_files', 'add_action_files', 'rosidl_generate_interfaces',
'generate_dynamic_reconfigure_options', 'generate_messages', 'catkin_package', 'catkin_metapackage',
BUILD_TARGET_COMMANDS + ['include_directories'],
]


class CMakeOrderStyle(IntEnum):
TEST_FIRST = 1 # Test commands come strictly before install commands
INSTALL_FIRST = 2 # Test commands come strictly after install commands
MIXED = 3 # Test and install commands are intermingled
UNKNOWN = 4 # There are not install commands and test commands


def classify_content_as_test_or_install(content):
if isinstance(content, CommandGroup) and is_testing_group(content):
return 'test'
elif not isinstance(content, Command):
return
elif content.command_name in TEST_COMMANDS:
return 'test'
elif content.command_name in INSTALL_COMMANDS:
return 'install'


def get_style(cmake):
"""Examine the contents of the cmake and determine the style."""
cats = []
for content in cmake.contents:
cat = classify_content_as_test_or_install(content)
if not cat:
continue
elif len(cats) == 0 or cats[-1] != cat:
cats.append(cat)

if len(cats) > 2:
return CMakeOrderStyle.MIXED

if len(cats) == 2:
if cats[0] == 'install':
return CMakeOrderStyle.INSTALL_FIRST
else:
return CMakeOrderStyle.TEST_FIRST
return CMakeOrderStyle.UNKNOWN


def get_ordering(style):
"""Given the style, return the correct ordering."""
if style == CMakeOrderStyle.INSTALL_FIRST:
return BASE_ORDERING + INSTALL_COMMANDS + TEST_COMMANDS + ['ament_package']
else:
return BASE_ORDERING + TEST_COMMANDS + INSTALL_COMMANDS + ['ament_package']


def get_ordering_index(command_name, ordering):
"""
Given a command name, determine the integer index into the ordering.
The ordering is a list of strings and arrays of strings.
If the command name matches one of the strings in the inner arrays,
the index of the inner array is returned.
If the command name matches one of the other strings, its index is returned.
Otherwise, the length of the ordering is returned (putting non-matches at the end)
"""
for i, o in enumerate(ordering):
if isinstance(o, list):
if command_name in o:
return i
elif command_name == o:
return i
if command_name:
# TODO: Raise Warning
print(f'\tUnsure of ordering for {command_name}')
return len(ordering)


def get_sort_key(content, anchors, ordering):
"""
Given a piece of cmake content, return a tuple representing its sort_key.
The first element of the tuple is the ordering_index of the content.
The second element is an additional variable used for sorting among elements with the same ordering_index
Most notably, we want all build commands with a particular library/executable to be grouped together.
In that case, we use the anchors parameter, which is an ordered list of all the library/executables in the file.
Then, the second variable is a tuple itself, with the first element being the index of library/executable in the
anchors list, and the second is an integer representing the canonical order of the build commands.
"""
if content is None:
return len(ordering) + 1, None
index = None
key = ()
if content.__class__ == CommandGroup:
key_token = ()
for token in content.initial_cmd.get_tokens(include_name=True):
if token == 'NOT':
continue
key_token = token
break
index = get_ordering_index(('group', key_token), ordering)
elif content.command_name == 'set':
token = content.get_tokens(include_name=True)[0]
if token.startswith('CMAKE_'):
set_type = 'CMAKE'
else:
set_type = 'OTHER'
index = get_ordering_index(('set', set_type), ordering)
else: # Command
index = get_ordering_index(content.command_name, ordering)
if content.command_name in BUILD_TARGET_COMMANDS:
token = content.first_token()
key = anchors.index(token), BUILD_TARGET_COMMANDS.index(content.command_name)
elif content.command_name == 'include_directories' and 'include_directories' in anchors:
key = -1, anchors.index('include_directories')
elif content.command_name == 'find_package':
token = content.get_tokens()[0]
if token == 'catkin' or token.startswith('ament_cmake'):
key = -1, token # Force ament_cmake/catkin to come first
else:
key = 0, token
return index, key


def get_ordered_build_targets(cmake):
targets = []
for content in cmake.contents:
if content.__class__ != Command:
continue
if content.command_name == 'include_directories':
targets.append('include_directories')
continue
elif content.command_name not in BUILD_TARGET_COMMANDS:
continue
token = content.first_token()
if token not in targets:
targets.append(token)
return targets


def get_insertion_index(cmake, cmd):
anchors = get_ordered_build_targets(cmake)
ordering = get_ordering(CMakeOrderStyle.TEST_FIRST)

new_key = get_sort_key(cmd, anchors, ordering)
i_index = 0

for i, content in enumerate(cmake.contents):
if isinstance(content, str):
continue
key = get_sort_key(content, anchors, ordering)
if key <= new_key:
i_index = i + 1
elif key[0] != len(ordering):
return i_index
return len(cmake.contents)


def insert_in_order(cmake, cmd):
cmake.insert(cmd, get_insertion_index(cmake, cmd))
172 changes: 172 additions & 0 deletions src/ros_glint/data/cmake.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
## Add support for C++11, supported in ROS Kinetic and newer
# add_definitions(-std=c++11)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependencies might have been
## pulled in transitively but can be declared for certainty nonetheless:
## * add a build_depend tag for "message_generation"
## * add a run_depend tag for "message_runtime"
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
## Generate actions in the 'action' folder
# add_action_files(
# Action1.action
# Action2.action
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs # Or other packages containing msgs
# )
###################################
## catkin specific configuration ##
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
# INCLUDE_DIRS include
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
## Build ##
## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
# include
# ${catkin_INCLUDE_DIRS}
## Declare a cpp library
# add_library(${PROJECT_NAME}
## Declare a cpp executable
## Add cmake target dependencies of the executable/library
## as an example, message headers may need to be generated before nodes
## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
# ${catkin_LIBRARIES}
## Install ##
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables and/or libraries for installation
## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# install(TARGETS ${PROJECT_NAME}_node
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
###########
## Add gtest based cpp test target and link libraries
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
####
## * add a build_depend tag for "message_generation"
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## Declare ROS dynamic reconfigure parameters ##
## To declare and build dynamic reconfigure parameters within this
## * add a build_depend and a run_depend tag for "dynamic_reconfigure"
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * add "dynamic_reconfigure" to
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
## Declare a C++ library
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/global_planner_test_suite_node.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
# catkin_install_python(PROGRAMS
# find dependencies
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
# comment the line when a copyright and license is added to all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
#set(ament_cmake_cpplint_FOUND TRUE)
11 changes: 11 additions & 0 deletions src/ros_glint/data/cmake_patterns.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# LIBRARIES %(package)s
# add_library(%(package)s
# src/${PROJECT_NAME}/%(package)s.cpp
# add_executable(%(package)s_node src/%(package)s_node.cpp)
# add_executable(${PROJECT_NAME}_node src/%(package)s_node.cpp)
# add_dependencies(%(package)s_node %(package)s_generate_messages_cpp)
# target_link_libraries(%(package)s_node
# install(TARGETS %(package)s %(package)s_node
# catkin_add_gtest(${PROJECT_NAME}-test test/test_%(package)s.cpp)
# add_dependencies(%(package)s ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
# add_dependencies(%(package)s_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
Loading

0 comments on commit 78a374f

Please sign in to comment.