|
|
|
|
|
|
|
This tutorial teaches you how to add a new compiler flag called `-enable-foo` to the compiler.
|
|
|
|
|
|
|
|
# Step 1
|
|
|
|
|
|
|
|
First add a global variable where your flag will be stored into.
|
|
|
|
This is done in `src/libsac2c/global/globals.mac`.
|
|
|
|
|
|
|
|
Just add a macro like this:
|
|
|
|
|
|
|
|
```c
|
|
|
|
GLOBAL (bool, enable_foo, FALSE, xfree_dummy)
|
|
|
|
// ^-type ^-name ^-default ^-free-function
|
|
|
|
```
|
|
|
|
|
|
|
|
Just look at what is already there.
|
|
|
|
For a string argument you need:
|
|
|
|
|
|
|
|
```c
|
|
|
|
GLOBAL (char *, enable_foo, NULL, xfree_char_ptr, )
|
|
|
|
```
|
|
|
|
|
|
|
|
# Step 2
|
|
|
|
|
|
|
|
Then you have to add the flag to the `AnalyseCommandlineSac2c` function in `src/libsac2c/global/options.c`.
|
|
|
|
This function is responsible for parsing the command line arguments and setting the global variables accordingly.
|
|
|
|
|
|
|
|
Again just look at what is already there and extend this tutorial if you learned anything noteworthy.
|
|
|
|
|
|
|
|
There two simple macros you can use:
|
|
|
|
|
|
|
|
- `ARGS_FLAG`
|
|
|
|
- `ARGS_OPTION`
|
|
|
|
|
|
|
|
There is also `ARGS_OPTION_BEGIN` and friends for one option that takes multiple flags like `-check pc`.
|
|
|
|
Read `getoptions.h` for more information about these or look at the code for `-check`.
|
|
|
|
|
|
|
|
## ARGS_FLAG
|
|
|
|
|
|
|
|
With `ARGS_FLAG` the compiler option is just a boolean.
|
|
|
|
The macro takes a name and an expression.
|
|
|
|
The macro will run the expression if the option is passed.
|
|
|
|
|
|
|
|
```c
|
|
|
|
ARGS_FLAG ("enable-foo", global.enable_foo = TRUE);
|
|
|
|
```
|
|
|
|
|
|
|
|
## ARGS_OPTION
|
|
|
|
|
|
|
|
This macro also takes a name and an expression.
|
|
|
|
But with the expression of `ARGS_OPTION` you also can access the `ARG` variable.
|
|
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
ARGS_OPTION ("enable-foo", global.enable_foo = STRcpy (ARG));
|
|
|
|
``` |