Allow user to define location for Kconfig config via
[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 := $(CURDIR)
36 export src := src
37 export srck := $(top)/util/kconfig
38 export obj ?= build
39 export objutil ?= $(obj)/util
40 export objk := $(objutil)/kconfig
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 DOTCONFIG ?= .config
51 KCONFIG_CONFIG = $(DOTCONFIG)
52 export KCONFIG_CONFIG
53 HAVE_DOTCONFIG := $(wildcard $(DOTCONFIG))
54 MAKEFLAGS += -rR --no-print-directory
55
56 # Make is silent per default, but 'make V=1' will show all compiler calls.
57 Q:=@
58 ifneq ($(V),1)
59 ifneq ($(Q),)
60 .SILENT:
61 endif
62 endif
63
64 CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
65 ROMCC:= $(objutil)/romcc/romcc
66 HOSTCC = gcc
67 HOSTCXX = g++
68 HOSTCFLAGS := -I$(srck) -I$(objk) -g
69 HOSTCXXFLAGS := -I$(srck) -I$(objk)
70 LIBGCC_FILE_NAME := $(shell test -r `$(CC) -print-libgcc-file-name` && $(CC) -print-libgcc-file-name)
71
72 DOXYGEN := doxygen
73 DOXYGEN_OUTPUT_DIR := doxygen
74
75 # Three cases where we don't need fully populated $(obj) lists:
76 # 1. when no .config exists
77 # 2. when make config (in any flavour) is run
78 # 3. when make distclean is run
79 # Don't waste time on reading all Makefile.incs in these cases
80 ifeq ($(strip $(HAVE_DOTCONFIG)),)
81 NOCOMPILE:=1
82 endif
83 ifneq ($(MAKECMDGOALS),)
84 ifneq ($(filter %config distclean,$(MAKECMDGOALS)),)
85 NOCOMPILE:=1
86 endif
87 endif
88
89 ifeq ($(NOCOMPILE),1)
90 all: config
91
92 else
93
94 include $(HAVE_DOTCONFIG)
95
96 ifneq ($(INNER_SCANBUILD),y)
97 ifeq ($(CONFIG_COMPILER_LLVM_CLANG),y)
98 CC:=clang -m32
99 HOSTCC:=clang
100 endif
101 endif
102
103 ifeq ($(CONFIG_CCACHE),y)
104 CCACHE:=$(wildcard $(addsuffix /ccache,$(subst :, ,$(PATH))))
105 ifeq ($(CCACHE),)
106 $(error ccache selected, but not found in PATH)
107 endif
108 CCACHE:=CCACHE_COMPILERCHECK=content CCACHE_BASEDIR=$(top) $(CCACHE)
109 CC := $(CCACHE) $(CC)
110 HOSTCC := $(CCACHE) $(HOSTCC)
111 HOSTCXX := $(CCACHE) $(HOSTCXX)
112 ROMCC := $(CCACHE) $(ROMCC)
113 endif
114
115 strip_quotes = $(subst ",,$(subst \",,$(1)))
116
117 ARCHDIR-$(CONFIG_ARCH_X86)    := i386
118 ARCHDIR-$(CONFIG_ARCH_POWERPC) := ppc
119
120 MAINBOARDDIR=$(call strip_quotes,$(CONFIG_MAINBOARD_DIR))
121 export MAINBOARDDIR
122
123 PLATFORM-y += src/arch/$(ARCHDIR-y) src/cpu src/mainboard/$(MAINBOARDDIR)
124 TARGETS-y :=
125
126 BUILD-y := src/lib src/boot src/console src/devices src/southbridge src/northbridge src/superio src/drivers
127 BUILD-y += util/cbfstool util/sconfig
128 BUILD-$(CONFIG_ARCH_X86) += src/pc80
129
130 ifneq ($(CONFIG_LOCALVERSION),"")
131 COREBOOT_EXTRA_VERSION := -$(call strip_quotes,$(CONFIG_LOCALVERSION))
132 endif
133
134 # The primary target needs to be here before we include the
135 # other files
136
137 ifeq ($(INNER_SCANBUILD),y)
138 CONFIG_SCANBUILD_ENABLE:=
139 endif
140
141 ifeq ($(CONFIG_SCANBUILD_ENABLE),y)
142 ifneq ($(CONFIG_SCANBUILD_REPORT_LOCATION),)
143 CONFIG_SCANBUILD_REPORT_LOCATION:=-o $(CONFIG_SCANBUILD_REPORT_LOCATION)
144 endif
145 all:
146         echo '#!/bin/sh' > .ccwrap
147         echo 'CC="$(CC)"' >> .ccwrap
148         echo 'if [ "$$1" = "--hostcc" ]; then shift; CC="$(HOSTCC)"; fi' >> .ccwrap
149         echo 'if [ "$$1" = "--hostcxx" ]; then shift; CC="$(HOSTCXX)"; fi' >> .ccwrap
150         echo 'eval $$CC $$*' >> .ccwrap
151         chmod +x .ccwrap
152         scan-build $(CONFIG_SCANBUILD_REPORT_LOCATION) -analyze-headers --use-cc=$(top)/.ccwrap --use-c++=$(top)/.ccwrap $(MAKE) INNER_SCANBUILD=y
153 else
154 all: $(obj)/config.h coreboot
155 endif
156
157 # must come rather early
158 .SECONDEXPANSION:
159
160 $(obj)/config.h:
161         $(MAKE) oldconfig
162
163 #######################################################################
164 # Build the tools
165
166 CBFSTOOL:=$(objutil)/cbfstool/cbfstool
167
168 # needed objects that every mainboard uses
169 # Creation of these is architecture and mainboard independent
170 $(obj)/mainboard/$(MAINBOARDDIR)/static.c: $(src)/mainboard/$(MAINBOARDDIR)/devicetree.cb  $(objutil)/sconfig/sconfig
171         @printf "    SCONFIG    $(subst $(src)/,,$(<))\n"
172         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
173         $(objutil)/sconfig/sconfig $(MAINBOARDDIR) $(obj)/mainboard/$(MAINBOARDDIR)
174
175 $(objutil)/%.o: $(objutil)/%.c
176         @printf "    HOSTCC     $(subst $(objutil)/,,$(@))\n"
177         $(HOSTCC) -MMD -I$(subst $(objutil)/,util/,$(dir $<)) -I$(dir $<) $(HOSTCFLAGS) -c -o $@ $<
178
179 $(obj)/%.ramstage.o: $(obj)/%.c $(obj)/config.h
180         @printf "    CC         $(subst $(obj)/,,$(@))\n"
181         $(CC) -MMD $(CFLAGS) -c -o $@ $<
182
183 ramstage-srcs:=$(obj)/mainboard/$(MAINBOARDDIR)/static.c
184 romstage-srcs:=
185 driver-srcs:=
186 smm-srcs:=
187
188 ramstage-objs:=
189 romstage-objs:=
190 driver-objs:=
191 smm-objs:=
192 types:=ramstage romstage driver smm
193
194 # Clean -y variables, include Makefile.inc
195 # Add paths to files in X-y to X-srcs
196 # Add subdirs-y to subdirs
197 includemakefiles= \
198         $(foreach type,$(2), $(eval $(type)-y:=)) \
199         $(eval subdirs-y:=) \
200         $(eval -include $(1)) \
201         $(foreach type,$(2), \
202                 $(eval $(type)-srcs+= \
203                         $$(subst $(top)/,, \
204                         $$(abspath $$(addprefix $(dir $(1)),$$($(type)-y)))))) \
205         $(eval subdirs+=$$(subst $(CURDIR)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
206
207 # For each path in $(subdirs) call includemakefiles
208 # Repeat until subdirs is empty
209 evaluate_subdirs= \
210         $(eval cursubdirs:=$(subdirs)) \
211         $(eval subdirs:=) \
212         $(foreach dir,$(cursubdirs), \
213                 $(eval $(call includemakefiles,$(dir)/Makefile.inc,$(types)))) \
214         $(if $(subdirs),$(eval $(call evaluate_subdirs)))
215
216 # collect all object files eligible for building
217 subdirs:=$(PLATFORM-y) $(BUILD-y)
218 $(eval $(call evaluate_subdirs))
219
220 src-to-obj=$(addsuffix .$(1).o, $(basename $(patsubst src/%, $(obj)/%, $($(1)-srcs))))
221
222 ramstage-objs:=$(call src-to-obj,ramstage)
223 romstage-objs:=$(call src-to-obj,romstage)
224 driver-objs:=$(call src-to-obj,driver)
225 smm-objs:=$(call src-to-obj,smm)
226
227 allsrcs:=$(foreach var, $(addsuffix -srcs,$(types)), $($(var)))
228 allobjs:=$(foreach var, $(addsuffix -objs,$(types)), $($(var)))
229 alldirs:=$(sort $(abspath $(dir $(allobjs))))
230
231 define ramstage-objs_asl_template
232 $(obj)/$(1).ramstage.o: src/$(1).asl
233         @printf "    IASL       $$(subst $(top)/,,$$(@))\n"
234         $(CPP) -D__ACPI__ -P -include $(abspath $(obj)/config.h) -I$(src) -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $$(basename $$@).asl
235         iasl -p $$(obj)/$(1) -tc $$(basename $$@).asl
236         mv $$(obj)/$(1).hex $$(basename $$@).c
237         $(CC) $$(CFLAGS) $$(if $$(subst dsdt,,$$(basename $$(notdir $(1)))), -DAmlCode=AmlCode_$$(basename $$(notdir $(1)))) -c -o $$@ $$(basename $$@).c
238         # keep %.o: %.c rule from catching the temporary .c file after a make clean
239         mv $$(basename $$@).c $$(basename $$@).hex
240 endef
241
242 # macro to define template macros that are used by use_template macro
243 define create_cc_template
244 # $1 obj class (ramstage, romstage, driver, smm)
245 # $2 source suffix (c, S)
246 # $3 additional compiler flags
247 de$(EMPTY)fine $(1)-objs_$(2)_template
248 $(obj)/$$(1).$(1).o: src/$$(1).$(2) $(obj)/config.h
249         @printf "    CC         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
250         $(CC) $(3) -MMD $$$$(CFLAGS) -c -o $$$$@ $$$$<
251 en$(EMPTY)def
252 endef
253
254 $(eval $(call create_cc_template,ramstage,c))
255 $(eval $(call create_cc_template,ramstage,S,-DASSEMBLY))
256 $(eval $(call create_cc_template,romstage,c,-D__PRE_RAM__))
257 $(eval $(call create_cc_template,romstage,S,-DASSEMBLY -D__PRE_RAM__))
258 $(eval $(call create_cc_template,driver,c))
259 $(eval $(call create_cc_template,driver,S,-DASSEMBLY))
260 $(eval $(call create_cc_template,smm,c))
261 $(eval $(call create_cc_template,smm,S))
262
263 foreach-src=$(foreach file,$($(1)-srcs),$(eval $(call $(1)-objs_$(subst .,,$(suffix $(file)))_template,$(subst src/,,$(basename $(file))))))
264 $(eval $(foreach type,$(types),$(call foreach-src,$(type))))
265
266 DEPENDENCIES = $(ramstage-objs:.o=.d) $(romstage-objs:.o=.d) $(driver-objs:.o=.d) $(smm-objs:.o=.d)
267 -include $(DEPENDENCIES)
268
269 printall:
270         @echo ramstage-objs:=$(ramstage-objs)
271         @echo romstage-objs:=$(romstage-objs)
272         @echo driver-objs:=$(driver-objs)
273         @echo smm-objs:=$(smm-objs)
274         @echo alldirs:=$(alldirs)
275         @echo allsrcs=$(allsrcs)
276         @echo DEPENDENCIES=$(DEPENDENCIES)
277         @echo LIBGCC_FILE_NAME=$(LIBGCC_FILE_NAME)
278
279 printcrt0s:
280         @echo crt0s=$(crt0s)
281         @echo ldscripts=$(ldscripts)
282
283 OBJS     := $(patsubst %,$(obj)/%,$(TARGETS-y))
284 INCLUDES := -Isrc -Isrc/include -I$(obj) -Isrc/arch/$(ARCHDIR-y)/include
285 INCLUDES += -Isrc/devices/oprom/include
286 # abspath is a workaround for romcc
287 INCLUDES += -include $(abspath $(obj)/config.h)
288
289 CFLAGS = $(INCLUDES) -Os -nostdinc -pipe -g
290 CFLAGS += -nostdlib -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
291 CFLAGS += -Wwrite-strings -Wredundant-decls -Wno-trigraphs
292 CFLAGS += -Wstrict-aliasing -Wshadow
293 ifeq ($(CONFIG_WARNINGS_ARE_ERRORS),y)
294 CFLAGS += -Werror
295 endif
296 CFLAGS += -fno-common -ffreestanding -fno-builtin -fomit-frame-pointer
297
298 CBFS_COMPRESS_FLAG:=l
299 CBFS_PAYLOAD_COMPRESS_FLAG:=
300 CBFS_PAYLOAD_COMPRESS_NAME:=none
301 ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZMA),y)
302 CBFS_PAYLOAD_COMPRESS_FLAG:=l
303 CBFS_PAYLOAD_COMPRESS_NAME:=LZMA
304 endif
305
306 coreboot: $(obj)/coreboot.rom
307
308 endif
309
310 $(shell mkdir -p $(obj) $(objutil)/kconfig/lxdialog $(objutil)/cbfstool $(objutil)/romcc $(objutil)/options $(alldirs))
311
312 $(obj)/build.h: .xcompile
313         @printf "    GEN        build.h\n"
314         rm -f $(obj)/build.h
315         printf "/* build system definitions (autogenerated) */\n" > $(obj)/build.ht
316         printf "#ifndef __BUILD_H\n" >> $(obj)/build.ht
317         printf "#define __BUILD_H\n\n" >> $(obj)/build.ht
318         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
319         printf "#define COREBOOT_EXTRA_VERSION \"$(COREBOOT_EXTRA_VERSION)\"\n" >> $(obj)/build.ht
320         printf "#define COREBOOT_BUILD \"`LANG= date`\"\n" >> $(obj)/build.ht
321         printf "\n" >> $(obj)/build.ht
322         printf "#define COREBOOT_COMPILER \"$(shell LANG= $(CC) --version | head -n1)\"\n" >> $(obj)/build.ht
323         printf "#define COREBOOT_ASSEMBLER \"$(shell LANG= $(AS) --version | head -n1)\"\n" >> $(obj)/build.ht
324         printf "#define COREBOOT_LINKER \"$(shell LANG= $(LD) --version | head -n1)\"\n" >> $(obj)/build.ht
325         printf "#define COREBOOT_COMPILE_TIME \"`LANG= date +%T`\"\n" >> $(obj)/build.ht
326         printf "#define COREBOOT_COMPILE_BY \"$(subst \,@,$(shell PATH=$$PATH:/usr/ucb whoami))\"\n" >> $(obj)/build.ht
327         printf "#define COREBOOT_COMPILE_HOST \"$(shell hostname -s 2>/dev/null)\"\n" >> $(obj)/build.ht
328         printf "#define COREBOOT_COMPILE_DOMAIN \"$(shell test `uname -s` = "Linux" && dnsdomainname || domainname 2>/dev/null)\"\n" >> $(obj)/build.ht
329         printf "#endif\n" >> $(obj)/build.ht
330         mv $(obj)/build.ht $(obj)/build.h
331
332 cscope:
333         cscope -bR
334
335 doxy: doxygen
336 doxygen:
337         $(DOXYGEN) documentation/Doxyfile.coreboot
338
339 doxyclean: doxygen-clean
340 doxygen-clean:
341         rm -rf $(DOXYGEN_OUTPUT_DIR)
342
343 clean-for-update: doxygen-clean
344         rm -f $(ramstage-objs) $(romstage-objs) $(driver-objs) $(smm-objs) .xcompile
345         rm -f $(DEPENDENCIES)
346         rm -f $(obj)/coreboot_ram* $(obj)/coreboot.romstage $(obj)/coreboot.pre* $(obj)/coreboot.bootblock $(obj)/coreboot.a
347         rm -rf $(obj)/bootblock* $(obj)/romstage* $(obj)/location.*
348         rm -f $(obj)/option_table.* $(obj)/crt0.S $(obj)/ldscript
349         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/static.c $(obj)/mainboard/$(MAINBOARDDIR)/config.py $(obj)/mainboard/$(MAINBOARDDIR)/static.dot
350         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/crt0.s $(obj)/mainboard/$(MAINBOARDDIR)/crt0.disasm
351         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/romstage.inc
352         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/bootblock.* $(obj)/mainboard/$(MAINBOARDDIR)/dsdt.*
353         rm -f $(obj)/cpu/x86/smm/smm_bin.c $(obj)/cpu/x86/smm/smm.* $(obj)/cpu/x86/smm/smm
354         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
355
356 clean: clean-for-update
357         rm -f $(obj)/coreboot* .ccwrap
358
359 clean-abuild:
360         rm -rf coreboot-builds
361
362 clean-cscope:
363         rm -f cscope.out
364
365 distclean: clean-cscope
366         rm -rf $(obj)
367         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig* .ccwrap .xcompile
368
369 update:
370         dongle.py -c /dev/term/1 $(obj)/coreboot.rom EOF
371
372 lint:
373         FAILED=0; LINTLOG=`mktemp`; \
374         for script in util/lint/lint-*; do \
375                 echo; echo `basename $$script`; \
376                 grep "^# DESCR:" $$script | sed "s,.*DESCR: *,," ; \
377                 echo ========; \
378                 $$script > $$LINTLOG; \
379                 if [ `wc -l $$LINTLOG | cut -d' ' -f1` -eq 0 ]; then \
380                         printf "success\n\n"; \
381                 else \
382                         echo test failed: ; \
383                         cat $$LINTLOG; \
384                         rm -f $$LINTLOG; \
385                         FAILED=$$(( $$FAILED + 1 )); \
386                 fi; \
387                 echo ========; \
388         done; \
389         test $$FAILED -eq 0 || { echo "ERROR: $$FAILED test(s) failed." &&  exit 1; }; \
390         rm -f $$LINTLOG
391
392 # This include must come _before_ the pattern rules below!
393 # Order _does_ matter for pattern rules.
394 include util/kconfig/Makefile
395
396 $(obj)/ldoptions: $(obj)/config.h
397         awk '/^#define ([^"])* ([^"])*$$/ {gsub("\\r","",$$3); print $$2 " = " $$3 ";";}' $< > $@
398
399 _WINCHECK=$(shell uname -o 2> /dev/null)
400 STACK=
401 ifeq ($(_WINCHECK),Msys)
402         STACK=-Wl,--stack,16384000
403 endif
404 ifeq ($(_WINCHECK),Cygwin)
405         STACK=-Wl,--stack,16384000
406 endif
407
408 $(objutil)/romcc/romcc: $(top)/util/romcc/romcc.c
409         @printf "    HOSTCC     $(subst $(obj)/,,$(@)) (this may take a while)\n"
410         @# Note: Adding -O2 here might cause problems. For details see:
411         @# http://www.coreboot.org/pipermail/coreboot/2010-February/055825.html
412         $(HOSTCC) -g $(STACK) -Wall -o $@ $<
413
414 .PHONY: $(PHONY) clean clean-abuild clean-cscope cscope distclean doxygen doxy coreboot .xcompile
415