# Makefile for VgaGames4

##SOURCE_DIR##
##MAKE##
##PREFIX##
##CC##
##VGAG_VERSION##
##VGAG_MAIOR##

EXTLIB_PREFIX = $(SOURCE_DIR)/extlib/exec
IOLIB_SCRIPT = $(SOURCE_DIR)/extlib/iolib-script

CFLAGS_VGLIB = -I$(SOURCE_DIR)/vglib

CFLAGS = -W -Wall -O2 -fsigned-char -g -I$(EXTLIB_PREFIX)/include
ALIB = -L$(EXTLIB_PREFIX)/lib `cat extlib/ext.libs`

#SOFLAGS = -KPIC
SOFLAGS = -fPIC
#SHAREDFLAG = -G
SHAREDFLAG = -shared

# don't change this
INCDIR = $(PREFIX)/include
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin
SHAREDIR = $(PREFIX)/share

OBJ = `./vglib/list-objs.sh`

all: so

install:
	mkdir -p $(INCDIR) || true
	mkdir -p $(LIBDIR) || true
	mkdir -p $(LIBDIR)/pkgconfig || true
	mkdir -p $(BINDIR) || true
	mkdir -p $(SHAREDIR) || true
	mkdir -p $(SHAREDIR)/vgagames$(VGAG_MAIOR) || true
	cd extlib && $(MAKE) PREFIX="$(EXTLIB_PREFIX)" DESTDIR="$(SHAREDIR)/vgagames$(VGAG_MAIOR)" install || true
	test ! -f vglib/vgagames$(VGAG_MAIOR).h && true \
	  || { cp vglib/vgagames$(VGAG_MAIOR).h $(INCDIR)/ && chmod 644 $(INCDIR)/vgagames$(VGAG_MAIOR).h; }
	test ! -f libvgagames$(VGAG_MAIOR).so.$(VGAG_VERSION) && true \
	  || sh ./_helper/so_install libvgagames$(VGAG_MAIOR).so.$(VGAG_VERSION) "$(LIBDIR)"
	test ! -f vgagames$(VGAG_MAIOR)-config && true \
	  || { cp vgagames$(VGAG_MAIOR)-config $(BINDIR)/ && chmod 755 $(BINDIR)/vgagames$(VGAG_MAIOR)-config; }
	cp -a share/* $(SHAREDIR)/vgagames$(VGAG_MAIOR)/
	$(BINDIR)/vgagames$(VGAG_MAIOR)-config --pc >$(LIBDIR)/pkgconfig/vgagames$(VGAG_MAIOR).pc

bin: all install
	cd bin && $(MAKE) CC=$(CC) BINDIR=$(BINDIR) all install

uninstall:
	cd bin && $(MAKE) BINDIR=$(BINDIR) uninstall
	rm -f $(BINDIR)/vgagames$(VGAG_MAIOR)-config
	rm -f $(LIBDIR)/libvgagames$(VGAG_MAIOR).so $(LIBDIR)/libvgagames$(VGAG_MAIOR).so.*
	rm -f $(LIBDIR)/pkgconfig/vgagames$(VGAG_MAIOR).pc
	rm -f $(INCDIR)/vgagames$(VGAG_MAIOR).h
	rm -rf $(SHAREDIR)/vgagames$(VGAG_MAIOR)

clean: subdir-clean
	cd bin && $(MAKE) clean
	rm -f *.o
	rm -f libvgagames$(VGAG_MAIOR).so libvgagames$(VGAG_MAIOR).so.*
	rm -f Makefile vgagames$(VGAG_MAIOR)-config


# *** library ***

so: subdir-all
	rm -f libvgagames$(VGAG_MAIOR).so.*
	$(CC) $(CFLAGS) $(SOFLAGS) $(SHAREDFLAG) -Wl,-h,libvgagames$(VGAG_MAIOR).so.$(VGAG_MAIOR) $(OBJ) $(ALIB) `$(IOLIB_SCRIPT) --extralibs` -lc -o libvgagames$(VGAG_MAIOR).so.$(VGAG_VERSION)


# *** files and subdirectories ***

extlib_prefix.d:
	mkdir -p $(EXTLIB_PREFIX) || true

subdir-all: extlib_prefix.d
	cd extlib && $(MAKE) PREFIX="$(EXTLIB_PREFIX)" CC="$(CC)" CFLAGS="$(CFLAGS)" all
	cd vglib && $(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS) $(CFLAGS_VGLIB)" SOFLAGS="$(SOFLAGS)" IOLIB_SCRIPT="$(IOLIB_SCRIPT)" all

subdir-clean:
	cd vglib && $(MAKE) clean || true
	cd extlib && $(MAKE) PREFIX="$(EXTLIB_PREFIX)" clean || true

