a firm Makefile
[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
26 # Variants
27 CFLAGS_debug      = -O0 -g3 -DDEBUG_libfirm
28 CFLAGS_profile    = -O3 -pg -DNDEBUG -fno-inline
29 LINKFLAGS_profile = -pg
30 CFLAGS_optimize   = -O3 -DNDEBUG
31
32 # General flags
33 CFLAGS    += $(CFLAGS_$(variant))
34 CFLAGS    += -Wall -W -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings
35 LINKFLAGS += $(LINKFLAGS_$(variant))
36 VPATH = $(srcdir)
37
38 .PHONY: all
39 all: firm
40
41 # A very naive way to create a config.h if it is missing
42 $(srcdir)config.h:
43         @echo MakeConfig $@
44         $(Q)rm -f $@
45         $(Q)echo "#define libfirm_VERSION_MAJOR 1" >> $@
46         $(Q)echo "#define libfirm_VERSION_MICRO 0" >> $@
47         $(Q)echo "#define libfirm_VERSION_MINOR 20" >> $@
48         $(Q)echo "#define HAVE_LONG_DOUBLE 1" >> $@
49         $(Q)echo "#define FIRM_STATISTICS" >> $@
50
51 # libFirm
52 libfirm_DIRS := \
53         ir         \
54         ir/adt     \
55         ir/ana     \
56         ir/arch    \
57         ir/common  \
58         ir/debug   \
59         ir/obstack \
60         ir/ident   \
61         ir/net     \
62         ir/ir      \
63         ir/lower   \
64         ir/libcore \
65         ir/opt     \
66         ir/st      \
67         ir/stat    \
68         ir/tr      \
69         ir/tv      \
70         ir/be
71 libfirm_SOURCES  = $(foreach dir,$(libfirm_DIRS),$(wildcard $(dir)/*.c))
72 libfirm_a        = $(builddir)/libfirm.a
73 libfirm_so       = $(builddir)/libfirm.so
74 libfirm_CPPFLAGS = -Iinclude/libfirm -Iinclude/libfirm/adt -I. $(foreach dir,$(libfirm_DIRS),-I$(dir))
75
76 .PHONY: firm
77 firm: $(libfirm_so)
78
79 # backends
80 backends = amd64 arm ia32 sparc TEMPLATE
81
82 EMITTER_GENERATOR = $(srcdir)ir/be/scripts/generate_emitter.pl
83 REGALLOC_IF_GENERATOR = $(srcdir)ir/be/scripts/generate_regalloc_if.pl
84 OPCODES_GENERATOR = $(srcdir)ir/be/scripts/generate_new_opcodes.pl
85 MACHINE_GENERATOR = $(srcdir)ir/be/scripts/generate_machine.pl
86
87 define backend_template
88 $(1)_SOURCES = $$(wildcard ir/be/$(1)/*.c)
89 $(1)_SOURCES := $$(filter-out ir/be/$(1)/gen_%.c, $$($(1)_SOURCES))
90 $(1)_GEN_HEADERS =
91
92 $(1)_SPEC = ir/be/$(1)/$(1)_spec.pl
93
94 $$(srcdir)ir/be/$(1)/gen_$(1)_emitter.h $$(srcdir)ir/be/$(1)/gen_$(1)_emitter.c: $$($(1)_SPEC) $$(EMITTER_GENERATOR)
95         @echo GEN $$@
96         $(Q)$$(EMITTER_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
97 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_emitter.c
98 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_emitter.h
99
100 $$(srcdir)ir/be/$(1)/gen_$(1)_regalloc_if.h $$(srcdir)ir/be/$(1)/gen_$(1)_regalloc_if.c: $$($(1)_SPEC) $$(REGALLOC_IF_GENERATOR)
101         @echo GEN $$@
102         $(Q)$$(REGALLOC_IF_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
103 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_regalloc_if.c
104 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_regalloc_if.h
105
106 $$(srcdir)ir/be/$(1)/gen_$(1)_machine.h $$(srcdir)ir/be/$(1)/gen_$(1)_machine.c: $$($(1)_SPEC) $$(MACHINE_GENERATOR)
107         @echo GEN $$@
108         $(Q)$$(MACHINE_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
109 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_machine.c
110 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_machine.h
111
112 $$(srcdir)ir/be/$(1)/gen_$(1)_new_nodes.h $$(srcdir)ir/be/$(1)/gen_$(1)_new_nodes.c.inl: $$($(1)_SPEC) $$(OPCODES_GENERATOR)
113         @echo GEN $$@
114         $(Q)$$(OPCODES_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
115 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_new_nodes.h
116
117 ir/be/$(1)/$(1)_new_nodes.c: ir/be/$(1)/gen_$(1)_new_nodes.c.inl
118
119 # We need to inform make of the headers it doesn't know yet...
120 $(1)_OBJECTS = $$($(1)_SOURCES:%.c=$$(builddir)/%.o)
121 $$($(1)_OBJECTS): $$($(1)_GEN_HEADERS)
122
123 libfirm_SOURCES += $$($(1)_SOURCES)
124 libfirm_DIRS += ir/be/$(1)
125 endef
126
127 $(foreach backend,$(backends),$(eval $(call backend_template,$(backend))))
128
129 # generators
130 IR_SPEC_GENERATED_FILES := \
131         include/libfirm/nodeops.h \
132         include/libfirm/opcodes.h \
133         ir/ir/gen_ir_cons.c.inl   \
134         ir/ir/gen_irop.c.inl      \
135         ir/ir/gen_irnode.c.inl    \
136         ir/ir/gen_irnode.h
137 IR_SPEC_GENERATOR := scripts/gen_ir.py
138 IR_SPEC := scripts/ir_spec.py
139
140 $(IR_SPEC_GENERATED_FILES): $(IR_SPEC_GENERATOR) $(IR_SPEC) scripts/spec_util.py
141         @echo GEN $@
142         $(Q)$(IR_SPEC_GENERATOR) $(IR_SPEC) ir/ir
143
144 IR_IO_GENERATOR := scripts/gen_ir_io.py
145 IR_IO_GENERATED_FILES := \
146         ir/ir/gen_irio_import.inl \
147         ir/ir/gen_irio_export.inl \
148         ir/ir/gen_irio_lex.inl
149
150 $(IR_IO_GENERATED_FILES): $(IR_IO_GENERATOR) $(IR_SPEC) scripts/spec_util.py
151         @echo GEN $@
152         $(Q)$(IR_IO_GENERATOR) $(IR_SPEC) ir/ir
153
154 ir/ir/irio.c: $(IR_IO_GENERATED_FILES)
155
156 libfirm_OBJECTS = $(libfirm_SOURCES:%.c=$(builddir)/%.o)
157 libfirm_DEPS    = $(libfirm_OBJECTS:%.o=%.d)
158 -include $(libfirm_DEPS)
159
160 $(libfirm_a): $(libfirm_OBJECTS)
161         @echo AR $@
162         $(Q)$(AR) ru $@ $^
163         @echo RANLIB $@
164         $(Q)$(RANLIB) $@
165
166 $(libfirm_so): $(libfirm_OBJECTS)
167         @echo LINK $@
168         $(Q)$(LINK) -shared -o $@ $^
169
170 # Generic rules
171 UNUSED := $(shell mkdir -p $(libfirm_DIRS:%=$(builddir)/%))
172 $(builddir)/%.o: %.c $(IR_SPEC_GENERATED_FILES) config.h
173         @echo CC $@
174         $(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(libfirm_CPPFLAGS) -MMD -c -o $@ $<
175
176 .PHONY: clean
177 clean:
178         @echo CLEAN
179         $(Q)rm -f $(libfirm_OBJECTS)
180         $(Q)rm -f $(libfirm_TARGET)