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