# Copyright (c) 2004-2007, The PalCom Project consortium.
# See http://www.ist-palcom.org/consortium.
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
# 
#     * Neither the name of the PalCom Project nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#########################################################################
#
# Generic Makefile for all pal-vm builds
#
# To be used from either command line or from Eclipse CDT (standard make 
# project).
#
# This makefile contains 4 main targets: 
#
# Two targets "all" and "clean" are meant for the normal users of the VM:
# These build/clean both release and debug versions of VM (in two separate 
# subdirs)
#
# Two additional targets exists, mainly meant for VM developers:
# "dev" and "dev-clean" builds/cleans two additional builds of the VM, 
# again in separate subdirs: A "Checked" build of the VM, which runs very 
# slowly, but does a number of extra runtime tests, and a "Profiling" build
# which use the GNU Gprof runtime profiling.
#
# 
# More fine-grained building as well as cross-building for other platform 
# is supported through the use of make variables on the command line: 
#
#    make arch=xxx ... 
#         where arch can be one of: cygwin, macosx, linux, unc20, bluegiga
#         Specifies which platform to build for
#
#    make release=1 ... [ build | cleanup ]
#         will compile and link the vm with full optimization and minimal 
#         debugging and profiling support.
#         Notice: specify only one of release, debug, checked, gprof
#
#    make debug=1 ... [ build | cleanup ]
#         will compile and link the vm with light debugging support enabled.
#         Use pal-dev-vm -d to utilize this
#         Notice: specify only one of release, debug, checked, gprof
#
#    make gprof=1 ... [ build | cleanup ]
#         will compile and link the vm with GNU Profiling enabled.
#         Use pal-dev-vm -p to utilize this
#         Notice: specify only one of release, debug, checked, gprof
#
#    make checked=1 ... [ build | cleanup ]
#         will compile and link the vm with extensive runtime checks enabled.
#         Use pal-dev-vm -c to utilize this
#         Notice: specify only one of release, debug, checked, gprof
#
#    make rescounting=1 ... 
#         will enable resource counting in the vm
#         This use the same subdirectories as would otherwise have been 
#         used, so a make clean may be necessary before this.
#
# If you want to build just a single variant of the VM, you should not use
# 'all' or 'dev' targets.
# instead use the 'build' target, and specify the variant using the options
# described above. E.g.
#
#    make checked=1 build
#         to build only the "Checked" variant
# 
# Correspondingly to clean a specific variant, you should not use the 'clean'
# or 'dev-clean' targets, but the 'cleanup' target, e.g.
#
#    make checked=1 cleanup
#         to clean only the "Checked" variant
# 
############################################################################

MAKE := make --no-print-directory

#### Supported platforms ###

ifeq ($(arch),)
ifeq ($(TERM),cygwin)
arch := cygwin
else
ifeq ($(shell uname),Linux)
arch := linux
else
ifeq ($(shell uname),Darwin)
arch := macosx
endif
endif
endif
endif

ifeq ($(arch),cygwin)
CXX        := g++
CC         := gcc
CXXFLAGS   := -D__cygwin__ -IC:/cygwin/usr/include -Wall -c -fmessage-length=0
DST        := Cygwin
EXE        := .exe
PLATSRCS   := ./src/VM/CygwinPlatform.cpp
endif

ifeq ($(arch),macosx)
CXX        := g++
CC         := gcc
CXXFLAGS   := -D__macosx__ -Wall -c -fmessage-length=0
DST        := MacOSX
PLATSRCS   := ./src/VM/UnixPlatform.cpp
endif

ifeq ($(arch),linux)
CXX        := g++
CC         := gcc
CXXFLAGS   := -D__linux__ -Wall -c -fmessage-length=0
LINKFLAGS  := -export-dynamic
LIBS       := -lpthread -ldl
DST        := Linux
PLATSRCS   := ./src/VM/UnixPlatform.cpp
endif

ifeq ($(arch),unc20)
BASE       := /usr/local/LxNETES-2.3/arm-elf
UCLIB      := $(BASE)/uClibc-0.9.19w
STL        := $(BASE)/STLport-4.5.3
CXX        := $(UCLIB)/bin/arm-uclibc-gcc
CC         := $(UCLIB)/bin/arm-uclibc-gcc
CXXFLAGS   := -Dlinux  -D__linux__ -D__UNC20__ \
              -D__NO_DL__ -D__STATIC__ -D_GNU_SOURCE -DEMBED \
              -I$(UCLIB)/include -Wall -mcpu=arm7tdmi -c
LINKFLAGS  := -L./src/static_include/lib -L$(UCLIB)/lib -elf2flt -static 
LIBS       := -lpthread
DST        := UNC20
GNU_DBG    := 
PLATSRCS   := ./src/VM/UnixPlatform.cpp
endif

ifeq ($(arch),axis)
BASE       := $(HOME)/axisarm/
UCLIB      := $(BASE)/arm-linux-uclibc
CXX        := $(UCLIB)/bin/g++
CC         := $(UCLIB)/bin/gcc
CXXFLAGS   := -Dlinux  -D__linux__ -D__AXIS__ \
              -D__NO_DL__ -D__STATIC__ -D_GNU_SOURCE -DEMBED \
              -I$(UCLIB)/include -Wall -c -march=armv4
LINKFLAGS  := -L./src/static_include/lib -L$(UCLIB)/lib -static 
LIBS       := -lpthread
DST        := Axis
PLATSRCS   := ./src/VM/UnixPlatform.cpp
endif

ifeq ($(arch),bluegiga)
BASE       := /users/frolund/bin/2.95.3
CXX        := $(BASE)/bin/arm-linux-g++
CC         := $(BASE)/bin/arm-linux-gcc
CXXFLAGS   := -D__linux__ -D_GNU_SOURCE -D__BLUEGIGA__ -c -Wall 
LINKFLAGS  := -L$(BASE)/arm-linux/lib -export-dynamic
LIBS       := -ldl -lpthread -lstdc++
DST        := BlueGiga
PLATSRCS   := ./src/VM/UnixPlatform.cpp
endif

#### End of supported platforms ###

ifndef GNU_OPT
GNU_OPT    := -O3 -fno-exceptions
endif
ifndef GNU_DBG
GNU_DBG    := -g3 -fno-exceptions
endif

ifndef release
ifndef debug
ifndef checked
ifndef gprof
ERROR      := "None of release, debug, checked, gprof specified"
endif
endif
endif
endif

# User release build (pal-vm / pal-dev-vm)
ifeq ($(release),1)
DST        := $(addsuffix -Release,$(DST))
CXXFLAGS   += $(GNU_OPT) -g0 -fomit-frame-pointer #-D__SCAN_HEAP_SUPPORT__
else
release    := 0
endif

# User debugging build (pal-vm -d / pal-dev-vm -d)
ifeq ($(debug),1)
ifeq ($(release),1)
ERROR      := "Both release and debug specified"
endif
DST        := $(addsuffix -Debug,$(DST))
CXXFLAGS   += $(GNU_OPT) $(GNU_DBG) -D__PROFILING__ -D__DEBUGGING_INFO__ -D__SCAN_HEAP_SUPPORT__
EXECUTABLE := pal-vm-debug$(EXE)
else
debug      := 0
EXECUTABLE := pal-vm$(EXE)
endif

# Developer runtime checking build (pal-dev-vm -c)
ifeq ($(checked),1)
ifeq ($(release),1)
ERROR      := "Both release and checked specified"
endif
ifeq ($(debug),1)
ERROR      := "Both debug and checked set"
endif
DST        := $(addsuffix -Checked,$(DST))
CXXFLAGS   += -O0 $(GNU_DBG) -D__DEBUG__ -D__DEBUGGING_INFO__
EXECUTABLE := pal-vm-checked$(EXE)
else
checked    := 0
endif

# Developer runtime GNU profiling build (pal-dev-vm -p)
ifeq ($(gprof),1)
ifeq ($(release),1)
ERROR      := "Both release and gprof specified"
endif
ifeq ($(debug),1)
ERROR      := "Both debug and gprof specified"
endif
ifeq ($(checked),1)
ERROR      := "Both checked and gprof specified"
endif
DST        := $(addsuffix -Profiling,$(DST))
CXXFLAGS   += $(GNU_OPT) $(GNU_DBG) -pg
EXECUTABLE := pal-vm-profiling$(EXE)
LINKFLAGS  += -pg
else
gprof      := 0
endif


# Independent flags below

# Resource counting
ifeq ($(rescounting),1)
CXXFLAGS   += -D__MEMORY_COUNTER__
endif

DSTDIRS  := $(DST)
RM       := rm -rf

# VM sources
SRCS := \
./src/VM/AnyInteger.cpp \
./src/VM/Array.cpp \
./src/VM/Block.cpp \
./src/VM/BlockMirror.cpp \
./src/VM/ByteArray.cpp \
./src/VM/ByteCodes.cpp \
./src/VM/ByteStream.cpp \
./src/VM/Class.cpp \
./src/VM/Component.cpp \
./src/VM/Coroutine.cpp \
./src/VM/DeSerializer.cpp \
./src/VM/FileInputStream.cpp \
./src/VM/Handle.cpp \
./src/VM/HashMap.cpp \
./src/VM/Heap.cpp \
./src/VM/SingleSemispaceHeap.cpp \
./src/VM/SingleMarkSweepCompactHeap.cpp \
./src/VM/HeapObject.cpp \
./src/VM/Integer.cpp \
./src/VM/Integer32.cpp \
./src/VM/Interpreter.cpp \
./src/VM/Main.cpp \
./src/VM/Memory.cpp \
./src/VM/Method.cpp \
./src/VM/Native.cpp \
./src/VM/NativeFuture.cpp \
./src/VM/NativeSymLookup.cpp \
./src/VM/Object.cpp \
./src/VM/Primitives.cpp \
./src/VM/Process.cpp \
./src/VM/ReadableArray.cpp \
./src/VM/Stack.cpp \
./src/VM/String.cpp \
./src/VM/Symbol.cpp \
./src/VM/System.cpp \
./src/VM/Threads.cpp \
./src/VM/UTF8.cpp \
./src/VM/LinkedList.cpp \
./src/VM/Universe.cpp \
./src/VM/ZlibStream.cpp \
$(PLATSRCS) 

CSRCS := \
./src/zlib/adler32.c \
./src/zlib/crc32.c \
./src/zlib/inflate.c \
./src/zlib/inftrees.c \
./src/zlib/inffast.c \
./src/zlib/zutil.c \
./src/zlib/uncompr.c

DSTDIRS += $(DST)/VM
DSTDIRS += $(DST)/zlib

# export the variables used to select build variant in sub-make
export arch release debug checked gprof

# Targets below

all:
	$(MAKE) release=1 build
	$(MAKE) debug=1   build

dev: all
	$(MAKE) checked=1 build
	$(MAKE) gprof=1   build

clean:
	$(MAKE) release=1 cleanup
	$(MAKE) debug=1   cleanup

dev-clean: clean
	$(MAKE) checked=1 cleanup
	$(MAKE) gprof=1   cleanup

# Generic build target, invoke appropriate build for platform
build: 
ifdef ERROR
	@echo ""
	@echo ERROR: $(ERROR)
	@echo See comment in beginning of GNUmakefile for correct usage
	@exit 1
endif
ifeq ($(arch),)
	@echo Cannot determine platform.
	@echo You need to invoke specific target, e.g.
	@echo "    make arch=unc20 ..."
else
	@$(MAKE) $(EXECUTABLE)
endif

# Generic cleaning target, invoke appropriate clean for platform
cleanup:
ifdef ERROR
	@echo ""
	@echo ERROR: $(ERROR)
	@echo See comment in beginning of GNUmakefile for correct usage
	@exit 1
endif
ifeq ($(arch),)
	@echo Cannot determine platform.
	@echo You need to invoke specific target, e.g.
	@echo "    make arch=unc20 ... clean"
else
#	-$(RM) $(OBJS) $(DEPS) $(EXECUTABLE) 
	-$(RM) $(DST)
	-$(RM) *~
	-@echo ' '
endif

################## GNU Makefile magic below ###########################

# Objectfiles and dependency files are placed in $DST in paths 
# corresponding to the source files in ./src/... but without the
# ./src prefix (looks stupid with objectfiles in a src dir)
OBJS       := $(addprefix $(DST)/,$(subst ./src/,,$(SRCS:%.cpp=%.o)))
OBJS       += $(addprefix $(DST)/,$(subst ./src/,,$(CSRCS:%.c=%.o)))
DEPS       := $(OBJS:%.o=%.d)
EXECUTABLE := $(DST)/$(EXECUTABLE)

# Automatic dependencies
ifneq ($(strip $(DEPS)),)
-include $(DEPS)
endif

$(DSTDIRS):
	mkdir -p $@

$(DST)/VM/%.o: ./src/VM/%.cpp 
	@echo 'Building file: $@ from input: $<'
	@echo $(CXX) $(CXXFLAGS) -o$@ $<
	@$(CXX) $(CXXFLAGS) -o$@ $< && \
	  echo -n $(@:%.o=%.d) $(dir $@) > $(@:%.o=%.d) && \
	  $(CXX) -MM -MG -P -w $(CXXFLAGS)  $< >> $(@:%.o=%.d)
	@echo ' '

$(DST)/zlib/%.o: ./src/zlib/%.c 
	@echo 'Building file: $@ from input: $<'
	@echo $(CC) $(CXXFLAGS) -o$@ $<
	@$(CC) $(CXXFLAGS) -DNOBYFOUR -DNO_DEFLATE -DNO_GZCOMPRESS -o$@ $< && \
	  echo -n $(@:%.o=%.d) $(dir $@) > $(@:%.o=%.d) && \
	  $(CC) -MM -MG -P -w $(CXXFLAGS)  $< >> $(@:%.o=%.d)
	@echo ' '

$(EXECUTABLE): $(DSTDIRS) $(OBJS) $(STATIC_OBJS)
	@echo 'Linking target: $@'
	@echo $(CXX) ${LINKFLAGS} -o $(EXECUTABLE) $(OBJS) $(STATIC_OBJS) $(LIBS)
	@$(CXX) $(LINKFLAGS) -o  $(EXECUTABLE) $(OBJS) $(STATIC_OBJS) $(LIBS)
	@echo 'Finished building target: $@'
	@echo ' '

.PHONY: all dev clean dev-clean build cleanup
