]> code.delx.au - refind/blob - net/discovery/Makefile.housekeeping
Add mention of code optimization to NEWS.txt
[refind] / net / discovery / Makefile.housekeeping
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 #
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')
19
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
32
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
51
52 echo :
53 @$(ECHO) "Using \"$(ECHO_E)\" for \"echo -e\""
54
55 ###############################################################################
56 #
57 # Generate a usable "seq" substitute
58 #
59 define seq
60 $(shell awk 'BEGIN { for ( i = $(1) ; i <= $(2) ; i++ ) print i }')
61 endef
62
63 ###############################################################################
64 #
65 # Determine host OS
66 #
67 HOST_OS := $(shell uname -s)
68 hostos :
69 @$(ECHO) $(HOST_OS)
70
71 ###############################################################################
72 #
73 # Determine compiler
74
75 CCDEFS := $(shell $(CC) -E -x c -c /dev/null -dM | cut -d" " -f2)
76 ccdefs:
77 @$(ECHO) $(CCDEFS)
78
79 ifeq ($(filter __ICC,$(CCDEFS)),__ICC)
80 CCTYPE := icc
81 else
82 CCTYPE := gcc
83 endif
84 cctype:
85 @$(ECHO) $(CCTYPE)
86
87 ###############################################################################
88 #
89 # Check for tools that can cause failed builds
90 #
91
92 ifeq ($(CCTYPE),gcc)
93 GCC_2_96_BANNER := $(shell $(CC) -v 2>&1 | grep -is 'gcc version 2\.96')
94 ifneq ($(GCC_2_96_BANNER),)
95 $(warning gcc 2.96 is unsuitable for compiling iPXE)
96 $(warning Use gcc 2.95 or a newer version instead)
97 $(error Unsuitable build environment found)
98 endif
99 endif
100
101 PERL_UNICODE_CHECK := $(shell $(PERL) -e 'use bytes; print chr(255)' | wc -c)
102 ifeq ($(PERL_UNICODE_CHECK),2)
103 $(warning Your Perl version has a Unicode handling bug)
104 $(warning Execute this command before building iPXE:)
105 $(warning export LANG=$${LANG%.UTF-8})
106 $(error Unsuitable build environment found)
107 endif
108
109 LD_GOLD_BANNER := $(shell $(LD) -v 2>&1 | grep 'GNU gold')
110 ifneq ($(LD_GOLD_BANNER),)
111 $(warning GNU gold is unsuitable for building iPXE)
112 $(warning Use GNU ld instead)
113 $(error Unsuitable build environment found)
114 endif
115
116 ###############################################################################
117 #
118 # Check if $(eval ...) is available to use
119 #
120
121 HAVE_EVAL :=
122 ifndef NO_EVAL
123 $(eval HAVE_EVAL := yes)
124 endif
125 eval :
126 @$(ECHO) $(HAVE_EVAL)
127
128 ###############################################################################
129 #
130 # Check for various tool workarounds
131 #
132
133 WORKAROUND_CFLAGS :=
134 WORKAROUND_ASFLAGS :=
135 WORKAROUND_LDFLAGS :=
136
137 # Make syntax does not allow use of comma or space in certain places.
138 # This ugly workaround is suggested in the manual.
139 #
140 COMMA := ,
141 EMPTY :=
142 SPACE := $(EMPTY) $(EMPTY)
143 HASH := \#
144 define NEWLINE
145
146
147 endef
148
149 # Some widespread patched versions of gcc include -fstack-protector by
150 # default, even when -ffreestanding is specified. We therefore need
151 # to disable -fstack-protector if the compiler supports it.
152 #
153 ifeq ($(CCTYPE),gcc)
154 SP_TEST = $(CC) -fno-stack-protector -x c -c /dev/null \
155 -o /dev/null >/dev/null 2>&1
156 SP_FLAGS := $(shell $(SP_TEST) && $(ECHO) '-fno-stack-protector')
157 WORKAROUND_CFLAGS += $(SP_FLAGS)
158 endif
159
160 # Some widespread patched versions of gcc include -fPIE -Wl,-pie by
161 # default. Note that gcc will exit *successfully* if it fails to
162 # recognise an option that starts with "no", so we have to test for
163 # output on stderr instead of checking the exit status.
164 #
165 ifeq ($(CCTYPE),gcc)
166 PIE_TEST = [ -z "`$(CC) -fno-PIE -nopie -x c -c /dev/null -o /dev/null 2>&1`" ]
167 PIE_FLAGS := $(shell $(PIE_TEST) && $(ECHO) '-fno-PIE -nopie')
168 WORKAROUND_CFLAGS += $(PIE_FLAGS)
169 endif
170
171 # gcc 4.4 generates .eh_frame sections by default, which distort the
172 # output of "size". Inhibit this.
173 #
174 ifeq ($(CCTYPE),gcc)
175 CFI_TEST = $(CC) -fno-dwarf2-cfi-asm -fno-exceptions -fno-unwind-tables \
176 -fno-asynchronous-unwind-tables -x c -c /dev/null \
177 -o /dev/null >/dev/null 2>&1
178 CFI_FLAGS := $(shell $(CFI_TEST) && \
179 $(ECHO) '-fno-dwarf2-cfi-asm -fno-exceptions ' \
180 '-fno-unwind-tables -fno-asynchronous-unwind-tables')
181 WORKAROUND_CFLAGS += $(CFI_FLAGS)
182 endif
183
184 # gcc 4.6 generates spurious warnings if -Waddress is in force.
185 # Inhibit this.
186 #
187 ifeq ($(CCTYPE),gcc)
188 WNA_TEST = $(CC) -Wno-address -x c -c /dev/null -o /dev/null >/dev/null 2>&1
189 WNA_FLAGS := $(shell $(WNA_TEST) && $(ECHO) '-Wno-address')
190 WORKAROUND_CFLAGS += $(WNA_FLAGS)
191 endif
192
193 # Some versions of gas choke on division operators, treating them as
194 # comment markers. Specifying --divide will work around this problem,
195 # but isn't available on older gas versions.
196 #
197 DIVIDE_TEST = $(AS) --divide /dev/null -o /dev/null 2>/dev/null
198 DIVIDE_FLAGS := $(shell $(DIVIDE_TEST) && $(ECHO) '--divide')
199 WORKAROUND_ASFLAGS += $(DIVIDE_FLAGS)
200
201 ###############################################################################
202 #
203 # Build verbosity
204 #
205 ifeq ($(V),1)
206 Q :=
207 QM := @\#
208 else
209 Q := @
210 QM := @
211 endif
212
213 ###############################################################################
214 #
215 # Checker
216 #
217 ifeq ($(C),1)
218 export REAL_CC := $(CC)
219 CC := cgcc
220 CFLAGS += -Wno-decl
221 endif
222
223 ###############################################################################
224 #
225 # Set BIN according to whatever was specified on the command line as
226 # the build target.
227 #
228
229 # Determine how many different BIN directories are mentioned in the
230 # make goals.
231 #
232 BIN_GOALS := $(filter bin bin/% bin-%,$(MAKECMDGOALS))
233 BIN_GOALS_BINS := $(sort $(foreach BG,$(BIN_GOALS),\
234 $(firstword $(subst /, ,$(BG)))))
235 NUM_BINS := $(words $(BIN_GOALS_BINS))
236
237 ifeq ($(NUM_BINS),0)
238
239 # No BIN directory was specified. Set BIN to "bin" as a sensible
240 # default.
241
242 BIN := bin
243
244 else # NUM_BINS == 0
245
246 ifeq ($(NUM_BINS),1)
247
248 # If exactly one BIN directory was specified, set BIN to match this
249 # directory.
250 #
251 BIN := $(firstword $(BIN_GOALS_BINS))
252
253 else # NUM_BINS == 1
254
255 # More than one BIN directory was specified. We cannot handle the
256 # latter case within a single make invocation, so set up recursive
257 # targets for each BIN directory. Use exactly one target for each BIN
258 # directory since running multiple make invocations within the same
259 # BIN directory is likely to cause problems.
260 #
261 # Leave $(BIN) undefined. This has implications for any target that
262 # depends on $(BIN); such targets should be made conditional upon the
263 # existence of $(BIN).
264 #
265 BIN_GOALS_FIRST := $(foreach BGB,$(BIN_GOALS_BINS),\
266 $(firstword $(filter $(BGB)/%,$(BIN_GOALS))))
267 BIN_GOALS_OTHER := $(filter-out $(BIN_GOALS_FIRST),$(BIN_GOALS))
268
269 $(BIN_GOALS_FIRST) : % : BIN_RECURSE
270 $(Q)$(MAKE) --no-print-directory BIN=$(firstword $(subst /, ,$@)) \
271 $(filter $(firstword $(subst /, ,$@))/%, $(BIN_GOALS))
272 $(BIN_GOALS_OTHER) : % : BIN_RECURSE
273 $(Q)$(TRUE)
274 .PHONY : BIN_RECURSE
275
276 endif # NUM_BINS == 1
277 endif # NUM_BINS == 0
278
279 ifdef BIN
280
281 # Create $(BIN) directory if it doesn't exist yet
282 #
283 ifeq ($(wildcard $(BIN)),)
284 $(shell $(MKDIR) -p $(BIN))
285 endif
286
287 # Target to allow e.g. "make bin-efi arch"
288 #
289 $(BIN) :
290 @# Do nothing, silently
291 .PHONY : $(BIN)
292
293 # Remove everything in $(BIN) for a "make clean"
294 #
295 CLEANUP += $(BIN)/*.* # Avoid picking up directories
296
297 endif # defined(BIN)
298
299 # Determine whether or not we need to include the dependency files
300 #
301 NO_DEP_TARGETS := $(BIN) clean veryclean
302 ifeq ($(MAKECMDGOALS),)
303 NEED_DEPS := 1
304 endif
305 ifneq ($(strip $(filter-out $(NO_DEP_TARGETS),$(MAKECMDGOALS))),)
306 NEED_DEPS := 1
307 endif
308
309 ###############################################################################
310 #
311 # Select build architecture and platform based on $(BIN)
312 #
313 # BIN has the form bin[-[arch-]platform]
314
315 ARCHS := $(patsubst arch/%,%,$(wildcard arch/*))
316 PLATFORMS := $(patsubst config/defaults/%.h,%,\
317 $(wildcard config/defaults/*.h))
318 archs :
319 @$(ECHO) $(ARCHS)
320
321 platforms :
322 @$(ECHO) $(PLATFORMS)
323
324 ifdef BIN
325
326 # Determine architecture portion of $(BIN), if present
327 BIN_ARCH := $(strip $(foreach A,$(ARCHS),\
328 $(patsubst bin-$(A)-%,$(A),\
329 $(filter bin-$(A)-%,$(BIN)))))
330
331 # Determine platform portion of $(BIN), if present
332 ifeq ($(BIN_ARCH),)
333 BIN_PLATFORM := $(patsubst bin-%,%,$(filter bin-%,$(BIN)))
334 else
335 BIN_PLATFORM := $(patsubst bin-$(BIN_ARCH)-%,%,$(BIN))
336 endif
337
338 # Determine build architecture
339 DEFAULT_ARCH := i386
340 ARCH := $(firstword $(BIN_ARCH) $(DEFAULT_ARCH))
341 CFLAGS += -DARCH=$(ARCH)
342 arch :
343 @$(ECHO) $(ARCH)
344 .PHONY : arch
345
346 # Determine build platform
347 DEFAULT_PLATFORM := pcbios
348 PLATFORM := $(firstword $(BIN_PLATFORM) $(DEFAULT_PLATFORM))
349 CFLAGS += -DPLATFORM=$(PLATFORM)
350 platform :
351 @$(ECHO) $(PLATFORM)
352
353 endif # defined(BIN)
354
355 # Include architecture-specific Makefile
356 ifdef ARCH
357 MAKEDEPS += arch/$(ARCH)/Makefile
358 include arch/$(ARCH)/Makefile
359 endif
360
361 # Include architecture-specific include path
362 ifdef ARCH
363 INCDIRS += arch/$(ARCH)/include
364 INCDIRS += arch/$(ARCH)/include/$(PLATFORM)
365 endif
366
367 ###############################################################################
368 #
369 # Source file handling
370
371 # SRCDIRS lists all directories containing source files.
372 srcdirs :
373 @$(ECHO) $(SRCDIRS)
374
375 # SRCS lists all .c or .S files found in any SRCDIR
376 #
377 SRCS += $(wildcard $(patsubst %,%/*.c,$(SRCDIRS)))
378 SRCS += $(wildcard $(patsubst %,%/*.S,$(SRCDIRS)))
379 srcs :
380 @$(ECHO) $(SRCS)
381
382 # AUTO_SRCS lists all files in SRCS that are not mentioned in
383 # NON_AUTO_SRCS. Files should be added to NON_AUTO_SRCS if they
384 # cannot be built using the standard build template.
385 #
386 AUTO_SRCS = $(filter-out $(NON_AUTO_SRCS),$(SRCS))
387 autosrcs :
388 @$(ECHO) $(AUTO_SRCS)
389
390 # Just about everything else in this section depends upon having
391 # $(BIN) set
392
393 ifdef BIN
394
395 # INCDIRS lists the include path
396 incdirs :
397 @$(ECHO) $(INCDIRS)
398
399 # Common flags
400 #
401 CFLAGS += $(foreach INC,$(INCDIRS),-I$(INC))
402 CFLAGS += -Os
403 CFLAGS += -g
404 ifeq ($(CCTYPE),gcc)
405 CFLAGS += -ffreestanding
406 CFLAGS += -Wall -W -Wformat-nonliteral
407 HOST_CFLAGS += -Wall -W -Wformat-nonliteral
408 endif
409 ifeq ($(CCTYPE),icc)
410 CFLAGS += -fno-builtin
411 CFLAGS += -no-ip
412 CFLAGS += -no-gcc
413 CFLAGS += -diag-disable 111 # Unreachable code
414 CFLAGS += -diag-disable 128 # Unreachable loop
415 CFLAGS += -diag-disable 170 # Array boundary checks
416 CFLAGS += -diag-disable 177 # Unused functions
417 CFLAGS += -diag-disable 181 # printf() format checks
418 CFLAGS += -diag-disable 188 # enum strictness
419 CFLAGS += -diag-disable 193 # Undefined preprocessor identifiers
420 CFLAGS += -diag-disable 280 # switch ( constant )
421 CFLAGS += -diag-disable 310 # K&R parameter lists
422 CFLAGS += -diag-disable 424 # Extra semicolon
423 CFLAGS += -diag-disable 589 # Declarations mid-code
424 CFLAGS += -diag-disable 593 # Unused variables
425 CFLAGS += -diag-disable 810 # Casting ints to smaller ints
426 CFLAGS += -diag-disable 981 # Sequence point violations
427 CFLAGS += -diag-disable 1292 # Ignored attributes
428 CFLAGS += -diag-disable 1338 # void pointer arithmetic
429 CFLAGS += -diag-disable 1361 # Variable-length arrays
430 CFLAGS += -diag-disable 1418 # Missing prototypes
431 CFLAGS += -diag-disable 1419 # Missing prototypes
432 CFLAGS += -diag-disable 1599 # Hidden variables
433 CFLAGS += -Wall -Wmissing-declarations
434 endif
435 CFLAGS += $(WORKAROUND_CFLAGS) $(EXTRA_CFLAGS)
436 ASFLAGS += $(WORKAROUND_ASFLAGS) $(EXTRA_ASFLAGS)
437 LDFLAGS += $(WORKAROUND_LDFLAGS) $(EXTRA_LDFLAGS)
438 HOST_CFLAGS += -O2 -g
439
440 # Inhibit -Werror if NO_WERROR is specified on make command line
441 #
442 ifneq ($(NO_WERROR),1)
443 CFLAGS += -Werror
444 ASFLAGS += --fatal-warnings
445 HOST_CFLAGS += -Werror
446 endif
447
448 # Function trace recorder state in the last build. This is needed
449 # in order to correctly rebuild whenever the function recorder is
450 # enabled/disabled.
451 #
452 FNREC_STATE := $(BIN)/.fnrec.state
453 ifeq ($(wildcard $(FNREC_STATE)),)
454 FNREC_OLD := <invalid>
455 else
456 FNREC_OLD := $(shell cat $(FNREC_STATE))
457 endif
458 ifeq ($(FNREC_OLD),$(FNREC))
459 $(FNREC_STATE) :
460 else
461 $(FNREC_STATE) : clean
462 $(shell $(ECHO) "$(FNREC)" > $(FNREC_STATE))
463 endif
464
465 VERYCLEANUP += $(FNREC_STATE)
466 MAKEDEPS += $(FNREC_STATE)
467
468 ifeq ($(FNREC),1)
469 # Enabling -finstrument-functions affects gcc's analysis and leads to spurious
470 # warnings about use of uninitialised variables.
471 #
472 CFLAGS += -Wno-uninitialized
473 CFLAGS += -finstrument-functions
474 CFLAGS += -finstrument-functions-exclude-file-list=core/fnrec.c
475 endif
476
477 # Enable per-item sections and section garbage collection. Note that
478 # some older versions of gcc support -fdata-sections but treat it as
479 # implying -fno-common, which would break our build. Some other older
480 # versions issue a spurious and uninhibitable warning if
481 # -ffunction-sections is used with -g, which would also break our
482 # build since we use -Werror.
483 #
484 ifeq ($(CCTYPE),gcc)
485 DS_TEST = $(ECHO) 'char x;' | \
486 $(CC) -fdata-sections -S -x c - -o - 2>/dev/null | \
487 grep -E '\.comm' > /dev/null
488 DS_FLAGS := $(shell $(DS_TEST) && $(ECHO) '-fdata-sections')
489 FS_TEST = $(CC) -ffunction-sections -g -c -x c /dev/null \
490 -o /dev/null 2>/dev/null
491 FS_FLAGS := $(shell $(FS_TEST) && $(ECHO) '-ffunction-sections')
492 CFLAGS += $(FS_FLAGS) $(DS_FLAGS)
493 endif
494 LDFLAGS += --gc-sections
495
496 # Force creation of static binaries (required for OpenBSD, does no
497 # harm on other platforms).
498 #
499 LDFLAGS += -static
500
501 # compiler.h is needed for our linking and debugging system
502 #
503 CFLAGS += -include include/compiler.h
504
505 # CFLAGS for specific object types
506 #
507 CFLAGS_c +=
508 CFLAGS_S += -DASSEMBLY
509
510 # Base object name of the current target
511 #
512 OBJECT = $(firstword $(subst ., ,$(@F)))
513
514 # CFLAGS for specific object files. You can define
515 # e.g. CFLAGS_rtl8139, and have those flags automatically used when
516 # compiling bin/rtl8139.o.
517 #
518 OBJ_CFLAGS = $(CFLAGS_$(OBJECT)) -DOBJECT=$(subst -,_,$(OBJECT))
519 $(BIN)/%.flags :
520 @$(ECHO) $(OBJ_CFLAGS)
521
522 # ICC requires postprocessing objects to fix up table alignments
523 #
524 ifeq ($(CCTYPE),icc)
525 POST_O = && $(ICCFIX) $@
526 POST_O_DEPS := $(ICCFIX)
527 else
528 POST_O :=
529 POST_O_DEPS :=
530 endif
531
532 # Rules for specific object types.
533 #
534 COMPILE_c = $(CC) $(CFLAGS) $(CFLAGS_c) $(OBJ_CFLAGS)
535 RULE_c = $(Q)$(COMPILE_c) -c $< -o $@ $(POST_O)
536 RULE_c_to_dbg%.o = $(Q)$(COMPILE_c) -DDBGLVL_MAX=$* -c $< -o $@ $(POST_O)
537 RULE_c_to_c = $(Q)$(COMPILE_c) -E -c $< > $@
538 RULE_c_to_s = $(Q)$(COMPILE_c) -S -g0 -c $< -o $@
539
540 PREPROCESS_S = $(CPP) $(CFLAGS) $(CFLAGS_S) $(OBJ_CFLAGS)
541 ASSEMBLE_S = $(AS) $(ASFLAGS)
542 RULE_S = $(Q)$(PREPROCESS_S) $< | $(ASSEMBLE_S) -o $@
543 RULE_S_to_dbg%.o = $(Q)$(PREPROCESS_S) -DDBGLVL_MAX=$* $< | $(ASSEMBLE_S) -o $@
544 RULE_S_to_s = $(Q)$(PREPROCESS_S) $< > $@
545
546 DEBUG_TARGETS += dbg%.o c s
547
548 # List of embedded images included in the last build of embedded.o.
549 # This is needed in order to correctly rebuild embedded.o whenever the
550 # list of objects changes.
551 #
552 EMBED := $(EMBEDDED_IMAGE) # Maintain backwards compatibility
553 EMBEDDED_LIST := $(BIN)/.embedded.list
554 ifeq ($(wildcard $(EMBEDDED_LIST)),)
555 EMBED_OLD := <invalid>
556 else
557 EMBED_OLD := $(shell cat $(EMBEDDED_LIST))
558 endif
559 ifneq ($(EMBED_OLD),$(EMBED))
560 $(shell $(ECHO) "$(EMBED)" > $(EMBEDDED_LIST))
561 endif
562
563 $(EMBEDDED_LIST) : $(MAKEDEPS)
564
565 VERYCLEANUP += $(EMBEDDED_LIST)
566
567 EMBEDDED_FILES := $(subst $(COMMA), ,$(EMBED))
568 EMBED_ALL := $(foreach i,$(call seq,1,$(words $(EMBEDDED_FILES))),\
569 EMBED ( $(i), \"$(word $(i), $(EMBEDDED_FILES))\",\
570 \"$(notdir $(word $(i),$(EMBEDDED_FILES)))\" ))
571
572 embedded_DEPS += $(EMBEDDED_FILES) $(EMBEDDED_LIST)
573
574 CFLAGS_embedded = -DEMBED_ALL="$(EMBED_ALL)"
575
576 # List of trusted root certificates
577 #
578 TRUSTED_LIST := $(BIN)/.trusted.list
579 ifeq ($(wildcard $(TRUSTED_LIST)),)
580 TRUST_OLD := <invalid>
581 else
582 TRUST_OLD := $(shell cat $(TRUSTED_LIST))
583 endif
584 ifneq ($(TRUST_OLD),$(TRUST))
585 $(shell $(ECHO) "$(TRUST)" > $(TRUSTED_LIST))
586 endif
587
588 $(TRUSTED_LIST) : $(MAKEDEPS)
589
590 VERYCLEANUP += $(TRUSTED_LIST)
591
592 # Trusted root certificate fingerprints
593 #
594 TRUSTED_CERTS := $(subst $(COMMA), ,$(TRUST))
595 TRUSTED_FPS := $(foreach CERT,$(TRUSTED_CERTS),\
596 0x$(subst :,$(COMMA) 0x,$(lastword $(subst =, ,\
597 $(shell $(OPENSSL) x509 -in $(CERT) -noout -sha256 \
598 -fingerprint))))$(COMMA))
599
600 rootcert_DEPS += $(TRUSTED_FILES) $(TRUSTED_LIST)
601
602 CFLAGS_rootcert = $(if $(TRUSTED_FPS),-DTRUSTED="$(TRUSTED_FPS)")
603
604 # List of embedded certificates
605 #
606 CERT_LIST := $(BIN)/.certificate.list
607 ifeq ($(wildcard $(CERT_LIST)),)
608 CERT_OLD := <invalid>
609 else
610 CERT_OLD := $(shell cat $(CERT_LIST))
611 endif
612 ifneq ($(CERT_OLD),$(CERT))
613 $(shell $(ECHO) "$(CERT)" > $(CERT_LIST))
614 endif
615
616 $(CERT_LIST) : $(MAKEDEPS)
617
618 VERYCLEANUP += $(CERT_LIST)
619
620 # Embedded certificates concatenated and then split into one file per
621 # certificate (even if original files contained certificate chains)
622 #
623 CERT_FILES := $(subst $(COMMA), ,$(CERT))
624 CERT_CONCAT := $(BIN)/.certificates.pem
625
626 ifneq ($(CERT),)
627
628 CERT_COUNT := $(shell grep "BEGIN CERTIFICATE" $(CERT_FILES) | wc -l)
629
630 $(CERT_CONCAT) : $(CERT_FILES) $(CERT_LIST)
631 $(Q)cat $(CERT_FILES) > $@
632
633 # We must use an (otherwise unnecessary) pattern rule here to encode
634 # the fact that one "csplit" command generates multiple targets
635 CERT_PEMS := $(foreach i,$(call seq,1,$(CERT_COUNT)),\
636 $(BIN)/.certificate.pem.$(i))
637 $(subst .pem.,.%.,$(CERT_PEMS)) : $(BIN)/.certificates.%
638 $(Q)$(CSPLIT) -q -n 1 -f $(BIN)/.certificate.pem. $< \
639 '/BEGIN CERTIFICATE/' '{*}'
640
641 CERT_DERS := $(subst .certificate.pem.,.certificate.der.,$(CERT_PEMS))
642 $(BIN)/.certificate.der.% : $(BIN)/.certificate.pem.%
643 $(Q)$(OPENSSL) x509 -in $< -outform DER -out $@
644
645 CERT_ALL := $(foreach i,$(call seq,1,$(CERT_COUNT)),\
646 CERT ( $(i), \"$(word $(i),$(CERT_DERS))\" ))
647
648 endif
649
650 certstore_DEPS += $(CERT_LIST) $(CERT_FILES) $(CERT_PEMS) $(CERT_DERS)
651
652 CFLAGS_certstore += -DCERT_ALL="$(CERT_ALL)"
653
654 CLEANUP += $(BIN)/.certificate.* $(BIN)/.certificates.*
655
656 # (Single-element) list of private keys
657 #
658 ifdef KEY
659 PRIVKEY := $(KEY) # Maintain backwards compatibility
660 endif
661 PRIVKEY_LIST := $(BIN)/.private_key.list
662 ifeq ($(wildcard $(PRIVKEY_LIST)),)
663 PRIVKEY_OLD := <invalid>
664 else
665 PRIVKEY_OLD := $(shell cat $(PRIVKEY_LIST))
666 endif
667 ifneq ($(PRIVKEY_OLD),$(PRIVKEY))
668 $(shell $(ECHO) "$(PRIVKEY)" > $(PRIVKEY_LIST))
669 endif
670
671 $(PRIVKEY_LIST) : $(MAKEDEPS)
672
673 VERYCLEANUP += $(PRIVKEY_LIST)
674
675 # Embedded private key
676 #
677 PRIVKEY_INC := $(BIN)/.private_key.der
678
679 ifdef PRIVKEY
680 $(PRIVKEY_INC) : $(PRIVKEY) $(PRIVKEY_LIST)
681 $(Q)$(OPENSSL) rsa -in $< -outform DER -out $@
682
683 privkey_DEPS += $(PRIVKEY_INC)
684 endif
685
686 CLEANUP += $(BIN)/.private_key.*
687
688 privkey_DEPS += $(PRIVKEY_LIST)
689
690 CFLAGS_privkey += $(if $(PRIVKEY),-DPRIVATE_KEY="\"$(PRIVKEY_INC)\"")
691
692 # (Single-element) list of named configurations
693 #
694 CONFIG_LIST := $(BIN)/.config.list
695 ifeq ($(wildcard $(CONFIG_LIST)),)
696 CONFIG_OLD := <invalid>
697 else
698 CONFIG_OLD := $(shell cat $(CONFIG_LIST))
699 endif
700 ifneq ($(CONFIG_OLD),$(CONFIG))
701 $(shell $(ECHO) "$(CONFIG)" > $(CONFIG_LIST))
702 endif
703
704 $(CONFIG_LIST) : $(MAKEDEPS)
705
706 VERYCLEANUP += $(CONFIG_LIST)
707
708 # Named configurations
709 #
710 ifneq ($(CONFIG),)
711 ifneq ($(wildcard config/$(CONFIG)),)
712 CFLAGS += -DCONFIG=$(CONFIG)
713 endif
714 CFLAGS += -DLOCAL_CONFIG=$(CONFIG)
715 endif
716
717 config/named.h : $(CONFIG_LIST)
718 $(Q)$(TOUCH) $@
719
720 .PRECIOUS : config/named.h
721
722 # These files use .incbin inline assembly to include a binary file.
723 # Unfortunately ccache does not detect this dependency and caches
724 # builds even when the binary file has changed.
725 #
726 $(BIN)/embedded.% : override CC := env CCACHE_DISABLE=1 $(CC)
727 $(BIN)/certstore.% : override CC := env CCACHE_DISABLE=1 $(CC)
728 $(BIN)/privkey.% : override CC := env CCACHE_DISABLE=1 $(CC)
729
730 # Debug message autocolourisation range
731 #
732 DBGCOL_LIST := $(BIN)/.dbgcol.list
733 ifeq ($(wildcard $(DBGCOL_LIST)),)
734 DBGCOL_OLD := <invalid>
735 else
736 DBGCOL_OLD := $(shell cat $(DBGCOL_LIST))
737 endif
738 ifneq ($(DBGCOL_OLD),$(DBGCOL))
739 $(shell $(ECHO) "$(DBGCOL)" > $(DBGCOL_LIST))
740 endif
741
742 $(DBGCOL_LIST) : $(MAKEDEPS)
743
744 VERYCLEANUP += $(DBGCOL_LIST)
745
746 DBGCOL_COLOURS := $(subst -, ,$(DBGCOL))
747 DBGCOL_MIN := $(word 1,$(DBGCOL_COLOURS))
748 DBGCOL_MAX := $(word 2,$(DBGCOL_COLOURS))
749
750 debug_DEPS += $(DBGCOL_LIST)
751
752 CFLAGS_debug += $(if $(DBGCOL_MIN),-DDBGCOL_MIN=$(DBGCOL_MIN))
753 CFLAGS_debug += $(if $(DBGCOL_MAX),-DDBGCOL_MAX=$(DBGCOL_MAX))
754
755 # We automatically generate rules for any file mentioned in AUTO_SRCS
756 # using the following set of templates. We use $(eval ...) if
757 # available, otherwise we generate separate Makefile fragments and
758 # include them.
759
760 # deps_template : generate dependency list for a given source file
761 #
762 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
763 #
764 define deps_template_file
765 $(call deps_template_parts,$(1),$(subst .,,$(suffix $(1))),$(basename $(notdir $(1))))
766 endef
767 #
768 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
769 # $(2) is the source type (e.g. "c")
770 # $(3) is the source base name (e.g. "rtl8139")
771 #
772 define deps_template_parts
773 @$(ECHO) " [DEPS] $(1)"
774 @$(MKDIR) -p $(BIN)/deps/$(dir $(1))
775 $(Q)$(CPP) $(CFLAGS) $(CFLAGS_$(2)) $(CFLAGS_$(3)) -DOBJECT=$(3) \
776 -Wno-error -M $(1) -MG -MP | \
777 sed 's/\.o\s*:/_DEPS +=/' > $(BIN)/deps/$(1).d
778 $(Q)$(if $(findstring drivers/,$(1)),\
779 $(PERL) $(PARSEROM) $(1) >> $(BIN)/deps/$(1).d)
780 endef
781
782 # rules_template : generate rules for a given source file
783 #
784 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
785 #
786 define rules_template
787 $(call rules_template_parts,$(1),$(subst .,,$(suffix $(1))),$(basename $(notdir $(1))))
788 endef
789 #
790 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
791 # $(2) is the source type (e.g. "c")
792 # $(3) is the source base name (e.g. "rtl8139")
793 #
794 define rules_template_parts
795 $$(BIN)/$(3).o : $(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(3)_DEPS)
796 $$(QM)$(ECHO) " [BUILD] $$@"
797 $$(RULE_$(2))
798 BOBJS += $$(BIN)/$(3).o
799 $(foreach TGT,$(DEBUG_TARGETS),$(if $(RULE_$(2)_to_$(TGT)),$(NEWLINE)$(call rules_template_target,$(1),$(2),$(3),$(TGT))))
800 $$(BIN)/deps/$(1).d : $$($(3)_DEPS)
801 TAGS : $$($(3)_DEPS)
802 endef
803 #
804 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
805 # $(2) is the source type (e.g. "c")
806 # $(3) is the source base name (e.g. "rtl8139")
807 # $(4) is the destination type (e.g. "dbg%.o")
808 #
809 define rules_template_target
810 $$(BIN)/$(3).$(4) : $(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(3)_DEPS)
811 $$(QM)$(ECHO) " [BUILD] $$@"
812 $$(RULE_$(2)_to_$(4))
813 $(TGT)_OBJS += $$(BIN)/$(3).$(4)
814 endef
815 #
816 # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
817 #
818 define rules_template_file
819 @$(ECHO) " [RULES] $(1)"
820 @$(MKDIR) -p $(BIN)/rules/$(dir $(1))
821 @$(ECHO_E) '$(subst $(NEWLINE),\n,$(call rules_template,$(1)))' \
822 > $(BIN)/rules/$(1).r
823 endef
824
825 # Generate the dependency files
826 #
827 $(BIN)/deps/%.d : % $(MAKEDEPS) $(PARSEROM)
828 $(call deps_template_file,$<)
829
830 # Calculate list of dependency files
831 #
832 AUTO_DEPS = $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
833 autodeps :
834 @$(ECHO) $(AUTO_DEPS)
835 VERYCLEANUP += $(BIN)/deps
836
837 # Include dependency files
838 #
839 ifdef NEED_DEPS
840 ifneq ($(AUTO_DEPS),)
841 -include $(AUTO_DEPS)
842 endif
843 endif
844
845 # Generate the rules files
846 #
847 $(BIN)/rules/%.r : % $(MAKEDEPS)
848 $(call rules_template_file,$<)
849
850 # Calculate list of rules files
851 #
852 AUTO_RULES = $(patsubst %,$(BIN)/rules/%.r,$(AUTO_SRCS))
853 autorules :
854 @$(ECHO) $(AUTO_RULES)
855 VERYCLEANUP += $(BIN)/rules
856
857 # Evaluate rules (or include rules files)
858 #
859 ifdef NEED_DEPS
860 ifneq ($(AUTO_RULES),)
861 ifneq ($(HAVE_EVAL),)
862 $(foreach SRC,$(AUTO_SRCS),$(eval $(call rules_template,$(SRC))))
863 else
864 -include $(AUTO_RULES)
865 endif
866 endif
867 endif
868
869 # The following variables are created by the rules files
870 #
871 bobjs :
872 @$(ECHO) $(BOBJS)
873 drivers :
874 @$(ECHO) $(DRIVERS)
875 .PHONY : drivers
876 roms :
877 @$(ECHO) $(ROMS)
878
879 # Generate error usage information
880 #
881 $(BIN)/%.einfo : $(BIN)/%.o
882 $(QM)$(ECHO) " [EINFO] $@"
883 $(Q)$(OBJCOPY) -O binary -j .einfo --set-section-flags .einfo=alloc \
884 $< $@
885
886 EINFOS := $(patsubst $(BIN)/%.o,$(BIN)/%.einfo,$(BOBJS))
887 $(BIN)/errors : $(EINFOS) $(EINFO)
888 $(QM)$(ECHO) " [EINFO] $@"
889 $(Q)$(EINFO) $(EINFOS) | sort > $@
890 CLEANUP += $(BIN)/errors # Doesn't match the $(BIN)/*.* pattern
891
892 # Generate the NIC file from the parsed source files. The NIC file is
893 # only for rom-o-matic.
894 #
895 $(BIN)/NIC : $(AUTO_DEPS)
896 @$(ECHO) '# This is an automatically generated file, do not edit' > $@
897 @$(ECHO) '# It does not affect anything in the build, ' \
898 'it is only for rom-o-matic' >> $@
899 @$(ECHO) >> $@
900 @perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
901 CLEANUP += $(BIN)/NIC # Doesn't match the $(BIN)/*.* pattern
902
903 # Analyse a target name (e.g. "bin/dfe538--prism2_pci.rom.tmp") and
904 # derive the variables:
905 #
906 # TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
907 # TGT_PREFIX : the prefix type (e.g. "pcirom")
908 # TGT_DRIVERS : the driver for each element (e.g. "rtl8139 prism2_pci")
909 # TGT_ROM_NAME : the ROM name (e.g. "dfe538")
910 #
911 DRIVERS_ipxe = $(DRIVERS)
912 CARD_DRIVER = $(firstword $(DRIVER_$(1)) $(1))
913 TGT_ELEMENTS = $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
914 TGT_ROM_NAME = $(firstword $(TGT_ELEMENTS))
915 TGT_DRIVERS = $(strip $(if $(DRIVERS_$(TGT_ROM_NAME)), \
916 $(DRIVERS_$(TGT_ROM_NAME)), \
917 $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
918 $(call CARD_DRIVER,$(TGT_ELEMENT))) ))
919 TGT_PREFIX_NAME = $(word 2,$(subst ., ,$(notdir $@)))
920 TGT_PREFIX = $(strip $(if $(filter rom,$(TGT_PREFIX_NAME)), \
921 $(ROM_TYPE_$(TGT_ROM_NAME))rom, \
922 $(TGT_PREFIX_NAME)))
923
924 # Look up ROM IDs for the current target
925 # (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the variables:
926 #
927 # TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
928 # TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
929 #
930 TGT_PCI_VENDOR = $(PCI_VENDOR_$(TGT_ROM_NAME))
931 TGT_PCI_DEVICE = $(PCI_DEVICE_$(TGT_ROM_NAME))
932
933 # Calculate link-time options for the current target
934 # (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the variables:
935 #
936 # TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
937 # (e.g. "obj_rtl8139 obj_prism2_pci")
938 # TGT_LD_IDS : symbols to define in order to fill in ID structures in the
939 # ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
940 #
941 TGT_LD_DRIVERS = $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
942 TGT_LD_IDS = pci_vendor_id=$(firstword $(TGT_PCI_VENDOR) 0) \
943 pci_device_id=$(firstword $(TGT_PCI_DEVICE) 0)
944
945 ifndef TGT_LD_ENTRY
946 TGT_LD_ENTRY = _$(TGT_PREFIX)_start
947 endif
948
949 # Calculate linker flags based on link-time options for the current
950 # target type (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the
951 # variables:
952 #
953 # TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
954 # "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
955 # --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
956 #
957 TGT_LD_FLAGS = $(foreach SYM,$(TGT_LD_ENTRY) $(TGT_LD_DRIVERS) obj_config,\
958 -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
959 $(patsubst %,--defsym %,$(TGT_LD_IDS)) \
960 -e $(TGT_LD_ENTRY)
961
962 # Calculate list of debugging versions of objects to be included in
963 # the target.
964 #
965 DEBUG_LIST = $(subst $(COMMA), ,$(DEBUG))
966 DEBUG_OBJ_LEVEL = $(firstword $(word 2,$(subst :, ,$(1))) 1)
967 DEBUG_OBJ_BASE = $(word 1,$(subst :, ,$(1))).dbg$(call DEBUG_OBJ_LEVEL,$(1))
968 DEBUG_OBJ = $(BIN)/$(call DEBUG_OBJ_BASE,$(1)).o
969 DEBUG_ORIG_OBJ = $(BIN)/$(word 1,$(subst :, ,$(1))).o
970 DEBUG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
971 DEBUG_ORIG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
972 BLIB_OBJS = $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
973
974 # Print out all derived information for a given target.
975 #
976 $(BIN)/%.info :
977 @$(ECHO) 'Elements : $(TGT_ELEMENTS)'
978 @$(ECHO) 'Prefix : $(TGT_PREFIX)'
979 @$(ECHO) 'Drivers : $(TGT_DRIVERS)'
980 @$(ECHO) 'ROM name : $(TGT_ROM_NAME)'
981 @$(ECHO)
982 @$(ECHO) 'PCI vendor : $(TGT_PCI_VENDOR)'
983 @$(ECHO) 'PCI device : $(TGT_PCI_DEVICE)'
984 @$(ECHO)
985 @$(ECHO) 'LD driver symbols : $(TGT_LD_DRIVERS)'
986 @$(ECHO) 'LD ID symbols : $(TGT_LD_IDS)'
987 @$(ECHO) 'LD entry point : $(TGT_LD_ENTRY)'
988 @$(ECHO)
989 @$(ECHO) 'LD target flags : $(TGT_LD_FLAGS)'
990 @$(ECHO)
991 @$(ECHO) 'Debugging objects : $(DEBUG_OBJS)'
992 @$(ECHO) 'Replaced objects : $(DEBUG_ORIG_OBJS)'
993
994 # List of objects included in the last build of blib. This is needed
995 # in order to correctly rebuild blib whenever the list of objects
996 # changes.
997 #
998 BLIB_LIST := $(BIN)/.blib.list
999 ifeq ($(wildcard $(BLIB_LIST)),)
1000 BLIB_OBJS_OLD := <invalid>
1001 else
1002 BLIB_OBJS_OLD := $(shell cat $(BLIB_LIST))
1003 endif
1004 ifneq ($(BLIB_OBJS_OLD),$(BLIB_OBJS))
1005 $(shell $(ECHO) "$(BLIB_OBJS)" > $(BLIB_LIST))
1006 endif
1007
1008 $(BLIB_LIST) : $(MAKEDEPS)
1009
1010 VERYCLEANUP += $(BLIB_LIST)
1011
1012 # Library of all objects
1013 #
1014 BLIB = $(BIN)/blib.a
1015 $(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
1016 $(Q)$(RM) $(BLIB)
1017 $(QM)$(ECHO) " [AR] $@"
1018 $(Q)$(AR) r $@ $(BLIB_OBJS)
1019 $(Q)$(RANLIB) $@
1020 blib : $(BLIB)
1021
1022 # Command to generate build ID. Must be unique for each $(BIN)/%.tmp,
1023 # even within the same build run.
1024 #
1025 BUILD_ID_CMD := perl -e 'printf "0x%08x", int ( rand ( 0xffffffff ) );'
1026
1027 # Build timestamp
1028 #
1029 BUILD_TIMESTAMP := $(shell date +%s)
1030
1031 # Build version
1032 #
1033 GIT_INDEX := $(if $(GITVERSION),$(if $(wildcard ../.git/index),../.git/index))
1034 $(BIN)/version.%.o : core/version.c $(MAKEDEPS) $(GIT_INDEX)
1035 $(QM)$(ECHO) " [VERSION] $@"
1036 $(Q)$(COMPILE_c) -DBUILD_NAME="\"$*\"" \
1037 -DVERSION_MAJOR=$(VERSION_MAJOR) \
1038 -DVERSION_MINOR=$(VERSION_MINOR) \
1039 -DVERSION_PATCH=$(VERSION_PATCH) \
1040 -DVERSION="\"$(VERSION)\"" \
1041 -c $< -o $@
1042
1043 # Build an intermediate object file from the objects required for the
1044 # specified target.
1045 #
1046 $(BIN)/%.tmp : $(BIN)/version.%.o $(BLIB) $(MAKEDEPS) $(LDSCRIPT)
1047 $(QM)$(ECHO) " [LD] $@"
1048 $(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $< $(BLIB) -o $@ \
1049 --defsym _build_id=`$(BUILD_ID_CMD)` \
1050 --defsym _build_timestamp=$(BUILD_TIMESTAMP) \
1051 -Map $(BIN)/$*.tmp.map
1052 $(Q)$(OBJDUMP) -ht $@ | $(PERL) $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
1053
1054 # Keep intermediate object file (useful for debugging)
1055 .PRECIOUS : $(BIN)/%.tmp
1056
1057 # Show a linker map for the specified target
1058 #
1059 $(BIN)/%.map : $(BIN)/%.tmp
1060 @less $(BIN)/$*.tmp.map
1061
1062 # Get objects list for the specified target
1063 #
1064 define objs_list
1065 $(sort $(foreach OBJ_SYMBOL,\
1066 $(filter obj_%,$(shell $(NM) $(1) | cut -d" " -f3)),\
1067 $(patsubst obj_%,%,$(OBJ_SYMBOL))))
1068 endef
1069 $(BIN)/%.objs : $(BIN)/%.tmp
1070 $(Q)$(ECHO) $(call objs_list,$<)
1071 $(BIN)/%.sizes : $(BIN)/%.tmp
1072 $(Q)$(SIZE) -t $(foreach OBJ,$(call objs_list,$<),$(wildcard $(BIN)/$(subst _,?,$(OBJ)).o)) | \
1073 sort -g
1074
1075 # Get dependency list for the specified target
1076 #
1077 define deps_list
1078 $(sort $(foreach OBJ,$(call objs_list,$(1)),$($(OBJ)_DEPS)))
1079 endef
1080 $(BIN)/%.deps : $(BIN)/%.tmp
1081 $(Q)$(ECHO) $(call deps_list,$<)
1082
1083 # Get unneeded source files for the specified target
1084 #
1085 define nodeps_list
1086 $(sort $(filter-out $(call deps_list,$(1)),\
1087 $(foreach BOBJ,$(BOBJS),\
1088 $($(basename $(notdir $(BOBJ)))_DEPS))))
1089 endef
1090 $(BIN)/%.nodeps : $(BIN)/%.tmp
1091 $(Q)$(ECHO) $(call nodeps_list,$<)
1092
1093 # Get licensing verdict for the specified target
1094 #
1095 define licensable_deps_list
1096 $(filter-out config/local/%.h,\
1097 $(filter-out $(BIN)/.%.list,\
1098 $(call deps_list,$(1))))
1099 endef
1100 define unlicensed_deps_list
1101 $(shell grep -L FILE_LICENCE $(call licensable_deps_list,$(1)))
1102 endef
1103 define licence_list
1104 $(sort $(foreach LICENCE,\
1105 $(filter __licence__%,$(shell $(NM) $(1) | cut -d" " -f3)),\
1106 $(word 2,$(subst __, ,$(LICENCE)))))
1107 endef
1108 $(BIN)/%.licence_list : $(BIN)/%.tmp
1109 $(Q)$(ECHO) $(call licence_list,$<)
1110 $(BIN)/%.licence : $(BIN)/%.tmp
1111 $(QM)$(ECHO) " [LICENCE] $@"
1112 $(Q)$(if $(strip $(call unlicensed_deps_list,$<)),\
1113 echo -n "Unable to determine licence because the following " ;\
1114 echo "files are missing a licence declaration:" ;\
1115 echo $(call unlicensed_deps_list,$<);\
1116 exit 1,\
1117 $(PERL) $(LICENCE) $(call licence_list,$<))
1118
1119 # Extract compression information from intermediate object file
1120 #
1121 $(BIN)/%.zinfo : $(BIN)/%.tmp
1122 $(QM)$(ECHO) " [ZINFO] $@"
1123 $(Q)$(OBJCOPY) -O binary -j .zinfo $< $@
1124
1125 # Build raw binary file from intermediate object file
1126 #
1127 $(BIN)/%.bin : $(BIN)/%.tmp
1128 $(QM)$(ECHO) " [BIN] $@"
1129 $(Q)$(OBJCOPY) -O binary -R .zinfo $< $@
1130
1131 # Compress raw binary file
1132 #
1133 $(BIN)/%.zbin : $(BIN)/%.bin $(BIN)/%.zinfo $(ZBIN)
1134 $(QM)$(ECHO) " [ZBIN] $@"
1135 $(Q)$(ZBIN) $(BIN)/$*.bin $(BIN)/$*.zinfo > $@
1136
1137 # Rules for each media format. These are generated and placed in an
1138 # external Makefile fragment. We could do this via $(eval ...), but
1139 # that would require make >= 3.80.
1140 #
1141 # Note that there's an alternative way to generate most .rom images:
1142 # they can be copied from their 'master' ROM image using cp and
1143 # reprocessed with makerom to add the PCI IDs and ident string. The
1144 # relevant rule would look something like:
1145 #
1146 # $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
1147 # cat $< $@
1148 # $(FINALISE_rom)
1149 #
1150 # You can derive the ROM/driver relationships using the variables
1151 # DRIVER_<rom> and/or ROMS_<driver>.
1152 #
1153 # We don't currently do this, because (a) it would require generating
1154 # yet more Makefile fragments (since you need a rule for each ROM in
1155 # ROMS), and (b) the linker is so fast that it probably wouldn't make
1156 # much difference to the overall build time.
1157
1158 # Add NON_AUTO_MEDIA to the media list, so that they show up in the
1159 # output of "make"
1160 #
1161 MEDIA += $(NON_AUTO_MEDIA)
1162
1163 media :
1164 @$(ECHO) $(MEDIA)
1165
1166 AUTO_MEDIA = $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
1167 automedia :
1168 @$(ECHO) $(AUTO_MEDIA)
1169
1170 # media_template : create media rules
1171 #
1172 # $(1) is the media name (e.g. "rom")
1173 #
1174 define media_template
1175 $(if $(filter $(1),$(AUTO_MEDIA)),$(call auto_media_template,$(1)))
1176 LIST_$(1) := $$(if $$(LIST_NAME_$(1)),$$($$(LIST_NAME_$(1))),$$(DRIVERS))
1177 ALL_$(1) = $$(foreach ITEM,$$(LIST_$(1)),$$(BIN)/$$(ITEM).$(1))
1178 $$(BIN)/all$(1)s : $$(ALL_$(1))
1179 $$(BIN)/allall : $$(BIN)/all$(1)s
1180 all$(1)s : $$(BIN)/all$(1)s
1181 allall : $$(BIN)/allall
1182 endef
1183 #
1184 # $(1) is the media name (e.g. "rom")
1185 #
1186 define auto_media_template
1187 $$(BIN)/%.$(1) : $$(BIN)/%.$(1).zbin
1188 $$(QM)echo " [FINISH] $$@"
1189 $$(Q)$$(CP) $$< $$@
1190 $$(Q)$$(if $$(PAD_$(1)),$$(PAD_$(1)) $$@)
1191 $$(Q)$$(if $$(FINALISE_$(1)),$$(FINALISE_$(1)) $$@)
1192 endef
1193 #
1194 # $(1) is the media name (e.g. "rom")
1195 #
1196 define media_template_file
1197 @$(ECHO) " [MEDIARULES] $(1)"
1198 @$(MKDIR) -p $(BIN)/rules/$(dir $(1))
1199 @$(ECHO_E) '$(subst $(NEWLINE),\n,$(call media_template,$(1)))' \
1200 > $(BIN)/rules/$(1).media.r
1201 endef
1202
1203 # Generate media rules files
1204 #
1205 $(BIN)/rules/%.media.r : $(MAKEDEPS)
1206 $(call media_template_file,$*)
1207
1208 # Calculate list of media rules files
1209 #
1210 MEDIA_RULES = $(patsubst %,$(BIN)/rules/%.media.r,$(MEDIA))
1211 mediarules :
1212 @$(ECHO) $(MEDIA_RULES)
1213
1214 # Evaluate media rules (or include media rules files)
1215 #
1216 ifdef NEED_DEPS
1217 ifneq ($(MEDIA_RULES),)
1218 ifneq ($(HAVE_EVAL),)
1219 $(foreach MEDIUM,$(MEDIA),$(eval $(call media_template,$(MEDIUM))))
1220 else
1221 -include $(MEDIA_RULES)
1222 endif
1223 endif
1224 endif
1225
1226 # Alias for ipxe.%
1227 #
1228 $(BIN)/etherboot.% : $(BIN)/ipxe.%
1229 ln -sf $(notdir $<) $@
1230
1231 endif # defined(BIN)
1232
1233 ###############################################################################
1234 #
1235 # The compression utilities
1236 #
1237 $(NRV2B) : util/nrv2b.c $(MAKEDEPS)
1238 $(QM)$(ECHO) " [HOSTCC] $@"
1239 $(Q)$(HOST_CC) $(HOST_CFLAGS) -DENCODE -DDECODE -DMAIN -DVERBOSE \
1240 -DNDEBUG -DBITSIZE=32 -DENDIAN=0 -o $@ $<
1241 CLEANUP += $(NRV2B)
1242
1243 $(ZBIN) : util/zbin.c util/nrv2b.c $(MAKEDEPS)
1244 $(QM)$(ECHO) " [HOSTCC] $@"
1245 $(Q)$(HOST_CC) $(HOST_CFLAGS) -o $@ $<
1246 CLEANUP += $(ZBIN)
1247
1248 ###############################################################################
1249 #
1250 # The EFI image converter
1251 #
1252 ELF2EFI_CFLAGS := -I$(BINUTILS_DIR)/include -I$(BFD_DIR)/include \
1253 -I$(ZLIB_DIR)/include -idirafter include
1254 ELF2EFI_LDFLAGS := -L$(BINUTILS_DIR)/lib -L$(BFD_DIR)/lib -L$(ZLIB_DIR)/lib \
1255 -lbfd -ldl -liberty -lz -Wl,--no-warn-search-mismatch
1256
1257 $(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
1258 $(QM)$(ECHO) " [HOSTCC] $@"
1259 $(Q)$(HOST_CC) $(HOST_CFLAGS) $(ELF2EFI_CFLAGS) -DEFI_TARGET_IA32 $< \
1260 $(ELF2EFI_LDFLAGS) -o $@
1261 CLEANUP += $(ELF2EFI32)
1262
1263 $(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
1264 $(QM)$(ECHO) " [HOSTCC] $@"
1265 $(Q)$(HOST_CC) $(HOST_CFLAGS) $(ELF2EFI_CFLAGS) -DEFI_TARGET_X64 $< \
1266 $(ELF2EFI_LDFLAGS) -o $@
1267 CLEANUP += $(ELF2EFI64)
1268
1269 $(EFIROM) : util/efirom.c $(MAKEDEPS)
1270 $(QM)$(ECHO) " [HOSTCC] $@"
1271 $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
1272 CLEANUP += $(EFIROM)
1273
1274 $(EFIFATBIN) : util/efifatbin.c $(MAKEDEPS)
1275 $(QM)$(ECHO) " [HOSTCC] $@"
1276 $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
1277 CLEANUP += $(EFIFATBIN)
1278
1279 ###############################################################################
1280 #
1281 # The ICC fixup utility
1282 #
1283 $(ICCFIX) : util/iccfix.c $(MAKEDEPS)
1284 $(QM)$(ECHO) " [HOSTCC] $@"
1285 $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
1286 CLEANUP += $(ICCFIX)
1287
1288 ###############################################################################
1289 #
1290 # The error usage information utility
1291 #
1292 $(EINFO) : util/einfo.c $(MAKEDEPS)
1293 $(QM)$(ECHO) " [HOSTCC] $@"
1294 $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
1295 CLEANUP += $(EINFO)
1296
1297 ###############################################################################
1298 #
1299 # Local configs
1300 #
1301 CONFIG_HEADERS := $(patsubst config/%,%,$(wildcard config/*.h))
1302 CONFIG_LOCAL_HEADERS := $(foreach HEADER,$(CONFIG_HEADERS),\
1303 config/local/$(HEADER))
1304
1305 $(CONFIG_LOCAL_HEADERS) :
1306 $(Q)$(TOUCH) $@
1307
1308 .PRECIOUS : $(CONFIG_LOCAL_HEADERS)
1309
1310 ifneq ($(CONFIG),)
1311
1312 CONFIG_LOCAL_NAMED_HEADERS := $(foreach HEADER,$(CONFIG_HEADERS),\
1313 config/local/$(CONFIG)/$(HEADER))
1314
1315 $(CONFIG_LOCAL_NAMED_HEADERS) :
1316 $(Q)$(MKDIR) -p $(dir $@)
1317 $(Q)$(TOUCH) $@
1318
1319 .PRECIOUS : $(CONFIG_LOCAL_NAMED_HEADERS)
1320
1321 endif
1322
1323 ###############################################################################
1324 #
1325 # Auto-incrementing build serial number. Append "bs" to your list of
1326 # build targets to get a serial number printed at the end of the
1327 # build. Enable -DBUILD_SERIAL in order to see it when the code runs.
1328 #
1329 BUILDSERIAL_H = config/.buildserial.h
1330 BUILDSERIAL_NOW = config/.buildserial.now
1331 BUILDSERIAL_NEXT = config/.buildserial.next
1332
1333 $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) :
1334 $(ECHO) 1 > $@
1335
1336 $(BUILDSERIAL_H) : $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT)
1337 $(ECHO) '#define BUILD_SERIAL_NUM $(shell cat $<)' > $@
1338
1339 ifeq ($(filter bs,$(MAKECMDGOALS)),bs)
1340 $(shell diff -q $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) > /dev/null || \
1341 cp -f $(BUILDSERIAL_NEXT) $(BUILDSERIAL_NOW))
1342 endif
1343
1344 bs : $(BUILDSERIAL_NOW)
1345 @$(ECHO) $$(( $(shell cat $<) + 1 )) > $(BUILDSERIAL_NEXT)
1346 @$(ECHO) "Build serial number is $(shell cat $<)"
1347
1348 ###############################################################################
1349 #
1350 # Build the TAGS file(s) for emacs
1351 #
1352 TAGS :
1353 ctags -e -R -f $@ --exclude=bin
1354
1355 CLEANUP += TAGS
1356
1357 ###############################################################################
1358 #
1359 # Force rebuild for any given target
1360 #
1361 %.rebuild :
1362 rm -f $*
1363 $(Q)$(MAKE) $*
1364
1365 ###############################################################################
1366 #
1367 # Symbol table checks
1368 #
1369
1370 ifdef BIN
1371
1372 SYMTAB = $(BIN)/symtab
1373 $(SYMTAB) : $(BLIB)
1374 $(OBJDUMP) -w -t $< > $@
1375
1376 CLEANUP += $(BIN)/symtab
1377
1378 symcheck : $(SYMTAB)
1379 $(PERL) $(SYMCHECK) $<
1380
1381 endif # defined(BIN)
1382
1383 ###############################################################################
1384 #
1385 # Build bochs symbol table
1386 #
1387
1388 ifdef BIN
1389
1390 $(BIN)/%.bxs : $(BIN)/%.tmp
1391 $(NM) $< | cut -d" " -f1,3 > $@
1392
1393 endif # defined(BIN)
1394
1395 ###############################################################################
1396 #
1397 # Documentation
1398 #
1399
1400 ifdef BIN
1401
1402 $(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
1403 $(Q)$(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
1404 -e 's{\@INCDIRS\@}{$(filter-out .,$(INCDIRS))}; ' \
1405 -e 's{\@BIN\@}{$(BIN)}; ' \
1406 -e 's{\@ARCH\@}{$(ARCH)}; ' \
1407 $< > $@
1408
1409 $(BIN)/doc : $(BIN)/doxygen.cfg
1410 $(Q)$(DOXYGEN) $<
1411
1412 .PHONY : $(BIN)/doc
1413
1414 doc : $(BIN)/doc
1415
1416 doc-clean :
1417 $(Q)$(RM) -r $(BIN)/doc
1418
1419 VERYCLEANUP += $(BIN)/doc
1420
1421 docview :
1422 @[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
1423 @if [ -n "$$BROWSER" ] ; then \
1424 ( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
1425 else \
1426 $(ECHO) "Documentation index in $(BIN)/doc/html/index.html" ; \
1427 fi
1428
1429 endif # defined(BIN)
1430
1431 ###############################################################################
1432 #
1433 # Keyboard maps
1434 #
1435
1436 hci/keymap/keymap_%.c :
1437 $(Q)$(PERL) $(GENKEYMAP) $* > $@
1438
1439 ###############################################################################
1440 #
1441 # Force deletion of incomplete targets
1442 #
1443
1444 .DELETE_ON_ERROR :
1445
1446 ###############################################################################
1447 #
1448 # Clean-up
1449 #
1450
1451 ifeq ($(NUM_BINS),0)
1452 ALLBINS := bin{,-*}
1453 CLEANUP := $(patsubst $(BIN)/%,$(ALLBINS)/%,$(CLEANUP))
1454 VERYCLEANUP := $(patsubst $(BIN)/%,$(ALLBINS)/%,$(VERYCLEANUP))
1455 endif
1456
1457 clean :
1458 $(RM) $(CLEANUP)
1459
1460 veryclean : clean
1461 $(RM) -r $(VERYCLEANUP)