minoca.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. SYSTEM_VERSION_MAJOR := 0
  150. SYSTEM_VERSION_MINOR := 2
  151. SYSTEM_VERSION_REVISION := 0
  152. INIT_REV := 1598fc5f1734f7d7ee01e014ee64e131601b78a7
  153. REVISION_EXTRA ?= $(shell \
  154. if [ -f $(SRCROOT)/os/.git/refs/replace/$(INIT_REV) ] ; then \
  155. echo 1 ; \
  156. else echo 1000 ; \
  157. fi)
  158. REVISION ?= $(shell \
  159. echo $$((`cd $(SRCROOT)/os/ && git rev-list --count HEAD` + \
  160. $(REVISION_EXTRA))))
  161. ifeq ($(REVISION),)
  162. REVISION := 0
  163. endif
  164. BUILD_TIME ?= $(shell echo $$((`date "+%s"` - 978307200)))
  165. BUILD_TIME_STRING ?= "$(shell date "+%a %b %d %Y %H:%M:%S")"
  166. BUILD_STRING ?= "($(USERNAME))"
  167. ##
  168. ## Export time and revision variables so they don't get re-evaluated at every
  169. ## recursive make.
  170. ##
  171. export REVISION
  172. export REVISION_EXTRA
  173. export BUILD_TIME
  174. export BUILD_TIME_STRING
  175. export BUILD_STRING
  176. ##
  177. ## Define a file that gets touched to indicate that something has changed and
  178. ## images need to be rebuilt.
  179. ##
  180. LAST_UPDATE_FILE := $(OBJROOT)/last-update
  181. UPDATE_LAST_UPDATE := date > $(LAST_UPDATE_FILE)
  182. ##
  183. ## Includes directory.
  184. ##
  185. INCLUDES += $(SRCROOT)/os/include
  186. ##
  187. ## Define default CFLAGS if none were specified elsewhere.
  188. ##
  189. CFLAGS ?= -Wall -Werror
  190. ifeq ($(DEBUG), chk)
  191. CFLAGS += -O1
  192. else
  193. CFLAGS += -O2 -Wno-unused-but-set-variable
  194. endif
  195. ##
  196. ## Compiler flags:
  197. ##
  198. ## -fno-builtin Don't recognize functions that do not begin with
  199. ## '__builtin__' as prefix.
  200. ##
  201. ## -fno-omit-frame-pointer Always create a frame pointer so that things can be
  202. ## debugged.
  203. ##
  204. ## -mno-ms-bitfields Honor the packed attribute.
  205. ##
  206. ## -g Build with DWARF debugging symbol information.
  207. ##
  208. ## -I ... Specifies a list of include directories.
  209. ##
  210. ## -c Compile or assemble, but do not link.
  211. ##
  212. ## -Wall Turn on all compiler warnings.
  213. ##
  214. ## -Werror Treat all warnings as errors.
  215. ##
  216. ## -D...=... Standard defines usable by all C files.
  217. ##
  218. ## -fvisibility=hidden Don't export every single symbol by default.
  219. ##
  220. ## -save-temps Save temporary files.
  221. ##
  222. EXTRA_CPPFLAGS += -I $(subst ;, -I ,$(INCLUDES)) \
  223. -DSYSTEM_VERSION_MAJOR=$(SYSTEM_VERSION_MAJOR) \
  224. -DSYSTEM_VERSION_MINOR=$(SYSTEM_VERSION_MINOR) \
  225. -DSYSTEM_VERSION_REVISION=$(SYSTEM_VERSION_REVISION) \
  226. -DSYSTEM_VERSION_RELEASE=SystemReleaseDevelopment \
  227. -DREVISION=$(REVISION) \
  228. -DBUILD_TIME_STRING=\"$(BUILD_TIME_STRING)\" \
  229. -DBUILD_TIME=$(BUILD_TIME) \
  230. -DBUILD_STRING=\"$(BUILD_STRING)\" \
  231. -DBUILD_USER=\"$(USERNAME)\" \
  232. ifeq ($(DEBUG), chk)
  233. EXTRA_CPPFLAGS += -DDEBUG=1
  234. endif
  235. EXTRA_CPPFLAGS_FOR_BUILD := $(EXTRA_CPPFLAGS)
  236. EXTRA_CFLAGS += -fno-builtin -fno-omit-frame-pointer -g -save-temps=obj \
  237. -ffunction-sections -fdata-sections -fvisibility=hidden
  238. EXTRA_CFLAGS_FOR_BUILD := $(EXTRA_CFLAGS)
  239. EXTRA_CFLAGS += -fpic
  240. ifneq ($(OS),Windows_NT)
  241. EXTRA_CFLAGS_FOR_BUILD += -fpic
  242. endif
  243. ##
  244. ## Restrict ARMv6 to armv6zk instructions to support the arm1176jzf-s.
  245. ##
  246. ifeq (armv6, $(ARCH))
  247. ifneq ($(BINARYTYPE), $(filter ntconsole win32 dll,$(BINARYTYPE)))
  248. EXTRA_CPPFLAGS += -march=armv6zk -marm -mfpu=vfp
  249. endif
  250. endif
  251. ifeq (x86, $(ARCH))
  252. EXTRA_CFLAGS += -mno-ms-bitfields
  253. ##
  254. ## Quark has an errata that requires no LOCK prefixes on instructions.
  255. ##
  256. ifeq ($(VARIANT),q)
  257. ifneq ($(BINARYTYPE), $(filter ntconsole win32 dll,$(BINARYTYPE)))
  258. EXTRA_CPPFLAGS += -Wa,-momit-lock-prefix=yes -march=i586
  259. endif
  260. endif
  261. ifeq ($(BINARYTYPE),app)
  262. EXTRA_CFLAGS += -mno-stack-arg-probe
  263. endif
  264. endif
  265. ifeq ($(BINARYTYPE),so)
  266. ENTRY ?= DriverEntry
  267. endif
  268. ##
  269. ## Build binaries on windows need a .exe suffix.
  270. ##
  271. ifeq ($(OS),Windows_NT)
  272. ifeq (x86, $(BUILD_ARCH))
  273. EXTRA_CFLAGS_FOR_BUILD += -mno-ms-bitfields
  274. endif
  275. ifeq (build,$(BINARYTYPE))
  276. BINARY := $(BINARY).exe
  277. endif
  278. endif
  279. ENTRY ?= _start
  280. ##
  281. ## Linker flags:
  282. ##
  283. ## -T linker_script Specifies a custom linker script.
  284. ##
  285. ## -Ttext X Use X as the starting address for the text section
  286. ## of the output file. One of the defined addresses
  287. ## above will get placed after the linker options, so
  288. ## this option MUST be last.
  289. ##
  290. ## -e <symbol> Sets the entry point of the binary to the given
  291. ## symbol.
  292. ##
  293. ## -u <symbol> Start with an undefined reference to the entry
  294. ## point, in case it is in a static library.
  295. ##
  296. ## -Map Create a linker map for reference as well.
  297. ##
  298. ifneq (,$(TEXT_ADDRESS))
  299. EXTRA_LDFLAGS += -Wl,-Ttext-segment=$(TEXT_ADDRESS) -Wl,-Ttext=$(TEXT_ADDRESS)
  300. endif
  301. ifneq (,$(LINKER_SCRIPT))
  302. EXTRA_LDFLAGS += -T$(LINKER_SCRIPT)
  303. endif
  304. EXTRA_LDFLAGS += -Wl,-e$(ENTRY) \
  305. -Wl,-u$(ENTRY) \
  306. -Wl,-Map=$(BINARY).map \
  307. -Wl,--gc-sections \
  308. ifeq ($(BINARYTYPE),so)
  309. EXTRA_LDFLAGS += -nodefaultlibs -nostartfiles -nostdlib
  310. endif
  311. ##
  312. ## Assembler flags:
  313. ##
  314. ## -g Build with debugging symbol information.
  315. ##
  316. ## -I ... Specify include directories to search.
  317. ##
  318. EXTRA_ASFLAGS += -Wa,-g
  319. ##
  320. ## For build executables, override the names even if set on the command line.
  321. ##
  322. ifneq (, $(BUILD))
  323. override CC = $(CC_FOR_BUILD)
  324. override AR = $(AR_FOR_BUILD)
  325. override OBJCOPY = $(OBJCOPY_FOR_BUILD)
  326. override STRIP = $(STRIP_FOR_BUILD)
  327. override CFLAGS = -Wall -Werror -O1
  328. override BFD_ARCH = $(BUILD_BFD_ARCH)
  329. override OBJ_FORMAT = $(BUILD_OBJ_FORMAT)
  330. ifneq ($(DEBUG), chk)
  331. override CFLAGS += -Wno-unused-but-set-variable
  332. endif
  333. override EXTRA_CFLAGS := $(EXTRA_CFLAGS_FOR_BUILD)
  334. override EXTRA_CPPFLAGS := $(EXTRA_CPPFLAGS_FOR_BUILD)
  335. override CPPFLAGS :=
  336. endif
  337. ##
  338. ## Makefile targets. .PHONY specifies that the following targets don't actually
  339. ## have files associated with them.
  340. ##
  341. .PHONY: test all clean wipe $(DIRS) $(TESTDIRS) prebuild postbuild
  342. ##
  343. ## prepend the current object directory to every extra directory.
  344. ##
  345. EXTRA_OBJ_DIRS += $(EXTRA_SRC_DIRS:%=$(OBJROOT)/$(THISDIR)/%) $(STRIPPED_DIR)
  346. all: $(DIRS) $(BINARY) postbuild
  347. $(DIRS): $(OBJROOT)/$(THISDIR)
  348. postbuild: $(BINARY)
  349. test: all $(TESTDIRS)
  350. $(DIRS):
  351. @$(CECHO_CYAN) Entering Directory: $(SRCROOT)/$(THISDIR)/$@ && \
  352. [ -d $@ ] || mkdir -p $@ && \
  353. $(MAKE) --no-print-directory -C $@ -f $(SRCDIR)/$@/Makefile \
  354. $(MAKECMDGOALS) SRCDIR=$(SRCDIR)/$@ && \
  355. $(CECHO_CYAN) Leaving Directory: $(SRCROOT)/$(THISDIR)/$@
  356. $(TESTDIRS): $(BINARY)
  357. @$(CECHO_CYAN) Entering Test Directory: $(SRCROOT)/$(THISDIR)/$@ && \
  358. [ -d $@ ] || mkdir -p $@ && \
  359. $(MAKE) --no-print-directory -C $@ -f $(SRCDIR)/$@/Makefile \
  360. -I$(SRCDIR)/$@ $(MAKECMDGOALS) SRCDIR=$(SRCDIR)/$@ && \
  361. $(CECHO_CYAN) Leaving Test Directory: $(SRCROOT)/$(THISDIR)/$@
  362. ##
  363. ## The dependencies of the binary object depend on the architecture and type of
  364. ## binary being built.
  365. ##
  366. ifneq (, $(BUILD))
  367. SAVED_ARCH := $(ARCH)
  368. ARCH := $(BUILD_ARCH)
  369. endif
  370. ifeq (x86, $(ARCH))
  371. ALLOBJS = $(X86_OBJS) $(OBJS)
  372. endif
  373. ifeq (x64, $(ARCH))
  374. ALLOBJS = $(X64_OBJS) $(OBJS)
  375. endif
  376. ifeq (armv7, $(ARCH))
  377. ALLOBJS = $(ARMV7_OBJS) $(OBJS)
  378. endif
  379. ifeq (armv6, $(ARCH))
  380. ALLOBJS = $(ARMV6_OBJS) $(OBJS)
  381. endif
  382. ifneq (, $(BUILD))
  383. ARCH := $(SAVED_ARCH)
  384. endif
  385. ifneq (, $(strip $(ALLOBJS)))
  386. ##
  387. ## The object files are dependent on the object directory, but the object
  388. ## directory being newer should not trigger a rebuild of the object files.
  389. ##
  390. $(ALLOBJS): | $(OBJROOT)/$(THISDIR)
  391. $(BINARY): $(ALLOBJS) $(TARGETLIBS)
  392. ifeq ($(BINARYTYPE),app)
  393. @echo Linking - $@
  394. @$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -pie -o $@ $^ -Bdynamic $(DYNLIBS)
  395. endif
  396. ifeq ($(BINARYTYPE),staticapp)
  397. @echo Linking - $@
  398. @$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -static -o $@ -Wl,--start-group $^ -Wl,--end-group -Bdynamic $(DYNLIBS)
  399. endif
  400. ifeq ($(BINARYTYPE),ntconsole)
  401. @echo Linking - $@
  402. @$(CC) -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  403. endif
  404. ifeq ($(BINARYTYPE),win32)
  405. @echo Linking - $@
  406. @$(CC) -mwindows -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  407. endif
  408. ifeq ($(BINARYTYPE),dll)
  409. @echo Linking - $@
  410. @$(CC) -shared -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  411. endif
  412. ifeq ($(BINARYTYPE),library)
  413. @echo Building Library - $@
  414. @$(AR) rcs $@ $^ $(TARGETLIBS)
  415. endif
  416. ifeq ($(BINARYTYPE),so)
  417. @echo Linking - $@
  418. @$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -shared -Wl,-soname=$(BINARY) -o $@ $^ -Bdynamic $(DYNLIBS)
  419. endif
  420. ifeq ($(BINARYTYPE),build)
  421. @echo Linking - $@
  422. @$(CC) $(LDFLAGS) -o $@ $^ $(TARGETLIBS) -Bdynamic $(DYNLIBS)
  423. endif
  424. ifeq ($(BINPLACE),bin)
  425. @echo Binplacing - $(OBJROOT)/$(THISDIR)/$(BINARY)
  426. @cp -pf $(BINARY) $(BINROOT)/
  427. @$(STRIP) -p -o $(STRIPPED_DIR)/$(BINARY) $(BINARY)
  428. @$(UPDATE_LAST_UPDATE)
  429. endif
  430. ifeq ($(BINPLACE),testbin)
  431. @echo Binplacing Test - $(OBJROOT)/$(THISDIR)/$(BINARY)
  432. @cp -pf $(BINARY) $(TESTBIN)/
  433. endif
  434. else
  435. .PHONY: $(BINARY)
  436. endif
  437. ##
  438. ## Prebuild is an "order-only" dependency of this directory, meaning that
  439. ## prebuild getting rebuilt does not cause this directory to need to be
  440. ## rebuilt.
  441. ##
  442. $(OBJROOT)/$(THISDIR): | prebuild $(BINROOT) $(TESTBIN) $(EXTRA_OBJ_DIRS)
  443. @mkdir -p $(OBJROOT)/$(THISDIR)
  444. $(BINROOT) $(TESTBIN) $(EXTRA_OBJ_DIRS):
  445. @mkdir -p $@
  446. ##
  447. ## Generic target specifying how to compile a file.
  448. ##
  449. %.o:%.c
  450. @echo Compiling - $(notdir $<)
  451. @$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
  452. ##
  453. ## Generic target specifying how to assemble a file.
  454. ##
  455. %.o:%.S
  456. @echo Assembling - $(notdir $<)
  457. @$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(ASFLAGS) $(EXTRA_ASFLAGS) -c -o $@ $<
  458. ##
  459. ## Generic target specifying how to compile a resource.
  460. ##
  461. %.rsc:%.rc
  462. @echo Compiling Resource - $(notdir $<)
  463. @$(RCC) -o $@ $<
  464. ##
  465. ## This ends the originated-in-source-directory make.
  466. ##
  467. endif