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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | List of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo). | List of labels | optional | [] |
| srcs | List of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR). | List of labels | optional | [] |
| data | List of additional files to include in the runfiles of the target. | List of labels | optional | [] |
| hdrs | Header and include files for this target. Made available to dependents. | List of labels | optional | [] |
| defines | List of preprocessor definitions to pass to the compiler (e.g., -DDEBUG). | List of strings | optional | [] |
| env | Specifies 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 -> String | optional | {} |
| fopts | List of Fortran-specific compiler flags (e.g., -O2, -fopenmp). | List of strings | optional | [] |
| includes | Include directories to add to the compilation search path. | List of strings | optional | [] |
| linkopts | List of additional linker flags to pass to the linker. | List of strings | optional | [] |
| linkstatic | If True, prefer linking deps statically. If False, prefer dynamic libraries. | Boolean | optional | True |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | List of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo). | List of labels | optional | [] |
| srcs | List of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR). | List of labels | optional | [] |
| data | List of additional files to include in the runfiles of the target. | List of labels | optional | [] |
| hdrs | Header and include files for this target. Made available to dependents. | List of labels | optional | [] |
| alwayslink | If 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. | Boolean | optional | False |
| defines | List of preprocessor definitions to pass to the compiler (e.g., -DDEBUG). | List of strings | optional | [] |
| fopts | List of Fortran-specific compiler flags (e.g., -O2, -fopenmp). | List of strings | optional | [] |
| implementation_deps | Dependencies used only for compiling this library. Their modules and includes are not propagated to dependents, but they are still linked transitively. | List of labels | optional | [] |
| includes | Include directories to add to the compilation search path. | List of strings | optional | [] |
| linkopts | List of additional linker flags to pass to the linker. | List of strings | optional | [] |
| local_defines | List of preprocessor definitions that apply only to this target, not to its dependents. | List of strings | optional | [] |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | List of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo). | List of labels | optional | [] |
| srcs | List of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR). | List of labels | optional | [] |
| data | List of additional files to include in the runfiles of the target. | List of labels | optional | [] |
| hdrs | Header and include files for this target. Made available to dependents. | List of labels | optional | [] |
| alwayslink | If 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. | Boolean | optional | False |
| defines | List of preprocessor definitions to pass to the compiler (e.g., -DDEBUG). | List of strings | optional | [] |
| fopts | List of Fortran-specific compiler flags (e.g., -O2, -fopenmp). | List of strings | optional | [] |
| implementation_deps | Dependencies used only for compiling this library. Their modules and includes are not propagated to dependents, but they are still linked transitively. | List of labels | optional | [] |
| includes | Include directories to add to the compilation search path. | List of strings | optional | [] |
| linkopts | List of additional linker flags to pass to the linker. | List of strings | optional | [] |
| local_defines | List of preprocessor definitions that apply only to this target, not to its dependents. | List of strings | optional | [] |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | List of dependencies. Can be C++ targets (CcInfo) or Fortran targets (CcInfo, FortranInfo). | List of labels | optional | [] |
| srcs | List of Fortran source files to compile (.f, .F, .f90, .F90, .for, .FOR). | List of labels | optional | [] |
| data | List of additional files to include in the runfiles of the target. | List of labels | optional | [] |
| hdrs | Header and include files for this target. Made available to dependents. | List of labels | optional | [] |
| defines | List of preprocessor definitions to pass to the compiler (e.g., -DDEBUG). | List of strings | optional | [] |
| env | Specifies 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 -> String | optional | {} |
| env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | [] |
| fopts | List of Fortran-specific compiler flags (e.g., -O2, -fopenmp). | List of strings | optional | [] |
| includes | Include directories to add to the compilation search path. | List of strings | optional | [] |
| linkopts | List of additional linker flags to pass to the linker. | List of strings | optional | [] |
| linkstatic | If True, prefer linking deps statically. If False, prefer dynamic libraries. | Boolean | optional | True |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| cc_toolchain | The C++ toolchain to use for linking. If not provided, will attempt to find one from the default C++ toolchain. | Label | optional | None |
| compile_flag | Flag to compile without linking. | String | optional | "-c" |
| compile_flags | Additional compiler flags to use when compiling Fortran source files. | List of strings | optional | [] |
| compiler | Identifier for the Fortran compiler (e.g. gfortran, flang, ifx). Used by config_settings at //fortran/compiler/ for select(). | String | optional | "" |
| fortran | The Fortran compiler executable (e.g., gfortran). | Label | optional | None |
| link_flags | Additional linker flags to use when linking Fortran executables and libraries (e.g., -lgfortran). | List of strings | optional | [] |
| module_dir_flag | Flag to specify the module output directory. Varies by compiler: -J (gfortran), -module-dir (LLVM flang), -module (ifx), -mdir (nagfor). | String | optional | "-J" |
| module_search_flag | Flag to add a module/include search path. | String | optional | "-I" |
| object_extension | File extension for compiled object files (e.g. .o on Unix, .obj on Windows). | String | optional | ".o" |
| output_flag | Flag to specify the output file. | String | optional | "-o" |