From 708957f434f4addd690ac987e352fef49ae077d9 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Fri, 24 Jan 2025 03:19:23 -0500 Subject: [PATCH 1/7] Add the CMake linter gersemi. --- .gersemirc | 5 +++++ CMakeLists.txt | 12 ++++++++++++ lint-requirements.txt | 2 ++ taskfiles/lint-cmake.yaml | 35 +++++++++++++++++++++++++++++++++++ taskfiles/lint.yaml | 6 ++++++ 5 files changed, 60 insertions(+) create mode 100644 .gersemirc create mode 100644 CMakeLists.txt create mode 100644 taskfiles/lint-cmake.yaml diff --git a/.gersemirc b/.gersemirc new file mode 100644 index 00000000..c6d6ef6c --- /dev/null +++ b/.gersemirc @@ -0,0 +1,5 @@ +# yamllint disable-line rule:line-length +# yaml-language-server: $schema=https://raw.githubusercontent.com/BlankSpruce/gersemi/master/gersemi/configuration.schema.json + +line_length: 100 +list_expansion: "favour-expansion" diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..9b2b190b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.16.3) +project(YSTDLIB_CPP LANGUAGES CXX) + +set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.") + +# When ystdlib-cpp is included as subproject (i.e. using add_subdirectory(ystdlib-cpp)) in the +# source tree of a project that uses it, installation rules are disabled. +if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" OFF) +else() + option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" ON) +endif() diff --git a/lint-requirements.txt b/lint-requirements.txt index 993e8b4f..425a18eb 100644 --- a/lint-requirements.txt +++ b/lint-requirements.txt @@ -1 +1,3 @@ +colorama>=0.4.6 +gersemi>=0.16.2 yamllint>=1.35.1 diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml new file mode 100644 index 00000000..9ae24a28 --- /dev/null +++ b/taskfiles/lint-cmake.yaml @@ -0,0 +1,35 @@ +version: "3" + +tasks: + cmake-check: + desc: "Runs the CMake linters." + deps: + - "venv" + cmds: + - task: "cmake" + vars: + FLAGS: "--diff --color" + + cmake-fix: + desc: "Runs the CMake linters and fixes all violations." + deps: + - "venv" + cmds: + - task: "cmake" + vars: + FLAGS: "--in-place" + + cmake: + internal: true + requires: + vars: + - "FLAGS" + dir: "{{.ROOT_DIR}}" + cmds: + - |- + . "{{.G_LINT_VENV_DIR}}/bin/activate" + find . \ + -path ./build -prune \ + -o -name CMakeLists.txt \ + -print0 | \ + xargs -0 --no-run-if-empty gersemi {{.FLAGS}} diff --git a/taskfiles/lint.yaml b/taskfiles/lint.yaml index 3d1833cb..eef45a19 100644 --- a/taskfiles/lint.yaml +++ b/taskfiles/lint.yaml @@ -1,6 +1,9 @@ version: "3" includes: + cmake: + flatten: true + taskfile: "./lint-cmake.yaml" yaml: flatten: true taskfile: "./lint-yaml.yaml" @@ -13,11 +16,13 @@ tasks: check: desc: "Runs the full suite of linters to identify warnings and violations." cmds: + - task: "cmake-check" - task: "yaml-check" fix: desc: "Runs the full suite of linters and fixes some violations." cmds: + - task: "cmake-fix" - task: "yaml-fix" venv: @@ -31,6 +36,7 @@ tasks: - "lint-requirements.txt" generates: - "{{.CHECKSUM_FILE}}" + run: "once" deps: - ":init" - task: ":utils:validate-checksum" From 0ab9fb429a52e54897998b43984c91e95daf856e Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Fri, 24 Jan 2025 07:15:55 -0500 Subject: [PATCH 2/7] Add more CMake filename patterns for linting. --- CMake/ystdlib-cpp-config.cmake.in | 7 +++++++ CMake/ystdlib-cpp-helpers.cmake | 6 ++++++ taskfiles/lint-cmake.yaml | 13 +++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 CMake/ystdlib-cpp-config.cmake.in create mode 100644 CMake/ystdlib-cpp-helpers.cmake diff --git a/CMake/ystdlib-cpp-config.cmake.in b/CMake/ystdlib-cpp-config.cmake.in new file mode 100644 index 00000000..45a31a59 --- /dev/null +++ b/CMake/ystdlib-cpp-config.cmake.in @@ -0,0 +1,7 @@ +# ystdlib-cpp CMake configuration file + +# gersemi: off +@PACKAGE_INIT@ +# gersemi: on + +include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_EXPORT_NAME@.cmake") diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake new file mode 100644 index 00000000..6aa51d2d --- /dev/null +++ b/CMake/ystdlib-cpp-helpers.cmake @@ -0,0 +1,6 @@ +# ystdlib_cpp_library() +# +# CMake function to imitate Bazel's cc_library rule. +# TODO: implement this function +function(ystdlib_cpp_library) +endfunction() diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index 9ae24a28..25de36e5 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -19,6 +19,9 @@ tasks: vars: FLAGS: "--in-place" + # Lint all CMake files, excluding those located in build directories. + # + # @param {string} FLAGS Options passed into the CMake linter gersemi. cmake: internal: true requires: @@ -29,7 +32,9 @@ tasks: - |- . "{{.G_LINT_VENV_DIR}}/bin/activate" find . \ - -path ./build -prune \ - -o -name CMakeLists.txt \ - -print0 | \ - xargs -0 --no-run-if-empty gersemi {{.FLAGS}} + -path ./build -prune -o \( \ + -name "CMakeLists.txt" -o \ + -name "*.cmake" -o \ + -name "*.cmake.in" \ + \) -print0 | \ + xargs -0 --no-run-if-empty gersemi {{.FLAGS}} From 0ebac1d02285668d3fe9ad881dfd110ce680524a Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sat, 25 Jan 2025 14:28:09 -0500 Subject: [PATCH 3/7] Add sources section in cmake lint tasks to prevent tasks from running multiple times unnecessarily --- taskfiles/lint-cmake.yaml | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index 25de36e5..b353098d 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -3,15 +3,26 @@ version: "3" tasks: cmake-check: desc: "Runs the CMake linters." + sources: &cmake_format_src_files + - "{{.ROOT_DIR}}/**/*.cmake" + - "{{.ROOT_DIR}}/**/*.cmake.in" + - "{{.ROOT_DIR}}/**/CMakeLists.txt" + - "{{.TASKFILE}}" + - exclude: "{{.ROOT_DIR}}/**/build/*" deps: - "venv" cmds: - task: "cmake" vars: FLAGS: "--diff --color" + - task: "cmake" + silent: true + vars: + FLAGS: "--check" cmake-fix: desc: "Runs the CMake linters and fixes all violations." + sources: *cmake_format_src_files deps: - "venv" cmds: @@ -19,9 +30,6 @@ tasks: vars: FLAGS: "--in-place" - # Lint all CMake files, excluding those located in build directories. - # - # @param {string} FLAGS Options passed into the CMake linter gersemi. cmake: internal: true requires: @@ -32,9 +40,7 @@ tasks: - |- . "{{.G_LINT_VENV_DIR}}/bin/activate" find . \ - -path ./build -prune -o \( \ - -name "CMakeLists.txt" -o \ - -name "*.cmake" -o \ - -name "*.cmake.in" \ - \) -print0 | \ - xargs -0 --no-run-if-empty gersemi {{.FLAGS}} + -path ./build -prune -o \ + \( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \ + -print0 | \ + xargs -0 --no-run-if-empty gersemi {{.FLAGS}} From f423c9c596c68b625fb516eff420c4cee2636e07 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sat, 25 Jan 2025 15:22:13 -0500 Subject: [PATCH 4/7] Add more paths to exclude --- taskfiles/lint-cmake.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index b353098d..2f6d3122 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -9,6 +9,8 @@ tasks: - "{{.ROOT_DIR}}/**/CMakeLists.txt" - "{{.TASKFILE}}" - exclude: "{{.ROOT_DIR}}/**/build/*" + - exclude: "{{.ROOT_DIR}}/**/submodules/*" + - exclude: "{{.ROOT_DIR}}/**/tools/*" deps: - "venv" cmds: @@ -40,7 +42,7 @@ tasks: - |- . "{{.G_LINT_VENV_DIR}}/bin/activate" find . \ - -path ./build -prune -o \ + \( -path '**/build' -o -path '**/submodules' -o -path '**/tools' \) -prune -o \ \( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \ -print0 | \ xargs -0 --no-run-if-empty gersemi {{.FLAGS}} From d96c61ea60af1b4b87ba5c0e04ef7041b0f3d38f Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sun, 26 Jan 2025 20:03:03 -0500 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: davidlion --- taskfiles/lint-cmake.yaml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index 2f6d3122..48cc4afb 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -11,13 +11,11 @@ tasks: - exclude: "{{.ROOT_DIR}}/**/build/*" - exclude: "{{.ROOT_DIR}}/**/submodules/*" - exclude: "{{.ROOT_DIR}}/**/tools/*" - deps: - - "venv" cmds: - - task: "cmake" + - task: "gersemi" vars: FLAGS: "--diff --color" - - task: "cmake" + - task: "gersemi" silent: true vars: FLAGS: "--check" @@ -28,11 +26,11 @@ tasks: deps: - "venv" cmds: - - task: "cmake" + - task: "gersemi" vars: FLAGS: "--in-place" - cmake: + gersemi: internal: true requires: vars: From 13018603e1ae5fc912880c9aa554246b09368ed1 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sun, 26 Jan 2025 20:10:21 -0500 Subject: [PATCH 6/7] Move deps venv to internal task --- taskfiles/lint-cmake.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/taskfiles/lint-cmake.yaml b/taskfiles/lint-cmake.yaml index 48cc4afb..87af4ba8 100644 --- a/taskfiles/lint-cmake.yaml +++ b/taskfiles/lint-cmake.yaml @@ -23,8 +23,6 @@ tasks: cmake-fix: desc: "Runs the CMake linters and fixes all violations." sources: *cmake_format_src_files - deps: - - "venv" cmds: - task: "gersemi" vars: @@ -36,6 +34,8 @@ tasks: vars: - "FLAGS" dir: "{{.ROOT_DIR}}" + deps: + - "venv" cmds: - |- . "{{.G_LINT_VENV_DIR}}/bin/activate" From a3c7ebbab0507486ff2a7ff4943d9609b74ddf3d Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Sun, 26 Jan 2025 20:11:10 -0500 Subject: [PATCH 7/7] Reduce Cmake project to a bare minium CMakeLists.txt --- CMake/ystdlib-cpp-config.cmake.in | 7 ------- CMake/ystdlib-cpp-helpers.cmake | 6 ------ CMakeLists.txt | 8 -------- 3 files changed, 21 deletions(-) delete mode 100644 CMake/ystdlib-cpp-config.cmake.in delete mode 100644 CMake/ystdlib-cpp-helpers.cmake diff --git a/CMake/ystdlib-cpp-config.cmake.in b/CMake/ystdlib-cpp-config.cmake.in deleted file mode 100644 index 45a31a59..00000000 --- a/CMake/ystdlib-cpp-config.cmake.in +++ /dev/null @@ -1,7 +0,0 @@ -# ystdlib-cpp CMake configuration file - -# gersemi: off -@PACKAGE_INIT@ -# gersemi: on - -include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_EXPORT_NAME@.cmake") diff --git a/CMake/ystdlib-cpp-helpers.cmake b/CMake/ystdlib-cpp-helpers.cmake deleted file mode 100644 index 6aa51d2d..00000000 --- a/CMake/ystdlib-cpp-helpers.cmake +++ /dev/null @@ -1,6 +0,0 @@ -# ystdlib_cpp_library() -# -# CMake function to imitate Bazel's cc_library rule. -# TODO: implement this function -function(ystdlib_cpp_library) -endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b2b190b..5ba1b194 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,11 +2,3 @@ cmake_minimum_required(VERSION 3.16.3) project(YSTDLIB_CPP LANGUAGES CXX) set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.") - -# When ystdlib-cpp is included as subproject (i.e. using add_subdirectory(ystdlib-cpp)) in the -# source tree of a project that uses it, installation rules are disabled. -if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) - option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" OFF) -else() - option(YSTDLIB_CPP_ENABLE_INSTALL "Enable installation rules" ON) -endif()