From 58604e43a9c4fcfe18d2d38a69b4b98089465e8d Mon Sep 17 00:00:00 2001 From: Devin Gillman Date: Fri, 1 Dec 2023 06:33:32 -0600 Subject: [PATCH] Easier build on M1 take 2 (#611) * Initial changes to build Hashlink on Mac arm64. * Only include mdbg on the x86_64. * Re-added build flags for 32 bit architectures. --- Makefile | 48 ++++++++++++++++++++++++++++++++--- include/mdbg/mach_excServer.c | 4 +++ include/mdbg/mach_excUser.c | 5 ++++ include/mdbg/mdbg.c | 4 +++ src/profile.c | 6 +---- src/std/debug.c | 25 +++++++++--------- src/std/error.c | 5 ++-- 7 files changed, 75 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index d00b44f4d..af13ba6e7 100755 --- a/Makefile +++ b/Makefile @@ -7,9 +7,17 @@ INSTALL_BIN_DIR ?= $(PREFIX)/bin INSTALL_LIB_DIR ?= $(PREFIX)/lib INSTALL_INCLUDE_DIR ?= $(PREFIX)/include + +# From upstream, changes made for m1, needs to be checked +#LIBS=fmt sdl ssl openal ui uv mysql sqlite +#ARCH ?= $(shell uname -m) + +#CFLAGS = -Wall -O3 -I src -std=c11 -D LIBHL_EXPORTS + LIBS=fmt sdl ssl openal ui uv mysql CFLAGS = -Wall -O3 -I src -msse2 -mfpmath=sse -std=c11 -I include -I include/pcre -I include/mikktspace -I include/minimp3 -D LIBHL_EXPORTS + LFLAGS = -L. -lhl EXTRA_LFLAGS ?= LIBFLAGS = @@ -60,6 +68,7 @@ LIBEXT = dll RELEASE_NAME=win ifeq ($(MARCH),32) +CFLAGS += -msse2 -mfpmath=sse CC=i686-pc-cygwin-gcc endif @@ -67,7 +76,33 @@ else ifeq ($(UNAME),Darwin) # Mac LIBEXT=dylib +<<<<<<< HEAD CFLAGS += -m$(MARCH) -I /usr/local/include -I /usr/local/opt/libjpeg-turbo/include -I /usr/local/opt/jpeg-turbo/include -I /usr/local/opt/sdl2/include/SDL2 -I /usr/local/opt/libvorbis/include -I /usr/local/opt/openal-soft/include -Dopenal_soft -DGL_SILENCE_DEPRECATION +======= + +BPREFIX := $(shell brew --prefix) + +BREW_LIBJPEG := $(shell brew --prefix libjpeg-turbo) +BREW_SDL2 := $(shell brew --prefix sdl2) +BREW_JPEGTURBO := $(shell brew --prefix jpeg-turbo) +BREW_VORBIS := $(shell brew --prefix libvorbis) +BREW_OPENAL := $(shell brew --prefix openal-soft) +BREW_MBEDTLS := $(shell brew --prefix mbedtls@2) +BREW_LIBPNG := $(shell brew --prefix libpng) +BREW_LIBOGG := $(shell brew --prefix libogg) +BREW_LIBUV := $(shell brew --prefix libuv) + +CFLAGS += -m$(MARCH) -I include -I $(BREW_LIBJPEG)/include \ + -I $(BREW_JPEGTURBO)/include -I $(BREW_SDL2)/include -I $(BREW_VORBIS)/include \ + -I $(BREW_MBEDTLS)/include -I $(BREW_LIBPNG)/include -I $(BREW_LIBOGG)/include \ + -I $(BREW_LIBUV)/include \ + -I $(BREW_OPENAL)/include -Dopenal_soft -DGL_SILENCE_DEPRECATION +LFLAGS += -Wl,-export_dynamic + +CFLAGS += -m$(MARCH) -I include -I /usr/local/include -I /usr/local/opt/libjpeg-turbo/include \ + -I /usr/local/opt/jpeg-turbo/include -I /usr/local/opt/sdl2/include -I /usr/local/opt/libvorbis/include \ + -I /usr/local/opt/openal-soft/include -Dopenal_soft -DGL_SILENCE_DEPRECATION +>>>>>>> dacd8fb1 (Easier build on M1 take 2 (#611)) LFLAGS += -Wl,-export_dynamic -L/usr/local/lib ifdef OSX_SDK @@ -76,15 +111,22 @@ CFLAGS += -isysroot $(ISYSROOT) LFLAGS += -isysroot $(ISYSROOT) endif -LIBFLAGS += -L/usr/local/opt/libjpeg-turbo/lib -L/usr/local/opt/jpeg-turbo/lib -L/usr/local/lib -L/usr/local/opt/libvorbis/lib -L/usr/local/opt/openal-soft/lib +LIBFLAGS += -L$(BREW_LIBJPEG)/lib -L$(BREW_SDL2)/lib -L$(BREW_JPEGTURBO)/lib \ + -L$(BREW_VORBIS)/lib -L$(BREW_OPENAL)/lib -L$(BREW_MBEDTLS)/lib \ + -L$(BREW_LIBPNG)/lib -L$(BREW_LIBOGG)/lib -L$(BREW_LIBUV)/lib LIBOPENGL = -framework OpenGL LIBOPENAL = -lopenal LIBSSL = -framework Security -framework CoreFoundation RELEASE_NAME = osx # Mac native debug +ifneq ($(ARCH),arm64) HL_DEBUG = include/mdbg/mdbg.o include/mdbg/mach_excServer.o include/mdbg/mach_excUser.o LIB += ${HL_DEBUG} +endif + +CFLAGS += -arch $(ARCH) +LFLAGS += -arch $(ARCH) else @@ -93,7 +135,7 @@ CFLAGS += -m$(MARCH) -fPIC -pthread -fno-omit-frame-pointer LFLAGS += -lm -Wl,-rpath,. -Wl,--export-dynamic -Wl,--no-undefined ifeq ($(MARCH),32) -CFLAGS += -I /usr/include/i386-linux-gnu +CFLAGS += -I /usr/include/i386-linux-gnu -msse2 -mfpmath=sse LIBFLAGS += -L/opt/libjpeg-turbo/lib else LIBFLAGS += -L/opt/libjpeg-turbo/lib64 @@ -132,7 +174,7 @@ uninstall: libs: $(LIBS) libhl: ${LIB} - ${CC} -o libhl.$(LIBEXT) -m${MARCH} ${LIBFLAGS} -shared ${LIB} -lpthread -lm + ${CC} ${CFLAGS} -o libhl.$(LIBEXT) -m${MARCH} ${LIBFLAGS} -shared ${LIB} -lpthread -lm hlc: ${BOOT} ${CC} ${CFLAGS} -o hlc ${BOOT} ${LFLAGS} ${EXTRA_LFLAGS} diff --git a/include/mdbg/mach_excServer.c b/include/mdbg/mach_excServer.c index 0ee9a9c2e..15044477e 100644 --- a/include/mdbg/mach_excServer.c +++ b/include/mdbg/mach_excServer.c @@ -7,6 +7,8 @@ /* Module mach_exc */ +#ifdef __x86_64__ + #define __MIG_check__Request__mach_exc_subsystem__ 1 #include @@ -803,3 +805,5 @@ mig_external mig_routine_t mach_exc_server_routine return catch_mach_exc_subsystem.routine[msgh_id].stub_routine; } + +#endif \ No newline at end of file diff --git a/include/mdbg/mach_excUser.c b/include/mdbg/mach_excUser.c index e861948f1..4d0817fe8 100644 --- a/include/mdbg/mach_excUser.c +++ b/include/mdbg/mach_excUser.c @@ -4,6 +4,9 @@ * with a MiG generated by bootstrap_cmds-116 * OPTIONS: */ + +#ifdef __x86_64__ + #define __MIG_check__Reply__mach_exc_subsystem__ 1 #include "mach_exc.h" @@ -808,3 +811,5 @@ mig_external kern_return_t mach_exception_raise_state_identity return KERN_SUCCESS; } + +#endif \ No newline at end of file diff --git a/include/mdbg/mdbg.c b/include/mdbg/mdbg.c index 234a2adb8..04148c13e 100644 --- a/include/mdbg/mdbg.c +++ b/include/mdbg/mdbg.c @@ -20,6 +20,8 @@ * DEALINGS IN THE SOFTWARE. */ +#ifdef __x86_64__ + #include #include #include @@ -882,3 +884,5 @@ void* MDBG_API(read_register)( pid_t pid, int thread, int reg, bool is64 ) { status_t MDBG_API(write_register)( pid_t pid, int thread, int reg, void *value, bool is64 ) { return write_register( get_task(pid), thread, reg, value, is64 ) == KERN_SUCCESS; } + +#endif \ No newline at end of file diff --git a/src/profile.c b/src/profile.c index 2b2925c44..8b5967ec1 100644 --- a/src/profile.c +++ b/src/profile.c @@ -152,17 +152,13 @@ static void *get_thread_stackptr( thread_handle *t, void **eip ) { *eip = (void*)shared_context.context.uc_mcontext.gregs[REG_EIP]; return (void*)shared_context.context.uc_mcontext.gregs[REG_ESP]; # endif -#elif defined(HL_MAC) -# ifdef HL_64 +#elif defined(HL_MAC) && defined(__x86_64__) struct __darwin_mcontext64 *mcontext = shared_context.context.uc_mcontext; if (mcontext != NULL) { *eip = (void*)mcontext->__ss.__rip; return (void*)mcontext->__ss.__rsp; } return NULL; -# else - return NULL; -# endif #else return NULL; #endif diff --git a/src/std/debug.c b/src/std/debug.c index b6b2f4c33..ea24ff7ad 100755 --- a/src/std/debug.c +++ b/src/std/debug.c @@ -32,8 +32,9 @@ # undef USE_PTRACE #endif -#ifdef HL_MAC +#if defined(HL_MAC) && defined(__x86_64__) # include +# define MAC_DEBUG #endif #if defined(HL_WIN) @@ -70,7 +71,7 @@ HL_API bool hl_debug_start( int pid ) { # if defined(HL_WIN) last_pid = -1; return (bool)DebugActiveProcess(pid); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_session_attach(pid); # elif defined(USE_PTRACE) return ptrace(PTRACE_ATTACH,pid,0,0) >= 0; @@ -84,7 +85,7 @@ HL_API bool hl_debug_stop( int pid ) { BOOL b = DebugActiveProcessStop(pid); CleanHandles(); return (bool)b; -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_session_detach(pid); # elif defined(USE_PTRACE) return ptrace(PTRACE_DETACH,pid,0,0) >= 0; @@ -96,7 +97,7 @@ HL_API bool hl_debug_stop( int pid ) { HL_API bool hl_debug_breakpoint( int pid ) { # if defined(HL_WIN) return (bool)DebugBreakProcess(OpenPID(pid)); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_session_pause(pid); # elif defined(USE_PTRACE) return kill(pid,SIGTRAP) == 0; @@ -108,7 +109,7 @@ HL_API bool hl_debug_breakpoint( int pid ) { HL_API bool hl_debug_read( int pid, vbyte *addr, vbyte *buffer, int size ) { # if defined(HL_WIN) return (bool)ReadProcessMemory(OpenPID(pid),addr,buffer,size,NULL); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_read_memory(pid, addr, buffer, size); # elif defined(USE_PTRACE) while( size ) { @@ -132,7 +133,7 @@ HL_API bool hl_debug_read( int pid, vbyte *addr, vbyte *buffer, int size ) { HL_API bool hl_debug_write( int pid, vbyte *addr, vbyte *buffer, int size ) { # if defined(HL_WIN) return (bool)WriteProcessMemory(OpenPID(pid),addr,buffer,size,NULL); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_write_memory(pid, addr, buffer, size); # elif defined(USE_PTRACE) while( size ) { @@ -157,7 +158,7 @@ HL_API bool hl_debug_write( int pid, vbyte *addr, vbyte *buffer, int size ) { HL_API bool hl_debug_flush( int pid, vbyte *addr, int size ) { # if defined(HL_WIN) return (bool)FlushInstructionCache(OpenPID(pid),addr,size); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return true; # elif defined(USE_PTRACE) return true; @@ -166,7 +167,7 @@ HL_API bool hl_debug_flush( int pid, vbyte *addr, int size ) { # endif } -#ifdef HL_MAC +#ifdef MAC_DEBUG static int get_reg( int r ) { switch( r ) { case 0: return REG_RSP; @@ -247,7 +248,7 @@ HL_API int hl_debug_wait( int pid, int *thread, int timeout ) { break; } return 4; -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_session_wait(pid, thread, timeout); # elif defined(USE_PTRACE) int status; @@ -272,7 +273,7 @@ HL_API int hl_debug_wait( int pid, int *thread, int timeout ) { HL_API bool hl_debug_resume( int pid, int thread ) { # if defined(HL_WIN) return (bool)ContinueDebugEvent(pid, thread, DBG_CONTINUE); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_session_resume(pid); # elif defined(USE_PTRACE) return ptrace(PTRACE_CONT,pid,0,0) >= 0; @@ -344,7 +345,7 @@ HL_API void *hl_debug_read_register( int pid, int thread, int reg, bool is64 ) { return (void*)*(int_val*)&c.ExtendedRegisters[10*16]; #endif return (void*)*GetContextReg(&c,reg); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_read_register(pid, thread, get_reg(reg), is64); # elif defined(USE_PTRACE) void *r = get_reg(reg); @@ -395,7 +396,7 @@ HL_API bool hl_debug_write_register( int pid, int thread, int reg, void *value, else *GetContextReg(&c,reg) = (REGDATA)value; return (bool)SetThreadContext(OpenTID(thread),&c); -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return mdbg_write_register(pid, thread, get_reg(reg), value, is64); # elif defined(USE_PTRACE) return ptrace(PTRACE_POKEUSER,thread,get_reg(reg),value) >= 0; diff --git a/src/std/error.c b/src/std/error.c index 71d63be23..01aa2df9a 100755 --- a/src/std/error.c +++ b/src/std/error.c @@ -225,8 +225,9 @@ static void _sigtrap_handler(int signum) { } #endif -#ifdef HL_MAC +#if defined(HL_MAC) && defined(__x86_64__) extern bool is_debugger_attached(void); +# define MAC_DEBUG #endif HL_PRIM bool hl_detect_debugger() { @@ -239,7 +240,7 @@ HL_PRIM bool hl_detect_debugger() { raise(SIGTRAP); } return (bool)debugger_present; -# elif defined(HL_MAC) +# elif defined(MAC_DEBUG) return is_debugger_attached(); # else return false;