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 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. # Determine host OS
  53. #
  54. HOST_OS := $(shell uname -s)
  55. hostos :
  56. @$(ECHO) $(HOST_OS)
  57. ###############################################################################
  58. #
  59. # Check for tools that can cause failed builds
  60. #
  61. .toolcheck :
  62. @if $(CC) -v 2>&1 | grep -is 'gcc version 2\.96' > /dev/null; then \
  63. $(ECHO) 'gcc 2.96 is unsuitable for compiling Etherboot'; \
  64. $(ECHO) 'Use gcc 2.95 or gcc 3.x instead'; \
  65. exit 1; \
  66. fi
  67. @if [ `perl -e 'use bytes; print chr(255)' | wc -c` = 2 ]; then \
  68. $(ECHO) 'Your Perl version has a Unicode handling bug'; \
  69. $(ECHO) 'Execute this command before compiling Etherboot:'; \
  70. $(ECHO) 'export LANG=$${LANG%.UTF-8}'; \
  71. exit 1; \
  72. fi
  73. @$(TOUCH) $@
  74. MAKEDEPS += .toolcheck
  75. VERYCLEANUP += .toolcheck
  76. ###############################################################################
  77. #
  78. # Check for various tool workarounds
  79. #
  80. # Check for an old version of gas (binutils 2.9.1)
  81. #
  82. OLDGAS := $(shell $(AS) --version | grep -q '2\.9\.1' && $(ECHO) -DGAS291)
  83. CFLAGS += $(OLDGAS)
  84. oldgas :
  85. @$(ECHO) $(oldgas)
  86. # Some widespread patched versions of gcc include -fstack-protector by
  87. # default, even when -ffreestanding is specified. We therefore need
  88. # to disable -fstack-protector if the compiler supports it.
  89. #
  90. SP_TEST = $(CC) -fno-stack-protector -x c -c /dev/null \
  91. -o /dev/null >/dev/null 2>&1
  92. SP_FLAGS := $(shell $(SP_TEST) && $(ECHO) '-fno-stack-protector')
  93. CFLAGS += $(SP_FLAGS)
  94. ###############################################################################
  95. #
  96. # Build verbosity
  97. #
  98. ifeq ($(V),1)
  99. Q :=
  100. QM := @\#
  101. else
  102. Q := @
  103. QM := @
  104. endif
  105. ###############################################################################
  106. #
  107. # Set BIN according to whatever was specified on the command line as
  108. # the build target.
  109. #
  110. # Determine how many different BIN directories are mentioned in the
  111. # make goals.
  112. #
  113. BIN_GOALS := $(filter bin/% bin-%,$(MAKECMDGOALS))
  114. BIN_GOAL_BINS := $(foreach BG,$(BIN_GOALS),$(firstword $(subst /, ,$(BG))))
  115. NUM_BINS := $(words $(sort $(BIN_GOAL_BINS)))
  116. ifeq ($(NUM_BINS),0)
  117. # No BIN directory was specified. Set BIN to "bin" as a sensible
  118. # default.
  119. BIN := bin
  120. else # NUM_BINS == 0
  121. ifeq ($(NUM_BINS),1)
  122. # If exactly one BIN directory was specified, set BIN to match this
  123. # directory.
  124. #
  125. BIN := $(firstword $(BIN_GOAL_BINS))
  126. else # NUM_BINS == 1
  127. # More than one BIN directory was specified. We cannot handle the
  128. # latter case within a single make invocation, so set up recursive
  129. # targets for each BIN directory.
  130. #
  131. # Leave $(BIN) undefined. This has implications for any target that
  132. # depends on $(BIN); such targets should be made conditional upon the
  133. # existence of $(BIN).
  134. #
  135. $(BIN_GOALS) : % : BIN_RECURSE
  136. $(Q)$(MAKE) --no-print-directory BIN=$(firstword $(subst /, ,$@)) $@
  137. .PHONY : BIN_RECURSE
  138. endif # NUM_BINS == 1
  139. endif # NUM_BINS == 0
  140. ifdef BIN
  141. # Create $(BIN) directory if it doesn't exist yet
  142. #
  143. ifeq ($(wildcard $(BIN)),)
  144. $(shell $(MKDIR) -p $(BIN))
  145. endif
  146. # Target to allow e.g. "make bin-efi arch"
  147. #
  148. $(BIN) :
  149. @# Do nothing, silently
  150. .PHONY : $(BIN)
  151. # Remove everything in $(BIN) for a "make clean"
  152. #
  153. CLEANUP += $(BIN)/*.* # Avoid picking up directories
  154. endif # defined(BIN)
  155. # Determine whether or not we need to include the dependency files
  156. #
  157. NO_DEP_TARGETS := $(BIN) clean veryclean
  158. ifeq ($(MAKECMDGOALS),)
  159. NEED_DEPS := 1
  160. endif
  161. ifneq ($(strip $(filter-out $(NO_DEP_TARGETS),$(MAKECMDGOALS))),)
  162. NEED_DEPS := 1
  163. endif
  164. ###############################################################################
  165. #
  166. # Select build architecture and platform based on $(BIN)
  167. #
  168. # BIN has the form bin[-[arch-]platform]
  169. ARCHS := $(patsubst arch/%,%,$(wildcard arch/*))
  170. PLATFORMS := $(patsubst config/defaults/%.h,%,\
  171. $(wildcard config/defaults/*.h))
  172. archs :
  173. @$(ECHO) $(ARCHS)
  174. platforms :
  175. @$(ECHO) $(PLATFORMS)
  176. ifdef BIN
  177. # Determine architecture portion of $(BIN), if present
  178. BIN_ARCH := $(strip $(foreach A,$(ARCHS),\
  179. $(patsubst bin-$(A)-%,$(A),\
  180. $(filter bin-$(A)-%,$(BIN)))))
  181. # Determine platform portion of $(BIN), if present
  182. ifeq ($(BIN_ARCH),)
  183. BIN_PLATFORM := $(patsubst bin-%,%,$(filter bin-%,$(BIN)))
  184. else
  185. BIN_PLATFORM := $(patsubst bin-$(BIN_ARCH)-%,%,$(BIN))
  186. endif
  187. # Determine build architecture
  188. DEFAULT_ARCH := i386
  189. ARCH := $(firstword $(BIN_ARCH) $(DEFAULT_ARCH))
  190. CFLAGS += -DARCH=$(ARCH)
  191. arch :
  192. @$(ECHO) $(ARCH)
  193. .PHONY : arch
  194. # Determine build platform
  195. DEFAULT_PLATFORM := pcbios
  196. PLATFORM := $(firstword $(BIN_PLATFORM) $(DEFAULT_PLATFORM))
  197. CFLAGS += -DPLATFORM=$(PLATFORM)
  198. platform :
  199. @$(ECHO) $(PLATFORM)
  200. endif # defined(BIN)
  201. # Include architecture-specific Makefile
  202. ifdef ARCH
  203. MAKEDEPS += arch/$(ARCH)/Makefile
  204. include arch/$(ARCH)/Makefile
  205. endif
  206. ###############################################################################
  207. #
  208. # Source file handling
  209. # SRCDIRS lists all directories containing source files.
  210. srcdirs :
  211. @$(ECHO) $(SRCDIRS)
  212. # SRCS lists all .c or .S files found in any SRCDIR
  213. #
  214. SRCS += $(wildcard $(patsubst %,%/*.c,$(SRCDIRS)))
  215. SRCS += $(wildcard $(patsubst %,%/*.S,$(SRCDIRS)))
  216. srcs :
  217. @$(ECHO) $(SRCS)
  218. # AUTO_SRCS lists all files in SRCS that are not mentioned in
  219. # NON_AUTO_SRCS. Files should be added to NON_AUTO_SRCS if they
  220. # cannot be built using the standard build template.
  221. #
  222. AUTO_SRCS = $(filter-out $(NON_AUTO_SRCS),$(SRCS))
  223. autosrcs :
  224. @$(ECHO) $(AUTO_SRCS)
  225. # Just about everything else in this section depends upon having
  226. # $(BIN) set
  227. ifdef BIN
  228. # Common flags
  229. #
  230. CFLAGS += -I include -I arch/$(ARCH)/include -I .
  231. CFLAGS += -Os -ffreestanding
  232. CFLAGS += -Wall -W -Wformat-nonliteral
  233. CFLAGS += -g
  234. CFLAGS += $(EXTRA_CFLAGS)
  235. ASFLAGS += $(EXTRA_ASFLAGS)
  236. LDFLAGS += $(EXTRA_LDFLAGS)
  237. # Embedded image, if present
  238. #
  239. EMBEDDED_IMAGE = /dev/null
  240. # Inhibit -Werror if NO_WERROR is specified on make command line
  241. #
  242. ifneq ($(NO_WERROR),1)
  243. CFLAGS += -Werror
  244. endif
  245. # compiler.h is needed for our linking and debugging system
  246. #
  247. CFLAGS += -include compiler.h
  248. # CFLAGS for specific object types
  249. #
  250. CFLAGS_c +=
  251. CFLAGS_S += -DASSEMBLY
  252. # Base object name of the current target
  253. #
  254. OBJECT = $(firstword $(subst ., ,$(@F)))
  255. # CFLAGS for specific object files. You can define
  256. # e.g. CFLAGS_rtl8139, and have those flags automatically used when
  257. # compiling bin/rtl8139.o.
  258. #
  259. OBJ_CFLAGS = $(CFLAGS_$(OBJECT)) -DOBJECT=$(subst -,_,$(OBJECT))
  260. $(BIN)/%.flags :
  261. @$(ECHO) $(OBJ_CFLAGS)
  262. # Rules for specific object types.
  263. #
  264. COMPILE_c = $(CC) $(CFLAGS) $(CFLAGS_c) $(OBJ_CFLAGS)
  265. RULE_c = $(Q)$(COMPILE_c) -c $< -o $@
  266. RULE_c_to_dbg%.o = $(Q)$(COMPILE_c) -Ddebug_$(OBJECT)=$* -c $< -o $@
  267. RULE_c_to_c = $(Q)$(COMPILE_c) -E -c $< > $@
  268. RULE_c_to_s = $(Q)$(COMPILE_c) -S -g0 -c $< -o $@
  269. PREPROCESS_S = $(CPP) $(CFLAGS) $(CFLAGS_S) $(OBJ_CFLAGS)
  270. ASSEMBLE_S = $(AS) $(ASFLAGS)
  271. RULE_S = $(Q)$(PREPROCESS_S) $< | $(ASSEMBLE_S) -o $@
  272. RULE_S_to_s = $(Q)$(PREPROCESS_S) $< > $@
  273. DEBUG_TARGETS += dbg%.o c s
  274. # We automatically generate rules for any file mentioned in AUTO_SRCS
  275. # using the following set of templates. It would be cleaner to use
  276. # $(eval ...), but this function exists only in GNU make >= 3.80.
  277. # src_template : generate Makefile rules for a given source file
  278. #
  279. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  280. # $(2) is the full path to the .d file (e.g. "bin/deps/drivers/net/rtl8139.d")
  281. # $(3) is the source type (e.g. "c")
  282. # $(4) is the source base name (e.g. "rtl8139")
  283. #
  284. define src_template
  285. @$(ECHO) " [DEPS] $(1)"
  286. @$(MKDIR) -p $(dir $(2))
  287. @$(RM) $(2)
  288. @$(TOUCH) $(2)
  289. $(foreach OBJ,$(if $(OBJS_$(4)),$(OBJS_$(4)),$(4)), \
  290. $(call obj_template,$(1),$(2),$(3),$(OBJ)))
  291. @$(PARSEROM) $(1) >> $(2)
  292. endef
  293. # obj_template : generate Makefile rules for a given resultant object
  294. # of a particular source file. (We can have multiple objects per
  295. # source file via the OBJS_xxx list.)
  296. #
  297. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  298. # $(2) is the full path to the .d file (e.g. "bin/deps/drivers/net/rtl8139.d")
  299. # $(3) is the source type (e.g. "c")
  300. # $(4) is the object name (e.g. "rtl8139")
  301. #
  302. define obj_template
  303. @$(CPP) $(CFLAGS) $(CFLAGS_$(3)) $(CFLAGS_$(4)) -DOBJECT=$(4) \
  304. -Wno-error -MM $(1) -MT "$(4)_DEPS" -MG -MP | \
  305. sed 's/_DEPS\s*:/_DEPS =/' >> $(2)
  306. @$(ECHO_E) '\n$$(BIN)/$(4).o : $(1) $$(MAKEDEPS) $$($(4)_DEPS)' \
  307. '\n\t$$(QM)$(ECHO) " [BUILD] $$@"' \
  308. '\n\t$$(RULE_$(3))\n' \
  309. '\nBOBJS += $$(BIN)/$(4).o\n' \
  310. $(foreach TGT,$(DEBUG_TARGETS), \
  311. $(if $(RULE_$(3)_to_$(TGT)), \
  312. '\n$$(BIN)/$(4).$(TGT) : $(1) $$(MAKEDEPS) $$($(4)_DEPS)' \
  313. '\n\t$$(QM)$(ECHO) " [BUILD] $$@"' \
  314. '\n\t$$(RULE_$(3)_to_$(TGT))\n' \
  315. '\n$(TGT)_OBJS += $$(BIN)/$(4).$(TGT)\n' ) ) \
  316. '\n$(2) : $$($(4)_DEPS)\n' \
  317. '\nTAGS : $$($(4)_DEPS)\n' \
  318. >> $(2)
  319. endef
  320. # Rule to generate the Makefile rules files to be included
  321. #
  322. $(BIN)/deps/%.d : % $(MAKEDEPS) $(PARSEROM)
  323. $(if $(filter $(AUTO_SRCS),$<),$(call src_template,$<,$@,$(subst .,,$(suffix $<)),$(basename $(notdir $<))),@$(ECHO) 'ERROR: $< is not an AUTO_SRC' ; exit 1)
  324. # Calculate and include the list of Makefile rules files
  325. #
  326. AUTO_DEPS = $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
  327. ifdef NEED_DEPS
  328. ifneq ($(AUTO_DEPS),)
  329. -include $(AUTO_DEPS)
  330. endif
  331. endif
  332. autodeps :
  333. @$(ECHO) $(AUTO_DEPS)
  334. VERYCLEANUP += $(BIN)/deps
  335. # The following variables are created by the Makefile rules files
  336. #
  337. bobjs :
  338. @$(ECHO) $(BOBJS)
  339. drivers :
  340. @$(ECHO) $(DRIVERS)
  341. .PHONY : drivers
  342. roms :
  343. @$(ECHO) $(ROMS)
  344. # Embedded binary
  345. $(BIN)/embedimg.bin: $(EMBEDDED_IMAGE)
  346. $(QM)$(ECHO) " [COPY] $@"
  347. $(Q)$(CP) -f $(EMBEDDED_IMAGE) $@
  348. $(BIN)/embed.o: $(BIN)/embedimg.bin
  349. CFLAGS_embed = -DEMBEDIMG=\"$(BIN)/embedimg.bin\"
  350. # Generate the NIC file from the parsed source files. The NIC file is
  351. # only for rom-o-matic.
  352. #
  353. $(BIN)/NIC : $(AUTO_DEPS)
  354. @$(ECHO) '# This is an automatically generated file, do not edit' > $@
  355. @$(ECHO) '# It does not affect anything in the build, ' \
  356. 'it is only for rom-o-matic' >> $@
  357. @$(ECHO) >> $@
  358. @perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
  359. CLEANUP += $(BIN)/NIC # Doesn't match the $(BIN)/*.* pattern
  360. # Analyse a target name (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and
  361. # derive the variables:
  362. #
  363. # TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
  364. # TGT_PREFIX : the prefix type (e.g. "zrom")
  365. # TGT_DRIVERS : the driver for each element (e.g. "rtl8139 prism2_pci")
  366. # TGT_ROM_NAME : the ROM name (e.g. "dfe538")
  367. # TGT_MEDIA : the media type (e.g. "rom")
  368. #
  369. DRIVERS_gpxe = $(DRIVERS)
  370. CARD_DRIVER = $(firstword $(DRIVER_$(1)) $(1))
  371. TGT_ELEMENTS = $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
  372. TGT_PREFIX = $(word 2,$(subst ., ,$(notdir $@)))
  373. TGT_ROM_NAME = $(firstword $(TGT_ELEMENTS))
  374. TGT_DRIVERS = $(strip $(if $(DRIVERS_$(TGT_ROM_NAME)), \
  375. $(DRIVERS_$(TGT_ROM_NAME)), \
  376. $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
  377. $(call CARD_DRIVER,$(TGT_ELEMENT))) ))
  378. TGT_MEDIA = $(subst z,,$(TGT_PREFIX))
  379. # Look up ROM IDs for the current target
  380. # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
  381. #
  382. # TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
  383. # TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
  384. #
  385. TGT_PCI_VENDOR = $(PCI_VENDOR_$(TGT_ROM_NAME))
  386. TGT_PCI_DEVICE = $(PCI_DEVICE_$(TGT_ROM_NAME))
  387. # Calculate link-time options for the current target
  388. # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
  389. #
  390. # TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
  391. # (e.g. "obj_rtl8139 obj_prism2_pci")
  392. # TGT_LD_IDS : symbols to define in order to fill in ID structures in the
  393. # ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
  394. #
  395. TGT_LD_DRIVERS = $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
  396. TGT_LD_PREFIX = obj_$(TGT_PREFIX)prefix
  397. TGT_LD_IDS = pci_vendor_id=$(firstword $(TGT_PCI_VENDOR) 0) \
  398. pci_device_id=$(firstword $(TGT_PCI_DEVICE) 0)
  399. # Calculate linker flags based on link-time options for the current
  400. # target type (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the
  401. # variables:
  402. #
  403. # TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
  404. # "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
  405. # --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
  406. #
  407. TGT_LD_FLAGS = $(foreach SYM,$(TGT_LD_PREFIX) $(TGT_LD_DRIVERS) obj_config,\
  408. -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
  409. $(patsubst %,--defsym %,$(TGT_LD_IDS))
  410. # Calculate makerom flags for the specific target
  411. # (e.g. "bin/dfe538--prism2_pci.zrom.tmp") and derive the variables:
  412. #
  413. # TGT_MAKEROM_FLAGS : target-specific flags for makerom (e.g.
  414. # "-p 0x1186,0x1300")
  415. #
  416. TGT_MAKEROM_FLAGS = $(strip $(MAKEROM_FLAGS_$(TGT_ROM_NAME)) \
  417. $(if $(TGT_PCI_VENDOR),$(strip -p $(TGT_PCI_VENDOR),$(TGT_PCI_DEVICE))))
  418. # Calculate list of debugging versions of objects to be included in
  419. # the target.
  420. #
  421. COMMA := ,
  422. DEBUG_LIST = $(subst $(COMMA), ,$(DEBUG))
  423. DEBUG_OBJ_LEVEL = $(firstword $(word 2,$(subst :, ,$(1))) 1)
  424. DEBUG_OBJ_BASE = $(word 1,$(subst :, ,$(1))).dbg$(call DEBUG_OBJ_LEVEL,$(1))
  425. DEBUG_OBJ = $(BIN)/$(call DEBUG_OBJ_BASE,$(1)).o
  426. DEBUG_ORIG_OBJ = $(BIN)/$(word 1,$(subst :, ,$(1))).o
  427. DEBUG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
  428. DEBUG_ORIG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
  429. BLIB_OBJS = $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
  430. # Print out all derived information for a given target.
  431. #
  432. $(BIN)/%.info :
  433. @$(ECHO) 'Elements : $(TGT_ELEMENTS)'
  434. @$(ECHO) 'Prefix : $(TGT_PREFIX)'
  435. @$(ECHO) 'Drivers : $(TGT_DRIVERS)'
  436. @$(ECHO) 'ROM name : $(TGT_ROM_NAME)'
  437. @$(ECHO) 'Media : $(TGT_MEDIA)'
  438. @$(ECHO)
  439. @$(ECHO) 'PCI vendor : $(TGT_PCI_VENDOR)'
  440. @$(ECHO) 'PCI device : $(TGT_PCI_DEVICE)'
  441. @$(ECHO)
  442. @$(ECHO) 'LD driver symbols : $(TGT_LD_DRIVERS)'
  443. @$(ECHO) 'LD prefix symbols : $(TGT_LD_PREFIX)'
  444. @$(ECHO) 'LD ID symbols : $(TGT_LD_IDS)'
  445. @$(ECHO)
  446. @$(ECHO) 'LD target flags : $(TGT_LD_FLAGS)'
  447. @$(ECHO)
  448. @$(ECHO) 'makerom target flags : $(TGT_MAKEROM_FLAGS)'
  449. @$(ECHO)
  450. @$(ECHO) 'Debugging objects : $(DEBUG_OBJS)'
  451. @$(ECHO) 'Replaced objects : $(DEBUG_ORIG_OBJS)'
  452. # List of objects included in the last build of blib. This is needed
  453. # in order to correctly rebuild blib whenever the list of objects
  454. # changes.
  455. #
  456. BLIB_LIST := $(BIN)/.blib.list
  457. ifeq ($(wildcard $(BLIB_LIST)),)
  458. BLIB_LIST_OBJS :=
  459. else
  460. BLIB_LIST_OBJS := $(shell cat $(BLIB_LIST))
  461. endif
  462. ifneq ($(BLIB_LIST_OBJS),$(BLIB_OBJS))
  463. $(shell $(ECHO) "$(BLIB_OBJS)" > $(BLIB_LIST))
  464. endif
  465. $(BLIB_LIST) :
  466. VERYCLEANUP += $(BLIB_LIST)
  467. # Library of all objects
  468. #
  469. BLIB = $(BIN)/blib.a
  470. $(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
  471. $(Q)$(RM) $(BLIB)
  472. $(QM)$(ECHO) " [AR] $@"
  473. $(Q)$(AR) r $@ $(BLIB_OBJS)
  474. $(Q)$(RANLIB) $@
  475. blib : $(BLIB)
  476. # Build an intermediate object file from the objects required for the
  477. # specified target.
  478. #
  479. $(BIN)/%.tmp : $(BLIB) $(MAKEDEPS) $(LDSCRIPT)
  480. $(QM)$(ECHO) " [LD] $@"
  481. $(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $(BLIB) -o $@ \
  482. -Map $(BIN)/$*.tmp.map
  483. $(Q)$(OBJDUMP) -ht $@ | $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
  484. # Keep intermediate object file (useful for debugging)
  485. .PRECIOUS : $(BIN)/%.tmp
  486. # Show a linker map for the specified target
  487. #
  488. $(BIN)/%.map : $(BIN)/%.tmp
  489. @less $(BIN)/$*.tmp.map
  490. # Extract compression information from intermediate object file
  491. #
  492. $(BIN)/%.zinfo : $(BIN)/%.tmp
  493. $(QM)$(ECHO) " [ZINFO] $@"
  494. $(Q)$(OBJCOPY) -O binary -j .zinfo $< $@
  495. # Build raw binary file from intermediate object file
  496. #
  497. $(BIN)/%.bin : $(BIN)/%.tmp
  498. $(QM)$(ECHO) " [BIN] $@"
  499. $(Q)$(OBJCOPY) -O binary -R .zinfo $< $@
  500. # Compress raw binary file
  501. #
  502. $(BIN)/%.zbin : $(BIN)/%.bin $(BIN)/%.zinfo $(ZBIN)
  503. $(QM)$(ECHO) " [ZBIN] $@"
  504. $(Q)$(ZBIN) $(BIN)/$*.bin $(BIN)/$*.zinfo > $@
  505. # Rules for each media format. These are generated and placed in an
  506. # external Makefile fragment. We could do this via $(eval ...), but
  507. # that would require make >= 3.80.
  508. #
  509. # Note that there's an alternative way to generate most .rom images:
  510. # they can be copied from their 'master' ROM image using cp and
  511. # reprocessed with makerom to add the PCI IDs and ident string. The
  512. # relevant rule would look something like:
  513. #
  514. # $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
  515. # cat $< $@
  516. # $(FINALISE_rom)
  517. #
  518. # You can derive the ROM/driver relationships using the variables
  519. # DRIVER_<rom> and/or ROMS_<driver>.
  520. #
  521. # We don't currently do this, because (a) it would require generating
  522. # yet more Makefile fragments (since you need a rule for each ROM in
  523. # ROMS), and (b) the linker is so fast that it probably wouldn't make
  524. # much difference to the overall build time.
  525. media :
  526. @$(ECHO) $(MEDIA)
  527. AUTO_MEDIA = $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
  528. automedia :
  529. @$(ECHO) $(AUTO_MEDIA)
  530. # media_template : create Makefile rules for specified media
  531. #
  532. # $(1) is the media name (e.g. "rom")
  533. # $(2) is the full path to the .d file (e.g. "bin/deps/rom.media.d")
  534. #
  535. define media_template
  536. @$(ECHO) " [MEDIADEPS] $(1)"
  537. @$(MKDIR) -p $(dir $(2))
  538. @$(RM) $(2)
  539. @$(TOUCH) $(2)
  540. @$(ECHO_E) '$$(BIN)/%.$(1) : $$(BIN)/%.$(1).zbin' \
  541. '\n\t$$(QM)$(ECHO) " [FINISH] $$@"' \
  542. '\n\t$$(Q)$$(CP) $$< $$@' \
  543. '\n\t$$(Q)$$(FINALISE_$(1))' \
  544. > $(2)
  545. endef
  546. # Rule to generate the Makefile rules to be included
  547. #
  548. $(BIN)/deps/%.media.d : $(MAKEDEPS)
  549. $(if $(filter $(AUTO_MEDIA),$*), \
  550. $(call media_template,$*,$@), \
  551. @$(ECHO) 'ERROR: $* is not an AUTO_MEDIA' ; exit 1)
  552. # Calculate and include the list of Makefile rules files
  553. #
  554. MEDIA_DEPS = $(patsubst %,$(BIN)/deps/%.media.d,$(AUTO_MEDIA))
  555. mediadeps :
  556. @$(ECHO) $(MEDIA_DEPS)
  557. ifdef NEED_DEPS
  558. ifneq ($(MEDIA_DEPS),)
  559. -include $(MEDIA_DEPS)
  560. endif
  561. endif
  562. # Wrap up binary blobs (for embedded images)
  563. #
  564. $(BIN)/%.o : payload/%.img
  565. $(QM)echo " [WRAP] $@"
  566. $(Q)$(LD) -b binary -r -o $@ $< --undefined obj_payload \
  567. --defsym obj_$*=0
  568. BOBJS += $(patsubst payload/%.img,$(BIN)/%.o,$(wildcard payload/*.img))
  569. # The "allXXXs" targets for each suffix
  570. #
  571. allall: allroms allzroms allpxes allisos alldsks
  572. allroms allzroms : all%s : $(foreach ROM,$(ROMS),$(BIN)/$(ROM).%)
  573. allpxes allisos alldsks : all%s : $(foreach DRIVER,$(DRIVERS),$(BIN)/$(DRIVER).%)
  574. # Alias for gpxe.%
  575. #
  576. $(BIN)/etherboot.% : $(BIN)/gpxe.%
  577. ln -sf $(notdir $<) $@
  578. endif # defined(BIN)
  579. ###############################################################################
  580. #
  581. # Rules for finalising files. TGT_MAKEROM_FLAGS is defined as part of
  582. # the automatic build system and varies by target; it includes the
  583. # "-p 0x1234,0x5678" string to set the PCI IDs.
  584. #
  585. FINALISE_rom = $(MAKEROM) $(MAKEROM_FLAGS) $(TGT_MAKEROM_FLAGS) \
  586. -i$(IDENT) -s 0 $@
  587. # Some ROMs require specific flags to be passed to makerom.pl
  588. #
  589. MAKEROM_FLAGS_3c503 = -3
  590. ###############################################################################
  591. #
  592. # The compression utilities
  593. #
  594. $(NRV2B) : util/nrv2b.c $(MAKEDEPS)
  595. $(QM)$(ECHO) " [HOSTCC] $@"
  596. $(Q)$(HOST_CC) -O2 -DENCODE -DDECODE -DMAIN -DVERBOSE -DNDEBUG \
  597. -DBITSIZE=32 -DENDIAN=0 -o $@ $<
  598. CLEANUP += $(NRV2B)
  599. $(ZBIN) : util/zbin.c util/nrv2b.c $(MAKEDEPS)
  600. $(QM)$(ECHO) " [HOSTCC] $@"
  601. $(Q)$(HOST_CC) -O2 -o $@ $<
  602. CLEANUP += $(ZBIN)
  603. ###############################################################################
  604. #
  605. # The EFI custom linker
  606. #
  607. $(EFILINK) : util/efilink.c $(MAKEDEPS)
  608. $(QM)$(ECHO) " [HOSTCC] $@"
  609. $(Q)$(HOST_CC) -O2 -o $@ $< -lbfd -liberty
  610. CLEANUP += $(EFILINK)
  611. ###############################################################################
  612. #
  613. # Auto-incrementing build serial number. Append "bs" to your list of
  614. # build targets to get a serial number printed at the end of the
  615. # build. Enable -DBUILD_SERIAL in order to see it when the code runs.
  616. #
  617. BUILDSERIAL_H = config/.buildserial.h
  618. BUILDSERIAL_NOW = config/.buildserial.now
  619. BUILDSERIAL_NEXT = config/.buildserial.next
  620. $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) :
  621. $(ECHO) 1 > $@
  622. $(BUILDSERIAL_H) : $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT)
  623. $(ECHO) '#define BUILD_SERIAL_NUM $(shell cat $<)' > $@
  624. ifeq ($(filter bs,$(MAKECMDGOALS)),bs)
  625. $(shell diff -q $(BUILDSERIAL_NOW) $(BUILDSERIAL_NEXT) > /dev/null || \
  626. cp -f $(BUILDSERIAL_NEXT) $(BUILDSERIAL_NOW))
  627. endif
  628. bs : $(BUILDSERIAL_NOW)
  629. @$(ECHO) $$(( $(shell cat $<) + 1 )) > $(BUILDSERIAL_NEXT)
  630. @$(ECHO) "Build serial number is $(shell cat $<)"
  631. ###############################################################################
  632. #
  633. # Build the TAGS file(s) for emacs
  634. #
  635. TAGS :
  636. ctags -e -R -f $@ --exclude=bin
  637. CLEANUP += TAGS
  638. ###############################################################################
  639. #
  640. # Force rebuild for any given target
  641. #
  642. %.rebuild :
  643. rm -f $*
  644. $(Q)$(MAKE) $*
  645. ###############################################################################
  646. #
  647. # Symbol table checks
  648. #
  649. ifdef BIN
  650. SYMTAB = $(BIN)/symtab
  651. $(SYMTAB) : $(BLIB)
  652. $(OBJDUMP) -w -t $< > $@
  653. CLEANUP += $(BIN)/symtab
  654. symcheck : $(SYMTAB)
  655. $(SYMCHECK) $<
  656. endif # defined(BIN)
  657. ###############################################################################
  658. #
  659. # Build bochs symbol table
  660. #
  661. ifdef BIN
  662. $(BIN)/%.bxs : $(BIN)/%.tmp
  663. $(NM) $< | cut -d" " -f1,3 > $@
  664. endif # defined(BIN)
  665. ###############################################################################
  666. #
  667. # Documentation
  668. #
  669. ifdef BIN
  670. $(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
  671. $(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
  672. -e 's{\@BIN\@}{$(BIN)}; ' \
  673. -e 's{\@ARCH\@}{$(ARCH)}; ' \
  674. $< > $@
  675. $(BIN)/doc : $(BIN)/doxygen.cfg
  676. $(DOXYGEN) $<
  677. .PHONY : $(BIN)/doc
  678. VERYCLEANUP += $(BIN)/doc
  679. doc : $(BIN)/doc
  680. docview :
  681. @[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
  682. @if [ -n "$$BROWSER" ] ; then \
  683. ( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
  684. else \
  685. $(ECHO) "Documentation index in $(BIN)/doc/html/index.html" ; \
  686. fi
  687. endif # defined(BIN)
  688. ###############################################################################
  689. #
  690. # Clean-up
  691. #
  692. clean :
  693. $(RM) $(CLEANUP)
  694. veryclean : clean
  695. $(RM) -r $(VERYCLEANUP)