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