aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/main.c
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-27 02:29:54 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-27 02:43:59 +0100
commit8cac2c3e3a61b64b9a9855dec413239bcec7f9d2 (patch)
tree6240cdabaa0352f08d62bbfa6de7c53f5a3f1063 /src/blogc-make/main.c
parent7bf68b0b617fb3ffa86f38fe06a49786883037f4 (diff)
downloadblogc-8cac2c3e3a61b64b9a9855dec413239bcec7f9d2.tar.gz
blogc-8cac2c3e3a61b64b9a9855dec413239bcec7f9d2.tar.bz2
blogc-8cac2c3e3a61b64b9a9855dec413239bcec7f9d2.zip
make: implemented a build tool for blogc
so, this is basically what happens when you don't have anything better to do in the christmas weekend. most of this code was written in the last 2 or 3 days. i'd like to thank the chivas brothers, the weather and my psychological problems for this achievement. on a serious note, this tool still needs a man page, more tests, and the aws lambda function should be adapted to use it instead of (or together with) make/busybox. also, while talking about aws lambda, this tool can be nicely embedded into the blogc binary, to produce a single "small" static binary for usage in lambda ;)
Diffstat (limited to 'src/blogc-make/main.c')
-rw-r--r--src/blogc-make/main.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/blogc-make/main.c b/src/blogc-make/main.c
new file mode 100644
index 0000000..bc8c926
--- /dev/null
+++ b/src/blogc-make/main.c
@@ -0,0 +1,125 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include <locale.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../common/error.h"
+#include "../common/utils.h"
+#include "ctx.h"
+#include "rules.h"
+
+
+static void
+print_help(void)
+{
+ printf(
+ "usage:\n"
+ " blogc-make [-h] [-v] [-V] [-f FILE] [RULE ...]\n"
+ " - A simple build tool for blogc.\n"
+ "\n"
+ "positional arguments:\n"
+ " RULE build rule(s) to run (default: all)\n"
+ "\n"
+ "optional arguments:\n"
+ " -h show this help message and exit\n"
+ " -v show version and exit\n"
+ " -V be verbose when executing commands\n"
+ " -f FILE settings file (default: settings.ini)\n");
+ bm_rule_print_help();
+}
+
+
+static void
+print_usage(void)
+{
+ printf("usage: blogc-make [-h] [-v] [-V] [-f FILE] [RULE ...]\n");
+}
+
+
+int
+#ifdef MAKE_EMBEDDED
+bm_main(int argc, char **argv)
+#else
+main(int argc, char **argv)
+#endif
+{
+ setlocale(LC_ALL, "");
+
+ int rv = 0;
+ bc_error_t *err = NULL;
+
+ bc_slist_t *rules = NULL;
+ bool verbose = false;
+ char *settings_file = NULL;
+ bm_ctx_t *ctx = NULL;
+
+ for (unsigned int i = 1; i < argc; i++) {
+ if (argv[i][0] == '-') {
+ switch (argv[i][1]) {
+ case 'h':
+ print_help();
+ goto cleanup;
+ case 'v':
+ printf("%s\n", PACKAGE_STRING);
+ goto cleanup;
+ case 'V':
+ verbose = true;
+ break;
+ case 'f':
+ if (argv[i][2] != '\0')
+ settings_file = bc_strdup(argv[i] + 2);
+ else if (i + 1 < argc)
+ settings_file = bc_strdup(argv[++i]);
+ break;
+#ifdef MAKE_EMBEDDED
+ case 'm':
+ // no-op, for embedding into blogc binary.
+ break;
+#endif
+ default:
+ print_usage();
+ fprintf(stderr, "blogc-make: error: invalid argument: "
+ "-%c\n", argv[i][1]);
+ rv = 3;
+ goto cleanup;
+ }
+ }
+ else {
+ rules = bc_slist_append(rules, bc_strdup(argv[i]));
+ }
+ }
+
+ if (rules == NULL) {
+ rules = bc_slist_append(rules, bc_strdup("all"));
+ }
+
+ ctx = bm_ctx_new(settings_file ? settings_file : "settings.ini", &err);
+ if (err != NULL) {
+ bc_error_print(err, "blogc-make");
+ rv = 3;
+ goto cleanup;
+ }
+
+ rv = bm_rule_executor(ctx, rules, verbose);
+
+cleanup:
+
+ bc_slist_free_full(rules, free);
+ free(settings_file);
+ bm_ctx_free(ctx);
+ bc_error_free(err);
+
+ return rv;
+}