This patch fixes an issue with the wrong build rules being selected.
[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 $(if $(wildcard .xcompile),,$(eval $(shell bash util/xcompile/xcompile > .xcompile)))
23 include .xcompile
24
25 export top := $(PWD)
26 export src := $(top)/src
27 export srck := $(top)/util/kconfig
28 export obj ?= $(top)/build
29 export objk := $(obj)/util/kconfig
30 export sconfig := $(top)/util/sconfig
31 export yapps2_py := $(sconfig)/yapps2.py
32 export config_g := $(sconfig)/config.g
33
34
35 export KERNELVERSION      := 4.0
36 export KCONFIG_AUTOHEADER := $(obj)/config.h
37 export KCONFIG_AUTOCONFIG := $(obj)/auto.conf
38
39 CONFIG_SHELL := sh
40 KBUILD_DEFCONFIG := configs/defconfig
41 UNAME_RELEASE := $(shell uname -r)
42 HAVE_DOTCONFIG := $(wildcard .config)
43 MAKEFLAGS += -rR --no-print-directory
44
45 # Make is silent per default, but 'make V=1' will show all compiler calls.
46 Q:=@
47 ifneq ($(V),1)
48 ifneq ($(Q),)
49 .SILENT:
50 endif
51 endif
52
53 CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
54 HOSTCC = gcc
55 HOSTCXX = g++
56 HOSTCFLAGS := -I$(srck) -I$(objk) -g
57 HOSTCXXFLAGS := -I$(srck) -I$(objk)
58 LIBGCC_FILE_NAME := $(shell $(CC) -print-libgcc-file-name)
59
60 DESTDIR = /opt
61
62 DOXYGEN := doxygen
63 DOXYGEN_OUTPUT_DIR := doxygen
64
65 ifeq ($(strip $(HAVE_DOTCONFIG)),)
66
67 all: config
68
69 else
70
71 include $(top)/.config
72
73 ARCHDIR-$(CONFIG_ARCH_X86)    := i386
74 ARCHDIR-$(CONFIG_ARCH_POWERPC) := ppc
75
76 MAINBOARDDIR=$(subst ",,$(CONFIG_MAINBOARD_DIR))
77 export MAINBOARDDIR
78
79 PLATFORM-y += src/arch/$(ARCHDIR-y) src/cpu src/mainboard/$(MAINBOARDDIR)
80 TARGETS-y :=
81
82 BUILD-y := src/lib src/boot src/console src/devices src/southbridge src/northbridge src/superio src/drivers util/x86emu
83 BUILD-y += util/cbfstool
84 BUILD-$(CONFIG_ARCH_X86) += src/pc80
85
86 ifneq ($(CONFIG_LOCALVERSION),"")
87 COREBOOT_EXTRA_VERSION := -$(subst ",,$(CONFIG_LOCALVERSION))
88 endif
89
90 # The primary target needs to be here before we include the
91 # other files
92
93 all: coreboot
94
95
96 #######################################################################
97 # Build the tools
98
99 CBFSTOOL:=$(obj)/util/cbfstool/cbfstool
100
101 $(obj)/mainboard/$(MAINBOARDDIR)/config.py: $(yapps2_py) $(config_g) 
102         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
103         python $(yapps2_py) $(config_g) $(obj)/mainboard/$(MAINBOARDDIR)/config.py
104
105
106 # needed objects that every mainboard uses 
107 # Creation of these is architecture and mainboard independent
108 $(obj)/mainboard/$(MAINBOARDDIR)/static.c: $(src)/mainboard/$(MAINBOARDDIR)/devicetree.cb  $(obj)/mainboard/$(MAINBOARDDIR)/config.py
109         mkdir -p $(obj)/mainboard/$(MAINBOARDDIR)
110         (cd $(obj)/mainboard/$(MAINBOARDDIR) ; PYTHONPATH=$(top)/util/sconfig export PYTHONPATH; python config.py  $(MAINBOARDDIR) $(top) $(obj)/mainboard/$(MAINBOARDDIR))
111
112 $(obj)/mainboard/$(MAINBOARDDIR)/static.o: $(obj)/mainboard/$(MAINBOARDDIR)/static.c
113 #
114
115 objs:=$(obj)/mainboard/$(MAINBOARDDIR)/static.o
116 initobjs:=
117 drivers:=
118 smmobjs:=
119 crt0s:=
120 ldscripts:=
121 types:=obj initobj driver smmobj
122 includemakefiles=$(foreach type,$(2), $(eval $(type)-y:=)) $(eval subdirs-y:=) $(eval include $(1)) $(if $(strip $(3)),$(foreach type,$(2),$(eval $(type)s+=$$(patsubst src/%,$(obj)/%,$$(addprefix $(dir $(1)),$$($(type)-y)))))) $(eval subdirs+=$$(subst $(PWD)/,,$$(abspath $$(addprefix $(dir $(1)),$$(subdirs-y)))))
123 evaluate_subdirs=$(eval cursubdirs:=$(subdirs)) $(eval subdirs:=) $(foreach dir,$(cursubdirs),$(eval $(call includemakefiles,$(dir)/Makefile.inc,$(types),$(1)))) $(if $(subdirs),$(eval $(call evaluate_subdirs, $(1))))
124
125 # collect all object files eligible for building
126 subdirs:=$(PLATFORM-y) $(BUILD-y)
127 $(eval $(call evaluate_subdirs, modify))
128
129 initobjs:=$(addsuffix .initobj.o, $(basename $(initobjs)))
130 drivers:=$(addsuffix .driver.o, $(basename $(drivers)))
131 smmobjs:=$(addsuffix .smmobj.o, $(basename $(smmobjs)))
132
133 allobjs:=$(foreach var, $(addsuffix s,$(types)), $($(var)))
134 alldirs:=$(sort $(abspath $(dir $(allobjs))))
135 source_with_ext=$(patsubst $(obj)/%.o,src/%.$(1),$(allobjs))
136 allsrc=$(wildcard $(call source_with_ext,c) $(call source_with_ext,S))
137
138 POST_EVALUATION:=y
139
140 # fetch rules (protected in POST_EVALUATION) that rely on the variables filled above
141 subdirs:=$(PLATFORM-y) $(BUILD-y)
142 $(eval $(call evaluate_subdirs))
143
144
145 define objs_dsl_template
146 $(obj)/$(1)%.o: src/$(1)%.asl
147         @printf "    IASL       $$(subst $(top)/,,$$(@))\n"
148         $(CPP) -D__ACPI__ -P $(CPPFLAGS) -include $(obj)/config.h -I$(src) -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $$(basename $$@).asl
149         iasl -p $$(basename $$@) -tc $$(basename $$@).asl
150         mv $$(basename $$@).hex $$(basename $$@).c
151         $(CC) -m32 $$(CFLAGS) $$(if $$(subst dsdt,,$$(basename $$(notdir $$@))), -DAmlCode=AmlCode_$$(basename $$(notdir $$@))) -c -o $$@ $$(basename $$@).c
152 endef
153
154 define objs_c_template
155 $(obj)/$(1)%.o: src/$(1)%.c $(obj)/config.h
156         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
157         $(CC) -m32 $$(CFLAGS) -c -o $$@ $$<
158 endef
159
160 define objs_S_template
161 $(obj)/$(1)%.o: src/$(1)%.S $(obj)/config.h
162         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
163         $(CC) -m32 -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
164 endef
165
166 define initobjs_c_template
167 $(obj)/$(1)%.initobj.o: src/$(1)%.c $(obj)/config.h
168         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
169         $(CC) -m32 $$(CFLAGS) -c -o $$@ $$<
170 endef
171
172 define initobjs_S_template
173 $(obj)/$(1)%.initobj.o: src/$(1)%.S $(obj)/config.h
174         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
175         $(CC) -m32 -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
176 endef
177
178 define drivers_c_template
179 $(obj)/$(1)%.driver.o: src/$(1)%.c $(obj)/config.h
180         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
181         $(CC) -m32 $$(CFLAGS) -c -o $$@ $$<
182 endef
183
184 define drivers_S_template
185 $(obj)/$(1)%.driver.o: src/$(1)%.S
186         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
187         $(CC) -m32 -DASSEMBLY $$(CFLAGS) -c -o $$@ $$<
188 endef
189
190 define smmobjs_c_template
191 $(obj)/$(1)%.smmobj.o: src/$(1)%.c
192         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
193         $(CC) -m32 $$(CFLAGS) -c -o $$@ $$<
194 endef
195
196 define smmobjs_S_template
197 $(obj)/$(1)%.smmobj.o: src/$(1)%.S
198         @printf "    CC         $$(subst $$(obj)/,,$$(@))\n"
199         $(CC) -m32 $$(CFLAGS) -c -o $$@ $$<
200 endef
201
202 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
203 usetemplate=$(foreach d,$(sort $(dir $($(1)))),$(eval $(call $(1)_$(2)_template,$(subst $(obj)/,,$(d)))))
204 $(eval $(call usetemplate,objs,dsl))
205 $(eval $(call usetemplate,objs,c))
206 $(eval $(call usetemplate,objs,S))
207 $(eval $(call usetemplate,initobjs,c))
208 $(eval $(call usetemplate,initobjs,S))
209 $(eval $(call usetemplate,drivers,c))
210 $(eval $(call usetemplate,drivers,S))
211 $(eval $(call usetemplate,smmobjs,c))
212 $(eval $(call usetemplate,smmobjs,S))
213
214 printall:
215         @echo objs:=$(objs)
216         @echo initobjs:=$(initobjs)
217         @echo drivers:=$(drivers)
218         @echo smmobjs:=$(smmobjs)
219         @echo alldirs:=$(alldirs)
220         @echo allsrc=$(allsrc)
221
222 OBJS     := $(patsubst %,$(obj)/%,$(TARGETS-y))
223 INCLUDES := -I$(top)/src -I$(top)/src/include -I$(obj) -I$(top)/src/arch/$(ARCHDIR-y)/include 
224 INCLUDES += -I$(shell $(CC) -print-search-dirs | head -n 1 | cut -d' ' -f2)include
225 INCLUDES += -I$(top)/util/x86emu/include
226 INCLUDES += -include $(obj)/config.h
227
228 CFLAGS = $(INCLUDES) -Os -nostdinc
229 CFLAGS += -nostdlib -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
230 CFLAGS += -Wwrite-strings -Wredundant-decls -Wno-trigraphs 
231 CFLAGS += -Wstrict-aliasing -Wshadow 
232 ifeq ($(CONFIG_WARNINGS_ARE_ERRORS),y)
233 CFLAGS += -Werror
234 endif
235 CFLAGS += -fno-common -ffreestanding -fno-builtin -fomit-frame-pointer
236
237 CBFS_COMPRESS_FLAG:=l
238 CBFS_PAYLOAD_COMPRESS_FLAG:=
239 ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZMA),y)
240 CBFS_PAYLOAD_COMPRESS_FLAG:=l
241 endif
242
243 coreboot: prepare prepare2 $(obj)/coreboot.rom
244
245 endif
246
247 prepare:
248         mkdir -p $(obj)
249         mkdir -p $(obj)/util/kconfig/lxdialog $(obj)/util/cbfstool
250         test -n "$(alldirs)" && mkdir -p $(alldirs) || true
251
252 prepare2: $(obj)/build.h
253
254 $(obj)/build.h:
255         @printf "    GEN        build.h\n"
256         rm -f $(obj)/build.h
257         printf "/* build system definitions (autogenerated) */\n" > $(obj)/build.ht
258         printf "#ifndef __BUILD_H\n" >> $(obj)/build.ht
259         printf "#define __BUILD_H\n\n" >> $(obj)/build.ht
260         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
261         printf "#define COREBOOT_EXTRA_VERSION \"$(COREBOOT_EXTRA_VERSION)\"\n" >> $(obj)/build.ht
262         printf "#define COREBOOT_BUILD \"`LANG= date`\"\n" >> $(obj)/build.ht
263         printf "\n" >> $(obj)/build.ht
264         printf "#define COREBOOT_COMPILER \"$(shell LANG= $(CC) --version | head -n1)\"\n" >> $(obj)/build.ht
265         printf "#define COREBOOT_ASSEMBLER \"$(shell LANG= $(AS) --version | head -n1)\"\n" >> $(obj)/build.ht
266         printf "#define COREBOOT_LINKER \"$(shell LANG= $(LD) --version | head -n1)\"\n" >> $(obj)/build.ht
267         printf "#define COREBOOT_COMPILE_TIME \"`LANG= date +%T`\"\n" >> $(obj)/build.ht
268         printf "#define COREBOOT_COMPILE_BY \"$(subst \,@,$(shell PATH=$$PATH:/usr/ucb whoami))\"\n" >> $(obj)/build.ht
269         printf "#define COREBOOT_COMPILE_HOST \"$(shell hostname -s)\"\n" >> $(obj)/build.ht
270         printf "#define COREBOOT_COMPILE_DOMAIN \"$(shell test `uname -s` = "Linux" && dnsdomainname || domainname)\"\n" >> $(obj)/build.ht
271         printf "#endif\n" >> $(obj)/build.ht
272         mv $(obj)/build.ht $(obj)/build.h
273
274 doxy: doxygen
275 doxygen:
276         $(DOXYGEN) documentation/Doxyfile.coreboot
277
278 doxyclean: doxygen-clean
279 doxygen-clean:
280         rm -rf $(DOXYGEN_OUTPUT_DIR)
281
282 clean-for-update: doxygen-clean
283         rm -f $(objs) $(initobjs) $(drivers) $(smmobjs) .xcompile
284         rm -f $(obj)/coreboot_ram* $(obj)/coreboot.romstage $(obj)/coreboot.pre* $(obj)/coreboot.bootblock $(obj)/coreboot.a
285         rm -rf $(obj)/bootblock* $(obj)/romstage* $(obj)/location.*
286         rm -f $(obj)/option_table.* $(obj)/crt0_includes.h $(obj)/ldscript
287         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/static.c $(obj)/mainboard/$(MAINBOARDDIR)/config.py $(obj)/mainboard/$(MAINBOARDDIR)/static.dot
288         rm -f $(obj)/mainboard/$(MAINBOARDDIR)/auto.inc $(obj)/mainboard/$(MAINBOARDDIR)/crt0.s $(obj)/mainboard/$(MAINBOARDDIR)/crt0.disasm
289         rmdir -p $(alldirs) 2>/dev/null >/dev/null || true
290         $(MAKE) -C util/sconfig clean
291
292 clean: clean-for-update
293         rm -f $(obj)/coreboot*
294
295 distclean: clean
296         rm -rf $(obj)
297         rm -f .config .config.old ..config.tmp .kconfig.d .tmpconfig*
298
299 update:
300         dongle.py -c /dev/term/1 $(obj)/coreboot.rom EOF
301
302 # This include must come _before_ the pattern rules below!
303 # Order _does_ matter for pattern rules.
304 include util/kconfig/Makefile
305
306 $(obj)/ldoptions: $(obj)/config.h
307         awk '/^#define ([^"])* ([^"])*$$/ {gsub("\\r","",$$3); print $$2 " = " $$3 ";";}' $< > $@
308
309 _OS=$(shell uname -s |cut -c-7)
310 STACK=
311 ifeq ($(_OS),MINGW32)
312         STACK=-Wl,--stack,16384000
313 endif
314 ifeq ($(_OS),CYGWIN_)
315         STACK=-Wl,--stack,16384000
316 endif
317 $(obj)/romcc: $(top)/util/romcc/romcc.c
318         @printf "    HOSTCC     $(subst $(obj)/,,$(@)) (this may take a while)\n"
319         @# Note: Adding -O2 here might cause problems. For details see:
320         @# http://www.coreboot.org/pipermail/coreboot/2010-February/055825.html
321         $(HOSTCC) -g $(STACK) -Wall -o $@ $<
322
323 .PHONY: $(PHONY) prepare prepare2 clean distclean doxygen doxy coreboot
324