1# -*- makefile -*- : Force emacs to use Makefile mode
2#
3# This file contains various boring housekeeping functions that would
4# otherwise seriously clutter up the main Makefile.
5
6###############################################################################
7#
8# Find a usable "echo -e" substitute.
9#
10TAB 			:= $(shell $(PRINTF) '\t')
11ECHO_E_ECHO		:= $(ECHO)
12ECHO_E_ECHO_E		:= $(ECHO) -e
13ECHO_E_BIN_ECHO 	:= /bin/echo
14ECHO_E_BIN_ECHO_E 	:= /bin/echo -e
15ECHO_E_ECHO_TAB		:= $(shell $(ECHO_E_ECHO) '\t' | cat)
16ECHO_E_ECHO_E_TAB	:= $(shell $(ECHO_E_ECHO_E) '\t' | cat)
17ECHO_E_BIN_ECHO_TAB 	:= $(shell $(ECHO_E_BIN_ECHO) '\t')
18ECHO_E_BIN_ECHO_E_TAB 	:= $(shell $(ECHO_E_BIN_ECHO_E) '\t')
19
20ifeq ($(ECHO_E_ECHO_TAB),$(TAB))
21ECHO_E		:= $(ECHO_E_ECHO)
22endif
23ifeq ($(ECHO_E_ECHO_E_TAB),$(TAB))
24ECHO_E		:= $(ECHO_E_ECHO_E)
25endif
26ifeq ($(ECHO_E_BIN_ECHO_TAB),$(TAB))
27ECHO_E		:= $(ECHO_E_BIN_ECHO)
28endif
29ifeq ($(ECHO_E_BIN_ECHO_E_TAB),$(TAB))
30ECHO_E		:= $(ECHO_E_BIN_ECHO_E)
31endif
32
33.echocheck :
34ifdef ECHO_E
35	@$(TOUCH) $@
36else
37	@$(PRINTF) '%24s : x%sx\n' 'tab' '$(TAB)'
38	@$(PRINTF) '%24s : x%sx\n' '"$(ECHO_E_ECHO) \t"' \
39				    '$(ECHO_E_ECHO_TAB)'
40	@$(PRINTF) '%24s : x%sx\n' '"$(ECHO_E_ECHO_E) \t"' \
41				    '$(ECHO_E_ECHO_E_TAB)'
42	@$(PRINTF) '%24s : x%sx\n' '"$(ECHO_E_BIN_ECHO) \t"' \
43				    '$(ECHO_E_BIN_ECHO_TAB)'
44	@$(PRINTF) '%24s : x%sx\n' '"$(ECHO_E_BIN_ECHO_E) \t"' \
45				    '$(ECHO_E_BIN_ECHO_E_TAB)'
46	@$(ECHO) "No usable \"echo -e\" substitute found"
47	@exit 1
48endif
49MAKEDEPS	+= .echocheck
50VERYCLEANUP	+= .echocheck
51
52echo :
53	@$(ECHO) "Using \"$(ECHO_E)\" for \"echo -e\""
54
55###############################################################################
56#
57# Generate a usable "seq" substitute
58#
59define seq
60	$(shell awk 'BEGIN { for ( i = $(1) ; i <= $(2) ; i++ ) print i }')
61endef
62
63###############################################################################
64#
65# Determine host OS
66#
67HOST_OS		:= $(shell uname -s)
68hostos :
69	@$(ECHO) $(HOST_OS)
70
71###############################################################################
72#
73# Determine compiler
74
75CCDEFS		:= $(shell $(CC) -E -x c -c /dev/null -dM | cut -d" " -f2)
76ccdefs:
77	@$(ECHO) $(CCDEFS)
78
79ifeq ($(filter __ICC,$(CCDEFS)),__ICC)
80CCTYPE		:= icc
81else
82CCTYPE		:= gcc
83endif
84cctype:
85	@$(ECHO) $(CCTYPE)
86
87###############################################################################
88#
89# Check for tools that can cause failed builds
90#
91.toolcheck :
92	@if $(CC) -v 2>&1 | grep -is 'gcc version 2\.96' > /dev/null; then \
93		$(ECHO) 'gcc 2.96 is unsuitable for compiling Etherboot'; \
94		$(ECHO) 'Use gcc 2.95 or gcc 3.x instead'; \
95		exit 1; \
96	fi
97	@if [ `perl -e 'use bytes; print chr(255)' | wc -c` = 2 ]; then \
98		$(ECHO) 'Your Perl version has a Unicode handling bug'; \
99		$(ECHO) 'Execute this command before compiling Etherboot:'; \
100		$(ECHO) 'export LANG=$${LANG%.UTF-8}'; \
101		exit 1; \
102	fi
103	@$(TOUCH) $@
104MAKEDEPS	+= .toolcheck
105VERYCLEANUP	+= .toolcheck
106
107###############################################################################
108#
109# Check for various tool workarounds
110#
111
112# Make syntax does not allow use of comma or space in certain places.
113# This ugly workaround is suggested in the manual.
114#
115COMMA	:= ,
116EMPTY	:=
117SPACE	:= $(EMPTY) $(EMPTY)
118
119# Check for an old version of gas (binutils 2.9.1)
120#
121OLDGAS	:= $(shell $(AS) --version | grep -q '2\.9\.1' && $(ECHO) -DGAS291)
122CFLAGS	+= $(OLDGAS)
123oldgas :
124	@$(ECHO) $(oldgas)
125
126# Some widespread patched versions of gcc include -fstack-protector by
127# default, even when -ffreestanding is specified.  We therefore need
128# to disable -fstack-protector if the compiler supports it.
129#
130ifeq ($(CCTYPE),gcc)
131SP_TEST = $(CC) -fno-stack-protector -x c -c /dev/null \
132		-o /dev/null >/dev/null 2>&1
133SP_FLAGS := $(shell $(SP_TEST) && $(ECHO) '-fno-stack-protector')
134CFLAGS	+= $(SP_FLAGS)
135endif
136
137# gcc 4.4 generates .eh_frame sections by default, which distort the
138# output of "size".  Inhibit this.
139#
140ifeq ($(CCTYPE),gcc)
141CFI_TEST = $(CC) -fno-dwarf2-cfi-asm -x c -c /dev/null \
142		 -o /dev/null >/dev/null 2>&1
143CFI_FLAGS := $(shell $(CFI_TEST) && $(ECHO) '-fno-dwarf2-cfi-asm')
144CFLAGS	+= $(CFI_FLAGS)
145endif
146
147# Some versions of gas choke on division operators, treating them as
148# comment markers.  Specifying --divide will work around this problem,
149# but isn't available on older gas versions.
150#
151DIVIDE_TEST = $(AS) --divide /dev/null -o /dev/null 2>/dev/null
152DIVIDE_FLAGS := $(shell $(DIVIDE_TEST) && $(ECHO) '--divide')
153ASFLAGS	+= $(DIVIDE_FLAGS)
154
155###############################################################################
156#
157# Build verbosity
158#
159ifeq ($(V),1)
160Q :=
161QM := @\#
162else
163Q := @
164QM := @
165endif
166
167###############################################################################
168#
169# Set BIN according to whatever was specified on the command line as
170# the build target.
171#
172
173# Determine how many different BIN directories are mentioned in the
174# make goals.
175#
176BIN_GOALS	:= $(filter bin/% bin-%,$(MAKECMDGOALS))
177BIN_GOAL_BINS	:= $(foreach BG,$(BIN_GOALS),$(firstword $(subst /, ,$(BG))))
178NUM_BINS	:= $(words $(sort $(BIN_GOAL_BINS)))
179
180ifeq ($(NUM_BINS),0)
181
182# No BIN directory was specified.  Set BIN to "bin" as a sensible
183# default.
184
185BIN		:= bin
186
187else # NUM_BINS == 0
188
189ifeq ($(NUM_BINS),1)
190
191# If exactly one BIN directory was specified, set BIN to match this
192# directory.
193#
194BIN		:= $(firstword $(BIN_GOAL_BINS))
195
196else # NUM_BINS == 1
197
198# More than one BIN directory was specified.  We cannot handle the
199# latter case within a single make invocation, so set up recursive
200# targets for each BIN directory.
201#
202# Leave $(BIN) undefined.  This has implications for any target that
203# depends on $(BIN); such targets should be made conditional upon the
204# existence of $(BIN).
205#
206$(BIN_GOALS) : % : BIN_RECURSE
207	$(Q)$(MAKE) --no-print-directory BIN=$(firstword $(subst /, ,$@)) $@
208.PHONY : BIN_RECURSE
209
210endif # NUM_BINS == 1
211endif # NUM_BINS == 0
212
213ifdef BIN
214
215# Create $(BIN) directory if it doesn't exist yet
216#
217ifeq ($(wildcard $(BIN)),)
218$(shell $(MKDIR) -p $(BIN))
219endif
220
221# Target to allow e.g. "make bin-efi arch"
222#
223$(BIN) :
224	@# Do nothing, silently
225.PHONY : $(BIN)
226
227# Remove everything in $(BIN) for a "make clean"
228#
229CLEANUP	+= $(BIN)/*.* # Avoid picking up directories
230
231endif # defined(BIN)
232
233# Determine whether or not we need to include the dependency files
234#
235NO_DEP_TARGETS	:= $(BIN) clean veryclean
236ifeq ($(MAKECMDGOALS),)
237NEED_DEPS	:= 1
238endif
239ifneq ($(strip $(filter-out $(NO_DEP_TARGETS),$(MAKECMDGOALS))),)
240NEED_DEPS	:= 1
241endif
242
243###############################################################################
244#
245# Select build architecture and platform based on $(BIN)
246#
247# BIN has the form bin[-[arch-]platform]
248
249ARCHS		:= $(patsubst arch/%,%,$(wildcard arch/*))
250PLATFORMS	:= $(patsubst config/defaults/%.h,%,\
251		     $(wildcard config/defaults/*.h))
252archs :
253	@$(ECHO) $(ARCHS)
254
255platforms :
256	@$(ECHO) $(PLATFORMS)
257
258ifdef BIN
259
260# Determine architecture portion of $(BIN), if present
261BIN_ARCH	:= $(strip $(foreach A,$(ARCHS),\
262			     $(patsubst bin-$(A)-%,$(A),\
263			       $(filter bin-$(A)-%,$(BIN)))))
264
265# Determine platform portion of $(BIN), if present
266ifeq ($(BIN_ARCH),)
267BIN_PLATFORM	:= $(patsubst bin-%,%,$(filter bin-%,$(BIN)))
268else
269BIN_PLATFORM	:= $(patsubst bin-$(BIN_ARCH)-%,%,$(BIN))
270endif
271
272# Determine build architecture
273DEFAULT_ARCH	:= i386
274ARCH		:= $(firstword $(BIN_ARCH) $(DEFAULT_ARCH))
275CFLAGS		+= -DARCH=$(ARCH)
276arch :
277	@$(ECHO) $(ARCH)
278.PHONY : arch
279
280# Determine build platform
281DEFAULT_PLATFORM := pcbios
282PLATFORM	:= $(firstword $(BIN_PLATFORM) $(DEFAULT_PLATFORM))
283CFLAGS		+= -DPLATFORM=$(PLATFORM)
284platform :
285	@$(ECHO) $(PLATFORM)
286
287endif # defined(BIN)
288
289# Include architecture-specific Makefile
290ifdef ARCH
291MAKEDEPS	+= arch/$(ARCH)/Makefile
292include arch/$(ARCH)/Makefile
293endif
294
295# Include architecture-specific include path
296ifdef ARCH
297INCDIRS		+= arch/$(ARCH)/include
298endif
299
300###############################################################################
301#
302# Source file handling
303
304# SRCDIRS lists all directories containing source files.
305srcdirs :
306	@$(ECHO) $(SRCDIRS)
307
308# SRCS lists all .c or .S files found in any SRCDIR
309#
310SRCS	+= $(wildcard $(patsubst %,%/*.c,$(SRCDIRS)))
311SRCS	+= $(wildcard $(patsubst %,%/*.S,$(SRCDIRS)))
312srcs :
313	@$(ECHO) $(SRCS)
314
315# AUTO_SRCS lists all files in SRCS that are not mentioned in
316# NON_AUTO_SRCS.  Files should be added to NON_AUTO_SRCS if they
317# cannot be built using the standard build template.
318#
319AUTO_SRCS = $(filter-out $(NON_AUTO_SRCS),$(SRCS))
320autosrcs :
321	@$(ECHO) $(AUTO_SRCS)
322
323# Just about everything else in this section depends upon having
324# $(BIN) set
325
326ifdef BIN
327
328# INCDIRS lists the include path
329incdirs :
330	@$(ECHO) $(INCDIRS)
331
332# Common flags
333#
334CFLAGS		+= $(foreach INC,$(INCDIRS),-I$(INC))
335CFLAGS		+= -Os
336CFLAGS		+= -g
337ifeq ($(CCTYPE),gcc)
338CFLAGS		+= -ffreestanding
339CFLAGS		+= -Wall -W -Wformat-nonliteral
340endif
341ifeq ($(CCTYPE),icc)
342CFLAGS		+= -fno-builtin
343CFLAGS		+= -no-ip
344CFLAGS		+= -no-gcc
345CFLAGS		+= -diag-disable 111 # Unreachable code
346CFLAGS		+= -diag-disable 128 # Unreachable loop
347CFLAGS		+= -diag-disable 170 # Array boundary checks
348CFLAGS		+= -diag-disable 177 # Unused functions
349CFLAGS		+= -diag-disable 181 # printf() format checks
350CFLAGS		+= -diag-disable 188 # enum strictness
351CFLAGS		+= -diag-disable 193 # Undefined preprocessor identifiers
352CFLAGS		+= -diag-disable 280 # switch ( constant )
353CFLAGS		+= -diag-disable 310 # K&R parameter lists
354CFLAGS		+= -diag-disable 424 # Extra semicolon
355CFLAGS		+= -diag-disable 589 # Declarations mid-code
356CFLAGS		+= -diag-disable 593 # Unused variables
357CFLAGS		+= -diag-disable 810 # Casting ints to smaller ints
358CFLAGS		+= -diag-disable 981 # Sequence point violations
359CFLAGS		+= -diag-disable 1292 # Ignored attributes
360CFLAGS		+= -diag-disable 1338 # void pointer arithmetic
361CFLAGS		+= -diag-disable 1361 # Variable-length arrays
362CFLAGS		+= -diag-disable 1418 # Missing prototypes
363CFLAGS		+= -diag-disable 1419 # Missing prototypes
364CFLAGS		+= -diag-disable 1599 # Hidden variables
365CFLAGS		+= -Wall -Wmissing-declarations
366endif
367CFLAGS		+= $(EXTRA_CFLAGS)
368ASFLAGS		+= $(EXTRA_ASFLAGS)
369LDFLAGS		+= $(EXTRA_LDFLAGS)
370
371# Inhibit -Werror if NO_WERROR is specified on make command line
372#
373ifneq ($(NO_WERROR),1)
374CFLAGS		+= -Werror
375ASFLAGS		+= --fatal-warnings
376endif
377
378# compiler.h is needed for our linking and debugging system
379#
380CFLAGS		+= -include compiler.h
381
382# CFLAGS for specific object types
383#
384CFLAGS_c	+=
385CFLAGS_S 	+= -DASSEMBLY
386
387# Base object name of the current target
388#
389OBJECT		= $(firstword $(subst ., ,$(@F)))
390
391# CFLAGS for specific object files.  You can define
392# e.g. CFLAGS_rtl8139, and have those flags automatically used when
393# compiling bin/rtl8139.o.
394#
395OBJ_CFLAGS	= $(CFLAGS_$(OBJECT)) -DOBJECT=$(subst -,_,$(OBJECT))
396$(BIN)/%.flags :
397	@$(ECHO) $(OBJ_CFLAGS)
398
399# ICC requires postprocessing objects to fix up table alignments
400#
401ifeq ($(CCTYPE),icc)
402POST_O		= && $(ICCFIX) $@
403POST_O_DEPS	:= $(ICCFIX)
404else
405POST_O		:=
406POST_O_DEPS	:=
407endif
408
409# Rules for specific object types.
410#
411COMPILE_c	= $(CC) $(CFLAGS) $(CFLAGS_c) $(OBJ_CFLAGS)
412RULE_c		= $(Q)$(COMPILE_c) -c $< -o $@ $(POST_O)
413RULE_c_to_dbg%.o = $(Q)$(COMPILE_c) -Ddebug_$(subst -,_,$(OBJECT))=$* -c $< -o $@ $(POST_O)
414RULE_c_to_c	= $(Q)$(COMPILE_c) -E -c $< > $@
415RULE_c_to_s	= $(Q)$(COMPILE_c) -S -g0 -c $< -o $@
416
417PREPROCESS_S	= $(CPP) $(CFLAGS) $(CFLAGS_S) $(OBJ_CFLAGS)
418ASSEMBLE_S	= $(AS) $(ASFLAGS)
419RULE_S		= $(Q)$(PREPROCESS_S) $< | $(ASSEMBLE_S) -o $@
420RULE_S_to_s	= $(Q)$(PREPROCESS_S) $< > $@
421
422DEBUG_TARGETS	+= dbg%.o c s
423
424# We automatically generate rules for any file mentioned in AUTO_SRCS
425# using the following set of templates.  It would be cleaner to use
426# $(eval ...), but this function exists only in GNU make >= 3.80.
427
428# src_template : generate Makefile rules for a given source file
429#
430# $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
431# $(2) is the full path to the .d file (e.g. "bin/deps/drivers/net/rtl8139.d")
432# $(3) is the source type (e.g. "c")
433# $(4) is the source base name (e.g. "rtl8139")
434#
435define src_template
436
437	@$(ECHO) "  [DEPS] $(1)"
438	@$(MKDIR) -p $(dir $(2))
439	@$(RM) $(2)
440	@$(TOUCH) $(2)
441	@$(CPP) $(CFLAGS) $(CFLAGS_$(3)) $(CFLAGS_$(4)) -DOBJECT=$(4) \
442		-Wno-error -MM $(1) -MG -MP | \
443		sed 's/\.o\s*:/_DEPS =/' >> $(2)
444	@$(ECHO_E) '\n$$(BIN)/$(4).o :' \
445		 '$(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(4)_DEPS)' \
446		 '\n\t$$(QM)$(ECHO) "  [BUILD] $$@"' \
447		 '\n\t$$(RULE_$(3))\n' \
448		 '\nBOBJS += $$(BIN)/$(4).o\n' \
449		 $(foreach TGT,$(DEBUG_TARGETS), \
450		    $(if $(RULE_$(3)_to_$(TGT)), \
451		    '\n$$(BIN)/$(4).$(TGT) :' \
452		    '$(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(4)_DEPS)' \
453		    '\n\t$$(QM)$(ECHO) "  [BUILD] $$@"' \
454		    '\n\t$$(RULE_$(3)_to_$(TGT))\n' \
455		    '\n$(TGT)_OBJS += $$(BIN)/$(4).$(TGT)\n' ) ) \
456		 '\n$(2) : $$($(4)_DEPS)\n' \
457		 '\nTAGS : $$($(4)_DEPS)\n' \
458		>> $(2)
459	@$(PARSEROM) $(1) >> $(2)
460
461endef
462
463# Rule to generate the Makefile rules files to be included
464#
465$(BIN)/deps/%.d : % $(MAKEDEPS) $(PARSEROM)
466	$(if $(filter $(AUTO_SRCS),$<),$(call src_template,$<,$@,$(subst .,,$(suffix $<)),$(basename $(notdir $<))),@$(ECHO) 'ERROR: $< is not an AUTO_SRC' ; exit 1)
467
468# Calculate and include the list of Makefile rules files
469#
470AUTO_DEPS	= $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
471ifdef NEED_DEPS
472ifneq ($(AUTO_DEPS),)
473-include $(AUTO_DEPS)
474endif
475endif
476autodeps :
477	@$(ECHO) $(AUTO_DEPS)
478VERYCLEANUP	+= $(BIN)/deps
479
480# The following variables are created by the Makefile rules files
481#
482bobjs :
483	@$(ECHO) $(BOBJS)
484drivers :
485	@$(ECHO) $(DRIVERS)
486.PHONY : drivers
487roms :
488	@$(ECHO) $(ROMS)
489
490# List of embedded images included in the last build of embedded.o.
491# This is needed in order to correctly rebuild embedded.o whenever the
492# list of objects changes.
493#
494EMBEDDED_LIST	:= $(BIN)/.embedded.list
495ifeq ($(wildcard $(EMBEDDED_LIST)),)
496EMBEDDED_LIST_IMAGE := <invalid>
497else
498EMBEDDED_LIST_IMAGE := $(shell cat $(EMBEDDED_LIST))
499endif
500ifneq ($(EMBEDDED_LIST_IMAGE),$(EMBEDDED_IMAGE))
501$(shell $(ECHO) "$(EMBEDDED_IMAGE)" > $(EMBEDDED_LIST))
502endif
503
504$(EMBEDDED_LIST) :
505
506VERYCLEANUP	+= $(EMBEDDED_LIST)
507
508EMBEDDED_FILES	:= $(subst $(COMMA), ,$(EMBEDDED_IMAGE))
509EMBED_ALL	:= $(foreach i,$(call seq,1,$(words $(EMBEDDED_FILES))),\
510		     EMBED ( $(i), \"$(word $(i), $(EMBEDDED_FILES))\",\
511			     \"$(notdir $(word $(i),$(EMBEDDED_FILES)))\" ))
512
513$(BIN)/embedded.o : $(EMBEDDED_FILES) $(EMBEDDED_LIST)
514CFLAGS_embedded = -DEMBED_ALL="$(EMBED_ALL)"
515
516# Generate the NIC file from the parsed source files.  The NIC file is
517# only for rom-o-matic.
518#
519$(BIN)/NIC : $(AUTO_DEPS)
520	@$(ECHO) '# This is an automatically generated file, do not edit' > $@
521	@$(ECHO) '# It does not affect anything in the build, ' \
522	     'it is only for rom-o-matic' >> $@
523	@$(ECHO) >> $@
524	@perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
525CLEANUP		+= $(BIN)/NIC	# Doesn't match the $(BIN)/*.* pattern
526
527# Analyse a target name (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and
528# derive the variables:
529#
530# TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
531# TGT_PREFIX   : the prefix type (e.g. "zrom")
532# TGT_DRIVERS  : the driver for each element (e.g. "rtl8139 prism2_pci")
533# TGT_ROM_NAME : the ROM name (e.g. "dfe538")
534# TGT_MEDIA    : the media type (e.g. "rom")
535#
536DRIVERS_gpxe	= $(DRIVERS)
537CARD_DRIVER	= $(firstword $(DRIVER_$(1)) $(1))
538TGT_ELEMENTS	= $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
539TGT_PREFIX	= $(word 2,$(subst ., ,$(notdir $@)))
540TGT_ROM_NAME	= $(firstword $(TGT_ELEMENTS))
541TGT_DRIVERS	= $(strip $(if $(DRIVERS_$(TGT_ROM_NAME)), \
542			       $(DRIVERS_$(TGT_ROM_NAME)), \
543			       $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
544			         $(call CARD_DRIVER,$(TGT_ELEMENT))) ))
545TGT_MEDIA	= $(subst z,,$(TGT_PREFIX))
546
547# Look up ROM IDs for the current target
548# (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
549#
550# TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
551# TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
552#
553TGT_PCI_VENDOR	= $(PCI_VENDOR_$(TGT_ROM_NAME))
554TGT_PCI_DEVICE	= $(PCI_DEVICE_$(TGT_ROM_NAME))
555
556# Calculate link-time options for the current target
557# (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
558#
559# TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
560#		   (e.g. "obj_rtl8139 obj_prism2_pci")
561# TGT_LD_IDS :     symbols to define in order to fill in ID structures in the
562#		   ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
563#
564TGT_LD_DRIVERS	= $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
565TGT_LD_PREFIX	= obj_$(TGT_PREFIX)prefix
566TGT_LD_IDS	= pci_vendor_id=$(firstword $(TGT_PCI_VENDOR) 0) \
567		  pci_device_id=$(firstword $(TGT_PCI_DEVICE) 0)
568
569# Calculate linker flags based on link-time options for the current
570# target type (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the
571# variables:
572#
573# TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
574#		 "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
575#		  --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
576#
577TGT_LD_FLAGS	= $(foreach SYM,$(TGT_LD_PREFIX) $(TGT_LD_DRIVERS) obj_config,\
578		    -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
579		  $(patsubst %,--defsym %,$(TGT_LD_IDS))
580
581# Calculate makerom flags for the specific target
582# (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
583#
584# TGT_MAKEROM_FLAGS : target-specific flags for makerom (e.g.
585#		      "-p 0x1186,0x1300")
586#
587TGT_MAKEROM_FLAGS = $(strip $(MAKEROM_FLAGS_$(TGT_ROM_NAME)) \
588       $(if $(TGT_PCI_VENDOR),$(strip -p $(TGT_PCI_VENDOR),$(TGT_PCI_DEVICE))))
589
590# Calculate list of debugging versions of objects to be included in
591# the target.
592#
593DEBUG_LIST	= $(subst $(COMMA), ,$(DEBUG))
594DEBUG_OBJ_LEVEL	= $(firstword $(word 2,$(subst :, ,$(1))) 1)
595DEBUG_OBJ_BASE	= $(word 1,$(subst :, ,$(1))).dbg$(call DEBUG_OBJ_LEVEL,$(1))
596DEBUG_OBJ	= $(BIN)/$(call DEBUG_OBJ_BASE,$(1)).o
597DEBUG_ORIG_OBJ	= $(BIN)/$(word 1,$(subst :, ,$(1))).o
598DEBUG_OBJS	= $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
599DEBUG_ORIG_OBJS	= $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
600BLIB_OBJS	= $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
601
602# Print out all derived information for a given target.
603#
604$(BIN)/%.info :
605	@$(ECHO) 'Elements             : $(TGT_ELEMENTS)'
606	@$(ECHO) 'Prefix               : $(TGT_PREFIX)'
607	@$(ECHO) 'Drivers              : $(TGT_DRIVERS)'
608	@$(ECHO) 'ROM name             : $(TGT_ROM_NAME)'
609	@$(ECHO) 'Media                : $(TGT_MEDIA)'
610	@$(ECHO)
611	@$(ECHO) 'PCI vendor           : $(TGT_PCI_VENDOR)'
612	@$(ECHO) 'PCI device           : $(TGT_PCI_DEVICE)'
613	@$(ECHO)
614	@$(ECHO) 'LD driver symbols    : $(TGT_LD_DRIVERS)'
615	@$(ECHO) 'LD prefix symbols    : $(TGT_LD_PREFIX)'
616	@$(ECHO) 'LD ID symbols        : $(TGT_LD_IDS)'
617	@$(ECHO)
618	@$(ECHO) 'LD target flags      : $(TGT_LD_FLAGS)'
619	@$(ECHO)
620	@$(ECHO) 'makerom target flags : $(TGT_MAKEROM_FLAGS)'
621	@$(ECHO)
622	@$(ECHO) 'Debugging objects    : $(DEBUG_OBJS)'
623	@$(ECHO) 'Replaced objects     : $(DEBUG_ORIG_OBJS)'
624
625# List of objects included in the last build of blib.  This is needed
626# in order to correctly rebuild blib whenever the list of objects
627# changes.
628#
629BLIB_LIST	:= $(BIN)/.blib.list
630ifeq ($(wildcard $(BLIB_LIST)),)
631BLIB_LIST_OBJS	:= <invalid>
632else
633BLIB_LIST_OBJS	:= $(shell cat $(BLIB_LIST))
634endif
635ifneq ($(BLIB_LIST_OBJS),$(BLIB_OBJS))
636$(shell $(ECHO) "$(BLIB_OBJS)" > $(BLIB_LIST))
637endif
638
639$(BLIB_LIST) :
640
641VERYCLEANUP	+= $(BLIB_LIST)
642
643# Library of all objects
644#
645BLIB		= $(BIN)/blib.a
646$(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
647	$(Q)$(RM) $(BLIB)
648	$(QM)$(ECHO) "  [AR] $@"
649	$(Q)$(AR) r $@ $(BLIB_OBJS)
650	$(Q)$(RANLIB) $@
651blib : $(BLIB)
652
653# Build an intermediate object file from the objects required for the
654# specified target.
655#
656$(BIN)/%.tmp : $(BLIB) $(MAKEDEPS) $(LDSCRIPT)
657	$(QM)$(ECHO) "  [LD] $@"
658	$(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $(BLIB) -o $@ \
659		-Map $(BIN)/$*.tmp.map
660	$(Q)$(OBJDUMP) -ht $@ | $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
661
662# Keep intermediate object file (useful for debugging)
663.PRECIOUS : $(BIN)/%.tmp
664
665# Show a linker map for the specified target
666#
667$(BIN)/%.map : $(BIN)/%.tmp
668	@less $(BIN)/$*.tmp.map
669
670# Get objects list for the specified target
671#
672define objs_list
673	$(sort $(foreach OBJ_SYMBOL,\
674		 $(filter obj_%,$(shell $(NM) $(1) | cut -d" " -f3)),\
675		 $(patsubst obj_%,%,$(OBJ_SYMBOL))))
676endef
677$(BIN)/%.objs : $(BIN)/%.tmp
678	$(Q)$(ECHO) $(call objs_list,$<)
679$(BIN)/%.sizes : $(BIN)/%.tmp
680	$(Q)$(SIZE) -t $(foreach OBJ,$(call objs_list,$<),$(wildcard $(BIN)/$(subst _,?,$(OBJ)).o)) | \
681		sort -g
682
683# Get dependency list for the specified target
684#
685define deps_list
686	$(sort $(foreach OBJ,$(call objs_list,$(1)),$($(OBJ)_DEPS)))
687endef
688$(BIN)/%.deps : $(BIN)/%.tmp
689	$(Q)$(ECHO) $(call deps_list,$<)
690
691# Get unneeded source files for the specified target
692#
693define nodeps_list
694	$(sort $(filter-out $(call deps_list,$(1)),\
695		 $(foreach BOBJ,$(BOBJS),\
696		   $($(basename $(notdir $(BOBJ)))_DEPS))))
697endef
698$(BIN)/%.nodeps : $(BIN)/%.tmp
699	$(Q)$(ECHO) $(call nodeps_list,$<)
700
701# Get licensing verdict for the specified target
702#
703define unlicensed_deps_list
704	$(shell grep -L FILE_LICENCE $(call deps_list,$(1)))
705endef
706define licence_list
707	$(patsubst __licence_%,%,\
708	  $(filter __licence_%,$(shell $(NM) $(1) | cut -d" " -f3)))
709endef
710$(BIN)/%.licence : $(BIN)/%.tmp
711	$(QM)$(ECHO) "  [LICENCE] $@"
712	$(Q)$(if $(strip $(call unlicensed_deps_list,$<)),\
713		echo -n "Unable to determine licence because the following " ;\
714		echo "files are missing a licence declaration:" ;\
715		echo $(call unlicensed_deps_list,$<);\
716		exit 1,\
717		$(LICENCE) $(call licence_list,$<))
718
719# Extract compression information from intermediate object file
720#
721$(BIN)/%.zinfo : $(BIN)/%.tmp
722	$(QM)$(ECHO) "  [ZINFO] $@"
723	$(Q)$(OBJCOPY) -O binary -j .zinfo $< $@
724
725# Build raw binary file from intermediate object file
726#
727$(BIN)/%.bin : $(BIN)/%.tmp
728	$(QM)$(ECHO) "  [BIN] $@"
729	$(Q)$(OBJCOPY) -O binary -R .zinfo $< $@
730
731# Compress raw binary file
732#
733$(BIN)/%.zbin : $(BIN)/%.bin $(BIN)/%.zinfo $(ZBIN)
734	$(QM)$(ECHO) "  [ZBIN] $@"
735	$(Q)$(ZBIN) $(BIN)/$*.bin $(BIN)/$*.zinfo > $@
736
737# Rules for each media format.  These are generated and placed in an
738# external Makefile fragment.  We could do this via $(eval ...), but
739# that would require make >= 3.80.
740#
741# Note that there's an alternative way to generate most .rom images:
742# they can be copied from their 'master' ROM image using cp and
743# reprocessed with makerom to add the PCI IDs and ident string.  The
744# relevant rule would look something like:
745#
746#   $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
747#	cat $< $@
748#	$(FINALISE_rom)
749#
750# You can derive the ROM/driver relationships using the variables
751# DRIVER_<rom> and/or ROMS_<driver>.
752#
753# We don't currently do this, because (a) it would require generating
754# yet more Makefile fragments (since you need a rule for each ROM in
755# ROMS), and (b) the linker is so fast that it probably wouldn't make
756# much difference to the overall build time.
757
758media :
759	@$(ECHO) $(MEDIA)
760
761AUTO_MEDIA	= $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
762automedia :
763	@$(ECHO) $(AUTO_MEDIA)
764
765# media_template : create Makefile rules for specified media
766#
767# $(1) is the media name (e.g. "rom")
768# $(2) is the full path to the .d file (e.g. "bin/deps/rom.media.d")
769#
770define media_template
771
772	@$(ECHO) "  [MEDIADEPS] $(1)"
773	@$(MKDIR) -p $(dir $(2))
774	@$(RM) $(2)
775	@$(TOUCH) $(2)
776	@$(ECHO_E) '$$(BIN)/%.$(1) : $$(BIN)/%.$(1).zbin' \
777		  '\n\t$$(QM)$(ECHO) "  [FINISH] $$@"' \
778		  '\n\t$$(Q)$$(CP) $$< $$@' \
779		  '\n\t$$(Q)$$(PAD_$(1))' \
780		  '\n\t$$(Q)$$(FINALISE_$(1))' \
781		> $(2)
782
783endef
784
785# Rule to generate the Makefile rules to be included
786#
787$(BIN)/deps/%.media.d : $(MAKEDEPS)
788	$(if $(filter $(AUTO_MEDIA),$*), \
789		$(call media_template,$*,$@), \
790		@$(ECHO) 'ERROR: $* is not an AUTO_MEDIA' ; exit 1)
791
792# Calculate and include the list of Makefile rules files
793#
794MEDIA_DEPS		= $(patsubst %,$(BIN)/deps/%.media.d,$(AUTO_MEDIA))
795mediadeps :
796	@$(ECHO) $(MEDIA_DEPS)
797ifdef NEED_DEPS
798ifneq ($(MEDIA_DEPS),)
799-include $(MEDIA_DEPS)
800endif
801endif
802
803# Wrap up binary blobs (for embedded images)
804#
805$(BIN)/%.o : payload/%.img
806	$(QM)echo "  [WRAP] $@"
807	$(Q)$(LD) -b binary -r -o $@ $< --undefined obj_payload \
808		--defsym obj_$*=0
809
810BOBJS += $(patsubst payload/%.img,$(BIN)/%.o,$(wildcard payload/*.img))
811
812# The "allXXXs" targets for each suffix
813#
814allall: allroms allzroms allpxes allisos alldsks
815allroms allzroms : all%s : $(foreach ROM,$(ROMS),$(BIN)/$(ROM).%)
816allpxes allisos alldsks : all%s : $(foreach DRIVER,$(DRIVERS),$(BIN)/$(DRIVER).%)
817
818# Alias for gpxe.%
819#
820$(BIN)/etherboot.% : $(BIN)/gpxe.%
821	ln -sf $(notdir $<) $@
822
823endif # defined(BIN)
824
825###############################################################################
826#
827# Rules for finalising files.  TGT_MAKEROM_FLAGS is defined as part of
828# the automatic build system and varies by target; it includes the
829# "-p 0x1234,0x5678" string to set the PCI IDs.
830#
831FINALISE_rom	= $(MAKEROM) $(MAKEROM_FLAGS) $(TGT_MAKEROM_FLAGS) \
832		  -i$(IDENT) -s 0 $@
833FINALISE_hrom	= $(FINALISE_rom)
834FINALISE_xrom	= $(MAKEROM) $(MAKEROM_FLAGS) $(TGT_MAKEROM_FLAGS) \
835		  -i$(IDENT) -n -s 0 $@
836
837# Some ROMs require specific flags to be passed to makerom.pl
838#
839MAKEROM_FLAGS_3c503 = -3
840
841###############################################################################
842#
843# The compression utilities
844#
845$(NRV2B) : util/nrv2b.c $(MAKEDEPS)
846	$(QM)$(ECHO) "  [HOSTCC] $@"
847	$(Q)$(HOST_CC) -O2 -DENCODE -DDECODE -DMAIN -DVERBOSE -DNDEBUG \
848		       -DBITSIZE=32 -DENDIAN=0 -o $@ $<
849CLEANUP	+= $(NRV2B)
850
851$(ZBIN) : util/zbin.c util/nrv2b.c $(MAKEDEPS)
852	$(QM)$(ECHO) "  [HOSTCC] $@"
853	$(Q)$(HOST_CC) -O2 -o $@ $<
854CLEANUP += $(ZBIN)
855
856###############################################################################
857#
858# The EFI image converter
859#
860ELF2EFI_CFLAGS	:= -I$(BINUTILS_DIR)/include -I$(BFD_DIR)/include \
861		   -idirafter include -L$(BINUTILS_DIR)/lib -L$(BFD_DIR)/lib \
862		   -lbfd -liberty -lz
863
864$(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
865	$(QM)$(ECHO) "  [HOSTCC] $@"
866	$(Q)$(HOST_CC) $(ELF2EFI_CFLAGS) -DMDE_CPU_IA32 -O2 -o $@ $<
867CLEANUP += $(ELF2EFI32)
868
869$(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
870	$(QM)$(ECHO) "  [HOSTCC] $@"
871	$(Q)$(HOST_CC) $(ELF2EFI_CFLAGS) -DMDE_CPU_X64 -O2 -o $@ $<
872CLEANUP += $(ELF2EFI64)
873
874$(EFIROM) : util/efirom.c $(MAKEDEPS)
875	$(QM)$(ECHO) "  [HOSTCC] $@"
876	$(Q)$(HOST_CC) -idirafter include -O2 -o $@ $<
877CLEANUP += $(EFIROM)
878
879###############################################################################
880#
881# The ICC fixup utility
882#
883$(ICCFIX) : util/iccfix.c $(MAKEDEPS)
884	$(QM)$(ECHO) "  [HOSTCC] $@"
885	$(Q)$(HOST_CC) -idirafter include -O2 -o $@ $<
886CLEANUP += $(ICCFIX)
887
888###############################################################################
889#
890# Auto-incrementing build serial number.  Append "bs" to your list of
891# build targets to get a serial number printed at the end of the
892# build.  Enable -DBUILD_SERIAL in order to see it when the code runs.
893#
894BUILDSERIAL_H		= config/.buildserial.h
895BUILDSERIAL_NOW		= config/.buildserial.now
896BUILDSERIAL_NEXT	= config/.buildserial.next
897
898$(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) :
899	$(ECHO) 1 > $@
900
901$(BUILDSERIAL_H) : $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT)
902	$(ECHO) '#define BUILD_SERIAL_NUM $(shell cat $<)' > $@
903
904ifeq ($(filter bs,$(MAKECMDGOALS)),bs)
905$(shell diff -q $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) > /dev/null || \
906	cp -f $(BUILDSERIAL_NEXT) $(BUILDSERIAL_NOW))
907endif
908
909bs : $(BUILDSERIAL_NOW)
910	@$(ECHO) $$(( $(shell cat $<) + 1 )) > $(BUILDSERIAL_NEXT)
911	@$(ECHO) "Build serial number is $(shell cat $<)"
912
913###############################################################################
914#
915# Build the TAGS file(s) for emacs
916#
917TAGS :
918	ctags -e -R -f $@ --exclude=bin
919
920CLEANUP	+= TAGS
921
922###############################################################################
923#
924# Force rebuild for any given target
925#
926%.rebuild :
927	rm -f $*
928	$(Q)$(MAKE) $*
929
930###############################################################################
931#
932# Symbol table checks
933#
934
935ifdef BIN
936
937SYMTAB	= $(BIN)/symtab
938$(SYMTAB) : $(BLIB)
939	$(OBJDUMP) -w -t $< > $@
940
941CLEANUP	+= $(BIN)/symtab
942
943symcheck : $(SYMTAB)
944	$(SYMCHECK) $<
945
946endif # defined(BIN)
947
948###############################################################################
949#
950# Build bochs symbol table
951#
952
953ifdef BIN
954
955$(BIN)/%.bxs : $(BIN)/%.tmp
956	$(NM) $< | cut -d" " -f1,3 > $@
957
958endif # defined(BIN)
959
960###############################################################################
961#
962# Documentation
963#
964
965ifdef BIN
966
967$(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
968	$(Q)$(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
969		-e  's{\@INCDIRS\@}{$(filter-out .,$(INCDIRS))}; ' \
970		-e  's{\@BIN\@}{$(BIN)}; ' \
971		-e  's{\@ARCH\@}{$(ARCH)}; ' \
972		$< > $@
973
974$(BIN)/doc : $(BIN)/doxygen.cfg
975	$(Q)$(DOXYGEN) $<
976
977.PHONY : $(BIN)/doc
978
979doc : $(BIN)/doc
980
981doc-clean :
982	$(Q)$(RM) -r $(BIN)/doc
983
984VERYCLEANUP	+= $(BIN)/doc
985
986docview :
987	@[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
988	@if [ -n "$$BROWSER" ] ; then \
989		( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
990	else \
991		$(ECHO) "Documentation index in $(BIN)/doc/html/index.html" ; \
992	fi
993
994endif # defined(BIN)
995
996###############################################################################
997#
998# Clean-up
999#
1000clean :
1001	$(RM) $(CLEANUP)
1002
1003veryclean : clean
1004	$(RM) -r $(VERYCLEANUP)
1005