You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Makefile.housekeeping 39KB

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