Skip to content

Instantly share code, notes, and snippets.

@Aearsis
Created February 22, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aearsis/f092bb941e92d0dec3f8a8af15c7b6ee to your computer and use it in GitHub Desktop.
Save Aearsis/f092bb941e92d0dec3f8a8af15c7b6ee to your computer and use it in GitHub Desktop.
Yet another Makefile for compiler principles
DU=$(notdir $(shell pwd))
ROOT=../../..
RO=${ROOT}/public-ro
TEST_PATH=${RO}/mlc/$(DU)/tests/
TESTS=$(shell echo $(TEST_PATH)/*.mls)
INTERMEDIATE=${DU}l.cpp ${DU}g.cpp ${DU}g.hpp
GEN_HPP=gen_ainstr.hpp gen_icopcodes.hpp gen_icinstr.hpp
GEN_OBJ=gen_icinstrtab.o gen_icfactory.o gen_aifactory.o
CPP=g++
CPPFLAGS+=-I./
CPPFLAGS+=-I${RO}/common/h
CPPFLAGS+=-I${RO}/mlc/h
CPPFLAGS+=-I${RO}/mlc/${DU}/h
#CPPFLAGS+=-D_DEBUG -DYYDEBUG
CPPFLAGS+=-Wall -std=c++11 -g
SRC=$(shell xmllint --xpath '//Files/Filter[1]/File/@RelativePath' mlc.vc8.vcproj | sed -r 's/RelativePath="([^"]*)"/\1/g' | sed 's/\\/\//g')
all: ${DU}
@rm -f ${INTERMEDIATE}
${DU}: $(SRC:.cpp=.o) ${DU}l.o
@echo "[ LD ]" $@
@${CPP} ${CPPFLAGS} $^ -o $@
ifneq ($(filter ${DU}, du4 du5 du6),)
${DU}: ${GEN_OBJ} ${DU}sem.o
${DU}sem.o: ${DU}sem.h ${DU}lval.hpp
%.o: %.cpp ${GEN_HPP}
else
%.o: %.cpp
endif
@echo "[ CC ]" $@
@${CPP} ${CPPFLAGS} $< -c -o $@
#
# Lexical
#
ifneq ($(filter ${DU}, du3 du4 du5 du6),)
${DU}: ${DU}g.o
%l.cpp: %l.lex %g.hpp
else
%l.cpp: %l.lex
endif
@echo "[ FLEX ]" $<
@flex -Cf -b -p -p -o $@ $<
#
# Syntax
#
%g.cpp %g.hpp: %g.y
@echo "[ BISON ]" $<
@bison --report=all -d --graph -o $*g.cpp $<
#
# Testing
#
test: $(TESTS:mls=test-ok)
%.test-out: %.mls $(DU)
@echo "[ TEST ]" $*
@./$(DU) $< $(@:test-out=test-mo) -D$*.test-stcd >$@
%.test-icmout: $(DU) icm %.test-out # hack - .test-mo -> test-out
@echo "[ ICM ]" $*
@touch $(@:test-icmout=icmin)
@./icm $(@:test-icmout=test-mo) stack >$@ <$(@:test-icmout=icmin)
%.test-ok: %.test-out %.test-icmout
@echo "[ CHECK ]" $*
@diff -q $(@:test-ok=test-out) $(@:test-ok=out)
#@diff -q $(@:test-ok=test-stcd) $(@:test-ok=stcd)
@diff -q $(@:test-ok=test-icmout) $(@:test-ok=icmout)
#
# Instruction sets
#
XML_PATH=${RO}/common/xml
ICM_XML_PATH=${RO}/icm/xml
gen_aifactory.cpp: ${XML_PATH}/genaifctrc.xslt
gen_ainstr.hpp: ${XML_PATH}/genainstrh.xslt
gen_icfactory.cpp: ${XML_PATH}/genicfctrc.xslt
gen_icinstr.hpp: ${XML_PATH}/genicinstrh.xslt
gen_icopcodes.hpp: ${XML_PATH}/genicoph.xslt
gen_icinstrtab.cpp: ${XML_PATH}/genictabc.xslt
gen_instr_decode.cpp: ${ICM_XML_PATH}/geninstrdecodec.xslt
gen_machines.cpp: ${ICM_XML_PATH}/genmachc.xslt
gen_mopcodes.hpp: ${ICM_XML_PATH}/genmopcodh.xslt
gen_mops.cpp: ${ICM_XML_PATH}/genmopsc.xslt
gen_%pp: ${XML_PATH}/icminstr.xml
@echo "[ XSLT ]" $@
@xsltproc --output $@ $(lastword $^) $<
#
# ICM
#
ICM_SRC=${RO}/common/src/flat_icblock.cpp ${RO}/common/src/ic_instr.cpp \
${RO}/icm/src/icexcept.cpp ${RO}/icm/src/icm.cpp ${RO}/icm/src/icm_machine.cpp \
gen_icinstrtab.o gen_icfactory.o
GEN_ICM=gen_instr_decode.cpp gen_machines.cpp gen_mopcodes.hpp gen_mops.cpp
icm: ${GEN_ICM} ${GEN_HPP} ${ICM_SRC}
@echo "[ CC ]" $@
@${CPP} ${CPPFLAGS} -I${RO}/icm/h ${GEN_ICM} ${ICM_SRC} -o $@
#
# Managing
#
clean:
@echo "[ CLEAN ]"
@rm -f *g.hpp *g.cpp *l.cpp *.o ${DU}g.output ${DU}g.dot $(SRC:.cpp=.o) ${DU} gen_* Makefile.xslt *.vcxproj *.sln *vc9* *.filters
@rm -f ${TESTS:mls=testout} ${TESTS:mls=teststcd} ${TESTS:mls=test-mo}
.PHONY: all test clean
# Hack: Silent removal of intermediate files
.SECONDARY: ${INTERMEDIATE} $(TESTS:mls=testout) $(TESTS:mls=test-stcd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment