[CMake] How to let the user select the compiler and compiler flags.

Daniel Carrera dcarrera at gmail.com
Mon Apr 1 13:50:42 EDT 2013


Hi David,

Thanks. That seems to work well. I tweaked your idea to allow the user
to have two different config files. To do that, instead of using -C, I
have two "include" lines in my CMakeLists.txt. It seems to work well.
I just have to make sure I explain clearly which file takes
precedence.

Cheers,
Daniel.




On 1 April 2013 16:34, David Cole <dlrdave at aol.com> wrote:
> Do use CACHE with your set statements to put them in the cache, and then use the -C command line argument to prime the cache with your file, rather than including it in your CMakeLists file.
>
> That should have the effect you're seeking...
>
> HTH,
> David C.
>
>
> On Apr 1, 2013, at 3:54 AM, Daniel Carrera <dcarrera at gmail.com> wrote:
>
>> Hi all,
>>
>> I am just getting started with CMake. Here is my question: Is there a
>> good way to have an external config file that specifies both the
>> compiler an compile flags?
>>
>> The program is compiled many times, and it makes sense to have the
>> compiler and flags in a config file rather than have them in the
>> command line. I am hoping to use CMake itself as the language of the
>> config file. For example:
>>
>> # -----   myprogram.cmake -----
>> # Local configuration file.
>>
>> set (CMAKE_Fortran_COMPILER      "mpif90")
>> set (CMAKE_Fortran_FLAGS         "-fdefault-real-8")
>> set (CMAKE_Fortran_FLAGS_DEBUG   "-O0 -g -fbounds-check")
>> set (CMAKE_Fortran_FLAGS_RELEASE "-O3")
>> set (CMAKE_BUILD_TYPE             RELEASE)
>>
>>
>> I think this would be a useful config file and I can just "include" it
>> in CMakeLists.txt. The problem is that the compiler must be specified
>> before I run project(), while the compile flags must be specified
>> AFTER project():
>>
>>
>> cmake_minimum_required (VERSION 2.8)
>> include ("path/to/myprogram.cmake")
>> enable_language (Fortran)
>> project (MyProgram)
>>
>>
>> At this point all the compile flags have been overwritten by
>> enable_language() and project().  Can anyone suggest a good solution?
>> I have a few ideas, but I am not sure about them:
>>
>> 1) I could give up on the idea of using CMake in the config file. The
>> config file could be a shell script that sets some environment
>> variables. My understanding is that this will work, but you would lose
>> the flexibility of CMake.
>>
>> 2) I could load the config file twice. Once before, and once after
>> project(). I am not sure if this would have any unexpected
>> side-effects.
>>
>> 3) I could store the flags in the cache. But I am not sure if
>> overwriting the cache is a good idea or not.
>>
>> 4) I could use a shell script to split the config file into the
>> compiler part and everything else, and then load them separately. I
>> think I like this option, but I am not fully aware of the
>> side-effects.
>>
>> Does anyone have any words of advice or wisdom to share with me?
>>
>> Cheers,
>> Daniel.
>> --
>> Lord of the rings calendar in your Linux/Unix/Mac terminal: cat
>> /usr/share/calendar/calendar.lotr
>> --
>>
>> 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



-- 
Lord of the rings calendar in your Linux/Unix/Mac terminal: cat
/usr/share/calendar/calendar.lotr


More information about the CMake mailing list