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

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