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