Basic
Source: examples/basic/
Hello world, libraries with module dependencies, and include files.
1.A. Hello World
basic/
├── BUILD.bazel
└── hello.f90fortran
program hello
implicit none
print *, "Hello from Fortran with rules_fortran!"
end program hellostarlark
fortran_test(
name = "hello",
srcs = ["hello.f90"],
)bash
bazel test //basic:hello --test_output=allHello from Fortran with rules_fortran!1.B. Libraries and Modules
basic/
├── BUILD.bazel
├── main.f90
├── math_module.f90
├── statistics.f90
└── io_module.f90fortran
module math_module
implicit none
private
public :: factorial, fibonacci, gcd
contains
function factorial(n) result(fact)
integer, intent(in) :: n
integer :: fact
integer :: i
fact = 1
do i = 2, n
fact = fact * i
end do
end function factorial
recursive function fibonacci(n) result(fib)
integer, intent(in) :: n
integer :: fib
if (n <= 1) then
fib = n
else
fib = fibonacci(n-1) + fibonacci(n-2)
end if
end function fibonacci
recursive function gcd(a, b) result(g)
integer, intent(in) :: a, b
integer :: g
if (b == 0) then
g = a
else
g = gcd(b, mod(a, b))
end if
end function gcd
end module math_modulefortran
module statistics
implicit none
private
public :: mean, variance, std_dev
contains
function mean(data) result(m)
real, dimension(:), intent(in) :: data
real :: m
m = sum(data) / size(data)
end function mean
function variance(data) result(v)
real, dimension(:), intent(in) :: data
real :: v, m
m = mean(data)
v = sum((data - m)**2) / size(data)
end function variance
function std_dev(data) result(s)
real, dimension(:), intent(in) :: data
real :: s
s = sqrt(variance(data))
end function std_dev
end module statisticsfortran
program main
use math_module
use statistics
use io_module
implicit none
integer :: n
real, dimension(10) :: data
! Test math functions
n = 5
print *, "Factorial of", n, "=", factorial(n)
print *, "Fibonacci of", n, "=", fibonacci(n)
print *, "GCD(48, 18) =", gcd(48, 18)
! Test statistics
data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
print *, "Mean:", mean(data)
print *, "Std Dev:", std_dev(data)
print *, "Scientific app completed!"
end program mainstarlark
fortran_library(
name = "math_lib",
srcs = [
"math_module.f90",
"statistics.f90",
],
)
fortran_library(
name = "io_lib",
srcs = ["io_module.f90"],
)
fortran_test(
name = "app",
srcs = ["main.f90"],
deps = [
":io_lib",
":math_lib",
],
)bash
bazel test //basic:app --test_output=all Factorial of 5 = 120
Fibonacci of 5 = 5
GCD(48, 18) = 6
Mean: 5.5
Std Dev: 2.8722813
Scientific app completed!1.C. Include Files
basic/
├── BUILD.bazel
├── include/
│ └── constants.inc
└── use_include.f90fortran
! Common constants shared via INCLUDE
REAL, PARAMETER :: PI = 3.14159265358979
REAL, PARAMETER :: E = 2.71828182845905
INTEGER, PARAMETER :: MAX_ITERATIONS = 1000fortran
program use_include
implicit none
INCLUDE 'constants.inc'
print *, "PI =", PI
print *, "E =", E
print *, "MAX_ITERATIONS =", MAX_ITERATIONS
! Verify values are correct
if (abs(PI - 3.14159265358979) > 0.0001) stop 1
if (abs(E - 2.71828182845905) > 0.0001) stop 1
if (MAX_ITERATIONS /= 1000) stop 1
print *, "PASSED"
end program use_includestarlark
fortran_test(
name = "constants",
srcs = ["use_include.f90"],
hdrs = ["include/constants.inc"],
includes = ["include"],
)bash
bazel test //basic:constants --test_output=allPI = 3.1415927
E = 2.7182817
MAX_ITERATIONS = 1000
PASSED