From 6b3508c49702c2f8eabde4317647ca373a3c6244 Mon Sep 17 00:00:00 2001
From: Nicolas "Pixel" Noble <pixel@nobis-crew.org>
Date: Sat, 7 Nov 2009 19:26:18 +0100
Subject: [PATCH 1/1] Makefile enhancement for better cross-compilation support.

---
 src/Makefile |   33 +++++++++++++++++++++++++++++----
 1 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index bb1839d..65b5639 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -87,25 +87,40 @@ XCFLAGS=
 CCOPTIONS= $(CCDEBUG) $(CCOPT) $(CCWARN) $(CFLAGS) $(XCFLAGS)
 LDOPTIONS= $(CCDEBUG) $(LDFLAGS)
 
+ifeq (,$(HOST_CC))
 HOST_CC= $(CC)
+endif
 HOST_RM= rm -f
 HOST_XCFLAGS=
 HOST_XLDFLAGS=
 HOST_XLIBS=
 
+ifeq (,$(TARGET_CC))
 TARGET_CC= $(CC)
 TARGET_STRIP= strip
+TARGET_AR= ar rcs
+endif
 TARGET_XCFLAGS= -D_FILE_OFFSET_BITS=64
 TARGET_XLDFLAGS=
 TARGET_XSHLDFLAGS= -shared
 TARGET_XLIBS=
 TARGET_ARCH= $(patsubst %,-DLUAJIT_TARGET=LUAJIT_ARCH_%,$(TARGET))
 TARGET_DISABLE= -U_FORTIFY_SOURCE
-ifneq (,$(findstring stack-protector,$(shell $(CC) -dumpspecs)))
+ifneq (,$(findstring stack-protector,$(shell $(TARGET_CC) -dumpspecs)))
   TARGET_DISABLE+= -fno-stack-protector
 endif
 
 ifneq (,$(findstring Windows,$(OS)))
+  HOST_SYS= Windows
+else
+  HOST_SYS:= $(shell uname -s)
+  ifneq (,$(findstring CYGWIN,$(HOST_SYS)))
+    HOST_SYS= Windows
+  endif
+endif
+
+ifeq (,$(TARGET_SYS))
+ifneq (,$(findstring Windows,$(OS)))
   TARGET_SYS= Windows
 else
   TARGET_SYS:= $(shell uname -s)
@@ -113,14 +128,17 @@ else
     TARGET_SYS= Windows
   endif
 endif
+endif
 
 ifeq (Linux,$(TARGET_SYS))
   TARGET_XLIBS= -ldl
   TARGET_XLDFLAGS= -Wl,-E
 else
-ifeq (Windows,$(TARGET_SYS))
+ifeq (Windows,$(HOST_SYS))
   HOST_RM= del
-  TARGET_STRIP= strip --strip-unneeded
+endif
+ifeq (Windows,$(TARGET_SYS))
+  TARGET_STRIP+= --strip-unneeded
 else
 ifeq (Darwin,$(TARGET_SYS))
   TARGET_XSHLDFLAGS= -dynamiclib -single_module -undefined dynamic_lookup
@@ -192,6 +210,7 @@ LJVMCORE_O= $(LJVM_O) $(LJCORE_O)
 # NYI: Need complete support for building as a shared library on POSIX.
 #      This is currently *only* suitable for MinGW and Cygwin, see below.
 LUAJIT_O= luajit.o
+LUAJIT_A= luajit.a
 LUAJIT_SO= luajit.so
 LUAJIT_T= luajit
 
@@ -206,6 +225,9 @@ ALL_DYNGEN= buildvm_x86.h
 WIN_RM= *.obj *.lib *.exp *.dll *.exe *.manifest
 ALL_RM= $(LUAJIT_T) $(LUAJIT_SO) $(HOST_T) $(ALL_GEN) *.o $(WIN_RM)
 
+ifeq (Windows,$(HOST_SYS))
+  BUILDVM_T= buildvm.exe
+endif
 ifeq (Windows,$(TARGET_SYS))
   LJVM_BOUT= $(LJVM_O)
   LJVM_MODE= peobj
@@ -213,7 +235,6 @@ ifeq (Windows,$(TARGET_SYS))
   # Imported symbols are bound to a specific DLL name under Windows.
   LUAJIT_SO= lua51.dll
   LUAJIT_T= luajit.exe
-  BUILDVM_T= buildvm.exe
   #
   # You can comment out the following two lines to build a static executable.
   # But then you won't be able to dynamically load any C modules, because
@@ -274,6 +295,10 @@ $(LUAJIT_SO): $(LJVMCORE_O)
 	$(Q)$(TARGET_CC) $(TARGET_SHLDFLAGS) -o $@ $(LJVMCORE_O) $(TARGET_LIBS)
 	$(Q)$(TARGET_STRIP) $@
 
+$(LUAJIT_A): $(LJVMCORE_O)
+	$(E) "LINK      $@"
+	$(Q)$(TARGET_AR) $@ $(LJVMCORE_O)
+
 $(LUAJIT_T): $(TARGET_O) $(TARGET_DEP)
 	$(E) "LINK      $@"
 	$(Q)$(TARGET_CC) $(TARGET_LDFLAGS) -o $@ $(TARGET_O) $(TARGET_LIBS)
-- 
1.6.3.3

