SUBDIRS = $(glob D, *)

.SUBDIRS: $(SUBDIRS)
    # The default main file is the name of the test directory
    MAIN = $(basename $(absname $(CWD)))

    # The default main file can be overriden if the test directory contains
    # a file named main.om with a line "MAIN = xxxx"
    if $(file-exists main.om)
        include main.om
        export

    FILES = $(rootname $(glob *.mod))
    LPProgram($(MAIN), $(FILES))

    actual: $(TJDIS) $(MAIN).lp
        $(TJDIS) $(MAIN).lp > actual

    .PHONY: linker-test
    linker-test: actual expected
        $(DIFF) expected actual

    # This target updates the tests so they pass
    .PHONY: update-linker-test
    update-linker-test: actual
        cp actual expected

    # Tests for just the compiler - no linking step
    %.dis-actual: %.lpo $(TJDIS)
        $(TJDIS) $< >$@

    .PHONY: compiler-test
    compiler-test: $(TJDIS) $(addsuffix .dis-actual, $(FILES))
        foreach(file, $(FILES))
            $(DIFF) $(file).dis $(file).dis-actual

    # This target updates the compiler-tests so they pass
    .PHONY: update-compiler-test
    update-compiler-test: $(addsuffix .dis-actual, $(FILES))
        foreach(file, $(FILES))
            cp $(file).dis-actual $(file).dis
