From c48f0176412f2c15fbb3cbc6052550959f581da9 Mon Sep 17 00:00:00 2001
From: Tristan Riehs <tristan.riehs@bordeaux-inp.fr>
Date: Sun, 30 Jun 2024 17:00:48 +0900
Subject: Add support for release compilation mode

Displays are not shared libraries, but object files that are part of
the final executable.
---
 src/Makefile.am |  4 +++-
 src/calculer.c  |  2 +-
 src/cli.c       | 12 ++++++++++++
 src/disp.c      | 23 ++++++++++++++++++++---
 src/ray.c       | 11 +++++++++++
 src/tui.c       | 11 +++++++++++
 6 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index d8f6457..3c4c982 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,7 +28,9 @@ linenoise_lib.c:
 endif # USE_READLINE_COND
 
 else # DEBUG_COND
-AM_CFLAGS += -DNDEBUG -Wno-unused-variable
+AM_CFLAGS += -DNDEBUG
+calculer_SOURCES += calc_data.c input.c cli.c tui.c ray.c
+AM_LDFLAGS += -lncurses
 
 if USE_READLINE_COND
 AM_LDFLAGS += -lreadline
diff --git a/src/calculer.c b/src/calculer.c
index 705fb74..6bf8fbb 100644
--- a/src/calculer.c
+++ b/src/calculer.c
@@ -153,7 +153,7 @@ main(int argc, char *argv[])
 	time_t now = time(NULL);
 	struct tm *tm = localtime(&now);
 	size_t bytes_written = strftime(logbuf, sizeof(logbuf), "%F\t", tm);
-	assert(bytes_written);
+	assert(bytes_written);	/* TODO: better handling */
 
 	int opt;
 	while ((opt = getopt(argc, argv, ":Vhl:m:M:i:")) != -1)
diff --git a/src/cli.c b/src/cli.c
index f357ed5..703d418 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -29,6 +29,7 @@ void
 cli_display_calc(int x, int y)
 {
 	printf("%d + %d = ", x, y);
+	fflush(stdout);
 }
 
 int
@@ -60,3 +61,14 @@ void cli_destroy(void) {}
 void *cli_pre_reload(void) {return NULL;}
 void cli_post_reload(void *) {}
 #endif
+
+#ifdef NDEBUG
+static struct disp __cli_disp = {
+	.init = cli_init,
+	.display_calc = cli_display_calc,
+	.read_input = cli_read_input,
+	.display_res = cli_display_res,
+	.destroy = cli_destroy
+};
+struct disp *cli_disp = &__cli_disp;
+#endif
diff --git a/src/disp.c b/src/disp.c
index 8a1a174..26bb2f2 100644
--- a/src/disp.c
+++ b/src/disp.c
@@ -25,6 +25,13 @@
 
 #include "disp.h"
 
+#ifdef NDEBUG
+extern struct disp *cli_disp;
+extern struct disp *tui_disp;
+extern struct disp *ray_disp;
+#endif
+
+#ifndef NDEBUG
 static void *
 load_generic_symbol(struct disp *disp, char *base_name)
 {
@@ -46,8 +53,9 @@ load_generic_symbol(struct disp *disp, char *base_name)
 
 	return ptr;
 }
+#endif
 
-
+#ifndef NDEBUG
 static void
 load_symbols(struct disp *disp)
 {
@@ -72,6 +80,7 @@ load_symbols(struct disp *disp)
 	printf("INFO: successfully loaded symbols from \"%s\"\n",
 	       disp->so_path);
 }
+#endif
 
 struct disp *
 get_disp(char *disp_name)
@@ -90,9 +99,15 @@ get_disp(char *disp_name)
 
 	return disp;
 #else
-#  error "not available yet"
-#endif
+	if (strcmp(disp_name, "cli") == 0)
+		return cli_disp;
+	if (strcmp(disp_name, "tui") == 0)
+		return tui_disp;
+	if (strcmp(disp_name, "ray") == 0)
+		return ray_disp;
+
 	return NULL;
+#endif
 }
 
 void
@@ -101,11 +116,13 @@ destroy_disp(struct disp *disp)
 	if (disp->destroy)
 		disp->destroy();
 
+#ifndef NDEBUG
 	if (disp->dl_handle)
 		dlclose(disp->dl_handle);
 
 	free(disp->so_path);
 	free(disp);
+#endif
 }
 
 #ifndef NDEBUG
diff --git a/src/ray.c b/src/ray.c
index 3050e5a..9fb9023 100644
--- a/src/ray.c
+++ b/src/ray.c
@@ -292,3 +292,14 @@ ray_post_reload(void *state)
 	TraceLog(LOG_INFO, "post-reload actions done");
 }
 #endif
+
+#ifdef NDEBUG
+static struct disp __ray_disp = {
+	.init = ray_init,
+	.display_calc = ray_display_calc,
+	.read_input = ray_read_input,
+	.display_res = ray_display_res,
+	.destroy = ray_destroy
+};
+struct disp *ray_disp = &__ray_disp;
+#endif
diff --git a/src/tui.c b/src/tui.c
index e02429f..6e15591 100644
--- a/src/tui.c
+++ b/src/tui.c
@@ -171,3 +171,14 @@ tui_post_reload(void *state)
 	redraw();
 }
 #endif
+
+#ifdef NDEBUG
+static struct disp __tui_disp = {
+	.init = tui_init,
+	.display_calc = tui_display_calc,
+	.read_input = tui_read_input,
+	.display_res = tui_display_res,
+	.destroy = tui_destroy
+};
+struct disp *tui_disp = &__tui_disp;
+#endif
-- 
cgit v1.2.3