From 91cd148a05e2c492a02eb77b12a2581b87bc9822 Mon Sep 17 00:00:00 2001 From: Andrei Warkentin Date: Tue, 28 Mar 2017 01:58:42 -0400 Subject: [PATCH] tcc: early OSX native support - build scripts - working '-run' mode, e.g.: ./tcc -B. -I./include -I. -I.. -D_ANSI_SOURCE -run examples/ex1.c Note: we don't bother parsing Mach-O/Fat images yet. We blindly dlopen the image. Signed-off-by: Andrei Warkentin --- Makefile | 22 ++++++++++++++++++++-- configure | 8 ++++++++ lib/Makefile | 3 +++ libtcc.c | 19 +++++++++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index c74d833e..8de469a3 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,21 @@ ifdef CONFIG_WIN32 endif CFGWIN = -win NATIVE_TARGET = $(ARCH)-$(if $(eq $(ARCH),arm),wince,win32) +else ifdef CONFIG_OSX + LIBS=-lm + ifndef CONFIG_NOLDL + LIBS+=-ldl + endif + # make libtcc as static or dynamic library? + ifeq ($(DISABLE_STATIC),yes) + LIBTCC=libtcc.dylib + ifndef DISABLE_RPATH + LINK_LIBTCC += -Wl,-rpath,"$(libdir)" + export LD_LIBRARY_PATH := $(CURDIR)/$(TOP) + endif + endif + CFGWIN =-unx + NATIVE_TARGET = $(ARCH)-osx else LIBS=-lm ifndef CONFIG_NOLDL @@ -72,6 +87,7 @@ CONFIG_$(ARCH) = yes NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386 NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64 NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE +NATIVE_DEFINES_$(CONFIG_OSX) += -DTCC_TARGET_X86_64 -DTCC_TARGET_MACHO NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM @@ -92,11 +108,11 @@ TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info all: $(PROGS) $(TCCLIBS) $(TCCDOCS) # cross compiler targets to build -TCC_X = i386 x86_64 i386-win32 x86_64-win32 arm arm64 arm-wince c67 +TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67 # TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi # cross libtcc1.a targets to build -LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 arm arm64 arm-wince +LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince PROGS_CROSS = $(foreach X,$(TCC_X),$X-tcc$(EXESUF)) LIBTCC1_CROSS = $(foreach X,$(LIBTCC1_X),libtcc1-$X.a) @@ -123,6 +139,7 @@ DEF-i386 = -DTCC_TARGET_I386 DEF-x86_64 = -DTCC_TARGET_X86_64 DEF-i386-win32 = -DTCC_TARGET_PE -DTCC_TARGET_I386 DEF-x86_64-win32= -DTCC_TARGET_PE -DTCC_TARGET_X86_64 +DEF-x86_64-osx = -DTCC_TARGET_MACHO -DTCC_TARGET_X86_64 DEF-arm-wince = -DTCC_TARGET_PE -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP -DTCC_ARM_HARDFLOAT DEF-arm64 = -DTCC_TARGET_ARM64 DEF-c67 = -DTCC_TARGET_C67 -w # disable warnigs @@ -160,6 +177,7 @@ i386_FILES = $(CORE_FILES) i386-gen.c i386-link.c i386-asm.c i386-asm.h i386-tok i386-win32_FILES = $(i386_FILES) tccpe.c x86_64_FILES = $(CORE_FILES) x86_64-gen.c x86_64-link.c i386-asm.c x86_64-asm.h x86_64-win32_FILES = $(x86_64_FILES) tccpe.c +x86_64-osx_FILES = $(x86_64_FILES) arm_FILES = $(CORE_FILES) arm-gen.c arm-link.c arm-asm.c arm-wince_FILES = $(arm_FILES) tccpe.c arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c diff --git a/configure b/configure index 4072082b..2a019d80 100755 --- a/configure +++ b/configure @@ -37,6 +37,7 @@ cygwin="no" gprof="no" bigendian="no" mingw32="no" +osx="no" LIBSUF=".a" EXESUF="" DLLSUF=".so" @@ -52,6 +53,7 @@ cpu= # OS specific targetos=`uname` case $targetos in + Darwin) osx=yes;; MINGW*) mingw32=yes;; MSYS*) mingw32=yes;; CYGWIN*) mingw32=yes; cygwin=yes; cross_prefix="mingw32-";; @@ -256,6 +258,9 @@ else if test x"$tccdir" = x""; then tccdir="${libdir}/tcc" fi + if test "$osx" = "yes" ; then + DLLSUF=".dylib" + fi fi # mingw32 if test x"$includedir" = x""; then @@ -486,6 +491,9 @@ fi if test "$mingw32" = "yes" ; then echo "CONFIG_WIN32=yes" >> config.mak fi +if test "$osx" = "yes" ; then + echo "CONFIG_OSX=yes" >> config.mak +fi if test "$bigendian" = "yes" ; then echo "WORDS_BIGENDIAN=yes" >> config.mak echo "#define WORDS_BIGENDIAN 1" >> $TMPH diff --git a/lib/Makefile b/lib/Makefile index 98617434..2ac3126b 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -46,6 +46,9 @@ TGT-i386 = -DTCC_TARGET_I386 OBJ-x86_64 = $(X86_64_O) TGT-x86_64 = -DTCC_TARGET_X86_64 +OBJ-x86_64-osx = $(X86_64_O) +TGT-x86_64-osx = -DTCC_TARGET_X86_64 -DTCC_TARGET_MACHO + OBJ-arm = $(ARM_O) TGT-arm = -DTCC_TARGET_ARM diff --git a/libtcc.c b/libtcc.c index 99bf7836..b4b2615d 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1081,7 +1081,19 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) break; #endif default: -#ifdef TCC_TARGET_PE +#if defined(TCC_TARGET_MACHO) + if (s1->output_type == TCC_OUTPUT_MEMORY) { + ret = 0; +#ifdef TCC_IS_NATIVE + if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) { + ret = -1; + } +#endif /* TCC_TARGET_MACHO */ + } else { + ret = tcc_load_dll(s1, fd, filename, + (flags & AFF_REFERENCED_DLL) != 0); + } +#elif defined(TCC_TARGET_PE) ret = pe_load_file(s1, filename, fd); #else /* as GNU ld, consider it is an ld script if not recognized */ @@ -1143,7 +1155,10 @@ ST_FUNC int tcc_add_crt(TCCState *s, const char *filename) /* the library name is the same as the argument of the '-l' option */ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) { -#ifdef TCC_TARGET_PE +#if defined(TCC_TARGET_MACHO) + const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL }; + const char **pp = s->static_link ? libs + 1 : libs; +#elif defined(TCC_TARGET_PE) const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL }; const char **pp = s->static_link ? libs + 4 : libs; #else