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