This repository showcases a sample Fortran Project.
- Linux OS or WSL2 via Windows
- VS Code installation
- GFortran or oneAPI Fortran compiler
- Modern Fortran extension installation
-
Clone the repository:
git clone git@github.com:MatthAlex/fortran-project-template.git
-
Load compiler modules (if on a compute cluster):
module load gcc # or module load intel
-
Create and activate a Python environment:
uv venv # to utilize uv python3 -m venv .venv # or by using the internal venv library source .venv/bin/activate # common for both
Remember to activate your enviroment before runtime or development tasks.
-
Install the runtime and development packages:
uv pip install -r pyproject.toml # or requirements.txt # or pip3 install -r requirements.txt # or pyproject.toml # if needed, add the development packages uv pip install -r requirements-dev.txt
-
Run the tests:
fpm test # by default, it will use gcc/gfortran to compile and run
-
Run the main program:
fpm run
-
Migrate your project:
See a step-by-step guide on how to proceed with migrating your project.
To maintain high code quality standards, the project employs several tools and practices:
- pre-commit checks: These integrate with git to run specified hooks before certain git commands, ensuring tests, linting, and formatting are automatically performed.
- Compile checks with
fpm build
: Ensures that the code compiles correctly. - Automated testing with
fpm test
: Runs the test suite to catch any regressions or issues. - Code formatting with
fprettify
: Ensures consistent code style across the project. - Linting with the Modern Fortran VSCode extension: Provides real-time linting and formatting capabilities.
The Modern Fortran VSCode extension provides real-time linting and formatting capabilities. This ensures consistent code style and catches potential issues as you type.
- Defaults provided in
.vscode/settings.json
and.fortls
: These configurations use the installedgfortran
compiler to lint andfprettify
to format the code. - Manual formatting: Open the command palette with
CTRL + P
, typeformat document
, and select the action to format the code.
VSCode uses the Fortran language server fortls
to provide code completion and other functionality.
pre-commit
integrates with git, running specified hooks before certain git commands. This setup ensures that tests, linting, and formatting are automatically performed, promoting consistent code quality. The git commands integrating pre-commit
are git commit
and git push
. Alternatively, to execute all hooks, run: pre-commit run
.
If any tests or checks fails, the commit (or push) will subsequently fail, allowing to address the problem before retrying to commit your changes.
To manage hooks within your environment:
pre-commit install # Install hooks
pre-commit uninstall # Uninstall hooks
To bypass a hook temporarily, for instance, to address a failed test in a subsequent commit:
SKIP test-fpm git commit -m "Commit message.." # in this case, we ignore the "test-fpm" hook
We generate automated documentation using the ford package. Consult the ford documentation for examples of config files and usage.
To generate and view the documentation, run:
ford ford.md
firefox docs/ford/index.html # Alternatively, use your preferred browser
Specific information about using VS Code for development are given here.
Contributions from the community are welcome. To contribute, either considering opening an issue or pull request with changes, suggestions, or your considerations.
For questions or suggestions, please contact us at email or open an issue.
The project is operating under an MIT license. You are free to use, modify, and distribute the code as needed for your project. Feel free to adapt and customize it to suit your requirements.
-
Install VS Code Extensions:
- Open Extensions (CTRL + Shift + X) and search for "Modern Fortran".
- Install other recommended extensions from the "Recommended" tab (see .vscode/extensions.json).
-
Create a directory for generated modules:
- Create a directory named
.generated_modules
to house allfortls
linter module and object files. This directory will be hidden from VS Code's Explorer (see .vscode/settings.json).
- Create a directory named
-
Review and amend configurations:
- "Modern Fortran" can accommodate several different configurations for the linter, including
gfortran
andifort
,fprettify
orfindent
, etc.
- "Modern Fortran" can accommodate several different configurations for the linter, including
uv
is available to install as a standalone script (always review such scripts before running!), or pip
and other sources. More information can be found in the documentation.
Once installed, to update uv
, simply invoke uv self update
.
# Create a virtual environment
uv venv
source .venv/bin/activate # to activate
# Install dependencies
uv pip install -r pyproject.toml # runtime
uv pip install -r requirements-dev.txt # development
The directory structure of the project is the following.
$ tree -Ia '__pycache__|.git|.pytest_cache|.venv|build|.gen*|ford'
.
├── app # The main program driver resides here
│ └── main.f90
├── docs
│ ├── MIGRATION.md
│ └── VSCODE.md
├── ford.md # FORD config file
├── .fortls # VSCode Modern Fortran config file
├── fpm.toml # Fortran Package Manager config file
├── .fprettify.rc # fprettify config file
├── .gitignore # Git ignore list of files and directories
├── LICENSE
├── .pre-commit-config.yaml # pre-commit config file
├── pyproject.toml # main lock/config file for the runtime environment
├── README.md # you are here!
├── requirements-dev.txt # development packages
├── requirements.txt # mirrors pyproject.toml
├── src # All source code files are placed in here, except main driver
│ ├── first_steps.f90
├── test # All tests are placed in here
│ └── check.f90
└── .vscode # Holds VSCode configs and runtime/debugging tasks
├── extensions.json # simply populates the "Recommended" Extensions tab
└── settings.json # also referred to as "Workspace Settings (JSON)"
This repository takes a lot of inspiration (and actual code) from easy.
fprettify
configuration does not work correctly on VS Code Modern Fortran.--case 1 1 1 1
is not recognized and will delete the text of the currently selected file.
WORKAROUND: Introduced an explicit fprettify config file that will run with pre-commit
and actually format the file according to specs.
[ERROR - 12:14:03 PM] [format] fprettify error output: directory --case 2 2 2 2 does not exist!