Add scan-build support to the build system.
[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 $(CC) -print-libgcc-file-name)
67
68 DESTDIR = /opt
69
70 DOXYGEN := doxygen
71 DOXYGEN_OUTPUT_DIR := doxygen
72
73 ifeq ($(strip $(HAVE_DOTCONFIG)),)
74
75 all: config
76
77 else
78
79 include $(top)/.config
80
81 ARCHDIR-$(CONFIG_ARCH_X86)    := i386
82 ARCHDIR-$(CONFIG_ARCH_POWERPC) := ppc
83
84 MAINBOARDDIR=$(subst ",,$(CONFIG_MAINBOARD_DIR))
85 export MAINBOARDDIR
86
87 PLATFORM-y += src/arch/$(ARCHDIR-y) src/cpu src/mainboard/$(MAINBOARDDIR)
88 TARGETS-y :=
89
90 BUILD-y := src/lib src/boot src/console src/devices src/southbridge src/northbridge src/superio src/drivers util/x86emu
91 BUILD-y += util/cbfstool
92 BUILD-$(CONFIG_ARCH_X86) += src/pc80
93
94 ifneq ($(CONFIG_LOCALVERSION),"")
95 COREBOOT_EXTRA_VERSION := -$(subst ",,$(CONFIG_LOCALVERSION))
96 endif
97
98 # The primary target needs to be here before we include the
99 # other files
100
101 ifeq ($(INNER_SCANBUILD),y)
102 CONFIG_SCANBUILD_ENABLE:=
103 endif
104
105 ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
106 ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
107 CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
108 endif
109 all:
110         echo '#!/bin/sh' > .ccwrap
111         echo 'CC="$(CC)"' >> .ccwrap
112         echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
113         echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
114         echo 'eval $$CC $$*' >> .ccwrap
115         chmod +x .ccwrap
116         scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
117 else
118 all: coreboot
119 endif
120
121
122 #######################################################################
123 # Build the tools
124
125 CBFSTOOL:=$(obj)/util/cbfstool/cbfstool
126
127 $(obj)/mainboard/$(MAINBOARDDIR)/config.py: $(yapps2_py) $(config_g) 
128         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
129         python $(yapps2_py) $(config_g) $(obj)/mainboard/$(MAINBOARDDIR)/config.py
130
131
132 # needed objects that every mainboard uses 
133 # Creation of these is architecture and mainboard independent
134 $(obj)/mainboard/$(MAINBOARDDIR)/static.c: $(src)/mainboard/$(MAINBOARDDIR)/devicetree.cb  $(obj)/mainboard/$(MAINBOARDDIR)/config.py
135         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
136         (cd $(obj)/mainboard/$(MAINBOARDDIR) ; PYTHONPATH=$(top)/util/sconfig export PYTHONPATH; python config.py  $(MAINBOARDDIR) $(top) $(obj)/mainboard/$(MAINBOARDDIR))
137
138 $(obj)/mainboard/$(MAINBOARDDIR)/static.o: $(obj)/mainboard/$(MAINBOARDDIR)/static.c
139         @printf "    CC         $(subst $(obj)/,,$(@))\n"
140         $(CC) $(CFLAGS) -c -o $@ $<
141
142 $(obj)/arch/i386/../../option_table.o: $(obj)/arch/i386/../../option_table.c
143         @printf "    CC         $(subst $(obj)/,,$(@))\n"
144         $(CC) $(CFLAGS) -c -o $@ $<
145
146 objs:=$(obj)/mainboard/$(MAINBOARDDIR)/static.o
147 initobjs:=
148 drivers:=
149 smmobjs:=
150 crt0s:=
151 ldscripts:=
152 types:=obj initobj driver smmobj
153
154 # Clean -y variables, include Makefile.inc
155 # If $(3) is non-empty, add paths to files in X-y, and add them to Xs
156 # Add subdirs-y to subdirs
157 includemakefiles= \
158         $(foreach type,$(2), $(eval $(type)-y:=)) \
159         $(eval subdirs-y:=) \
160         $(eval -include $(1)) \
161         $(if $(strip $(3)), \
162                 $(foreach type,$(2), \
163                         $(eval $(type)s+= \
164                                 $$(patsubst util/%, \
165                                         $(obj)/util/%, \
166                                         $$(patsubst src/%, \
167                                                 $(obj)/%, \
168                                                 $$(addprefix $(dir $(1)),$$($(type)-y))))))) \
169         $(eval subdirs+=$$(subst $(PWD)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
170
171 # For each path in $(subdirs) call includemakefiles, passing $(1) as $(3)
172 # Repeat until subdirs is empty
173 evaluate_subdirs= \
174         $(eval cursubdirs:=$(subdirs)) \
175         $(eval subdirs:=) \
176         $(foreach dir,$(cursubdirs), \
177                 $(eval $(call includemakefiles,$(dir)/Makefile.inc,$(types),$(1)))) \
178         $(if $(subdirs),$(eval $(call evaluate_subdirs, $(1))))
179
180 # collect all object files eligible for building
181 subdirs:=$(PLATFORM-y) $(BUILD-y)
182 $(eval $(call evaluate_subdirs, modify))
183
184 initobjs:=$(addsuffix .initobj.o, $(basename $(initobjs)))
185 drivers:=$(addsuffix .driver.o, $(basename $(drivers)))
186 smmobjs:=$(addsuffix .smmobj.o, $(basename $(smmobjs)))
187
188 allobjs:=$(foreach var, $(addsuffix s,$(types)), $($(var)))
189 alldirs:=$(sort $(abspath $(dir $(allobjs))))
190 source_with_ext=$(patsubst $(obj)/%.o,src/%.$(1),$(allobjs))
191 allsrc=$(wildcard $(call source_with_ext,c) $(call source_with_ext,S))
192
193 POST_EVALUATION:=y
194
195 # fetch rules (protected in POST_EVALUATION) that rely on the variables filled above
196 subdirs:=$(PLATFORM-y) $(BUILD-y)
197 $(eval $(call evaluate_subdirs))
198
199
200 define objs_asl_template
201 $(obj)/$(1)%.o: src/$(1)%.asl
202         @printf "    IASL       $$(subst $(top)/,,$$(@))\n"
203         $(CPP) -D__ACPI__ -P $(CPPFLAGS) -include $(obj)/config.h -I$(src) -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $$(basename $$@).asl
204         iasl -p $$(basename $$@) -tc $$(basename $$@).asl
205         mv $$(basename $$@).hex $$(basename $$@).c
206         $(CC) $$(CFLAGS) $$(if $$(subst dsdt,,$$(basename $$(notdir $$@))), -DAmlCode=AmlCode_$$(basename $$(notdir $$@))) -c -o $$@ $$(basename $$@).c
207 endef
208
209 define objs_c_template
210 $(obj)/$(1)%.o: $(1)%.c $(obj)/config.h
211         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
212         $(CC) $$(CFLAGS) -c -o $$@ $$<
213
214 $(obj)/$(1)%.o: src/$(1)%.c $(obj)/config.h
215         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
216         $(CC) $$(CFLAGS) -c -o $$@ $$<
217 endef
218
219 define objs_S_template
220 $(obj)/$(1)%.o: $(1)%.S $(obj)/config.h
221         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
222         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
223
224 $(obj)/$(1)%.o: src/$(1)%.S $(obj)/config.h
225         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
226         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
227 endef
228
229 define initobjs_c_template
230 $(obj)/$(1)%.initobj.o: src/$(1)%.c $(obj)/config.h
231         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
232         $(CC) $$(CFLAGS) -c -o $$@ $$<
233 endef
234
235 define initobjs_S_template
236 $(obj)/$(1)%.initobj.o: src/$(1)%.S $(obj)/config.h
237         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
238         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
239 endef
240
241 define drivers_c_template
242 $(obj)/$(1)%.driver.o: src/$(1)%.c $(obj)/config.h
243         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
244         $(CC) $$(CFLAGS) -c -o $$@ $$<
245 endef
246
247 define drivers_S_template
248 $(obj)/$(1)%.driver.o: src/$(1)%.S
249         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
250         $(CC) -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
251 endef
252
253 define smmobjs_c_template
254 $(obj)/$(1)%.smmobj.o: src/$(1)%.c
255         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
256         $(CC) $$(CFLAGS) -c -o $$@ $$<
257 endef
258
259 define smmobjs_S_template
260 $(obj)/$(1)%.smmobj.o: src/$(1)%.S
261         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
262         $(CC) $$(CFLAGS) -c -o $$@ $$<
263 endef
264
265 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
266 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
267 $(eval $(call usetemplate,objs,asl))
268 $(eval $(call usetemplate,objs,c))
269 $(eval $(call usetemplate,objs,S))
270 $(eval $(call usetemplate,initobjs,c))
271 $(eval $(call usetemplate,initobjs,S))
272 $(eval $(call usetemplate,drivers,c))
273 $(eval $(call usetemplate,drivers,S))
274 $(eval $(call usetemplate,smmobjs,c))
275 $(eval $(call usetemplate,smmobjs,S))
276
277 printall:
278         @echo objs:=$(objs)
279         @echo initobjs:=$(initobjs)
280         @echo drivers:=$(drivers)
281         @echo smmobjs:=$(smmobjs)
282         @echo alldirs:=$(alldirs)
283         @echo allsrc=$(allsrc)
284
285 printcrt0s:
286         @echo $(patsubst $(top)/%,%,$(crt0s))
287
288 OBJS     := $(patsubst %,$(obj)/%,$(TARGETS-y))
289 INCLUDES := -I$(top)/src -I$(top)/src/include -I$(obj) -I$(top)/src/arch/$(ARCHDIR-y)/include 
290 INCLUDES += -I$(shell $(CC) -print-search-dirs | head -n 1 | cut -d' ' -f2)include
291 INCLUDES += -I$(top)/util/x86emu/include
292 INCLUDES += -include $(obj)/config.h
293
294 CFLAGS = $(INCLUDES) -Os -nostdinc
295 CFLAGS += -nostdlib -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
296 CFLAGS += -Wwrite-strings -Wredundant-decls -Wno-trigraphs 
297 CFLAGS += -Wstrict-aliasing -Wshadow 
298 ifeq ($(CONFIG_WARNINGS_ARE_ERRORS),y)
299 CFLAGS += -Werror
300 endif
301 CFLAGS += -fno-common -ffreestanding -fno-builtin -fomit-frame-pointer
302
303 CBFS_COMPRESS_FLAG:=l
304 CBFS_PAYLOAD_COMPRESS_FLAG:=
305 ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZMA),y)
306 CBFS_PAYLOAD_COMPRESS_FLAG:=l
307 endif
308
309 coreboot: prepare prepare2 $(obj)/coreboot.rom
310
311 endif
312
313 prepare:
314         mkdir -p $(obj)
315         mkdir -p $(obj)/util/kconfig/lxdialog $(obj)/util/cbfstool
316         test -n "$(alldirs)" && mkdir -p $(alldirs) || true
317
318 prepare2: $(obj)/build.h
319
320 $(obj)/build.h:
321         @printf "    GEN        build.h\n"
322         rm -f $(obj)/build.h
323         printf "/* build system definitions (autogenerated) */\n" > $(obj)/build.ht
324         printf "#ifndef __BUILD_H\n" >> $(obj)/build.ht
325         printf "#define __BUILD_H\n\n" >> $(obj)/build.ht
326         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
327         printf "#define COREBOOT_EXTRA_VERSION \"$(COREBOOT_EXTRA_VERSION)\"\n" >> $(obj)/build.ht
328         printf "#define COREBOOT_BUILD \"`LANG= date`\"\n" >> $(obj)/build.ht
329         printf "\n" >> $(obj)/build.ht
330         printf "#define COREBOOT_COMPILER \"$(shell LANG= $(CC) --version | head -n1)\"\n" >> $(obj)/build.ht
331         printf "#define COREBOOT_ASSEMBLER \"$(shell LANG= $(AS) --version | head -n1)\"\n" >> $(obj)/build.ht
332         printf "#define COREBOOT_LINKER \"$(shell LANG= $(LD) --version | head -n1)\"\n" >> $(obj)/build.ht
333         printf "#define COREBOOT_COMPILE_TIME \"`LANG= date +%T`\"\n" >> $(obj)/build.ht
334         printf "#define COREBOOT_COMPILE_BY \"$(subst \,@,$(shell PATH=$$PATH:/usr/ucb whoami))\"\n" >> $(obj)/build.ht
335         printf "#define COREBOOT_COMPILE_HOST \"$(shell hostname -s)\"\n" >> $(obj)/build.ht
336         printf "#define COREBOOT_COMPILE_DOMAIN \"$(shell test `uname -s` = "Linux" && dnsdomainname || domainname)\"\n" >> $(obj)/build.ht
337         printf "#endif\n" >> $(obj)/build.ht
338         mv $(obj)/build.ht $(obj)/build.h
339
340 doxy: doxygen
341 doxygen:
342         $(DOXYGEN) documentation/Doxyfile.coreboot
343
344 doxyclean: doxygen-clean
345 doxygen-clean:
346         rm -rf $(DOXYGEN_OUTPUT_DIR)
347
348 clean-for-update: doxygen-clean
349         rm -f $(objs) $(initobjs) $(drivers) $(smmobjs) .xcompile
350         rm -f $(obj)/coreboot_ram* $(obj)/coreboot.romstage $(obj)/coreboot.pre* $(obj)/coreboot.bootblock $(obj)/coreboot.a
351         rm -rf $(obj)/bootblock* $(obj)/romstage* $(obj)/location.*
352         rm -f $(obj)/option_table.* $(obj)/crt0_includes.h $(obj)/ldscript
353         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/static.c $(obj)/mainboard/$(MAINBOARDDIR)/config.py $(obj)/mainboard/$(MAINBOARDDIR)/static.dot
354         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/auto.inc $(obj)/mainboard/$(MAINBOARDDIR)/crt0.s $(obj)/mainboard/$(MAINBOARDDIR)/crt0.disasm
355         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
356         $(MAKE) -C util/sconfig clean
357
358 clean: clean-for-update
359         rm -f $(obj)/coreboot* .ccwrap
360
361 distclean: clean
362         rm -rf $(obj)
363         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig*
364
365 update:
366         dongle.py -c /dev/term/1 $(obj)/coreboot.rom EOF
367
368 # This include must come _before_ the pattern rules below!
369 # Order _does_ matter for pattern rules.
370 include util/kconfig/Makefile
371
372 $(obj)/ldoptions: $(obj)/config.h
373         awk '/^#define ([^"])* ([^"])*$$/ {gsub("\\r","",$$3); print $$2 " = " $$3 ";";}' $< > $@
374
375 _OS=$(shell uname -s |cut -c-7)
376 STACK=
377 ifeq ($(_OS),MINGW32)
378         STACK=-Wl,--stack,16384000
379 endif
380 ifeq ($(_OS),CYGWIN_)
381         STACK=-Wl,--stack,16384000
382 endif
383 $(obj)/romcc: $(top)/util/romcc/romcc.c
384         @printf "    HOSTCC     $(subst $(obj)/,,$(@)) (this may take a while)\n"
385         @# Note: Adding -O2 here might cause problems. For details see:
386         @# http://www.coreboot.org/pipermail/coreboot/2010-February/055825.html
387         $(HOSTCC) -g $(STACK) -Wall -o $@ $<
388
389 .PHONY: $(PHONY) prepare prepare2 clean distclean doxygen doxy coreboot
390