#
#  Makefile for GXemul
#

BIN=gxemul
LIBS=$(XLIB) $(OTHERLIBS)

all: build

build: do_src
	$(CXX) $(COPTIM) src/components/*/*.o src/console/*.o src/cpus/*.o src/debugger/*.o src/devices/*.o src/disk/*.o src/file/*.o src/machines/*.o src/main/*.o src/main/*/*.o src/net/*.o src/old_main/*.o src/promemul/*.o src/symbol/*.o src/ui/*/*.o $(LIBS) -o $(BIN)

do_src:
	cd src; $(MAKE)

test: build
	test/check_delete_calls.sh
	@rm -f tmp_valgrind.out
	$(VALGRIND) ./$(BIN) -WW@U
	@if [ -s tmp_valgrind.out ]; then cat tmp_valgrind.out; false; fi

documentation: build
	doc/generate_machine_doc.sh
	sed s/PAGETITLE/Machines/g < doc/head.html > doc/machines.html
	./$(BIN) -WW@M >> doc/machines.html
	cat doc/tail.html >> doc/machines.html
	sed s/PAGETITLE/Components/g < doc/head.html > doc/components.html
	./$(BIN) -WW@C >> doc/components.html
	cat doc/tail.html >> doc/components.html
	$(DOXYGEN) doc/Doxyfile

install: documentation
	@echo Installing binaries, man page, and documentation...
	mkdir -p $(DESTDIR)$(PREFIX)/bin
	cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin/
	mkdir -p $(DESTDIR)$(MANDIR)/man1
	cp -f man/gxemul.1 $(DESTDIR)$(MANDIR)/man1/
	mkdir -p $(DESTDIR)$(PREFIX)/share/doc/gxemul
	cp -R doc/* $(DESTDIR)$(PREFIX)/share/doc/gxemul/

uninstall:
	@echo Removing binaries, man pages, and documentation...
	rm -f $(DESTDIR)$(PREFIX)/bin/gxemul
	rm -f $(DESTDIR)$(MANDIR)/man1/gxemul.1
	rm -rf $(DESTDIR)$(PREFIX)/share/doc/gxemul

clean:
	rm -f $(BIN) *core core.* *.gmon _* *.exe ktrace.out tmp_*.out* callgrind.out* cachegrind.out*
	rm -f unittest*.h components*.h commands*.h
	rm -rf doc/doxygen
	cd src; $(MAKE) clean

#  experiments and demos are not cleaned on a normal clean, only on a clean_all.

clean_all: clean
	cd experiments; $(MAKE) clean_all
	cd demos; $(MAKE) clean
	rm -f config.h Makefile src/Makefile src/cpus/Makefile
	rm -f src/ui/*/Makefile src/ui/Makefile src/main/fileloaders/Makefile
	rm -f src/main/commands/Makefile
	rm -f src/components/Makefile src/components/*/Makefile
	rm -f src/debugger/Makefile src/devices/Makefile
	rm -f src/devices/fonts/Makefile src/disk/Makefile
	rm -f src/file/Makefile src/machines/Makefile
	rm -f src/main/Makefile src/old_main/Makefile src/net/Makefile
	rm -f src/promemul/Makefile src/include/Makefile
	rm -f src/useremul/Makefile src/include/Makefile
	rm -f src/console/Makefile src/symbol/Makefile
	rm -f doc/machines.html doc/components.html doc/machines/machine_*.html
