Reputation: 87
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?
Upvotes: 4
Views: 2197
Reputation: 123
I just add this into the "CMakeLists.txt" and it works.
option(USE_MPI "Build with MPI" ON)
if(USE_MPI)
message("We will use mpi compiler")
set(CMAKE_C_COMPILER mpicc CACHE STRING "mpicc compiler" FORCE)
set(CMAKE_CXX_COMPILER mpic++ CACHE STRING "mpiC++ compiler" FORCE)
endif()
Upvotes: 0
Reputation:
You can use CMAKE_TOOLCHAIN_FILE to set the compiler you need:
# gcc.cmake
message("We will use GCC compiler")
set(CMAKE_C_COMPILER gcc CACHE STRING "C compiler" FORCE)
set(CMAKE_CXX_COMPILER g++ CACHE STRING "C++ compiler" FORCE)
# clang.cmake
message("We will use Clang compiler")
set(CMAKE_C_COMPILER clang CACHE STRING "C compiler" FORCE)
set(CMAKE_CXX_COMPILER clang++ CACHE STRING "C++ compiler" FORCE)
> cmake -H. -B_builds/clang -DCMAKE_TOOLCHAIN_FILE=clang.cmake
We will use Clang compiler
-- The C compiler identification is Clang 3.5.0
-- The CXX compiler identification is Clang 3.5.0
> cmake -H. -B_builds/gcc -DCMAKE_TOOLCHAIN_FILE=gcc.cmake
We will use GCC compiler
-- The C compiler identification is GNU 4.9.0
-- The CXX compiler identification is GNU 4.9.0
Upvotes: 1
Reputation: 42899
The best way to do so, and which is encouraged by CMake, is to have several out-of-source build directories with different configurations.
It is notoriously hard probably even impossible to change the compiler after having created the cmake cache, therefore it is wiser to directly set the option when invoking cmake for the first time using the command line:
mkdir mpi-build && cd mpi-build && cmake -DUSE_MPI=ON ..
Upvotes: 3