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