# Meson backend for F2PY
### General backend structure for future `f2py` backends
- Create a structure such that backends can be added as a plugin for F2PY
- Compilation isn't the selling point of F2PY.
- So instead of having dedicated file for just building with meson, make a code structure where a supperclass `Backend` will provide the blueprint datamembers and functions which will be overriden by other subclasses dedicated for specific build system.
- Make a *plugin-like* code structure.
- Make a folder within `f2py` -> `f2py/backends`.
- Make a superclass `Backend.py`
- This superclass will provide blueprint for essential features.
- [The list of features](https://hackmd.io/E8XNIbMEQhWt_SbVWM7Pvw#F2PY-compilation-flag-equivalents)
- Would have abstract funtions to load the C wrapper and to build the module.
- `MesonBackend.py` will contain class `MesonBackend` which will initialize the superclass and override the abstract function.
- This code structures ensures more backends can be added as `plugins` in the future.
- The added backends will be accessible by `--backend=<backend>` flag.
- `backends/__init__.py` will contain a dictionary mapping backend strings to respective classes
```python
from meson_plugin import MesonBackend
backends = {
'meson': MesonBackend
}
```
### How will the frontend integrate with backend.
- Modify [generate_files()](https://github.com/NamamiShanker/numpy/blob/f2py_front/numpy/f2py/service.py#L273) function to return path to the created C wrapper file.
- Retreive appropriate backend simply with
```python
from backends import backends
backend = backends[args.backend](...arguments...)
backend.load('path/to/C/wrapper')
backend.build()
```
- Initialize `MesonBackend` object with appropriate arguments.
- Load C wrapper file.
- Build the module in the specified directory.
### How to generate `meson.build` file?
Templating is necessary for generating a working `meson.build` file.
- Using Jinja and other templating engines wasn't an option.
- **Question** Why?
- [name=Rohit] Tempita comes bundled with NumPy or Scipy.. Need to check.
- **Question** How do you check which software comes bundled with a package?
- **Question** Being bundled means it is shipped with the software.. is the `Tempita` library present in SciPy or NumPy, or do you install it as an external dependency?
- [name=Rohit]'s suggestion: However, its unmaintained and your current approach is a lot more canonical, tempita is super old too
- **Question** What do you mean by canonical
### How to call Meson?
Meson can be called in two ways:-
- `from mesonbuild import mesonmain` and call [mesonmain.main(args)](https://github.com/mesonbuild/meson/blob/28d07c31b818d04dede11a3e48a87e5265d53c8f/mesonbuild/mesonmain.py#L259)
- Didn't consider this option because this would require `meson` as a direct dependency of numpy.
- Instead used python `subprocess` to call meson from the command line passing the meson.build file and required options.
- Would need to add dependency check for installed Meson and print corresponding warning when use installs NumPy.
- Security risk.
- **Question** Should we add a check here - [f2py/diagnose.py](https://github.com/numpy/numpy/blob/main/numpy/f2py/diagnose.p)?
- Lets skip it.
## Notes
- numpy.f2py.get_include()
- `--help-distutils` use it to hide link lists
- Lets not release `argparse` F2PY until cutom_target for f77 and f90 is fully incorporated because `f2pyarg` is still undergoing changes as I am attaching Meson backend to it
- [x] Move built module to the root folder
- [name=Rohit] Walk the buildir pattern match .so file and move it to the root.
## What's next for F2PY?
-
- [x] Check if changes are working with SciPy
- [ ] Check failing tests
- [ ] Adding exclusive f77 and f90 compilers and flags support.
- Using `custom_target` and passing corresponding flags.
- [ ] Test meson backend with SciPy
- [ ] Writing tests for building with Meson.
- Check if 0 optimization level triggers Debug mode in Meson.
- Use https://mesonbuild.com/Builtin-options.html#core-options `buildtype`
Detailed blog post explaining different design steps taken and the reasons behind them
## PR
- Add link to design post