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