From 44c74f36336a62dd0e2aeeefbc53ac4ea2a5d200 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 28 Dec 2023 19:57:02 +0100 Subject: Add an example to the README --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index d65ff49..e256438 100644 --- a/README.md +++ b/README.md @@ -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; +} +``` -- cgit v1.2.3