Skip to content

Commit

Permalink
cmake: Rewrite optional dependency build system
Browse files Browse the repository at this point in the history
Generate an error when an explicitly requested optional dependency
is not found instead of silently carrying on. This is achieved by
creating a wrapper around the `option()` function to check whether
the user explicitly passed a CMake flag `-DWITH_FEATURE=ON`.
  • Loading branch information
jngrad committed May 27, 2020
1 parent 480bc34 commit 4f280f6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
50 changes: 38 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ include(GNUInstallDirs)
project(ESPResSo)
include(FindPythonModule)
include(option_enum)
include(option_if_available)
if(POLICY CMP0074)
# make find_package() use <PackageName>_ROOT variables
cmake_policy(SET CMP0074 NEW)
Expand Down Expand Up @@ -69,11 +70,11 @@ set(CMAKE_FIND_FRAMEWORK LAST)
# ##############################################################################

option(WITH_PYTHON "Build with Python bindings" ON)
option(WITH_GSL "Build with GSL support" ON)
option_if_available(WITH_GSL "Build with GSL support" ON)
option(WITH_CUDA "Build with GPU support" OFF)
option(WITH_HDF5 "Build with HDF5 support" ON)
option_if_available(WITH_HDF5 "Build with HDF5 support" ON)
option(WITH_TESTS "Enable tests" ON)
option(WITH_SCAFACOS "Build with Scafacos support" OFF)
option_if_available(WITH_SCAFACOS "Build with ScaFaCoS support" OFF)
option(WITH_BENCHMARKS "Enable benchmarks" OFF)
option(WITH_VALGRIND_INSTRUMENTATION
"Build with valgrind instrumentation markers" OFF)
Expand Down Expand Up @@ -186,17 +187,34 @@ endif()

# We need the parallel hdf5 version!
if(WITH_HDF5)
# The FindHDF5 function will fall back to the serial version if no parallel
# version was found, and print to the CMake log that HDF5 was found. There is
# no QUIET argument to override that message. This can be confusing to people
# who are not familiar with the way hdf5 is distributed in Linux package
# repositories (libhdf5-dev is the serial version).
set(HDF5_PREFER_PARALLEL 1)
find_package(HDF5 "1.8" COMPONENTS C)
if(HDF5_IS_PARALLEL)
set(H5MD 1)
include(FindPythonModule)
find_python_module(h5py)
add_feature_info(HDF5 ON "parallel")
else()
unset(H5MD)
unset(HDF5_FOUND)
endif(HDF5_IS_PARALLEL)
if(HDF5_FOUND)
if(HDF5_IS_PARALLEL)
set(H5MD 1)
include(FindPythonModule)
find_python_module(h5py)
add_feature_info(HDF5 ON "parallel")
else()
unset(H5MD)
set(HDF5_FOUND FALSE)
if(NOT WITH_HDF5_IS_DEFAULT_VALUE)
message(
FATAL_ERROR
"Optional dependency HDF5 explicitly requested, but parallel version not found."
)
endif()
endif(HDF5_IS_PARALLEL)
elseif(NOT WITH_HDF5_IS_DEFAULT_VALUE)
message(
FATAL_ERROR
"Optional dependency HDF5 explicitly requested, but not found.")
endif(HDF5_FOUND)
endif(WITH_HDF5)

# Check for the h5xx submodule and try to check it out if not found or update it
Expand All @@ -220,13 +238,21 @@ if(WITH_SCAFACOS)
pkg_check_modules(SCAFACOS scafacos)
if(SCAFACOS_FOUND)
set(SCAFACOS 1)
elseif(NOT WITH_SCAFACOS_IS_DEFAULT_VALUE)
message(
FATAL_ERROR
"Optional dependency ScaFaCoS explicitly requested, but not found.")
endif(SCAFACOS_FOUND)
endif(WITH_SCAFACOS)

if(WITH_GSL)
find_package(GSL)
if(GSL_FOUND)
set(GSL 1)
elseif(NOT WITH_GSL_IS_DEFAULT_VALUE)
message(
FATAL_ERROR "Optional dependency GSL explicitly requested, but not found."
)
endif(GSL_FOUND)
endif(WITH_GSL)

Expand Down
39 changes: 39 additions & 0 deletions cmake/option_if_available.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (C) 2020 The ESPResSo project
#
# This file is part of ESPResSo.
#
# ESPResSo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ESPResSo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Like `option()`, but create an extra boolean variable to store whether the
# option was set to its default value or to a user-provided value. With this
# command, the project can be installed with optional dependencies without
# the need to provide a list of CMake flags. Unavailable dependencies will be
# silently ignored. However, if the user specifically requested an optional
# dependency by passing the corresponding CMake flag, the build system has
# the possibility to throw an error if the dependency is unavailable.
#
# Note that when calling CMake again without clearing the build folder,
# variables from the previous CMake call are loaded in memory. For example,
# if the user passed a value to an `option_if_available()` the first time but
# not the second time, the variable will still be flagged as a user-provided
# value in the second CMake call.
macro(option_if_available varname help_text default_value)
if("${${varname}}" STREQUAL "")
set(${varname}_IS_DEFAULT_VALUE TRUE)
else()
set(${varname}_IS_DEFAULT_VALUE FALSE)
endif()
option(${varname} ${help_text} ${default_value})
endmacro()

0 comments on commit 4f280f6

Please sign in to comment.