Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. SHELL := /bin/sh
  2. MAKEFLAGS += -rR
  3. MAKEFLAGS += --no-print-directory
  4. .PHONY: all
  5. all: x15
  6. VERSION = 0.1
  7. export VERSION
  8. COMMA := ,
  9. ifndef V
  10. V := 0
  11. endif
  12. # Use callable variables so that commands can be split into multiple
  13. # lines, but produce a single line when echoed. This makes copying
  14. # commands easy. It also makes them somewhat self-describing.
  15. ifeq ($(V),0)
  16. Q := @
  17. # $(call xbuild_action_print,<action_short_name>,<target>)
  18. define xbuild_action_print
  19. @printf " %-7s %s\n" $(1) $(2)
  20. @
  21. endef
  22. else ifneq ($(V),1)
  23. $(error invalid value for V)
  24. endif
  25. export Q
  26. # $(call xbuild_action_mkdir,<target>)
  27. define xbuild_action_mkdir
  28. $(Q)mkdir -p $(dir $(1))
  29. endef
  30. # $(call xbuild_action,<action>,<target>)
  31. define xbuild_action
  32. $(call xbuild_action_mkdir,$(2))
  33. $(call xbuild_action_print,$(1),$(2))
  34. endef
  35. define xbuild_kconfig_invoke
  36. $(Q)$(MAKE) -f $(SRCDIR)/$(KCONFIG_PATH)/Makefile $@
  37. endef
  38. define xbuild_gen_autoconf_h
  39. $(call xbuild_action,GEN,$@)cat $< \
  40. | sed -e 's/^\([^#]\)/#define \1/g' \
  41. -e 's/=/ /' \
  42. | grep '^#define' > $@
  43. endef
  44. # $(call xbuild_check_cc_option,<option>)
  45. define xbuild_check_cc_option
  46. $(shell printf "int main(void){ return 0; }\n" \
  47. | $(CC) -Wall -Werror -x c $(1) -c - -o /dev/null 2> /dev/null \
  48. && printf -- "%s" $(1))
  49. endef
  50. # $(call xbuild_replace_source_suffix,<suffix>,<file_names>)
  51. define xbuild_replace_source_suffix
  52. $(sort $(patsubst %.c,%.$(1),$(filter %.c,$(2))) \
  53. $(patsubst %.S,%.$(1),$(filter %.S,$(2))))
  54. endef
  55. define xbuild_compile
  56. $(call xbuild_action,CC,$@) \
  57. $(COMPILE) -MMD -MP -c -o $@ $<
  58. endef
  59. define xbuild_gen_linker_script
  60. $(call xbuild_action,LDS,$@) \
  61. $(CPP) $(XBUILD_CPPFLAGS) -MMD -MP \
  62. -MF $@.d -MT $@ -P -o $@ $<
  63. endef
  64. # $(call xbuild_link,<objects>)
  65. define xbuild_link
  66. $(call xbuild_action,LD,$@) \
  67. $(COMPILE) -o $@ $(1) $(XBUILD_LDFLAGS)
  68. endef
  69. XBUILD_GEN_SYMTAB = $(SRCDIR)/tools/gen_symtab.py
  70. XBUILD_GEN_SYMTAB_DEPS = $(XBUILD_GEN_SYMTAB)
  71. XBUILD_GEN_UNWIND = $(SRCDIR)/tools/gen_dwarf.py
  72. XBUILD_GEN_UNWIND_DEPS = $(XBUILD_GEN_UNWIND)
  73. # $(call xbuild_gen_symtab)
  74. define xbuild_gen_symtab
  75. $(call xbuild_action,GEN,$@) \
  76. $(NM) -S -n $< | $(XBUILD_GEN_SYMTAB) > $@
  77. endef
  78. define xbuild_gen_unwind
  79. $(call xbuild_action,GEN,$@) \
  80. $(XBUILD_GEN_UNWIND) $(x15_PRELIM) > $@
  81. endef
  82. define xbuild_clean
  83. $(Q)rm -f x15 $(x15_PRELIM) \
  84. $(x15_OBJDEPS) $(x15_OBJECTS) \
  85. $(x15_SYMTAB_C) $(x15_SYMTAB_D) $(x15_SYMTAB_O) \
  86. $(x15_UNWIND_C) $(x15_UNWIND_D) $(x15_UNWIND_O) \
  87. $(x15_LDS_D) $(x15_LDS)
  88. endef
  89. define xbuild_distclean
  90. $(clean)
  91. $(Q)rm -f .config \
  92. .config.old \
  93. include/generated/autoconf.h \
  94. include/generated/autoconf.mk
  95. endef
  96. ARCH ?= $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ )
  97. export ARCH
  98. SRCDIR := $(realpath $(dir $(realpath $(firstword $(MAKEFILE_LIST)))))
  99. VPATH := $(SRCDIR)
  100. export SRCDIR VPATH
  101. PREFIX ?= /usr/local
  102. DATAROOTDIR ?= share
  103. # Do not use MAKEFILE_LIST as its value is updated only on inclusion,
  104. # which makes it unsuitable as a rule dependency in most of the file.
  105. #
  106. # These additional Makefiles are included in order.
  107. MAKEFILE_INCLUDES := \
  108. $(SRCDIR)/arch/$(ARCH)/Makefile \
  109. $(SRCDIR)/doc/Makefile \
  110. $(SRCDIR)/kern/Makefile \
  111. $(SRCDIR)/test/Makefile \
  112. $(SRCDIR)/vm/Makefile
  113. ALL_MAKEFILES := $(MAKEFILE_LIST) $(MAKEFILE_INCLUDES)
  114. ifeq ($(words $(MAKECMDGOALS)),0)
  115. else ifeq ($(words $(MAKECMDGOALS)),1)
  116. else
  117. $(error up to one target may be given)
  118. endif
  119. KCONFIG_PATH := tools/kconfig
  120. # Export to Kconfig
  121. export KCONFIG_PATH
  122. HOSTCC := $(if $(shell type gcc 2>/dev/null),gcc,cc)
  123. HOSTCXX = g++
  124. HOSTCFLAGS := -g
  125. HOSTCXXFLAGS := -g
  126. # Export to Kconfig
  127. export HOSTCC HOSTCXX HOSTCFLAGS HOSTCXXFLAGS
  128. BOARDS := $(wildcard $(SRCDIR)/arch/$(ARCH)/configs/*_defconfig)
  129. BOARDS := $(sort $(notdir $(BOARDS)))
  130. .PHONY: help
  131. help:
  132. @printf 'Configuration targets:\n'
  133. @$(Q)$(MAKE) -f $(SRCDIR)/$(KCONFIG_PATH)/Makefile $@
  134. @printf '\n'
  135. @printf 'Cleaning targets:\n'
  136. @printf ' clean - Remove most generated files but keep configuration\n'
  137. @printf ' distclean - Remove all generated files\n'
  138. @printf '\n'
  139. @printf 'Build targets:\n'
  140. @printf ' all - Build all targets marked with [*]\n'
  141. @printf '* x15 - Build the kernel ELF image\n'
  142. @printf '\n'
  143. @printf 'Documentation targets:\n'
  144. @printf '* docs - Build all documentation\n'
  145. $(DOC_HELP)
  146. @printf '\n'
  147. @printf 'Installation targets:\n'
  148. @printf ' install - Install the kernel and documentation\n'
  149. @printf ' install-strip - Same as install but also strip the kernel\n'
  150. @printf ' install-x15 - Install the kernel only\n'
  151. @printf ' install-strip-x15 - Same as install-x15 but also strip the kernel\n'
  152. @printf ' install-docs - Install documentation files\n'
  153. $(DOC_INSTALL_HELP)
  154. @printf '\n'
  155. @printf 'Architecture specific targets ($(ARCH)):\n'
  156. @$(if $(BOARDS), \
  157. $(foreach b, $(BOARDS), \
  158. printf " %-24s - Build for %s\\n" $(b) $(subst _defconfig,,$(b));))
  159. @$(ARCH_HELP)
  160. @printf '\n'
  161. @printf 'Options:\n'
  162. @printf ' make V=0 - Quiet build\n'
  163. @printf ' make V=1 - Verbose build\n'
  164. @printf '\n'
  165. @printf 'Notes:\n'
  166. @printf '- One target at most may be specified.\n'
  167. @printf '- The compiler program and flags may be given at configuration time\n'
  168. @printf ' through the CC and CFLAGS variables respectively.\n'
  169. @printf '- Out-of-tree builds are performed with the following command:\n'
  170. @printf ' make -f path/to/src/Makefile [options] [target]\n'
  171. @printf '- The source directory must be completely clean for reliable out-of-tree builds.\n'
  172. @printf '- Use the DESTDIR and PREFIX variables to control installation, e.g.:\n'
  173. @printf ' make DESTDIR=/stagingroot PREFIX=/usr install\n'
  174. @printf '\n'
  175. @printf 'See README for more information.\n'
  176. # Don't create a %config pattern rule as it would conflict with .config
  177. KCONFIG_TARGETS := config nconfig menuconfig xconfig gconfig \
  178. allnoconfig allyesconfig alldefconfig randconfig \
  179. oldconfig olddefconfig defconfig savedefconfig \
  180. listnewconfig
  181. .PHONY: $(KCONFIG_TARGETS)
  182. $(KCONFIG_TARGETS):
  183. $(call xbuild_kconfig_invoke)
  184. %_defconfig:
  185. $(call xbuild_kconfig_invoke)
  186. include/generated/autoconf.h: .config $(ALL_MAKEFILES)
  187. $(call xbuild_gen_autoconf_h)
  188. -include .config
  189. ifdef CONFIG_COMPILER
  190. # Use printf to remove quotes
  191. CC := $(shell printf -- $(CONFIG_COMPILER))
  192. else
  193. CC := gcc
  194. endif
  195. # The CC variable is used by Kconfig to set the value of CONFIG_COMPILER.
  196. export CC
  197. TOOLCHAIN_NAME = $(shell printf "%s" $(CC) | rev | cut -s -d - -f 2- | rev)
  198. ifneq ($(TOOLCHAIN_NAME),)
  199. TOOLCHAIN_PREFIX = $(TOOLCHAIN_NAME)-
  200. endif
  201. CPP := $(CC) -E
  202. NM := $(TOOLCHAIN_PREFIX)nm
  203. OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
  204. CFLAGS ?= -O2 -g
  205. # Export to CONFIG_CFLAGS
  206. export CFLAGS
  207. XBUILD_CPPFLAGS :=
  208. XBUILD_CFLAGS :=
  209. XBUILD_CPPFLAGS += -pipe
  210. # Do not include headers from the hosted environment, but
  211. # do include headers from the compiler.
  212. XBUILD_CPPFLAGS += -nostdinc
  213. XBUILD_CPPFLAGS += -isystem $(shell $(CC) -print-file-name=include)
  214. XBUILD_CPPFLAGS += -std=gnu11
  215. XBUILD_CPPFLAGS += -ffreestanding
  216. XBUILD_CPPFLAGS += -include $(SRCDIR)/kern/config.h
  217. XBUILD_CPPFLAGS += -include include/generated/autoconf.h
  218. XBUILD_CPPFLAGS += -I$(SRCDIR)
  219. XBUILD_CPPFLAGS += -I$(SRCDIR)/include
  220. XBUILD_CPPFLAGS += -I$(SRCDIR)/arch/$(ARCH)
  221. ifndef CONFIG_ASSERT
  222. XBUILD_CPPFLAGS += -DNDEBUG
  223. endif
  224. XBUILD_CFLAGS += -fsigned-char
  225. XBUILD_CFLAGS += -fno-common
  226. XBUILD_CFLAGS += -fasynchronous-unwind-tables
  227. # XXX Some assemblers consider the / symbol to denote comments. The --divide
  228. # option suppresses that behavior.
  229. XBUILD_CFLAGS += $(call xbuild_check_cc_option,-Wa$(COMMA)--divide)
  230. XBUILD_CFLAGS += -Wall
  231. XBUILD_CFLAGS += -Wextra
  232. XBUILD_CFLAGS += -Wshadow
  233. XBUILD_CFLAGS += -Wstrict-prototypes
  234. # XXX Temporary, until a single solution is adopted to silence these warnings.
  235. XBUILD_CFLAGS += -Wno-unneeded-internal-declaration
  236. ifeq ($(CC),gcc)
  237. XBUILD_CFLAGS += -fcf-protection=none
  238. XBUILD_CFLAGS += -Wno-clobbered
  239. else
  240. XBUILD_CPPFLAGS += -gdwarf-4
  241. endif
  242. XBUILD_CFLAGS += $(call xbuild_check_cc_option,-fno-PIE)
  243. XBUILD_CFLAGS += $(call xbuild_check_cc_option,-Qunused-arguments)
  244. XBUILD_LDFLAGS += -static -nostdlib -lgcc
  245. # Disable the build ID feature of the linker
  246. XBUILD_LDFLAGS += -Wl,--build-id=none
  247. x15_SOURCES-y :=
  248. x15_LDS_S := arch/$(ARCH)/x15.lds.S
  249. # Include the additional Makefiles here, as they may augment the build
  250. # variables.
  251. include $(MAKEFILE_INCLUDES)
  252. ifeq ($(shell grep "^CONFIG_TEST" .config > /dev/null; echo $$?), 0)
  253. x15_SOURCES-y += test/test.c
  254. XBUILD_CPPFLAGS += -DCONFIG_RUN_TEST
  255. endif
  256. # Export to Kconfig.
  257. # Must be defined by the architecture-specific Makefile.
  258. export KCONFIG_DEFCONFIG
  259. ifdef CONFIG_COMPILER_OPTIONS
  260. # Use printf to remove quotes
  261. XBUILD_CFLAGS += $(shell printf -- $(CONFIG_COMPILER_OPTIONS))
  262. endif
  263. COMPILE := $(CC) $(XBUILD_CPPFLAGS) $(XBUILD_CFLAGS)
  264. # Don't change preprocessor and compiler flags from this point
  265. x15_PRELIM := .x15.prelim
  266. x15_SOURCES := $(x15_SOURCES-y)
  267. x15_OBJDEPS := $(call xbuild_replace_source_suffix,d,$(x15_SOURCES))
  268. x15_OBJECTS := $(call xbuild_replace_source_suffix,o,$(x15_SOURCES))
  269. x15_UNWIND_C := .unwind.c
  270. x15_UNWIND_D := $(call xbuild_replace_source_suffix,d,$(x15_UNWIND_C))
  271. x15_UNWIND_O := $(call xbuild_replace_source_suffix,o,$(x15_UNWIND_C))
  272. x15_SYMTAB_C := .symtab.c
  273. x15_SYMTAB_D := $(call xbuild_replace_source_suffix,d,$(x15_SYMTAB_C))
  274. x15_SYMTAB_O := $(call xbuild_replace_source_suffix,o,$(x15_SYMTAB_C))
  275. x15_LDS := $(basename $(x15_LDS_S))
  276. x15_LDS_D := $(x15_LDS).d
  277. XBUILD_LDFLAGS += -Wl,--script=$(x15_LDS)
  278. define gen_sorted_init_ops
  279. $(call xbuild_action,GEN,$@) \
  280. $(SRCDIR)/tools/tsort_init_ops.sh "$(COMPILE)" "$@" $^
  281. endef
  282. .INTERMEDIATE: .x15.sorted_init_ops
  283. .x15.sorted_init_ops: $(filter %.c,$(x15_SOURCES)) include/generated/autoconf.h
  284. $(call gen_sorted_init_ops)
  285. # Adding the sorted initialization operations as a dependency allows checking
  286. # for loops at build time.
  287. x15_DEPS := $(x15_LDS) .x15.sorted_init_ops
  288. # Compiling produces dependency rules as a side-effect. When the dependency
  289. # rules file doesn't exist, the main source file is enough to trigger a
  290. # rebuild. Afterwards, the dependency rules file is included here and the
  291. # rules provide correct incremental compilation.
  292. -include $(x15_OBJDEPS) $(x15_LDS_D)
  293. %.o: %.c include/generated/autoconf.h
  294. $(xbuild_compile)
  295. %.o: %.S include/generated/autoconf.h
  296. $(xbuild_compile)
  297. %.lds: %.lds.S include/generated/autoconf.h
  298. $(xbuild_gen_linker_script)
  299. ifeq ($(CONFIG_SYMTAB),y)
  300. x15_SYMTAB_DEP := $(x15_SYMTAB_C)
  301. x15_SYMTAB_OBJ := $(x15_SYMTAB_O)
  302. else
  303. x15_SYMTAB_DEP :=
  304. x15_SYMTAB_OBJ :=
  305. endif
  306. $(x15_PRELIM): $(x15_OBJECTS) $(x15_DEPS)
  307. $(call xbuild_link,$(x15_OBJECTS))
  308. $(x15_SYMTAB_C): $(x15_PRELIM) $(XBUILD_GEN_SYMTAB_DEPS)
  309. $(call xbuild_gen_symtab)
  310. $(x15_UNWIND_C): $(x15_PRELIM) $(XBUILD_GEN_SYMTAB_DEPS)
  311. $(call xbuild_gen_unwind)
  312. x15: $(x15_PRELIM) $(x15_SYMTAB_OBJ) $(x15_UNWIND_O)
  313. $(call xbuild_link,$(x15_OBJECTS) $(x15_SYMTAB_OBJ) $(x15_UNWIND_O))
  314. .PHONY: install-x15
  315. install-x15:
  316. install -D -m 644 x15 $(DESTDIR)/boot/x15
  317. .PHONY: install-strip-x15
  318. install-strip-x15:
  319. install -s -D -m 644 x15 $(DESTDIR)/boot/x15
  320. .PHONY: install
  321. install: install-x15 install-docs
  322. .PHONY: install-strip
  323. install-strip: install-strip-x15 install-docs
  324. .PHONY: clean
  325. clean: clean-docs
  326. $(Q)$(MAKE) -f $(SRCDIR)/$(KCONFIG_PATH)/Makefile $@
  327. $(call xbuild_clean)
  328. .PHONY: distclean
  329. distclean: clean distclean-docs
  330. $(Q)$(MAKE) -f $(SRCDIR)/$(KCONFIG_PATH)/Makefile $@
  331. $(call xbuild_distclean)