Compile git revision into libfirm
[libfirm] / Makefile
1 # libfirm Makefile
2 #
3 # This is currently experimental and not fully supported, but we plan to replace
4 # the jambuild with this at some point in the future.
5 #
6 # Most variable names are similar to the names used by autoconf...
7 -include config.mak
8
9 # Some build configuration defaults
10 top_srcdir   ?=
11 top_builddir ?= build
12 host         ?= unknown-host
13 variant      ?= debug
14
15 srcdir       ?= $(top_srcdir)
16 builddir     ?= $(top_builddir)/$(variant)
17
18 # This hides the noisy commandline outputs. You can see them with "make Q="
19 Q ?= @
20
21 CC ?= cc
22 LINK ?= $(CC)
23 AR ?= ar ru
24 RANLIB ?= ranlib
25 DLLEXT ?= .so
26
27 # Variants
28 CFLAGS_all        = -fPIC
29 CFLAGS_debug      = $(CFLAGS_all) -O0 -g3 -DDEBUG_libfirm
30 CFLAGS_profile    = $(CFLAGS_all) -O3 -pg -DNDEBUG -fno-inline
31 LINKFLAGS_profile = -pg
32 CFLAGS_optimize   = $(CFLAGS_all) -O3 -DNDEBUG
33
34 # General flags
35 CFLAGS    += $(CFLAGS_$(variant))
36 CFLAGS    += -Wall -W -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings
37 LINKFLAGS += $(LINKFLAGS_$(variant))
38 VPATH = $(srcdir)
39
40 REVISION ?= $(shell git describe --abbrev=40 --always --dirty --match '')
41
42 # Update revision.h if necessary
43 UNUSED := $(shell \
44         REV="\#define libfirm_VERSION_REVISION \"$(REVISION)\""; \
45         echo "$$REV" | cmp -s - firm_revision.h 2> /dev/null || echo "$$REV" > firm_revision.h \
46 )
47
48 .PHONY: all
49 all: firm
50
51 # This rule is necessary so that make does not abort if headers get deleted
52 # (the deleted header might still be referenced in a .d file)
53 %.h:
54         @:
55
56 # A very naive way to create a config.h if it is missing
57 $(srcdir)config.h:
58         @echo MakeConfig $@
59         $(Q)rm -f $@
60         $(Q)echo "#define libfirm_VERSION_MAJOR 1" >> $@
61         $(Q)echo "#define libfirm_VERSION_MICRO 0" >> $@
62         $(Q)echo "#define libfirm_VERSION_MINOR 20" >> $@
63
64 # libFirm
65 libfirm_DIRS := \
66         ir         \
67         ir/adt     \
68         ir/ana     \
69         ir/arch    \
70         ir/common  \
71         ir/debug   \
72         ir/obstack \
73         ir/ident   \
74         ir/net     \
75         ir/ir      \
76         ir/lower   \
77         ir/libcore \
78         ir/lpp     \
79         ir/opt     \
80         ir/st      \
81         ir/stat    \
82         ir/tr      \
83         ir/tv      \
84         ir/kaps    \
85         ir/be
86 libfirm_SOURCES  = $(foreach dir,$(libfirm_DIRS),$(wildcard $(dir)/*.c))
87 libfirm_a        = $(builddir)/libfirm.a
88 libfirm_dll      = $(builddir)/libfirm$(DLLEXT)
89 libfirm_CPPFLAGS = -Iinclude/libfirm -Iinclude/libfirm/adt -I. $(foreach dir,$(libfirm_DIRS),-I$(dir))
90
91 .PHONY: firm
92 firm: $(libfirm_dll)
93
94 # backends
95 backends = amd64 arm ia32 sparc TEMPLATE
96
97 EMITTER_GENERATOR = $(srcdir)ir/be/scripts/generate_emitter.pl
98 EMITTER_GENERATOR2 = $(srcdir)ir/be/scripts/generate_emitter_new.pl
99 REGALLOC_IF_GENERATOR = $(srcdir)ir/be/scripts/generate_regalloc_if.pl
100 OPCODES_GENERATOR = $(srcdir)ir/be/scripts/generate_new_opcodes.pl
101 MACHINE_GENERATOR = $(srcdir)ir/be/scripts/generate_machine.pl
102
103 define backend_template
104 $(1)_SOURCES = $$(wildcard ir/be/$(1)/*.c)
105 $(1)_SOURCES := $$(filter-out ir/be/$(1)/gen_%.c, $$($(1)_SOURCES))
106 $(1)_GEN_HEADERS =
107
108 $(1)_SPEC = ir/be/$(1)/$(1)_spec.pl
109
110 $$(srcdir)ir/be/$(1)/gen_$(1)_emitter.h $$(srcdir)ir/be/$(1)/gen_$(1)_emitter.c: $$($(1)_SPEC) $$(EMITTER_GENERATOR) $(EMITTER_GENERATOR2)
111         @echo GEN $$@
112         $(Q)$$(EMITTER_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
113 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_emitter.c
114 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_emitter.h
115
116 $$(srcdir)ir/be/$(1)/gen_$(1)_regalloc_if.h $$(srcdir)ir/be/$(1)/gen_$(1)_regalloc_if.c: $$($(1)_SPEC) $$(REGALLOC_IF_GENERATOR)
117         @echo GEN $$@
118         $(Q)$$(REGALLOC_IF_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
119 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_regalloc_if.c
120 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_regalloc_if.h
121
122 $$(srcdir)ir/be/$(1)/gen_$(1)_machine.h $$(srcdir)ir/be/$(1)/gen_$(1)_machine.c: $$($(1)_SPEC) $$(MACHINE_GENERATOR)
123         @echo GEN $$@
124         $(Q)$$(MACHINE_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
125 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_machine.c
126 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_machine.h
127
128 $$(srcdir)ir/be/$(1)/gen_$(1)_new_nodes.h $$(srcdir)ir/be/$(1)/gen_$(1)_new_nodes.c.inl: $$($(1)_SPEC) $$(OPCODES_GENERATOR)
129         @echo GEN $$@
130         $(Q)$$(OPCODES_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
131 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_new_nodes.h
132
133 ir/be/$(1)/$(1)_new_nodes.c: ir/be/$(1)/gen_$(1)_new_nodes.c.inl
134
135 # We need to inform make of the headers it doesn't know yet...
136 $(1)_OBJECTS = $$($(1)_SOURCES:%.c=$$(builddir)/%.o)
137 $$($(1)_OBJECTS): $$($(1)_GEN_HEADERS)
138
139 libfirm_SOURCES += $$($(1)_SOURCES)
140 libfirm_DIRS += ir/be/$(1)
141 endef
142
143 $(foreach backend,$(backends),$(eval $(call backend_template,$(backend))))
144
145 # generators
146 IR_SPEC_GENERATED_FILES := \
147         include/libfirm/nodeops.h \
148         include/libfirm/opcodes.h \
149         ir/ir/gen_ir_cons.c.inl   \
150         ir/ir/gen_irop.c.inl      \
151         ir/ir/gen_irnode.c.inl    \
152         ir/ir/gen_irnode.h
153 IR_SPEC_GENERATOR := scripts/gen_ir.py
154 IR_SPEC := scripts/ir_spec.py
155
156 $(IR_SPEC_GENERATED_FILES): $(IR_SPEC_GENERATOR) $(IR_SPEC) scripts/spec_util.py
157         @echo GEN $@
158         $(Q)$(IR_SPEC_GENERATOR) $(IR_SPEC) ir/ir
159
160 IR_IO_GENERATOR := scripts/gen_ir_io.py
161 IR_IO_GENERATED_FILES := \
162         ir/ir/gen_irio_import.inl \
163         ir/ir/gen_irio_export.inl \
164         ir/ir/gen_irio_lex.inl
165
166 $(IR_IO_GENERATED_FILES): $(IR_IO_GENERATOR) $(IR_SPEC) scripts/spec_util.py
167         @echo GEN $@
168         $(Q)$(IR_IO_GENERATOR) $(IR_SPEC) ir/ir
169
170 ir/ir/irio.c: $(IR_IO_GENERATED_FILES)
171
172 libfirm_OBJECTS = $(libfirm_SOURCES:%.c=$(builddir)/%.o)
173 libfirm_DEPS    = $(libfirm_OBJECTS:%.o=%.d)
174 -include $(libfirm_DEPS)
175
176 $(libfirm_a): $(libfirm_OBJECTS)
177         @echo AR $@
178         $(Q)$(AR) ru $@ $^
179         @echo RANLIB $@
180         $(Q)$(RANLIB) $@
181
182 $(libfirm_dll): $(libfirm_OBJECTS)
183         @echo LINK $@
184         $(Q)$(LINK) -shared -o $@ $^
185
186 # Generic rules
187 UNUSED := $(shell mkdir -p $(libfirm_DIRS:%=$(builddir)/%))
188 $(builddir)/%.o: %.c $(IR_SPEC_GENERATED_FILES) config.h
189         @echo CC $@
190         $(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(libfirm_CPPFLAGS) -MMD -c -o $@ $<
191
192 .PHONY: clean
193 clean:
194         @echo CLEAN
195         $(Q)rm -f $(libfirm_OBJECTS)
196         $(Q)rm -f $(libfirm_TARGET)