2
0
Эх сурвалжийг харах

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.
Devin Gillman 1 жил өмнө
parent
commit
dacd8fb13f

+ 33 - 4
Makefile

@@ -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 =
@@ -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
@@ -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
@@ -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
 
@@ -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
@@ -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}

+ 4 - 0
include/mdbg/mach_excServer.c

@@ -7,6 +7,8 @@
 
 /* Module mach_exc */
 
+#ifdef __x86_64__
+
 #define	__MIG_check__Request__mach_exc_subsystem__ 1
 
 #include <string.h>
@@ -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 - 0
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

+ 4 - 0
include/mdbg/mdbg.c

@@ -20,6 +20,8 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#ifdef __x86_64__
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
@@ -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

+ 1 - 5
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

+ 13 - 12
src/std/debug.c

@@ -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)
@@ -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;
@@ -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;
@@ -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;
@@ -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 ) {
@@ -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 ) {
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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);
@@ -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;

+ 3 - 2
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;