Fortran Build Concepts
Fortran vs C/C++
Fortran has different compilation semantics than C/C++, which affects how build systems must handle it.
Module Files
In C/C++, headers are textual includes processed by the preprocessor. In Fortran, module declarations compile to binary .mod files containing type signatures, interfaces, and metadata.
module math ! Compiles to math.mod
implicit none
contains
function add(a, b) result(c)
real, intent(in) :: a, b
real :: c
c = a + b
end function add
end module mathWhen another file says use math, the compiler reads math.mod to verify types and interfaces. This means:
- Modules must be compiled before their users - strict build ordering required
- Module files are compiler-specific - gfortran
.modfiles are incompatible with flang - Module names are case-insensitive -
USE MATHanduse mathrefer to the same module
Dependency Ordering
Consider three files:
a.f90: module a (no dependencies)
b.f90: module b, uses a
c.f90: program c, uses a and bValid compilation order: a.f90 → b.f90 → c.f90
Invalid: b.f90 first (needs a.mod), or c.f90 first (needs both)
In C/C++, object files can be compiled in any order. In Fortran, sources must be compiled in dependency order.
How rules_fortran Handles This
Module Propagation
When one target depends on another, module files are propagated via FortranInfo:
a.f90: module a
b.f90: module b, uses a
c.f90: program c, uses a and bfortran_library(
name = "a",
srcs = ["a.f90"],
)
fortran_library(
name = "b",
srcs = ["b.f90"],
deps = [":a"],
)
fortran_binary(
name = "c",
srcs = ["c.f90"],
deps = [":a", ":b"],
)The deps attribute makes all modules from dependencies available when compiling.
Unlike C/C++ headers which are just textual includes, Fortran modules are compiled artifacts. The build system must ensure modules are compiled before their users - this is handled automatically through deps.
C Interoperability
rules_fortran provides CcInfo from fortran_library, enabling:
cc_binary/cc_library/cc_testcan depend onfortran_libraryfortran_binary/fortran_library/fortran_testcan depend oncc_library
See the C/Fortran Interop example for details.