[CMake] How to determine the compiler used outside of cmake

Chuck Atkins chuck.atkins at kitware.com
Fri May 3 16:19:05 EDT 2013


Instead of overriding the compiler in the CMakeLists.txt to a specific
compiler version, why not guide CMake's compiler detection from the outside
and have CMake "choose" the one you want?  For instance, since you're
already using a wrapper to drive the CMAKe build, you could call cmake with
"cmake -DCMAKE_C_COMPILER=/usr/bin/gcc44
-DCMAKE_CXX_COMPILER=/usr/bin/g++44 ..." or inside your wrapper script, you
could export the CC and CXX environment variables.  This will allow your
CMake build to be clean and portable while abstracting the configuration
specific to your particular environment to the wrapper script you're
already using.  You could also look at using pre-defined cache files to
prime a collection of commonly used settings.  Using a pre-defined cache
file has actually turned out to be a good solution for my group here when
we had to maintain a hand-full of commonly used build configurations.  The
cache files just look like:

---
set(OPTION1 ON CACHE BOOL "Doc for Option 1")
set(OPTION2 /path/to/foo CACHE FILEPATH "Doc for Option 2")
set(CMAKE_CXX_COMPILER /usr/bin/g++44 CACHE FILEPATH "CXX Compiler")
...
---

And then you can instruct CMake to pre-load it's cache values via:
cmake -C /path/to/settings.cmake /path/to/source

A common use case for us is to have a particular set of options that are
set specifically for RHEL5 and another set when building for RHEL6.  It's
then often convenient to keep these in the repo with the rest of the code.

Just a though
- Chuck


On Thu, May 2, 2013 at 3:35 PM, Miller, Frank <FMiller at sjm.com> wrote:

>  Update. The reason I didn’t have CMAKE_CXX_COMPILER in my cache is
> because I was doing a****
>
> ** **
>
>   set(CMAKE_CXX_COMPILER /usr/bin/g++44 )****
>
> ** **
>
> at the top of my root CMakeLists.txt. That preempts the set( CACHE ) call
> that project() does. My bad.****
>
> ** **
>
> Frank ****
>
> ** **
>
> *From:* cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] *On
> Behalf Of *Miller, Frank
> *Sent:* Thursday, May 02, 2013 9:51 AM
> *To:* m.hergarden at euphoria-it.nl
> *Cc:* cmake at cmake.org
> *Subject:* Re: [CMake] How to determine the compiler used outside of cmake
> ****
>
> ** **
>
> Interesting. I am running a very recent version (2.8.10.20130415) built
> from git. Perhaps the handling of CMAKE_CXX_COMPILER has changed recently.
> ****
>
> ** **
>
> I can’t believe I didn’t think to manually put it in the cache. That will
> work for me. Thanks.****
>
> ** **
>
> As for the bigger problem I am solving... I have a wrapper script that
> drives the build. This is done to emulate a legacy build system and add
> some nice-to-haves. For example, the user can call the script from their
> source tree and it will call cmake/ninja in the proper binary tree. The
> default compiler to be used is hard coded into the CMakeLists.txt and I
> would like to have the script detect when the compiler changes and do a
> reconfigure automatically.****
>
> ** **
>
> Now that I’m explaining the larger problem, I think I will have to put
> extra information in the cache to detect when the user manually overrides
> the default compiler. So I probably would have come around to your
> suggestion on my own.****
>
> ** **
>
> Thanks again,****
>
> ** **
>
> Frank****
>
> ** **
>
> *From:* m.hergarden [mailto:m.hergarden at euphoria-it.nl<m.hergarden at euphoria-it.nl>]
>
> *Sent:* Thursday, May 02, 2013 12:16 AM
> *To:* Miller, Frank
> *Cc:* cmake at cmake.org
> *Subject:* Re: [CMake] How to determine the compiler used outside of cmake
> ****
>
> ** **
>
> The compiler does show up in my cache:
> CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++, but that doesn't solve your issue.
> What is the bigger problem you are solving? If you really really need it
> in the cache, you can always write a variable yourself to make sure it's
> there.
>
> Regards,
> Micha
>
> On 05/01/2013 09:56 PM, Miller, Frank wrote:****
>
>  Greetings,****
>
>  ****
>
> Given a configured build tree, I would like to determine the c++ compiler
> used without reconfiguring. I expected the CMAKE_CXX_COMPILER variable
> would be in the CMakeCache.txt but I was wrong. I tried to run a simple
> cmake script with ‘cmake -P’ and print the value of CMAKE_CXX_COMPILER but
> that does not work. I found the value in
> ‘CMakeFiles/2.8.10.20130415/CMakeCXXCompiler.cmake’, so I have a
> workaround, but that smells like the wrong solution. Does anyone know of a
> better way?****
>
>  ****
>
> Thanks as always,****
>
>  ****
>
> Frank****
>
>  ****
>
>  ****
>
>
> This communication, including any attachments, may contain information
> that is proprietary, privileged, confidential or legally exempt from
> disclosure. If you are not a named addressee, you are hereby notified that
> you are not authorized to read, print, retain a copy of or disseminate any
> portion of this communication without the consent of the sender and that
> doing so may be unlawful. If you have received this communication in error,
> please immediately notify the sender via return e-mail and delete it from
> your system.
>
> ****
>
> --****
>
> ** **
>
> Powered by www.kitware.com****
>
> ** **
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html****
>
> ** **
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ****
>
> ** **
>
> Follow this link to subscribe/unsubscribe:****
>
> http://www.cmake.org/mailman/listinfo/cmake****
>
>  ** **
>
> -- ****
>
> Met vriendelijke groet,****
>
> Micha Hergarden****
>
> [image: image001]
>
> Lieve Vrouweplein 9-10
> 5038 TS Tilburg
>
> *CONTACT*
>
> *T*    +31 (0) 13 460 92 80
> *F*    +31 (0) 13 460 92 81
> *E    *m.hergarden at euphoria-it.nl
> *W  *www.euphoria-it.nl
>
> Helpdesk: 013 - 460 92 86
> Storingdienst: 06-47942098****
>
>  ****
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130503/d3a8dde4/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 3239 bytes
Desc: not available
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130503/d3a8dde4/attachment-0001.png>


More information about the CMake mailing list