#############################################
# Makefile.linux - linux makefile
# Copyright (C) 2017 mittorn

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
##############################################

# Default options - optimized to debug on local machine
CC ?= gcc
CXX ?= g++
CFLAGS ?= -g -O1 -fsigned-char -Wall -Wextra -Wsign-compare -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-but-set-variable
LDFLAGS =
LBITS := $(shell getconf LONG_BIT)
DEPS :=

XASH_COMMIT := $(firstword $(shell git rev-parse --short=6 HEAD) unknown)

ifeq ($(XASH_COMMIT),unknown)
$(warning You seems to build xash3d without git)
$(warning Please use git if you are going to publish your build)
endif

# Pass 64BIT=1 to make arguments to allow amd64 builds
ifneq ($(64BIT),1)
	ifeq ($(LBITS),64)
		LDFLAGS += -m32
		CFLAGS += -m32
	endif
endif

TOPDIR = $(PWD)/..
INCLUDES :=
XASH_SINGLE_BINARY ?= 1
INSTALL_DIR ?= ./install/
ifeq ($(NANOGL),1)
INCLUDES += -Inanogl -Inanogl/GL
endif
INCLUDES += -I/usr/include/SDL2 -Icommon -I../common -I. -I../pm_shared -Iclient -Iserver -Iclient/vgui -Icommon/sdl

#############################
# Preprocessor defines:
#############################
DEFINES = 

# Specify commit hash in version string
DEFINES += -DXASH_BUILD_COMMIT=\"$(XASH_COMMIT)\"


# Only SDL backend exists on linux
ifeq ($(XASH_DEDICATED),1)
   DEFINES += -DXASH_DEDICATED
else
   DEFINES += -DXASH_SDL
   LIBS += -lSDL2
endif

#############################################
# GL/GLES translators.
# You need clone it to engine folder to use
# Only one translator should be enabled
#############################################
ifeq ($(NANOGL),1)
DEFINES += -DXASH_NANOGL -D__MULTITEXTURE_SUPPORT__  -DEGL_LIB=\"libEGL.so\"
endif

ifeq ($(WES),1)
DEFINES += -DXASH_WES -D__MULTITEXTURE_SUPPORT__  -DEGL_LIB=\"libEGL.so\"
endif

ifeq ($(REGAL),1)
DEFINES += -DXASH_REGAL -D__MULTITEXTURE_SUPPORT__  -DEGL_LIB=\"regal/lib/linux-32/libRegal.so\"
LIBS += regal/lib/linux-32/libRegal.so
endif

# Some libc implementations cannot use libdl in static builds, so disable it by default
ifeq ($(XASH_STATIC),1)

    ifneq ($(XASH_STATIC_LIBDL),1)
	DEFINES += -DNO_LIBDL
    endif

    XASH_SINGLE_BINARY := 1
endif

ifneq ($(XASH_STATIC),1)
    LIBS += -ldl
endif

ifeq ($(XASH_STATIC_LIBDL),1)
    LIBS += -ldl
endif

LIBS += -lm -pthread


ifeq ($(XASH_SINGLE_BINARY),1)
    DEFINES += -DSINGLE_BINARY
endif

# Collect files
SRCS_CPP =
SRCS = $(wildcard server/*.c) $(wildcard client/vgui/*.c) $(wildcard client/avi/*.c) $(wildcard common/*.c) $(wildcard common/imagelib/*.c) $(wildcard common/soundlib/*.c) $(wildcard common/soundlib/libmpg/*.c)
ifneq ($(XASH_DEDICATED),1)
	SRCS += $(wildcard client/*.c)
	SRCS += $(wildcard platform/sdl/*.c)
	ifeq ($(WES),1)
		SRCS += $(wildcard gl-wes-v2/src/*.c)
	endif
	ifeq ($(NANOGL),1)
		SRCS_CPP += $(wildcard nanogl/*.cpp)
	endif
endif
OBJS = $(patsubst %.c,obj/%.o,$(SRCS))
OBJS_CPP = $(patsubst %.cpp,obj/%.o,$(SRCS_CPP))

#################################
# Windows DLL loader
# Should be enabled only on i386 builds
#################################
LOADER :=
ifeq ($(XASH_DLL_LOADER),1)
	DEFINES += -DDLL_LOADER
	ifeq ($(XASH_SINGLE_BINARY),1)
		LOADER = libloader.a
	else
		LOADER = libloader.so
	endif
	DEPS += $(LOADER)
	LIBS += $(LOADER)
endif

# Rules for binaries
ifeq ($(XASH_SINGLE_BINARY),0)
	BINARIES = libxash.so
libxash.so : $(OBJS) $(OBJS_CPP) $(DEPS)
		$(CC) -fvisibility=hidden -o libxash.so $(LDFLAGS) -shared $(OBJS) $(OBJS_CPP) $(LIBS)

else
BINARIES = xash
xash: $(OBJS) $(OBJS_CPP) $(DEPS)
ifeq ($(XASH_STATIC),1)
		$(CC) -o xash -static $(LDFLAGS) $(OBJS) $(OBJS_CPP) $(LIBS)
else
		$(CC) -o xash -fvisibility=hidden $(LDFLAGS) $(OBJS) $(OBJS_CPP) $(LIBS)
endif

endif

ifeq ($(XASH_DLL_LOADER),1)
$(LOADER):
	$(MAKE) -f ../loader/Makefile.linux -C ../loader $(LOADER)
	cp ../loader/$(LOADER) .
endif

# Create dirs for object files
DIRS := obj obj/server obj/client/avi obj/client/vgui obj/common/sdl obj/common/imagelib obj/common/soundlib/libmpg obj/platform/sdl obj/nanogl
$(OBJS): | $(DIRS)

$(DIRS) :
	mkdir -p $(DIRS)

# Object rules
obj/%.o : %.c
	$(CC) $(CFLAGS) $(INCLUDES) $(DEFINES) -c "$<" -o "$@"  -Wno-unused-result -fvisibility=hidden

obj/%.o : %.cpp
	$(CXX) $(CFLAGS) $(INCLUDES) $(DEFINES) -c "$<" -o "$@"


default: $(BINARIES)

.PHONY: depend clean list install

clean:
	$(RM) $(OBJS) $(OBJS_CPP) $(BINARIES) ../loader/*.o ../loader/*.a ../loader/*.so $(LOADER)

list:
	@echo Sources:
	@echo $(SRCS)
	@echo C++ Sources:
	@echo $(SRCS_CPP)
	@echo Objects:
	@echo $(OBJS) $(OBJS_CPP)
	@echo Dirs:
	@echo $(DIRS)
	@echo Dependencies:
	@echo $(DEPS)

# Simple install rule
install: $(BINARIES)
	mkdir -p $(INSTALL_DIR)

ifeq ($(XASH_SINGLE_BINARY),1)
	cp xash $(INSTALL_DIR)/xash_bin
else
	cp libxash.so $(INSTALL_DIR)/
	cp libloader.so $(INSTALL_DIR)/
endif
