Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions beman_tidy/lib/checks/base/base_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def __init__(self, repo_info, beman_standard_check_config, name=None):
assert self.repo_path is not None
self.library_name = f"beman.{self.short_name}"
assert self.library_name is not None
self.library_alias = f"beman::{self.short_name}"
assert self.library_alias is not None

# set beman library maturity model
beman_library_maturity_model = beman_standard_check_config[
Expand Down
126 changes: 94 additions & 32 deletions beman_tidy/lib/checks/beman_standard/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def get_cmake_parse_raw(self, skip_comments=True):
def get_cmake_parse_tree(self, skip_comments=True):
return cmake_parser.parser.parse_tree(self.read(), skip_comments=skip_comments)

def get_cmake_library_name(self, ast):
def get_cmake_library_name(self):
ast = self.get_cmake_parse_raw()
cmake_library_name = None

for item in ast:
Expand All @@ -59,7 +60,8 @@ def get_cmake_library_name(self, ast):

return cmake_library_name

def get_cmake_project_name(self, ast):
def get_cmake_project_name(self):
ast = self.get_cmake_parse_raw()
cmake_project_name = None

for item in ast:
Expand All @@ -70,6 +72,47 @@ def get_cmake_project_name(self, ast):

return cmake_project_name

def get_cmake_library_alias(self):
ast = self.get_cmake_parse_raw()
cmake_library_alias = None

for item in ast:
if item.identifier == "add_library":
args = [arg.value for arg in item.args]

# Check that there are 3 args [library_alias, 'ALIAS', library_name]
if len(args) != 3:
continue

# Check that the 2nd arg is 'ALIAS'
if args[1].upper() != "ALIAS":
continue

# Ensure the alias's final component matches the library target's final component.
# E.g., "beman::foo" -> "foo"
alias_last = args[0].split("::")[-1]
target_last = args[2].split(".")[-1]
if alias_last != target_last:
continue

# If the alias matches the expected alias, return it.
if args[0] == self.library_alias:
cmake_library_alias = args[0]
break

return cmake_library_alias

def get_cmake_target_names(self):
ast = self.get_cmake_parse_raw()
cmake_target_names = []

for item in ast:
if item.identifier == "add_library" or item.identifier == "add_executable":
if item.args:
cmake_target_names.append(item.args[0].value)

return cmake_target_names


# TODO cmake.default

Expand All @@ -83,8 +126,7 @@ def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config)

def check(self):
ast = self.get_cmake_parse_raw()
cmake_project_name = self.get_cmake_project_name(ast)
cmake_project_name = self.get_cmake_project_name()

if cmake_project_name is None:
self.log("CMake project name not found. "
Expand Down Expand Up @@ -118,8 +160,7 @@ def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config)

def check(self):
ast = self.get_cmake_parse_raw()
cmake_library_name = self.get_cmake_library_name(ast)
cmake_library_name = self.get_cmake_library_name()

if cmake_library_name is None:
self.log("CMake library target name not found. "
Expand Down Expand Up @@ -150,46 +191,67 @@ def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config)

def check(self):
ast = self.get_cmake_parse_raw()
cmake_library_alias = self.get_cmake_library_alias()

if cmake_library_alias is None:
self.log("Missing or invalid CMake library alias target. "
f"Expected alias target: '{self.library_alias}'. "
"Please update the CMakeLists.txt file according to the Beman Standard. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_alias for more information.")
return False

expected_library_alias = "beman::" + self.short_name
# If we found an alias matching the expected value, the check passes.
return True

for item in ast:
if item.identifier == "add_library":
args = [arg.value for arg in item.args]
def fix(self):
self.log(
"Please update the CMakeLists.txt file so that it creates an alias of the library target named 'beman::<short_name>'. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_alias for more information."
)
return False

# Check that there are 3 args [library_alias, 'ALIAS', library_name]
if len(args) != 3:
continue

# Check that the 2nd arg is 'ALIAS'
if args[1] != "ALIAS":
continue
# Note: this check currently parses only the top-level CMakeLists.txt,
# so it will not see targets defined in add_subdirectory() files.
# TODO: extend check to recurse into subdirectories for full coverage
@register_beman_standard_check("cmake.target_names")
class CMakeTargetNamesCheck(CMakeBaseCheck):
def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config)

# Check that the 1st argument stripped of the prefix matches the 3rd arg stripped
if list(filter(None, args[0].split(":"))) != args[2].split("."):
continue
def check(self):
wrong_prefix = False

if args[0] == expected_library_alias:
return True
cmake_target_names = self.get_cmake_target_names()

self.log("Missing or invalid CMake library alias target. "
f"Expected alias target: '{expected_library_alias}'. "
"Please update the CMakeLists.txt file according to the Beman Standard. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_alias for more information.")
return False
for target_name in cmake_target_names:
# skip the library name and library alias targets
if target_name == self.library_name or target_name == self.library_alias:
continue

# all other targets must begin with '<library_name>.'
if not target_name.startswith(f"{self.library_name}."):
wrong_prefix = True
self.log(f"Invalid CMake target name - got: '{target_name}', expected prefix: '{self.library_name}.'")

if wrong_prefix:
self.log(
"Please update the CMakeLists.txt file according to the Beman Standard. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmaketarget_names for more information."
)

return False

return True

def fix(self):
self.log(
"Please update the CMakeLists.txt file so that it creates an alias of the library target named 'beman::<short_name>'. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmakelibrary_alias for more information."
"Please update the CMakeLists.txt file so that all targets, aside from the library target, begin with the '<library_name>.' prefix. "
"See https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md#cmaketarget_names for more information."
)
return False


# TODO cmake.target_names


# TODO cmake.passive_targets


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# wrong 'add_library' target name
add_library(beman.foobar)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# wrong 'add_executable' target name
add_executable(beman.exemplary.examples.basic)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# multiple wrong target names
add_library(beman.exemplar2)
add_executable(beman.exemplary.tests.67)
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ if(BEMAN_EXEMPLAR_BUILD_TESTS)
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
add_executable(beman.exemplar.examples.basic)
add_subdirectory(examples)
endif()
50 changes: 50 additions & 0 deletions tests/lib/checks/beman_standard/cmake/test_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CMakeProjectNameCheck,
CMakeLibraryNameCheck,
CMakeLibraryAliasCheck,
CMakeTargetNamesCheck,
)

test_data_prefix = "tests/lib/checks/beman_standard/cmake/data"
Expand Down Expand Up @@ -165,3 +166,52 @@ def test__cmake_library_alias__fix_inplace(repo_info, beman_standard_check_confi
Note: Skipping this test as it is not implemented.
"""
pass


def test__cmake_target_names__valid(repo_info, beman_standard_check_config):
"""
Test that a valid CMakeLists.txt file passes the cmake.target_names check.
"""
valid_cmake_paths = [
# CMakeLists.txt from beman.exemplar
Path(f"{valid_prefix}/CMakeLists-v1.txt"),
]

run_check_for_each_path(
True,
valid_cmake_paths,
CMakeTargetNamesCheck,
repo_info,
beman_standard_check_config,
)


def test__cmake_target_names__invalid(repo_info, beman_standard_check_config):
"""
Test that an invalid CMakeLists.txt file fails the cmake.target_names check.
"""
invalid_cmake_paths = [
# CMakeLists.txt with invalid 'add_library' target name
Path(f"{invalid_prefix}/invalid-target_names-v1.txt"),
# CMakeLists.txt with invalid 'add_executable' target name
Path(f"{invalid_prefix}/invalid-target_names-v2.txt"),
# CMakeLists.txt with multiple invalid target names
Path(f"{invalid_prefix}/invalid-target_names-v3.txt"),
]

run_check_for_each_path(
False,
invalid_cmake_paths,
CMakeTargetNamesCheck,
repo_info,
beman_standard_check_config,
)


@pytest.mark.skip(reason="not implemented")
def test__cmake_target_names__fix_inplace(repo_info, beman_standard_check_config):
"""
Test that the fix method corrects an invalid CMakeLists.txt file.
Note: Skipping this test as it is not implemented.
"""
pass
Loading