Skip to content

Commit

Permalink
Easier build on M1 take 2 (HaxeFoundation#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 and crazyjul committed Jan 10, 2024
1 parent 7c4783b commit 58604e4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 22 deletions.
48 changes: 45 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -60,14 +68,41 @@ LIBEXT = dll
RELEASE_NAME=win

ifeq ($(MARCH),32)
CFLAGS += -msse2 -mfpmath=sse
CC=i686-pc-cygwin-gcc
endif

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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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}
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 @@ -32,8 +32,9 @@
# undef 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 @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
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 58604e4

Please sign in to comment.