From 8a16a2ed09c28f8ecf9617f3676093764b261554 Mon Sep 17 00:00:00 2001 From: Blank Spruce <32396809+BlankSpruce@users.noreply.github.com> Date: Sat, 29 Jun 2024 09:59:58 +0200 Subject: [PATCH] Improve consistency of target_link_libraries --- CHANGELOG.md | 4 + README.md | 2 +- gersemi/__version__.py | 2 +- .../project_command_dumpers.py | 30 ++++++- .../target_link_libraries_command_dumper.py | 59 ------------- .../target_link_libraries_command.in.cmake | 4 +- .../target_link_libraries_command.out.cmake | 82 +++++++++++++------ 7 files changed, 95 insertions(+), 88 deletions(-) delete mode 100644 gersemi/command_invocation_dumpers/target_link_libraries_command_dumper.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aee4ac..40f68e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## [0.13.4] 2024-06-29 +### Fixed +- improve consistency of `target_link_libraries` + ## [0.13.3] 2024-06-15 ### Added - support for new keywords in native commands available in CMake 3.30 diff --git a/README.md b/README.md index c405158..b09e5e9 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ You can use gersemi with a pre-commit hook by adding the following to `.pre-comm ```yaml repos: - repo: https://github.com/BlankSpruce/gersemi - rev: 0.13.3 + rev: 0.13.4 hooks: - id: gersemi ``` diff --git a/gersemi/__version__.py b/gersemi/__version__.py index d6f1370..132f988 100644 --- a/gersemi/__version__.py +++ b/gersemi/__version__.py @@ -4,4 +4,4 @@ __license__ = "MPL 2.0" __title__ = "gersemi" __url__ = "https://github.com/BlankSpruce/gersemi" -__version__ = "0.13.3" +__version__ = "0.13.4" diff --git a/gersemi/command_invocation_dumpers/project_command_dumpers.py b/gersemi/command_invocation_dumpers/project_command_dumpers.py index 0a58e85..502795e 100644 --- a/gersemi/command_invocation_dumpers/project_command_dumpers.py +++ b/gersemi/command_invocation_dumpers/project_command_dumpers.py @@ -1,4 +1,4 @@ -from typing import List +from typing import Iterable, List, Mapping from gersemi.command_line_formatter import CommandLineFormatter from gersemi.keyword_with_pairs_formatter import KeywordWithPairsFormatter from gersemi.keywords import AnyMatcher, KeywordMatcher @@ -9,7 +9,6 @@ MultipleSignatureCommandInvocationDumper, ) from .section_aware_command_invocation_dumper import SectionAwareCommandInvocationDumper -from .target_link_libraries_command_dumper import TargetLinkLibraries from .two_word_keyword_isolator import TwoWordKeywordIsolator @@ -456,6 +455,33 @@ class TargetLinkDirectories(ArgumentAwareCommandInvocationDumper): multi_value_keywords = ["INTERFACE", "PUBLIC", "PRIVATE"] +_debug_optimized_general: Mapping[KeywordMatcher, Iterable[KeywordMatcher]] = { + "one_value_keywords": ["debug", "optimized", "general"] +} + + +class TargetLinkLibraries( + SectionAwareCommandInvocationDumper, ArgumentAwareCommandInvocationDumper +): + front_positional_arguments = [""] + multi_value_keywords = [ + "INTERFACE", + "PUBLIC", + "PRIVATE", + "LINK_PRIVATE", + "LINK_PUBLIC", + "LINK_INTERFACE_LIBRARIES", + ] + sections = { + "INTERFACE": _debug_optimized_general, + "PUBLIC": _debug_optimized_general, + "PRIVATE": _debug_optimized_general, + "LINK_PRIVATE": _debug_optimized_general, + "LINK_PUBLIC": _debug_optimized_general, + "LINK_INTERFACE_LIBRARIES": _debug_optimized_general, + } + + class TargetLinkOptions(ArgumentAwareCommandInvocationDumper): front_positional_arguments = [""] options = ["BEFORE"] diff --git a/gersemi/command_invocation_dumpers/target_link_libraries_command_dumper.py b/gersemi/command_invocation_dumpers/target_link_libraries_command_dumper.py deleted file mode 100644 index 00d4ab3..0000000 --- a/gersemi/command_invocation_dumpers/target_link_libraries_command_dumper.py +++ /dev/null @@ -1,59 +0,0 @@ -from lark import Tree -from lark.visitors import Transformer_InPlace -from gersemi.ast_helpers import is_one_of_keywords -from gersemi.utils import advance -from .argument_aware_command_invocation_dumper import ( - ArgumentAwareCommandInvocationDumper, -) - - -class IsolateConfigurationTypeAndItem(Transformer_InPlace): - keywords = ["debug", "optimized", "general"] - - def arguments(self, children): - new_children = [] - if len(children) < 2: - return Tree("arguments", new_children) - - iterator = zip(children, children[1:]) - is_one_of_defined_keywords = is_one_of_keywords(self.keywords) - for one_behind, current in iterator: - if is_one_of_defined_keywords(one_behind): - new_children += [Tree("specified_item", [one_behind, current])] - _, current = advance(iterator, times=1, default=(None, None)) - if current is None: - break - else: - new_children += [one_behind] - else: - new_children += [item for item in [current] if item is not None] - return Tree("arguments", new_children) - - -class TargetLinkLibraries(ArgumentAwareCommandInvocationDumper): - front_positional_arguments = [""] - multi_value_keywords = [ - "INTERFACE", - "PUBLIC", - "PRIVATE", - "LINK_PRIVATE", - "LINK_PUBLIC", - "LINK_INTERFACE_LIBRARIES", - ] - - def specified_item(self, tree): - specifier, item = tree.children - formatted_specifier = self.visit(specifier) - with self.not_indented(): - formatted_item = self.visit(item) - return f"{formatted_specifier} {formatted_item}" - - def _split_arguments(self, arguments): - preprocessed = IsolateConfigurationTypeAndItem().transform( - Tree("arguments", arguments) - ) - return super()._split_arguments(preprocessed.children) - - def arguments(self, tree): - preprocessed = IsolateConfigurationTypeAndItem().transform(tree) - return super().arguments(preprocessed) diff --git a/tests/formatter/target_link_libraries_command.in.cmake b/tests/formatter/target_link_libraries_command.in.cmake index 9f9a9f6..537f299 100644 --- a/tests/formatter/target_link_libraries_command.in.cmake +++ b/tests/formatter/target_link_libraries_command.in.cmake @@ -56,4 +56,6 @@ target_link_libraries(long_arg__________________________________________________ target_link_libraries(long_arg____________________________________________________________ PUBLIC debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ general long_arg____________________________________________________________) -target_link_libraries(long_arg____________________________________________________________ PUBLIC long_arg____________________________________________________________ debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ long_arg____________________________________________________________ general long_arg____________________________________________________________ PRIVATE long_arg____________________________________________________________ debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ long_arg____________________________________________________________ general long_arg____________________________________________________________ long_arg____________________________________________________________ INTERFACE long_arg____________________________________________________________ debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ long_arg____________________________________________________________ general long_arg____________________________________________________________ long_arg____________________________________________________________) \ No newline at end of file +target_link_libraries(long_arg____________________________________________________________ PUBLIC long_arg____________________________________________________________ debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ long_arg____________________________________________________________ general long_arg____________________________________________________________ PRIVATE long_arg____________________________________________________________ debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ long_arg____________________________________________________________ general long_arg____________________________________________________________ long_arg____________________________________________________________ INTERFACE long_arg____________________________________________________________ debug long_arg____________________________________________________________ optimized long_arg____________________________________________________________ long_arg____________________________________________________________ general long_arg____________________________________________________________ long_arg____________________________________________________________) + +target_link_libraries(TGT PUBLIC debug foo optimized long_arg____________________________________________________________ general foo debug long_arg__________________________________________________________) diff --git a/tests/formatter/target_link_libraries_command.out.cmake b/tests/formatter/target_link_libraries_command.out.cmake index 20f3f1a..e3d4f14 100644 --- a/tests/formatter/target_link_libraries_command.out.cmake +++ b/tests/formatter/target_link_libraries_command.out.cmake @@ -115,32 +115,44 @@ target_link_libraries( target_link_libraries( TGT PUBLIC - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ - general long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ + general + long_arg____________________________________________________________ ) target_link_libraries( TGT PUBLIC long_arg____________________________________________________________ - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ long_arg____________________________________________________________ - general long_arg____________________________________________________________ + general + long_arg____________________________________________________________ PRIVATE long_arg____________________________________________________________ - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ long_arg____________________________________________________________ - general long_arg____________________________________________________________ + general + long_arg____________________________________________________________ long_arg____________________________________________________________ INTERFACE long_arg____________________________________________________________ - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ long_arg____________________________________________________________ - general long_arg____________________________________________________________ + general + long_arg____________________________________________________________ long_arg____________________________________________________________ ) @@ -224,31 +236,53 @@ target_link_libraries( target_link_libraries( long_arg____________________________________________________________ PUBLIC - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ - general long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ + general + long_arg____________________________________________________________ ) target_link_libraries( long_arg____________________________________________________________ PUBLIC long_arg____________________________________________________________ - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ long_arg____________________________________________________________ - general long_arg____________________________________________________________ + general + long_arg____________________________________________________________ PRIVATE long_arg____________________________________________________________ - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ long_arg____________________________________________________________ - general long_arg____________________________________________________________ + general + long_arg____________________________________________________________ long_arg____________________________________________________________ INTERFACE long_arg____________________________________________________________ - debug long_arg____________________________________________________________ - optimized long_arg____________________________________________________________ + debug + long_arg____________________________________________________________ + optimized + long_arg____________________________________________________________ long_arg____________________________________________________________ - general long_arg____________________________________________________________ + general + long_arg____________________________________________________________ long_arg____________________________________________________________ ) + +target_link_libraries( + TGT + PUBLIC + debug foo + optimized + long_arg____________________________________________________________ + general foo + debug long_arg__________________________________________________________ +)