summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/make/Makefile.fsm
blob: a573b065e75af2a075cb10e36351f679db6dbcd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Makefile.fsm - Makefile to build FSM.

# You need to define:
#   - FSM with the FSM prefix name
#   - FSM_SOURCES with the name of each of your FSM files.  For example, if
#     you have a example.fsm and check.fsm, you need to set FSM_SOURCES to
#     "example check".  The first one is the one including the others.

# FSM build:
#
# The %_cb.c file can not be auto-generated.  When the %_cb_skel.c is changed,
# user should check if they still are synchronised using a merge program, like
# vimdiff.
#
# To help user, the user can define the CB variable to request a particular
# behaviour.
#
# If CB=touch, ignore the auto-generation and touch the %_cb.c file.
#
# If CB=vimdiff, use vimdiff to merge differences.
#
# If CB=merge, try to merge automatically.  In this case, it uses a old copy
# of %_cb_skel.c to merge.  This will fail the first time if no old copy is
# available.

# If templates are modified, files should be rebuilt.
FSM_TEMPLATES := template_fsm.h template_fsm.c template_cb.h \
	template_cb_decl.h template_cb_impl.c \
	$(FSM_SOURCES:%=template_%_cb_skel.c)
FSM_TEMPLATES := $(FSM_TEMPLATES:%=fsm_templates/%)

# Theses files are auto-generated, remove them on clean.
FSM_FILES = $(FSM)_fsm.h $(FSM)_fsm.c $(FSM)_cb.h \
	    $(FSM_SOURCES:%=$(FSM)_%_cb_skel.c)
EXTRA_CLEAN_FILES += $(FSM_FILES) $(FSM).png

DFAGEN_DIR := $(BASE)/../../tools/dfagen
DFAGEN_DEPS := $(DFAGEN_DIR)/dfagen.py
DFAGEN := python $(DFAGEN_DIR)/dfagen.py

# Add source to build.
$(PROGS)_SOURCES += $(FSM)_fsm.c \
		    $(FSM_SOURCES:%=$(FSM)_%_cb.c) \
		    $(FSM_SOURCES:%=%.c) \
		    fsm.c

# Include general Makefile for first rule to be default.
include $(BASE)/make/Makefile.gen

# Bootstrap, fsm should be generated before fsm.h is used.
fsm.c: fsm.h
main.c: fsm.h
fsm.h: $(FSM)_fsm.h

$(DFAGEN_DIR)/dfagen.py: $(DFAGEN_DIR)/dfagen/parser.py
$(DFAGEN_DIR)/dfagen/parser.py: $(DFAGEN_DIR)/dfagen/parser.g
	yapps $<

$(FSM_FILES): $(FSM_SOURCES:%=%.fsm) $(FSM).conf $(FSM_TEMPLATES) \
	$(DFAGEN_DEPS)
	$(DFAGEN) -o c -d $< -c $(FSM).conf -p $(FSM)

.PRECIOUS: $(FSM_SOURCES:%=$(FSM)_%_cb.c)

ifndef CB
%_cb.c: %_cb_skel.c
	@echo "Please update $@"
	@false
else ifeq ($(CB),touch)
%_cb.c: %_cb_skel.c
	@echo "Updating $@ blindly"
	cp $< .$<.old
	touch $@
else ifeq ($(CB),merge)
%_cb.c: %_cb_skel.c
	@echo "Try to merge $@..."
	@if [ -r .$<.old ]; then \
		merge $@ .$<.old $<; \
		RET=$$?; \
	else \
		echo "No history, cannot merge"; \
		RET=1; \
	fi; \
	cp $< .$<.old; \
	exit $$RET
else
%_cb.c: %_cb_skel.c
	@echo "Using $(CB) to update $@"
	cp $< .$<.old
	$(CB) $@ $<
endif


png: $(FSM).png

%.png: %.dot
	dot -Tpng -o $@ $<

%.dot: $(FSM_SOURCES:%=%.fsm) $(DFAGEN_DEPS)
	$(DFAGEN) -o dot -d $< -p $(FSM)