Inconsistent Behavior of GPU-related CMake (cache) variables

WaLBerla'c CMake system exposes three variables to note the state of GPU support in the build

  • WALBERLA_BUILD_WITH_CUDA is a cache variable indicating that CUDA is enabled
  • WALBERLA_BUILD_WITH_HIP is a cache variable indicating that HIP is enabled
  • WALBERLA_BUILD_WITH_GPU_SUPPORT is a scoped variable indicating that any of the above is enabled.

However, they are being set in a way inconsistent with CMake semantics, which can cause several problems, especially when embedding waLBerla as a library into another project.

Inconsistent Late Deactivation

In the code checking for availability of CUDA or HIP compilers, there is, at the end, a clause that aims to set the respective _BUILD_WITH variables to false if the corresponding compiler is not found:

# CMakeLists.txt:1058
message( WARNING "CUDA could not be enabled. The host compiler might not be compatible. Check CMakeFiles/CMakeError.log for more information" )
set ( WALBERLA_BUILD_WITH_CUDA FALSE )
# CMakeLists.txt:1107
message("HIP compiler not found. HIP support is not possible")
set ( WALBERLA_BUILD_WITH_HIP FALSE )

These Do Not Update The Cache Variables! Instead, they create a new scoped variable with the same name, which is only visible in the source tree below the waLBerla CMakeLists.txt, but not globally in the project. Read carefully the documentation of set().

CMake semantics are that, in most cases, cache variables, once set, should not be changed. In this case, that means that if the user requires CUDA or HIP support by enabling the WALBERLA_BUILD_WITH_CUDA or WALBERLA_BUILD_WITH_HIP options on the CLI or in a preset, the respective system is either available, or the configure step should fail.

WALBERLA_BUILD_WITH_GPU_SUPPORT is not exported

The reverse problem is that WALBERLA_BUILD_WITH_GPU_SUPPORT is a regular scoped variable and is therefore never exported outside of the scope of waLBerla's cmake, such that an external embedding project cannot read it. This should be a build-system private cache variable that is set once CUDA or HIP support is verified:

set (WALBERLA_BUILD_WITH_GPU_SUPPORT TRUE CACHE BOOL "")
mark_as_advanced( WALBERLA_BUILD_WITH_GPU_SUPPORT )