make only needs to read Makefile.incs once, thanks to the
[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 := src
37 export srck := $(top)/util/kconfig
38 export obj ?= 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 $(obj)/build.h coreboot
140 endif
141
142 # must come rather early
143 .SECONDEXPANSION:
144
145 $(obj)/config.h:
146         $(MAKE) oldconfig
147
148 #######################################################################
149 # Build the tools
150
151 CBFSTOOL:=$(obj)/util/cbfstool/cbfstool
152
153 $(obj)/mainboard/$(MAINBOARDDIR)/config.py: $(yapps2_py) $(config_g) 
154         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
155         python $(yapps2_py) $(config_g) $(obj)/mainboard/$(MAINBOARDDIR)/config.py
156
157
158 # needed objects that every mainboard uses 
159 # Creation of these is architecture and mainboard independent
160 $(obj)/mainboard/$(MAINBOARDDIR)/static.c: $(src)/mainboard/$(MAINBOARDDIR)/devicetree.cb  $(obj)/mainboard/$(MAINBOARDDIR)/config.py
161         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
162         (PYTHONPATH=$(top)/util/sconfig python $(obj)/mainboard/$(MAINBOARDDIR)/config.py  $(MAINBOARDDIR) $(top) $(obj)/mainboard/$(MAINBOARDDIR))
163
164 objs:=$(obj)/mainboard/$(MAINBOARDDIR)/static.o
165 initobjs:=
166 drivers:=
167 smmobjs:=
168 crt0s:=
169 ldscripts:=
170 types:=obj initobj driver smmobj
171
172 # Clean -y variables, include Makefile.inc
173 # If $(3) is non-empty, add paths to files in X-y, and add them to Xs
174 # Add subdirs-y to subdirs
175 includemakefiles= \
176         $(foreach type,$(2), $(eval $(type)-y:=)) \
177         $(eval subdirs-y:=) \
178         $(eval -include $(1)) \
179         $(foreach type,$(2), \
180                 $(eval $(type)s+= \
181                         $$(subst $(top)/,, \
182                         $$(abspath $$(patsubst src/%, \
183                                         $(obj)/%, \
184                                         $$(addprefix $(dir $(1)),$$($(type)-y))))))) \
185         $(eval subdirs+=$$(subst $(PWD)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
186
187 # For each path in $(subdirs) call includemakefiles, passing $(1) as $(3)
188 # Repeat until subdirs is empty
189 evaluate_subdirs= \
190         $(eval cursubdirs:=$(subdirs)) \
191         $(eval subdirs:=) \
192         $(foreach dir,$(cursubdirs), \
193                 $(eval $(call includemakefiles,$(dir)/Makefile.inc,$(types)))) \
194         $(if $(subdirs),$(eval $(call evaluate_subdirs)))
195
196 # collect all object files eligible for building
197 subdirs:=$(PLATFORM-y) $(BUILD-y)
198 $(eval $(call evaluate_subdirs))
199
200 initobjs:=$(addsuffix .initobj.o, $(basename $(initobjs)))
201 drivers:=$(addsuffix .driver.o, $(basename $(drivers)))
202 smmobjs:=$(addsuffix .smmobj.o, $(basename $(smmobjs)))
203
204 allobjs:=$(foreach var, $(addsuffix s,$(types)), $($(var)))
205 alldirs:=$(sort $(abspath $(dir $(allobjs))))
206 source_with_ext=$(patsubst $(obj)/%.o,src/%.$(1),$(allobjs))
207 allsrc=$(wildcard $(call source_with_ext,c) $(call source_with_ext,S))
208
209 define objs_asl_template
210 $(obj)/$(1)%.o: src/$(1)%.asl
211         @printf "    IASL       $$(subst $(top)/,,$$(@))\n"
212         $(CPP) -D__ACPI__ -P -include $(abspath $(obj)/config.h) -I$(src) -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $$(basename $$@).asl
213         iasl -p $$(basename $$@) -tc $$(basename $$@).asl
214         mv $$(basename $$@).hex $$(basename $$@).c
215         $(CC) $$(CFLAGS) $$(if $$(subst dsdt,,$$(basename $$(notdir $$@))), -DAmlCode=AmlCode_$$(basename $$(notdir $$@))) -c -o $$@ $$(basename $$@).c
216 endef
217
218 # macro to define template macros that are used by use_template macro
219 define create_cc_template
220 # $1 obj class (objs, initobjs, ...)
221 # $2 source suffix (c, S)
222 # $3 .o infix ("" ".initobj", ...)
223 # $4 additional compiler flags
224 de$(EMPTY)fine $(1)_$(2)_template
225 $(obj)/$$(1)%$(3).o: src/$$(1)%.$(2) | $(obj)/build.h $(obj)/config.h
226         printf "    CC         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
227         $(CC) $(4) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
228 en$(EMPTY)def
229 endef
230
231 $(eval $(call create_cc_template,objs,c))
232 $(eval $(call create_cc_template,objs,S,,-DASSEMBLY))
233 $(eval $(call create_cc_template,initobjs,c,.initobj))
234 $(eval $(call create_cc_template,initobjs,S,.initobj,-DASSEMBLY))
235 $(eval $(call create_cc_template,drivers,c,.driver))
236 $(eval $(call create_cc_template,drivers,S,.driver,-DASSEMBLY))
237 $(eval $(call create_cc_template,smmobjs,c,.smmobj))
238 $(eval $(call create_cc_template,smmobjs,S,.smmobj))
239
240 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
241 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
242 $(eval $(call usetemplate,objs,asl))
243 $(eval $(call usetemplate,objs,c))
244 $(eval $(call usetemplate,objs,S))
245 $(eval $(call usetemplate,initobjs,c))
246 $(eval $(call usetemplate,initobjs,S))
247 $(eval $(call usetemplate,drivers,c))
248 $(eval $(call usetemplate,drivers,S))
249 $(eval $(call usetemplate,smmobjs,c))
250 $(eval $(call usetemplate,smmobjs,S))
251
252 DEPENDENCIES = $(objs:.o=.d) $(initobjs:.o=.d) $(drivers:.o=.d) $(smmobjs:.o=.d)
253 -include $(DEPENDENCIES)
254
255 printall:
256         @echo objs:=$(objs)
257         @echo initobjs:=$(initobjs)
258         @echo drivers:=$(drivers)
259         @echo smmobjs:=$(smmobjs)
260         @echo alldirs:=$(alldirs)
261         @echo allsrc=$(allsrc)
262         @echo DEPENDENCIES=$(DEPENDENCIES)
263         @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
264
265 printcrt0s:
266         @echo $(patsubst $(top)/%,%,$(crt0s))
267
268 OBJS     := $(patsubst %,$(obj)/%,$(TARGETS-y))
269 INCLUDES := -Isrc -Isrc/include -I$(obj) -Isrc/arch/$(ARCHDIR-y)/include 
270 INCLUDES += -Isrc/devices/oprom/include
271 # abspath is a workaround for romcc
272 INCLUDES += -include $(abspath $(obj)/config.h) -include $(abspath $(obj)/build.h)
273
274 CFLAGS = $(INCLUDES) -Os -nostdinc -pipe
275 CFLAGS += -nostdlib -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
276 CFLAGS += -Wwrite-strings -Wredundant-decls -Wno-trigraphs 
277 CFLAGS += -Wstrict-aliasing -Wshadow
278 ifeq ($(CONFIG_WARNINGS_ARE_ERRORS),y)
279 CFLAGS += -Werror
280 endif
281 CFLAGS += -fno-common -ffreestanding -fno-builtin -fomit-frame-pointer
282
283 CBFS_COMPRESS_FLAG:=l
284 CBFS_PAYLOAD_COMPRESS_FLAG:=
285 ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZMA),y)
286 CBFS_PAYLOAD_COMPRESS_FLAG:=l
287 endif
288
289 coreboot: prepare $(obj)/coreboot.rom
290
291 endif
292
293 prepare:
294         mkdir -p $(obj)
295         mkdir -p $(obj)/util/kconfig/lxdialog $(obj)/util/cbfstool
296         test -n "$(alldirs)" && mkdir -p $(alldirs) || true
297
298 $(obj)/build.h $(abspath $(obj)/build.h): .xcompile
299         @printf "    GEN        build.h\n"
300         rm -f $(obj)/build.h
301         printf "/* build system definitions (autogenerated) */\n" > $(obj)/build.ht
302         printf "#ifndef __BUILD_H\n" >> $(obj)/build.ht
303         printf "#define __BUILD_H\n\n" >> $(obj)/build.ht
304         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
305         printf "#define COREBOOT_EXTRA_VERSION \"$(COREBOOT_EXTRA_VERSION)\"\n" >> $(obj)/build.ht
306         printf "#define COREBOOT_BUILD \"`LANG= date`\"\n" >> $(obj)/build.ht
307         printf "\n" >> $(obj)/build.ht
308         printf "#define COREBOOT_COMPILER \"$(shell LANG= $(CC) --version | head -n1)\"\n" >> $(obj)/build.ht
309         printf "#define COREBOOT_ASSEMBLER \"$(shell LANG= $(AS) --version | head -n1)\"\n" >> $(obj)/build.ht
310         printf "#define COREBOOT_LINKER \"$(shell LANG= $(LD) --version | head -n1)\"\n" >> $(obj)/build.ht
311         printf "#define COREBOOT_COMPILE_TIME \"`LANG= date +%T`\"\n" >> $(obj)/build.ht
312         printf "#define COREBOOT_COMPILE_BY \"$(subst \,@,$(shell PATH=$$PATH:/usr/ucb whoami))\"\n" >> $(obj)/build.ht
313         printf "#define COREBOOT_COMPILE_HOST \"$(shell hostname -s 2>/dev/null)\"\n" >> $(obj)/build.ht
314         printf "#define COREBOOT_COMPILE_DOMAIN \"$(shell test `uname -s` = "Linux" && dnsdomainname || domainname 2>/dev/null)\"\n" >> $(obj)/build.ht
315         printf "#endif\n" >> $(obj)/build.ht
316         mv $(obj)/build.ht $(obj)/build.h
317
318 doxy: doxygen
319 doxygen:
320         $(DOXYGEN) documentation/Doxyfile.coreboot
321
322 doxyclean: doxygen-clean
323 doxygen-clean:
324         rm -rf $(DOXYGEN_OUTPUT_DIR)
325
326 clean-for-update: doxygen-clean
327         rm -f $(objs) $(initobjs) $(drivers) $(smmobjs) .xcompile
328         rm -f $(DEPENDENCIES)
329         rm -f $(obj)/coreboot_ram* $(obj)/coreboot.romstage $(obj)/coreboot.pre* $(obj)/coreboot.bootblock $(obj)/coreboot.a
330         rm -rf $(obj)/bootblock* $(obj)/romstage* $(obj)/location.*
331         rm -f $(obj)/option_table.* $(obj)/crt0_includes.h $(obj)/ldscript
332         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/static.c $(obj)/mainboard/$(MAINBOARDDIR)/config.py $(obj)/mainboard/$(MAINBOARDDIR)/static.dot
333         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/crt0.s $(obj)/mainboard/$(MAINBOARDDIR)/crt0.disasm
334         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/romstage.inc
335         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/bootblock.* $(obj)/mainboard/$(MAINBOARDDIR)/dsdt.*
336         rm -f $(obj)/cpu/x86/smm/smm_bin.c $(obj)/cpu/x86/smm/smm.* $(obj)/cpu/x86/smm/smm
337         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
338         $(MAKE) -C util/sconfig clean
339
340 clean: clean-for-update
341         rm -f $(obj)/coreboot* .ccwrap
342
343 distclean: clean
344         rm -rf $(obj)
345         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig*
346
347 update:
348         dongle.py -c /dev/term/1 $(obj)/coreboot.rom EOF
349
350 # This include must come _before_ the pattern rules below!
351 # Order _does_ matter for pattern rules.
352 include util/kconfig/Makefile
353
354 $(obj)/ldoptions: $(obj)/config.h
355         awk '/^#define ([^"])* ([^"])*$$/ {gsub("\\r","",$$3); print $$2 " = " $$3 ";";}' $< > $@
356
357 _OS=$(shell uname -s |cut -c-7)
358 STACK=
359 ifeq ($(_OS),MINGW32)
360         STACK=-Wl,--stack,16384000
361 endif
362 ifeq ($(_OS),CYGWIN_)
363         STACK=-Wl,--stack,16384000
364 endif
365 $(obj)/romcc: $(top)/util/romcc/romcc.c
366         @printf "    HOSTCC     $(subst $(obj)/,,$(@)) (this may take a while)\n"
367         @# Note: Adding -O2 here might cause problems. For details see:
368         @# http://www.coreboot.org/pipermail/coreboot/2010-February/055825.html
369         $(HOSTCC) -g $(STACK) -Wall -o $@ $<
370
371 .PHONY: $(PHONY) prepare clean distclean doxygen doxy coreboot .xcompile
372