#(C)2006 Ren Industries
# Edited by Kuchiki Rukia, Radical Edward, and Urahara Kisuke

### BASIC CONFIGURATION ###

# Rukia: Basic information needed for the project
NAME = array
OBJECTS = amxxmodule.cpp Array.cpp ArrayNatives.cpp BinTrieNatives.cpp KeytableNatives.cpp HashtableNatives.cpp
LINK = Judy.a
INCLUDE =

OPTIMIZE = -O3 -funroll-loops
#WARN = -Wall -Werror
OMIT = -fomit-frame-pointer -fno-strict-aliasing
#ALIGN = -fPIC

# Note: Linux is not standardized. To compile for all linux distros, use all optimizations shown above.
# The following C++ features are NOT standardized, and usage will result in incompatibility:
# exceptions, rtti, STL, libgcc, libstdc++.a, virtual inheritance, vtables, some optimizations.
# Setting all USE_* macros to false will result in completely compatible .so's

USE_RTTI = false
USE_EXCEPTIONS = false
USE_STATIC_LIB = false
USE_STDLIB = false

### ADVANCED CONFIGURATION ###

# Urahara: Information for 32/64 bit processors. Useful for new AMD/Intel 64 bit chips
BIN_SUFFIX_32 = i386.so
BIN_32_FLAGS =

BIN_SUFFIX_64 = amd64.so
BIN_64_FLAGS =

# Ed: GCC version information; we recommend you use the newest GCC.
CPP = gcc
#CPP = gcc-4.1

# Ed: Release and Debug flags.
OPT_FLAGS = -s -pipe
DEBUG_FLAGS = -g -ggdb3

### end MANUAL CONFIGURATION ###

### GCC CONFIGURATION ###

# Rukia: Special GCC 4 and above code
GCC_VERSION := $(shell $(CPP) -dumpversion >&1 | cut -b1)

ifeq "$(GCC_VERSION)" "4"
        OPT_FLAGS += -fvisibility=hidden -fvisibility-inlines-hidden
endif

### BEGIN AUTO CONFIGURATION

# Rukia: Switch settings depending on if you are debugging or not
ifeq "$(DEBUG)" "true"
	BIN_DIR = Debug
	CFLAGS = $(DEBUG_FLAGS)
else
	BIN_DIR = Release
	CFLAGS = $(OPT_FLAGS)
endif

### AMD64 SUPPORT ###

# Rukia: Switch flags and ending for 64 bit
ifeq "$(AMD64)" "true"
	BINARY = $(NAME)_$(BIN_SUFFIX_64)
	CFLAGS += $(BIN_64_FLAGS)
else
	BINARY = $(NAME)_$(BIN_SUFFIX_32)
	CFLAGS += $(BIN_32_FLAGS)
	OPT_FLAGS += -march=i586
endif

### COMPATIBILITY CONFIG ###

# Rukia: Add manual configuration
CFLAGS += $(OPTIMIZE) $(WARN) $(OMIT) $(ALIGN)

# Urahara: Usage of this library is acceptable in many circumstances. Just don't expect too much.
ifeq "$(USE_STDLIB)" "true"
	CFLAGS += -static-libgcc
endif

# Urahara: Use of RTTI increases incompatibility
ifeq "$(USE_RTTI)" "false"
	CFLAGS += -fno-rtti
endif

# Urahara: Use of exceptions increases incompatibility
ifeq "$(USE_EXCEPTIONS)" "false"
	CFLAGS += -fno-exceptions
endif

# Urahara: Usage of these libraries increases incompatibility
ifeq "$(USE_STATIC_LIB)" "true"
	LINK += /lib/libgcc_eh.a /lib/libstdc++.a
endif

### END AUTO CONFIGURATION ###

### BEGIN COMPILATION ###

OBJ_LINUX := $(OBJECTS:%.cpp=$(BIN_DIR)/%.o)

$(BIN_DIR)/%.o: %.cpp
	$(CPP) $(INCLUDE) $(CFLAGS) -o $@ -c $<

all:
	mkdir -p $(BIN_DIR)
	$(MAKE) this

amd64:
	$(MAKE) all AMD64=true

this: $(OBJ_LINUX)
	$(CPP) $(INCLUDE) $(CFLAGS) $(OBJ_LINUX) $(LINK) -shared -lm -o$(BIN_DIR)/$(BINARY)

debug:	
	$(MAKE) all DEBUG=true

default: all

### END COMPILATION ###

### CLEAN UP ###

clean:
	rm -rf Release/*.o
	rm -rf Release/$(NAME)_$(BIN_SUFFIX_32)
	rm -rf Release/$(NAME)_$(BIN_SUFFIX_64)
	rm -rf Debug/*.o
	rm -rf Debug/$(NAME)_$(BIN_SUFFIX_32)
	rm -rf Debug/$(NAME)_$(BIN_SUFFIX_64)
	