minoca.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. ################################################################################
  2. #
  3. # Copyright (c) 2012 Minoca Corp. All Rights Reserved
  4. #
  5. # Module Name:
  6. #
  7. # OS Project Makefile
  8. #
  9. # Abstract:
  10. #
  11. # This file implements the base Makefile that all other makefiles include.
  12. #
  13. # Author:
  14. #
  15. # Evan Green 26-Jul-2012
  16. #
  17. # Environment:
  18. #
  19. # Build
  20. #
  21. ################################################################################
  22. ##
  23. ## Define the default target.
  24. ##
  25. all:
  26. ##
  27. ## Don't let make use any builtin rules.
  28. ##
  29. .SUFFIXES:
  30. ##
  31. ## Define the architecture and object of the build machine.
  32. ##
  33. OS ?= $(shell uname -s)
  34. BUILD_ARCH = $(shell uname -m)
  35. ifeq ($(BUILD_ARCH), $(filter i686 i586,$(BUILD_ARCH)))
  36. BUILD_ARCH := x86
  37. BUILD_BFD_ARCH := i386
  38. BUILD_OBJ_FORMAT := elf32-i386
  39. ifeq ($(OS),Windows_NT)
  40. BUILD_OBJ_FORMAT := pe-i386
  41. endif
  42. else ifeq ($(BUILD_ARCH), $(filter armv7 armv6,$(BUILD_ARCH)))
  43. BUILD_BFD_ARCH := arm
  44. BUILD_OBJ_FORMAT := elf32-littlearm
  45. else ifeq ($(BUILD_ARCH), x86_64)
  46. BUILD_ARCH := x64
  47. BUILD_OBJ_FORMAT := elf64-x86-64
  48. BUILD_BFD_ARCH := arm
  49. else
  50. $(error Unknown architecture $(BUILD_ARCH))
  51. endif
  52. ##
  53. ## Define build locations.
  54. ##
  55. SRCROOT := $(subst \,/,$(SRCROOT))
  56. OUTROOT := $(SRCROOT)/$(ARCH)$(VARIANT)$(DEBUG)
  57. BINROOT := $(OUTROOT)/bin
  58. TESTBIN := $(OUTROOT)/testbin
  59. OBJROOT := $(OUTROOT)/obj
  60. STRIPPED_DIR := $(BINROOT)/stripped
  61. CURDIR := $(subst \,/,$(CURDIR))
  62. ##
  63. ## If the current directory is not in the object root, then change to the
  64. ## object directory and re-run make from there. The condition is "if removing
  65. ## OBJROOT makes no change".
  66. ##
  67. ifeq ($(CURDIR), $(subst $(OBJROOT),,$(CURDIR)))
  68. THISDIR := $(subst $(SRCROOT)/,,$(CURDIR))
  69. OBJDIR := $(OBJROOT)/$(THISDIR)
  70. MAKETARGET = $(MAKE) --no-print-directory -C $@ -f $(CURDIR)/Makefile \
  71. -I$(CURDIR) $(MAKECMDGOALS) SRCDIR=$(CURDIR)
  72. .PHONY: $(OBJDIR) clean wipe
  73. $(OBJDIR):
  74. +@[ -d $@ ] || mkdir -p $@
  75. +@$(MAKETARGET)
  76. clean:
  77. rm -rf $(OBJROOT)/$(THISDIR)
  78. wipe:
  79. rm -rf $(OBJROOT)
  80. rm -rf $(BINROOT)
  81. rm -rf $(TESTBIN)
  82. Makefile: ;
  83. %.mk :: ;
  84. % :: $(OBJDIR) ; @:
  85. else
  86. THISDIR := $(subst $(OBJROOT)/,,$(CURDIR))
  87. ##
  88. ## VPATH specifies which directories make should look in to find all files.
  89. ## Paths are separated by colons.
  90. ##
  91. VPATH += :$(SRCDIR):
  92. ##
  93. ## Executable variables
  94. ##
  95. CC_FOR_BUILD ?= gcc
  96. AR_FOR_BUILD ?= ar
  97. OBJCOPY_FOR_BUILD ?= objcopy
  98. STRIP_FOR_BUILD ?= strip
  99. ifeq ($(OS), $(filter Windows_NT Minoca,$(OS)))
  100. CECHO_CYAN ?= cecho -fC
  101. else
  102. CECHO_CYAN ?= echo
  103. endif
  104. ifeq (x86, $(ARCH)$(VARIANT))
  105. CC := i686-pc-minoca-gcc
  106. RCC := windres
  107. AR := i686-pc-minoca-ar
  108. OBJCOPY := i686-pc-minoca-objcopy
  109. STRIP := i686-pc-minoca-strip
  110. BFD_ARCH := i386
  111. OBJ_FORMAT := elf32-i386
  112. endif
  113. ifeq (x86q, $(ARCH)$(VARIANT))
  114. CC := i586-pc-minoca-gcc
  115. AR := i586-pc-minoca-ar
  116. OBJCOPY := i586-pc-minoca-objcopy
  117. STRIP := i586-pc-minoca-strip
  118. RCC := windres
  119. BFD_ARCH := i386
  120. OBJ_FORMAT := elf32-i386
  121. endif
  122. ifeq (x64, $(ARCH))
  123. CC := x86_64-pc-minoca-gcc
  124. AR := x86_64-pc-minoca-ar
  125. OBJCOPY := x86_64-pc-minoca-objcopy
  126. STRIP := x86_64-pc-minoca-strip
  127. BFD_ARCH := x86-64
  128. OBJ_FORMAT := elf64-x86-64
  129. endif
  130. ifeq (armv7, $(ARCH))
  131. CC := arm-none-minoca-gcc
  132. AR := arm-none-minoca-ar
  133. OBJCOPY := arm-none-minoca-objcopy
  134. STRIP := arm-none-minoca-strip
  135. BFD_ARCH := arm
  136. OBJ_FORMAT := elf32-littlearm
  137. endif
  138. ifeq (armv6, $(ARCH))
  139. CC := arm-none-minoca-gcc
  140. AR := arm-none-minoca-ar
  141. OBJCOPY := arm-none-minoca-objcopy
  142. STRIP := arm-none-minoca-strip
  143. BFD_ARCH := arm
  144. OBJ_FORMAT := elf32-littlearm
  145. endif
  146. ##
  147. ## These define versioning information for the code.
  148. ##
  149. GEN_VERSION := @echo "Creating - version.h" && $(SRCROOT)/os/tasks/build/print_version.sh
  150. ##
  151. ## Define a file that gets touched to indicate that something has changed and
  152. ## images need to be rebuilt.
  153. ##
  154. LAST_UPDATE_FILE := $(OBJROOT)/last-update
  155. UPDATE_LAST_UPDATE := date > $(LAST_UPDATE_FILE)
  156. ##
  157. ## Includes directory.
  158. ##
  159. INCLUDES += $(SRCROOT)/os/include
  160. ##
  161. ## Define default CFLAGS if none were specified elsewhere.
  162. ##
  163. CFLAGS ?= -Wall -Werror
  164. ifeq ($(DEBUG), chk)
  165. CFLAGS += -O1
  166. else
  167. CFLAGS += -O2 -Wno-unused-but-set-variable
  168. endif
  169. ##
  170. ## Compiler flags
  171. ##
  172. EXTRA_CPPFLAGS += -I $(subst ;, -I ,$(INCLUDES))
  173. ifeq ($(DEBUG), chk)
  174. EXTRA_CPPFLAGS += -DDEBUG=1
  175. else
  176. EXTRA_CPPFLAGS += -DNDEBUG=1
  177. endif
  178. EXTRA_CPPFLAGS_FOR_BUILD := $(EXTRA_CPPFLAGS)
  179. EXTRA_CFLAGS += -fno-builtin -fno-omit-frame-pointer -g -save-temps=obj \
  180. -ffunction-sections -fdata-sections -fvisibility=hidden
  181. EXTRA_CFLAGS_FOR_BUILD := $(EXTRA_CFLAGS)
  182. EXTRA_CFLAGS += -fpic
  183. ifneq ($(OS),Windows_NT)
  184. EXTRA_CFLAGS_FOR_BUILD += -fpic
  185. endif
  186. ##
  187. ## Restrict ARMv6 to armv6zk instructions to support the arm1176jzf-s.
  188. ##
  189. ifeq (armv6, $(ARCH))
  190. ifneq ($(BINARYTYPE), $(filter ntconsole win32 dll,$(BINARYTYPE)))
  191. EXTRA_CPPFLAGS += -march=armv6zk -marm -mfpu=vfp
  192. endif
  193. endif
  194. ifeq (x86, $(ARCH))
  195. EXTRA_CFLAGS += -mno-ms-bitfields
  196. ##
  197. ## Quark has an errata that requires no LOCK prefixes on instructions.
  198. ##
  199. ifeq ($(VARIANT),q)
  200. ifneq ($(BINARYTYPE), $(filter ntconsole win32 dll,$(BINARYTYPE)))
  201. EXTRA_CPPFLAGS += -Wa,-momit-lock-prefix=yes -march=i586
  202. endif
  203. endif
  204. ifeq ($(BINARYTYPE),app)
  205. EXTRA_CFLAGS += -mno-stack-arg-probe
  206. endif
  207. endif
  208. ifeq ($(BINARYTYPE),so)
  209. ENTRY ?= DriverEntry
  210. endif
  211. ##
  212. ## Build binaries on windows need a .exe suffix.
  213. ##
  214. ifeq ($(OS),Windows_NT)
  215. ifeq (x86, $(BUILD_ARCH))
  216. EXTRA_CFLAGS_FOR_BUILD += -mno-ms-bitfields
  217. endif
  218. ifeq (build,$(BINARYTYPE))
  219. BINARY := $(BINARY).exe
  220. endif
  221. endif
  222. ENTRY ?= _start
  223. ##
  224. ## Linker flags
  225. ##
  226. ifneq (,$(TEXT_ADDRESS))
  227. EXTRA_LDFLAGS += -Wl,-Ttext-segment=$(TEXT_ADDRESS) -Wl,-Ttext=$(TEXT_ADDRESS)
  228. endif
  229. ifneq (,$(LINKER_SCRIPT))
  230. EXTRA_LDFLAGS += -T$(LINKER_SCRIPT)
  231. endif
  232. EXTRA_LDFLAGS += -Wl,-e$(ENTRY) \
  233. -Wl,-u$(ENTRY) \
  234. -Wl,-Map=$(BINARY).map \
  235. -Wl,--gc-sections \
  236. ifeq ($(BINARYTYPE),so)
  237. EXTRA_LDFLAGS += -nodefaultlibs -nostartfiles -nostdlib
  238. endif
  239. ##
  240. ## Assembler flags
  241. ##
  242. EXTRA_ASFLAGS += -Wa,-g
  243. ##
  244. ## For build executables, override the names even if set on the command line.
  245. ##
  246. ifneq (, $(BUILD))
  247. override CC = $(CC_FOR_BUILD)
  248. override AR = $(AR_FOR_BUILD)
  249. override OBJCOPY = $(OBJCOPY_FOR_BUILD)
  250. override STRIP = $(STRIP_FOR_BUILD)
  251. override CFLAGS = -Wall -Werror -O1
  252. override BFD_ARCH = $(BUILD_BFD_ARCH)
  253. override OBJ_FORMAT = $(BUILD_OBJ_FORMAT)
  254. ifneq ($(DEBUG), chk)
  255. override CFLAGS += -Wno-unused-but-set-variable
  256. endif
  257. override EXTRA_CFLAGS := $(EXTRA_CFLAGS_FOR_BUILD)
  258. override EXTRA_CPPFLAGS := $(EXTRA_CPPFLAGS_FOR_BUILD)
  259. override CPPFLAGS :=
  260. endif
  261. ##
  262. ## Makefile targets. .PHONY specifies that the following targets don't actually
  263. ## have files associated with them.
  264. ##
  265. .PHONY: test all clean wipe $(DIRS) $(TESTDIRS) prebuild postbuild
  266. ##
  267. ## prepend the current object directory to every extra directory.
  268. ##
  269. EXTRA_OBJ_DIRS += $(EXTRA_SRC_DIRS:%=$(OBJROOT)/$(THISDIR)/%) $(STRIPPED_DIR)
  270. all: $(DIRS) $(BINARY) postbuild
  271. $(DIRS): $(OBJROOT)/$(THISDIR)
  272. postbuild: $(BINARY)
  273. test: all $(TESTDIRS)
  274. $(DIRS):
  275. @$(CECHO_CYAN) Entering Directory: $(SRCROOT)/$(THISDIR)/$@ && \
  276. [ -d $@ ] || mkdir -p $@ && \
  277. $(MAKE) --no-print-directory -C $@ -f $(SRCDIR)/$@/Makefile \
  278. $(MAKECMDGOALS) SRCDIR=$(SRCDIR)/$@ && \
  279. $(CECHO_CYAN) Leaving Directory: $(SRCROOT)/$(THISDIR)/$@
  280. $(TESTDIRS): $(BINARY)
  281. @$(CECHO_CYAN) Entering Test Directory: $(SRCROOT)/$(THISDIR)/$@ && \
  282. [ -d $@ ] || mkdir -p $@ && \
  283. $(MAKE) --no-print-directory -C $@ -f $(SRCDIR)/$@/Makefile \
  284. -I$(SRCDIR)/$@ $(MAKECMDGOALS) SRCDIR=$(SRCDIR)/$@ && \
  285. $(CECHO_CYAN) Leaving Test Directory: $(SRCROOT)/$(THISDIR)/$@
  286. ##
  287. ## The dependencies of the binary object depend on the architecture and type of
  288. ## binary being built.
  289. ##
  290. ifneq (, $(BUILD))
  291. SAVED_ARCH := $(ARCH)
  292. ARCH := $(BUILD_ARCH)
  293. endif
  294. ifeq (x86, $(ARCH))
  295. ALLOBJS = $(X86_OBJS) $(OBJS)
  296. endif
  297. ifeq (x64, $(ARCH))
  298. ALLOBJS = $(X64_OBJS) $(OBJS)
  299. endif
  300. ifeq (armv7, $(ARCH))
  301. ALLOBJS = $(ARMV7_OBJS) $(OBJS)
  302. endif
  303. ifeq (armv6, $(ARCH))
  304. ALLOBJS = $(ARMV6_OBJS) $(OBJS)
  305. endif
  306. ifneq (, $(BUILD))
  307. ARCH := $(SAVED_ARCH)
  308. endif
  309. ifneq (, $(strip $(ALLOBJS)))
  310. ##
  311. ## The object files are dependent on the object directory, but the object
  312. ## directory being newer should not trigger a rebuild of the object files.
  313. ##
  314. $(ALLOBJS): | $(OBJROOT)/$(THISDIR)
  315. $(BINARY): $(ALLOBJS) $(TARGETLIBS)
  316. ifeq ($(BINARYTYPE),app)
  317. @echo Linking - $@
  318. @$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -pie -o $@ $^ -Bdynamic $(DYNLIBS)
  319. endif
  320. ifeq ($(BINARYTYPE),staticapp)
  321. @echo Linking - $@
  322. @$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -static -o $@ -Wl,--start-group $^ -Wl,--end-group -Bdynamic $(DYNLIBS)
  323. endif
  324. ifeq ($(BINARYTYPE),ntconsole)
  325. @echo Linking - $@
  326. @$(CC) -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  327. endif
  328. ifeq ($(BINARYTYPE),win32)
  329. @echo Linking - $@
  330. @$(CC) -mwindows -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  331. endif
  332. ifeq ($(BINARYTYPE),dll)
  333. @echo Linking - $@
  334. @$(CC) -shared -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  335. endif
  336. ifeq ($(BINARYTYPE),library)
  337. @echo Building Library - $@
  338. @$(AR) rcs $@ $^ $(TARGETLIBS)
  339. endif
  340. ifeq ($(BINARYTYPE),so)
  341. @echo Linking - $@
  342. @$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -shared -Wl,-soname=$(BINARY) -o $@ $^ -Bdynamic $(DYNLIBS)
  343. endif
  344. ifeq ($(BINARYTYPE),build)
  345. @echo Linking - $@
  346. @$(CC) $(LDFLAGS) -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  347. endif
  348. ifeq ($(BINPLACE),bin)
  349. @echo Binplacing - $(OBJROOT)/$(THISDIR)/$(BINARY)
  350. @cp -pf $(BINARY) $(BINROOT)/
  351. @$(STRIP) -p -o $(STRIPPED_DIR)/$(BINARY) $(BINARY)
  352. @$(UPDATE_LAST_UPDATE)
  353. endif
  354. ifeq ($(BINPLACE),testbin)
  355. @echo Binplacing Test - $(OBJROOT)/$(THISDIR)/$(BINARY)
  356. @cp -pf $(BINARY) $(TESTBIN)/
  357. endif
  358. else
  359. .PHONY: $(BINARY)
  360. endif
  361. ##
  362. ## Prebuild is an "order-only" dependency of this directory, meaning that
  363. ## prebuild getting rebuilt does not cause this directory to need to be
  364. ## rebuilt.
  365. ##
  366. $(OBJROOT)/$(THISDIR): | prebuild $(BINROOT) $(TESTBIN) $(EXTRA_OBJ_DIRS)
  367. @mkdir -p $(OBJROOT)/$(THISDIR)
  368. $(BINROOT) $(TESTBIN) $(EXTRA_OBJ_DIRS):
  369. @mkdir -p $@
  370. ##
  371. ## Generic target specifying how to compile a file.
  372. ##
  373. %.o:%.c
  374. @echo Compiling - $(notdir $<)
  375. @$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
  376. ##
  377. ## Generic target specifying how to assemble a file.
  378. ##
  379. %.o:%.S
  380. @echo Assembling - $(notdir $<)
  381. @$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(ASFLAGS) $(EXTRA_ASFLAGS) -c -o $@ $<
  382. ##
  383. ## Generic target specifying how to compile a resource.
  384. ##
  385. %.rsc:%.rc
  386. @echo Compiling Resource - $(notdir $<)
  387. @$(RCC) -o $@ $<
  388. ##
  389. ## This ends the originated-in-source-directory make.
  390. ##
  391. endif