Here project also consists of 2 source files (utils.c
and main.c) and one header (utils.h).
Buildscript rebuilds both source files in parallel and also caches
objects that do not need to be changed.
This example is much more complex. It automatically detects all files
that have .c extension and are placed somewhere in
directory tree starting from src.
Code inside of function main is much simpler now. It
prepares link_cmd variable, then walks the directory tree,
then waits for all processes, then runs link command and then cleans all
allocated memory.
Function walker is much more interesting - this is where
all magic happens. This function will be called for each file and
directory by cbuild_dir_walk.
This function consists of 4 parts - preparation, generation of output
path, check and command call.
- Preparation: This part constructs string view out
of
args.pathand then checks if suffix is.c, so this filters-out files that are not C source files. - Output path generation: This part appends
.oto source path and then replaces all slashes with dots. This will allocate from CBuild’s temporary allocator (arena), and it does this in loop, but such loops will be quite small and that arena has size of 8M, so cleaning it on each iteration is not that important. - Check: This part consists of call to
cbuild_compare_mtimeand 3ifchecks. If this function returns0it means that output file is newer than input one. If it returns something lower than0it means that error has happened. If it returns value greater than0it means that input is newer than output, so recompilation is needed. - Command call: If previous check decided that
recompilation is needed it runs new command and appends to
procsarray, same as previous example.
Object files to linker command is appended by walker
function, because their paths are known only there. This is also a
reason why temporary allocator can not be cleaned during loop
execution.