we (also) build a shared lib for which some architecture require PIC code
[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 .PHONY: all
41 all: firm
42
43 # This rule is necessary so that make does not abort if headers get deleted
44 # (the deleted header might still be referenced in a .d file)
45 %.h:
46         @:
47
48 # A very naive way to create a config.h if it is missing
49 $(srcdir)config.h:
50         @echo MakeConfig $@
51         $(Q)rm -f $@
52         $(Q)echo "#define libfirm_VERSION_MAJOR 1" >> $@
53         $(Q)echo "#define libfirm_VERSION_MICRO 0" >> $@
54         $(Q)echo "#define libfirm_VERSION_MINOR 20" >> $@
55
56 # libFirm
57 libfirm_DIRS := \
58         ir         \
59         ir/adt     \
60         ir/ana     \
61         ir/arch    \
62         ir/common  \
63         ir/debug   \
64         ir/obstack \
65         ir/ident   \
66         ir/net     \
67         ir/ir      \
68         ir/lower   \
69         ir/libcore \
70         ir/lpp     \
71         ir/opt     \
72         ir/st      \
73         ir/stat    \
74         ir/tr      \
75         ir/tv      \
76         ir/kaps    \
77         ir/be
78 libfirm_SOURCES  = $(foreach dir,$(libfirm_DIRS),$(wildcard $(dir)/*.c))
79 libfirm_a        = $(builddir)/libfirm.a
80 libfirm_dll      = $(builddir)/libfirm$(DLLEXT)
81 libfirm_CPPFLAGS = -Iinclude/libfirm -Iinclude/libfirm/adt -I. $(foreach dir,$(libfirm_DIRS),-I$(dir))
82
83 .PHONY: firm
84 firm: $(libfirm_dll)
85
86 # backends
87 backends = amd64 arm ia32 sparc TEMPLATE
88
89 EMITTER_GENERATOR = $(srcdir)ir/be/scripts/generate_emitter.pl
90 REGALLOC_IF_GENERATOR = $(srcdir)ir/be/scripts/generate_regalloc_if.pl
91 OPCODES_GENERATOR = $(srcdir)ir/be/scripts/generate_new_opcodes.pl
92 MACHINE_GENERATOR = $(srcdir)ir/be/scripts/generate_machine.pl
93
94 define backend_template
95 $(1)_SOURCES = $$(wildcard ir/be/$(1)/*.c)
96 $(1)_SOURCES := $$(filter-out ir/be/$(1)/gen_%.c, $$($(1)_SOURCES))
97 $(1)_GEN_HEADERS =
98
99 $(1)_SPEC = ir/be/$(1)/$(1)_spec.pl
100
101 $$(srcdir)ir/be/$(1)/gen_$(1)_emitter.h $$(srcdir)ir/be/$(1)/gen_$(1)_emitter.c: $$($(1)_SPEC) $$(EMITTER_GENERATOR)
102         @echo GEN $$@
103         $(Q)$$(EMITTER_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
104 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_emitter.c
105 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_emitter.h
106
107 $$(srcdir)ir/be/$(1)/gen_$(1)_regalloc_if.h $$(srcdir)ir/be/$(1)/gen_$(1)_regalloc_if.c: $$($(1)_SPEC) $$(REGALLOC_IF_GENERATOR)
108         @echo GEN $$@
109         $(Q)$$(REGALLOC_IF_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
110 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_regalloc_if.c
111 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_regalloc_if.h
112
113 $$(srcdir)ir/be/$(1)/gen_$(1)_machine.h $$(srcdir)ir/be/$(1)/gen_$(1)_machine.c: $$($(1)_SPEC) $$(MACHINE_GENERATOR)
114         @echo GEN $$@
115         $(Q)$$(MACHINE_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
116 $(1)_SOURCES += ir/be/$(1)/gen_$(1)_machine.c
117 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_machine.h
118
119 $$(srcdir)ir/be/$(1)/gen_$(1)_new_nodes.h $$(srcdir)ir/be/$(1)/gen_$(1)_new_nodes.c.inl: $$($(1)_SPEC) $$(OPCODES_GENERATOR)
120         @echo GEN $$@
121         $(Q)$$(OPCODES_GENERATOR) $$($(1)_SPEC) $$(srcdir)ir/be/$(1)
122 $(1)_GEN_HEADERS += ir/be/$(1)/gen_$(1)_new_nodes.h
123
124 ir/be/$(1)/$(1)_new_nodes.c: ir/be/$(1)/gen_$(1)_new_nodes.c.inl
125
126 # We need to inform make of the headers it doesn't know yet...
127 $(1)_OBJECTS = $$($(1)_SOURCES:%.c=$$(builddir)/%.o)
128 $$($(1)_OBJECTS): $$($(1)_GEN_HEADERS)
129
130 libfirm_SOURCES += $$($(1)_SOURCES)
131 libfirm_DIRS += ir/be/$(1)
132 endef
133
134 $(foreach backend,$(backends),$(eval $(call backend_template,$(backend))))
135
136 # generators
137 IR_SPEC_GENERATED_FILES := \
138         include/libfirm/nodeops.h \
139         include/libfirm/opcodes.h \
140         ir/ir/gen_ir_cons.c.inl   \
141         ir/ir/gen_irop.c.inl      \
142         ir/ir/gen_irnode.c.inl    \
143         ir/ir/gen_irnode.h
144 IR_SPEC_GENERATOR := scripts/gen_ir.py
145 IR_SPEC := scripts/ir_spec.py
146
147 $(IR_SPEC_GENERATED_FILES): $(IR_SPEC_GENERATOR) $(IR_SPEC) scripts/spec_util.py
148         @echo GEN $@
149         $(Q)$(IR_SPEC_GENERATOR) $(IR_SPEC) ir/ir
150
151 IR_IO_GENERATOR := scripts/gen_ir_io.py
152 IR_IO_GENERATED_FILES := \
153         ir/ir/gen_irio_import.inl \
154         ir/ir/gen_irio_export.inl \
155         ir/ir/gen_irio_lex.inl
156
157 $(IR_IO_GENERATED_FILES): $(IR_IO_GENERATOR) $(IR_SPEC) scripts/spec_util.py
158         @echo GEN $@
159         $(Q)$(IR_IO_GENERATOR) $(IR_SPEC) ir/ir
160
161 ir/ir/irio.c: $(IR_IO_GENERATED_FILES)
162
163 libfirm_OBJECTS = $(libfirm_SOURCES:%.c=$(builddir)/%.o)
164 libfirm_DEPS    = $(libfirm_OBJECTS:%.o=%.d)
165 -include $(libfirm_DEPS)
166
167 $(libfirm_a): $(libfirm_OBJECTS)
168         @echo AR $@
169         $(Q)$(AR) ru $@ $^
170         @echo RANLIB $@
171         $(Q)$(RANLIB) $@
172
173 $(libfirm_dll): $(libfirm_OBJECTS)
174         @echo LINK $@
175         $(Q)$(LINK) -shared -o $@ $^
176
177 # Generic rules
178 UNUSED := $(shell mkdir -p $(libfirm_DIRS:%=$(builddir)/%))
179 $(builddir)/%.o: %.c $(IR_SPEC_GENERATED_FILES) config.h
180         @echo CC $@
181         $(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(libfirm_CPPFLAGS) -MMD -c -o $@ $<
182
183 .PHONY: clean
184 clean:
185         @echo CLEAN
186         $(Q)rm -f $(libfirm_OBJECTS)
187         $(Q)rm -f $(libfirm_TARGET)