-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
90 lines (68 loc) · 3.09 KB
/
Makefile
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
ARCH := x86_64
OS := darwin
CC = clang -target $(ARCH)-$(OS)
AR := $(shell command -v llvm-ar 2>/dev/null || echo ar)
COMPILER_RT_VERSION := 19.1.3
CFLAGS := -Wall -Wextra -Wpedantic -DDEBUG
OPTFLAGS := -g
LDFLAGS := -static
include platform/$(OS)/platform.mk
SRCS := $(wildcard src/*/*.c) $(wildcard platform/$(OS)/src/*.c) $(wildcard platform/$(OS)/arch/$(ARCH)/*.c)
ASMS := $(wildcard src/*/*.S) $(wildcard platform/$(OS)/src/*.S) $(wildcard platform/$(OS)/arch/$(ARCH)/*.S)
OBJS := $(SRCS:.c=.o) $(ASMS:.S=.o)
TESTSRCS := $(wildcard tests/*.c)
TESTEXES := $(TESTSRCS:tests/%.c=tests/bin/%)
HEADERS := $(wildcard include/*.h) $(wildcard include/*/*.h)
ifndef VERBOSE
V := @
endif
.PHONY: all release tests clean clangd
all: sdk/usr/include sdk/usr/lib
release: OPTFLAGS := -O2
release: CFLAGS := -Wall -Wextra -Wpedantic
release: all
tests: $(TESTEXES)
sdk/usr/include: $(HEADERS)
@printf "Installing headers...\n"
$(V)rm -rf sdk/usr/include
$(V)mkdir -p sdk/usr
$(V)cp -r include sdk/usr
sdk/usr/lib: crt/crt0.o crt/crti.o crt/crtn.o src/libc.a
@printf "Installing libraries...\n"
$(V)rm -rf sdk/usr/lib
$(V)mkdir -p sdk/usr/lib
$(V)cp src/libc.a sdk/usr/lib
$(V)cp crt/crt*.o sdk/usr/lib
$(V)for lib in libSystem.a libgcc.a; do ln -s libc.a sdk/usr/lib/$$lib; done
$(V)for crt in crt1.o crt1.10.5.o; do ln -s crt0.o sdk/usr/lib/$$crt; done
tests/bin/%: tests/%.c all
@printf " \033[1;32mCC\033[0m $@\n"
$(V)$(CC) --sysroot sdk -std=c99 -Iinclude $(CFLAGS) $(OPTFLAGS) -c $< -o tests/$*.o
$(V)$(CC) --sysroot sdk $(LDFLAGS) $(OPTFLAGS) -lc tests/$*.o -o $@
src/libc.a: builtins $(OBJS)
@printf " \033[1;34mAR\033[0m %s\n" "libc.a"
$(V)$(AR) rcs $@ $(OBJS) $(wildcard src/builtins/*.o)
crt/crt0.o: crt/cstart.o crt/start.o
$(V)printf " \033[1;34mLD\033[0m crt0.o\n"
$(V)$(CC) --sysroot sdk $(LDFLAGS) $(OPTFLAGS) -nostdlib -r $^ -o $@
crt/start.o $(ASMS:.S=.o): %.o: %.S $(HEADERS)
@src=$@; src=$${src##*/}; printf " \033[1;33mAS\033[0m %s\n" "$$src"
$(V)$(CC) --sysroot sdk -Iinclude -D__UNIQ_LIBC_PRIVATE_API $(CFLAGS) $(OPTFLAGS) -c $< -o $@
%.o: %.c sdk/usr/include $(HEADERS)
@src=$@; src=$${src##*/}; printf " \033[1;32mCC\033[0m %s\n" "$$src"
$(V)$(CC) -std=c99 -fno-builtin --sysroot sdk -Iinclude -D__UNIQ_LIBC_PRIVATE_API $(CFLAGS) $(OPTFLAGS) -c $< -o $@
clean:
@printf "Cleaning up...\n"
$(V)rm -rf sdk/* tests/*.o tests/bin/* src/libc.a crt/*.o src/*/*.o platform/*/src/*.o platform/*/arch/*/*.o
distclean: clean
$(V)rm -rf compiler-rt .clangd
compiler-rt:
@printf "Downloading compiler-rt...\n"
$(V)rm -rf compiler-rt
$(V)curl -# -L https://github.com/llvm/llvm-project/releases/download/llvmorg-$(COMPILER_RT_VERSION)/compiler-rt-$(COMPILER_RT_VERSION).src.tar.xz | xz -d | tar -x
$(V)mv compiler-rt-$(COMPILER_RT_VERSION).src compiler-rt
builtins: compiler-rt sdk/usr/include $(HEADERS)
@$(MAKE) -f platform/$(OS)/arch/$(ARCH)/arch.mk NOASM=$(NOASM) CC="$(CC)" V=$(V) CFLAGS="$(CFLAGS)" OPTFLAGS="$(OPTFLAGS)" ARCH=$(ARCH)
clangd:
@printf "Generating clangd config...\n"
$(V)printf 'CompileFlags:\n Add: [-I$(PWD)/include, -D__UNIQ_LIBC_PRIVATE_API, --target=x86_64-apple-darwin]\n' > .clangd