minoca.mk 12 KB

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