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