cmake.py
Go to the documentation of this file.
1 from ros_introspection.cmake import Command, is_testing_group
2 
3 from roscompile.cmake import enforce_cmake_ordering, remove_empty_cmake_lines
4 
5 from .util import REPLACE_PACKAGES
6 
7 
8 CATKIN_CMAKE_VARS = {
9  '${CATKIN_GLOBAL_BIN_DESTINATION}': 'bin',
10  '${CATKIN_GLOBAL_INCLUDE_DESTINATION}': 'include',
11  '${CATKIN_GLOBAL_LIB_DESTINATION}': 'lib',
12  '${CATKIN_GLOBAL_LIBEXEC_DESTINATION}': 'lib',
13  '${CATKIN_GLOBAL_SHARE_DESTINATION}': 'share',
14  '${CATKIN_PACKAGE_BIN_DESTINATION}': 'lib/${PROJECT_NAME}',
15  '${CATKIN_PACKAGE_INCLUDE_DESTINATION}': 'include/${PROJECT_NAME}',
16  '${CATKIN_PACKAGE_LIB_DESTINATION}': 'lib',
17  '${CATKIN_PACKAGE_SHARE_DESTINATION}': 'share/${PROJECT_NAME}',
18 }
19 
20 
22  """Convert find_package commands to find one package at a time."""
23  components = ['ament_cmake']
24  for cmd in cmake.content_map['find_package']:
25  tokens = cmd.get_tokens()
26  if not tokens or tokens[0] != 'catkin':
27  continue
28  if cmd.get_section('REQUIRED'):
29  cmps = cmd.get_section('COMPONENTS')
30  if cmps:
31  components += cmps.values
32 
33  cmake.remove_command(cmd)
34 
35  for component in components:
36  if component == 'message_generation':
37  continue
38  if component in REPLACE_PACKAGES:
39  component = REPLACE_PACKAGES[component]
40  cmd = Command('find_package')
41  cmd.add_section('', [component])
42  cmd.add_section('REQUIRED')
43  cmake.add_command(cmd)
44 
45 
47  if not package.cmake.content_map['catkin_package']:
48  return
49 
50  pkg_cmd = package.cmake.content_map['catkin_package'][0]
51  for sname, cmd_name in [('CATKIN_DEPENDS', 'ament_export_dependencies'),
52  ('INCLUDE_DIRS', 'ament_export_include_directories'),
53  ('LIBRARIES', 'ament_export_libraries')]:
54  section = pkg_cmd.get_section(sname)
55  if not section:
56  continue
57  no_cpp = len(package.source_code.get_source_by_language('c++'))
58  if sname == 'CATKIN_DEPENDS' and no_cpp == 0 and 'message_runtime' in section.values:
59  continue
60  cmd = Command(cmd_name)
61  cmd.add_section('', [REPLACE_PACKAGES.get(k, k) for k in section.values])
62  package.cmake.add_command(cmd)
63 
64  cmd = Command('ament_package')
65  package.cmake.add_command(cmd)
66 
67 
69  for cmd in cmake.content_map['install']:
70  for section in cmd.get_sections('DESTINATION'):
71  for i, value in enumerate(section.values):
72  if value in CATKIN_CMAKE_VARS:
73  section.values[i] = CATKIN_CMAKE_VARS[value]
74  cmd.changed = True
75 
76 
77 def remove_cpp11_flag(cmake):
78  to_remove = []
79  for cmd in cmake.content_map['set_directory_properties']:
80  section = cmd.get_section('COMPILE_OPTIONS')
81  bits = list(filter(None, section.values[0][1:-1].split(';')))
82  if '-std=c++11' in bits:
83  bits.remove('-std=c++11')
84  if len(bits) == 0:
85  to_remove.append(cmd)
86  else:
87  section.values = ['"%s"' % ';'.join(bits)]
88  cmd.changed = True
89  for cmd in to_remove:
90  cmake.remove_command(cmd)
91 
92 
94  return [REPLACE_PACKAGES.get(k, k) for k in package.source_code.get_build_dependencies()]
95 
96 
98  cat_var = '${catkin_INCLUDE_DIRS}'
99  # TODO: Probably remove? dirs = ['${%s_INCLUDE_DIRS}' % s for s in get_clean_build_dependencies(package)]
100  for cmd in package.cmake.content_map['include_directories']:
101  section = cmd.sections[0]
102  if cat_var in section.values:
103  section.values.remove(cat_var)
104  cmd.changed = True
105 
106 
107 def rename_commands(cmake, source_name, target_name, remove_sections=[]):
108  for cmd in cmake.content_map[source_name]:
109  cmd.command_name = target_name
110  cmd.changed = True
111  for name in remove_sections:
112  cmd.remove_sections(name)
113  cmake.content_map[target_name] = cmake.content_map[source_name]
114  del cmake.content_map[source_name]
115 
116 
117 def set_up_catkin_libs(package):
118  deps = ['"{}"'.format(s) for s in get_clean_build_dependencies(package)]
119 
120  cat_var = '${catkin_LIBRARIES}'
121  rename_commands(package.cmake, 'target_link_libraries', 'ament_target_dependencies')
122  for cmd in package.cmake.content_map['ament_target_dependencies']:
123  for section in cmd.get_real_sections():
124  if cat_var in section.values:
125  section.values.remove(cat_var)
126  section.values += deps
127  cmd.changed = True
128 
129 
130 def update_tests(package):
131  for content in package.cmake.content_map['group']:
132  if is_testing_group(content):
133  content.initial_tag.sections[0].name = 'BUILD_TESTING'
134  content.initial_tag.changed = True
135  rename_commands(content.sub, 'catkin_add_gtest', 'ament_add_gtest')
136 
137  rename_commands(package.cmake, 'catkin_add_gtest', 'ament_add_gtest')
138 
139 
140 def update_cmake(package):
141  package.cmake.upgrade_minimum_version((3, 5))
142  split_find_package_commands(package.cmake)
143  catkin_to_ament_package(package)
144  update_installation_variables(package.cmake)
145  remove_cpp11_flag(package.cmake)
146  set_up_include_exports(package)
147  set_up_catkin_libs(package)
148  update_tests(package)
149 
150  # Remove deprecated Commands
151  for old_cmd_name in ['catkin_python_setup', 'add_dependencies', 'catkin_package']:
152  package.cmake.remove_all_commands(old_cmd_name)
153  enforce_cmake_ordering(package)
154  remove_empty_cmake_lines(package)
def get_clean_build_dependencies(package)
Definition: cmake.py:93
def rename_commands(cmake, source_name, target_name, remove_sections=[])
Definition: cmake.py:107
def split_find_package_commands(cmake)
Definition: cmake.py:21
def catkin_to_ament_package(package)
Definition: cmake.py:46
def update_installation_variables(cmake)
Definition: cmake.py:68
def set_up_include_exports(package)
Definition: cmake.py:97


magical_ros2_conversion_tool
Author(s):
autogenerated on Wed Mar 3 2021 03:56:02