This patch is what I needed to compile coreboot with LLVM.
[coreboot.git] / Makefile
1 ##
2 ## This file is part of the coreboot project.
3 ##
4 ## Copyright (C) 2008 Advanced Micro Devices, Inc.
5 ## Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
6 ## Copyright (C) 2009-2010 coresystems GmbH
7 ##
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; version 2 of the License.
11 ##
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ## GNU General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 ##
21
22 ifeq ($(INNER_SCANBUILD),y)
23 CC_real:=$(CC)
24 endif
25 $(if $(wildcard .xcompile),,$(eval $(shell bash util/xcompile/xcompile > .xcompile)))
26 include .xcompile
27 ifeq ($(INNER_SCANBUILD),y)
28 CC:=$(CC_real)
29 HOSTCC:=$(CC_real) --hostcc
30 HOSTCXX:=$(CC_real) --hostcxx
31 endif
32
33 export top := $(PWD)
34 export src := $(top)/src
35 export srck := $(top)/util/kconfig
36 export obj ?= $(top)/build
37 export objk := $(obj)/util/kconfig
38 export sconfig := $(top)/util/sconfig
39 export yapps2_py := $(sconfig)/yapps2.py
40 export config_g := $(sconfig)/config.g
41
42
43 export KERNELVERSION      := 4.0
44 export KCONFIG_AUTOHEADER := $(obj)/config.h
45 export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
46
47 CONFIG_SHELL := sh
48 KBUILD_DEFCONFIG := configs/defconfig
49 UNAME_RELEASE := $(shell uname -r)
50 HAVE_DOTCONFIG := $(wildcard .config)
51 MAKEFLAGS += -rR --no-print-directory
52
53 # Make is silent per default, but 'make V=1' will show all compiler calls.
54 Q:=@
55 ifneq ($(V),1)
56 ifneq ($(Q),)
57 .SILENT:
58 endif
59 endif
60
61 CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
62 HOSTCC = gcc
63 HOSTCXX = g++
64 HOSTCFLAGS := -I$(srck) -I$(objk) -g
65 HOSTCXXFLAGS := -I$(srck) -I$(objk)
66 LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && $(CC) -print-libgcc-file-name)
67
68 DOXYGEN := doxygen
69 DOXYGEN_OUTPUT_DIR := doxygen
70
71 ifeq ($(strip $(HAVE_DOTCONFIG)),)
72
73 all: config
74
75 else
76
77 include $(top)/.config
78
79 ARCHDIR-$(CONFIG_ARCH_X86)    := i386
80 ARCHDIR-$(CONFIG_ARCH_POWERPC) := ppc
81
82 MAINBOARDDIR=$(subst ",,$(CONFIG_MAINBOARD_DIR))
83 export MAINBOARDDIR
84
85 PLATFORM-y += src/arch/$(ARCHDIR-y) src/cpu src/mainboard/$(MAINBOARDDIR)
86 TARGETS-y :=
87
88 BUILD-y := src/lib src/boot src/console src/devices src/southbridge src/northbridge src/superio src/drivers util/x86emu
89 BUILD-y += util/cbfstool
90 BUILD-$(CONFIG_ARCH_X86) += src/pc80
91
92 ifneq ($(CONFIG_LOCALVERSION),"")
93 COREBOOT_EXTRA_VERSION := -$(subst ",,$(CONFIG_LOCALVERSION))
94 endif
95
96 # The primary target needs to be here before we include the
97 # other files
98
99 ifeq ($(INNER_SCANBUILD),y)
100 CONFIG_SCANBUILD_ENABLE:=
101 endif
102
103 ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
104 ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
105 CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
106 endif
107 all:
108         echo '#!/bin/sh' > .ccwrap
109         echo 'CC="$(CC)"' >> .ccwrap
110         echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
111         echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
112         echo 'eval $$CC $$*' >> .ccwrap
113         chmod +x .ccwrap
114         scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
115 else
116 all: coreboot
117 endif
118
119
120 #######################################################################
121 # Build the tools
122
123 CBFSTOOL:=$(obj)/util/cbfstool/cbfstool
124
125 $(obj)/mainboard/$(MAINBOARDDIR)/config.py: $(yapps2_py) $(config_g) 
126         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
127         python $(yapps2_py) $(config_g) $(obj)/mainboard/$(MAINBOARDDIR)/config.py
128
129
130 # needed objects that every mainboard uses 
131 # Creation of these is architecture and mainboard independent
132 $(obj)/mainboard/$(MAINBOARDDIR)/static.c: $(src)/mainboard/$(MAINBOARDDIR)/devicetree.cb  $(obj)/mainboard/$(MAINBOARDDIR)/config.py
133         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
134         (cd $(obj)/mainboard/$(MAINBOARDDIR) ; PYTHONPATH=$(top)/util/sconfig export PYTHONPATH; python config.py  $(MAINBOARDDIR) $(top) $(obj)/mainboard/$(MAINBOARDDIR))
135
136 $(obj)/mainboard/$(MAINBOARDDIR)/static.o: $(obj)/mainboard/$(MAINBOARDDIR)/static.c
137         @printf "    CC         $(subst $(obj)/,,$(@))\n"
138         $(CC) $(CFLAGS) -c -o $@ $<
139
140 $(obj)/arch/i386/../../option_table.o: $(obj)/arch/i386/../../option_table.c
141         @printf "    CC         $(subst $(obj)/,,$(@))\n"
142         $(CC) $(CFLAGS) -c -o $@ $<
143
144 objs:=$(obj)/mainboard/$(MAINBOARDDIR)/static.o
145 initobjs:=
146 drivers:=
147 smmobjs:=
148 crt0s:=
149 ldscripts:=
150 types:=obj initobj driver smmobj
151
152 # Clean -y variables, include Makefile.inc
153 # If $(3) is non-empty, add paths to files in X-y, and add them to Xs
154 # Add subdirs-y to subdirs
155 includemakefiles= \
156         $(foreach type,$(2), $(eval $(type)-y:=)) \
157         $(eval subdirs-y:=) \
158         $(eval -include $(1)) \
159         $(if $(strip $(3)), \
160                 $(foreach type,$(2), \
161                         $(eval $(type)s+= \
162                                 $$(patsubst util/%, \
163                                         $(obj)/util/%, \
164                                         $$(patsubst src/%, \
165                                                 $(obj)/%, \
166                                                 $$(addprefix $(dir $(1)),$$($(type)-y))))))) \
167         $(eval subdirs+=$$(subst $(PWD)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
168
169 # For each path in $(subdirs) call includemakefiles, passing $(1) as $(3)
170 # Repeat until subdirs is empty
171 evaluate_subdirs= \
172         $(eval cursubdirs:=$(subdirs)) \
173         $(eval subdirs:=) \
174         $(foreach dir,$(cursubdirs), \
175                 $(eval $(call includemakefiles,$(dir)/Makefile.inc,$(types),$(1)))) \
176         $(if $(subdirs),$(eval $(call evaluate_subdirs, $(1))))
177
178 # collect all object files eligible for building
179 subdirs:=$(PLATFORM-y) $(BUILD-y)
180 $(eval $(call evaluate_subdirs, modify))
181
182 initobjs:=$(addsuffix .initobj.o, $(basename $(initobjs)))
183 drivers:=$(addsuffix .driver.o, $(basename $(drivers)))
184 smmobjs:=$(addsuffix .smmobj.o, $(basename $(smmobjs)))
185
186 allobjs:=$(foreach var, $(addsuffix s,$(types)), $($(var)))
187 alldirs:=$(sort $(abspath $(dir $(allobjs))))
188 source_with_ext=$(patsubst $(obj)/%.o,src/%.$(1),$(allobjs))
189 allsrc=$(wildcard $(call source_with_ext,c) $(call source_with_ext,S))
190
191 POST_EVALUATION:=y
192
193 # fetch rules (protected in POST_EVALUATION) that rely on the variables filled above
194 subdirs:=$(PLATFORM-y) $(BUILD-y)
195 $(eval $(call evaluate_subdirs))
196
197
198 define objs_asl_template
199 $(obj)/$(1)%.o: src/$(1)%.asl
200         @printf "    IASL       $$(subst $(top)/,,$$(@))\n"
201         $(CPP) -D__ACPI__ -P $(CPPFLAGS) -include $(obj)/config.h -I$(src) -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $$(basename $$@).asl
202         iasl -p $$(basename $$@) -tc $$(basename $$@).asl
203         mv $$(basename $$@).hex $$(basename $$@).c
204         $(CC) $$(CFLAGS) $$(if $$(subst dsdt,,$$(basename $$(notdir $$@))), -DAmlCode=AmlCode_$$(basename $$(notdir $$@))) -c -o $$@ $$(basename $$@).c
205 endef
206
207 define objs_c_template
208 $(obj)/$(1)%.o: $(1)%.c $(obj)/config.h
209         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
210         $(CC) $$(CFLAGS) -c -o $$@ $$<
211
212 $(obj)/$(1)%.o: src/$(1)%.c $(obj)/config.h
213         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
214         $(CC) $$(CFLAGS) -c -o $$@ $$<
215 endef
216
217 define objs_S_template
218 $(obj)/$(1)%.o: $(1)%.S $(obj)/config.h
219         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
220         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
221
222 $(obj)/$(1)%.o: src/$(1)%.S $(obj)/config.h
223         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
224         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
225 endef
226
227 define initobjs_c_template
228 $(obj)/$(1)%.initobj.o: src/$(1)%.c $(obj)/config.h
229         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
230         $(CC) $$(CFLAGS) -c -o $$@ $$<
231 endef
232
233 define initobjs_S_template
234 $(obj)/$(1)%.initobj.o: src/$(1)%.S $(obj)/config.h
235         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
236         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
237 endef
238
239 define drivers_c_template
240 $(obj)/$(1)%.driver.o: src/$(1)%.c $(obj)/config.h
241         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
242         $(CC) $$(CFLAGS) -c -o $$@ $$<
243 endef
244
245 define drivers_S_template
246 $(obj)/$(1)%.driver.o: src/$(1)%.S
247         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
248         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
249 endef
250
251 define smmobjs_c_template
252 $(obj)/$(1)%.smmobj.o: src/$(1)%.c
253         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
254         $(CC) $$(CFLAGS) -c -o $$@ $$<
255 endef
256
257 define smmobjs_S_template
258 $(obj)/$(1)%.smmobj.o: src/$(1)%.S
259         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
260         $(CC) $$(CFLAGS) -c -o $$@ $$<
261 endef
262
263 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
264 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
265 $(eval $(call usetemplate,objs,asl))
266 $(eval $(call usetemplate,objs,c))
267 $(eval $(call usetemplate,objs,S))
268 $(eval $(call usetemplate,initobjs,c))
269 $(eval $(call usetemplate,initobjs,S))
270 $(eval $(call usetemplate,drivers,c))
271 $(eval $(call usetemplate,drivers,S))
272 $(eval $(call usetemplate,smmobjs,c))
273 $(eval $(call usetemplate,smmobjs,S))
274
275 printall:
276         @echo objs:=$(objs)
277         @echo initobjs:=$(initobjs)
278         @echo drivers:=$(drivers)
279         @echo smmobjs:=$(smmobjs)
280         @echo alldirs:=$(alldirs)
281         @echo allsrc=$(allsrc)
282         @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
283
284 printcrt0s:
285         @echo $(patsubst $(top)/%,%,$(crt0s))
286
287 OBJS     := $(patsubst %,$(obj)/%,$(TARGETS-y))
288 INCLUDES := -I$(top)/src -I$(top)/src/include -I$(obj) -I$(top)/src/arch/$(ARCHDIR-y)/include 
289 INCLUDES += -I$(top)/util/x86emu/include
290 INCLUDES += -include $(obj)/config.h
291
292 CFLAGS = $(INCLUDES) -Os -nostdinc
293 CFLAGS += -nostdlib -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
294 CFLAGS += -Wwrite-strings -Wredundant-decls -Wno-trigraphs 
295 CFLAGS += -Wstrict-aliasing -Wshadow
296 ifeq ($(CONFIG_WARNINGS_ARE_ERRORS),y)
297 CFLAGS += -Werror
298 endif
299 CFLAGS += -fno-common -ffreestanding -fno-builtin -fomit-frame-pointer
300
301 CBFS_COMPRESS_FLAG:=l
302 CBFS_PAYLOAD_COMPRESS_FLAG:=
303 ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZMA),y)
304 CBFS_PAYLOAD_COMPRESS_FLAG:=l
305 endif
306
307 coreboot: prepare $(obj)/coreboot.rom
308
309 endif
310
311 prepare:
312         mkdir -p $(obj)
313         mkdir -p $(obj)/util/kconfig/lxdialog $(obj)/util/cbfstool
314         test -n "$(alldirs)" && mkdir -p $(alldirs) || true
315
316 $(obj)/build.h: .xcompile
317         @printf "    GEN        build.h\n"
318         rm -f $(obj)/build.h
319         printf "/* build system definitions (autogenerated) */\n" > $(obj)/build.ht
320         printf "#ifndef __BUILD_H\n" >> $(obj)/build.ht
321         printf "#define __BUILD_H\n\n" >> $(obj)/build.ht
322         printf "#define COREBOOT_VERSION \"$(KERNELVERSION)-r$(shell if [ -d $(top)/.svn -a -f `which svnversion` ]; then svnversion $(top); else if [ -d $(top)/.git -a -f `which git` ]; then git --git-dir=/$(top)/.git log|grep git-svn-id|cut -f 2 -d@|cut -f 1 -d' '|sort -g|tail -1; fi; fi)\"\n" >> $(obj)/build.ht
323         printf "#define COREBOOT_EXTRA_VERSION \"$(COREBOOT_EXTRA_VERSION)\"\n" >> $(obj)/build.ht
324         printf "#define COREBOOT_BUILD \"`LANG= date`\"\n" >> $(obj)/build.ht
325         printf "\n" >> $(obj)/build.ht
326         printf "#define COREBOOT_COMPILER \"$(shell LANG= $(CC) --version | head -n1)\"\n" >> $(obj)/build.ht
327         printf "#define COREBOOT_ASSEMBLER \"$(shell LANG= $(AS) --version | head -n1)\"\n" >> $(obj)/build.ht
328         printf "#define COREBOOT_LINKER \"$(shell LANG= $(LD) --version | head -n1)\"\n" >> $(obj)/build.ht
329         printf "#define COREBOOT_COMPILE_TIME \"`LANG= date +%T`\"\n" >> $(obj)/build.ht
330         printf "#define COREBOOT_COMPILE_BY \"$(subst \,@,$(shell PATH=$$PATH:/usr/ucb whoami))\"\n" >> $(obj)/build.ht
331         printf "#define COREBOOT_COMPILE_HOST \"$(shell hostname -s)\"\n" >> $(obj)/build.ht
332         printf "#define COREBOOT_COMPILE_DOMAIN \"$(shell test `uname -s` = "Linux" && dnsdomainname || domainname)\"\n" >> $(obj)/build.ht
333         printf "#endif\n" >> $(obj)/build.ht
334         mv $(obj)/build.ht $(obj)/build.h
335
336 doxy: doxygen
337 doxygen:
338         $(DOXYGEN) documentation/Doxyfile.coreboot
339
340 doxyclean: doxygen-clean
341 doxygen-clean:
342         rm -rf $(DOXYGEN_OUTPUT_DIR)
343
344 clean-for-update: doxygen-clean
345         rm -f $(objs) $(initobjs) $(drivers) $(smmobjs) .xcompile
346         rm -f $(obj)/coreboot_ram* $(obj)/coreboot.romstage $(obj)/coreboot.pre* $(obj)/coreboot.bootblock $(obj)/coreboot.a
347         rm -rf $(obj)/bootblock* $(obj)/romstage* $(obj)/location.*
348         rm -f $(obj)/option_table.* $(obj)/crt0_includes.h $(obj)/ldscript
349         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/static.c $(obj)/mainboard/$(MAINBOARDDIR)/config.py $(obj)/mainboard/$(MAINBOARDDIR)/static.dot
350         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/auto.inc $(obj)/mainboard/$(MAINBOARDDIR)/crt0.s $(obj)/mainboard/$(MAINBOARDDIR)/crt0.disasm
351         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
352         $(MAKE) -C util/sconfig clean
353
354 clean: clean-for-update
355         rm -f $(obj)/coreboot* .ccwrap
356
357 distclean: clean
358         rm -rf $(obj)
359         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig*
360
361 update:
362         dongle.py -c /dev/term/1 $(obj)/coreboot.rom EOF
363
364 # This include must come _before_ the pattern rules below!
365 # Order _does_ matter for pattern rules.
366 include util/kconfig/Makefile
367
368 $(obj)/ldoptions: $(obj)/config.h
369         awk '/^#define ([^"])* ([^"])*$$/ {gsub("\\r","",$$3); print $$2 " = " $$3 ";";}' $< > $@
370
371 _OS=$(shell uname -s |cut -c-7)
372 STACK=
373 ifeq ($(_OS),MINGW32)
374         STACK=-Wl,--stack,16384000
375 endif
376 ifeq ($(_OS),CYGWIN_)
377         STACK=-Wl,--stack,16384000
378 endif
379 $(obj)/romcc: $(top)/util/romcc/romcc.c
380         @printf "    HOSTCC     $(subst $(obj)/,,$(@)) (this may take a while)\n"
381         @# Note: Adding -O2 here might cause problems. For details see:
382         @# http://www.coreboot.org/pipermail/coreboot/2010-February/055825.html
383         $(HOSTCC) -g $(STACK) -Wall -o $@ $<
384
385 .PHONY: $(PHONY) prepare clean distclean doxygen doxy coreboot .xcompile
386