4 VARIABLE_PATTERN = re.compile(
r'\$\{([^\}]+)\}')
5 QUOTED_PATTERN = re.compile(
r'"([^"]+)"')
7 BUILD_TARGET_COMMANDS = [
'add_library',
'add_executable',
'add_rostest',
'add_dependencies',
'target_link_libraries']
9 ORDERING = [
'cmake_minimum_required',
'project',
'set_directory_properties',
'find_package',
'pkg_check_modules',
10 'set',
'catkin_generate_virtualenv',
'catkin_python_setup',
'add_definitions',
11 'add_message_files',
'add_service_files',
'add_action_files',
12 'generate_dynamic_reconfigure_options',
'generate_messages',
'catkin_package',
'catkin_metapackage',
13 BUILD_TARGET_COMMANDS + [
'include_directories'],
14 [
'catkin_download_test_data',
'roslint_cpp',
'roslint_python',
'roslint_add_test',
'catkin_add_nosetests'],
15 'catkin_add_gtest',
'group',
16 [
'install',
'catkin_install_python']]
20 for i, o
in enumerate(ORDERING):
24 elif command_name == o:
27 print(
'\tUnsure of ordering for', command_name)
33 return len(ORDERING) + 1,
None 36 if content.__class__ == CommandGroup:
38 sections = content.initial_tag.get_real_sections()
40 key = sections[0].name
43 if content.command_name
in BUILD_TARGET_COMMANDS:
44 token = content.first_token()
45 if token
not in anchors:
47 key = anchors.index(token), BUILD_TARGET_COMMANDS.index(content.command_name)
48 elif content.command_name ==
'include_directories' and 'include_directories' in anchors:
49 key = anchors.index(
'include_directories')
54 def __init__(self, prename='', name_val_sep=' ', val_sep=' '):
64 def __init__(self, name='', values=None, style=None):
79 return len(self.
name) > 0
or len(self.
values) > 0
82 s = self.style.prename
83 if len(self.
name) > 0:
86 s += self.style.name_val_sep
87 s += self.style.val_sep.join(self.
values)
100 return [s
for s
in self.
sections if type(s) != str]
112 self.sections.append(
Section(key, values, style))
117 self.sections.append(section)
128 self.
sections = [section
for section
in self.
sections if section
not in bad_sections]
135 if include_name
and section.name:
136 tokens.append(section.name)
137 tokens += section.values
142 if len(sections) == 0:
146 last.values.append(s)
154 for section
in map(str, self.
sections):
155 if s[-1]
not in '( \n' and section[0]
not in ' \n':
158 if '\n' in s
and s[-1] !=
'\n':
175 def __init__(self, file_path=None, initial_contents=None, depth=0):
177 if initial_contents
is None:
183 if content.__class__ == Command:
184 self.
content_map[content.command_name].append(content)
185 elif content.__class__ == CommandGroup:
191 tokens = cmd.get_tokens(include_name=
True)
192 self.
variables[tokens[0]] =
' '.join(tokens[1:])
200 return project_tags[0].get_tokens(include_name=
True)[0]
205 m = VARIABLE_PATTERN.search(s)
209 for k, v
in self.variables.iteritems():
210 s = s.replace(
'${%s}' % k, v)
215 if token
and token[0] ==
'#':
217 m = QUOTED_PATTERN.match(token)
221 tokens += token.split(
' ')
233 for i, content
in enumerate(self.
contents):
234 if type(content) == str:
239 elif key[0] != len(ORDERING):
246 if i_index > 0
and type(self.
contents[i_index - 1]) != str:
247 sub_contents.append(
'\n')
249 sub_contents.append(
' ' * self.
depth)
250 sub_contents.append(cmd)
251 sub_contents.append(
'\n')
253 sub_contents.append(cmd)
255 sub_contents.append(
'\n')
259 if cmd.__class__ == Command:
261 elif cmd.__class__ == CommandGroup:
265 print(
'\tRemoving %s' % str(cmd).replace(
'\n',
' ').replace(
' ',
''))
266 self.contents.remove(cmd)
279 if resolve_target_name:
280 target = resolved_tokens[0]
282 tokens = cmd.get_tokens(
True)
285 deps = resolved_tokens[1:]
316 if content.__class__ != Command:
318 if content.command_name ==
'include_directories':
319 targets.append(
'include_directories')
321 elif content.command_name
not in BUILD_TARGET_COMMANDS:
323 token = content.first_token()
324 if token
not in targets:
325 targets.append(token)
331 cmd = content.initial_tag
332 if cmd.command_name !=
'if' or len(cmd.sections) == 0
or cmd.sections[0].name !=
'CATKIN_ENABLE_TESTING':
334 sections.append(content.sub)
340 test_files.update(sub.get_library_source())
341 test_files.update(sub.get_executable_source())
346 if len(sections) > 0:
348 if not create_if_needed:
353 initial_cmd.add_section(
'CATKIN_ENABLE_TESTING')
355 test_contents =
CMake(initial_contents=[
'\n'], depth=self.
depth + 1)
359 cg =
CommandGroup(initial_cmd, test_contents, final_cmd)
364 """ Return the first command that matches the command name and 365 has a matching section name. If the section name is not found, 366 return a command with the matching command name""" 370 s = cmd.get_section(section_name)
376 """ This function ensures that there's a CMake command of the given type 377 with the given section name and items somewhere in the file. """ 378 if len(items) == 0
and not zero_okay:
388 cmd.add_section(section_name, sorted(items))
391 needed_items = [item
for item
in items
if item
not in existing]
392 section.values += sorted(needed_items)
396 return ''.join(map(str, self.
contents))
401 with open(fn,
'w')
as cmake:
402 cmake.write(str(self))
def get_resolved_tokens(self, cmd, include_name=False)
def get_test_sections(self)
def get_ordered_build_targets(self)
def get_real_sections(self)
def remove_sections(self, key)
def get_project_name(self)
def __init__(self, file_path=None, initial_contents=None, depth=0)
def __init__(self, prename='', name_val_sep=' ', val_sep=' ')
def remove_all_commands(self, cmd_name)
def get_source_helper(self, tag)
def get_target_build_rules(self)
def __init__(self, command_name)
def __init__(self, initial_tag, sub, close_tag)
def section_check(self, items, cmd_name, section_name='', zero_okay=False)
def get_tokens(self, include_name=False)
def add_section(self, key, values=None, style=None)
def remove_command(self, cmd)
def get_test_source(self)
def add_command(self, cmd)
def get_executable_source(self)
def get_sort_key(content, anchors)
def get_test_section(self, create_if_needed=False)
def get_section(self, key)
def get_ordering_index(command_name)
def resolve_variables(self, var)
def __init__(self, name='', values=None, style=None)
def get_sections(self, key)
def get_library_source(self)
def get_source_build_rules(self, tag, resolve_target_name=False)
def get_executables(self)
def get_command_section(self, command_name, section_name)
def get_insertion_index(self, cmd)