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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. # gcc 4.4 generates .eh_frame sections by default, which distort the
  139. # output of "size". Inhibit this.
  140. #
  141. ifeq ($(CCTYPE),gcc)
  142. CFI_TEST = $(CC) -fno-dwarf2-cfi-asm -fno-exceptions -fno-unwind-tables \
  143. -fno-asynchronous-unwind-tables -x c -c /dev/null \
  144. -o /dev/null >/dev/null 2>&1
  145. CFI_FLAGS := $(shell $(CFI_TEST) && \
  146. $(ECHO) '-fno-dwarf2-cfi-asm -fno-exceptions ' \
  147. '-fno-unwind-tables -fno-asynchronous-unwind-tables')
  148. WORKAROUND_CFLAGS += $(CFI_FLAGS)
  149. endif
  150. # gcc 4.6 generates spurious warnings if -Waddress is in force.
  151. # Inhibit this.
  152. #
  153. ifeq ($(CCTYPE),gcc)
  154. WNA_TEST = $(CC) -Wno-address -x c -c /dev/null -o /dev/null >/dev/null 2>&1
  155. WNA_FLAGS := $(shell $(WNA_TEST) && $(ECHO) '-Wno-address')
  156. WORKAROUND_CFLAGS += $(WNA_FLAGS)
  157. endif
  158. # Some versions of gas choke on division operators, treating them as
  159. # comment markers. Specifying --divide will work around this problem,
  160. # but isn't available on older gas versions.
  161. #
  162. DIVIDE_TEST = $(AS) --divide /dev/null -o /dev/null 2>/dev/null
  163. DIVIDE_FLAGS := $(shell $(DIVIDE_TEST) && $(ECHO) '--divide')
  164. WORKAROUND_ASFLAGS += $(DIVIDE_FLAGS)
  165. ###############################################################################
  166. #
  167. # Build verbosity
  168. #
  169. ifeq ($(V),1)
  170. Q :=
  171. QM := @\#
  172. else
  173. Q := @
  174. QM := @
  175. endif
  176. ###############################################################################
  177. #
  178. # Checker
  179. #
  180. ifeq ($(C),1)
  181. export REAL_CC := $(CC)
  182. CC := cgcc
  183. CFLAGS += -Wno-decl
  184. endif
  185. ###############################################################################
  186. #
  187. # Set BIN according to whatever was specified on the command line as
  188. # the build target.
  189. #
  190. # Determine how many different BIN directories are mentioned in the
  191. # make goals.
  192. #
  193. BIN_GOALS := $(filter bin bin/% bin-%,$(MAKECMDGOALS))
  194. BIN_GOALS_BINS := $(sort $(foreach BG,$(BIN_GOALS),\
  195. $(firstword $(subst /, ,$(BG)))))
  196. NUM_BINS := $(words $(BIN_GOALS_BINS))
  197. ifeq ($(NUM_BINS),0)
  198. # No BIN directory was specified. Set BIN to "bin" as a sensible
  199. # default.
  200. BIN := bin
  201. else # NUM_BINS == 0
  202. ifeq ($(NUM_BINS),1)
  203. # If exactly one BIN directory was specified, set BIN to match this
  204. # directory.
  205. #
  206. BIN := $(firstword $(BIN_GOALS_BINS))
  207. else # NUM_BINS == 1
  208. # More than one BIN directory was specified. We cannot handle the
  209. # latter case within a single make invocation, so set up recursive
  210. # targets for each BIN directory. Use exactly one target for each BIN
  211. # directory since running multiple make invocations within the same
  212. # BIN directory is likely to cause problems.
  213. #
  214. # Leave $(BIN) undefined. This has implications for any target that
  215. # depends on $(BIN); such targets should be made conditional upon the
  216. # existence of $(BIN).
  217. #
  218. BIN_GOALS_FIRST := $(foreach BGB,$(BIN_GOALS_BINS),\
  219. $(firstword $(filter $(BGB)/%,$(BIN_GOALS))))
  220. BIN_GOALS_OTHER := $(filter-out $(BIN_GOALS_FIRST),$(BIN_GOALS))
  221. $(BIN_GOALS_FIRST) : % : BIN_RECURSE
  222. $(Q)$(MAKE) --no-print-directory BIN=$(firstword $(subst /, ,$@)) \
  223. $(filter $(firstword $(subst /, ,$@))/%, $(BIN_GOALS))
  224. $(BIN_GOALS_OTHER) : % : BIN_RECURSE
  225. $(Q)$(TRUE)
  226. .PHONY : BIN_RECURSE
  227. endif # NUM_BINS == 1
  228. endif # NUM_BINS == 0
  229. ifdef BIN
  230. # Create $(BIN) directory if it doesn't exist yet
  231. #
  232. ifeq ($(wildcard $(BIN)),)
  233. $(shell $(MKDIR) -p $(BIN))
  234. endif
  235. # Target to allow e.g. "make bin-efi arch"
  236. #
  237. $(BIN) :
  238. @# Do nothing, silently
  239. .PHONY : $(BIN)
  240. # Remove everything in $(BIN) for a "make clean"
  241. #
  242. CLEANUP += $(BIN)/*.* # Avoid picking up directories
  243. endif # defined(BIN)
  244. # Determine whether or not we need to include the dependency files
  245. #
  246. NO_DEP_TARGETS := $(BIN) clean veryclean
  247. ifeq ($(MAKECMDGOALS),)
  248. NEED_DEPS := 1
  249. endif
  250. ifneq ($(strip $(filter-out $(NO_DEP_TARGETS),$(MAKECMDGOALS))),)
  251. NEED_DEPS := 1
  252. endif
  253. ###############################################################################
  254. #
  255. # Select build architecture and platform based on $(BIN)
  256. #
  257. # BIN has the form bin[-[arch-]platform]
  258. ARCHS := $(patsubst arch/%,%,$(wildcard arch/*))
  259. PLATFORMS := $(patsubst config/defaults/%.h,%,\
  260. $(wildcard config/defaults/*.h))
  261. archs :
  262. @$(ECHO) $(ARCHS)
  263. platforms :
  264. @$(ECHO) $(PLATFORMS)
  265. ifdef BIN
  266. # Determine architecture portion of $(BIN), if present
  267. BIN_ARCH := $(strip $(foreach A,$(ARCHS),\
  268. $(patsubst bin-$(A)-%,$(A),\
  269. $(filter bin-$(A)-%,$(BIN)))))
  270. # Determine platform portion of $(BIN), if present
  271. ifeq ($(BIN_ARCH),)
  272. BIN_PLATFORM := $(patsubst bin-%,%,$(filter bin-%,$(BIN)))
  273. else
  274. BIN_PLATFORM := $(patsubst bin-$(BIN_ARCH)-%,%,$(BIN))
  275. endif
  276. # Determine build architecture
  277. DEFAULT_ARCH := i386
  278. ARCH := $(firstword $(BIN_ARCH) $(DEFAULT_ARCH))
  279. CFLAGS += -DARCH=$(ARCH)
  280. arch :
  281. @$(ECHO) $(ARCH)
  282. .PHONY : arch
  283. # Determine build platform
  284. DEFAULT_PLATFORM := pcbios
  285. PLATFORM := $(firstword $(BIN_PLATFORM) $(DEFAULT_PLATFORM))
  286. CFLAGS += -DPLATFORM=$(PLATFORM)
  287. platform :
  288. @$(ECHO) $(PLATFORM)
  289. endif # defined(BIN)
  290. # Include architecture-specific Makefile
  291. ifdef ARCH
  292. MAKEDEPS += arch/$(ARCH)/Makefile
  293. include arch/$(ARCH)/Makefile
  294. endif
  295. # Include architecture-specific include path
  296. ifdef ARCH
  297. INCDIRS += arch/$(ARCH)/include
  298. INCDIRS += arch/$(ARCH)/include/$(PLATFORM)
  299. endif
  300. ###############################################################################
  301. #
  302. # Source file handling
  303. # SRCDIRS lists all directories containing source files.
  304. srcdirs :
  305. @$(ECHO) $(SRCDIRS)
  306. # SRCS lists all .c or .S files found in any SRCDIR
  307. #
  308. SRCS += $(wildcard $(patsubst %,%/*.c,$(SRCDIRS)))
  309. SRCS += $(wildcard $(patsubst %,%/*.S,$(SRCDIRS)))
  310. srcs :
  311. @$(ECHO) $(SRCS)
  312. # AUTO_SRCS lists all files in SRCS that are not mentioned in
  313. # NON_AUTO_SRCS. Files should be added to NON_AUTO_SRCS if they
  314. # cannot be built using the standard build template.
  315. #
  316. AUTO_SRCS = $(filter-out $(NON_AUTO_SRCS),$(SRCS))
  317. autosrcs :
  318. @$(ECHO) $(AUTO_SRCS)
  319. # Just about everything else in this section depends upon having
  320. # $(BIN) set
  321. ifdef BIN
  322. # INCDIRS lists the include path
  323. incdirs :
  324. @$(ECHO) $(INCDIRS)
  325. # Common flags
  326. #
  327. CFLAGS += $(foreach INC,$(INCDIRS),-I$(INC))
  328. CFLAGS += -Os
  329. CFLAGS += -g
  330. ifeq ($(CCTYPE),gcc)
  331. CFLAGS += -ffreestanding
  332. CFLAGS += -Wall -W -Wformat-nonliteral
  333. HOST_CFLAGS += -Wall -W -Wformat-nonliteral
  334. endif
  335. ifeq ($(CCTYPE),icc)
  336. CFLAGS += -fno-builtin
  337. CFLAGS += -no-ip
  338. CFLAGS += -no-gcc
  339. CFLAGS += -diag-disable 111 # Unreachable code
  340. CFLAGS += -diag-disable 128 # Unreachable loop
  341. CFLAGS += -diag-disable 170 # Array boundary checks
  342. CFLAGS += -diag-disable 177 # Unused functions
  343. CFLAGS += -diag-disable 181 # printf() format checks
  344. CFLAGS += -diag-disable 188 # enum strictness
  345. CFLAGS += -diag-disable 193 # Undefined preprocessor identifiers
  346. CFLAGS += -diag-disable 280 # switch ( constant )
  347. CFLAGS += -diag-disable 310 # K&R parameter lists
  348. CFLAGS += -diag-disable 424 # Extra semicolon
  349. CFLAGS += -diag-disable 589 # Declarations mid-code
  350. CFLAGS += -diag-disable 593 # Unused variables
  351. CFLAGS += -diag-disable 810 # Casting ints to smaller ints
  352. CFLAGS += -diag-disable 981 # Sequence point violations
  353. CFLAGS += -diag-disable 1292 # Ignored attributes
  354. CFLAGS += -diag-disable 1338 # void pointer arithmetic
  355. CFLAGS += -diag-disable 1361 # Variable-length arrays
  356. CFLAGS += -diag-disable 1418 # Missing prototypes
  357. CFLAGS += -diag-disable 1419 # Missing prototypes
  358. CFLAGS += -diag-disable 1599 # Hidden variables
  359. CFLAGS += -Wall -Wmissing-declarations
  360. endif
  361. CFLAGS += $(WORKAROUND_CFLAGS) $(EXTRA_CFLAGS)
  362. ASFLAGS += $(WORKAROUND_ASFLAGS) $(EXTRA_ASFLAGS)
  363. LDFLAGS += $(WORKAROUND_LDFLAGS) $(EXTRA_LDFLAGS)
  364. HOST_CFLAGS += -O2 -g
  365. # Inhibit -Werror if NO_WERROR is specified on make command line
  366. #
  367. ifneq ($(NO_WERROR),1)
  368. CFLAGS += -Werror
  369. ASFLAGS += --fatal-warnings
  370. HOST_CFLAGS += -Werror
  371. endif
  372. # Function trace recorder state in the last build. This is needed
  373. # in order to correctly rebuild whenever the function recorder is
  374. # enabled/disabled.
  375. #
  376. FNREC_STATE := $(BIN)/.fnrec.state
  377. ifeq ($(wildcard $(FNREC_STATE)),)
  378. FNREC_OLD := <invalid>
  379. else
  380. FNREC_OLD := $(shell cat $(FNREC_STATE))
  381. endif
  382. ifeq ($(FNREC_OLD),$(FNREC))
  383. $(FNREC_STATE) :
  384. else
  385. $(FNREC_STATE) : clean
  386. $(shell $(ECHO) "$(FNREC)" > $(FNREC_STATE))
  387. endif
  388. VERYCLEANUP += $(FNREC_STATE)
  389. MAKEDEPS += $(FNREC_STATE)
  390. ifeq ($(FNREC),1)
  391. # Enabling -finstrument-functions affects gcc's analysis and leads to spurious
  392. # warnings about use of uninitialised variables.
  393. #
  394. CFLAGS += -Wno-uninitialized
  395. CFLAGS += -finstrument-functions
  396. CFLAGS += -finstrument-functions-exclude-file-list=core/fnrec.c
  397. endif
  398. # Enable per-item sections and section garbage collection. Note that
  399. # some older versions of gcc support -fdata-sections but treat it as
  400. # implying -fno-common, which would break our build. Some other older
  401. # versions issue a spurious and uninhibitable warning if
  402. # -ffunction-sections is used with -g, which would also break our
  403. # build since we use -Werror.
  404. #
  405. ifeq ($(CCTYPE),gcc)
  406. DS_TEST = $(ECHO) 'char x;' | \
  407. $(CC) -fdata-sections -S -x c - -o - 2>/dev/null | \
  408. grep -E '\.comm' > /dev/null
  409. DS_FLAGS := $(shell $(DS_TEST) && $(ECHO) '-fdata-sections')
  410. FS_TEST = $(CC) -ffunction-sections -g -c -x c /dev/null \
  411. -o /dev/null 2>/dev/null
  412. FS_FLAGS := $(shell $(FS_TEST) && $(ECHO) '-ffunction-sections')
  413. CFLAGS += $(FS_FLAGS) $(DS_FLAGS)
  414. endif
  415. LDFLAGS += --gc-sections
  416. # Force creation of static binaries (required for OpenBSD, does no
  417. # harm on other platforms).
  418. #
  419. LDFLAGS += -static
  420. # compiler.h is needed for our linking and debugging system
  421. #
  422. CFLAGS += -include include/compiler.h
  423. # The section type character (e.g. "@" in "@progbits") varies by
  424. # architecture.
  425. #
  426. CFLAGS += -DASM_TCHAR='$(ASM_TCHAR)' -DASM_TCHAR_OPS='$(ASM_TCHAR_OPS)'
  427. # CFLAGS for specific object types
  428. #
  429. CFLAGS_c +=
  430. CFLAGS_S += -DASSEMBLY
  431. # Base object name of the current target
  432. #
  433. OBJECT = $(firstword $(subst ., ,$(@F)))
  434. # CFLAGS for specific object files. You can define
  435. # e.g. CFLAGS_rtl8139, and have those flags automatically used when
  436. # compiling bin/rtl8139.o.
  437. #
  438. OBJ_CFLAGS = $(CFLAGS_$(OBJECT)) -DOBJECT=$(subst -,_,$(OBJECT))
  439. $(BIN)/%.flags :
  440. @$(ECHO) $(OBJ_CFLAGS)
  441. # ICC requires postprocessing objects to fix up table alignments
  442. #
  443. ifeq ($(CCTYPE),icc)
  444. POST_O = && $(ICCFIX) $@
  445. POST_O_DEPS := $(ICCFIX)
  446. else
  447. POST_O :=
  448. POST_O_DEPS :=
  449. endif
  450. # Debug level calculations
  451. #
  452. DBGLVL_MAX = -DDBGLVL_MAX=$(firstword $(subst ., ,$(1)))
  453. DBGLVL_DFLT = -DDBGLVL_DFLT=$(lastword $(subst ., ,$(1)))
  454. DBGLVL = $(call DBGLVL_MAX,$(1)) $(call DBGLVL_DFLT,$(1))
  455. # Rules for specific object types.
  456. #
  457. COMPILE_c = $(CC) $(CFLAGS) $(CFLAGS_c) $(OBJ_CFLAGS)
  458. RULE_c = $(Q)$(COMPILE_c) -c $< -o $@ $(POST_O)
  459. RULE_c_to_ids.o = $(Q)$(ECHO_E) '$(OBJ_IDS_ASM_NL)' | $(ASSEMBLE_S) -o $@
  460. RULE_c_to_dbg%.o= $(Q)$(COMPILE_c) $(call DBGLVL,$*) -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) $(call DBGLVL,$*) $< | $(ASSEMBLE_S) -o $@
  467. RULE_S_to_s = $(Q)$(PREPROCESS_S) $< > $@
  468. GENERIC_TARGETS += ids.o 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. .PRECIOUS : config/named.h
  602. # These files use .incbin inline assembly to include a binary file.
  603. # Unfortunately ccache does not detect this dependency and caches
  604. # builds even when the binary file has changed.
  605. #
  606. $(BIN)/embedded.% : override CC := env CCACHE_DISABLE=1 $(CC)
  607. $(BIN)/certstore.% : override CC := env CCACHE_DISABLE=1 $(CC)
  608. $(BIN)/privkey.% : override CC := env CCACHE_DISABLE=1 $(CC)
  609. # Debug message autocolourisation range
  610. #
  611. DBGCOL_LIST := $(BIN)/.dbgcol.list
  612. ifeq ($(wildcard $(DBGCOL_LIST)),)
  613. DBGCOL_OLD := <invalid>
  614. else
  615. DBGCOL_OLD := $(shell cat $(DBGCOL_LIST))
  616. endif
  617. ifneq ($(DBGCOL_OLD),$(DBGCOL))
  618. $(shell $(ECHO) "$(DBGCOL)" > $(DBGCOL_LIST))
  619. endif
  620. $(DBGCOL_LIST) : $(MAKEDEPS)
  621. VERYCLEANUP += $(DBGCOL_LIST)
  622. DBGCOL_COLOURS := $(subst -, ,$(DBGCOL))
  623. DBGCOL_MIN := $(firstword $(DBGCOL_COLOURS))
  624. DBGCOL_MAX := $(lastword $(DBGCOL_COLOURS))
  625. debug_DEPS += $(DBGCOL_LIST)
  626. CFLAGS_debug += $(if $(DBGCOL_MIN),-DDBGCOL_MIN=$(DBGCOL_MIN))
  627. CFLAGS_debug += $(if $(DBGCOL_MAX),-DDBGCOL_MAX=$(DBGCOL_MAX))
  628. # We automatically generate rules for any file mentioned in AUTO_SRCS
  629. # using the following set of templates. We use $(eval ...) if
  630. # available, otherwise we generate separate Makefile fragments and
  631. # include them.
  632. # deps_template : generate dependency list for a given source file
  633. #
  634. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  635. #
  636. define deps_template_file
  637. $(call deps_template_parts,$(1),$(subst .,,$(suffix $(1))),$(basename $(notdir $(1))))
  638. endef
  639. #
  640. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  641. # $(2) is the source type (e.g. "c")
  642. # $(3) is the source base name (e.g. "rtl8139")
  643. #
  644. define deps_template_parts
  645. @$(ECHO) " [DEPS] $(1)"
  646. @$(MKDIR) -p $(BIN)/deps/$(dir $(1))
  647. $(Q)$(CPP) $(CFLAGS) $(CFLAGS_$(2)) $(CFLAGS_$(3)) -DOBJECT=$(3) \
  648. -Wno-error -M $(1) -MG -MP | \
  649. sed 's/\.o\s*:/_DEPS +=/' > $(BIN)/deps/$(1).d
  650. endef
  651. # rules_template : generate rules for a given source file
  652. #
  653. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  654. #
  655. define rules_template
  656. $(call rules_template_parts,$(1),$(subst .,,$(suffix $(1))),$(basename $(notdir $(1))))
  657. endef
  658. #
  659. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  660. # $(2) is the source type (e.g. "c")
  661. # $(3) is the source base name (e.g. "rtl8139")
  662. #
  663. define rules_template_parts
  664. $$(BIN)/$(3).o : $(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(3)_DEPS)
  665. $$(QM)$(ECHO) " [BUILD] $$@"
  666. $$(RULE_$(2))
  667. BOBJS += $$(BIN)/$(3).o
  668. $(foreach TGT,$(GENERIC_TARGETS),$(if $(RULE_$(2)_to_$(TGT)),$(NEWLINE)$(call rules_template_target,$(1),$(2),$(3),$(TGT))))
  669. $$(BIN)/deps/$(1).d : $$($(3)_DEPS)
  670. TAGS : $$($(3)_DEPS)
  671. endef
  672. #
  673. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  674. # $(2) is the source type (e.g. "c")
  675. # $(3) is the source base name (e.g. "rtl8139")
  676. # $(4) is the destination type (e.g. "dbg%.o")
  677. #
  678. define rules_template_target
  679. $$(BIN)/$(3).$(4) : $(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(3)_DEPS)
  680. $$(QM)$(ECHO) " [BUILD] $$@"
  681. $$(RULE_$(2)_to_$(4))
  682. $(TGT)_OBJS += $$(BIN)/$(3).$(4)
  683. endef
  684. #
  685. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  686. #
  687. define rules_template_file
  688. @$(ECHO) " [RULES] $(1)"
  689. @$(MKDIR) -p $(BIN)/rules/$(dir $(1))
  690. @$(ECHO_E) '$(subst $(NEWLINE),\n,$(call rules_template,$(1)))' \
  691. > $(BIN)/rules/$(1).r
  692. endef
  693. # Generate the dependency files
  694. #
  695. $(BIN)/deps/%.d : % $(MAKEDEPS)
  696. $(call deps_template_file,$<)
  697. # Calculate list of dependency files
  698. #
  699. AUTO_DEPS = $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
  700. autodeps :
  701. @$(ECHO) $(AUTO_DEPS)
  702. VERYCLEANUP += $(BIN)/deps
  703. # Include dependency files
  704. #
  705. ifdef NEED_DEPS
  706. ifneq ($(AUTO_DEPS),)
  707. -include $(AUTO_DEPS)
  708. endif
  709. endif
  710. # Generate the rules files
  711. #
  712. $(BIN)/rules/%.r : % $(MAKEDEPS)
  713. $(call rules_template_file,$<)
  714. # Calculate list of rules files
  715. #
  716. AUTO_RULES = $(patsubst %,$(BIN)/rules/%.r,$(AUTO_SRCS))
  717. autorules :
  718. @$(ECHO) $(AUTO_RULES)
  719. VERYCLEANUP += $(BIN)/rules
  720. # Evaluate rules (or include rules files)
  721. #
  722. ifdef NEED_DEPS
  723. ifneq ($(AUTO_RULES),)
  724. ifneq ($(HAVE_EVAL),)
  725. $(foreach SRC,$(AUTO_SRCS),$(eval $(call rules_template,$(SRC))))
  726. else
  727. -include $(AUTO_RULES)
  728. endif
  729. endif
  730. endif
  731. # Files to be parsed using parserom.pl
  732. #
  733. ROM_SRCS = $(foreach SRC,$(AUTO_SRCS),\
  734. $(if $(findstring drivers/,$(SRC)),$(SRC)))
  735. romsrcs :
  736. @$(ECHO) $(ROM_SRCS)
  737. # List of files to be parsed using parserom.pl
  738. #
  739. ROM_SRCS_LIST := $(BIN)/.rom.list
  740. ifeq ($(wildcard $(ROM_SRCS_LIST)),)
  741. ROM_SRCS_OLD := <invalid>
  742. else
  743. ROM_SRCS_OLD := $(shell cat $(ROM_SRCS_LIST))
  744. endif
  745. ifneq ($(ROM_SRCS_OLD),$(ROM_SRCS))
  746. $(shell $(ECHO) "$(ROM_SRCS)" > $(ROM_SRCS_LIST))
  747. endif
  748. $(ROM_SRCS_LIST) : $(MAKEDEPS)
  749. VERYCLEANUP += $(ROM_SRCS_LIST)
  750. # ROM definition file
  751. #
  752. ROMDEFS = $(BIN)/.rom.defs
  753. $(ROMDEFS) : $(ROM_SRCS) $(ROM_SRCS_LIST) $(PARSEROM) $(MAKEDEPS)
  754. $(QM)$(ECHO) " [PARSEROM]"
  755. $(Q)$(PERL) $(PARSEROM) $(ROM_SRCS) > $@
  756. VERYCLEANUP += $(ROMDEFS)
  757. # Evaluate ROM definition file
  758. ifdef NEED_DEPS
  759. ifneq ($(ROM_SRCS),)
  760. -include $(ROMDEFS)
  761. endif
  762. endif
  763. # Device ID tables (using IDs from ROM definition file)
  764. #
  765. define obj_pci_id_asm
  766. .section ".pci_devlist.$(1)", "a", $(ASM_TCHAR)progbits
  767. .globl pci_devlist_$(1)
  768. pci_devlist_$(1):
  769. .short ( 0x$(1) & 0xffff )
  770. endef
  771. define obj_isa_id_asm
  772. endef
  773. OBJ_IDS_ASM = $(foreach ROM,$(ROMS_$(OBJECT)),$(call obj_$(ROM_TYPE_$(ROM))_id_asm,$(ROM)))
  774. OBJ_IDS_ASM_NL = $(subst $(NEWLINE),\n,$(OBJ_IDS_ASM))
  775. $(BIN)/%.ids :
  776. @$(ECHO_E) '$(OBJ_IDS_ASM_NL)'
  777. BOBJS += $(patsubst %,$(BIN)/%.ids.o,$(DRIVERS))
  778. # The following variables are created by the autogenerated rules
  779. #
  780. bobjs :
  781. @$(ECHO) $(BOBJS)
  782. drivers_% :
  783. @$(ECHO) $(DRIVERS_$*)
  784. drivers :
  785. @$(ECHO) $(DRIVERS)
  786. .PHONY : drivers
  787. roms :
  788. @$(ECHO) $(ROMS)
  789. # Generate error usage information
  790. #
  791. $(BIN)/%.einfo : $(BIN)/%.o
  792. $(QM)$(ECHO) " [EINFO] $@"
  793. $(Q)$(OBJCOPY) -O binary -j .einfo --set-section-flags .einfo=alloc \
  794. $< $@
  795. EINFOS := $(patsubst $(BIN)/%.o,$(BIN)/%.einfo,$(BOBJS))
  796. $(BIN)/errors : $(EINFOS) $(EINFO)
  797. $(QM)$(ECHO) " [EINFO] $@"
  798. $(Q)$(EINFO) $(EINFOS) | sort > $@
  799. CLEANUP += $(BIN)/errors # Doesn't match the $(BIN)/*.* pattern
  800. # Generate the NIC file from the parsed source files. The NIC file is
  801. # only for rom-o-matic.
  802. #
  803. $(BIN)/NIC : $(AUTO_DEPS)
  804. @$(ECHO) '# This is an automatically generated file, do not edit' > $@
  805. @$(ECHO) '# It does not affect anything in the build, ' \
  806. 'it is only for rom-o-matic' >> $@
  807. @$(ECHO) >> $@
  808. @perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
  809. CLEANUP += $(BIN)/NIC # Doesn't match the $(BIN)/*.* pattern
  810. # Select drivers to be included in the all-drivers build
  811. #
  812. DRIVERS_ipxe = $(DRIVERS_net) $(DRIVERS_infiniband) \
  813. $(DRIVERS_xen) $(DRIVERS_hyperv)
  814. # Analyse a target name (e.g. "bin/dfe538--prism2_pci.rom.tmp") and
  815. # derive the variables:
  816. #
  817. # TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
  818. # TGT_PREFIX : the prefix type (e.g. "pcirom")
  819. # TGT_DRIVERS : the driver for each element (e.g. "rtl8139 prism2_pci")
  820. # TGT_ROM_NAME : the ROM name (e.g. "dfe538")
  821. #
  822. TGT_ELEMENTS = $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
  823. TGT_ROM_NAME = $(firstword $(TGT_ELEMENTS))
  824. TGT_DRIVERS = $(strip $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
  825. $(if $(DRIVERS_$(TGT_ELEMENT)), \
  826. $(DRIVERS_$(TGT_ELEMENT)), \
  827. $(firstword $(DRIVER_$(TGT_ELEMENT)) \
  828. $(TGT_ELEMENT)))))
  829. TGT_PREFIX_NAME = $(word 2,$(subst ., ,$(notdir $@)))
  830. TGT_PREFIX = $(strip $(if $(filter rom,$(TGT_PREFIX_NAME)), \
  831. $(ROM_TYPE_$(TGT_ROM_NAME))rom, \
  832. $(TGT_PREFIX_NAME)))
  833. # Look up ROM IDs for the current target
  834. # (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the variables:
  835. #
  836. # TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
  837. # TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
  838. #
  839. TGT_PCI_VENDOR = $(PCI_VENDOR_$(TGT_ROM_NAME))
  840. TGT_PCI_DEVICE = $(PCI_DEVICE_$(TGT_ROM_NAME))
  841. # Calculate link-time options for the current target
  842. # (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the variables:
  843. #
  844. # TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
  845. # (e.g. "obj_rtl8139 obj_prism2_pci")
  846. # TGT_LD_IDS : symbols to define in order to fill in ID structures in the
  847. # ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
  848. #
  849. TGT_LD_DRIVERS = $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
  850. TGT_LD_IDS = pci_vendor_id=$(firstword $(TGT_PCI_VENDOR) 0) \
  851. pci_device_id=$(firstword $(TGT_PCI_DEVICE) 0)
  852. TGT_LD_DEVLIST = $(foreach ELEM,$(TGT_ELEMENTS),$(if $(PCI_VENDOR_$(ELEM)),\
  853. pci_devlist_$(patsubst 0x%,%,$(PCI_VENDOR_$(ELEM)))$(patsubst 0x%,%,$(PCI_DEVICE_$(ELEM)))))
  854. TGT_LD_ENTRY = _$(TGT_PREFIX)_start
  855. # Calculate linker flags based on link-time options for the current
  856. # target type (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the
  857. # variables:
  858. #
  859. # TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
  860. # "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
  861. # --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
  862. #
  863. TGT_LD_FLAGS = $(foreach SYM,$(TGT_LD_ENTRY) $(TGT_LD_DRIVERS) \
  864. $(TGT_LD_DEVLIST) obj_config obj_config_$(PLATFORM),\
  865. -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
  866. $(patsubst %,--defsym %,$(TGT_LD_IDS)) \
  867. -e $(TGT_LD_ENTRY)
  868. # Calculate list of debugging versions of objects to be included in
  869. # the target.
  870. #
  871. DEBUG_LIST = $(subst $(COMMA), ,$(DEBUG))
  872. DEBUG_MAX = $(firstword $(word 2,$(subst :, ,$(1))) 1)
  873. DEBUG_DFLT = $(if $(word 3,$(subst :, ,$(1))),.$(word 3,$(subst :, ,$(1))))
  874. DEBUG_LEVEL = $(call DEBUG_MAX,$(1))$(call DEBUG_DFLT,$(1))
  875. DEBUG_BASE = $(firstword $(subst :, ,$(1))).dbg$(call DEBUG_LEVEL,$(1))
  876. DEBUG_OBJ = $(BIN)/$(call DEBUG_BASE,$(1)).o
  877. DEBUG_ORIG_OBJ = $(BIN)/$(firstword $(subst :, ,$(1))).o
  878. DEBUG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
  879. DEBUG_ORIG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
  880. BLIB_OBJS = $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
  881. # Print out all derived information for a given target.
  882. #
  883. $(BIN)/%.info :
  884. @$(ECHO) 'Elements : $(TGT_ELEMENTS)'
  885. @$(ECHO) 'Prefix : $(TGT_PREFIX)'
  886. @$(ECHO) 'Drivers : $(TGT_DRIVERS)'
  887. @$(ECHO) 'ROM name : $(TGT_ROM_NAME)'
  888. @$(ECHO)
  889. @$(ECHO) 'PCI vendor : $(TGT_PCI_VENDOR)'
  890. @$(ECHO) 'PCI device : $(TGT_PCI_DEVICE)'
  891. @$(ECHO)
  892. @$(ECHO) 'LD driver symbols : $(TGT_LD_DRIVERS)'
  893. @$(ECHO) 'LD ID symbols : $(TGT_LD_IDS)'
  894. @$(ECHO) 'LD devlist symbols : $(TGT_LD_DEVLIST)'
  895. @$(ECHO) 'LD entry point : $(TGT_LD_ENTRY)'
  896. @$(ECHO)
  897. @$(ECHO) 'LD target flags : $(TGT_LD_FLAGS)'
  898. @$(ECHO)
  899. @$(ECHO) 'Debugging objects : $(DEBUG_OBJS)'
  900. @$(ECHO) 'Replaced objects : $(DEBUG_ORIG_OBJS)'
  901. # List of objects included in the last build of blib. This is needed
  902. # in order to correctly rebuild blib whenever the list of objects
  903. # changes.
  904. #
  905. BLIB_LIST := $(BIN)/.blib.list
  906. ifeq ($(wildcard $(BLIB_LIST)),)
  907. BLIB_OBJS_OLD := <invalid>
  908. else
  909. BLIB_OBJS_OLD := $(shell cat $(BLIB_LIST))
  910. endif
  911. ifneq ($(BLIB_OBJS_OLD),$(BLIB_OBJS))
  912. $(shell $(ECHO) "$(BLIB_OBJS)" > $(BLIB_LIST))
  913. endif
  914. $(BLIB_LIST) : $(MAKEDEPS)
  915. VERYCLEANUP += $(BLIB_LIST)
  916. # Library of all objects
  917. #
  918. BLIB = $(BIN)/blib.a
  919. $(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
  920. $(Q)$(RM) $(BLIB)
  921. $(QM)$(ECHO) " [AR] $@"
  922. $(Q)$(AR) r $@ $(sort $(BLIB_OBJS))
  923. $(Q)$(RANLIB) $@
  924. blib : $(BLIB)
  925. # Command to generate build ID. Must be unique for each $(BIN)/%.tmp,
  926. # even within the same build run.
  927. #
  928. BUILD_ID_CMD := perl -e 'printf "0x%08x", int ( rand ( 0xffffffff ) );'
  929. # Build timestamp
  930. #
  931. BUILD_TIMESTAMP := $(shell date +%s)
  932. # Build version
  933. #
  934. GIT_INDEX := $(if $(GITVERSION),$(if $(wildcard ../.git/index),../.git/index))
  935. $(BIN)/version.%.o : core/version.c $(MAKEDEPS) $(GIT_INDEX)
  936. $(QM)$(ECHO) " [VERSION] $@"
  937. $(Q)$(COMPILE_c) -DBUILD_NAME="\"$*\"" \
  938. -DVERSION_MAJOR=$(VERSION_MAJOR) \
  939. -DVERSION_MINOR=$(VERSION_MINOR) \
  940. -DVERSION_PATCH=$(VERSION_PATCH) \
  941. -DVERSION="\"$(VERSION)\"" \
  942. -c $< -o $@
  943. # Build an intermediate object file from the objects required for the
  944. # specified target.
  945. #
  946. $(BIN)/%.tmp : $(BIN)/version.%.o $(BLIB) $(MAKEDEPS) $(LDSCRIPT)
  947. $(QM)$(ECHO) " [LD] $@"
  948. $(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $< $(BLIB) -o $@ \
  949. --defsym _build_id=`$(BUILD_ID_CMD)` \
  950. --defsym _build_timestamp=$(BUILD_TIMESTAMP) \
  951. -Map $(BIN)/$*.tmp.map
  952. $(Q)$(OBJDUMP) -ht $@ | $(PERL) $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
  953. # Keep intermediate object file (useful for debugging)
  954. .PRECIOUS : $(BIN)/%.tmp
  955. # Show a linker map for the specified target
  956. #
  957. $(BIN)/%.map : $(BIN)/%.tmp
  958. @less $(BIN)/$*.tmp.map
  959. # Get objects list for the specified target
  960. #
  961. define objs_list
  962. $(sort $(foreach OBJ_SYMBOL,\
  963. $(filter obj_%,$(shell $(NM) $(1) | cut -d" " -f3)),\
  964. $(patsubst obj_%,%,$(OBJ_SYMBOL))))
  965. endef
  966. $(BIN)/%.objs : $(BIN)/%.tmp
  967. $(Q)$(ECHO) $(call objs_list,$<)
  968. $(BIN)/%.sizes : $(BIN)/%.tmp
  969. $(Q)$(SIZE) -t $(foreach OBJ,$(call objs_list,$<),$(wildcard $(BIN)/$(subst _,?,$(OBJ)).o)) | \
  970. sort -g
  971. # Get dependency list for the specified target
  972. #
  973. define deps_list
  974. $(sort $(foreach OBJ,$(call objs_list,$(1)),$($(OBJ)_DEPS)))
  975. endef
  976. $(BIN)/%.deps : $(BIN)/%.tmp
  977. $(Q)$(ECHO) $(call deps_list,$<)
  978. # Get unneeded source files for the specified target
  979. #
  980. define nodeps_list
  981. $(sort $(filter-out $(call deps_list,$(1)),\
  982. $(foreach BOBJ,$(BOBJS),\
  983. $($(basename $(notdir $(BOBJ)))_DEPS))))
  984. endef
  985. $(BIN)/%.nodeps : $(BIN)/%.tmp
  986. $(Q)$(ECHO) $(call nodeps_list,$<)
  987. # Get licensing verdict for the specified target
  988. #
  989. define licensable_deps_list
  990. $(filter-out config/local/%.h,\
  991. $(filter-out $(BIN)/.%.list,\
  992. $(call deps_list,$(1))))
  993. endef
  994. define unlicensed_deps_list
  995. $(shell grep -L FILE_LICENCE $(call licensable_deps_list,$(1)))
  996. endef
  997. define licence_list
  998. $(sort $(foreach LICENCE,\
  999. $(filter __licence__%,$(shell $(NM) $(1) | cut -d" " -f3)),\
  1000. $(word 2,$(subst __, ,$(LICENCE)))))
  1001. endef
  1002. $(BIN)/%.licence_list : $(BIN)/%.tmp
  1003. $(Q)$(ECHO) $(call licence_list,$<)
  1004. $(BIN)/%.licence : $(BIN)/%.tmp
  1005. $(QM)$(ECHO) " [LICENCE] $@"
  1006. $(Q)$(if $(strip $(call unlicensed_deps_list,$<)),\
  1007. echo -n "Unable to determine licence because the following " ;\
  1008. echo "files are missing a licence declaration:" ;\
  1009. echo $(call unlicensed_deps_list,$<);\
  1010. exit 1,\
  1011. $(PERL) $(LICENCE) $(call licence_list,$<))
  1012. # Extract compression information from intermediate object file
  1013. #
  1014. $(BIN)/%.zinfo : $(BIN)/%.tmp
  1015. $(QM)$(ECHO) " [ZINFO] $@"
  1016. $(Q)$(OBJCOPY) -O binary -j .zinfo $< $@
  1017. # Build raw binary file from intermediate object file
  1018. #
  1019. $(BIN)/%.bin : $(BIN)/%.tmp
  1020. $(QM)$(ECHO) " [BIN] $@"
  1021. $(Q)$(OBJCOPY) -O binary -R .zinfo $< $@
  1022. # Compress raw binary file
  1023. #
  1024. $(BIN)/%.zbin : $(BIN)/%.bin $(BIN)/%.zinfo $(ZBIN)
  1025. $(QM)$(ECHO) " [ZBIN] $@"
  1026. $(Q)$(ZBIN) $(BIN)/$*.bin $(BIN)/$*.zinfo > $@
  1027. # Rules for each media format. These are generated and placed in an
  1028. # external Makefile fragment. We could do this via $(eval ...), but
  1029. # that would require make >= 3.80.
  1030. #
  1031. # Note that there's an alternative way to generate most .rom images:
  1032. # they can be copied from their 'master' ROM image using cp and
  1033. # reprocessed with makerom to add the PCI IDs and ident string. The
  1034. # relevant rule would look something like:
  1035. #
  1036. # $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
  1037. # cat $< $@
  1038. # $(FINALISE_rom)
  1039. #
  1040. # You can derive the ROM/driver relationships using the variables
  1041. # DRIVER_<rom> and/or ROMS_<driver>.
  1042. #
  1043. # We don't currently do this, because (a) it would require generating
  1044. # yet more Makefile fragments (since you need a rule for each ROM in
  1045. # ROMS), and (b) the linker is so fast that it probably wouldn't make
  1046. # much difference to the overall build time.
  1047. # Add NON_AUTO_MEDIA to the media list, so that they show up in the
  1048. # output of "make"
  1049. #
  1050. MEDIA += $(NON_AUTO_MEDIA)
  1051. media :
  1052. @$(ECHO) $(MEDIA)
  1053. AUTO_MEDIA = $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
  1054. automedia :
  1055. @$(ECHO) $(AUTO_MEDIA)
  1056. # media_template : create media rules
  1057. #
  1058. # $(1) is the media name (e.g. "rom")
  1059. #
  1060. define media_template
  1061. $(if $(filter $(1),$(AUTO_MEDIA)),$(call auto_media_template,$(1)))
  1062. LIST_$(1) := $$(if $$(LIST_NAME_$(1)),$$($$(LIST_NAME_$(1))),$$(DRIVERS))
  1063. ALL_$(1) = $$(foreach ITEM,$$(LIST_$(1)),$$(BIN)/$$(ITEM).$(1))
  1064. $$(BIN)/all$(1)s : $$(ALL_$(1))
  1065. $$(BIN)/allall : $$(BIN)/all$(1)s
  1066. all$(1)s : $$(BIN)/all$(1)s
  1067. allall : $$(BIN)/allall
  1068. endef
  1069. #
  1070. # $(1) is the media name (e.g. "rom")
  1071. #
  1072. define auto_media_template
  1073. $$(BIN)/%.$(1) : $$(BIN)/%.$(1).zbin
  1074. $$(QM)echo " [FINISH] $$@"
  1075. $$(Q)$$(CP) $$< $$@
  1076. $$(Q)$$(if $$(PAD_$(1)),$$(PAD_$(1)) $$@)
  1077. $$(Q)$$(if $$(FINALISE_$(1)),$$(FINALISE_$(1)) $$@)
  1078. endef
  1079. #
  1080. # $(1) is the media name (e.g. "rom")
  1081. #
  1082. define media_template_file
  1083. @$(ECHO) " [MEDIARULES] $(1)"
  1084. @$(MKDIR) -p $(BIN)/rules/$(dir $(1))
  1085. @$(ECHO_E) '$(subst $(NEWLINE),\n,$(call media_template,$(1)))' \
  1086. > $(BIN)/rules/$(1).media.r
  1087. endef
  1088. # Generate media rules files
  1089. #
  1090. $(BIN)/rules/%.media.r : $(MAKEDEPS)
  1091. $(call media_template_file,$*)
  1092. # Calculate list of media rules files
  1093. #
  1094. MEDIA_RULES = $(patsubst %,$(BIN)/rules/%.media.r,$(MEDIA))
  1095. mediarules :
  1096. @$(ECHO) $(MEDIA_RULES)
  1097. # Evaluate media rules (or include media rules files)
  1098. #
  1099. ifdef NEED_DEPS
  1100. ifneq ($(MEDIA_RULES),)
  1101. ifneq ($(HAVE_EVAL),)
  1102. $(foreach MEDIUM,$(MEDIA),$(eval $(call media_template,$(MEDIUM))))
  1103. else
  1104. -include $(MEDIA_RULES)
  1105. endif
  1106. endif
  1107. endif
  1108. # Alias for ipxe.%
  1109. #
  1110. $(BIN)/etherboot.% : $(BIN)/ipxe.%
  1111. ln -sf $(notdir $<) $@
  1112. endif # defined(BIN)
  1113. ###############################################################################
  1114. #
  1115. # The compression utilities
  1116. #
  1117. ZBIN_LDFLAGS := -llzma
  1118. $(ZBIN) : util/zbin.c $(MAKEDEPS)
  1119. $(QM)$(ECHO) " [HOSTCC] $@"
  1120. $(Q)$(HOST_CC) $(HOST_CFLAGS) $< $(ZBIN_LDFLAGS) -o $@
  1121. CLEANUP += $(ZBIN)
  1122. ###############################################################################
  1123. #
  1124. # The EFI image converter
  1125. #
  1126. $(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
  1127. $(QM)$(ECHO) " [HOSTCC] $@"
  1128. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -DEFI_TARGET32 $< -o $@
  1129. CLEANUP += $(ELF2EFI32)
  1130. $(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
  1131. $(QM)$(ECHO) " [HOSTCC] $@"
  1132. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -DEFI_TARGET64 $< -o $@
  1133. CLEANUP += $(ELF2EFI64)
  1134. $(EFIROM) : util/efirom.c $(MAKEDEPS)
  1135. $(QM)$(ECHO) " [HOSTCC] $@"
  1136. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1137. CLEANUP += $(EFIROM)
  1138. $(EFIFATBIN) : util/efifatbin.c $(MAKEDEPS)
  1139. $(QM)$(ECHO) " [HOSTCC] $@"
  1140. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1141. CLEANUP += $(EFIFATBIN)
  1142. ###############################################################################
  1143. #
  1144. # The ICC fixup utility
  1145. #
  1146. $(ICCFIX) : util/iccfix.c $(MAKEDEPS)
  1147. $(QM)$(ECHO) " [HOSTCC] $@"
  1148. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1149. CLEANUP += $(ICCFIX)
  1150. ###############################################################################
  1151. #
  1152. # The error usage information utility
  1153. #
  1154. $(EINFO) : util/einfo.c $(MAKEDEPS)
  1155. $(QM)$(ECHO) " [HOSTCC] $@"
  1156. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1157. CLEANUP += $(EINFO)
  1158. ###############################################################################
  1159. #
  1160. # Local configs
  1161. #
  1162. CONFIG_HEADERS := $(patsubst config/%,%,$(wildcard config/*.h))
  1163. CONFIG_LOCAL_HEADERS := $(foreach HEADER,$(CONFIG_HEADERS),\
  1164. config/local/$(HEADER))
  1165. $(CONFIG_LOCAL_HEADERS) :
  1166. $(Q)$(TOUCH) $@
  1167. .PRECIOUS : $(CONFIG_LOCAL_HEADERS)
  1168. ifneq ($(CONFIG),)
  1169. CONFIG_LOCAL_NAMED_HEADERS := $(foreach HEADER,$(CONFIG_HEADERS),\
  1170. config/local/$(CONFIG)/$(HEADER))
  1171. $(CONFIG_LOCAL_NAMED_HEADERS) :
  1172. $(Q)$(MKDIR) -p $(dir $@)
  1173. $(Q)$(TOUCH) $@
  1174. .PRECIOUS : $(CONFIG_LOCAL_NAMED_HEADERS)
  1175. endif
  1176. ###############################################################################
  1177. #
  1178. # Build the TAGS file(s) for emacs
  1179. #
  1180. TAGS :
  1181. ctags -e -R -f $@ --exclude=bin
  1182. CLEANUP += TAGS
  1183. ###############################################################################
  1184. #
  1185. # Force rebuild for any given target
  1186. #
  1187. %.rebuild :
  1188. rm -f $*
  1189. $(Q)$(MAKE) $*
  1190. ###############################################################################
  1191. #
  1192. # Symbol table checks
  1193. #
  1194. ifdef BIN
  1195. SYMTAB = $(BIN)/symtab
  1196. $(SYMTAB) : $(BLIB)
  1197. $(OBJDUMP) -w -t $< > $@
  1198. CLEANUP += $(BIN)/symtab
  1199. symcheck : $(SYMTAB)
  1200. $(PERL) $(SYMCHECK) $<
  1201. endif # defined(BIN)
  1202. ###############################################################################
  1203. #
  1204. # Build bochs symbol table
  1205. #
  1206. ifdef BIN
  1207. $(BIN)/%.bxs : $(BIN)/%.tmp
  1208. $(NM) $< | cut -d" " -f1,3 > $@
  1209. endif # defined(BIN)
  1210. ###############################################################################
  1211. #
  1212. # Documentation
  1213. #
  1214. ifdef BIN
  1215. $(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
  1216. $(Q)$(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
  1217. -e 's{\@INCDIRS\@}{$(filter-out .,$(INCDIRS))}; ' \
  1218. -e 's{\@BIN\@}{$(BIN)}; ' \
  1219. -e 's{\@ARCH\@}{$(ARCH)}; ' \
  1220. $< > $@
  1221. $(BIN)/doc : $(BIN)/doxygen.cfg
  1222. $(Q)$(DOXYGEN) $<
  1223. .PHONY : $(BIN)/doc
  1224. doc : $(BIN)/doc
  1225. doc-clean :
  1226. $(Q)$(RM) -r $(BIN)/doc
  1227. VERYCLEANUP += $(BIN)/doc
  1228. docview :
  1229. @[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
  1230. @if [ -n "$$BROWSER" ] ; then \
  1231. ( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
  1232. else \
  1233. $(ECHO) "Documentation index in $(BIN)/doc/html/index.html" ; \
  1234. fi
  1235. endif # defined(BIN)
  1236. ###############################################################################
  1237. #
  1238. # Keyboard maps
  1239. #
  1240. hci/keymap/keymap_%.c :
  1241. $(Q)$(PERL) $(GENKEYMAP) $* > $@
  1242. ###############################################################################
  1243. #
  1244. # Force deletion of incomplete targets
  1245. #
  1246. .DELETE_ON_ERROR :
  1247. ###############################################################################
  1248. #
  1249. # Clean-up
  1250. #
  1251. ifeq ($(NUM_BINS),0)
  1252. ALLBINS := bin{,-*}
  1253. CLEANUP := $(patsubst $(BIN)/%,$(ALLBINS)/%,$(CLEANUP))
  1254. VERYCLEANUP := $(patsubst $(BIN)/%,$(ALLBINS)/%,$(VERYCLEANUP))
  1255. endif
  1256. clean :
  1257. $(RM) $(CLEANUP)
  1258. veryclean : clean
  1259. $(RM) -r $(VERYCLEANUP)