Build Fortran libraries, executables, and tests with Bazel. Compile with Flang and link with Bazel's selected C/C++ toolchain. C and C++ targets can link against Fortran libraries through ordinary Bazel dependencies.
Install Bazelisk, then try the included examples on Linux x86_64 with glibc or on macOS Arm64.
git clone https://github.com/hermeticbuild/rules_fortran.git
cd rules_fortran/tests/integration
bazelisk test //:hello_test //:chain_test //:c_main_testThe examples use the published Flang compiler and build the target modules and runtime with Hermetic LLVM.
To use the rules in your own Bzlmod project, follow the
toolchain setup guide. It uses a Git checkout of
rules_fortran and a pinned Hermetic LLVM revision.
A target that reads a Fortran module with use declares the module's
library in deps. Bazel builds the module before compiling its consumer.
BUILD.bazel
load("@rules_fortran//fortran:fortran_library.bzl", "fortran_library")
load("@rules_fortran//fortran:fortran_test.bzl", "fortran_test")
fortran_library(
name = "arithmetic",
srcs = ["arithmetic.f90"],
)
fortran_test(
name = "arithmetic_test",
srcs = ["arithmetic_test.f90"],
deps = [":arithmetic"],
)arithmetic.f90
module arithmetic
implicit none
contains
integer function twice(value)
integer, intent(in) :: value
twice = 2 * value
end function twice
end module arithmeticarithmetic_test.f90
program arithmetic_test
use arithmetic, only: twice
implicit none
if (twice(21) /= 42) stop 1
end program arithmetic_testUse fortran_binary for an executable. For a C
caller, declare the interface in a header and use bind(C) in Fortran.
The interop example links
a C main with
a Fortran routine.
The Flang integration can target x86_64 and Arm64 for Linux (glibc and musl), macOS, and Windows (MinGW and MSVC). GNU Fortran and shared libraries are planned.