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

Makefile.housekeeping 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. # CFLAGS for specific object types
  424. #
  425. CFLAGS_c +=
  426. CFLAGS_S += -DASSEMBLY
  427. # Base object name of the current target
  428. #
  429. OBJECT = $(firstword $(subst ., ,$(@F)))
  430. # CFLAGS for specific object files. You can define
  431. # e.g. CFLAGS_rtl8139, and have those flags automatically used when
  432. # compiling bin/rtl8139.o.
  433. #
  434. OBJ_CFLAGS = $(CFLAGS_$(OBJECT)) -DOBJECT=$(subst -,_,$(OBJECT))
  435. $(BIN)/%.flags :
  436. @$(ECHO) $(OBJ_CFLAGS)
  437. # ICC requires postprocessing objects to fix up table alignments
  438. #
  439. ifeq ($(CCTYPE),icc)
  440. POST_O = && $(ICCFIX) $@
  441. POST_O_DEPS := $(ICCFIX)
  442. else
  443. POST_O :=
  444. POST_O_DEPS :=
  445. endif
  446. # Rules for specific object types.
  447. #
  448. COMPILE_c = $(CC) $(CFLAGS) $(CFLAGS_c) $(OBJ_CFLAGS)
  449. RULE_c = $(Q)$(COMPILE_c) -c $< -o $@ $(POST_O)
  450. RULE_c_to_dbg%.o = $(Q)$(COMPILE_c) -DDBGLVL_MAX=$* -c $< -o $@ $(POST_O)
  451. RULE_c_to_c = $(Q)$(COMPILE_c) -E -c $< > $@
  452. RULE_c_to_s = $(Q)$(COMPILE_c) -S -g0 -c $< -o $@
  453. PREPROCESS_S = $(CPP) $(CFLAGS) $(CFLAGS_S) $(OBJ_CFLAGS)
  454. ASSEMBLE_S = $(AS) $(ASFLAGS)
  455. RULE_S = $(Q)$(PREPROCESS_S) $< | $(ASSEMBLE_S) -o $@
  456. RULE_S_to_dbg%.o = $(Q)$(PREPROCESS_S) -DDBGLVL_MAX=$* $< | $(ASSEMBLE_S) -o $@
  457. RULE_S_to_s = $(Q)$(PREPROCESS_S) $< > $@
  458. DEBUG_TARGETS += dbg%.o c s
  459. # List of embedded images included in the last build of embedded.o.
  460. # This is needed in order to correctly rebuild embedded.o whenever the
  461. # list of objects changes.
  462. #
  463. EMBED := $(EMBEDDED_IMAGE) # Maintain backwards compatibility
  464. EMBEDDED_LIST := $(BIN)/.embedded.list
  465. ifeq ($(wildcard $(EMBEDDED_LIST)),)
  466. EMBED_OLD := <invalid>
  467. else
  468. EMBED_OLD := $(shell cat $(EMBEDDED_LIST))
  469. endif
  470. ifneq ($(EMBED_OLD),$(EMBED))
  471. $(shell $(ECHO) "$(EMBED)" > $(EMBEDDED_LIST))
  472. endif
  473. $(EMBEDDED_LIST) : $(MAKEDEPS)
  474. VERYCLEANUP += $(EMBEDDED_LIST)
  475. EMBEDDED_FILES := $(subst $(COMMA), ,$(EMBED))
  476. EMBED_ALL := $(foreach i,$(call seq,1,$(words $(EMBEDDED_FILES))),\
  477. EMBED ( $(i), \"$(word $(i), $(EMBEDDED_FILES))\",\
  478. \"$(notdir $(word $(i),$(EMBEDDED_FILES)))\" ))
  479. embedded_DEPS += $(EMBEDDED_FILES) $(EMBEDDED_LIST)
  480. CFLAGS_embedded = -DEMBED_ALL="$(EMBED_ALL)"
  481. # List of trusted root certificates
  482. #
  483. TRUSTED_LIST := $(BIN)/.trusted.list
  484. ifeq ($(wildcard $(TRUSTED_LIST)),)
  485. TRUST_OLD := <invalid>
  486. else
  487. TRUST_OLD := $(shell cat $(TRUSTED_LIST))
  488. endif
  489. ifneq ($(TRUST_OLD),$(TRUST))
  490. $(shell $(ECHO) "$(TRUST)" > $(TRUSTED_LIST))
  491. endif
  492. $(TRUSTED_LIST) : $(MAKEDEPS)
  493. VERYCLEANUP += $(TRUSTED_LIST)
  494. # Trusted root certificate fingerprints
  495. #
  496. TRUSTED_CERTS := $(subst $(COMMA), ,$(TRUST))
  497. TRUSTED_FPS := $(foreach CERT,$(TRUSTED_CERTS),\
  498. 0x$(subst :,$(COMMA) 0x,$(lastword $(subst =, ,\
  499. $(shell $(OPENSSL) x509 -in $(CERT) -noout -sha256 \
  500. -fingerprint))))$(COMMA))
  501. rootcert_DEPS += $(TRUSTED_FILES) $(TRUSTED_LIST)
  502. CFLAGS_rootcert = $(if $(TRUSTED_FPS),-DTRUSTED="$(TRUSTED_FPS)")
  503. # List of embedded certificates
  504. #
  505. CERT_LIST := $(BIN)/.certificate.list
  506. ifeq ($(wildcard $(CERT_LIST)),)
  507. CERT_OLD := <invalid>
  508. else
  509. CERT_OLD := $(shell cat $(CERT_LIST))
  510. endif
  511. ifneq ($(CERT_OLD),$(CERT))
  512. $(shell $(ECHO) "$(CERT)" > $(CERT_LIST))
  513. endif
  514. $(CERT_LIST) : $(MAKEDEPS)
  515. VERYCLEANUP += $(CERT_LIST)
  516. # Embedded certificates concatenated and then split into one file per
  517. # certificate (even if original files contained certificate chains)
  518. #
  519. CERT_FILES := $(subst $(COMMA), ,$(CERT))
  520. CERT_CONCAT := $(BIN)/.certificates.pem
  521. ifneq ($(CERT),)
  522. CERT_COUNT := $(shell grep "BEGIN CERTIFICATE" $(CERT_FILES) | wc -l)
  523. $(CERT_CONCAT) : $(CERT_FILES) $(CERT_LIST)
  524. $(Q)cat $(CERT_FILES) > $@
  525. # We must use an (otherwise unnecessary) pattern rule here to encode
  526. # the fact that one "csplit" command generates multiple targets
  527. CERT_PEMS := $(foreach i,$(call seq,1,$(CERT_COUNT)),\
  528. $(BIN)/.certificate.pem.$(i))
  529. $(subst .pem.,.%.,$(CERT_PEMS)) : $(BIN)/.certificates.%
  530. $(Q)$(CSPLIT) -q -n 1 -f $(BIN)/.certificate.pem. $< \
  531. '/BEGIN CERTIFICATE/' '{*}'
  532. CERT_DERS := $(subst .certificate.pem.,.certificate.der.,$(CERT_PEMS))
  533. $(BIN)/.certificate.der.% : $(BIN)/.certificate.pem.%
  534. $(Q)$(OPENSSL) x509 -in $< -outform DER -out $@
  535. CERT_ALL := $(foreach i,$(call seq,1,$(CERT_COUNT)),\
  536. CERT ( $(i), \"$(word $(i),$(CERT_DERS))\" ))
  537. endif
  538. certstore_DEPS += $(CERT_LIST) $(CERT_FILES) $(CERT_PEMS) $(CERT_DERS)
  539. CFLAGS_certstore += -DCERT_ALL="$(CERT_ALL)"
  540. CLEANUP += $(BIN)/.certificate.* $(BIN)/.certificates.*
  541. # (Single-element) list of private keys
  542. #
  543. ifdef KEY
  544. PRIVKEY := $(KEY) # Maintain backwards compatibility
  545. endif
  546. PRIVKEY_LIST := $(BIN)/.private_key.list
  547. ifeq ($(wildcard $(PRIVKEY_LIST)),)
  548. PRIVKEY_OLD := <invalid>
  549. else
  550. PRIVKEY_OLD := $(shell cat $(PRIVKEY_LIST))
  551. endif
  552. ifneq ($(PRIVKEY_OLD),$(PRIVKEY))
  553. $(shell $(ECHO) "$(PRIVKEY)" > $(PRIVKEY_LIST))
  554. endif
  555. $(PRIVKEY_LIST) : $(MAKEDEPS)
  556. VERYCLEANUP += $(PRIVKEY_LIST)
  557. # Embedded private key
  558. #
  559. PRIVKEY_INC := $(BIN)/.private_key.der
  560. ifdef PRIVKEY
  561. $(PRIVKEY_INC) : $(PRIVKEY) $(PRIVKEY_LIST)
  562. $(Q)$(OPENSSL) rsa -in $< -outform DER -out $@
  563. privkey_DEPS += $(PRIVKEY_INC)
  564. endif
  565. CLEANUP += $(BIN)/.private_key.*
  566. privkey_DEPS += $(PRIVKEY_LIST)
  567. CFLAGS_privkey += $(if $(PRIVKEY),-DPRIVATE_KEY="\"$(PRIVKEY_INC)\"")
  568. # (Single-element) list of named configurations
  569. #
  570. CONFIG_LIST := $(BIN)/.config.list
  571. ifeq ($(wildcard $(CONFIG_LIST)),)
  572. CONFIG_OLD := <invalid>
  573. else
  574. CONFIG_OLD := $(shell cat $(CONFIG_LIST))
  575. endif
  576. ifneq ($(CONFIG_OLD),$(CONFIG))
  577. $(shell $(ECHO) "$(CONFIG)" > $(CONFIG_LIST))
  578. endif
  579. $(CONFIG_LIST) : $(MAKEDEPS)
  580. VERYCLEANUP += $(CONFIG_LIST)
  581. # Named configurations
  582. #
  583. ifneq ($(CONFIG),)
  584. ifneq ($(wildcard config/$(CONFIG)),)
  585. CFLAGS += -DCONFIG=$(CONFIG)
  586. endif
  587. CFLAGS += -DLOCAL_CONFIG=$(CONFIG)
  588. endif
  589. config/named.h : $(CONFIG_LIST)
  590. $(Q)$(TOUCH) $@
  591. .PRECIOUS : config/named.h
  592. # These files use .incbin inline assembly to include a binary file.
  593. # Unfortunately ccache does not detect this dependency and caches
  594. # builds even when the binary file has changed.
  595. #
  596. $(BIN)/embedded.% : override CC := env CCACHE_DISABLE=1 $(CC)
  597. $(BIN)/certstore.% : override CC := env CCACHE_DISABLE=1 $(CC)
  598. $(BIN)/privkey.% : override CC := env CCACHE_DISABLE=1 $(CC)
  599. # Debug message autocolourisation range
  600. #
  601. DBGCOL_LIST := $(BIN)/.dbgcol.list
  602. ifeq ($(wildcard $(DBGCOL_LIST)),)
  603. DBGCOL_OLD := <invalid>
  604. else
  605. DBGCOL_OLD := $(shell cat $(DBGCOL_LIST))
  606. endif
  607. ifneq ($(DBGCOL_OLD),$(DBGCOL))
  608. $(shell $(ECHO) "$(DBGCOL)" > $(DBGCOL_LIST))
  609. endif
  610. $(DBGCOL_LIST) : $(MAKEDEPS)
  611. VERYCLEANUP += $(DBGCOL_LIST)
  612. DBGCOL_COLOURS := $(subst -, ,$(DBGCOL))
  613. DBGCOL_MIN := $(word 1,$(DBGCOL_COLOURS))
  614. DBGCOL_MAX := $(word 2,$(DBGCOL_COLOURS))
  615. debug_DEPS += $(DBGCOL_LIST)
  616. CFLAGS_debug += $(if $(DBGCOL_MIN),-DDBGCOL_MIN=$(DBGCOL_MIN))
  617. CFLAGS_debug += $(if $(DBGCOL_MAX),-DDBGCOL_MAX=$(DBGCOL_MAX))
  618. # We automatically generate rules for any file mentioned in AUTO_SRCS
  619. # using the following set of templates. We use $(eval ...) if
  620. # available, otherwise we generate separate Makefile fragments and
  621. # include them.
  622. # deps_template : generate dependency list for a given source file
  623. #
  624. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  625. #
  626. define deps_template_file
  627. $(call deps_template_parts,$(1),$(subst .,,$(suffix $(1))),$(basename $(notdir $(1))))
  628. endef
  629. #
  630. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  631. # $(2) is the source type (e.g. "c")
  632. # $(3) is the source base name (e.g. "rtl8139")
  633. #
  634. define deps_template_parts
  635. @$(ECHO) " [DEPS] $(1)"
  636. @$(MKDIR) -p $(BIN)/deps/$(dir $(1))
  637. $(Q)$(CPP) $(CFLAGS) $(CFLAGS_$(2)) $(CFLAGS_$(3)) -DOBJECT=$(3) \
  638. -Wno-error -M $(1) -MG -MP | \
  639. sed 's/\.o\s*:/_DEPS +=/' > $(BIN)/deps/$(1).d
  640. $(Q)$(if $(findstring drivers/,$(1)),\
  641. $(PERL) $(PARSEROM) $(1) >> $(BIN)/deps/$(1).d)
  642. endef
  643. # rules_template : generate rules for a given source file
  644. #
  645. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  646. #
  647. define rules_template
  648. $(call rules_template_parts,$(1),$(subst .,,$(suffix $(1))),$(basename $(notdir $(1))))
  649. endef
  650. #
  651. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  652. # $(2) is the source type (e.g. "c")
  653. # $(3) is the source base name (e.g. "rtl8139")
  654. #
  655. define rules_template_parts
  656. $$(BIN)/$(3).o : $(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(3)_DEPS)
  657. $$(QM)$(ECHO) " [BUILD] $$@"
  658. $$(RULE_$(2))
  659. BOBJS += $$(BIN)/$(3).o
  660. $(foreach TGT,$(DEBUG_TARGETS),$(if $(RULE_$(2)_to_$(TGT)),$(NEWLINE)$(call rules_template_target,$(1),$(2),$(3),$(TGT))))
  661. $$(BIN)/deps/$(1).d : $$($(3)_DEPS)
  662. TAGS : $$($(3)_DEPS)
  663. endef
  664. #
  665. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  666. # $(2) is the source type (e.g. "c")
  667. # $(3) is the source base name (e.g. "rtl8139")
  668. # $(4) is the destination type (e.g. "dbg%.o")
  669. #
  670. define rules_template_target
  671. $$(BIN)/$(3).$(4) : $(1) $$(MAKEDEPS) $$(POST_O_DEPS) $$($(3)_DEPS)
  672. $$(QM)$(ECHO) " [BUILD] $$@"
  673. $$(RULE_$(2)_to_$(4))
  674. $(TGT)_OBJS += $$(BIN)/$(3).$(4)
  675. endef
  676. #
  677. # $(1) is the full path to the source file (e.g. "drivers/net/rtl8139.c")
  678. #
  679. define rules_template_file
  680. @$(ECHO) " [RULES] $(1)"
  681. @$(MKDIR) -p $(BIN)/rules/$(dir $(1))
  682. @$(ECHO_E) '$(subst $(NEWLINE),\n,$(call rules_template,$(1)))' \
  683. > $(BIN)/rules/$(1).r
  684. endef
  685. # Generate the dependency files
  686. #
  687. $(BIN)/deps/%.d : % $(MAKEDEPS) $(PARSEROM)
  688. $(call deps_template_file,$<)
  689. # Calculate list of dependency files
  690. #
  691. AUTO_DEPS = $(patsubst %,$(BIN)/deps/%.d,$(AUTO_SRCS))
  692. autodeps :
  693. @$(ECHO) $(AUTO_DEPS)
  694. VERYCLEANUP += $(BIN)/deps
  695. # Include dependency files
  696. #
  697. ifdef NEED_DEPS
  698. ifneq ($(AUTO_DEPS),)
  699. -include $(AUTO_DEPS)
  700. endif
  701. endif
  702. # Generate the rules files
  703. #
  704. $(BIN)/rules/%.r : % $(MAKEDEPS)
  705. $(call rules_template_file,$<)
  706. # Calculate list of rules files
  707. #
  708. AUTO_RULES = $(patsubst %,$(BIN)/rules/%.r,$(AUTO_SRCS))
  709. autorules :
  710. @$(ECHO) $(AUTO_RULES)
  711. VERYCLEANUP += $(BIN)/rules
  712. # Evaluate rules (or include rules files)
  713. #
  714. ifdef NEED_DEPS
  715. ifneq ($(AUTO_RULES),)
  716. ifneq ($(HAVE_EVAL),)
  717. $(foreach SRC,$(AUTO_SRCS),$(eval $(call rules_template,$(SRC))))
  718. else
  719. -include $(AUTO_RULES)
  720. endif
  721. endif
  722. endif
  723. # The following variables are created by the rules files
  724. #
  725. bobjs :
  726. @$(ECHO) $(BOBJS)
  727. drivers :
  728. @$(ECHO) $(DRIVERS)
  729. .PHONY : drivers
  730. roms :
  731. @$(ECHO) $(ROMS)
  732. # Generate error usage information
  733. #
  734. $(BIN)/%.einfo : $(BIN)/%.o
  735. $(QM)$(ECHO) " [EINFO] $@"
  736. $(Q)$(OBJCOPY) -O binary -j .einfo --set-section-flags .einfo=alloc \
  737. $< $@
  738. EINFOS := $(patsubst $(BIN)/%.o,$(BIN)/%.einfo,$(BOBJS))
  739. $(BIN)/errors : $(EINFOS) $(EINFO)
  740. $(QM)$(ECHO) " [EINFO] $@"
  741. $(Q)$(EINFO) $(EINFOS) | sort > $@
  742. CLEANUP += $(BIN)/errors # Doesn't match the $(BIN)/*.* pattern
  743. # Generate the NIC file from the parsed source files. The NIC file is
  744. # only for rom-o-matic.
  745. #
  746. $(BIN)/NIC : $(AUTO_DEPS)
  747. @$(ECHO) '# This is an automatically generated file, do not edit' > $@
  748. @$(ECHO) '# It does not affect anything in the build, ' \
  749. 'it is only for rom-o-matic' >> $@
  750. @$(ECHO) >> $@
  751. @perl -ne 'chomp; print "$$1\n" if /\# NIC\t(.*)$$/' $^ >> $@
  752. CLEANUP += $(BIN)/NIC # Doesn't match the $(BIN)/*.* pattern
  753. # Analyse a target name (e.g. "bin/dfe538--prism2_pci.rom.tmp") and
  754. # derive the variables:
  755. #
  756. # TGT_ELEMENTS : the elements of the target (e.g. "dfe538 prism2_pci")
  757. # TGT_PREFIX : the prefix type (e.g. "pcirom")
  758. # TGT_DRIVERS : the driver for each element (e.g. "rtl8139 prism2_pci")
  759. # TGT_ROM_NAME : the ROM name (e.g. "dfe538")
  760. #
  761. DRIVERS_ipxe = $(DRIVERS)
  762. CARD_DRIVER = $(firstword $(DRIVER_$(1)) $(1))
  763. TGT_ELEMENTS = $(subst --, ,$(firstword $(subst ., ,$(notdir $@))))
  764. TGT_ROM_NAME = $(firstword $(TGT_ELEMENTS))
  765. TGT_DRIVERS = $(strip $(if $(DRIVERS_$(TGT_ROM_NAME)), \
  766. $(DRIVERS_$(TGT_ROM_NAME)), \
  767. $(foreach TGT_ELEMENT,$(TGT_ELEMENTS), \
  768. $(call CARD_DRIVER,$(TGT_ELEMENT))) ))
  769. TGT_PREFIX_NAME = $(word 2,$(subst ., ,$(notdir $@)))
  770. TGT_PREFIX = $(strip $(if $(filter rom,$(TGT_PREFIX_NAME)), \
  771. $(ROM_TYPE_$(TGT_ROM_NAME))rom, \
  772. $(TGT_PREFIX_NAME)))
  773. # Look up ROM IDs for the current target
  774. # (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the variables:
  775. #
  776. # TGT_PCI_VENDOR : the PCI vendor ID (e.g. "0x1186")
  777. # TGT_PCI_DEVICE : the PCI device ID (e.g. "0x1300")
  778. #
  779. TGT_PCI_VENDOR = $(PCI_VENDOR_$(TGT_ROM_NAME))
  780. TGT_PCI_DEVICE = $(PCI_DEVICE_$(TGT_ROM_NAME))
  781. # Calculate link-time options for the current target
  782. # (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the variables:
  783. #
  784. # TGT_LD_DRIVERS : symbols to require in order to drag in the relevant drivers
  785. # (e.g. "obj_rtl8139 obj_prism2_pci")
  786. # TGT_LD_IDS : symbols to define in order to fill in ID structures in the
  787. # ROM header (e.g."pci_vendor_id=0x1186 pci_device_id=0x1300")
  788. #
  789. TGT_LD_DRIVERS = $(subst -,_,$(patsubst %,obj_%,$(TGT_DRIVERS)))
  790. TGT_LD_IDS = pci_vendor_id=$(firstword $(TGT_PCI_VENDOR) 0) \
  791. pci_device_id=$(firstword $(TGT_PCI_DEVICE) 0)
  792. TGT_LD_ENTRY = _$(TGT_PREFIX)_start
  793. # Calculate linker flags based on link-time options for the current
  794. # target type (e.g. "bin/dfe538--prism2_pci.rom.tmp") and derive the
  795. # variables:
  796. #
  797. # TGT_LD_FLAGS : target-specific flags to pass to linker (e.g.
  798. # "-u obj_zpciprefix -u obj_rtl8139 -u obj_prism2_pci
  799. # --defsym pci_vendor=0x1186 --defsym pci_device=0x1300")
  800. #
  801. TGT_LD_FLAGS = $(foreach SYM,$(TGT_LD_ENTRY) $(TGT_LD_DRIVERS) obj_config,\
  802. -u $(SYM) --defsym check_$(SYM)=$(SYM) ) \
  803. $(patsubst %,--defsym %,$(TGT_LD_IDS)) \
  804. -e $(TGT_LD_ENTRY)
  805. # Calculate list of debugging versions of objects to be included in
  806. # the target.
  807. #
  808. DEBUG_LIST = $(subst $(COMMA), ,$(DEBUG))
  809. DEBUG_OBJ_LEVEL = $(firstword $(word 2,$(subst :, ,$(1))) 1)
  810. DEBUG_OBJ_BASE = $(word 1,$(subst :, ,$(1))).dbg$(call DEBUG_OBJ_LEVEL,$(1))
  811. DEBUG_OBJ = $(BIN)/$(call DEBUG_OBJ_BASE,$(1)).o
  812. DEBUG_ORIG_OBJ = $(BIN)/$(word 1,$(subst :, ,$(1))).o
  813. DEBUG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_OBJ,$(D)))
  814. DEBUG_ORIG_OBJS = $(foreach D,$(DEBUG_LIST),$(call DEBUG_ORIG_OBJ,$(D)))
  815. BLIB_OBJS = $(DEBUG_OBJS) $(filter-out $(DEBUG_ORIG_OBJS),$(BOBJS))
  816. # Print out all derived information for a given target.
  817. #
  818. $(BIN)/%.info :
  819. @$(ECHO) 'Elements : $(TGT_ELEMENTS)'
  820. @$(ECHO) 'Prefix : $(TGT_PREFIX)'
  821. @$(ECHO) 'Drivers : $(TGT_DRIVERS)'
  822. @$(ECHO) 'ROM name : $(TGT_ROM_NAME)'
  823. @$(ECHO)
  824. @$(ECHO) 'PCI vendor : $(TGT_PCI_VENDOR)'
  825. @$(ECHO) 'PCI device : $(TGT_PCI_DEVICE)'
  826. @$(ECHO)
  827. @$(ECHO) 'LD driver symbols : $(TGT_LD_DRIVERS)'
  828. @$(ECHO) 'LD ID symbols : $(TGT_LD_IDS)'
  829. @$(ECHO) 'LD entry point : $(TGT_LD_ENTRY)'
  830. @$(ECHO)
  831. @$(ECHO) 'LD target flags : $(TGT_LD_FLAGS)'
  832. @$(ECHO)
  833. @$(ECHO) 'Debugging objects : $(DEBUG_OBJS)'
  834. @$(ECHO) 'Replaced objects : $(DEBUG_ORIG_OBJS)'
  835. # List of objects included in the last build of blib. This is needed
  836. # in order to correctly rebuild blib whenever the list of objects
  837. # changes.
  838. #
  839. BLIB_LIST := $(BIN)/.blib.list
  840. ifeq ($(wildcard $(BLIB_LIST)),)
  841. BLIB_OBJS_OLD := <invalid>
  842. else
  843. BLIB_OBJS_OLD := $(shell cat $(BLIB_LIST))
  844. endif
  845. ifneq ($(BLIB_OBJS_OLD),$(BLIB_OBJS))
  846. $(shell $(ECHO) "$(BLIB_OBJS)" > $(BLIB_LIST))
  847. endif
  848. $(BLIB_LIST) : $(MAKEDEPS)
  849. VERYCLEANUP += $(BLIB_LIST)
  850. # Library of all objects
  851. #
  852. BLIB = $(BIN)/blib.a
  853. $(BLIB) : $(BLIB_OBJS) $(BLIB_LIST) $(MAKEDEPS)
  854. $(Q)$(RM) $(BLIB)
  855. $(QM)$(ECHO) " [AR] $@"
  856. $(Q)$(AR) r $@ $(sort $(BLIB_OBJS))
  857. $(Q)$(RANLIB) $@
  858. blib : $(BLIB)
  859. # Command to generate build ID. Must be unique for each $(BIN)/%.tmp,
  860. # even within the same build run.
  861. #
  862. BUILD_ID_CMD := perl -e 'printf "0x%08x", int ( rand ( 0xffffffff ) );'
  863. # Build timestamp
  864. #
  865. BUILD_TIMESTAMP := $(shell date +%s)
  866. # Build version
  867. #
  868. GIT_INDEX := $(if $(GITVERSION),$(if $(wildcard ../.git/index),../.git/index))
  869. $(BIN)/version.%.o : core/version.c $(MAKEDEPS) $(GIT_INDEX)
  870. $(QM)$(ECHO) " [VERSION] $@"
  871. $(Q)$(COMPILE_c) -DBUILD_NAME="\"$*\"" \
  872. -DVERSION_MAJOR=$(VERSION_MAJOR) \
  873. -DVERSION_MINOR=$(VERSION_MINOR) \
  874. -DVERSION_PATCH=$(VERSION_PATCH) \
  875. -DVERSION="\"$(VERSION)\"" \
  876. -c $< -o $@
  877. # Build an intermediate object file from the objects required for the
  878. # specified target.
  879. #
  880. $(BIN)/%.tmp : $(BIN)/version.%.o $(BLIB) $(MAKEDEPS) $(LDSCRIPT)
  881. $(QM)$(ECHO) " [LD] $@"
  882. $(Q)$(LD) $(LDFLAGS) -T $(LDSCRIPT) $(TGT_LD_FLAGS) $< $(BLIB) -o $@ \
  883. --defsym _build_id=`$(BUILD_ID_CMD)` \
  884. --defsym _build_timestamp=$(BUILD_TIMESTAMP) \
  885. -Map $(BIN)/$*.tmp.map
  886. $(Q)$(OBJDUMP) -ht $@ | $(PERL) $(SORTOBJDUMP) >> $(BIN)/$*.tmp.map
  887. # Keep intermediate object file (useful for debugging)
  888. .PRECIOUS : $(BIN)/%.tmp
  889. # Show a linker map for the specified target
  890. #
  891. $(BIN)/%.map : $(BIN)/%.tmp
  892. @less $(BIN)/$*.tmp.map
  893. # Get objects list for the specified target
  894. #
  895. define objs_list
  896. $(sort $(foreach OBJ_SYMBOL,\
  897. $(filter obj_%,$(shell $(NM) $(1) | cut -d" " -f3)),\
  898. $(patsubst obj_%,%,$(OBJ_SYMBOL))))
  899. endef
  900. $(BIN)/%.objs : $(BIN)/%.tmp
  901. $(Q)$(ECHO) $(call objs_list,$<)
  902. $(BIN)/%.sizes : $(BIN)/%.tmp
  903. $(Q)$(SIZE) -t $(foreach OBJ,$(call objs_list,$<),$(wildcard $(BIN)/$(subst _,?,$(OBJ)).o)) | \
  904. sort -g
  905. # Get dependency list for the specified target
  906. #
  907. define deps_list
  908. $(sort $(foreach OBJ,$(call objs_list,$(1)),$($(OBJ)_DEPS)))
  909. endef
  910. $(BIN)/%.deps : $(BIN)/%.tmp
  911. $(Q)$(ECHO) $(call deps_list,$<)
  912. # Get unneeded source files for the specified target
  913. #
  914. define nodeps_list
  915. $(sort $(filter-out $(call deps_list,$(1)),\
  916. $(foreach BOBJ,$(BOBJS),\
  917. $($(basename $(notdir $(BOBJ)))_DEPS))))
  918. endef
  919. $(BIN)/%.nodeps : $(BIN)/%.tmp
  920. $(Q)$(ECHO) $(call nodeps_list,$<)
  921. # Get licensing verdict for the specified target
  922. #
  923. define licensable_deps_list
  924. $(filter-out config/local/%.h,\
  925. $(filter-out $(BIN)/.%.list,\
  926. $(call deps_list,$(1))))
  927. endef
  928. define unlicensed_deps_list
  929. $(shell grep -L FILE_LICENCE $(call licensable_deps_list,$(1)))
  930. endef
  931. define licence_list
  932. $(sort $(foreach LICENCE,\
  933. $(filter __licence__%,$(shell $(NM) $(1) | cut -d" " -f3)),\
  934. $(word 2,$(subst __, ,$(LICENCE)))))
  935. endef
  936. $(BIN)/%.licence_list : $(BIN)/%.tmp
  937. $(Q)$(ECHO) $(call licence_list,$<)
  938. $(BIN)/%.licence : $(BIN)/%.tmp
  939. $(QM)$(ECHO) " [LICENCE] $@"
  940. $(Q)$(if $(strip $(call unlicensed_deps_list,$<)),\
  941. echo -n "Unable to determine licence because the following " ;\
  942. echo "files are missing a licence declaration:" ;\
  943. echo $(call unlicensed_deps_list,$<);\
  944. exit 1,\
  945. $(PERL) $(LICENCE) $(call licence_list,$<))
  946. # Extract compression information from intermediate object file
  947. #
  948. $(BIN)/%.zinfo : $(BIN)/%.tmp
  949. $(QM)$(ECHO) " [ZINFO] $@"
  950. $(Q)$(OBJCOPY) -O binary -j .zinfo $< $@
  951. # Build raw binary file from intermediate object file
  952. #
  953. $(BIN)/%.bin : $(BIN)/%.tmp
  954. $(QM)$(ECHO) " [BIN] $@"
  955. $(Q)$(OBJCOPY) -O binary -R .zinfo $< $@
  956. # Compress raw binary file
  957. #
  958. $(BIN)/%.zbin : $(BIN)/%.bin $(BIN)/%.zinfo $(ZBIN)
  959. $(QM)$(ECHO) " [ZBIN] $@"
  960. $(Q)$(ZBIN) $(BIN)/$*.bin $(BIN)/$*.zinfo > $@
  961. # Rules for each media format. These are generated and placed in an
  962. # external Makefile fragment. We could do this via $(eval ...), but
  963. # that would require make >= 3.80.
  964. #
  965. # Note that there's an alternative way to generate most .rom images:
  966. # they can be copied from their 'master' ROM image using cp and
  967. # reprocessed with makerom to add the PCI IDs and ident string. The
  968. # relevant rule would look something like:
  969. #
  970. # $(BIN)/dfe538%rom : $(BIN)/rtl8139%rom
  971. # cat $< $@
  972. # $(FINALISE_rom)
  973. #
  974. # You can derive the ROM/driver relationships using the variables
  975. # DRIVER_<rom> and/or ROMS_<driver>.
  976. #
  977. # We don't currently do this, because (a) it would require generating
  978. # yet more Makefile fragments (since you need a rule for each ROM in
  979. # ROMS), and (b) the linker is so fast that it probably wouldn't make
  980. # much difference to the overall build time.
  981. # Add NON_AUTO_MEDIA to the media list, so that they show up in the
  982. # output of "make"
  983. #
  984. MEDIA += $(NON_AUTO_MEDIA)
  985. media :
  986. @$(ECHO) $(MEDIA)
  987. AUTO_MEDIA = $(filter-out $(NON_AUTO_MEDIA),$(MEDIA))
  988. automedia :
  989. @$(ECHO) $(AUTO_MEDIA)
  990. # media_template : create media rules
  991. #
  992. # $(1) is the media name (e.g. "rom")
  993. #
  994. define media_template
  995. $(if $(filter $(1),$(AUTO_MEDIA)),$(call auto_media_template,$(1)))
  996. LIST_$(1) := $$(if $$(LIST_NAME_$(1)),$$($$(LIST_NAME_$(1))),$$(DRIVERS))
  997. ALL_$(1) = $$(foreach ITEM,$$(LIST_$(1)),$$(BIN)/$$(ITEM).$(1))
  998. $$(BIN)/all$(1)s : $$(ALL_$(1))
  999. $$(BIN)/allall : $$(BIN)/all$(1)s
  1000. all$(1)s : $$(BIN)/all$(1)s
  1001. allall : $$(BIN)/allall
  1002. endef
  1003. #
  1004. # $(1) is the media name (e.g. "rom")
  1005. #
  1006. define auto_media_template
  1007. $$(BIN)/%.$(1) : $$(BIN)/%.$(1).zbin
  1008. $$(QM)echo " [FINISH] $$@"
  1009. $$(Q)$$(CP) $$< $$@
  1010. $$(Q)$$(if $$(PAD_$(1)),$$(PAD_$(1)) $$@)
  1011. $$(Q)$$(if $$(FINALISE_$(1)),$$(FINALISE_$(1)) $$@)
  1012. endef
  1013. #
  1014. # $(1) is the media name (e.g. "rom")
  1015. #
  1016. define media_template_file
  1017. @$(ECHO) " [MEDIARULES] $(1)"
  1018. @$(MKDIR) -p $(BIN)/rules/$(dir $(1))
  1019. @$(ECHO_E) '$(subst $(NEWLINE),\n,$(call media_template,$(1)))' \
  1020. > $(BIN)/rules/$(1).media.r
  1021. endef
  1022. # Generate media rules files
  1023. #
  1024. $(BIN)/rules/%.media.r : $(MAKEDEPS)
  1025. $(call media_template_file,$*)
  1026. # Calculate list of media rules files
  1027. #
  1028. MEDIA_RULES = $(patsubst %,$(BIN)/rules/%.media.r,$(MEDIA))
  1029. mediarules :
  1030. @$(ECHO) $(MEDIA_RULES)
  1031. # Evaluate media rules (or include media rules files)
  1032. #
  1033. ifdef NEED_DEPS
  1034. ifneq ($(MEDIA_RULES),)
  1035. ifneq ($(HAVE_EVAL),)
  1036. $(foreach MEDIUM,$(MEDIA),$(eval $(call media_template,$(MEDIUM))))
  1037. else
  1038. -include $(MEDIA_RULES)
  1039. endif
  1040. endif
  1041. endif
  1042. # Alias for ipxe.%
  1043. #
  1044. $(BIN)/etherboot.% : $(BIN)/ipxe.%
  1045. ln -sf $(notdir $<) $@
  1046. endif # defined(BIN)
  1047. ###############################################################################
  1048. #
  1049. # The compression utilities
  1050. #
  1051. ZBIN_LDFLAGS := -llzma
  1052. $(ZBIN) : util/zbin.c $(MAKEDEPS)
  1053. $(QM)$(ECHO) " [HOSTCC] $@"
  1054. $(Q)$(HOST_CC) $(HOST_CFLAGS) $< $(ZBIN_LDFLAGS) -o $@
  1055. CLEANUP += $(ZBIN)
  1056. ###############################################################################
  1057. #
  1058. # The EFI image converter
  1059. #
  1060. ELF2EFI_CFLAGS := -I$(BINUTILS_DIR)/include -I$(BFD_DIR)/include \
  1061. -I$(ZLIB_DIR)/include -idirafter include
  1062. ELF2EFI_LDFLAGS := -L$(BINUTILS_DIR)/lib -L$(BFD_DIR)/lib -L$(ZLIB_DIR)/lib \
  1063. -lbfd -ldl -liberty -lz -Wl,--no-warn-search-mismatch
  1064. $(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
  1065. $(QM)$(ECHO) " [HOSTCC] $@"
  1066. $(Q)$(HOST_CC) $(HOST_CFLAGS) $(ELF2EFI_CFLAGS) -DEFI_TARGET_IA32 $< \
  1067. $(ELF2EFI_LDFLAGS) -o $@
  1068. CLEANUP += $(ELF2EFI32)
  1069. $(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
  1070. $(QM)$(ECHO) " [HOSTCC] $@"
  1071. $(Q)$(HOST_CC) $(HOST_CFLAGS) $(ELF2EFI_CFLAGS) -DEFI_TARGET_X64 $< \
  1072. $(ELF2EFI_LDFLAGS) -o $@
  1073. CLEANUP += $(ELF2EFI64)
  1074. $(EFIROM) : util/efirom.c $(MAKEDEPS)
  1075. $(QM)$(ECHO) " [HOSTCC] $@"
  1076. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1077. CLEANUP += $(EFIROM)
  1078. $(EFIFATBIN) : util/efifatbin.c $(MAKEDEPS)
  1079. $(QM)$(ECHO) " [HOSTCC] $@"
  1080. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1081. CLEANUP += $(EFIFATBIN)
  1082. ###############################################################################
  1083. #
  1084. # The ICC fixup utility
  1085. #
  1086. $(ICCFIX) : util/iccfix.c $(MAKEDEPS)
  1087. $(QM)$(ECHO) " [HOSTCC] $@"
  1088. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1089. CLEANUP += $(ICCFIX)
  1090. ###############################################################################
  1091. #
  1092. # The error usage information utility
  1093. #
  1094. $(EINFO) : util/einfo.c $(MAKEDEPS)
  1095. $(QM)$(ECHO) " [HOSTCC] $@"
  1096. $(Q)$(HOST_CC) $(HOST_CFLAGS) -idirafter include -o $@ $<
  1097. CLEANUP += $(EINFO)
  1098. ###############################################################################
  1099. #
  1100. # Local configs
  1101. #
  1102. CONFIG_HEADERS := $(patsubst config/%,%,$(wildcard config/*.h))
  1103. CONFIG_LOCAL_HEADERS := $(foreach HEADER,$(CONFIG_HEADERS),\
  1104. config/local/$(HEADER))
  1105. $(CONFIG_LOCAL_HEADERS) :
  1106. $(Q)$(TOUCH) $@
  1107. .PRECIOUS : $(CONFIG_LOCAL_HEADERS)
  1108. ifneq ($(CONFIG),)
  1109. CONFIG_LOCAL_NAMED_HEADERS := $(foreach HEADER,$(CONFIG_HEADERS),\
  1110. config/local/$(CONFIG)/$(HEADER))
  1111. $(CONFIG_LOCAL_NAMED_HEADERS) :
  1112. $(Q)$(MKDIR) -p $(dir $@)
  1113. $(Q)$(TOUCH) $@
  1114. .PRECIOUS : $(CONFIG_LOCAL_NAMED_HEADERS)
  1115. endif
  1116. ###############################################################################
  1117. #
  1118. # Build the TAGS file(s) for emacs
  1119. #
  1120. TAGS :
  1121. ctags -e -R -f $@ --exclude=bin
  1122. CLEANUP += TAGS
  1123. ###############################################################################
  1124. #
  1125. # Force rebuild for any given target
  1126. #
  1127. %.rebuild :
  1128. rm -f $*
  1129. $(Q)$(MAKE) $*
  1130. ###############################################################################
  1131. #
  1132. # Symbol table checks
  1133. #
  1134. ifdef BIN
  1135. SYMTAB = $(BIN)/symtab
  1136. $(SYMTAB) : $(BLIB)
  1137. $(OBJDUMP) -w -t $< > $@
  1138. CLEANUP += $(BIN)/symtab
  1139. symcheck : $(SYMTAB)
  1140. $(PERL) $(SYMCHECK) $<
  1141. endif # defined(BIN)
  1142. ###############################################################################
  1143. #
  1144. # Build bochs symbol table
  1145. #
  1146. ifdef BIN
  1147. $(BIN)/%.bxs : $(BIN)/%.tmp
  1148. $(NM) $< | cut -d" " -f1,3 > $@
  1149. endif # defined(BIN)
  1150. ###############################################################################
  1151. #
  1152. # Documentation
  1153. #
  1154. ifdef BIN
  1155. $(BIN)/doxygen.cfg : doxygen.cfg $(MAKEDEPS)
  1156. $(Q)$(PERL) -pe 's{\@SRCDIRS\@}{$(SRCDIRS)}; ' \
  1157. -e 's{\@INCDIRS\@}{$(filter-out .,$(INCDIRS))}; ' \
  1158. -e 's{\@BIN\@}{$(BIN)}; ' \
  1159. -e 's{\@ARCH\@}{$(ARCH)}; ' \
  1160. $< > $@
  1161. $(BIN)/doc : $(BIN)/doxygen.cfg
  1162. $(Q)$(DOXYGEN) $<
  1163. .PHONY : $(BIN)/doc
  1164. doc : $(BIN)/doc
  1165. doc-clean :
  1166. $(Q)$(RM) -r $(BIN)/doc
  1167. VERYCLEANUP += $(BIN)/doc
  1168. docview :
  1169. @[ -f $(BIN)/doc/html/index.html ] || $(MAKE) $(BIN)/doc
  1170. @if [ -n "$$BROWSER" ] ; then \
  1171. ( $$BROWSER $(BIN)/doc/html/index.html & ) ; \
  1172. else \
  1173. $(ECHO) "Documentation index in $(BIN)/doc/html/index.html" ; \
  1174. fi
  1175. endif # defined(BIN)
  1176. ###############################################################################
  1177. #
  1178. # Keyboard maps
  1179. #
  1180. hci/keymap/keymap_%.c :
  1181. $(Q)$(PERL) $(GENKEYMAP) $* > $@
  1182. ###############################################################################
  1183. #
  1184. # Force deletion of incomplete targets
  1185. #
  1186. .DELETE_ON_ERROR :
  1187. ###############################################################################
  1188. #
  1189. # Clean-up
  1190. #
  1191. ifeq ($(NUM_BINS),0)
  1192. ALLBINS := bin{,-*}
  1193. CLEANUP := $(patsubst $(BIN)/%,$(ALLBINS)/%,$(CLEANUP))
  1194. VERYCLEANUP := $(patsubst $(BIN)/%,$(ALLBINS)/%,$(VERYCLEANUP))
  1195. endif
  1196. clean :
  1197. $(RM) $(CLEANUP)
  1198. veryclean : clean
  1199. $(RM) -r $(VERYCLEANUP)