[e1000e] Add e1000e driver
[gpxe.git] / src / Makefile.housekeeping
blob7e7ad762fbe78fcc21803e4dbc4e59e718d95c0f
1 # -*- makefile -*- : Force emacs to use Makefile mode
3 # This file contains various boring housekeeping functions that would
4 # otherwise seriously clutter up the main Makefile.
6 ###############################################################################
8 # Find a usable "echo -e" substitute.
10 TAB                     := $(shell $(PRINTF) '\t')
11 ECHO_E_ECHO             := $(ECHO)
12 ECHO_E_ECHO_E           := $(ECHO) -e
13 ECHO_E_BIN_ECHO         := /bin/echo
14 ECHO_E_BIN_ECHO_E       := /bin/echo -e
15 ECHO_E_ECHO_TAB         := $(shell $(ECHO_E_ECHO) '\t' | cat)
16 ECHO_E_ECHO_E_TAB       := $(shell $(ECHO_E_ECHO_E) '\t' | cat)
17 ECHO_E_BIN_ECHO_TAB     := $(shell $(ECHO_E_BIN_ECHO) '\t')
18 ECHO_E_BIN_ECHO_E_TAB   := $(shell $(ECHO_E_BIN_ECHO_E) '\t')
20 ifeq ($(ECHO_E_ECHO_TAB),$(TAB))
21 ECHO_E          := $(ECHO_E_ECHO)
22 endif
23 ifeq ($(ECHO_E_ECHO_E_TAB),$(TAB))
24 ECHO_E          := $(ECHO_E_ECHO_E)
25 endif
26 ifeq ($(ECHO_E_BIN_ECHO_TAB),$(TAB))
27 ECHO_E          := $(ECHO_E_BIN_ECHO)
28 endif
29 ifeq ($(ECHO_E_BIN_ECHO_E_TAB),$(TAB))
30 ECHO_E          := $(ECHO_E_BIN_ECHO_E)
31 endif
33 .echocheck :
34 ifdef ECHO_E
35         @$(TOUCH) $@
36 else
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
48 endif
49 MAKEDEPS        += .echocheck
50 VERYCLEANUP     += .echocheck
52 echo :
53         @$(ECHO) "Using \"$(ECHO_E)\" for \"echo -e\""
55 ###############################################################################
57 # Generate a usable "seq" substitute
59 define seq
60         $(shell awk 'BEGIN { for ( i = $(1) ; i <= $(2) ; i++ ) print i }')
61 endef
63 ###############################################################################
65 # Determine host OS
67 HOST_OS         := $(shell uname -s)
68 hostos :
69         @$(ECHO) $(HOST_OS)
71 ###############################################################################
73 # Determine compiler
75 CCDEFS          := $(shell $(CC) -E -x c -c /dev/null -dM | cut -d" " -f2)
76 ccdefs:
77         @$(ECHO) $(CCDEFS)
79 ifeq ($(filter __ICC,$(CCDEFS)),__ICC)
80 CCTYPE          := icc
81 else
82 CCTYPE          := gcc
83 endif
84 cctype:
85         @$(ECHO) $(CCTYPE)
87 ###############################################################################
89 # Check for tools that can cause failed builds
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) $@
104 MAKEDEPS        += .toolcheck
105 VERYCLEANUP     += .toolcheck
107 ###############################################################################
109 # Check for various tool workarounds
112 # Make syntax does not allow use of comma or space in certain places.
113 # This ugly workaround is suggested in the manual.
115 COMMA   := ,
116 EMPTY   :=
117 SPACE   := $(EMPTY) $(EMPTY)
119 # Check for an old version of gas (binutils 2.9.1)
121 OLDGAS  := $(shell $(AS) --version | grep -q '2\.9\.1' && $(ECHO) -DGAS291)
122 CFLAGS  += $(OLDGAS)
123 oldgas :
124         @$(ECHO) $(oldgas)
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.
130 ifeq ($(CCTYPE),gcc)
131 SP_TEST = $(CC) -fno-stack-protector -x c -c /dev/null \
132                 -o /dev/null >/dev/null 2>&1
133 SP_FLAGS := $(shell $(SP_TEST) && $(ECHO) '-fno-stack-protector')
134 CFLAGS  += $(SP_FLAGS)
135 endif
137 # gcc 4.4 generates .eh_frame sections by default, which distort the
138 # output of "size".  Inhibit this.
140 ifeq ($(CCTYPE),gcc)
141 CFI_TEST = $(CC) -fno-dwarf2-cfi-asm -x c -c /dev/null \
142                  -o /dev/null >/dev/null 2>&1
143 CFI_FLAGS := $(shell $(CFI_TEST) && $(ECHO) '-fno-dwarf2-cfi-asm')
144 CFLAGS  += $(CFI_FLAGS)
145 endif
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.
151 DIVIDE_TEST = $(AS) --divide /dev/null -o /dev/null 2>/dev/null
152 DIVIDE_FLAGS := $(shell $(DIVIDE_TEST) && $(ECHO) '--divide')
153 ASFLAGS += $(DIVIDE_FLAGS)
155 ###############################################################################
157 # Build verbosity
159 ifeq ($(V),1)
160 Q :=
161 QM := @\#
162 else
163 Q := @
164 QM := @
165 endif
167 ###############################################################################
169 # Set BIN according to whatever was specified on the command line as
170 # the build target.
173 # Determine how many different BIN directories are mentioned in the
174 # make goals.
176 BIN_GOALS       := $(filter bin/% bin-%,$(MAKECMDGOALS))
177 BIN_GOAL_BINS   := $(foreach BG,$(BIN_GOALS),$(firstword $(subst /, ,$(BG))))
178 NUM_BINS        := $(words $(sort $(BIN_GOAL_BINS)))
180 ifeq ($(NUM_BINS),0)
182 # No BIN directory was specified.  Set BIN to "bin" as a sensible
183 # default.
185 BIN             := bin
187 else # NUM_BINS == 0
189 ifeq ($(NUM_BINS),1)
191 # If exactly one BIN directory was specified, set BIN to match this
192 # directory.
194 BIN             := $(firstword $(BIN_GOAL_BINS))
196 else # NUM_BINS == 1
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.
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).
206 $(BIN_GOALS) : % : BIN_RECURSE
207         $(Q)$(MAKE) --no-print-directory BIN=$(firstword $(subst /, ,$@)) $@
208 .PHONY : BIN_RECURSE
210 endif # NUM_BINS == 1
211 endif # NUM_BINS == 0
213 ifdef BIN
215 # Create $(BIN) directory if it doesn't exist yet
217 ifeq ($(wildcard $(BIN)),)
218 $(shell $(MKDIR) -p $(BIN))
219 endif
221 # Target to allow e.g. "make bin-efi arch"
223 $(BIN) :
224         @# Do nothing, silently
225 .PHONY : $(BIN)
227 # Remove everything in $(BIN) for a "make clean"
229 CLEANUP += $(BIN)/*.* # Avoid picking up directories
231 endif # defined(BIN)
233 # Determine whether or not we need to include the dependency files
235 NO_DEP_TARGETS  := $(BIN) clean veryclean
236 ifeq ($(MAKECMDGOALS),)
237 NEED_DEPS       := 1
238 endif
239 ifneq ($(strip $(filter-out $(NO_DEP_TARGETS),$(MAKECMDGOALS))),)
240 NEED_DEPS       := 1
241 endif
243 ###############################################################################
245 # Select build architecture and platform based on $(BIN)
247 # BIN has the form bin[-[arch-]platform]
249 ARCHS           := $(patsubst arch/%,%,$(wildcard arch/*))
250 PLATFORMS       := $(patsubst config/defaults/%.h,%,\
251                      $(wildcard config/defaults/*.h))
252 archs :
253         @$(ECHO) $(ARCHS)
255 platforms :
256         @$(ECHO) $(PLATFORMS)
258 ifdef BIN
260 # Determine architecture portion of $(BIN), if present
261 BIN_ARCH        := $(strip $(foreach A,$(ARCHS),\
262                              $(patsubst bin-$(A)-%,$(A),\
263                                $(filter bin-$(A)-%,$(BIN)))))
265 # Determine platform portion of $(BIN), if present
266 ifeq ($(BIN_ARCH),)
267 BIN_PLATFORM    := $(patsubst bin-%,%,$(filter bin-%,$(BIN)))
268 else
269 BIN_PLATFORM    := $(patsubst bin-$(BIN_ARCH)-%,%,$(BIN))
270 endif
272 # Determine build architecture
273 DEFAULT_ARCH    := i386
274 ARCH            := $(firstword $(BIN_ARCH) $(DEFAULT_ARCH))
275 CFLAGS          += -DARCH=$(ARCH)
276 arch :
277         @$(ECHO) $(ARCH)
278 .PHONY : arch
280 # Determine build platform
281 DEFAULT_PLATFORM := pcbios
282 PLATFORM        := $(firstword $(BIN_PLATFORM) $(DEFAULT_PLATFORM))
283 CFLAGS          += -DPLATFORM=$(PLATFORM)
284 platform :
285         @$(ECHO) $(PLATFORM)
287 endif # defined(BIN)
289 # Include architecture-specific Makefile
290 ifdef ARCH
291 MAKEDEPS        += arch/$(ARCH)/Makefile
292 include arch/$(ARCH)/Makefile
293 endif
295 # Include architecture-specific include path
296 ifdef ARCH
297 INCDIRS         += arch/$(ARCH)/include
298 endif
300 ###############################################################################
302 # Source file handling
304 # SRCDIRS lists all directories containing source files.
305 srcdirs :
306         @$(ECHO) $(SRCDIRS)
308 # SRCS lists all .c or .S files found in any SRCDIR
310 SRCS    += $(wildcard $(patsubst %,%/*.c,$(SRCDIRS)))
311 SRCS    += $(wildcard $(patsubst %,%/*.S,$(SRCDIRS)))
312 srcs :
313         @$(ECHO) $(SRCS)
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.
319 AUTO_SRCS = $(filter-out $(NON_AUTO_SRCS),$(SRCS))
320 autosrcs :
321         @$(ECHO) $(AUTO_SRCS)
323 # Just about everything else in this section depends upon having
324 # $(BIN) set
326 ifdef BIN
328 # INCDIRS lists the include path
329 incdirs :
330         @$(ECHO) $(INCDIRS)
332 # Common flags
334 CFLAGS          += $(foreach INC,$(INCDIRS),-I$(INC))
335 CFLAGS          += -Os
336 CFLAGS          += -g
337 ifeq ($(CCTYPE),gcc)
338 CFLAGS          += -ffreestanding
339 CFLAGS          += -Wall -W -Wformat-nonliteral
340 endif
341 ifeq ($(CCTYPE),icc)
342 CFLAGS          += -fno-builtin
343 CFLAGS          += -no-ip
344 CFLAGS          += -no-gcc
345 CFLAGS          += -diag-disable 111 # Unreachable code
346 CFLAGS          += -diag-disable 128 # Unreachable loop
347 CFLAGS          += -diag-disable 170 # Array boundary checks
348 CFLAGS          += -diag-disable 177 # Unused functions
349 CFLAGS          += -diag-disable 181 # printf() format checks
350 CFLAGS          += -diag-disable 188 # enum strictness
351 CFLAGS          += -diag-disable 193 # Undefined preprocessor identifiers
352 CFLAGS          += -diag-disable 280 # switch ( constant )
353 CFLAGS          += -diag-disable 310 # K&R parameter lists
354 CFLAGS          += -diag-disable 424 # Extra semicolon
355 CFLAGS          += -diag-disable 589 # Declarations mid-code
356 CFLAGS          += -diag-disable 593 # Unused variables
357 CFLAGS          += -diag-disable 810 # Casting ints to smaller ints
358 CFLAGS          += -diag-disable 981 # Sequence point violations
359 CFLAGS          += -diag-disable 1292 # Ignored attributes
360 CFLAGS          += -diag-disable 1338 # void pointer arithmetic
361 CFLAGS          += -diag-disable 1361 # Variable-length arrays
362 CFLAGS          += -diag-disable 1418 # Missing prototypes
363 CFLAGS          += -diag-disable 1419 # Missing prototypes
364 CFLAGS          += -diag-disable 1599 # Hidden variables
365 CFLAGS          += -Wall -Wmissing-declarations
366 endif
367 CFLAGS          += $(EXTRA_CFLAGS)
368 ASFLAGS         += $(EXTRA_ASFLAGS)
369 LDFLAGS         += $(EXTRA_LDFLAGS)
371 # Inhibit -Werror if NO_WERROR is specified on make command line
373 ifneq ($(NO_WERROR),1)
374 CFLAGS          += -Werror
375 ASFLAGS         += --fatal-warnings
376 endif
378 # Function trace recorder state in the last build.  This is needed
379 # in order to correctly rebuild whenever the function recorder is
380 # enabled/disabled.
382 FNREC_STATE     := $(BIN)/.fnrec.state
383 ifeq ($(wildcard $(FNREC_STATE)),)
384 FNREC_STATE_OLD := <invalid>
385 else
386 FNREC_STATE_OLD := $(shell cat $(FNREC_STATE))
387 endif
388 ifeq ($(FNREC_STATE_OLD),$(FNREC))
389 $(FNREC_STATE) :
390 else
391 $(FNREC_STATE) : clean
392 $(shell $(ECHO) "$(FNREC)" > $(FNREC_STATE))
393 endif
395 VERYCLEANUP     += $(FNREC_STATE)
396 MAKEDEPS        += $(FNREC_STATE)
398 ifeq ($(FNREC),1)
399 # Enabling -finstrument-functions affects gcc's analysis and leads to spurious
400 # warnings about use of uninitialised variables.
402 CFLAGS          += -Wno-uninitialized
403 CFLAGS          += -finstrument-functions
404 CFLAGS          += -finstrument-functions-exclude-file-list=core/fnrec.c
405 endif
407 # compiler.h is needed for our linking and debugging system
409 CFLAGS          += -include compiler.h
411 # CFLAGS for specific object types
413 CFLAGS_c        +=
414 CFLAGS_S        += -DASSEMBLY
416 # Base object name of the current target
418 OBJECT          = $(firstword $(subst ., ,$(@F)))
420 # CFLAGS for specific object files.  You can define
421 # e.g. CFLAGS_rtl8139, and have those flags automatically used when
422 # compiling bin/rtl8139.o.
424 OBJ_CFLAGS      = $(CFLAGS_$(OBJECT)) -DOBJECT=$(subst -,_,$(OBJECT))
425 $(BIN)/%.flags :
426         @$(ECHO) $(OBJ_CFLAGS)
428 # ICC requires postprocessing objects to fix up table alignments
430 ifeq ($(CCTYPE),icc)
431 POST_O          = && $(ICCFIX) $@
432 POST_O_DEPS     := $(ICCFIX)
433 else
434 POST_O          :=
435 POST_O_DEPS     :=
436 endif
438 # Rules for specific object types.
440 COMPILE_c       = $(CC) $(CFLAGS) $(CFLAGS_c) $(OBJ_CFLAGS)
441 RULE_c          = $(Q)$(COMPILE_c) -c $< -o $@ $(POST_O)
442 RULE_c_to_dbg%.o = $(Q)$(COMPILE_c) -Ddebug_$(subst -,_,$(OBJECT))=$* -c $< -o $@ $(POST_O)
443 RULE_c_to_c     = $(Q)$(COMPILE_c) -E -c $< > $@
444 RULE_c_to_s     = $(Q)$(COMPILE_c) -S -g0 -c $< -o $@
446 PREPROCESS_S    = $(CPP) $(CFLAGS) $(CFLAGS_S) $(OBJ_CFLAGS)
447 ASSEMBLE_S      = $(AS) $(ASFLAGS)
448 RULE_S          = $(Q)$(PREPROCESS_S) $< | $(ASSEMBLE_S) -o $@
449 RULE_S_to_s     = $(Q)$(PREPROCESS_S) $< > $@
451 DEBUG_TARGETS   += dbg%.o c s
453 # We automatically generate rules for any file mentioned in AUTO_SRCS
454 # using the following set of templates.  It would be cleaner to use
455 # $(eval ...), but this function exists only in GNU make >= 3.80.
457 # src_template : generate Makefile rules for a given source file
459 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
460 # $(2) is the full path to the .d file (e.g. "bin/deps/drivers/net/rtl8139.d")
461 # $(3) is the source type (e.g. "c")
462 # $(4) is the source base name (e.g. "rtl8139")
464 define src_template
466         @$(ECHO) "  [DEPS] $(1)"
467         @$(MKDIR) -p $(dir $(2))
468         @$(RM) $(2)
469         @$(TOUCH) $(2)
470         @$(CPP) $(CFLAGS) $(CFLAGS_$(3)) $(CFLAGS_$(4)) -DOBJECT=$(4) \
471                 -Wno-error -MM $(1) -MG -MP | \
472                 sed 's/\.o\s*:/_DEPS =/' >> $(2)
473         @$(ECHO_E) '\n$$(BIN)/$(4).o :' \
474                  '$(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(4)_DEPS)' \
475                  '\n\t$$(QM)$(ECHO) "  [BUILD] $$@"' \
476                  '\n\t$$(RULE_$(3))\n' \
477                  '\nBOBJS += $$(BIN)/$(4).o\n' \
478                  $(foreach TGT,$(DEBUG_TARGETS), \
479                     $(if $(RULE_$(3)_to_$(TGT)), \
480                     '\n$$(BIN)/$(4).$(TGT) :' \
481                     '$(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(4)_DEPS)' \
482                     '\n\t$$(QM)$(ECHO) "  [BUILD] $$@"' \
483                     '\n\t$$(RULE_$(3)_to_$(TGT))\n' \
484                     '\n$(TGT)_OBJS += $$(BIN)/$(4).$(TGT)\n' ) ) \
485                  '\n$(2) : $$($(4)_DEPS)\n' \
486                  '\nTAGS : $$($(4)_DEPS)\n' \
487                 >> $(2)
488         @$(PARSEROM) $(1) >> $(2)
490 endef
492 # Rule to generate the Makefile rules files to be included
494 $(BIN)/deps/%.d : % $(MAKEDEPS) $(PARSEROM)
495         $(if $(filter $(AUTO_SRCS),$<),$(call src_template,$<,$@,$(subst .,,$(suffix $<)),$(basename $(notdir $<))),@$(ECHO) 'ERROR: $< is not an AUTO_SRC' ; exit 1)
497 # Calculate and include the list of Makefile rules files
499 AUTO_DEPS       = $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
500 ifdef NEED_DEPS
501 ifneq ($(AUTO_DEPS),)
502 -include $(AUTO_DEPS)
503 endif
504 endif
505 autodeps :
506         @$(ECHO) $(AUTO_DEPS)
507 VERYCLEANUP     += $(BIN)/deps
509 # The following variables are created by the Makefile rules files
511 bobjs :
512         @$(ECHO) $(BOBJS)
513 drivers :
514         @$(ECHO) $(DRIVERS)
515 .PHONY : drivers
516 roms :
517         @$(ECHO) $(ROMS)
519 # List of embedded images included in the last build of embedded.o.
520 # This is needed in order to correctly rebuild embedded.o whenever the
521 # list of objects changes.
523 EMBEDDED_LIST   := $(BIN)/.embedded.list
524 ifeq ($(wildcard $(EMBEDDED_LIST)),)
525 EMBEDDED_LIST_IMAGE := <invalid>
526 else
527 EMBEDDED_LIST_IMAGE := $(shell cat $(EMBEDDED_LIST))
528 endif
529 ifneq ($(EMBEDDED_LIST_IMAGE),$(EMBEDDED_IMAGE))
530 $(shell $(ECHO) "$(EMBEDDED_IMAGE)" > $(EMBEDDED_LIST))
531 endif
533 $(EMBEDDED_LIST) :
535 VERYCLEANUP     += $(EMBEDDED_LIST)
537 EMBEDDED_FILES  := $(subst $(COMMA), ,$(EMBEDDED_IMAGE))
538 EMBED_ALL       := $(foreach i,$(call seq,1,$(words $(EMBEDDED_FILES))),\
539                      EMBED ( $(i), \"$(word $(i), $(EMBEDDED_FILES))\",\
540                              \"$(notdir $(word $(i),$(EMBEDDED_FILES)))\" ))
542 $(BIN)/embedded.o : $(EMBEDDED_FILES) $(EMBEDDED_LIST)
544 # This file uses .incbin inline assembly to include a binary file.
545 # Unfortunately ccache does not detect this dependency and caches builds even
546 # when the binary file has changed.
548 $(BIN)/embedded.o : override CC := env CCACHE_DISABLE=1 $(CC)
550 CFLAGS_embedded = -DEMBED_ALL="$(EMBED_ALL)"
552 # Generate the NIC file from the parsed source files.  The NIC file is
553 # only for rom-o-matic.
555 $(BIN)/NIC : $(AUTO_DEPS)
556         @$(ECHO) '# This is an automatically generated file, do not edit' > $@
557         @$(ECHO) '# It does not affect anything in the build, ' \
558              'it is only for rom-o-matic' >> $@
559         @$(ECHO) >> $@
560         @perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
561 CLEANUP         += $(BIN)/NIC   # Doesn't match the $(BIN)/*.* pattern
563 # Analyse a target name (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and
564 # derive the variables:
566 # TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
567 # TGT_PREFIX   : the prefix type (e.g. "zrom")
568 # TGT_DRIVERS  : the driver for each element (e.g. "rtl8139 prism2_pci")
569 # TGT_ROM_NAME : the ROM name (e.g. "dfe538")
570 # TGT_MEDIA    : the media type (e.g. "rom")
572 DRIVERS_gpxe    = $(DRIVERS)
573 CARD_DRIVER     = $(firstword $(DRIVER_$(1)) $(1))
574 TGT_ELEMENTS    = $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
575 TGT_PREFIX      = $(word 2,$(subst ., ,$(notdir $@)))
576 TGT_ROM_NAME    = $(firstword $(TGT_ELEMENTS))
577 TGT_DRIVERS     = $(strip $(if $(DRIVERS_$(TGT_ROM_NAME)), \
578                                $(DRIVERS_$(TGT_ROM_NAME)), \
579                                $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
580                                  $(call CARD_DRIVER,$(TGT_ELEMENT))) ))
581 TGT_MEDIA       = $(subst z,,$(TGT_PREFIX))
583 # Look up ROM IDs for the current target
584 # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
586 # TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
587 # TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
589 TGT_PCI_VENDOR  = $(PCI_VENDOR_$(TGT_ROM_NAME))
590 TGT_PCI_DEVICE  = $(PCI_DEVICE_$(TGT_ROM_NAME))
592 # Calculate link-time options for the current target
593 # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
595 # TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
596 #                  (e.g. "obj_rtl8139 obj_prism2_pci")
597 # TGT_LD_IDS :     symbols to define in order to fill in ID structures in the
598 #                  ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
600 TGT_LD_DRIVERS  = $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
601 TGT_LD_PREFIX   = obj_$(TGT_PREFIX)prefix
602 TGT_LD_IDS      = pci_vendor_id=$(firstword $(TGT_PCI_VENDOR) 0) \
603                   pci_device_id=$(firstword $(TGT_PCI_DEVICE) 0)
605 # Calculate linker flags based on link-time options for the current
606 # target type (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the
607 # variables:
609 # TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
610 #                "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
611 #                 --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
613 TGT_LD_FLAGS    = $(foreach SYM,$(TGT_LD_PREFIX) $(TGT_LD_DRIVERS) obj_config,\
614                     -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
615                   $(patsubst %,--defsym %,$(TGT_LD_IDS))
617 # Calculate makerom flags for the specific target
618 # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
620 # TGT_MAKEROM_FLAGS : target-specific flags for makerom (e.g.
621 #                     "-p 0x1186,0x1300")
623 TGT_MAKEROM_FLAGS = $(strip $(MAKEROM_FLAGS_$(TGT_ROM_NAME)) \
624        $(if $(TGT_PCI_VENDOR),$(strip -p $(TGT_PCI_VENDOR),$(TGT_PCI_DEVICE))))
626 # Calculate list of debugging versions of objects to be included in
627 # the target.
629 DEBUG_LIST      = $(subst $(COMMA), ,$(DEBUG))
630 DEBUG_OBJ_LEVEL = $(firstword $(word 2,$(subst :, ,$(1))) 1)
631 DEBUG_OBJ_BASE  = $(word 1,$(subst :, ,$(1))).dbg$(call DEBUG_OBJ_LEVEL,$(1))
632 DEBUG_OBJ       = $(BIN)/$(call DEBUG_OBJ_BASE,$(1)).o
633 DEBUG_ORIG_OBJ  = $(BIN)/$(word 1,$(subst :, ,$(1))).o
634 DEBUG_OBJS      = $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
635 DEBUG_ORIG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
636 BLIB_OBJS       = $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
638 # Print out all derived information for a given target.
640 $(BIN)/%.info :
641         @$(ECHO) 'Elements             : $(TGT_ELEMENTS)'
642         @$(ECHO) 'Prefix               : $(TGT_PREFIX)'
643         @$(ECHO) 'Drivers              : $(TGT_DRIVERS)'
644         @$(ECHO) 'ROM name             : $(TGT_ROM_NAME)'
645         @$(ECHO) 'Media                : $(TGT_MEDIA)'
646         @$(ECHO)
647         @$(ECHO) 'PCI vendor           : $(TGT_PCI_VENDOR)'
648         @$(ECHO) 'PCI device           : $(TGT_PCI_DEVICE)'
649         @$(ECHO)
650         @$(ECHO) 'LD driver symbols    : $(TGT_LD_DRIVERS)'
651         @$(ECHO) 'LD prefix symbols    : $(TGT_LD_PREFIX)'
652         @$(ECHO) 'LD ID symbols        : $(TGT_LD_IDS)'
653         @$(ECHO)
654         @$(ECHO) 'LD target flags      : $(TGT_LD_FLAGS)'
655         @$(ECHO)
656         @$(ECHO) 'makerom target flags : $(TGT_MAKEROM_FLAGS)'
657         @$(ECHO)
658         @$(ECHO) 'Debugging objects    : $(DEBUG_OBJS)'
659         @$(ECHO) 'Replaced objects     : $(DEBUG_ORIG_OBJS)'
661 # List of objects included in the last build of blib.  This is needed
662 # in order to correctly rebuild blib whenever the list of objects
663 # changes.
665 BLIB_LIST       := $(BIN)/.blib.list
666 ifeq ($(wildcard $(BLIB_LIST)),)
667 BLIB_LIST_OBJS  := <invalid>
668 else
669 BLIB_LIST_OBJS  := $(shell cat $(BLIB_LIST))
670 endif
671 ifneq ($(BLIB_LIST_OBJS),$(BLIB_OBJS))
672 $(shell $(ECHO) "$(BLIB_OBJS)" > $(BLIB_LIST))
673 endif
675 $(BLIB_LIST) :
677 VERYCLEANUP     += $(BLIB_LIST)
679 # Library of all objects
681 BLIB            = $(BIN)/blib.a
682 $(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
683         $(Q)$(RM) $(BLIB)
684         $(QM)$(ECHO) "  [AR] $@"
685         $(Q)$(AR) r $@ $(BLIB_OBJS)
686         $(Q)$(RANLIB) $@
687 blib : $(BLIB)
689 # Build an intermediate object file from the objects required for the
690 # specified target.
692 $(BIN)/%.tmp : $(BLIB) $(MAKEDEPS) $(LDSCRIPT) 
693         $(QM)$(ECHO) "  [LD] $@"
694         $(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $(BLIB) -o $@ \
695                 -Map $(BIN)/$*.tmp.map
696         $(Q)$(OBJDUMP) -ht $@ | $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
698 # Keep intermediate object file (useful for debugging)
699 .PRECIOUS : $(BIN)/%.tmp
701 # Show a linker map for the specified target
703 $(BIN)/%.map : $(BIN)/%.tmp
704         @less $(BIN)/$*.tmp.map
706 # Get objects list for the specified target
708 define objs_list
709         $(sort $(foreach OBJ_SYMBOL,\
710                  $(filter obj_%,$(shell $(NM) $(1) | cut -d" " -f3)),\
711                  $(patsubst obj_%,%,$(OBJ_SYMBOL))))
712 endef
713 $(BIN)/%.objs : $(BIN)/%.tmp
714         $(Q)$(ECHO) $(call objs_list,$<)
715 $(BIN)/%.sizes : $(BIN)/%.tmp
716         $(Q)$(SIZE) -t $(foreach OBJ,$(call objs_list,$<),$(wildcard $(BIN)/$(subst _,?,$(OBJ)).o)) | \
717                 sort -g
719 # Get dependency list for the specified target
721 define deps_list
722         $(sort $(foreach OBJ,$(call objs_list,$(1)),$($(OBJ)_DEPS)))
723 endef
724 $(BIN)/%.deps : $(BIN)/%.tmp
725         $(Q)$(ECHO) $(call deps_list,$<)
727 # Get unneeded source files for the specified target
729 define nodeps_list
730         $(sort $(filter-out $(call deps_list,$(1)),\
731                  $(foreach BOBJ,$(BOBJS),\
732                    $($(basename $(notdir $(BOBJ)))_DEPS))))
733 endef
734 $(BIN)/%.nodeps : $(BIN)/%.tmp
735         $(Q)$(ECHO) $(call nodeps_list,$<)
737 # Get licensing verdict for the specified target
739 define unlicensed_deps_list
740         $(shell grep -L FILE_LICENCE $(call deps_list,$(1)))
741 endef
742 define licence_list
743         $(patsubst __licence_%,%,\
744           $(filter __licence_%,$(shell $(NM) $(1) | cut -d" " -f3)))
745 endef
746 $(BIN)/%.licence : $(BIN)/%.tmp
747         $(QM)$(ECHO) "  [LICENCE] $@"
748         $(Q)$(if $(strip $(call unlicensed_deps_list,$<)),\
749                 echo -n "Unable to determine licence because the following " ;\
750                 echo "files are missing a licence declaration:" ;\
751                 echo $(call unlicensed_deps_list,$<);\
752                 exit 1,\
753                 $(LICENCE) $(call licence_list,$<))
755 # Extract compression information from intermediate object file
757 $(BIN)/%.zinfo : $(BIN)/%.tmp
758         $(QM)$(ECHO) "  [ZINFO] $@"
759         $(Q)$(OBJCOPY) -O binary -j .zinfo $< $@
761 # Build raw binary file from intermediate object file
763 $(BIN)/%.bin : $(BIN)/%.tmp
764         $(QM)$(ECHO) "  [BIN] $@"
765         $(Q)$(OBJCOPY) -O binary -R .zinfo $< $@
767 # Compress raw binary file
769 $(BIN)/%.zbin : $(BIN)/%.bin $(BIN)/%.zinfo $(ZBIN)
770         $(QM)$(ECHO) "  [ZBIN] $@"
771         $(Q)$(ZBIN) $(BIN)/$*.bin $(BIN)/$*.zinfo > $@
773 # Rules for each media format.  These are generated and placed in an
774 # external Makefile fragment.  We could do this via $(eval ...), but
775 # that would require make >= 3.80.
777 # Note that there's an alternative way to generate most .rom images:
778 # they can be copied from their 'master' ROM image using cp and
779 # reprocessed with makerom to add the PCI IDs and ident string.  The
780 # relevant rule would look something like:
782 #   $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
783 #       cat $< $@
784 #       $(FINALISE_rom)
786 # You can derive the ROM/driver relationships using the variables
787 # DRIVER_<rom> and/or ROMS_<driver>.
789 # We don't currently do this, because (a) it would require generating
790 # yet more Makefile fragments (since you need a rule for each ROM in
791 # ROMS), and (b) the linker is so fast that it probably wouldn't make
792 # much difference to the overall build time.
794 media :
795         @$(ECHO) $(MEDIA)
797 AUTO_MEDIA      = $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
798 automedia :
799         @$(ECHO) $(AUTO_MEDIA)
801 # media_template : create Makefile rules for specified media
803 # $(1) is the media name (e.g. "rom")
804 # $(2) is the full path to the .d file (e.g. "bin/deps/rom.media.d")
806 define media_template
808         @$(ECHO) "  [MEDIADEPS] $(1)"
809         @$(MKDIR) -p $(dir $(2))
810         @$(RM) $(2)
811         @$(TOUCH) $(2)
812         @$(ECHO_E) '$$(BIN)/%.$(1) : $$(BIN)/%.$(1).zbin' \
813                   '\n\t$$(QM)$(ECHO) "  [FINISH] $$@"' \
814                   '\n\t$$(Q)$$(CP) $$< $$@' \
815                   '\n\t$$(Q)$$(PAD_$(1))' \
816                   '\n\t$$(Q)$$(FINALISE_$(1))' \
817                 > $(2)
819 endef
821 # Rule to generate the Makefile rules to be included
823 $(BIN)/deps/%.media.d : $(MAKEDEPS)
824         $(if $(filter $(AUTO_MEDIA),$*), \
825                 $(call media_template,$*,$@), \
826                 @$(ECHO) 'ERROR: $* is not an AUTO_MEDIA' ; exit 1)
828 # Calculate and include the list of Makefile rules files
830 MEDIA_DEPS              = $(patsubst %,$(BIN)/deps/%.media.d,$(AUTO_MEDIA))
831 mediadeps :
832         @$(ECHO) $(MEDIA_DEPS)
833 ifdef NEED_DEPS
834 ifneq ($(MEDIA_DEPS),)
835 -include $(MEDIA_DEPS)
836 endif
837 endif
839 # Wrap up binary blobs (for embedded images)
841 $(BIN)/%.o : payload/%.img
842         $(QM)echo "  [WRAP] $@"
843         $(Q)$(LD) -b binary -r -o $@ $< --undefined obj_payload \
844                 --defsym obj_$*=0
846 BOBJS += $(patsubst payload/%.img,$(BIN)/%.o,$(wildcard payload/*.img))
848 # The "allXXXs" targets for each suffix
850 allall: allroms allzroms allpxes allisos alldsks
851 allroms allzroms : all%s : $(foreach ROM,$(ROMS),$(BIN)/$(ROM).%)
852 allpxes allisos alldsks : all%s : $(foreach DRIVER,$(DRIVERS),$(BIN)/$(DRIVER).%)
854 # Alias for gpxe.%
856 $(BIN)/etherboot.% : $(BIN)/gpxe.%
857         ln -sf $(notdir $<) $@
859 endif # defined(BIN)
861 ###############################################################################
863 # Rules for finalising files.  TGT_MAKEROM_FLAGS is defined as part of
864 # the automatic build system and varies by target; it includes the
865 # "-p 0x1234,0x5678" string to set the PCI IDs.
867 FINALISE_rom    = $(MAKEROM) $(MAKEROM_FLAGS) $(TGT_MAKEROM_FLAGS) \
868                   -i$(IDENT) -s 0 $@
869 FINALISE_hrom   = $(FINALISE_rom)
870 FINALISE_xrom   = $(MAKEROM) $(MAKEROM_FLAGS) $(TGT_MAKEROM_FLAGS) \
871                   -i$(IDENT) -n -s 0 $@
873 # Some ROMs require specific flags to be passed to makerom.pl
875 MAKEROM_FLAGS_3c503 = -3
877 ###############################################################################
879 # The compression utilities
881 $(NRV2B) : util/nrv2b.c $(MAKEDEPS)
882         $(QM)$(ECHO) "  [HOSTCC] $@"
883         $(Q)$(HOST_CC) -O2 -DENCODE -DDECODE -DMAIN -DVERBOSE -DNDEBUG \
884                        -DBITSIZE=32 -DENDIAN=0 -o $@ $<
885 CLEANUP += $(NRV2B)
887 $(ZBIN) : util/zbin.c util/nrv2b.c $(MAKEDEPS)
888         $(QM)$(ECHO) "  [HOSTCC] $@"
889         $(Q)$(HOST_CC) -O2 -o $@ $<
890 CLEANUP += $(ZBIN)
892 ###############################################################################
894 # The EFI image converter
896 ELF2EFI_CFLAGS  := -I$(BINUTILS_DIR)/include -I$(BFD_DIR)/include \
897                    -idirafter include -L$(BINUTILS_DIR)/lib -L$(BFD_DIR)/lib \
898                    -lbfd -liberty -lz
900 $(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
901         $(QM)$(ECHO) "  [HOSTCC] $@"
902         $(Q)$(HOST_CC) $(ELF2EFI_CFLAGS) -DMDE_CPU_IA32 -O2 -o $@ $<
903 CLEANUP += $(ELF2EFI32)
905 $(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
906         $(QM)$(ECHO) "  [HOSTCC] $@"
907         $(Q)$(HOST_CC) $(ELF2EFI_CFLAGS) -DMDE_CPU_X64 -O2 -o $@ $<
908 CLEANUP += $(ELF2EFI64)
910 $(EFIROM) : util/efirom.c $(MAKEDEPS)
911         $(QM)$(ECHO) "  [HOSTCC] $@"
912         $(Q)$(HOST_CC) -idirafter include -O2 -o $@ $<
913 CLEANUP += $(EFIROM)
915 ###############################################################################
917 # The ICC fixup utility
919 $(ICCFIX) : util/iccfix.c $(MAKEDEPS)
920         $(QM)$(ECHO) "  [HOSTCC] $@"
921         $(Q)$(HOST_CC) -idirafter include -O2 -o $@ $<
922 CLEANUP += $(ICCFIX)
924 ###############################################################################
926 # Auto-incrementing build serial number.  Append "bs" to your list of
927 # build targets to get a serial number printed at the end of the
928 # build.  Enable -DBUILD_SERIAL in order to see it when the code runs.
930 BUILDSERIAL_H           = config/.buildserial.h
931 BUILDSERIAL_NOW         = config/.buildserial.now
932 BUILDSERIAL_NEXT        = config/.buildserial.next
934 $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) :
935         $(ECHO) 1 > $@
937 $(BUILDSERIAL_H) : $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT)
938         $(ECHO) '#define BUILD_SERIAL_NUM $(shell cat $<)' > $@
940 ifeq ($(filter bs,$(MAKECMDGOALS)),bs)
941 $(shell diff -q $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) > /dev/null || \
942         cp -f $(BUILDSERIAL_NEXT) $(BUILDSERIAL_NOW))
943 endif
945 bs : $(BUILDSERIAL_NOW)
946         @$(ECHO) $$(( $(shell cat $<) + 1 )) > $(BUILDSERIAL_NEXT)
947         @$(ECHO) "Build serial number is $(shell cat $<)"
949 ###############################################################################
951 # Build the TAGS file(s) for emacs
953 TAGS :
954         ctags -e -R -f $@ --exclude=bin
956 CLEANUP += TAGS
958 ###############################################################################
960 # Force rebuild for any given target
962 %.rebuild :
963         rm -f $*
964         $(Q)$(MAKE) $*
966 ###############################################################################
968 # Symbol table checks
971 ifdef BIN
973 SYMTAB  = $(BIN)/symtab
974 $(SYMTAB) : $(BLIB)
975         $(OBJDUMP) -w -t $< > $@
977 CLEANUP += $(BIN)/symtab
979 symcheck : $(SYMTAB)
980         $(SYMCHECK) $<
982 endif # defined(BIN)
984 ###############################################################################
986 # Build bochs symbol table
989 ifdef BIN
991 $(BIN)/%.bxs : $(BIN)/%.tmp
992         $(NM) $< | cut -d" " -f1,3 > $@
994 endif # defined(BIN)
996 ###############################################################################
998 # Documentation
1001 ifdef BIN
1003 $(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
1004         $(Q)$(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
1005                 -e  's{\@INCDIRS\@}{$(filter-out .,$(INCDIRS))}; ' \
1006                 -e  's{\@BIN\@}{$(BIN)}; ' \
1007                 -e  's{\@ARCH\@}{$(ARCH)}; ' \
1008                 $< > $@
1010 $(BIN)/doc : $(BIN)/doxygen.cfg
1011         $(Q)$(DOXYGEN) $<
1013 .PHONY : $(BIN)/doc
1015 doc : $(BIN)/doc
1017 doc-clean :
1018         $(Q)$(RM) -r $(BIN)/doc
1020 VERYCLEANUP     += $(BIN)/doc
1022 docview :
1023         @[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
1024         @if [ -n "$$BROWSER" ] ; then \
1025                 ( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
1026         else \
1027                 $(ECHO) "Documentation index in $(BIN)/doc/html/index.html" ; \
1028         fi
1030 endif # defined(BIN)
1032 ###############################################################################
1034 # Clean-up
1036 clean :
1037         $(RM) $(CLEANUP)
1039 veryclean : clean
1040         $(RM) -r $(VERYCLEANUP)