# Makefile.def

# Set variables:
#
# for install:
#    ASL - path name of iASL compiler
#
# Internal variables to be pre-defined:
#   TOP    - relative name of aslts directory
#   AMLMOD - name of resulting AML module (DefinitionBlock->AMLFileName)
#            without .aml extension.

# Two level hierarchy of compilation:
# optimization & (32/64 mode).
#
#  Directory |           Flags
#            |---------------------------
#            | optimization |  32/64 mode
# ---------------------------------------
#    opt/32  |              |  -r 1
#    opt/64  |              |  -r 2
#   nopt/32  |  -oa         |  -r 1
#   nopt/64  |  -oa         |  -r 2

SETOF_INDEXES= 0 1 2 3
SETOF_AMLDIRS= nopt/32 nopt/64 opt/32 opt/64
SETOF_ASLFAGS= "-oa -r 1" "-oa -r 2" "-r 1" "-r 2"
COMMON_ASL_FLAGS= "-cr -vs -l -sc -sa -ic -ta -ts -so -lm -ln -ls -li"

COMPILER_LOG="$(TOP)/tmp/aml/$(aslversion)/compile.txt"
COMPILER_ERROR_LOG="$(TOP)/tmp/aml/$(aslversion)/error.txt"

# Create general directory

GDMODE=775
INST= install
INST.dir= $(INST) -d -m $(GDMODE) $@
ASLMOD= MAIN

# Empty all (specify 'all' target, if needed, before Makefile.def include)

empty_all:	FORCE

# Rule to create directories located into aslts/tmp/aml;
# aslversion is determined dynamically.

${SETOF_AMLDIRS:%=$(TOP)/tmp/aml/$(aslversion)/%}:
	@$(INST.dir)

# Make-install one particular Test Case for all modes

install_all_modes_of_test_case:	${SETOF_AMLDIRS:%=$(TOP)/tmp/aml/$(aslversion)/%}
	@rval=0; \
	if [ -f "$(ASL)" ]; then \
		dd=`pwd`; \
		echo "---- Test path: $$dd" >> $(COMPILER_LOG); \
		echo "---- Test path: $$dd" >> $(COMPILER_ERROR_LOG); \
		for i in ${SETOF_INDEXES}; do \
			set -- $(SETOF_AMLDIRS); \
			shift $$i; \
			CUR_AMLDIR=$$1; \
			set -- $(SETOF_ASLFAGS); \
			shift $$i; \
			CUR_ASLFLAGS=$$1; \
			echo "---- Test type: $$CUR_AMLDIR   (Flags $(COMMON_ASL_FLAGS) $$CUR_ASLFLAGS $(ADD_ASLFLAGS))" >> $(COMPILER_LOG); \
			echo "---- Test type: $$CUR_AMLDIR   (Flags $(COMMON_ASL_FLAGS) $$CUR_ASLFLAGS $(ADD_ASLFLAGS))" >> $(COMPILER_ERROR_LOG); \
			for j in ${AMLMOD}; do \
				rm -f $$j.aml; \
			done; \
			for j in ${ASLMOD} $(ASLMODADD); do \
				echo "---- Compile: $$j.asl" >> $(COMPILER_LOG); \
				"$(ASL)" $$CUR_ASLFLAGS "$(COMMON_ASL_FLAGS)" $(ADD_ASLFLAGS) $$j.asl >> $(COMPILER_LOG) 2>> $(COMPILER_ERROR_LOG); \
				ret=$$?; \
				echo "" >> $(COMPILER_LOG); \
				rm $$j.asm; \
				rm $$j.c; \
				rm $$j.h; \
				rm $$j.i; \
				rm $$j.hex; \
				rm $$j.lst; \
				rm $$j.map; \
				rm $$j.nsp; \
				rm $$j.offset.h; \
				rm $$j.src; \
				if [ $$ret != 0 ]; then \
					rval=1; \
					>&2 echo "**** Unexpected iASL failure in $$dd/$$j.asl!"; \
					exit 1; \
				fi; \
			done; \
			if [ $$ret != 0 ]; then break; fi; \
			for j in ${AMLMOD}; do \
				ls -l $$j.aml >> $(COMPILER_LOG); \
				echo "---- Move: $$j.aml $(TOP)/tmp/aml/$(aslversion)/$$CUR_AMLDIR" >> $(COMPILER_LOG); \
				mv $$j.aml $(TOP)/tmp/aml/$(aslversion)/$$CUR_AMLDIR; \
				ret=$$?; \
				if [ $$ret != 0 ]; then rval=2; echo "**** mv failed!"; break; fi; \
			done; \
		done; \
	else \
		echo "Bad iASL 0: <$(ASL)> does not exist"; \
		rval=1; \
	fi; \
	if [ $$rval != 0 ]; then exit 1; fi

# Make-install one particular Test Case for all modes.
# Move results to aslts/tmp/aml directory.
# Determine current version of iASL.
# Create aslts/tmp/aml/<version> directory with the precautions.

install_test_case:
	@if [ ! -f "$(ASL)" ]; then \
		echo "Bad iASL 1: <$(ASL)> does not exist"; exit 1; \
	else \
		aslversion=""; \
		testcasedir=`pwd`; \
		cd $(TOP); \
		if [ $$? -ne 0 ]; then \
			echo "Bad dir: $(TOP)"; exit 1; \
		else \
			dd=`pwd`; \
			base=`basename "$$dd"`; \
			if [ x$$base != xaslts ]; then \
				echo "Bad base name of ASLTS Test Suite: <$$base> of <$$dd>"; exit 1; \
			else \
				x=`"$(ASL)" -va xxx | grep "Compiler version"`; \
				echo "---- iASL version: $$x"; \
				if [ -z "$$x" ]; then \
					echo "Bad iASL 2: bad signon <$(ASL)>"; exit 1; \
				else \
					aslversion=`echo "$$x" | awk -F" " '{ print $$5}'`; \
					if [ -z "$$aslversion" ]; then \
						echo "Bad iASL 3: could not get version <$(ASL)>"; exit 1; \
					else \
						if [ ! -d ./tmp ]; then \
							mkdir tmp; \
						fi; \
						cd ./tmp; \
						if [ $$? -ne 0 ]; then \
							echo "Bad dir: aslts/tmp"; exit 1; \
						else \
							if [ ! -d ./aml ]; then \
								mkdir aml; \
							fi; \
							cd ./aml; \
							if [ $$? -ne 0 ]; then \
								echo "Bad dir: aslts/tmp/aml"; exit 1; \
							else \
								if [ ! -d "./$$aslversion" ]; then \
									mkdir $$aslversion; \
								fi; \
								cd ./$$aslversion; \
								if [ $$? -ne 0 ]; then \
									echo "Bad dir: aslts/tmp"; exit 1; \
								else \
									cd "$$testcasedir"; \
									export aslversion; make install_all_modes_of_test_case; \
								fi; \
							fi; \
						fi; \
					fi; \
				fi; \
			fi; \
		fi; \
	fi

# Compile one particular Test Case for all modes.
# No moving results to aslts/tmp/aml directory.

compile_test_case:
	@rval=0; \

	echo "Compile_test_case"

	if [ -f "$(ASL)" ]; then \
		for i in ${SETOF_INDEXES}; do \
			set -- $(SETOF_AMLDIRS); \
			shift $$i; \
			CUR_AMLDIR=$$1; \
			set -- $(SETOF_ASLFAGS); \
			shift $$i; \
			CUR_ASLFLAGS=$$1; \
			for j in ${ASLMOD} $(ASLMODADD); do \
				"$(ASL)" $$CUR_ASLFLAGS $(ADD_ASLFLAGS) $$j.asl; \
				ret=$$?; \
				if [ $$ret != 0 ]; then rval=1; echo "**** Unexpected iASL failure!"; exit 1; fi; \
			done; \
		done; \
	else \
		echo "Bad iASL 4: <$(ASL)> does not exist"; \
		rval=1; \
	fi; \
	if [ $$rval != 0 ]; then exit 1; fi

