Skip to content
Draft
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
113 changes: 112 additions & 1 deletion beman_tidy/lib/checks/beman_standard/cmake.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

import re
from abc import ABC
from collections.abc import Iterable

Expand All @@ -13,7 +14,9 @@
)

from ..system.registry import register_beman_standard_check
from ..base.base_check import BaseCheck
from ..base.file_base_check import FileBaseCheck
from ...utils.string import normalize_path_for_display

# [cmake.*] checks category.
# All checks in this file extend the CMakeBaseCheck class.
Expand Down Expand Up @@ -212,7 +215,115 @@ def fix(self):
return False


# TODO cmake.use_find_package
_USE_FIND_PACKAGE_DOC = (
"https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md"
"#cmakeuse_find_package"
)


# Known exception from repository.disallow_git_submodules: the mpark/wg21
# paper framework is not a C++ dependency, so it is not fetched via CMake.
_ALLOWED_SUBMODULE_URL = re.compile(r"^https://github\.com/mpark/wg21(?:\.git)?/?$")
_SUBMODULE_URL = re.compile(r"^\s*url\s*=\s*(\S+)\s*$", re.MULTILINE)


def _has_fetch_content_declare(content):
ast = cmake_parser.parser.parse_raw(content, skip_comments=True)
return any(
hasattr(item, "identifier")
and item.identifier.lower() == "fetchcontent_declare"
for item in ast
)


@register_beman_standard_check("cmake.use_find_package")
class CMakeUseFindPackageCheck(BaseCheck):
def __init__(self, repo_info, beman_standard_check_config):
super().__init__(repo_info, beman_standard_check_config)

def _has_disallowed_submodules(self):
gitmodules_path = self.repo_path / ".gitmodules"
if not gitmodules_path.is_file():
return False
try:
content = gitmodules_path.read_text(encoding="utf-8", errors="ignore")
except OSError:
self.log(
"Unable to read .gitmodules. "
f"See {_USE_FIND_PACKAGE_DOC} for more information."
)
return True
urls = _SUBMODULE_URL.findall(content)
if not urls:
# Non-empty .gitmodules without a parsable url: treat it as a submodule.
return len(content.strip()) > 0
return any(not _ALLOWED_SUBMODULE_URL.match(url) for url in urls)

def _cmake_files_to_check(self):
cmake_files = []

root_cmake = self.repo_path / "CMakeLists.txt"
if root_cmake.is_file():
cmake_files.append(root_cmake)

for cmake_file in self.repo_path.glob("*.cmake"):
cmake_files.append(cmake_file)

for subdir in ("tests", "examples"):
subdir_path = self.repo_path / subdir
if not subdir_path.is_dir():
continue
for cmake_file in subdir_path.rglob("*"):
if not cmake_file.is_file():
continue
if "infra" in cmake_file.relative_to(self.repo_path).parts:
continue
if cmake_file.name == "CMakeLists.txt" or cmake_file.suffix == ".cmake":
cmake_files.append(cmake_file)

return sorted(set(cmake_files))

def check(self):
if self._has_disallowed_submodules():
self.log(
"The repository should fetch dependencies via find_package, not git submodules. "
"Please replace the git submodules (other than mpark/wg21) with find_package. "
f"See {_USE_FIND_PACKAGE_DOC} for more information."
)
return False

for cmake_file in self._cmake_files_to_check():
try:
content = cmake_file.read_text(encoding="utf-8", errors="replace")
except OSError:
continue

display_path = normalize_path_for_display(cmake_file, self.repo_path)
try:
uses_fetch_content = _has_fetch_content_declare(content)
except cmake_parser.error.CMakeParseError:
self.log(
f"Unable to parse '{display_path}'; skipping it. "
f"See {_USE_FIND_PACKAGE_DOC} for more information."
)
continue

if uses_fetch_content:
self.log(
f"The file '{display_path}' uses FetchContent_Declare to fetch dependencies. "
"Dependencies should be fetched via find_package instead. "
f"See {_USE_FIND_PACKAGE_DOC} for more information."
)
return False

return True

def fix(self):
self.log(
"Please fetch dependencies via find_package instead of git submodules or FetchContent. "
f"See {_USE_FIND_PACKAGE_DOC} for more information."
)
return False


@register_beman_standard_check("cmake.project_name")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "dependency"]
path = deps/dependency
url = https://github.com/example/dependency.git
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(beman.exemplar LANGUAGES CXX)

add_library(beman.exemplar INTERFACE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(beman.exemplar LANGUAGES CXX)

include(FetchContent)

FetchContent_Declare(
task
GIT_REPOSITORY https://github.com/bemanproject/task
GIT_TAG main
)

add_library(beman.exemplar INTERFACE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(beman.exemplar LANGUAGES CXX)

add_library(beman.exemplar INTERFACE)
add_subdirectory(tests/beman/exemplar)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FetchContent)

FetchContent_Declare(
GTest
GIT_REPOSITORY https://github.com/google/googletest.git
)

add_executable(beman.exemplar.tests.identity)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(beman.exemplar LANGUAGES CXX)

add_library(beman.exemplar INTERFACE)
add_subdirectory(examples)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FetchContent)

FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
)

add_executable(beman.exemplar.examples.identity_direct_usage)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "papers/wg21"]
path = papers/P0000/wg21
url = https://github.com/mpark/wg21.git
[submodule "dependency"]
path = deps/dependency
url = https://github.com/example/dependency.git
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(
beman.exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 0.1.0
)

include(infra/cmake/beman-install-library.cmake)

add_library(beman.exemplar INTERFACE)
add_library(beman::exemplar ALIAS beman.exemplar)

if(BEMAN_EXEMPLAR_BUILD_TESTS)
enable_testing()
add_subdirectory(tests/beman/exemplar)
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(
beman.exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 0.1.0
)

include(infra/cmake/beman-install-library.cmake)

add_library(beman.exemplar INTERFACE)
add_library(beman::exemplar ALIAS beman.exemplar)

if(BEMAN_EXEMPLAR_BUILD_TESTS)
enable_testing()
add_subdirectory(tests/beman/exemplar)
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FetchContent)

fetchcontent_declare(
task
GIT_REPOSITORY https://github.com/bemanproject/task
GIT_TAG main
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(
beman.exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 0.1.0
)

include(infra/cmake/beman-install-library.cmake)

add_library(beman.exemplar INTERFACE)
add_library(beman::exemplar ALIAS beman.exemplar)

if(BEMAN_EXEMPLAR_BUILD_TESTS)
enable_testing()
add_subdirectory(tests/beman/exemplar)
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_executable(beman.exemplar.examples.identity_direct_usage)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

find_package(GTest REQUIRED)

add_executable(beman.exemplar.tests.identity)
target_link_libraries(
beman.exemplar.tests.identity
PRIVATE beman::exemplar GTest::gtest_main
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(beman.exemplar LANGUAGES CXX)

add_library(beman.exemplar INTERFACE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(FetchContent)

FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "papers/wg21"]
path = papers/P0000/wg21
url = https://github.com/mpark/wg21.git
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(
beman.exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 0.1.0
)

include(infra/cmake/beman-install-library.cmake)

add_library(beman.exemplar INTERFACE)
add_library(beman::exemplar ALIAS beman.exemplar)

if(BEMAN_EXEMPLAR_BUILD_TESTS)
enable_testing()
add_subdirectory(tests/beman/exemplar)
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.30...4.3)

project(
beman.exemplar
DESCRIPTION "A Beman Library Exemplar"
LANGUAGES CXX
VERSION 0.1.0
)

include(infra/cmake/beman-install-library.cmake)

add_library(beman.exemplar INTERFACE)
add_library(beman::exemplar ALIAS beman.exemplar)

if(BEMAN_EXEMPLAR_BUILD_TESTS)
enable_testing()
add_subdirectory(tests/beman/exemplar)
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Previously fetched with:
# FetchContent_Declare(
# GTest
# GIT_REPOSITORY https://github.com/google/googletest.git
# )
find_package(GTest REQUIRED)

add_executable(beman.exemplar.tests.identity)
Loading
Loading