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