Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Fix" handling of CMAKE_Fortran_FLAGS*. #204

Open
dhermes opened this issue Feb 13, 2020 · 0 comments
Open

"Fix" handling of CMAKE_Fortran_FLAGS*. #204

dhermes opened this issue Feb 13, 2020 · 0 comments

Comments

@dhermes
Copy link
Owner

dhermes commented Feb 13, 2020

TL;DR the set(OLD_CMAKE_Fortran_FLAGS... lines (and the corresponding unset(OLD_CMAKE_Fortran_FLAG... lines) should just be removed. Documentation should be added that explains how to use any of the following to customize the build:

  • FFLAGS environment variable
  • CMAKE_Fortran_FLAGS environment variable
  • CMAKE_Fortran_FLAGS_DEBUG environment variable
  • CMAKE_Fortran_FLAGS_RELEASE environment variable
  • CMAKE_Fortran_FLAGS_<CMAKE_BUILD_TYPE> environment variable (if any CMAKE_BUILD_TYPE other than Debug or Release are added)
  • -DCMAKE_Fortran_FLAGS flag to cmake
  • -DCMAKE_Fortran_FLAGS_DEBUG flag to cmake
  • -DCMAKE_Fortran_FLAGS_RELEASE flag to cmake
  • -DCMAKE_Fortran_FLAGS_<CMAKE_BUILD_TYPE> flag to cmake

See also CMAKE_<LANG>_FLAGS, CMAKE_<LANG>_FLAGS_<CONFIG>, CMAKE_<LANG>_FLAGS_DEBUG, CMAKE_<LANG>_FLAGS_INIT, etc.


Right now, the "restore" stage just puts the flags back at the end of configure (so that they are present at build time).

For example (via conda-forge/staged-recipes#10762), running

cmake                                                       ^
    -G "MinGW Makefiles"                                    ^
    -DCMAKE_IGNORE_PATH="%CMAKE_IGNORE_PATH%"               ^
    -DCMAKE_Fortran_COMPILER=gfortran                       ^
    -DCMAKE_BUILD_TYPE=Release                              ^
    -DCMAKE_INSTALL_PREFIX:PATH="%LIBRARY_PREFIX%"          ^
    -DCMAKE_Fortran_FLAGS="-fno-asynchronous-unwind-tables" ^
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON                        ^
    -S "%SRC_DIR%\src\fortran"                              ^
    -B .

produces a build with the flag present:

...
%BUILD_PREFIX%\Library\mingw-w64\bin\gfortran.exe -Dbezier_EXPORTS  -fno-asynchronous-unwind-tables -O3 -DNDEBUG -O3   -fno-second-underscore -Wall -Wextra -Wno-compare-reals -Wimplicit-interface -fmax-errors=1 -std=f2008 -Werror -march=native -DNDEBUG -O3 -funroll-loops -c %SRC_DIR%\src\fortran\curve.f90 -o CMakeFiles\bezier.dir\curve.f90.obj
...

Note that this can be set either as a -DCMAKE_Fortran_FLAGS* flag, via the CMAKE_Fortran_FLAGS* environment variables or via the special FFLAGS environment variable.


We can get some "helpful" diagnostics on the values of these flags via

diff --git a/src/fortran/CMakeLists.txt b/src/fortran/CMakeLists.txt
index 82bcdfc4..af09ebcb 100644
--- a/src/fortran/CMakeLists.txt
+++ b/src/fortran/CMakeLists.txt
@@ -44,6 +44,10 @@ set(CMAKE_Fortran_FLAGS "")
 set(CMAKE_Fortran_FLAGS_DEBUG "")
 set(CMAKE_Fortran_FLAGS_RELEASE "")
 
+if(NOT "${OLD_CMAKE_Fortran_FLAGS}" STREQUAL "")
+  message(FATAL_ERROR "OLD_CMAKE_Fortran_FLAGS = ${OLD_CMAKE_Fortran_FLAGS}, CMAKE_Fortran_FLAGS = ${CMAKE_Fortran_FLAGS})")
+endif()
+
 add_subdirectory(quadpack)
 
 file(GLOB SOURCES *.f90)

and can ensure flags don't get through via:

diff --git a/src/fortran/CMakeLists.txt b/src/fortran/CMakeLists.txt
index 82bcdfc4..f8932d6e 100644
--- a/src/fortran/CMakeLists.txt
+++ b/src/fortran/CMakeLists.txt
@@ -104,11 +104,3 @@ install(
 install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 install(EXPORT BezierConfig DESTINATION share/bezier/cmake)
 export(TARGETS bezier FILE BezierConfig.cmake)
-
-# Restore all Fortran flags.
-set(CMAKE_Fortran_FLAGS ${OLD_CMAKE_Fortran_FLAGS})
-set(CMAKE_Fortran_FLAGS_DEBUG ${OLD_CMAKE_Fortran_FLAGS_DEBUG})
-set(CMAKE_Fortran_FLAGS_RELEASE ${OLD_CMAKE_Fortran_FLAGS_RELEASE})
-unset(OLD_CMAKE_Fortran_FLAGS)
-unset(OLD_CMAKE_Fortran_FLAGS_DEBUG)
-unset(OLD_CMAKE_Fortran_FLAGS_RELEASE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant