#
# ECLiPSe III Compiler
#
# $Id: Makefile.in,v 1.7 2010/03/19 05:55:38 jschimpf Exp $
#
# BEGIN LICENSE BLOCK
# Version: CMPL 1.1
#
# The contents of this file are subject to the Cisco-style Mozilla Public
# License Version 1.1 (the "License"); you may not use this file except
# in compliance with the License.  You may obtain a copy of the License
# at www.eclipse-clp.org/license.
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
# the License for the specific language governing rights and limitations
# under the License. 
# 
# The Original Code is  The ECLiPSe Constraint Logic Programming System. 
# The Initial Developer of the Original Code is  Cisco Systems, Inc. 
# Portions created by the Initial Developer are
# Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
# 
# Contributor(s): Joachim Schimpf.
# 
# END LICENSE BLOCK

ARCH = @ARCH@
top_srcdir = @top_srcdir@
PREFIX = @prefix@

BOOTDIR = $(top_srcdir)/Kernel
STABLE_ECLIPSE = $(PREFIX)/bin/$(ARCH)/eclipse

DEBUG = off

COMPILER_MODULES = \
	ecl_compiler.ecl \
	compiler_analysis.ecl \
	compiler_codegen.ecl \
	compiler_common.ecl \
	compiler_indexing.ecl \
	compiler_normalise.ecl \
	compiler_peephole.ecl \
	compiler_regassign.ecl \
	compiler_map.ecl \
	compiler_varclass.ecl \
	source_processor.ecl

INCLUDE_FILES = \
	compiler_builtins.ecl \
	compiler_compound.ecl

OTHER_FILES = \
	compiler_test.ecl

PRIVATE_ECI = \
	$(COMPILER_MODULES:%.ecl=%.eci)

PUBLIC_ECI = \
	$(PREFIX)/lib/source_processor.eci

PUBLIC_ECL = \
	$(PREFIX)/lib/source_processor.ecl

LOCAL_ECO = $(COMPILER_MODULES:%.ecl=%.eco)

AUX_ECO = $(COMPILER_MODULES:%.ecl=aux_%.eco)

BOOT_ECO = $(COMPILER_MODULES:%.ecl=$(BOOTDIR)/lib/%.eco)

PUBLIC_ECO =


# force deletion of targets when command failed
.DELETE_ON_ERROR:

default:
	@echo "You can make the following:"
	@echo "  make aux_eco		Make stage-1 compiler (aux_xxx.eco files)"
	@echo "  make eco		Make stage-2 compiler (xxx.eco files) using stage-1"
	@echo "  make test[o]		Run unit tests (on local xxx.eco files)"
	@echo "  make bootfiles	Build and install xxx.eco files in kernel lib/"
	@echo "  make install		Build and install .eci file"
	@echo "  make test[o]_installed Run unit tests on installed compiler"
	@echo "  make ecoclean		Remove stage-2 .eco files"
	@echo "  make doc		Generate documentation in doc/"
	@echo "  make cover		Run unit tests and produce output in coverage/"
	@echo "  make pretty		Pretty-print sources in pretty/"
	@echo "  make xxx.lst		Compile to .lst assembly files"
	@echo "  make xxx.asm		Compile to .asm loadable files"



PERM = a+r,u+w,go-w
DIRPERM = 2755

.SUFFIXES:	$(SUFFIXES) .ecl .eci .eco

%/.eco/stamp:
	mkdir -p $(@D)
	@/bin/chmod $(DIRPERM) $(@D)
	@touch $@; /bin/chmod $(PERM) $@


#----------------------------------------------------------------------
# Cleanup
#----------------------------------------------------------------------

clean:	ecoclean
	rm -rf *.eci *.lst .asm *.res doc pretty coverage

# This does NOT remove the boot files in $(BOOTDIR)/lib,
# nor the aux_*.eco files (because they cannot be remade easily once
# the compiler sources have been edited)
ecoclean:
	rm -f $(LOCAL_ECO)


#----------------------------------------------------------------------
# Build new compiler .eco files using STABLE_ECLIPSE's stable
# compiler, using a two-stage process. This is necessary if
# the new code is so incompatible with the stable one that
# the modules cannot be shared during the compilation process.
#
# Stage 1: build the compiler under different module/file names
#	(aux_ecl_compiler.eco), also using aux_source_processor, etc
#
# Stage 2: compile the compiler again with this aux_ecl_compiler.eco,
#	giving ecl_compiler.eco
#
#----------------------------------------------------------------------

aux_eco:		$(AUX_ECO)

eco:			$(LOCAL_ECO)

# Replicate all changes here for aux_compiler_xxx below!
compiler_common.eco:		source_processor.eco
compiler_analysis.eco:		compiler_common.eco compiler_map.eco
compiler_codegen.eco:		compiler_common.eco \
			compiler_compound.ecl compiler_builtins.ecl
compiler_indexing.eco:		compiler_common.eco
compiler_normalise.eco:		compiler_common.eco
compiler_peephole.eco:		compiler_common.eco
compiler_regassign.eco:		compiler_common.eco
compiler_varclass.eco:		compiler_common.eco compiler_map.eco
ecl_compiler.eco:	compiler_common.eco compiler_normalise.eco \
			compiler_analysis.eco compiler_codegen.eco \
			compiler_indexing.eco compiler_peephole.eco \
			compiler_regassign.eco compiler_varclass.eco \
			source_processor.eco

aux_compiler_common.eco:	aux_source_processor.eco
aux_compiler_analysis.eco:	aux_compiler_common.eco aux_compiler_map.eco
aux_compiler_codegen.eco:	aux_compiler_common.eco \
			compiler_compound.ecl compiler_builtins.ecl
aux_compiler_indexing.eco:	aux_compiler_common.eco
aux_compiler_normalise.eco:	aux_compiler_common.eco
aux_compiler_peephole.eco:	aux_compiler_common.eco
aux_compiler_regassign.eco:	aux_compiler_common.eco
aux_compiler_varclass.eco:	aux_compiler_common.eco aux_compiler_map.eco
aux_ecl_compiler.eco:	aux_compiler_common.eco aux_compiler_normalise.eco \
			aux_compiler_analysis.eco aux_compiler_codegen.eco \
			aux_compiler_indexing.eco aux_compiler_peephole.eco \
			aux_compiler_regassign.eco aux_compiler_varclass.eco \
			aux_source_processor.eco


# Stage 1: build compiler under a different name (aux_ecl_compiler.eco)

aux_%.eco:	%.ecl auxmap.ecl
	$(STABLE_ECLIPSE) -e "compile(auxmap),\
		compile(\"$<\",\
		[output:eco(\"$@\")])"
	-@/bin/chmod $(PERM) $@

# Stage 2: build compiler_xxx.eco using the aux_ecl_compiler from stage 1
# Don't add an AUX_ECO dependency here - we want to keep stage-1 stable!
%.eco:	%.ecl
	$(STABLE_ECLIPSE) -e "ensure_loaded(aux_ecl_compiler),\
		aux_ecl_compiler:compile(\"$<\",\
		[debug:$(DEBUG),output:eco,srcroot:\"..\"])"
	-@/bin/chmod $(PERM) $@


# Single-stage build - can be done if no incompatible changes
# If using this, comment out the stage-2 rule above!
#%.eco:	%.ecl
#	$(STABLE_ECLIPSE) -e "compile(\"$<\",\
#		[debug:$(DEBUG),output:eco,load:none,srcroot:\"..\"])"
#	-@/bin/chmod $(PERM) $@


%.lst:	%.ecl
	$(STABLE_ECLIPSE) -e "ensure_loaded(aux_ecl_compiler),\
		aux_ecl_compiler:compile(\"$<\",\
		[debug:$(DEBUG),output:listing,srcroot:\"..\"])"

%.asm:	%.ecl
	$(STABLE_ECLIPSE) -e "ensure_loaded(aux_ecl_compiler),\
		aux_ecl_compiler:compile(\"$<\",\
		[debug:$(DEBUG),output:asm,srcroot:\"..\"])"


#----------------------------------------------------------------------
# Test
#----------------------------------------------------------------------

# Run unit tests and compare with reference output
# First compile the test harness using the stable compiler,
# then run it, testing the compiler in the local .eco files

test:	$(LOCAL_ECO) compiler_test.eco
	$(STABLE_ECLIPSE) -e "ensure_loaded(ecl_compiler),\
		ensure_loaded(compiler_test),ftest"
	@if diff test.ref test.res ; then echo Compiler unit test ok ; \
	else echo Compiler unit test failed ; false ; fi

testo:	$(LOCAL_ECO) compiler_test.eco
	$(STABLE_ECLIPSE) -e "ensure_loaded(ecl_compiler),\
		ensure_loaded(compiler_test),ftesto"
	@if diff testo.ref testo.res ; then echo Optimised Compiler unit test ok ; \
	else echo Compiler unit test failed ; false ; fi

test_installed:
	$(STABLE_ECLIPSE) -e "compile(compiler_test),ftest"
	@if diff test.ref test.res ; then echo Compiler unit test ok ; \
	else echo Compiler unit test failed ; false ; fi

testo_installed:
	$(STABLE_ECLIPSE) -e "compile(compiler_test),ftesto"
	@if diff testo.ref testo.res ; then echo Optimised Compiler unit test ok ; \
	else echo Compiler unit test failed ; false ; fi

refs:
	mv test.res test.ref
	mv testo.res testo.ref


# Run unit tests and generate coverage output in coverage/ subdirectory

cover:	$(LOCAL_ECO) compiler_test.eco
	@$(STABLE_ECLIPSE) -b compiler_test.eco -e ctest


#----------------------------------------------------------------------
# Installation
#----------------------------------------------------------------------

bootfiles:	$(BOOT_ECO)

$(BOOTDIR)/lib/%.eco:	%.eco
	/bin/cp $< $@
	-@/bin/chmod $(PERM) $@


install:	$(PUBLIC_ECI) $(PUBLIC_ECO) $(PUBLIC_ECL)

$(PREFIX)/lib/%.eci:	%.ecl $(PREFIX)/lib/.stamp
	$(STABLE_ECLIPSE) -e "lib(document), icompile(\"$(*F)\",\"$(@D)\")"
	-@/bin/chmod $(PERM) $@

$(PREFIX)/lib/%.ecl:	%.ecl $(PREFIX)/lib/.stamp
	/bin/cp $< $@
	@/bin/chmod $(PERM) $@


#----------------------------------------------------------------------
# Generate internal documentation
#----------------------------------------------------------------------

%.eci:	%.ecl $(BOOT_ECO)
	$(STABLE_ECLIPSE) -e "lib(document),ensure_loaded(ecl_compiler),icompile(\"$(*F)\",\"$(@D)\")"


doc:	doc/index.html

doc/index.html:	$(PRIVATE_ECI)
	$(STABLE_ECLIPSE) -e "lib(document),ecis_to_htmls(.,doc,\"\",\"ECLiPSe III Compiler\")"


# Pretty-print the sources

pretty:	pretty/index.html

pretty/index.html: $(COMPILER_MODULES)
	-@$(STABLE_ECLIPSE) -e \
	    "lib(pretty_printer),ensure_loaded(ecl_compiler),argv(all,[_|F]),pretty_print(F,[link_back:\"../doc/index.html\"])" \
	    -- $(COMPILER_MODULES)


