diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2023-12-28 19:57:02 +0100 | 
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2023-12-28 19:57:02 +0100 | 
| commit | 44c74f36336a62dd0e2aeeefbc53ac4ea2a5d200 (patch) | |
| tree | d631e6c4a5a9ba270e657a5a2c44aaa92a6976a4 /README.md | |
| parent | 3d3ed71d3e9930810015bcebf02b562892e251c0 (diff) | |
Add an example to the README
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 47 | 
1 files changed, 47 insertions, 0 deletions
| @@ -15,4 +15,51 @@ simply aims to be a set of potentially useful functions and macros to  make writing a build script in C a bit easier.  If there is functionality  you are missing, then add it.  You’re a programmer aren’t you? +All functions and macros are documented in cbs.h. +  CBS is very much inspired by Tsoding’s ‘Nob’. + + +## Example + +Assuming you have a source file `my-file.c` — you can compile this build +script (called `build.c` for example) with `cc build.c` to bootstrap — +and then run `./a.out` to build the `my-file` binary linked against the +liblux library. + +If you make any modifications to the build script *or* to the cbs.h +header, there is no need to manually recompile — the script will rebuild +itself. + +```c +#include "cbs.h" + +#define needs_rebuild(dst, src) (!fexists(dst) || fmdolder(dst, src)) + +#define CC     "cc" +#define CFLAGS "-Wall", "-Wextra", "-Werror", "-O3" +#define TARGET "my-file" + +int +main(int argc, char **argv) +{ +	int ec; +	struct cmd cmd = {0}; + +	cbsinit(argc, argv); +	rebuild(); + +	if (!needs_rebuild(TARGET, TARGET ".c")) +		return EXIT_SUCCESS; + +	cmdadd(&cmd, "cc"); +	if (!pcquery(&cmd, "liblux", PKGC_LIBS | PKGC_CFLAGS)) +		cmdadd(&cmd, "-llux"); +	cmdadd(&cmd, CFLAGS, "-o", TARGET, TARGET ".c"); +	cmdput(cmd); +	if ((ec = cmdexec(cmd)) != EXIT_SUCCESS) +		diex("%s failed with exit-code %d", *cmd.argv, ec); + +	return EXIT_SUCCESS; +} +``` |