changeset 107:62aeb864093f

molko-bcc: initial import
author David Demelier <markand@malikania.fr>
date Wed, 01 Apr 2020 20:05:00 +0200
parents ed1a6bb02a78
children 5e38f88cb9ab
files .hgignore Makefile src/core/theme.c tools/molko-bcc.c
diffstat 4 files changed, 211 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Apr 01 12:10:00 2020 +0200
+++ b/.hgignore	Wed Apr 01 20:05:00 2020 +0200
@@ -12,10 +12,10 @@
 \.d$
 \.o$
 \.a$
-^molko(\.exe)?$
 ^examples/example-inventory(\.exe)?$
 ^examples/example-message(\.exe)?$
 ^examples/example-sound(\.exe)?$
+^molko(\.exe)?$
 ^tests/test-color(\.exe)?$
 ^tests/test-error(\.exe)?$
 ^tests/test-inventory(\.exe)?$
@@ -23,6 +23,7 @@
 ^tests/test-panic(\.exe)?$
 ^tests/test-save(\.exe)?$
 ^tests/test-script(\.exe)?$
+^tools/molko-bcc(\.exe)?$
 ^tools/molko-map(\.exe)?$
 
 # doxygen stuff.
--- a/Makefile	Wed Apr 01 12:10:00 2020 +0200
+++ b/Makefile	Wed Apr 01 20:05:00 2020 +0200
@@ -18,14 +18,29 @@
 
 .POSIX:
 
+#
+# User options begin here.
+# -------------------------------------------------------------------
+#
+# CC:           C compiler
+# AR:           Archiver
+# CFLAGS:       General C flags
+# LDFLAGS:      General linker flags
+#
+# PREFIX:       Installation prefix
+# BINDIR:       Where to install binaries
+# SHAREDIR:     Where to install resources
+#
+
 CC=             cc
 AR=             ar
 CFLAGS=         -O0 -std=c11 -g
-LDFLAGS=        -lm
-# Use this instead to build a release.
-# CFLAGS=         -O3 -DNDEBUG -std=c11 -Wall -Wextra
-PROG=           molko
-LIB=            libmolko.a
+
+PREFIX=         /usr/local
+BINDIR=         ${PREFIX}/bin
+SHAREDIR=       ${PREFIX}/share
+
+# End of user options.
 
 SQLITE_SRC=     extern/libsqlite/sqlite3.c
 SQLITE_OBJ=     extern/libsqlite/sqlite3.o
@@ -76,10 +91,6 @@
 ADV_OBJS=       ${ADV_SRCS:.c=.o}
 ADV_DEPS=       ${ADV_SRCS:.c=.d}
 
-PREFIX=         /usr/local
-BINDIR=         ${PREFIX}/bin
-SHAREDIR=       ${PREFIX}/share
-
 SDL_CFLAGS=     `pkg-config --cflags sdl2 SDL2_image SDL2_mixer SDL2_ttf`
 SDL_LDFLAGS=    `pkg-config --libs sdl2 SDL2_image SDL2_mixer SDL2_ttf`
 
@@ -105,7 +116,8 @@
 TESTS_OBJS=     ${TESTS:.c=.o}
 TESTS_DEPS=     ${TESTS:.c=.d}
 
-TOOLS=          tools/molko-map.c
+TOOLS=          tools/molko-map.c                       \
+                tools/molko-bcc.c
 TOOLS_PRGS=     ${TOOLS:.c=}
 TOOLS_DEPS=     ${TOOLS:.c=.d}
 
@@ -117,11 +129,12 @@
                 -Iextern/libgreatest \
                 -Isrc/core \
                 -Isrc/adventure
+MY_LDFLAGS=     -lm
 
 .SUFFIXES:
 .SUFFIXES: .o .c
 
-all: ${PROG}
+all: molko
 
 -include ${CORE_DEPS} ${ADV_DEPS} ${TESTS_DEPS} ${TOOLS_DEPS}
 
@@ -129,10 +142,12 @@
 	${CC} ${MY_CFLAGS} ${SDL_CFLAGS} ${CFLAGS} -MMD -c $< -o $@
 
 .c:
-	${CC} ${MY_CFLAGS} -o $@ ${CFLAGS} $< ${LIB} ${SQLITE_LIB} ${SDL_LDFLAGS} ${LDFLAGS}
+	${CC} ${MY_CFLAGS} -o $@ ${CFLAGS} $< libmolko.a ${SQLITE_LIB} ${SDL_LDFLAGS} ${MY_LDFLAGS} ${LDFLAGS}
 
 .o:
-	${CC} -o $@ $< ${LIB} ${SQLITE_LIB} ${SDL_LDFLAGS} ${LDFLAGS}
+	${CC} -o $@ $< libmolko.a ${SQLITE_LIB} ${SDL_LDFLAGS} ${MY_LDFLAGS} ${LDFLAGS}
+
+# {{{ Core
 
 ${SQLITE_OBJ}: ${SQLITE_SRC}
 	${CC} ${CFLAGS} ${SQLITE_FLAGS} -c ${SQLITE_SRC} -o $@
@@ -140,31 +155,59 @@
 ${SQLITE_LIB}: ${SQLITE_OBJ}
 	${AR} -rc $@ ${SQLITE_OBJ}
 
-${LIB}: ${CORE_OBJS} ${SQLITE_LIB}
+libmolko.a: ${CORE_OBJS} ${SQLITE_LIB}
 	${AR} -rc $@ ${CORE_OBJS}
 
-${PROG}: ${LIB} ${ADV_OBJS}
-	${CC} -o $@ ${ADV_OBJS} ${LIB} ${SQLITE_LIB} ${SDL_LDFLAGS} ${LDFLAGS}
+# }}}
+
+# {{{ Molko's Adventure
 
-${TESTS_OBJS}: ${LIB}
+molko: libmolko.a ${ADV_OBJS}
+	${CC} -o $@ ${ADV_OBJS} libmolko.a ${SQLITE_LIB} ${SDL_LDFLAGS} ${MY_LDFLAGS} ${LDFLAGS}
 
-${EXAMPLES_OBJS}: ${LIB}
+# }}}
+
+# {{{ Examples
+
+${EXAMPLES_OBJS}: libmolko.a
 
 examples: ${EXAMPLES_PRGS}
 
+# }}}
+
+# {{{ Tests
+
+${TESTS_OBJS}: libmolko.a
+
 tests: ${TESTS_PRGS}
 	for t in ${TESTS_PRGS}; do ./$$t; done
 
+# }}}
+
+# {{{ Tools
+
 tools: ${TOOLS_PRGS}
 
 # Custom rule: does not depend on anything else than jansson.
 tools/molko-map: tools/molko-map.c
-	${CC} ${MY_CFLAGS} -o $@ tools/molko-map.c ${CFLAGS} ${JANSSON_CFLAGS} ${JANSSON_LDFLAGS}
+	${CC} ${MY_CFLAGS} -o $@ tools/molko-map.c ${CFLAGS} ${JANSSON_CFLAGS} ${JANSSON_LDFLAGS} ${LDFLAGS}
+
+# Custom rule: does not depend on anything.
+tools/molko-bcc: tools/molko-bcc.c
+	${CC} ${MY_CFLAGS} -o $@ tools/molko-bcc.c ${LDFLAGS}
+
+# }}}
+
+# {{{ Docs
 
 doxygen:
 	doxygen doxygen/Doxyfile
 
-everything: ${PROG} ${EXAMPLES_PRGS} ${TOOLS_PRGS} ${TESTS_PRGS}
+# }}}
+
+# {{{ Misc
+
+everything: molko ${EXAMPLES_PRGS} ${TOOLS_PRGS} ${TESTS_PRGS}
 
 install:
 	mkdir -p ${DESTDIR}${BINDIR}
@@ -173,14 +216,44 @@
 	mkdir -p ${DESTDIR}${SHAREDIR}/molko
 	cp -R assets/* ${DESTDIR}${SHAREDIR}/molko
 
-clean:
-	rm -rf doxygen/html doxygen/man
+clean-core:
 	rm -f ${SQLITE_OBJ} ${SQLITE_LIB}
-	rm -f ${LIB} ${PROG}
+	rm -f libmolko.a
 	rm -f ${CORE_OBJS} ${CORE_DEPS}
-	rm -f ${ADV_OBJS} ${ADV_DEPS}
+
+clean-doxygen:
+	rm -rf doxygen/html doxygen/man
+
+clean-examples:
+	rm -f ${EXAMPLES_PRGS} ${EXAMPLES_OBJS} ${EXAMPLES_DEPS}
+
+clean-molko:
+	rm -f molko ${ADV_OBJS} ${ADV_DEPS}
+
+clean-tests:
 	rm -f ${TESTS_PRGS} ${TESTS_OBJS} ${TESTS_DEPS}
-	rm -f ${EXAMPLES_PRGS} ${EXAMPLES_OBJS} ${EXAMPLES_DEPS}
+
+clean-tools:
 	rm -f ${TOOLS_PRGS} ${TOOLS_DEPS}
 
-.PHONY: all clean doxygen everything examples tests tools
+clean: clean-core \
+       clean-doxygen \
+       clean-examples \
+       clean-molko \
+       clean-tests \
+       clean-tools
+
+# }}}
+
+.PHONY: all \
+        clean-core \
+        clean-doxygen \
+        clean-examples \
+        clean-molko \
+        clean-tests \
+        clean-tools \
+        doxygen \
+        everything \
+        examples \
+        tests \
+        tools
--- a/src/core/theme.c	Wed Apr 01 12:10:00 2020 +0200
+++ b/src/core/theme.c	Wed Apr 01 20:05:00 2020 +0200
@@ -23,7 +23,7 @@
 #include "font.h"
 #include "frame.h"
 #include "label.h"
-#include "math.h"
+#include "maths.h"
 #include "painter.h"
 #include "panic.h"
 #include "texture.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/molko-bcc.c	Wed Apr 01 20:05:00 2020 +0200
@@ -0,0 +1,109 @@
+/*
+ * molko-bcc.c -- simple binary compiler
+ *
+ * Copyright (c) 2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdnoreturn.h>
+#include <string.h>
+#include <unistd.h>
+
+static bool fstatic;
+
+noreturn static void
+usage(void)
+{
+	fprintf(stderr, "usage: molko-bcc [-s] input varname\n");
+	exit(1);
+}
+
+noreturn static void
+die(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	fputs("abort: ", stderr);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	exit(1);
+}
+
+static void
+process(const char *input, const char *variable)
+{
+	FILE *fp;
+	int ch, idx = 0;
+
+	if (strcmp(input, "-") == 0)
+		fp = stdin;
+	else if (!(fp = fopen(input, "rb")))
+		die("%s: %s\n", input, strerror(errno));
+
+	if (fstatic)
+		printf("static ");
+
+	printf("const unsigned char %s[] = {\n", variable);
+
+	while ((ch = fgetc(fp)) != EOF) {
+		if (idx == 0)
+			putchar('\t');
+
+		printf("0x%02x", (unsigned char)ch);
+
+		if (!feof(fp))
+			printf(", ");
+
+		if (++idx == 4) {
+			idx = 0;
+			putchar('\n');
+		}
+	}
+
+	if (idx != 0)
+		printf("\n");
+
+	puts("};");
+	fclose(fp);
+}
+
+int
+main(int argc, char **argv)
+{
+	int ch;
+
+	while ((ch = getopt(argc, argv, "s")) != -1) {
+		switch (ch) {
+		case 's':
+			fstatic = true;
+			break;
+		default:
+			break;
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+
+	if (argc < 2)
+		usage();
+
+	process(argv[0], argv[1]);
+}