MantisBT - CMake
View Issue Details
0014294CMakeCMakepublic2013-07-15 20:172015-01-05 08:39
Greg Coombe 
Brad King 
normalmajoralways
closedfixed 
LinuxUbuntu12.04
CMake 2.8.11.2 
CMake 3.1CMake 3.1 
0014294: Specifying CMAKE_CXX_COMPILER=g++ after first run deletes all cached values
If I specify a command-line option using the "option()" function and provide it on the command line along with CMAKE_<lang>_COMPILER option (specified without full path), it is overwritten.
1. Make a small CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.11)
option(RERUN_TEST "Re-run me!" OFF)
message(STATUS "Re-run test variable is " ${RERUN_TEST})


2. Run twice with both the option specified, as well as the compiler:

$ cmake -D RERUN_TEST=ON -D CMAKE_CXX_COMPILER=g++ CMakeLists.txt
-- The C compiler identification is GNU 4.6.3
-- The CXX compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Re-run test variable is ON
-- Configuring done
-- Generating done


$ cmake -D RERUN_TEST=ON -D CMAKE_CXX_COMPILER=g++ CMakeLists.txt
-- Re-run test variable is ON
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_CXX_COMPILER= g++

-- The C compiler identification is GNU 4.6.3
-- The CXX compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Re-run test variable is OFF
-- Configuring done
-- Generating done


So now we've built the project without the command-line option.

3. Interestingly, note that using the full path of the compiler doesn't have the same behavior:

$ cmake -D RERUN_TEST=ON -D CMAKE_CXX_COMPILER=/usr/bin/g++ CMakeLists.txt
-- Re-run test variable is ON
-- Configuring done
-- Generating done


Speculation:
It appears that CMake is writing the CMAKE_<lang>_COMPILER value to the cache before expanding it, then interpreting the different value as requiring a re-run. This re-run ignores the command-line options (maybe because the cache is nuked: http://public.kitware.com/Bug/view.php?id=13756 [^]).
No tags attached.
duplicate of 0014366closed Stephen Kelly Command line definitions ignored if -DCMAKE_C_COMPILER is passed 
related to 0006102closed Bill Hoffman Changing compilers should be an easier operation 
related to 0013756closed Brad King Infinite loop when reconfiguring with different CMAKE_C_COMPILER 
txt CMakeLists.txt (131) 2013-07-15 20:17
https://public.kitware.com/Bug/file/4818/CMakeLists.txt
Issue History
2013-07-15 20:17Greg CoombeNew Issue
2013-07-15 20:17Greg CoombeFile Added: CMakeLists.txt
2013-07-16 08:39Brad KingRelationship addedrelated to 0006102
2013-07-16 08:39Brad KingRelationship addedrelated to 0013756
2013-07-16 08:43Brad KingStatusnew => backlog
2013-07-16 08:43Brad KingSummaryCommand-line options overwritten by re-run => Specifying CMAKE_CXX_COMPILER=g++ after first run deletes all cached values
2013-07-16 08:43Brad KingNote Added: 0033549
2013-07-16 08:48Brad KingNote Added: 0033550
2014-07-24 13:46Brad KingRelationship addedduplicate of 0014366
2014-07-24 13:49Brad KingNote Added: 0036455
2014-07-24 13:50Brad KingAssigned To => Brad King
2014-07-24 13:50Brad KingStatusbacklog => assigned
2014-07-24 13:50Brad KingTarget Version => CMake 3.1
2014-07-28 10:52Brad KingStatusassigned => resolved
2014-07-28 10:52Brad KingResolutionopen => fixed
2014-07-28 10:52Brad KingFixed in Version => CMake 3.1
2015-01-05 08:39Robert MaynardNote Added: 0037594
2015-01-05 08:39Robert MaynardStatusresolved => closed

Notes
(0033549)
Brad King   
2013-07-16 08:43   
This is caused by the cache-deletion-to-change-compiler created for issue 0006102.

You can work around this by using the environment to select the compiler:

 CXX=g++ cmake ...

or by always passing a full path to set CMAKE_CXX_COMPILER.
(0033550)
Brad King   
2013-07-16 08:48   
What happens on the second run is that CMake processes your command-line cache options first. Since they "change" the compiler CMake wipes out the current cache which includes anything just processed.

We have to store the full path in the CMAKE_CXX_COMPILER cache option so that future re-configurations (possibly during "make") are not affected by the PATH. The logic that decides that the compiler has changed does not account for the transformation to a full path.
(0036455)
Brad King   
2014-07-24 13:49   
I think this was resolved for 0014366 by:

 Fix resetting the compiler on the command line
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=8b2a2db0 [^]

first released in CMake 3.0.

However I think a follow-up change is needed:

 cmGlobalGenerator: Do not re-add CMAKE_<LANG>_COMPILER
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=731427a6 [^]
(0037594)
Robert Maynard   
2015-01-05 08:39   
Closing resolved issues that have not been updated in more than 4 months