Skip to content

Commit

Permalink
Added subcommand interface to the build system
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 4, 2024
1 parent 272f928 commit 42afc38
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions src/make.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
#define CARBON_IMPLEMENTATION
#include "../carbon.h"

#define TESTBIN "./test/carbon"
#define WORKDIR \
"carbon" \
"-" CARBON_EXPAND_AND_QUOTE(CARBON_VERSION_MAJOR) \
"." CARBON_EXPAND_AND_QUOTE(CARBON_VERSION_MINOR) \
"-" CARBON_TARGET_OS \
"-" CARBON_CPU_ARCH

static const char * const help_msg = "usage: %s [SUBCMD]\n"
"Subcommands:\n"
" help display this help\n"
" clean remove previously created build artifacts\n"
" mrproper same as `clean` plus remove this binary\n"
" check only run tests\n"
" examples only build examples\n"
"\n"
"If not provided any subcommand, it runs the full build pipeline.\n"
"\n"
"Report bugs to: <https://github.com/sparky-game/carbon/issues>\n"
"%s homepage: <https://github.com/sparky-game/carbon>\n";

static void call_cmd(const char *cmd) {
if (!system(cmd)) return;
CARBON_ERROR("Unable to run `%s`", cmd);
Expand Down Expand Up @@ -53,16 +67,17 @@ static inline void compress_dir(const char *path) {
}

static void run_tests(void) {
const char *cc_cmd = CARBON_COMPILER " -I . -std=gnu99 -Wall -Wextra -fsanitize=address,undefined test/*.c -o test/carbon";
const char *test_cmd = "./test/carbon -n";
CARBON_INFO(" CCLD test/carbon");
call_cmd(cc_cmd);
CARBON_INFO_COLOR(CARBON_COLOR_YELLOW, "[*] Running tests...");
static const char *test_cmd = TESTBIN " -n";
CARBON_INFO(" CCLD " TESTBIN);
call_cmd(CARBON_COMPILER " -I . -std=gnu99 -Wall -Wextra -fsanitize=address,undefined test/*.c -o " TESTBIN);
CARBON_INFO("+ %s", test_cmd);
call_cmd(test_cmd);
return;
}

static void build_examples(void) {
CARBON_INFO_COLOR(CARBON_COLOR_YELLOW, "[*] Building examples...");
usz files_count = 0;
char **files = carbon_fs_pattern_match("examples/*.c", &files_count);
for (usz i = 0; i < files_count; ++i) {
Expand All @@ -73,31 +88,14 @@ static void build_examples(void) {
}

static inline void clean(void) {
rm_dash_r("test/carbon");
rm_dash_r(TESTBIN);
rm_dash_r(WORKDIR);
rm_dash_r(WORKDIR ".tgz");
const char *delete_cmd = "find examples -type f -executable -delete";
CARBON_INFO("+ %s", delete_cmd);
call_cmd(carbon_string_fmt(delete_cmd));
}

static inline void mrproper(void) {
clean();
rm_dash_r("make");
}

static void handle_args(int argc, char **argv) {
if (argc != 2) return;
if (!carbon_string_cmp(argv[1], "clean")) {
clean();
exit(0);
}
if (!carbon_string_cmp(argv[1], "mrproper")) {
mrproper();
exit(0);
}
}

static void build(void) {
CARBON_INFO(" MKDIR " WORKDIR);
if (!carbon_fs_create_directory(WORKDIR)) {
Expand Down Expand Up @@ -125,10 +123,28 @@ int main(int argc, char **argv) {
CARBON_ERROR("Unable to change CWD to binary's directory");
return 1;
}
handle_args(argc, argv);
CARBON_INFO_COLOR(CARBON_COLOR_YELLOW, "[*] Running tests...");
if (argc == 2 && !carbon_string_cmp(argv[1], "help")) {
CARBON_INFO_RAW(help_msg, argv[0], CARBON_NAME);
return 0;
}
if (argc == 2 && !carbon_string_cmp(argv[1], "clean")) {
clean();
return 0;
}
if (argc == 2 && !carbon_string_cmp(argv[1], "mrproper")) {
clean();
rm_dash_r(argv[0]);
return 0;
}
if (argc == 2 && !carbon_string_cmp(argv[1], "check")) {
run_tests();
return 0;
}
if (argc == 2 && !carbon_string_cmp(argv[1], "examples")) {
build_examples();
return 0;
}
run_tests();
CARBON_INFO_COLOR(CARBON_COLOR_YELLOW, "[*] Building examples...");
build_examples();
CARBON_INFO_COLOR(CARBON_COLOR_YELLOW, "[*] Building and packaging...");
build();
Expand Down

0 comments on commit 42afc38

Please sign in to comment.