
#
# Define these variables from the beginning
#

CLEANFILES =
SUFFIXES =
EXTRA_DIST =


#
# Miscellaneous rules
#

lol_recursive_rule = \
	@if test -n "$(SUBDIRS)"; then for subdir in $(SUBDIRS); do \
	  ($(am__cd) $$subdir && $(MAKE) $(AM_MAKEFLAGS) $(patsubst %-recursive, %, $@)) || exit 1; \
	done; fi

bench: bench-recursive
bench-recursive:
	$(lol_recursive_rule)

lolcheck: lolcheck-recursive
lolcheck-recursive:
	$(lol_recursive_rule)


#
# Flex and Bison files build rule
#

generated: generated-recursive
generated-recursive:
	$(lol_recursive_rule)
	@if test -n "$(filter %-scanner.l, $(EXTRA_DIST))"; then \
	  echo $(MKDIR_P) generated; \
	  $(MKDIR_P) generated; \
	  rm -f generated/[a-zA-Z]*; \
	  for scanner in $(patsubst %-scanner.l, %, $(filter %-scanner.l, $(EXTRA_DIST))); do \
	    b="`basename $$scanner`"; \
	    echo flex -v -o "generated/$$b-scanner.cpp" "$$scanner-scanner.l"; \
	    flex -v -o "generated/$$b-scanner.cpp" "$$scanner-scanner.l" || exit 1; \
	  done; \
	  for parser in $(patsubst %-parser.y, %, $(filter %-parser.y, $(EXTRA_DIST))); do \
	    b="`basename $$parser`"; \
	    echo bison -o "generated/$$b-parser.cpp" --defines="generated/$$b-parser.h" -d -b "generated/$$b" "$$parser-parser.y"; \
	    bison -o "generated/$$b-parser.cpp" --defines="generated/$$b-parser.h" -d -b "generated/$$b" "$$parser-parser.y" || exit 1; \
	  done; \
	fi


#
# .lolfx build rule
#

lolfx_gen = $(lolfx_gen_$(V))
lolfx_gen_ = $(lolfx_gen_$(AM_DEFAULT_VERBOSITY))
lolfx_gen_0 = @echo "  LOLFX " $@;

SUFFIXES += .lolfx
.lolfx.o:
	$(lolfx_gen)
	$(AM_V_at)(echo "char const *"; \
         echo "lolfx_resource_$(notdir $(basename $(filter %.lolfx, $^))) ="; \
	 $(SED) 's/"/\\"/g' $(filter %.lolfx, $^) | $(SED) 's/\([^\r]*\).*/"\1\\n"/'; \
	 echo ";") \
	> $@.lolfx.cpp && \
	$(CXXCOMPILE) -xc++ -c $@.lolfx.cpp -o $@ && \
	exit=0 || exit=1; rm -f $@.lolfx.cpp ; exit "$$exit"

# Disable on the fly compilation because emscripten doesn't support it
#	| $(CXXCOMPILE) -xc++ -c - -o $@


#
# PS3 fself support rules
#

fself_gen = $(fself_gen_$(V))
fself_gen_ = $(fself_gen_$(AM_DEFAULT_VERBOSITY))
fself_gen_0 = echo "  FSELF " $${x}.self;
fself_gen_1 = echo make_fself $${x}$(EXEEXT) $${x}.self;

all-local: all-local-ps3
all-local-ps3: $(PROGRAMS) $(noinst_PROGRAMS)
	@if test -n "$^"; then for x in $(^:%$(EXEEXT)=%); do \
	  if test x$(MAKE_FSELF) != xno; then \
	    $(fself_gen) \
	    make_fself $${x}$(EXEEXT) $${x}.self; \
	  fi; \
	done; fi

CLEANFILES += $(PROGRAMS:%$(EXEEXT)=%.self) \
              $(PROGRAMS:%$(EXEEXT)=%.elf) \
              $(noinst_PROGRAMS:%$(EXEEXT)=%.self) \
              $(noinst_PROGRAMS:%$(EXEEXT)=%.elf)


#
# Win32 rules
#

# This is here in case we forget to make clean after a Windows build
CLEANFILES += $(PROGRAMS:%$(EXEEXT)=%.exe) \
              $(noinst_PROGRAMS:%$(EXEEXT)=%.exe)


#
# Emscripten rules
#

CLEANFILES += $(PROGRAMS:%$(EXEEXT)=%.data) \
              $(noinst_PROGRAMS:%$(EXEEXT)=%.data)

#
# Android rules
#

all-local: all-local-android
clean-local: clean-local-android

if USE_ANDROID
all-local-android: $(foreach p, $(PROGRAMS:%$(EXEEXT)=%), .$(p).androidstamp)
.%.androidstamp: %$(EXEEXT)
	$(eval p := $(^:%$(EXEEXT)=%))
	$(eval d := .$(p).androiddir)
	rm -rf "$(d)"
	$(MKDIR_P) "$(d)" $(sort $(foreach f, $($(p)_DATA), "$(d)/assets/$(dir $(f))"))
	$(foreach f, $($(p)_DATA), $(LN_S) "$(abs_srcdir)/$(f)" "$(d)/assets/$(f)" &&) true
	$(SED) -e 's,@PROGRAM@,$(subst -,_,$(p)),' "$(top_srcdir)/build/android/AndroidManifest.xml" > "$(d)/AndroidManifest.xml"
	$(MKDIR_P) "$(d)/src/net/lolengine"
	$(SED) -e 's,@PROGRAM@,$(subst -,_,$(p)),' "$(top_srcdir)/build/android/LolActivity.java" > "$(d)/src/net/lolengine/LolActivity.java"
	$(MKDIR_P) "$(d)/res/values"
	$(SED) -e 's,@PROGRAM@,$(subst -,_,$(p)),' "$(top_srcdir)/build/android/strings.xml" > "$(d)/res/values/strings.xml"
	$(MKDIR_P) "$(d)/res/drawable"
	cp "$(top_srcdir)/build/android/icon.png" "$(d)/res/drawable/"
	$(MKDIR_P) "$(d)/libs/armeabi"
	$(LN_S) "$(abs_builddir)/$(p)$(EXEEXT)" "$(d)/libs/armeabi/lib$(p).so"
	$(LN_S) "$${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/libs/armeabi/libstlport_shared.so" "$(d)/libs/armeabi"
	android update project -t android-9 -n "$(p)" -p "$(d)"
	ant debug -f "$(d)/build.xml"
	touch $@
else
all-local-android:
endif

clean-local-android:
	$(foreach p, $(PROGRAMS:%$(EXEEXT)=%), rm -rf ".$(p).androiddir";)
	$(foreach p, $(PROGRAMS:%$(EXEEXT)=%), rm -f ".$(p).androidstamp";)


#
# Additional rules
#

EXTRA_DIST += $(foreach p, $(PROGRAMS), $($(p)_DATA))


.PHONY: bench lolcheck generated

