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