Skip to content

Commit 58a4434

Browse files
Merge pull request PowerGridModel#1196 from PowerGridModel/feature/cmake-linter-precommit
CI: add cmake linter
2 parents 4222404 + 1c19c9e commit 58a4434

File tree

17 files changed

+259
-176
lines changed

17 files changed

+259
-176
lines changed

.gersemirc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
#
3+
# SPDX-License-Identifier: MPL-2.0
4+
5+
definitions: [./cmake/stubs/]
6+
disable_formatting: false
7+
extensions: []
8+
indent: 4
9+
line_length: 80
10+
list_expansion: favour-inlining
11+
unsafe: false
12+
warn_about_unknown_commands: true

.github/workflows/check-code-quality.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ jobs:
5252
sudo apt-get install -y clang-format-18
5353
find . -regex '.*\.\(h\|c\|cpp\|hpp\|cc\|cxx\)' -exec clang-format-18 -style=file -i {} \;
5454
55+
- name: Install and run cmake-linter
56+
run: |
57+
pip install gersemi
58+
gersemi -i --warnings-as-errors src/ tests/ cmake/
59+
5560
- name: Install and run markdownlint
5661
run: |
5762
npm install -g markdownlint-cli
@@ -95,4 +100,3 @@ jobs:
95100
else
96101
echo "The test-workflow.yml file matches main."
97102
fi
98-

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ repos:
3737
hooks:
3838
- id: clang-format
3939
types_or: [ c++, c ]
40+
- repo: https://github.com/BlankSpruce/gersemi
41+
rev: 0.23.1
42+
hooks:
43+
- id: gersemi
44+
name: cmake-linter
45+
alias: cmake-linter
4046
- repo: https://github.com/igorshubovych/markdownlint-cli
4147
rev: v0.45.0
4248
hooks:

CMakeLists.txt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# CMakeList.txt : CMake project for power_grid_model, include source and define
66
# project specific logic here.
77
#
8-
cmake_minimum_required (VERSION 3.23)
8+
cmake_minimum_required(VERSION 3.23)
99

1010
include("cmake/pgm_version.cmake")
1111

12-
project (power_grid_model VERSION ${PGM_VERSION})
12+
project(power_grid_model VERSION ${PGM_VERSION})
1313

1414
option(PGM_ENABLE_DEV_BUILD "Enable developer build (e.g.: tests)" OFF)
1515

@@ -29,9 +29,9 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
2929

3030
include(GNUInstallDirs)
3131

32-
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.30.0")
33-
# libboost-headers packaged by conda does not come with BoostConfig.cmake
34-
cmake_policy(SET CMP0167 OLD)
32+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.30.0")
33+
# libboost-headers packaged by conda does not come with BoostConfig.cmake
34+
cmake_policy(SET CMP0167 OLD)
3535
endif()
3636

3737
find_package(Boost REQUIRED)
@@ -40,24 +40,22 @@ find_package(nlohmann_json CONFIG REQUIRED)
4040
find_package(msgpack-cxx REQUIRED)
4141

4242
if(NOT WIN32)
43-
# thread
44-
find_package(Threads REQUIRED)
43+
# thread
44+
find_package(Threads REQUIRED)
4545
endif()
4646

4747
# add C library
4848
add_subdirectory("power_grid_model_c")
4949

5050
# dev build
5151
if(${PGM_ENABLE_DEV_BUILD})
52-
include(CTest)
53-
enable_testing()
54-
# get tests
55-
add_subdirectory("tests")
56-
# get c api example
57-
add_subdirectory("power_grid_model_c_example")
52+
include(CTest)
53+
enable_testing()
54+
# get tests
55+
add_subdirectory("tests")
56+
# get c api example
57+
add_subdirectory("power_grid_model_c_example")
5858
endif()
5959

60-
61-
6260
# export the power grid model
6361
include("cmake/export_power_grid_model.cmake")

cmake/export_power_grid_model.cmake

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,42 @@
22
#
33
# SPDX-License-Identifier: MPL-2.0
44

5-
cmake_minimum_required (VERSION 3.23)
5+
cmake_minimum_required(VERSION 3.23)
66

77
# export the license and readme
8-
configure_file("${PROJECT_SOURCE_DIR}/cmake/README.md.in" "${CMAKE_CURRENT_BINARY_DIR}/README.md")
9-
install(FILES "LICENSE" "${CMAKE_CURRENT_BINARY_DIR}/README.md"
10-
DESTINATION "share"
8+
configure_file(
9+
"${PROJECT_SOURCE_DIR}/cmake/README.md.in"
10+
"${CMAKE_CURRENT_BINARY_DIR}/README.md"
11+
)
12+
install(
13+
FILES "LICENSE" "${CMAKE_CURRENT_BINARY_DIR}/README.md"
14+
DESTINATION "share"
1115
)
1216

1317
# export the power grid model targets
14-
install(EXPORT "power_grid_modelTargets"
15-
DESTINATION "lib/cmake/power_grid_model"
16-
NAMESPACE power_grid_model::
17-
COMPONENT power_grid_model
18+
install(
19+
EXPORT "power_grid_modelTargets"
20+
DESTINATION "lib/cmake/power_grid_model"
21+
NAMESPACE power_grid_model::
22+
COMPONENT power_grid_model
1823
)
1924

2025
# export configuration and version to enable find_package(power_grid_model)
2126
include(CMakePackageConfigHelpers)
2227

23-
configure_package_config_file("${PROJECT_SOURCE_DIR}/cmake/power_grid_modelConfig.cmake.in"
24-
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfig.cmake"
25-
INSTALL_DESTINATION "lib/cmake/power_grid_model"
28+
configure_package_config_file(
29+
"${PROJECT_SOURCE_DIR}/cmake/power_grid_modelConfig.cmake.in"
30+
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfig.cmake"
31+
INSTALL_DESTINATION "lib/cmake/power_grid_model"
2632
)
2733
write_basic_package_version_file(
28-
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfigVersion.cmake"
29-
VERSION ${PGM_VERSION}
30-
COMPATIBILITY SameMajorVersion
34+
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfigVersion.cmake"
35+
VERSION ${PGM_VERSION}
36+
COMPATIBILITY SameMajorVersion
37+
)
38+
install(
39+
FILES
40+
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfig.cmake"
41+
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfigVersion.cmake"
42+
DESTINATION "lib/cmake/power_grid_model"
3143
)
32-
install(FILES
33-
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfig.cmake"
34-
"${CMAKE_CURRENT_BINARY_DIR}/power_grid_model/power_grid_modelConfigVersion.cmake"
35-
DESTINATION "lib/cmake/power_grid_model"
36-
)

cmake/pgm_version.cmake

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
#
33
# SPDX-License-Identifier: MPL-2.0
44

5-
cmake_minimum_required (VERSION 3.23)
5+
cmake_minimum_required(VERSION 3.23)
66

77
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" _PGM_VERSION)
88
string(STRIP ${_PGM_VERSION} _PGM_VERSION_STRIPPED)
9-
string(REGEX REPLACE "^([0-9]+\\.[0-9]+(\\.[0-9]+)?).*" "\\1" PGM_VERSION "${_PGM_VERSION_STRIPPED}")
9+
string(
10+
REGEX REPLACE
11+
"^([0-9]+\\.[0-9]+(\\.[0-9]+)?).*"
12+
"\\1"
13+
PGM_VERSION
14+
"${_PGM_VERSION_STRIPPED}"
15+
)

cmake/stubs/doctest.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
#
3+
# SPDX-License-Identifier: MPL-2.0
4+
5+
cmake_minimum_required(VERSION 3.23)
6+
7+
# stub for doctest_discover_tests
8+
function(doctest_discover_tests target)
9+
# no-op
10+
endfunction()

power_grid_model_c/power_grid_model/CMakeLists.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44

55
add_library(power_grid_model INTERFACE)
66

7-
target_link_libraries(power_grid_model INTERFACE
8-
Eigen3::Eigen Boost::headers msgpack-cxx nlohmann_json::nlohmann_json
7+
target_link_libraries(
8+
power_grid_model
9+
INTERFACE
10+
Eigen3::Eigen
11+
Boost::headers
12+
msgpack-cxx
13+
nlohmann_json::nlohmann_json
914
)
1015

1116
if(Threads_FOUND)
12-
target_link_libraries(power_grid_model INTERFACE Threads::Threads)
17+
target_link_libraries(power_grid_model INTERFACE Threads::Threads)
1318
endif()
1419

15-
target_include_directories(power_grid_model INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
20+
target_include_directories(
21+
power_grid_model
22+
INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include"
23+
)
1624

1725
# only use MPL version of eigen
1826
target_compile_definitions(power_grid_model INTERFACE "EIGEN_MPL2_ONLY=1")
1927

20-
target_compile_options(power_grid_model BEFORE INTERFACE
21-
"$<$<CXX_COMPILER_ID:Clang>:-Wno-unknown-attributes>"
22-
"$<$<CXX_COMPILER_ID:MSVC>:/bigobj>"
28+
target_compile_options(
29+
power_grid_model
30+
BEFORE
31+
INTERFACE
32+
"$<$<CXX_COMPILER_ID:Clang>:-Wno-unknown-attributes>"
33+
"$<$<CXX_COMPILER_ID:MSVC>:/bigobj>"
2334
)

power_grid_model_c/power_grid_model_c/CMakeLists.txt

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,55 @@
33
# SPDX-License-Identifier: MPL-2.0
44

55
# C API library
6-
add_library(power_grid_model_c SHARED
7-
"src/buffer.cpp"
8-
"src/handle.cpp"
9-
"src/meta_data.cpp"
10-
"src/model.cpp"
11-
"src/options.cpp"
12-
"src/dataset_definitions.cpp"
13-
"src/serialization.cpp"
14-
"src/dataset.cpp"
15-
"src/math_solver.cpp"
6+
add_library(
7+
power_grid_model_c
8+
SHARED
9+
"src/buffer.cpp"
10+
"src/handle.cpp"
11+
"src/meta_data.cpp"
12+
"src/model.cpp"
13+
"src/options.cpp"
14+
"src/dataset_definitions.cpp"
15+
"src/serialization.cpp"
16+
"src/dataset.cpp"
17+
"src/math_solver.cpp"
1618
)
1719

18-
target_include_directories(power_grid_model_c PUBLIC
19-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
20-
$<INSTALL_INTERFACE:include>
20+
target_include_directories(
21+
power_grid_model_c
22+
PUBLIC
23+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
24+
$<INSTALL_INTERFACE:include>
2125
)
2226

2327
file(
24-
GLOB_RECURSE
25-
pgm_c_public_headers
26-
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h"
28+
GLOB_RECURSE pgm_c_public_headers
29+
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h"
2730
)
2831

29-
target_link_libraries(power_grid_model_c
30-
PRIVATE power_grid_model
31-
)
32+
target_link_libraries(power_grid_model_c PRIVATE power_grid_model)
3233

33-
target_sources(power_grid_model_c PUBLIC
34-
FILE_SET pgm_c_public_headers
35-
TYPE HEADERS
36-
BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include/"
37-
FILES "${pgm_c_public_headers}"
38-
)
34+
target_sources(
35+
power_grid_model_c
36+
PUBLIC
37+
FILE_SET pgm_c_public_headers
38+
TYPE HEADERS
39+
BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include/"
40+
FILES "${pgm_c_public_headers}"
41+
)
3942

40-
set_target_properties(power_grid_model_c PROPERTIES
41-
VERSION ${PGM_VERSION}
42-
SOVERSION ${PGM_VERSION}
43-
INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
44-
INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE
43+
set_target_properties(
44+
power_grid_model_c
45+
PROPERTIES
46+
VERSION ${PGM_VERSION}
47+
SOVERSION ${PGM_VERSION}
48+
INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
49+
INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE
4550
)
4651

47-
install(TARGETS power_grid_model_c
48-
EXPORT power_grid_modelTargets
49-
COMPONENT power_grid_model
50-
FILE_SET pgm_c_public_headers
52+
install(
53+
TARGETS power_grid_model_c
54+
EXPORT power_grid_modelTargets
55+
COMPONENT power_grid_model
56+
FILE_SET pgm_c_public_headers
5157
)

power_grid_model_c/power_grid_model_cpp/CMakeLists.txt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
# C++ wrapper library around C API
66
add_library(power_grid_model_cpp INTERFACE)
77

8-
target_include_directories(power_grid_model_cpp INTERFACE
9-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
10-
$<INSTALL_INTERFACE:include>
8+
target_include_directories(
9+
power_grid_model_cpp
10+
INTERFACE
11+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
12+
$<INSTALL_INTERFACE:include>
1113
)
1214

1315
file(
14-
GLOB_RECURSE
15-
pgm_cpp_public_headers
16-
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp"
16+
GLOB_RECURSE pgm_cpp_public_headers
17+
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp"
1718
)
1819

19-
target_link_libraries(power_grid_model_cpp
20-
INTERFACE power_grid_model_c
21-
)
20+
target_link_libraries(power_grid_model_cpp INTERFACE power_grid_model_c)
2221

23-
target_sources(power_grid_model_cpp PUBLIC
24-
FILE_SET pgm_cpp_public_headers
25-
TYPE HEADERS
26-
BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include/"
27-
FILES "${pgm_cpp_public_headers}"
22+
target_sources(
23+
power_grid_model_cpp
24+
PUBLIC
25+
FILE_SET pgm_cpp_public_headers
26+
TYPE HEADERS
27+
BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include/"
28+
FILES "${pgm_cpp_public_headers}"
2829
)
2930

30-
set_target_properties(power_grid_model_cpp PROPERTIES
31-
VERSION ${PGM_VERSION}
32-
SOVERSION ${PGM_VERSION}
33-
CXX_STANDARD 17
31+
set_target_properties(
32+
power_grid_model_cpp
33+
PROPERTIES VERSION ${PGM_VERSION} SOVERSION ${PGM_VERSION} CXX_STANDARD 17
3434
)
3535

36-
37-
install(TARGETS power_grid_model_cpp
38-
EXPORT power_grid_modelTargets
39-
COMPONENT power_grid_model
40-
FILE_SET pgm_cpp_public_headers
36+
install(
37+
TARGETS power_grid_model_cpp
38+
EXPORT power_grid_modelTargets
39+
COMPONENT power_grid_model
40+
FILE_SET pgm_cpp_public_headers
4141
)

0 commit comments

Comments
 (0)