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 # Objects to be removed by "make clean"
8 CLEANUP := $(BIN)/*.* # *.* to avoid catching the "CVS" directory
10 # Version number calculations
16 MM_VERSION = $(VERSION_MAJOR).$(VERSION_MINOR)
17 VERSION = $(MM_VERSION).$(VERSION_PATCH)$(EXTRAVERSION)
18 CFLAGS += -DVERSION_MAJOR=$(VERSION_MAJOR) \
19 -DVERSION_MINOR=$(VERSION_MINOR) \
20 -DVERSION=\"$(VERSION)\"
21 IDENT = '$(@F) $(VERSION) (GPL) etherboot.org'
25 # Check for tools that can cause failed builds
27 .toolcheck : Makefile Config
28 @if $(CC) -v 2>&1 | grep -is 'gcc version 2\.96' > /dev/null; then \
29 echo 'gcc 2.96 is unsuitable for compiling Etherboot'; \
30 echo 'Use gcc 2.95 or gcc 3.x instead'; \
33 @if [ `perl -e 'use bytes; print chr(255)' | wc -c` = 2 ]; then \
34 echo 'Your Perl version has a Unicode handling bug'; \
35 echo 'Execute this command before compiling Etherboot:'; \
36 echo 'export LANG=$${LANG%.UTF-8}'; \
40 VERYCLEANUP += .toolcheck
52 # Check for an old version of gas (binutils 2.9.1)
54 OLDGAS := $(shell $(AS) --version | grep -q '2\.9\.1' && echo -DGAS291)
59 # compiler.h is needed for our linking and debugging system
61 CFLAGS += -include compiler.h
63 # config/%.h files are generated from config.h using mkconfig.pl
68 # SRCDIRS lists all directories containing source files.
72 # SRCS lists all .c or .S files found in any SRCDIR
74 SRCS += $(wildcard $(patsubst %,%/*.c,$(SRCDIRS)))
75 SRCS += $(wildcard $(patsubst %,%/*.S,$(SRCDIRS)))
79 # AUTO_SRCS lists all files in SRCS that are not mentioned in
80 # NON_AUTO_SRCS. Files should be added to NON_AUTO_SRCS if they
81 # cannot be built using the standard build template.
83 AUTO_SRCS = $(filter-out $(NON_AUTO_SRCS),$(SRCS))
87 # We automatically generate rules for any file mentioned in AUTO_SRCS
88 # using the following set of templates. It would be cleaner to use
89 # $(eval ...), but this function exists only in GNU make >= 3.80.
91 # src_template : generate Makefile rules for a given source file
93 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
94 # $(2) is the full path to the .d file (e.g. "bin/deps/drivers/net/rtl8139.d")
95 # $(3) is the source type (e.g. "c")
96 # $(4) is the source base name (e.g. "rtl8139")
100 @echo "Generating Makefile rules for $(1)"
101 @$(MKDIR) -p $(dir $(2))
104 $(foreach OBJ,$(if $(OBJS_$(4)),$(OBJS_$(4)),$(4)), \
105 $(call obj_template,$(1),$(2),$(3),$(OBJ)))
106 @$(PARSEROM) $(1) >> $(2)
110 # obj_template : generate Makefile rules for a given resultant object
111 # of a particular source file. (We can have multiple objects per
112 # source file via the OBJS_xxx list.)
114 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
115 # $(2) is the full path to the .d file (e.g. "bin/deps/drivers/net/rtl8139.d")
116 # $(3) is the source type (e.g. "c")
117 # $(4) is the object name (e.g. "rtl8139")
121 @$(CPP) $(CFLAGS) $(CFLAGS_$(3)) $(CFLAGS_$(4)) \
122 -M $(1) -MT "$(4)_DEPS" -MG -MP | \
123 sed 's/_DEPS\s*:/_DEPS =/' >> $(2)
124 @echo -e '\n$$(BIN)/$(4).o : $(1) $$(MAKEDEPS) $$($(4)_DEPS)' \
125 '\n\t$$(QM)echo " [BUILD] $$@"\n' \
126 '\n\t$$(RULE_$(3))\n' \
127 '\nBOBJS += $$(BIN)/$(4).o\n' \
128 $(foreach TGT,$(DEBUG_TARGETS), \
129 $(if $(RULE_$(3)_to_$(TGT)), \
130 '\n$$(BIN)/$(4).$(TGT) : $(1) $$(MAKEDEPS) $$($(4)_DEPS)' \
131 '\n\t$$(QM)echo " [BUILD] $$@"\n' \
132 '\n\t$$(RULE_$(3)_to_$(TGT))\n' \
133 '\n$(TGT)_OBJS += $$(BIN)/$(4).$(TGT)\n' ) ) \
134 '\n$(2) : $$($(4)_DEPS)\n' \
135 '\nTAGS : $$($(4)_DEPS)\n' \
140 # Rule to generate the Makefile rules files to be included
142 $(BIN)/deps/%.d : % $(MAKEDEPS) $(PARSEROM)
143 $(if $(filter $(AUTO_SRCS),$<),$(call src_template,$<,$@,$(subst .,,$(suffix $<)),$(basename $(notdir $<))),@echo 'ERROR: $< is not an AUTO_SRC' ; exit 1)
145 # Calculate and include the list of Makefile rules files
147 AUTO_DEPS = $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
151 VERYCLEANUP += $(BIN)/deps
153 # The following variables are created by the Makefile rules files
163 # Generate the NIC file from the parsed source files. The NIC file is
164 # only for rom-o-matic.
166 $(BIN)/NIC : $(AUTO_DEPS)
167 @echo '# This is an automatically generated file, do not edit' > $@
168 @echo '# It does not affect anything in the build, ' \
169 'it is only for rom-o-matic' >> $@
171 @perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
172 CLEANUP += $(BIN)/NIC
174 # Analyse a target name (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and
175 # derive the variables:
177 # TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
178 # TGT_PREFIX : the prefix type (e.g. "zrom")
179 # TGT_DRIVERS : the driver for each element (e.g. "rtl8139 prism2_pci")
180 # TGT_ROM_NAME : the ROM name (e.g. "dfe538")
181 # TGT_MEDIA : the media type (e.g. "rom")
183 DRIVERS_etherboot = $(DRIVERS)
184 CARD_DRIVER = $(firstword $(DRIVER_$(1)) $(1))
185 TGT_ELEMENTS = $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
186 TGT_PREFIX = $(word 2,$(subst ., ,$(notdir $@)))
187 TGT_ROM_NAME = $(firstword $(TGT_ELEMENTS))
188 TGT_DRIVERS = $(strip $(if $(DRIVERS_$(TGT_ROM_NAME)), \
189 $(DRIVERS_$(TGT_ROM_NAME)), \
190 $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
191 $(call CARD_DRIVER,$(TGT_ELEMENT))) ))
192 TGT_MEDIA = $(subst z,,$(TGT_PREFIX))
194 # Look up ROM IDs for the current target
195 # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
197 # TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
198 # TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
200 TGT_PCI_VENDOR = $(PCI_VENDOR_$(TGT_ROM_NAME))
201 TGT_PCI_DEVICE = $(PCI_DEVICE_$(TGT_ROM_NAME))
203 # Calculate link-time options for the current target
204 # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
206 # TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
207 # (e.g. "obj_rtl8139 obj_prism2_pci")
208 # TGT_LD_IDS : symbols to define in order to fill in ID structures in the
209 # ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
211 TGT_LD_DRIVERS = $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
212 TGT_LD_PREFIX = obj_$(TGT_PREFIX)prefix
213 TGT_LD_IDS = $(if $(TGT_PCI_VENDOR),pci_vendor_id=$(TGT_PCI_VENDOR)) \
214 $(if $(TGT_PCI_DEVICE),pci_device_id=$(TGT_PCI_DEVICE))
216 # Calculate linker flags based on link-time options for the current
217 # target type (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the
220 # TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
221 # "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
222 # --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
224 TGT_LD_FLAGS = $(foreach SYM,$(TGT_LD_PREFIX) $(TGT_LD_DRIVERS) obj_config,\
225 -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
226 $(patsubst %,--defsym %,$(TGT_LD_IDS))
228 # Calculate makerom flags for the specific target
229 # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
231 # TGT_MAKEROM_FLAGS : target-specific flags for makerom (e.g.
232 # "-p 0x1186,0x1300")
234 TGT_MAKEROM_FLAGS = $(strip $(MAKEROM_FLAGS_$(TGT_ROM_NAME)) \
235 $(if $(TGT_PCI_VENDOR),$(strip -p $(TGT_PCI_VENDOR),$(TGT_PCI_DEVICE))))
237 # Calculate list of debugging versions of objects to be included in
241 DEBUG_LIST = $(subst $(COMMA), ,$(DEBUG))
242 DEBUG_OBJ_LEVEL = $(firstword $(word 2,$(subst :, ,$(1))) 1)
243 DEBUG_OBJ_BASE = $(word 1,$(subst :, ,$(1))).dbg$(call DEBUG_OBJ_LEVEL,$(1))
244 DEBUG_OBJ = $(BIN)/$(call DEBUG_OBJ_BASE,$(1)).o
245 DEBUG_ORIG_OBJ = $(BIN)/$(word 1,$(subst :, ,$(1))).o
246 DEBUG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
247 DEBUG_ORIG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
248 BLIB_OBJS = $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
250 # Print out all derived information for a given target.
253 @echo 'Elements : $(TGT_ELEMENTS)'
254 @echo 'Prefix : $(TGT_PREFIX)'
255 @echo 'Drivers : $(TGT_DRIVERS)'
256 @echo 'ROM name : $(TGT_ROM_NAME)'
257 @echo 'Media : $(TGT_MEDIA)'
259 @echo 'PCI vendor : $(TGT_PCI_VENDOR)'
260 @echo 'PCI device : $(TGT_PCI_DEVICE)'
262 @echo 'LD driver symbols : $(TGT_LD_DRIVERS)'
263 @echo 'LD prefix symbols : $(TGT_LD_PREFIX)'
264 @echo 'LD ID symbols : $(TGT_LD_IDS)'
266 @echo 'LD target flags : $(TGT_LD_FLAGS)'
268 @echo 'makerom target flags : $(TGT_MAKEROM_FLAGS)'
270 @echo 'Debugging objects : $(DEBUG_OBJS)'
271 @echo 'Replaced objects : $(DEBUG_ORIG_OBJS)'
273 # List of objects included in the last build of blib. This is needed
274 # in order to correctly rebuild blib whenever the list of objects
277 BLIB_LIST = $(BIN)/.blib.list
278 ifneq ($(shell cat $(BLIB_LIST)),$(BLIB_OBJS))
279 $(shell echo "$(BLIB_OBJS)" > $(BLIB_LIST))
284 VERYCLEANUP += $(BLIB_LIST)
286 # Library of all objects
289 $(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
292 $(Q)$(AR) r $@ $(BLIB_OBJS)
296 # Build an intermediate object file from the objects required for the
299 $(BIN)/%.tmp : $(BLIB) $(MAKEDEPS) $(LDSCRIPT)
301 $(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $(BLIB) -o $@ \
302 -Map $(BIN)/$*.tmp.map
303 $(Q)$(OBJDUMP) -ht $@ | $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
305 # Show a linker map for the specified target
307 $(BIN)/%.map : $(BIN)/%.tmp
308 @less $(BIN)/$*.tmp.map
310 # Build raw binary file from intermediate object file
312 $(BIN)/%.bin : $(BIN)/%.tmp
313 $(QM)echo " [BIN] $@"
314 $(Q)$(OBJCOPY) -O binary $< $@
316 # Build bochs symbol table
317 $(BIN)/%.bxs : $(BIN)/%.tmp
318 $(NM) $< | cut -d" " -f1,3 > $@
320 # Rules for each media format. These are generated and placed in an
321 # external Makefile fragment. We could do this via $(eval ...), but
322 # that would require make >= 3.80.
324 # Note that there's an alternative way to generate most .rom images:
325 # they can be copied from their 'master' ROM image using cp and
326 # reprocessed with makerom to add the PCI IDs and ident string. The
327 # relevant rule would look something like:
329 # $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
333 # You can derive the ROM/driver relationships using the variables
334 # DRIVER_<rom> and/or ROMS_<driver>.
336 # We don't currently do this, because (a) it would require generating
337 # yet more Makefile fragments (since you need a rule for each ROM in
338 # ROMS), and (b) the linker is so fast that it probably wouldn't make
339 # much difference to the overall build time.
344 AUTO_MEDIA = $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
348 # media_template : create Makefile rules for specified media
350 # $(1) is the media name (e.g. "rom")
351 # $(2) is the full path to the .d file (e.g. "bin/deps/rom.media.d")
353 define media_template
355 @echo "Generating Makefile rules for $(1) media"
356 @$(MKDIR) -p $(dir $(2))
359 @echo -e '$$(BIN)/%$(1) : $$(BIN)/%$(1).bin' \
360 '\n\t$$(QM)echo " [FINISH] $$@"' \
361 '\n\t$$(Q)$$(CP) $$< $$@' \
362 '\n\t$$(Q)$$(FINALISE_$(1))' \
367 # Rule to generate the Makefile rules to be included
369 $(BIN)/deps/%.media.d : $(MAKEDEPS)
370 $(if $(filter $(AUTO_MEDIA),$*), \
371 $(call media_template,$*,$@), \
372 @echo 'ERROR: $* is not an AUTO_MEDIA' ; exit 1)
374 # Calculate and include the list of Makefile rules files
376 MEDIA_DEPS = $(patsubst %,$(BIN)/deps/%.media.d,$(AUTO_MEDIA))
379 include $(MEDIA_DEPS)
381 # The "allXXXs" targets for each suffix
383 allroms allzroms : all%s : $(foreach ROM,$(ROMS),$(BIN)/$(ROM).%)
384 all%s : $(foreach DRIVER,$(DRIVERS),$(BIN)/$(DRIVER).%)
386 # The compressor utility
388 $(NRV2B) : util/nrv2b.c $(MAKEDEPS)
389 $(HOST_CC) -O2 -DENCODE -DDECODE -DMAIN -DVERBOSE -DNDEBUG \
390 -DBITSIZE=32 -DENDIAN=0 -o $@ $<
393 # Auto-incrementing build serial number. Append "bs" to your list of
394 # build targets to get a serial number printed at the end of the
395 # build. Enable -DBUILD_SERIAL in order to see it when the code runs.
397 BUILDSERIAL_H = config/.buildserial.h
398 BUILDSERIAL_NOW = config/.buildserial.now
399 BUILDSERIAL_NEXT = config/.buildserial.next
401 $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) :
404 $(BUILDSERIAL_H) : $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT)
405 echo '#define BUILD_SERIAL_NUM $(shell cat $<)' > $@
407 ifeq ($(filter bs,$(MAKECMDGOALS)),bs)
408 $(shell diff -q $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) > /dev/null || \
409 cp -f $(BUILDSERIAL_NEXT) $(BUILDSERIAL_NOW))
412 bs : $(BUILDSERIAL_NOW)
413 @echo $$(( $(shell cat $<) + 1 )) > $(BUILDSERIAL_NEXT)
414 @echo "Build serial number is $(shell cat $<)"
416 # List of available architectures
418 ARCHS = $(filter-out CVS,$(patsubst arch/%,%,$(wildcard arch/*)))
422 OTHER_ARCHS = $(filter-out $(ARCH),$(ARCHS))
426 # Build the TAGS file for emacs
431 ctags -e -R -f $@ --exclude=bin \
432 $(foreach ARCH,$(OTHER_ARCHS),--exclude=arch/$(ARCH))
435 # Symbol table checks
437 SYMTAB = $(BIN)/symtab
441 CLEANUP += $(BIN)/symtab
446 # Force rebuild for any given target
450 $(MAKE) $(MAKEFLAGS) $(BIN)/$*
454 $(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
455 $(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
456 -e 's{\@BIN\@}{$(BIN)}; ' \
457 -e 's{\@ARCH\@}{$(ARCH)}; ' \
460 $(BIN)/doc : $(BIN)/doxygen.cfg
465 VERYCLEANUP += $(BIN)/doc
470 @[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
471 @if [ -n "$$BROWSER" ] ; then \
472 ( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
474 echo "Documentation index in $(BIN)/doc/html/index.html" ; \
483 $(RM) -r $(VERYCLEANUP)
485 # Make clean tarballs for release
487 gpxe-tarball : ../VERSION
488 (echo -n $(VERSION) ''; date -u +'%Y-%m-%d') > ../VERSION
489 $(RM) -r /tmp/gpxe/gpxe-$(VERSION)
490 mkdir -p /tmp/gpxe/gpxe-$(VERSION)
491 cp -rP .. /tmp/gpxe/gpxe-$(VERSION)
492 $(RM) -r /tmp/gpxe/CVS
493 ( cd /tmp/gpxe/gpxe-$(VERSION)/src ; $(RM) -r bin/deps ; $(MAKE) clean ; $(MAKE) veryclean )
494 ( cd /tmp/gpxe; tar cf /tmp/gpxe/gpxe-$(VERSION).tar --exclude CVS --exclude "#*" \
495 --exclude "*~" gpxe-$(VERSION) )
496 bzip2 -9 < /tmp/gpxe/gpxe-$(VERSION).tar > /tmp/gpxe/gpxe-$(VERSION).tar.bz2
497 gzip -9 < /tmp/gpxe/gpxe-$(VERSION).tar > /tmp/gpxe/gpxe-$(VERSION).tar.gz
498 $(RM) -r /tmp/gpxe/gpxe-$(VERSION)
499 $(RM) /tmp/gpxe/gpxe-$(VERSION).tar
500 ( cd /tmp/gpxe ; tar -zxf /tmp/gpxe/gpxe-$(VERSION).tar.gz )