From 5764178a6f44a1c37d9c4f927030ff25ca830c9e Mon Sep 17 00:00:00 2001 From: Danilo Horta Date: Fri, 8 Nov 2024 09:41:46 +0000 Subject: [PATCH] Refactor Makefile and source files for conditional compilation - Removed redundant OS and terminal macro definitions in Makefile. - Used `-D` compiler flags to define OS and terminal macros in Makefile. - Changed `#if` statements to `#if defined` for macros in source files. - Updated conditional includes in `athr.h`, `athr.c`, `athr_thr.c`, etc. This refactor simplifies the Makefile by removing redundant macro definitions and leveraging compiler flags. Source files are updated to ensure compatibility with diverse compilers through `#if defined`. This change improves readability and maintainability across the project. --- Makefile | 26 +++++--------------------- athr.c | 4 ++-- athr.h | 4 ++-- athr_canvas.c | 4 ++-- athr_elapsed.c | 10 +++++----- athr_terminal.c | 12 ++++++------ athr_terminal_ioctl.c | 2 +- athr_thr.c | 20 ++++++++++---------- athr_thr.h | 4 ++-- 9 files changed, 35 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 15b1bd1..c873691 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,6 @@ TEST_OBJ = $(TEST_SRC:.c=.o) TEST_TARGET = $(basename $(TEST_OBJ)) PKG_CONFIG_FOUND := $(if $(shell which pkg-config),true,false) -ATHR_TERMINAL_WIN32 := 1 -ATHR_TERMINAL_CURSES := 2 -ATHR_TERMINAL_IOCTL := 3 -ATHR_TERMINAL := -ATHR_OS_WIN32 := 1 -ATHR_OS_UNIX := 2 -ATHR_OS := ifeq '$(findstring ;,$(PATH))' ';' UNAME := Windows @@ -42,14 +35,14 @@ else endif ifeq ($(UNAME),Windows) - ATHR_OS := ATHR_OS_WIN32 - ATHR_TERMINAL := ATHR_TERMINAL_WIN32 + CFLAGS += -DATHR_OS_WIN32 + CFLAGS += -DATHR_TERMINAL_WIN32 else - ATHR_OS := ATHR_OS_UNIX + CFLAGS += -DATHR_OS_UNIX ifeq ($(CURSES_FOUND),true) - ATHR_TERMINAL := ATHR_TERMINAL_CURSES + CFLAGS += -DATHR_TERMINAL_CURSES else - ATHR_TERMINAL := ATHR_TERMINAL_IOCTL + CFLAGS += -DATHR_TERMINAL_IOCTL endif SRC := $(filter-out athr_terminal_win32.c,$(SRC)) HDR := $(filter-out athr_terminal_win32.h,$(SRC)) @@ -60,21 +53,12 @@ ifeq ($(CURSES_FOUND),false) HDR := $(filter-out athr_terminal_curses.h,$(SRC)) endif -CFLAGS += -DATHR_TERMINAL_WIN32=$(ATHR_TERMINAL_WIN32) -CFLAGS += -DATHR_TERMINAL_CURSES=$(ATHR_TERMINAL_CURSES) -CFLAGS += -DATHR_TERMINAL_IOCTL=$(ATHR_TERMINAL_IOCTL) -CFLAGS += -DATHR_TERMINAL=$(ATHR_TERMINAL) -CFLAGS += -DATHR_OS_WIN32=$(ATHR_OS_WIN32) -CFLAGS += -DATHR_OS_UNIX=$(ATHR_OS_UNIX) -CFLAGS += -DATHR_OS=$(ATHR_OS) - $(info UNAME = $(UNAME)) $(info CC = $(CC)) $(info PKG_CONFIG_FOUND = $(PKG_CONFIG_FOUND)) $(info CURSES_FOUND = $(CURSES_FOUND)) $(info CURSES_LIBS = $(CURSES_LIBS)) $(info ATHR_TERMINAL = $(ATHR_TERMINAL)) -$(info ATHR_OS = $(ATHR_OS)) $(info CFLAGS = $(CFLAGS)) all: $(LIB) diff --git a/athr.c b/athr.c index c27b531..69f72ce 100644 --- a/athr.c +++ b/athr.c @@ -12,9 +12,9 @@ #include #include -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #include "athr_ovs_atomic_msvc.h" -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) #include "athr_ovs_atomic_posix.h" #endif diff --git a/athr.h b/athr.h index ba006b9..bbb39b2 100644 --- a/athr.h +++ b/athr.h @@ -7,9 +7,9 @@ #include "athr_widget_main.h" #include -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #include "athr_ovs_atomic_msvc.h" -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) #include #include #include diff --git a/athr_canvas.c b/athr_canvas.c index 7638bf9..d6bd1d5 100644 --- a/athr_canvas.c +++ b/athr_canvas.c @@ -6,9 +6,9 @@ #include #include -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #include "athr_ovs_atomic_msvc.h" -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) #include #include #include diff --git a/athr_elapsed.c b/athr_elapsed.c index c721647..23dfedf 100644 --- a/athr_elapsed.c +++ b/athr_elapsed.c @@ -4,7 +4,7 @@ #error "C11 or a more recent version is required" #endif -#if ATHR_OS == ATHR_OS_UNIX +#if defined(ATHR_OS_UNIX) #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L @@ -22,12 +22,12 @@ struct athr_elapsed { #error Undefined TIME_UTC #endif -#if ATHR_OS == ATHR_OS_UNIX +#if defined(ATHR_OS_UNIX) #include #include #endif -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #include #endif @@ -102,11 +102,11 @@ int athr_elapsed_sleep(long ms) struct timespec duration = { .tv_sec = (time_t)(ms / MILLISECONDS_IN_A_SECOND), .tv_nsec = (long)((ms % MILLISECONDS_IN_A_SECOND) * 1000000) }; -#if ATHR_OS == ATHR_OS_UNIX +#if defined(ATHR_OS_UNIX) return nanosleep(&duration, NULL); #endif -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) struct timespec start; timespec_get(&start, TIME_UTC); diff --git a/athr_terminal.c b/athr_terminal.c index 1e54e4f..429ba25 100644 --- a/athr_terminal.c +++ b/athr_terminal.c @@ -1,14 +1,14 @@ #include "athr_terminal.h" -#if ATHR_TERMINAL == ATHR_TERMINAL_CURSES +#if defined(ATHR_TERMINAL_CURSES) #include "athr_terminal_curses.h" #endif -#if ATHR_TERMINAL == ATHR_TERMINAL_WIN32 +#if defined(ATHR_TERMINAL_WIN32) #include "athr_terminal_win32.h" #endif -#if ATHR_TERMINAL == ATHR_TERMINAL_IOCTL +#if defined(ATHR_TERMINAL_IOCTL) #include "athr_terminal_ioctl.h" #endif @@ -22,11 +22,11 @@ void athr_terminal_force_fallback_use(bool v) { force_fallback_use = v; } unsigned athr_terminal_width(void) { if (force_fallback_use) return athr_terminal_fallback_width(); -#if ATHR_TERMINAL == ATHR_TERMINAL_CURSES +#if defined(ATHR_TERMINAL_CURSES) return athr_terminal_curses_width(); -#elif ATHR_TERMINAL == ATHR_TERMINAL_WIN32 +#elif defined(ATHR_TERMINAL_WIN32) return athr_terminal_win32_width(); -#elif ATHR_TERMINAL == ATHR_TERMINAL_IOCTL +#elif defined(ATHR_TERMINAL_IOCTL) return athr_terminal_ioctl_width(); #else return __athr_terminal_fallback_width(); diff --git a/athr_terminal_ioctl.c b/athr_terminal_ioctl.c index c5f8b97..95cdaa1 100644 --- a/athr_terminal_ioctl.c +++ b/athr_terminal_ioctl.c @@ -9,7 +9,7 @@ #include #include -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #include #endif diff --git a/athr_thr.c b/athr_thr.c index e1308db..9fef8d8 100644 --- a/athr_thr.c +++ b/athr_thr.c @@ -1,9 +1,9 @@ #include "athr_thr.h" -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #define WRAPPER_RETURN DWORD WINAPI #define WRAPPER_ARG_T LPVOID -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) #define WRAPPER_RETURN void * #define WRAPPER_ARG_T void * #endif @@ -13,9 +13,9 @@ static WRAPPER_RETURN __thr_wrapper(WRAPPER_ARG_T arg) struct athr_thread *thr = (struct athr_thread *)arg; thr->func(thr->arg); -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) ExitThread(0); -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) pthread_exit(0); #endif @@ -29,10 +29,10 @@ int athr_thread_create(struct athr_thread *x, athr_thread_start *func, void *arg x->arg = arg; int rc = 0; -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) thr->handle = CreateThread(NULL, 0, __thr_wrapper, (LPVOID)thr, 0, NULL); rc = !thr->handle; -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) rc = pthread_create(&x->handle, 0, __thr_wrapper, (void *)x); #endif x->has_been_created = !rc; @@ -42,20 +42,20 @@ int athr_thread_create(struct athr_thread *x, athr_thread_start *func, void *arg void athr_thread_detach(struct athr_thread *x) { if (!x->has_been_created) return; -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) CloseHandle(thr->handle); -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) pthread_detach(x->handle); #endif } int athr_thread_join(struct athr_thread *x) { -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) if (WaitForSingleObject(thr, INFINITE) == WAIT_FAILED) return 1; CloseHandle(thr->handle); return 0; -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) void *pres = NULL; return pthread_join(x->handle, &pres); #endif diff --git a/athr_thr.h b/athr_thr.h index 3ac38bf..fb4659e 100644 --- a/athr_thr.h +++ b/athr_thr.h @@ -1,10 +1,10 @@ #ifndef ATHR_THR_H #define ATHR_THR_H -#if ATHR_OS == ATHR_OS_WIN32 +#if defined(ATHR_OS_WIN32) #include typedef HANDLE athr_thr_handle; -#elif ATHR_OS == ATHR_OS_UNIX +#elif defined(ATHR_OS_UNIX) #include typedef pthread_t athr_thr_handle; #endif