Goose supports a plugin system for running custom transpilers or code generators before compilation. This lets you use tools like flex, bison, or any command that produces .c files from other file types.
Define plugins in the plugins section of goose.yaml. Each plugin has a name, a file extension to match, and a command to run:
plugins:
lex:
ext: ".l"
command: "flex"
yacc:
ext: ".y"
command: "bison -d"| Field | Required | Description |
|---|---|---|
ext |
yes | File extension to match (including the dot) |
command |
yes | Shell command to invoke on each matching file |
When you run goose build (or goose run, goose test), the build pipeline:
- Scans
src_dirrecursively for files matching each plugin's extension - Runs the command on each file:
command 'input.l' > 'build/gen/input.c' - Places generated
.cfiles inbuild/gen/ - Compiles the generated files alongside your regular source files
- Adds
build/gen/to the include path so generated headers are accessible
A project using flex for lexing and bison for parsing:
myparser/
goose.yaml
src/
main.c
lexer.l
parser.y
project:
name: "myparser"
version: "0.1.0"
build:
cflags: "-Wall -std=c11"
includes:
- "src"
plugins:
lex:
ext: ".l"
command: "flex"
yacc:
ext: ".y"
command: "bison -d"
dependencies:Running goose build will:
- Find
src/lexer.land runflex 'src/lexer.l' > 'build/gen/lexer.c' - Find
src/parser.yand runbison -d 'src/parser.y' > 'build/gen/parser.c' - Compile
src/main.c,build/gen/lexer.c, andbuild/gen/parser.ctogether
Any command that reads a source file and writes C to stdout can be a plugin. For example, a script that generates C from a template format:
plugins:
tmpl:
ext: ".tmpl"
command: "./tools/tmplgen"The command is invoked as:
./tools/tmplgen 'src/page.tmpl' > 'build/gen/page.c'- Plugin transpilation runs before the main compilation step
- Generated files live in
build/gen/and are cleaned withgoose clean - The
build/gen/directory is automatically added to the include path - Plugins are processed in the order they appear in
goose.yaml - Each plugin can match multiple files -- all are transpiled
- Up to 16 plugins can be defined (
MAX_PLUGINS)