Skip to content

miinso/rules_fortran

Repository files navigation

rules_fortran

Fortran rules for Bazel using LLVM Flang.

Builds libraries, binaries, and tests. Cross-compiles to WebAssembly. Works with C/Fortran interop, from-source BLAS/LAPACK, and OpenMP.

Documentation -- Examples

Platforms

Host Native target Cross-compilation
Linux x86_64 x86_64-unknown-linux-gnu wasm32-unknown-emscripten
Linux ARM64 aarch64-unknown-linux-gnu wasm32-unknown-emscripten
macOS x86_64 x86_64-apple-darwin wasm32-unknown-emscripten
macOS ARM64 arm64-apple-darwin wasm32-unknown-emscripten
Windows x86_64 x86_64-pc-windows-msvc wasm32-unknown-emscripten

Setup

Add to your MODULE.bazel:

bazel_dep(name = "rules_fortran")
git_override(
    module_name = "rules_fortran",
    remote = "https://github.com/miinso/rules_fortran.git",
    commit = "...",  # see releases
)

Usage

load("@rules_fortran//fortran:defs.bzl", "fortran_binary", "fortran_library", "fortran_test")

fortran_library(
    name = "mylib",
    srcs = ["mylib.f90"],
)

fortran_binary(
    name = "myapp",
    srcs = ["main.f90"],
    deps = [":mylib"],
)

fortran_test(
    name = "mytest",
    srcs = ["test.f90"],
    deps = [":mylib"],
)

C interop

Fortran and C targets can depend on each other directly:

fortran_library(name = "fortran_math", srcs = ["math.f90"])
cc_binary(name = "app", srcs = ["main.c"], deps = [":fortran_math"])

OpenMP

fortran_test(
    name = "hello_omp",
    srcs = ["hello_omp.f90"],
    copts = ["-fopenmp"],
    deps = ["@libomp"],
)

WebAssembly

Cross-compile to wasm32 with Emscripten:

fortran_library(name = "hello", srcs = ["hello.f90"])
cc_binary(name = "hello_cc", deps = [":hello"])
wasm_cc_binary(name = "hello_wasm", cc_target = ":hello_cc")

See examples/ for BLAS/LAPACK, OpenMP+wasm, and more.

Packages

 
 
 

Contributors

Languages