# F2PY frontend refactoring study
## Non compilation flags
- Proposal to create a service file to move all the helper functions to.
- change `check_dir` function to create directory if not exists just like f2py2e
- auxfuncs.options only uses verbose setting. Refactor it and shift it to service
- What about `--coutput` and `--f2py-wrapper-output`
- `-m` Flag operation
- C wrapper is produced
- Check differences - https://www.diffchecker.com/LdaMxbQw
- I suspect its due to default values of some flags or some missing flags
- ~~`-h` is still buggy~~
- Bug was solved by passing correct module name. If the user doesn't provide a module name, then pass it as null to core functions.
- Bug is caused in calling `crackfortran.py(3301)crackfortran()`
- `postlist = postcrack(grouplist[0])`
- Do I have to support only the flags you have added in the parser? Some of them are missing like `--f2py-wrapper-output`, `--skip-empty-wrappers`
## Compilation flags `-c`
`run_compile` using distutils is entirely different from run_main. It just segregates flags and sends entire data to `np.distutils` as seen from [this line](https://github.com/numpy/numpy/blob/fa5bffc454fbafacfb3298ac30ea335635b54d07/numpy/f2py/f2py2e.py#L656) where it is handled internally. I will try to emulate that.
Currently the aim is to generate [ext_args](https://github.com/numpy/numpy/blob/fa5bffc454fbafacfb3298ac30ea335635b54d07/numpy/f2py/f2py2e.py#L637) but I am stuck on how to generate [f2py_flags](https://github.com/numpy/numpy/blob/main/numpy/f2py/f2py2e.py#L533-L549). `f2py_flags` is just a subset of `sys.argv` (for ex: `['--no-wrap-functions', '--lower', '--debug-capi', '--quiet', '--skip-empty-wrappers', 'only:', 'FIB', ':']`) and its directly passed to [distutils](https://github.com/numpy/numpy/blob/fa5bffc454fbafacfb3298ac30ea335635b54d07/numpy/f2py/f2py2e.py#L644). These options are later passed to `run_main()` [seen here](https://github.com/numpy/numpy/blob/main/numpy/distutils/command/build_src.py#L521-L567).
Just copying what `f2py2e` does will result in a bad design. We would need to reconstruct a part of arguments and send it in as `f2py_options`. **Need advice on what should I do?**
Also, run_compile edits `sys.argv` array [here](https://github.com/numpy/numpy/blob/main/numpy/f2py/f2py2e.py#L657-L667) which is processed later in distutils [here](https://github.com/pypa/distutils/blob/main/distutils/core.py#L131-L134). An example of `sys.argv` array at this point:-
```bash=
['/Users/namami/mambaforge/envs/numpy-dev/bin/f2py', '--verbose', '--quiet', 'build', '--build-temp', '/var/folders/jf/h7p7323j6yx6pg1q5h5vlpn80000gn/T/tmp20n0bg_e', '--build-base', '/var/folders/jf/h7p7323j6yx6pg1q5h5vlpn80000gn/T/tmp20n0bg_e', '--build-platlib', '.', '--disable-optimization']
```
**There are two options:-**
1. Imitate `run_compile()` with distutils. Will take less time but will result in a bad design.
2. Understand how `run_compile()` uses distutils and redesign it. It will be a detour and may cost us lot of time.
Studying [run_compile()](https://github.com/numpy/numpy/blob/fa5bffc454fbafacfb3298ac30ea335635b54d07/numpy/f2py/f2py2e.py#L505)
- Default build directory for compilation and non compilation flags are different. Non compilation flags use current dir, while `-c` use a temp dir.
- `-Dmacro=val` is supported as you can see [here](https://github.com/numpy/numpy/blob/fa5bffc454fbafacfb3298ac30ea335635b54d07/numpy/f2py/f2py2e.py#L622).
- -U is used then probably to undefine macros
- Still need to understand this usage [sysinfo_flags](https://github.com/numpy/numpy/blob/main/numpy/f2py/f2py2e.py#L647-L654)
- tempfile.mkdtemp() [does not delete created tempdirs](https://docs.python.org/3/library/tempfile.html#tempfile.mkdtemp). **Remember** to delete manually
- Use context manager
- numpy/numpy/f2py/tests/util.py
**Doubts**
- `f2pyarg` does not support [-include\<header\> ](https://github.com/numpy/numpy/blob/fa5bffc454fbafacfb3298ac30ea335635b54d07/numpy/f2py/f2py2e.py#L86), should I add support?
- argparse can't handle it. It needs to be like `-include <header>`. We should start deprecation.
- [This part](https://github.com/numpy/numpy/blob/main/numpy/f2py/f2py2e.py#L562-L577) tries to map compiler family. But the mapping dictionary is empty. Should we remove it?
- What is [this piece of code](https://github.com/numpy/numpy/blob/main/numpy/f2py/f2py2e.py#L632-L634) supposed to do?
- Argparse won't parse arguments correctly if the order is changed.
**TODO**
- [x] Proper build directory parser
- [ ] `.pyf.src` segregation
- [ ] Typing implementation
- [ ] Documentation
-- Example `ext_args`:
```python=
{'define_macros': [('debug', None)],
'extra_objects': [], # Object files, .o, .a, etc
'f2py_options': ['-includestdio.h',
'--debug-capi',
'--quiet',
'skip:',
'FIB',
':',
'include-paths',
'path1:path2:...'],
'include_dirs': ['~/bin/'], # -I
'libraries': [], #-l
'library_dirs': [], #-L
'name': 'untitled',
'sources': ['fibfortran.f'],
'undef_macros': ['unbug']} #-U
```
TODO:
1) Add doc in reference guide regarding CLI was redesigned, add link to your blog posts.