Skip to content

Commit

Permalink
Refactor Makefile and source files for conditional compilation
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
horta committed Nov 8, 2024
1 parent 7458876 commit 5764178
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 51 deletions.
26 changes: 5 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions athr.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <stdlib.h>
#include <string.h>

#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

Expand Down
4 changes: 2 additions & 2 deletions athr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "athr_widget_main.h"
#include <stdint.h>

#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 <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
4 changes: 2 additions & 2 deletions athr_canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <stdlib.h>
#include <string.h>

#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 <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
10 changes: 5 additions & 5 deletions athr_elapsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,12 +22,12 @@ struct athr_elapsed {
#error Undefined TIME_UTC
#endif

#if ATHR_OS == ATHR_OS_UNIX
#if defined(ATHR_OS_UNIX)
#include <sys/time.h>
#include <unistd.h>
#endif

#if ATHR_OS == ATHR_OS_WIN32
#if defined(ATHR_OS_WIN32)
#include <windows.h>
#endif

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions athr_terminal.c
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion athr_terminal_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdio.h>
#include <sys/ioctl.h>

#if ATHR_OS == ATHR_OS_WIN32
#if defined(ATHR_OS_WIN32)
#include <windows.h>
#endif

Expand Down
20 changes: 10 additions & 10 deletions athr_thr.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions athr_thr.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef ATHR_THR_H
#define ATHR_THR_H

#if ATHR_OS == ATHR_OS_WIN32
#if defined(ATHR_OS_WIN32)
#include <windows.h>
typedef HANDLE athr_thr_handle;
#elif ATHR_OS == ATHR_OS_UNIX
#elif defined(ATHR_OS_UNIX)
#include <pthread.h>
typedef pthread_t athr_thr_handle;
#endif
Expand Down

0 comments on commit 5764178

Please sign in to comment.