From 49b58febba4cf27d1915d6847cef1924023b09ec Mon Sep 17 00:00:00 2001 From: Harshit-Dhanwalkar Date: Mon, 8 Jun 2026 15:51:46 +0530 Subject: [PATCH] Work on support for MacOS --- C/Makefile | 50 +++++++++++++++++++++++++++++------------------ C/lib/nl_alloc.h | 5 +++++ C/lib/nl_io.h | 9 ++++++++- C/lib/nl_printf.c | 1 + C/lib/nl_printf.h | 3 ++- C/lib/nl_string.h | 13 ++++++++---- C/src/ascii.c | 17 ++++++++++++---- README.md | 4 ++++ 8 files changed, 73 insertions(+), 29 deletions(-) diff --git a/C/Makefile b/C/Makefile index 7731dde..83a90f7 100644 --- a/C/Makefile +++ b/C/Makefile @@ -1,19 +1,31 @@ CC = gcc -CFLAGS = -Wall -Wextra -O2 -Iinclude -Ilib -fno-stack-protector -# LDFLAGS = -lm -LDFLAGS += -nostdlib # drops crt startup files and default libs -# LDFLAGS += -nodefaultlibs # drops default libs but keeps crt startup files -LDFLAGS += -Wl,--no-as-needed -# LDFLAGS += -Wl,-rpath,/lib/x86_64-linux-gnu # for libdl.so # TEST: -# LDFLAGS += /lib/x86_64-linux-gnu/libdl.so.2 -LDFLAGS += -Wl,-dynamic-linker,/lib64/ld-linux-x86-64.so.2 -LDFLAGS += /usr/lib/x86_64-linux-gnu/crti.o -LDFLAGS += /usr/lib/x86_64-linux-gnu/crtn.o -LDFLAGS += -ldl # Dynamic loading symbols -LDFLAGS += -lpthread # Multi-threaded capture-render, pthreads producer/consumer -LDFLAGS += -lc -LDFLAGS += -msse4.1 # SIMD - SSE2 for the YUYV to gray conversion -# LDFLAGS += -static +CFLAGS = -Wall -Wextra -O2 -Iinclude -Ilib + +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Linux) + CFLAGS += -fno-stack-protector -D__LINUX_NOLIBC__ + # LDFLAGS = -lm + LDFLAGS += -nostdlib # drops crt startup files and default libs + # LDFLAGS += -nodefaultlibs # drops default libs but keeps crt startup files + LDFLAGS += -Wl,--no-as-needed + LDLIBS := -ldl -lpthread -lc + # LDFLAGS += -Wl,-rpath,/lib/x86_64-linux-gnu + # LDFLAGS += /lib/x86_64-linux-gnu/libdl.so.2 + LDFLAGS += -Wl,-dynamic-linker,/lib64/ld-linux-x86-64.so.2 + LDFLAGS += /usr/lib/x86_64-linux-gnu/crti.o + LDFLAGS += /usr/lib/x86_64-linux-gnu/crtn.o + LDFLAGS += -ldl # Dynamic loading symbols + LDFLAGS += -lpthread # Multi-threaded capture-render, pthreads producer/consumer + LDFLAGS += -lc + LDFLAGS += -msse4.1 # SIMD - SSE2 for the YUYV to gray conversion + # LDFLAGS += -static + LIBSRCS = $(wildcard lib/*.c) +else ifeq ($(UNAME_S),Darwin) + CFLAGS += + LDFLAGS += + LDLIBS := + LIBSRCS = +endif SRCDIR = src INCDIR = include @@ -21,10 +33,10 @@ LIBDIR = lib BUILDDIR = build OBJDIR = $(BUILDDIR)/obj -LIBSRCS = $(wildcard $(LIBDIR)/*.c) -SOURCES = $(wildcard $(SRCDIR)/*.c) $(LIBSRCS) -OBJECTS = $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES)) -TARGET = $(BUILDDIR)/webcam_ascii +LIBSRCS = $(wildcard $(LIBDIR)/*.c) +SOURCES = $(wildcard $(SRCDIR)/*.c) $(LIBSRCS) +OBJECTS = $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES)) +TARGET = $(BUILDDIR)/webcam_ascii PLUGIN_SRCS = $(wildcard filters/*.c) PLUGIN_C_SRCS = $(filter-out filters/%.h, $(PLUGIN_SRCS)) PLUGIN_TARGETS = $(patsubst filters/%.c,$(BUILDDIR)/%.so,$(PLUGIN_C_SRCS)) diff --git a/C/lib/nl_alloc.h b/C/lib/nl_alloc.h index c953d31..5f8d967 100644 --- a/C/lib/nl_alloc.h +++ b/C/lib/nl_alloc.h @@ -1,6 +1,7 @@ #ifndef NL_ALLOC_H #define NL_ALLOC_H +#ifdef __LINUX_NOLIBC__ #include void *nl_malloc(size_t n); @@ -11,4 +12,8 @@ void nl_free(void *ptr); #define calloc(nm, sz) nl_calloc(nm, sz) #define free(p) nl_free(p) +#else // MACOS +#include +#endif + #endif diff --git a/C/lib/nl_io.h b/C/lib/nl_io.h index 998131c..bc5ac7f 100644 --- a/C/lib/nl_io.h +++ b/C/lib/nl_io.h @@ -6,9 +6,10 @@ inotify */ -#include "nl_syscall.h" #include +#ifdef __LINUX_NOLIBC__ +#include "nl_syscall.h" /* Basic I/O */ static inline ssize_t nl_write(int fd, const void *buf, size_t n) { return (ssize_t)__sc3(SYS_write, fd, (long)buf, (long)n); @@ -121,4 +122,10 @@ static inline int nl_tcsetattr(int fd, int action, const struct termios *t) { #define inotify_init1(f) nl_inotify_init1(f) #define inotify_add_watch(f, p, m) nl_inotify_add_watch(f, p, m) +#else +// #include +#include +#include +#endif + #endif diff --git a/C/lib/nl_printf.c b/C/lib/nl_printf.c index 53c0d4b..114fe31 100644 --- a/C/lib/nl_printf.c +++ b/C/lib/nl_printf.c @@ -1,4 +1,5 @@ #include "nl_printf.h" +// #include "nolibc.h" #include #include diff --git a/C/lib/nl_printf.h b/C/lib/nl_printf.h index 74aa9bc..48fe0e9 100644 --- a/C/lib/nl_printf.h +++ b/C/lib/nl_printf.h @@ -31,7 +31,8 @@ static inline int nl_fmt_fps(char *buf, size_t sz, double fps) { char _fb[1024]; \ int _fn = nl_snprintf(_fb, sizeof(_fb), fmt, ##__VA_ARGS__); \ if (_fn > 0) { \ - size_t _nwrite = (_fn < (int)sizeof(_fb) - 1) ? _fn : sizeof(_fb) - 1; \ + size_t _nwrite = \ + (_fn < (int)sizeof(_fb) - 1) ? (size_t)_fn : sizeof(_fb) - 1; \ nl_write((int)(long)(fd), _fb, _nwrite); \ } \ } while (0) diff --git a/C/lib/nl_string.h b/C/lib/nl_string.h index 8b50e29..29a1082 100644 --- a/C/lib/nl_string.h +++ b/C/lib/nl_string.h @@ -12,10 +12,15 @@ static inline size_t nl_strlen(const char *s) { } static inline void *nl_memcpy(void *dst, const void *src, size_t n) { - uint8_t *d = (uint8_t *)dst; - const uint8_t *s = (const uint8_t *)src; - while (n--) - *d++ = *s++; + // uint8_t *d = (uint8_t *)dst; + // const uint8_t *s = (const uint8_t *)src; + // while (n--) + // *d++ = *s++; + unsigned char *d = (unsigned char *)dst; + const unsigned char *s = (const unsigned char *)src; + for (size_t i = 0; i < n; i++) { + d[i] = s[i]; + } return dst; } diff --git a/C/src/ascii.c b/C/src/ascii.c index 8fc3d42..9824697 100644 --- a/C/src/ascii.c +++ b/C/src/ascii.c @@ -1,6 +1,5 @@ #include "ascii.h" -#include #include #include "nolibc.h" @@ -9,11 +8,12 @@ static inline uint8_t clamp_u8(int v) { return (v < 0) ? 0 : (v > 255) ? 255 : (uint8_t)v; } -static inline int my_abs(int x) { - return x < 0 ? -x : x; -} +static inline int my_abs(int x) { return x < 0 ? -x : x; } // Image conversion +#ifdef __x86_64__ +#include + void yuyv_to_gray_simd(const uint8_t *yuyv, uint8_t *gray, int width, int height) { int total_pixels = width * height; @@ -36,6 +36,15 @@ void yuyv_to_gray_simd(const uint8_t *yuyv, uint8_t *gray, int width, for (; i < total_pixels; i++) gray[i] = yuyv[i * 2]; } +#else +void yuyv_to_gray_simd(const uint8_t *yuyv, uint8_t *gray, int width, + int height) { + int total = width * height; + for (int i = 0; i < total; i++) { + gray[i] = yuyv[i * 2]; + } +} +#endif void yuyv_to_rgb(const uint8_t *yuyv, uint8_t *rgb, int width, int height) { int pairs = (width * height) / 2; diff --git a/README.md b/README.md index 5a8906c..5fcfb33 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,10 @@ gcc -O2 -fPIC -shared -Iinclude filters/my_filter.c -o build/my_filter.so - [ ] Replace `pthread` with raw `futex` syscalls - [ ] Replace `dlopen` with a minimal ELF loader +## Fixes +- [ ] [Issue #2](https://github.com/Harshit-Dhanwalkar/AsciiCam/issues/2) MacOS support + - Rewrite `capture.c` for MacOS port using [AVFoundation](https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html). ([Stackoverflow : how do I set up a video input using the AVFoundation framework](https://stackoverflow.com/questions/32053460/how-do-i-set-up-a-video-input-using-the-avfoundation-framework)) + --- Project is under [PolyForm Noncommercial License BY-NC](LICENCE).