################################
### Main makefile
################################

###
### Some general variables
###
DEPDIR = dep
SRCDIR = src
OBJDIR = obj
# eme will most probably not compile with non-gcc C/C++ compilers
CC = gcc

# System guessing
include guess.mk



###
### Default for user definable variables
###
DEFAULT_COMPATIBILITY = no
DEFAULT_DEBUG = no
DEFAULT_PROFILE = none
DEFAULT_UNICODE = yes
DEFAULT_MEMORY_POOL = yes


###
### User definable variables 
###
# Sets the variables to their default value if they weren't already defined
ifeq '$(COMPATIBILITY)' ''
  COMPATIBILITY = $(DEFAULT_COMPATIBILITY)
endif
ifeq '$(DEBUG)' ''
  DEBUG = $(DEFAULT_DEBUG)
endif
ifeq '$(PROFILE)' ''
  PROFILE = $(DEFAULT_PROFILE)
endif
ifeq '$(UNICODE)' ''
  UNICODE = $(DEFAULT_UNICODE)
endif
ifeq '$(MEMORY_POOL)' ''
  MEMORY_POOL = $(DEFAULT_MEMORY_POOL)
endif



###
### Compiler flags
###
ALL_CFLAGS = $(VERSION_FLAGS) $(MY_FLAGS) $(CFLAGS)

# System independent flags
MY_FLAGS=-Wall -fcheck-new -fno-exceptions

ifeq '$(DEBUG)' 'no'
  MY_FLAGS += -O2
else
  MY_FLAGS += -g -DDEBUG
  ALLEGRO_DEBUG = debug
  ifeq '$(DEBUG)' 'slow'
    MY_FLAGS += -DUSER_DEBUG
  endif
endif

ifeq '$(COMPATIBILITY)' 'yes'
  MY_FLAGS += -DEME__COMPATIBILITY
endif

ifneq '$(PROFILE)' 'none'
  MY_FLAGS += -DPROFILE
  ifeq '$(PROFILE)' 'all'
    MY_FLAGS += -DPROFILE_SEME
  endif
endif

ifeq '$(UNICODE)' 'yes'
  MY_FLAGS += -DUSE_UNICODE
endif

ifeq '$(MEMORY_POOL)' 'yes'
  MY_FLAGS += -DUSE_MEMORY_POOL
endif


# System dependent flags
ifeq '$(SYSTEM)' 'unix'
  ifeq '$(OVERRIDE_LIBS)' ''
    LIBS = -lstdc++ -lathm `allegro-config --libs $(ALLEGRO_DEBUG)`
  else
    LIBS = -lstdc++ $(OVERRIDE_LIBS)
  endif
  ifeq '$(OVERRIDE_INCLUDE)' ''
    MY_FLAGS += `allegro-config --cflags $(ALLEGRO_DEBUG)`
  else
    MY_FLAGS += $(OVERRIDE_INCLUDE)
  endif
  MY_FLAGS += -pipe
else
  ifeq '$(SYSTEM)' 'djgpp'
    ifeq '$(OVERRIDE_LIBS)' ''
      LIBS = -lstdcxx -lathm -lalleg
    else
      LIBS = -lstdcxx $(OVERRIDE_LIBS)
    endif
    ifeq '$(OVERRIDE_INCLUDE)' ''
    else
      MY_FLAGS += $(OVERRIDE_INCLUDE)
    endif
  else
system-error: 
	@echo Please specify the system on the command line
	@echo For gcc under Linux, works probably for other Unices
	@echo $$  make SYSTEM=unix
	@echo For djgpp under DOS
	@echo $$  make SYSTEM=djgpp
    ifeq '$(SYSTEM)' ''
	@echo Unable to guess SYSTEM
    else
	@echo Unknown system : \"$(SYSTEM)\"
    endif
  endif
endif



# List of files to compile
include files.mk

# eme version
include version.mk


###
### These are the rules.
###

###
### Default target
###
all: lib/libeme.a emetool seme ieme examples

clean: lib-clean objs-clean seme-clean ieme-clean examples-clean

distclean: lib-clean objs-clean
	-$(RM) emetool$(EXE)
	-$(RM) $(SRCDIR)/config.inc
	$(MAKE) -C seme clean
	$(MAKE) -C seme $@
	$(MAKE) -C ieme clean
	$(MAKE) -C ieme $@
	$(MAKE) -C examples clean
	$(MAKE) -C examples $@

maintainer-clean: distclean devel-clean

mostlyclean: objs-clean
	$(MAKE) -C seme $@
	$(MAKE) -C ieme $@
	$(MAKE) -C examples $@

# TODO: targets install, uninstall, install-strip, dist, check

help:
	@echo List of targets
	@echo help : Prints this help
	@echo all : Builds all
	@echo clean : Removes all generated files, but configuration files
	@echo mostlyclean : Same as clean, but keep libeme and seme and ieme executables
	@echo distclean : Removes all generated files, including configuration files
	@echo seme seme-clean : Builds, removes seme
	@echo ieme ieme-clean : Builds, removes ieme
	@echo examples examples-clean : Builds, removes examples
	@echo lib/libeme.a lib-clean : Builds, removes eme library
	@echo objs-clean : Removes the object files
	@echo depend dep-clean : Creates, removes the dependencies, works only under Unix


# Configuration testing
include config.mk

# emetool building
include emetool.mk


###
### Sub directories
###
seme ieme examples: lib/libeme.a
	$(MAKE) -C $@

seme-clean:
	$(MAKE) -C seme clean

ieme-clean:
	$(MAKE) -C ieme clean

examples-clean:
	$(MAKE) -C examples clean


###
### Library
###
lib/libeme.a: $(OBJS)
	ar rc $@ obj/*.o
	-ranlib $@

lib-clean:
	-$(RM) lib/libeme.a


###
### Object files
###
$(OBJDIR)/%.o:
	$(CC) $(ALL_CFLAGS) -c $< -o $@

ifdef DOS_SHELL
objs-clean:
	-del $(OBJDIR)/*.o
else
objs-clean:
	-$(RM) $(OBJS)
endif


###
### Dependencies
###
### Works only under unix

ifeq '$(SYSTEM)' 'unix'
depend: $(DEPS)

$(DEPDIR)/%.d: $(SRCDIR)/%.cc
	$(CC) -MM $(ALL_CFLAGS) $< > $(DEPDIR)/dep.tmp
	echo -n $(OBJDIR)/ > $@
	cat $(DEPDIR)/dep.tmp >> $@

$(DEPDIR)/%.d: $(SRCDIR)/%.c
	$(CC) -MM $(ALL_CFLAGS) $< > $(DEPDIR)/dep.tmp
	echo -n $(OBJDIR)/ > $@
	cat $(DEPDIR)/dep.tmp >> $@

ifdef DOS_SHELL
dep-clean:
	-del $(DEPDIR)/*.d
else
dep-clean:
	-$(RM) $(DEPS)
endif
endif

# include dependencies only if we're not deleting them
ifneq '$(findstring clean,$(MAKECMDGOALS))' 'clean'
include $(DEPS)
endif


###
### Devel targets
###
### These are not meant to work under DOS/Windows
 
I18N_FILES=frtext.cfg emehelp.en emehelp.fr

maintainer-help:
	@echo The following targets are only for the maintainer
	@echo devel doc i18n : Builds some things
	@echo devel-clean : Removes some things
	@echo maintainer-clean : Same as distclean, plus devel-clean

devel: doc i18n 

doc:
	$(MAKE) -C $@

i18n: $(I18N_FILES)

emehelp.%: transl/emehelp.%.latin1
	textconv -ia $< -ou $@

%text.cfg: transl/%text.cfg
	cp $< $@

devel-clean:
	$(MAKE) -C doc clean
	-$(RM) $(I18N_FILES)


.PHONY: all depend seme ieme examples help
.PHONY: devel devel-clean doc i18n
.PHONY: lib-clean objs-clean dep-clean seme-clean ieme-clean examples-clean
.PHONY: distclean maintainer-clean mostlyclean

# vim: syntax=make noet
