Skip to content

Commit

Permalink
Easier build on M1 take 2 (#611)
Browse files Browse the repository at this point in the history
* Initial changes to build Hashlink on Mac arm64.

* Only include mdbg on the x86_64.

* Re-added build flags for 32 bit architectures.
  • Loading branch information
wynan authored Dec 1, 2023
1 parent a21544c commit dacd8fb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 23 deletions.
37 changes: 33 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ INSTALL_LIB_DIR ?= $(PREFIX)/lib
INSTALL_INCLUDE_DIR ?= $(PREFIX)/include

LIBS=fmt sdl ssl openal ui uv mysql sqlite
ARCH ?= $(shell uname -m)

CFLAGS = -Wall -O3 -I src -msse2 -mfpmath=sse -std=c11 -D LIBHL_EXPORTS
CFLAGS = -Wall -O3 -I src -std=c11 -D LIBHL_EXPORTS
LFLAGS = -L. -lhl
EXTRA_LFLAGS ?=
LIBFLAGS =
Expand Down Expand Up @@ -72,6 +73,7 @@ RELEASE_NAME=win
VS_RUNTIME_LIBRARY ?= c:/windows/system32/vcruntime140.dll

ifeq ($(MARCH),32)
CFLAGS += -msse2 -mfpmath=sse
CC=i686-pc-cygwin-gcc
BUILD_DIR = Release
VS_SDL_LIBRARY ?= include/sdl/lib/x86/SDL2.dll
Expand All @@ -86,6 +88,26 @@ else ifeq ($(UNAME),Darwin)

# Mac
LIBEXT=dylib

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
Expand All @@ -97,15 +119,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

Expand All @@ -114,7 +143,7 @@ CFLAGS += -m$(MARCH) -fPIC -pthread -fno-omit-frame-pointer
LFLAGS += -lm -Wl,-rpath,.:'$$ORIGIN':$(INSTALL_LIB_DIR) -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
Expand Down Expand Up @@ -160,7 +189,7 @@ src/std/regexp.o: src/std/regexp.c
${CC} ${CFLAGS} -o $@ -c $< ${PCRE_FLAGS}

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}
Expand Down
4 changes: 4 additions & 0 deletions include/mdbg/mach_excServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/* Module mach_exc */

#ifdef __x86_64__

#define __MIG_check__Request__mach_exc_subsystem__ 1

#include <string.h>
Expand Down Expand Up @@ -803,3 +805,5 @@ mig_external mig_routine_t mach_exc_server_routine

return catch_mach_exc_subsystem.routine[msgh_id].stub_routine;
}

#endif
5 changes: 5 additions & 0 deletions include/mdbg/mach_excUser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -808,3 +811,5 @@ mig_external kern_return_t mach_exception_raise_state_identity

return KERN_SUCCESS;
}

#endif
4 changes: 4 additions & 0 deletions include/mdbg/mdbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* DEALINGS IN THE SOFTWARE.
*/

#ifdef __x86_64__

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand Down Expand Up @@ -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
6 changes: 1 addition & 5 deletions src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions src/std/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
# define USE_PTRACE
#endif

#ifdef HL_MAC
#if defined(HL_MAC) && defined(__x86_64__)
# include <mdbg/mdbg.h>
# define MAC_DEBUG
#endif

#if defined(HL_WIN)
Expand Down Expand Up @@ -66,7 +67,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;
Expand All @@ -80,7 +81,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;
Expand All @@ -92,7 +93,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;
Expand All @@ -104,7 +105,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 ) {
Expand All @@ -128,7 +129,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 ) {
Expand All @@ -153,7 +154,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;
Expand All @@ -162,7 +163,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;
Expand Down Expand Up @@ -243,7 +244,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;
Expand All @@ -268,7 +269,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;
Expand Down Expand Up @@ -340,7 +341,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);
Expand Down Expand Up @@ -391,7 +392,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;
Expand Down
5 changes: 3 additions & 2 deletions src/std/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down

0 comments on commit dacd8fb

Please sign in to comment.