[CMake] How to configure Fortran compiler, flags, and so on.

Yngve Inntjore Levinsen yngve.levinsen at gmail.com
Tue Apr 2 04:46:47 EDT 2013


Hi,

If you define variables in your environment before configuring cmake
they will be read by cmake.

The following environment variables (maybe I forget some) are relevant
for fortran code:
FC = fortran compiler
FFLAGS = fortran flags (added to default flags)
LDFLAGS = linker flags (any language)

In addition, you can read any variables you want in your cmake script
using $ENV{}, for example

set(MY_FFLAGS_DOUBLE $ENV{FFLAGS_DOUBLE})

Note, if you already have run cmake once in your build folder, it will
ignore FFLAGS etc and use the cached variables. Hence I would recommend
to protect these ENV{} calls in if defined clauses to get same behaviour:

if(NOT DEFINED MY_FFLAGS_DOUBLE)
   set(MY_FFLAGS_DOUBLE $ENV{FFLAGS_DOUBLE} CACHE STRING "Fortran double
flags")
endif()

Here I also added "CACHE" so that this variable is stored for next runs
of cmake. I think this is close to what you are asking for (and is the
same behaviour you will have for FFLAGS and friends).

Hope this helps!

Cheers,
Yngve

On 31. mars 2013 01:33, Daniel Carrera wrote:
> Hello all,
>
> I am starting to learn about CMake. So far I have only written very
> minimal CMakeLists.txt files. I am wondering how hard it would be to
> make CMake read some configuration options for Fortran 95 from an
> external file similar to this:
>
> ---- myprogram.conf ----
> FC = mpif90
> F77 = $(FC)
> FFLAGS = -fbounds-check -I/usr/lib/openmpi/include/
> FFLAGS_DOUBLE = -fdefault-real-8
>
>
> The "end users" are largely the developers. The program is recompiled
> and rerun regularly and these settings rarely change, so they need to
> be in some sort of global config file. I was thinking that it might
> make sense to write a thin shell script wrapper around CMake that
> basically does this:
>
> source $CONF_PATH/myprogram.conf
> cmake $SOURCE_PATH
>
>
> So then all those settings become environment variables. And then I'd
> do something inside CMakeLists.txt to make sure that CMake uses the
> contents of those variables to compile the program.
>
> Am I on the right track here? Or am I just badly re-inventing the
> wheel? If I am on the right track, can someone show me how I would get
> CMake to use these variables? I have tried to find documentation, but
> what I've found seems specific to C/C++:
>
> http://www.cmake.org/Wiki/CMake_Useful_Variables
>
> Thanks for the help.
>
> 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



More information about the CMake mailing list