Makefiles: Fortran module dependency named via preprocessor DEFINE variable
Here is a simple example to show the problem. This is possibly related to #16853.
I am using CMake 3.20.2, GCC toolchain 10 (gfortran 10.3.1), on Rocky Linux 8.8. GNU Make version is 4.2.1.
main.f90:
1 #define MODULE_NAME MyConstants
2
3 module MODULE_NAME
4 implicit none
5 real(8) :: pi = 3.1415926535897932d0
6 end module
7
8 program main
9 use MODULE_NAME
10 implicit none
11 print *, 'pi = ', pi
12 end program
CMakeLists.txt:
1 project(test LANGUAGES Fortran)
2 set(CMAKE_Fortran_PREPROCESS YES)
3 add_executable(main.out main.f90)
I have both these files in a directory alone, and run:
cmake -B build
cd build
make
And I get the following output:
Scanning dependencies of target main.out
[ 50%] Building Fortran object CMakeFiles/main.out.dir/main.f90.o
Error copying Fortran module "module_name.mod". Tried "MODULE_NAME.mod" and "module_name.mod".
make[2]: *** [CMakeFiles/main.out.dir/depend.make:7: CMakeFiles/main.out.dir/module_name.mod.stamp] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main.out.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
It appears when CMake determines module dependencies, it doesn't preprocess the file (at least not fully, as suggested in #16853), and it thinks the module name is "MODULE_NAME" instead of "MyConstants".