Skip to content

Commit b6c84e9

Browse files
committed
Implement support for CMake
1 parent 84c6c1b commit b6c84e9

File tree

16 files changed

+734
-21
lines changed

16 files changed

+734
-21
lines changed

.github/workflows/build.yml

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ name: CI
33
on: [push, pull_request]
44

55
env:
6-
BUILD_DIR: _build
6+
M_BUILD_DIR: _build_meson
7+
C_BUILD_DIR: _build_cmake
78
PIP_PACKAGES: >-
89
meson==0.55.3
910
ninja
11+
cmake
1012
gcovr
1113
PIP_EXTRAS: >-
1214
pkgconfig
@@ -60,7 +62,7 @@ jobs:
6062

6163
- name: Configure build
6264
run: >-
63-
meson setup ${{ env.BUILD_DIR }}
65+
meson setup ${{ env.M_BUILD_DIR }}
6466
--buildtype=debug
6567
--prefix=$PWD/_dist
6668
--libdir=lib
@@ -70,16 +72,33 @@ jobs:
7072
-Dpython=true
7173
7274
- name: Build library
73-
run: meson compile -C ${{ env.BUILD_DIR }}
75+
run: meson compile -C ${{ env.M_BUILD_DIR }}
7476

7577
- name: Run unit tests
7678
run: |
77-
meson test -C ${{ env.BUILD_DIR }} --print-errorlogs --no-rebuild --num-processes 2 -t 2
78-
ninja -C ${{ env.BUILD_DIR }} coverage
79+
meson test -C ${{ env.M_BUILD_DIR }} --print-errorlogs --no-rebuild --num-processes 2 -t 2
80+
ninja -C ${{ env.M_BUILD_DIR }} coverage
81+
82+
- name: Configure CMake build
83+
run: >-
84+
cmake
85+
-B ${{ env.C_BUILD_DIR }}
86+
-G Ninja
87+
-DCMAKE_INSTALL_PREFIX=$PWD/_cdist
88+
89+
- name: Build project (CMake)
90+
run: cmake --build ${{ env.C_BUILD_DIR }}
91+
92+
- name: Run unit tests (CTest)
93+
run: ctest
94+
working-directory: ${{ env.C_BUILD_DIR }}
95+
96+
- name: Install project (CMake)
97+
run: cmake --install ${{ env.C_BUILD_DIR }}
7998

8099
- name: Install project
81100
run: |
82-
meson install -C ${{ env.BUILD_DIR }} --no-rebuild
101+
meson install -C ${{ env.M_BUILD_DIR }} --no-rebuild
83102
echo "DFTD3_PREFIX=$PWD/_dist" >> $GITHUB_ENV
84103
85104
- name: Create package
@@ -107,7 +126,7 @@ jobs:
107126

108127
- name: Configure out-of-tree build
109128
run: >-
110-
meson setup ${{ env.BUILD_DIR }}
129+
meson setup ${{ env.M_BUILD_DIR }}
111130
--prefix=$PWD/_dist
112131
--libdir=lib
113132
--warnlevel=0
@@ -116,11 +135,11 @@ jobs:
116135
PKG_CONFIG_PATH: ${{ env.PKG_CONFIG_PATH }}:${{ env.DFTD3_PREFIX }}/lib/pkgconfig
117136

118137
- name: Build Python extension module
119-
run: meson compile -C ${{ env.BUILD_DIR }}
138+
run: meson compile -C ${{ env.M_BUILD_DIR }}
120139
working-directory: python
121140

122141
- name: Install Python extension module (meson)
123-
run: meson install -C ${{ env.BUILD_DIR }} --no-rebuild
142+
run: meson install -C ${{ env.M_BUILD_DIR }} --no-rebuild
124143
working-directory: python
125144

126145
python-build:
@@ -219,22 +238,35 @@ jobs:
219238
mingw-w64-${{ matrix.arch }}-gcc-fortran
220239
mingw-w64-${{ matrix.arch }}-python
221240
mingw-w64-${{ matrix.arch }}-python-pip
241+
mingw-w64-${{ matrix.arch }}-cmake
222242
mingw-w64-${{ matrix.arch }}-ninja
223243
224244
- name: Install meson
225245
run: pip3 install meson==0.55.3
226246

227247
- name: Configure build
228-
run: meson setup ${{ env.BUILD_DIR }} --warnlevel=0
248+
run: meson setup ${{ env.M_BUILD_DIR }} --warnlevel=0
229249
env:
230250
FC: gfortran
231251
CC: gcc
232252

233253
- name: Build project
234-
run: meson compile -C ${{ env.BUILD_DIR }}
254+
run: meson compile -C ${{ env.M_BUILD_DIR }}
235255

236256
- name: Run unit tests
237-
run: meson test -C ${{ env.BUILD_DIR }} --print-errorlogs --no-rebuild
257+
run: meson test -C ${{ env.M_BUILD_DIR }} --print-errorlogs --no-rebuild
258+
env:
259+
OMP_NUM_THREADS: 2,1
260+
261+
- name: Configure cmake build
262+
run: cmake -B ${{ env.C_BUILD_DIR }} -G Ninja -DWITH_BLAS=FALSE
263+
264+
- name: Build project (CMake)
265+
run: cmake --build ${{ env.C_BUILD_DIR }}
266+
267+
- name: Run unit tests (CTest)
268+
run: ctest
269+
working-directory: ${{ env.C_BUILD_DIR }}
238270
env:
239271
OMP_NUM_THREADS: 2,1
240272

@@ -276,10 +308,20 @@ jobs:
276308
run: pip3 install meson ninja
277309

278310
- name: Configure meson build
279-
run: meson setup ${{ env.BUILD_DIR }}
311+
run: meson setup ${{ env.M_BUILD_DIR }}
280312

281313
- name: Build library
282-
run: meson compile -C ${{ env.BUILD_DIR }}
314+
run: meson compile -C ${{ env.M_BUILD_DIR }}
283315

284316
- name: Run unit tests
285-
run: meson test -C ${{ env.BUILD_DIR }} --print-errorlogs --no-rebuild
317+
run: meson test -C ${{ env.M_BUILD_DIR }} --print-errorlogs --no-rebuild
318+
319+
- name: Configure cmake build
320+
run: cmake -B ${{ env.C_BUILD_DIR }} -G Ninja
321+
322+
- name: Build library (CMake)
323+
run: cmake --build ${{ env.C_BUILD_DIR }}
324+
325+
- name: Run unit tests (CTest)
326+
run: ctest
327+
working-directory: ${{ env.C_BUILD_DIR }}

CMakeLists.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# This file is part of s-dftd3.
2+
# SPDX-Identifier: LGPL-3.0-or-later
3+
#
4+
# s-dftd3 is free software: you can redistribute it and/or modify it under
5+
# the terms of the GNU Lesser General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# s-dftd3 is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU Lesser General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU Lesser General Public License
15+
# along with s-dftd3. If not, see <https://www.gnu.org/licenses/>.
16+
17+
cmake_minimum_required(VERSION 3.14)
18+
19+
project(
20+
"s-dftd3"
21+
LANGUAGES "Fortran"
22+
VERSION "0.4.0"
23+
DESCRIPTION "Simple reimplementation of the DFT-D3 dispersion model"
24+
)
25+
26+
# Follow GNU conventions for installing directories
27+
include(GNUInstallDirs)
28+
29+
# Collect subprojects
30+
set(lib-deps)
31+
add_subdirectory("subprojects" EXCLUDE_FROM_ALL)
32+
33+
# General configuration information
34+
add_subdirectory("config")
35+
36+
if(NOT TARGET "OpenMP::OpenMP_Fortran" AND WITH_OpenMP)
37+
find_package("OpenMP" REQUIRED)
38+
endif()
39+
40+
if(NOT TARGET "BLAS::BLAS" AND WITH_BLAS)
41+
find_package("BLAS" REQUIRED)
42+
endif()
43+
44+
# Collect source of the project
45+
set(srcs)
46+
add_subdirectory("src")
47+
48+
# multicharge library target
49+
add_library(
50+
"${PROJECT_NAME}-lib"
51+
"${srcs}"
52+
)
53+
set_target_properties(
54+
"${PROJECT_NAME}-lib"
55+
PROPERTIES
56+
POSITION_INDEPENDENT_CODE TRUE
57+
OUTPUT_NAME "${PROJECT_NAME}"
58+
VERSION "${PROJECT_VERSION}"
59+
SOVERSION "${PROJECT_VERSION_MAJOR}"
60+
Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include"
61+
)
62+
target_link_libraries(
63+
"${PROJECT_NAME}-lib"
64+
PUBLIC
65+
"${lib-deps}"
66+
)
67+
if(WITH_OpenMP)
68+
target_link_libraries(
69+
"${PROJECT_NAME}-lib"
70+
PUBLIC
71+
"OpenMP::OpenMP_Fortran"
72+
)
73+
endif()
74+
if(WITH_BLAS)
75+
target_link_libraries(
76+
"${PROJECT_NAME}-lib"
77+
PUBLIC
78+
"BLAS::BLAS"
79+
)
80+
target_compile_definitions(
81+
"${PROJECT_NAME}-lib"
82+
PRIVATE
83+
"WITH_BLAS=1"
84+
)
85+
endif()
86+
target_include_directories(
87+
"${PROJECT_NAME}-lib"
88+
PUBLIC
89+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
90+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${module-dir}>
91+
)
92+
93+
# Add example application
94+
add_subdirectory("app")
95+
96+
# Export targets for other projects
97+
add_library("${PROJECT_NAME}" INTERFACE)
98+
target_link_libraries("${PROJECT_NAME}" INTERFACE "${PROJECT_NAME}-lib")
99+
install(
100+
TARGETS
101+
"${PROJECT_NAME}"
102+
"${PROJECT_NAME}-lib"
103+
EXPORT
104+
"${PROJECT_NAME}-targets"
105+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
106+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
107+
)
108+
install(
109+
EXPORT
110+
"${PROJECT_NAME}-targets"
111+
NAMESPACE
112+
"${PROJECT_NAME}::"
113+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
114+
)
115+
install(
116+
DIRECTORY
117+
"${CMAKE_CURRENT_BINARY_DIR}/include/"
118+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}"
119+
)
120+
# Package license files
121+
install(
122+
FILES
123+
"COPYING"
124+
"COPYING.LESSER"
125+
DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME}"
126+
)
127+
128+
# add the testsuite
129+
enable_testing()
130+
add_subdirectory("test")

0 commit comments

Comments
 (0)