#setting targets and sub-directories
SUBDIRS = $(filter $(wildcard *),parser theorem-prover sub main )
TARGET = lite
ifeq ($(MAKECMDGOALS),all)
SUBDIRS = $(filter $(wildcard *),parser theorem-prover zchaff cprover ctool pam sato3.2.1 pacc sub main)
TARGET = all
endif
ifeq ($(MAKECMDGOALS),with-cprover)
SUBDIRS = $(filter $(wildcard *),parser theorem-prover zchaff cprover sub main)
TARGET = with-cprover
endif
ifeq ($(MAKECMDGOALS),clean)
SUBDIRS = $(filter $(wildcard *),parser theorem-prover ctool pam zchaff cprover pacc sub main)
TARGET = clean
endif
ifeq ($(MAKECMDGOALS),distclean)
SUBDIRS = $(filter $(wildcard *),parser theorem-prover zchaff cprover ctool pam sato3.2.1 pacc sub main)
TARGET = clean
endif
#valid OS types
OSTYPES = linux Windows_NT

DEPS = dummy.d
-include Makefile.common

LIB_SUBDIRS = $(filter $(SUBDIRS),main parser sub ctool pacc pam)
LIBFLAGS = $(foreach lib,$(LIB_SUBDIRS),$(lib)/lib$(lib).a)  $(wildcard theorem-prover/*/*.a) \
	   $(wildcard cprover/libcprover.a) $(wildcard zchaff/libzchaff.a) $(wildcard pam/*.a) \
	   main/libmain.a -lgmp 
BIN = main/magic

all with-cprover lite : $(BIN)

$(BIN) : magic-check $(SUBDIRS)
	$(CC) $(COMMON-CFLAGS) -o $(BIN) $(PAM-OBJS) $(LIBFLAGS) $(LIBS)

magic-check :
ifndef MAGICROOT
	$(error environment variable MAGICROOT undefined)
endif
ifeq (,$(filter $(OSTYPES),$(OSTYPE)))
	$(error unknown OS type $(OSTYPE) .. must be one of $(OSTYPES))
endif

clean distclean : magic-check $(SUBDIRS)
	rm -f $(BIN)

$(SUBDIRS) :
	$(MAKE) -C $@ $(TARGET)

.PHONY : all with-cprover lite clean distclean magic-check $(SUBDIRS)
