Flag -DCMAKE_Fortran_FLAGS not changing the Makefile

Hello,
I need to set a flag (“Mfree”) for my Fortran compiler. However, using the -DCMAKE_Fortran_FLAGS is not making any difference. I have compared both Makefiles (with and without) and they are the same (except for the parent directory). The full command to invoke the compilation is:

sudo /usr/local/bin/cmake -DCMAKE_INSTALL_PREFIX=/opt/NCEP -DCMAKE_C_COMPILER=/opt/openmpi/bin/mpicc -DCMAKE_Fortran_COMPILER=/opt/openmpi/bin/mpif90 -DCMAKE_Fortran_FLAGS=“-Mfree” -DCMAKE_PREFIX_PATH=/opt/netcdf /opt/ERASE/NCEPLIBS

Thanks.

I do this by setting environment variables FFLAGS as per CMAKE__FLAGS — CMake 3.20.3 Documentation

It’s only effective on the first CMake configure “cmake -B build” of the project.
To make it command local on Unix-like as usual:

FFLAGS="-Mfree" cmake ...

Hi @scivision,
Unfortunately including FFLAGS="-Mfree" doesn’t help either. I also tried $sudo /usr/local/bin/cmake --build /opt/NCEPLIBS/build --target clean to clean up as much as possible but same result. Unless the issue is caused by running cmake as a superuser, I’m out of ideas. Thanks.

The build directory probably needs to be wiped out e.g. rm -r /opt/NCEPLIBS/build otherwise CMake will have already initialized CMAKE_Fortran_FLAGS and ignores FFLAGS.

I wiped out everything and tried to build from a different directory as a user (rather than as sudo) but the result is still the same, it doesn’t take the flag and fails with the Label field of continuation line is not blank error message while trying to compile.

OK the problem is sudo. I tried this with a minimal CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(foo Fortran)

message(STATUS "Env. FFLAGS: $ENV{FFLAGS}")
message(STATUS "CMAKE_Fortran_FLAGS: ${CMAKE_Fortran_FLAGS}")
FFLAGS="-fimplicit-none" cmake -B build

works as expected–both variables are populated.

However,

FFLAGS="-fimplicit-none" sudo cmake -B build

leaves both variables blank. This is true at least for Bash and Zsh.

I had simply given incorrect shell syntax. This works as expected:

sudo FFLAGS="-fimplicit-none" cmake -B build

Hi @scivision,
Trying to compile as a user rather than sudo didn’t make the trick for me. At the end, I had to fork (from the developer) and add the CMAKE_Fortran_FLAGS to the options included in CmakeLists.txt. It seems that it has finally taken the flag although it’s generating a long list of errors caused by other parts of the code. Thanks.

Can you check wether exporting the environment variable FFLAGS prior to your cmake call does solve the issue? I suppose it has to do with the usage of ExternalProject_Add in the CMakeLists.txt file. The setting for CMAKE_Fortran_FLAGS is not propagated to the invocation of the ExternalProject_Add configure/build command. Setting the FFLAGS=-Mfree variable prior to the invocation of cmake does influence only the cmake command’s shell but not the subsequently started ones for ExternalProject_Add commands. And try to build that as a restricted user without using sudo.

  export FFLAGS=-Mfree
  cmake -B build_dir -S source_dir <options>

That was my 1st attempt before everything else. cmake is only taking the flags included in CmakeList.txt so the solution was to modify this file.

1 Like

OK. If using ExternalProject, as Volker notes sudo may be making more complexities with the environment variables propagating through.

I would also strongly encourage in general using build systems including CMake without sudo. Perhaps only use sudo on the cmake --install build step if needed.

1 Like