Makefile 3.57 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
1
2
OBJDIR := obj
BINDIR := bin
3
4
SRCDIR := rt
PYDIR  := src
PoroCYon's avatar
PoroCYon committed
5
6
LDDIR  := ld
TESTDIR:= test
Shiz's avatar
epoch  
Shiz committed
7

PoroCYon's avatar
PoroCYon committed
8
9
BITS ?= $(shell getconf LONG_BIT)

PoroCYon's avatar
PoroCYon committed
10
# -mpreferred-stack-boundary=3 messes up the stack and kills SSE!
PoroCYon's avatar
PoroCYon committed
11
12
13
14
#COPTFLAGS=-Os -fvisibility=hidden -fwhole-program -fno-plt \
#  -ffast-math -funsafe-math-optimizations -fno-stack-protector -fomit-frame-pointer \
#  -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables

PoroCYon's avatar
PoroCYon committed
15
COPTFLAGS=-Os -fno-plt -fno-stack-protector -fno-stack-check -fno-unwind-tables \
PoroCYon's avatar
PoroCYon committed
16
  -fno-asynchronous-unwind-tables -fomit-frame-pointer -ffast-math -no-pie \
PoroCYon's avatar
PoroCYon committed
17
18
  -fno-pic -fno-PIE -ffunction-sections -fdata-sections -fno-plt \
  -fmerge-all-constants -mno-fancy-math-387 -mno-ieee-fp
PoroCYon's avatar
PoroCYon committed
19
CXXOPTFLAGS=$(COPTFLAGS) -fno-exceptions \
Shiz's avatar
epoch  
Shiz committed
20
21
22
  -fno-rtti -fno-enforce-eh-specs -fnothrow-opt -fno-use-cxa-get-exception-ptr \
  -fno-implicit-templates -fno-threadsafe-statics -fno-use-cxa-atexit

23
CFLAGS=-Wall -Wextra -Wpedantic -std=gnu11 -nostartfiles -fno-PIC $(COPTFLAGS) #-DUSE_DL_FINI
Shiz's avatar
epoch  
Shiz committed
24
CXXFLAGS=-Wall -Wextra -Wpedantic -std=c++11 $(CXXOPTFLAGS) -nostartfiles -fno-PIC
PoroCYon's avatar
PoroCYon committed
25

PoroCYon's avatar
PoroCYon committed
26
ASFLAGS=-I $(SRCDIR)/
PoroCYon's avatar
PoroCYon committed
27
LDFLAGS_ :=
PoroCYon's avatar
PoroCYon committed
28
ifeq ($(BITS),32)
PoroCYon's avatar
PoroCYon committed
29
30
31
# I think prescott is basically nocona but 32-bit only, althought I'm not sure
# if this one is optimal
CFLAGS += -m32 -march=prescott
PoroCYon's avatar
PoroCYon committed
32
LDFLAGS += -m32
PoroCYon's avatar
PoroCYon committed
33
ASFLAGS += -f elf32
PoroCYon's avatar
PoroCYon committed
34
LDFLAGS_ := -m32
PoroCYon's avatar
PoroCYon committed
35
else
PoroCYon's avatar
PoroCYon committed
36
37
# I've heard nocona gets slightly smaller binaries than core2
CFLAGS += -m64 -march=nocona
PoroCYon's avatar
PoroCYon committed
38
LDFLAGS += -m64
PoroCYon's avatar
PoroCYon committed
39
ASFLAGS += -f elf64
PoroCYon's avatar
PoroCYon committed
40
LDFLAGS_ := -m64
PoroCYon's avatar
PoroCYon committed
41
endif
PoroCYon's avatar
PoroCYon committed
42
43
LDFLAGS += -nostartfiles -nostdlib
LDFLAGS_ := $(LDFLAGS_) -T $(LDDIR)/link.ld -Wl,--oformat=binary $(LDFLAGS)
PoroCYon's avatar
PoroCYon committed
44

45
46
CFLAGS   += -m$(BITS) $(shell pkg-config --cflags sdl2)
CXXFLAGS += -m$(BITS) $(shell pkg-config --cflags sdl2)
PoroCYon's avatar
PoroCYon committed
47

48
LIBS=-lc
Shiz's avatar
epoch  
Shiz committed
49

50
SMOLFLAGS +=
PoroCYon's avatar
PoroCYon committed
51
ASFLAGS   += -DALIGN_STACK -DUSE_INTERP #-DUSE_DNLOAD_LOADER
52
#-DUSE_DNLOAD_LOADER #-DUSE_DT_DEBUG #-DUSE_DL_FINI #-DNO_START_ARG #-DUNSAFE_DYNAMIC
PoroCYon's avatar
PoroCYon committed
53
54
55

NASM    ?= nasm
PYTHON3 ?= python3
Shiz's avatar
epoch  
Shiz committed
56

57
all: $(BINDIR)/hello-crt $(BINDIR)/sdl-crt $(BINDIR)/flag $(BINDIR)/hello-_start
PoroCYon's avatar
PoroCYon committed
58

59
LIBS += $(filter-out -pthread,$(shell pkg-config --libs sdl2)) -lX11 #-lGL
Shiz's avatar
epoch  
Shiz committed
60
61

clean:
PoroCYon's avatar
PoroCYon committed
62
	@$(RM) -vrf $(OBJDIR) $(BINDIR)
Shiz's avatar
epoch  
Shiz committed
63

PoroCYon's avatar
PoroCYon committed
64
65
%/:
	@mkdir -vp "$@"
Shiz's avatar
epoch  
Shiz committed
66

PoroCYon's avatar
PoroCYon committed
67
# TODO: handle this in a more graceful and future-proof way!
68
69
70
ifneq ($(findstring (GCC) 9,$(shell $(CC) --version)),)
INCLINKOPT := -flinker-output=nolto-rel
else
PoroCYon's avatar
PoroCYon committed
71
72
73
ifneq ($(findstring (GCC) 10,$(shell $(CC) --version)),)
INCLINKOPT := -flinker-output=nolto-rel
else
74
75
INCLINKOPT :=
endif
PoroCYon's avatar
PoroCYon committed
76
endif
77

Shiz's avatar
epoch  
Shiz committed
78
79
.SECONDARY:

PoroCYon's avatar
PoroCYon committed
80
81
82
83
84
$(OBJDIR)/%.lto.o: $(SRCDIR)/%.c $(OBJDIR)/
	$(CC) -flto $(CFLAGS) -c "$<" -o "$@"
$(OBJDIR)/%.lto.o: $(TESTDIR)/%.c $(OBJDIR)/
	$(CC) -flto $(CFLAGS) -c "$<" -o "$@"

PoroCYon's avatar
PoroCYon committed
85
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)/
PoroCYon's avatar
PoroCYon committed
86
	$(CC) $(CFLAGS) -c "$<" -o "$@"
PoroCYon's avatar
PoroCYon committed
87
$(OBJDIR)/%.o: $(TESTDIR)/%.c $(OBJDIR)/
PoroCYon's avatar
PoroCYon committed
88
	$(CC) $(CFLAGS) -c "$<" -o "$@"
PoroCYon's avatar
PoroCYon committed
89

PoroCYon's avatar
PoroCYon committed
90
$(OBJDIR)/%.start.o: $(OBJDIR)/%.lto.o $(OBJDIR)/crt1.lto.o
91
	$(CC) $(LDFLAGS) -r $(INCLINKOPT) -o "$@" $^
PoroCYon's avatar
PoroCYon committed
92

93
$(OBJDIR)/symbols.%.asm: $(OBJDIR)/%.o
94
	$(PYTHON3) $(PYDIR)/smol.py $(SMOLFLAGS) $(LIBS) "$<" "$@"
Shiz's avatar
epoch  
Shiz committed
95

PoroCYon's avatar
PoroCYon committed
96
97
$(OBJDIR)/stub.%.o: $(OBJDIR)/symbols.%.asm $(SRCDIR)/header32.asm \
        $(SRCDIR)/loader32.asm
PoroCYon's avatar
PoroCYon committed
98
	$(NASM) $(ASFLAGS) $< -o $@
Shiz's avatar
epoch  
Shiz committed
99

100
101
102
103
$(OBJDIR)/stub.%.start.o: $(OBJDIR)/symbols.%.start.asm $(SRCDIR)/header32.asm \
        $(SRCDIR)/loader32.asm
	$(NASM) $(ASFLAGS) $< -o $@

104
$(BINDIR)/%: $(OBJDIR)/%.o $(OBJDIR)/stub.%.o $(BINDIR)/
PoroCYon's avatar
PoroCYon committed
105
	$(CC) -Wl,-Map=$(BINDIR)/$*.map $(LDFLAGS_) $(OBJDIR)/$*.o $(OBJDIR)/stub.$*.o -o "$@"
PoroCYon's avatar
PoroCYon committed
106
	./rmtrailzero.py "$@" "$(OBJDIR)/$(notdir $@)" && mv "$(OBJDIR)/$(notdir $@)" "$@" && chmod +x "$@"
Shiz's avatar
epoch  
Shiz committed
107

108
$(BINDIR)/%-crt: $(OBJDIR)/%.start.o $(OBJDIR)/stub.%.start.o $(BINDIR)/
PoroCYon's avatar
PoroCYon committed
109
	$(CC) -Wl,-Map=$(BINDIR)/$*-crt.map $(LDFLAGS_) $(OBJDIR)/$*.start.o $(OBJDIR)/stub.$*.start.o -o "$@"
110

PoroCYon's avatar
PoroCYon committed
111
.PHONY: all clean
Shiz's avatar
epoch  
Shiz committed
112