I want to allow users to run a cmake configuration tool (such as ccmake or cmake-gui) to change which compiler is used. This is necessary as I'm building a cross-platform program which sometimes runs on a cluster (and therefore needs the cluster-specific compilers that have appropriate MPI construction) but sometimes runs in a non-MPI mode.
Let's assume that the compiler can normally be whatever the platform expects (typically gcc/g++ or in a worst case MSVC) but that when we're using MPI we always know we need to compile through mpicc/mpicxx.
I feel like the following code should work:
cmake_minimum_required(VERSION 2.4)
option(USE_MPI "Is this on an MPI system?" OFF)
if (USE_MPI)
message(STATUS "Setting compiler to MPIC")
set(CMAKE_CXX_COMPILER mpicxx)
set(CMAKE_C_COMPILER mpicc)
endif(USE_MPI)
project(test_project)
add_executable(testprogram test.cpp)
The above is a simplified example, but I believe it demonstrates what I am trying to do.
This assumes that mpicxx and mpicc are on the path, but that's not really the issue. The problem crops up because when you run ccmake or cmake-gui, it evaluates and steps down the default option path of 'off', then hits the project line, and writes into the cache the path and choices appropriate for the standard, native compiler (typically gcc). You can change the options in the gui, but it is too late, configuring/generating again won't update the cache. Even if the user does turn the option on, the compiler remains set.
What I seem to want to do, is re-run through the compiler-finding routine that occurs on the project() line.
Is there a better way to do this? Am I misunderstanding how I should be setting up the compiler?