Fortran rules

Rules

fortran_binary

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

fortran_binary(name, deps, srcs, data, hdrs, defines, env, fopts, includes, linkopts, linkstatic)

Compiles Fortran source files and links them into an executable.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsList of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo).List of labelsoptional[]
srcsList of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR).List of labelsoptional[]
dataList of additional files to include in the runfiles of the target.List of labelsoptional[]
hdrsHeader and include files for this target. Made available to dependents.List of labelsoptional[]
definesList of preprocessor definitions to pass to the compiler (e.g., -DDEBUG).List of stringsoptional[]
envSpecifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(rootpath), $(execpath), location, and "Make variable" substitution.Dictionary: String -> Stringoptional{}
foptsList of Fortran-specific compiler flags (e.g., -O2, -fopenmp).List of stringsoptional[]
includesInclude directories to add to the compilation search path.List of stringsoptional[]
linkoptsList of additional linker flags to pass to the linker.List of stringsoptional[]
linkstaticIf True, prefer linking deps statically. If False, prefer dynamic libraries.BooleanoptionalTrue

fortran_library

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

fortran_library(name, deps, srcs, data, hdrs, alwayslink, defines, fopts, implementation_deps,
                includes, linkopts, local_defines)

Compiles Fortran source files into a static library. Libraries can export Fortran modules for use by dependent targets.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsList of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo).List of labelsoptional[]
srcsList of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR).List of labelsoptional[]
dataList of additional files to include in the runfiles of the target.List of labelsoptional[]
hdrsHeader and include files for this target. Made available to dependents.List of labelsoptional[]
alwayslinkIf True, any binary that depends (directly or indirectly) on this library will link in all object files, even if some contain no symbols referenced by the binary.BooleanoptionalFalse
definesList of preprocessor definitions to pass to the compiler (e.g., -DDEBUG).List of stringsoptional[]
foptsList of Fortran-specific compiler flags (e.g., -O2, -fopenmp).List of stringsoptional[]
implementation_depsDependencies used only for compiling this library. Their modules and includes are not propagated to dependents, but they are still linked transitively.List of labelsoptional[]
includesInclude directories to add to the compilation search path.List of stringsoptional[]
linkoptsList of additional linker flags to pass to the linker.List of stringsoptional[]
local_definesList of preprocessor definitions that apply only to this target, not to its dependents.List of stringsoptional[]

fortran_shared_library

load("@rules_fortran//fortran:defs.bzl", "fortran_shared_library")

fortran_shared_library(name, deps, srcs, data, hdrs, alwayslink, defines, fopts,
                       implementation_deps, includes, linkopts, local_defines)

Compiles Fortran source files into a shared (dynamic) library. Libraries can export Fortran modules for use by dependent targets.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsList of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo).List of labelsoptional[]
srcsList of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR).List of labelsoptional[]
dataList of additional files to include in the runfiles of the target.List of labelsoptional[]
hdrsHeader and include files for this target. Made available to dependents.List of labelsoptional[]
alwayslinkIf True, any binary that depends (directly or indirectly) on this library will link in all object files, even if some contain no symbols referenced by the binary.BooleanoptionalFalse
definesList of preprocessor definitions to pass to the compiler (e.g., -DDEBUG).List of stringsoptional[]
foptsList of Fortran-specific compiler flags (e.g., -O2, -fopenmp).List of stringsoptional[]
implementation_depsDependencies used only for compiling this library. Their modules and includes are not propagated to dependents, but they are still linked transitively.List of labelsoptional[]
includesInclude directories to add to the compilation search path.List of stringsoptional[]
linkoptsList of additional linker flags to pass to the linker.List of stringsoptional[]
local_definesList of preprocessor definitions that apply only to this target, not to its dependents.List of stringsoptional[]

fortran_test

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

fortran_test(name, deps, srcs, data, hdrs, defines, env, env_inherit, fopts, includes, linkopts,
             linkstatic)

Compiles Fortran source files and links them into a test executable. Can be run with bazel test.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsList of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo).List of labelsoptional[]
srcsList of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR).List of labelsoptional[]
dataList of additional files to include in the runfiles of the target.List of labelsoptional[]
hdrsHeader and include files for this target. Made available to dependents.List of labelsoptional[]
definesList of preprocessor definitions to pass to the compiler (e.g., -DDEBUG).List of stringsoptional[]
envSpecifies additional environment variables to set when the test is executed by bazel test. Values are subject to $(rootpath), $(execpath), location, and "Make variable" substitution.Dictionary: String -> Stringoptional{}
env_inheritSpecifies additional environment variables to inherit from the external environment when the test is executed by bazel test.List of stringsoptional[]
foptsList of Fortran-specific compiler flags (e.g., -O2, -fopenmp).List of stringsoptional[]
includesInclude directories to add to the compilation search path.List of stringsoptional[]
linkoptsList of additional linker flags to pass to the linker.List of stringsoptional[]
linkstaticIf True, prefer linking deps statically. If False, prefer dynamic libraries.BooleanoptionalTrue

fortran_toolchain

load("@rules_fortran//fortran:defs.bzl", "fortran_toolchain")

fortran_toolchain(name, cc_toolchain, compile_flag, compile_flags, compiler, fortran, link_flags,
                  module_dir_flag, module_search_flag, object_extension, output_flag)

Defines a Fortran toolchain that provides the Fortran compiler and compilation/linking flags.

Example:

load("@rules_fortran//fortran:fortran_toolchain.bzl", "fortran_toolchain")

fortran_toolchain(
    name = "gfortran_toolchain",
    compiler = "gfortran",
    fortran = "@gcc_toolchain//:bin/gfortran",
    compile_flags = ["-O2"],
    link_flags = ["-lgfortran"],
)

Register the resulting toolchain with:

```python
register_toolchains("//:gfortran_toolchain")

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
cc_toolchainThe C++ toolchain to use for linking. If not provided, will attempt to find one from the default C++ toolchain.LabeloptionalNone
compile_flagFlag to compile without linking.Stringoptional"-c"
compile_flagsAdditional compiler flags to use when compiling Fortran source files.List of stringsoptional[]
compilerIdentifier for the Fortran compiler (e.g. gfortran, flang, ifx). Used by config_settings at //fortran/compiler/ for select().Stringoptional""
fortranThe Fortran compiler executable (e.g., gfortran).LabeloptionalNone
link_flagsAdditional linker flags to use when linking Fortran executables and libraries (e.g., -lgfortran).List of stringsoptional[]
module_dir_flagFlag to specify the module output directory. Varies by compiler: -J (gfortran), -module-dir (LLVM flang), -module (ifx), -mdir (nagfor).Stringoptional"-J"
module_search_flagFlag to add a module/include search path.Stringoptional"-I"
object_extensionFile extension for compiled object files (e.g. .o on Unix, .obj on Windows).Stringoptional".o"
output_flagFlag to specify the output file.Stringoptional"-o"