Makefile 3.04 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 \
17
  -fno-pic -fno-PIE -m64 -march=core2 -ffunction-sections -fdata-sections -fno-plt
PoroCYon's avatar
PoroCYon committed
18
CXXOPTFLAGS=$(COPTFLAGS) -fno-exceptions \
Shiz's avatar
epoch  
Shiz committed
19
20
21
  -fno-rtti -fno-enforce-eh-specs -fnothrow-opt -fno-use-cxa-get-exception-ptr \
  -fno-implicit-templates -fno-threadsafe-statics -fno-use-cxa-atexit

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

PoroCYon's avatar
PoroCYon committed
25
ASFLAGS=-I $(SRCDIR)/
PoroCYon's avatar
PoroCYon committed
26
LDFLAGS_ :=
PoroCYon's avatar
PoroCYon committed
27
ifeq ($(BITS),32)
PoroCYon's avatar
PoroCYon committed
28
LDFLAGS += -m32
PoroCYon's avatar
PoroCYon committed
29
ASFLAGS += -f elf32
PoroCYon's avatar
PoroCYon committed
30
LDFLAGS_ := -m32
PoroCYon's avatar
PoroCYon committed
31
else
PoroCYon's avatar
PoroCYon committed
32
LDFLAGS += -m64
PoroCYon's avatar
PoroCYon committed
33
ASFLAGS += -f elf64
PoroCYon's avatar
PoroCYon committed
34
LDFLAGS_ := -m64
PoroCYon's avatar
PoroCYon committed
35
endif
PoroCYon's avatar
PoroCYon committed
36
37
LDFLAGS += -nostartfiles -nostdlib
LDFLAGS_ := $(LDFLAGS_) -T $(LDDIR)/link.ld -Wl,--oformat=binary $(LDFLAGS)
PoroCYon's avatar
PoroCYon committed
38

39
40
CFLAGS   += -m$(BITS) $(shell pkg-config --cflags sdl2)
CXXFLAGS += -m$(BITS) $(shell pkg-config --cflags sdl2)
PoroCYon's avatar
PoroCYon committed
41

42
LIBS=-lc
Shiz's avatar
epoch  
Shiz committed
43

44
45
SMOLFLAGS +=
ASFLAGS   += -DUSE_INTERP -DNO_START_ARG -DUNSAFE_DYNAMIC -DUSE_DNLOAD_LOADER #-DALIGN_STACK
46
#-DUSE_DNLOAD_LOADER #-DUSE_DT_DEBUG #-DUSE_DL_FINI #-DNO_START_ARG #-DUNSAFE_DYNAMIC
PoroCYon's avatar
PoroCYon committed
47
48
49

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

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

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

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

PoroCYon's avatar
PoroCYon committed
58
59
%/:
	@mkdir -vp "$@"
Shiz's avatar
epoch  
Shiz committed
60
61
62

.SECONDARY:

PoroCYon's avatar
PoroCYon committed
63
64
65
66
67
$(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
68
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)/
PoroCYon's avatar
PoroCYon committed
69
	$(CC) $(CFLAGS) -c "$<" -o "$@"
PoroCYon's avatar
PoroCYon committed
70
$(OBJDIR)/%.o: $(TESTDIR)/%.c $(OBJDIR)/
PoroCYon's avatar
PoroCYon committed
71
	$(CC) $(CFLAGS) -c "$<" -o "$@"
PoroCYon's avatar
PoroCYon committed
72

PoroCYon's avatar
PoroCYon committed
73
74
$(OBJDIR)/%.start.o: $(OBJDIR)/%.lto.o $(OBJDIR)/crt1.lto.o
	$(CC) $(LDFLAGS) -r -o "$@" $^
PoroCYon's avatar
PoroCYon committed
75

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

PoroCYon's avatar
PoroCYon committed
79
80
$(OBJDIR)/stub.%.o: $(OBJDIR)/symbols.%.asm $(SRCDIR)/header32.asm \
        $(SRCDIR)/loader32.asm
PoroCYon's avatar
PoroCYon committed
81
	$(NASM) $(ASFLAGS) $< -o $@
Shiz's avatar
epoch  
Shiz committed
82

83
84
85
86
$(OBJDIR)/stub.%.start.o: $(OBJDIR)/symbols.%.start.asm $(SRCDIR)/header32.asm \
        $(SRCDIR)/loader32.asm
	$(NASM) $(ASFLAGS) $< -o $@

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

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

PoroCYon's avatar
PoroCYon committed
94
.PHONY: all clean
Shiz's avatar
epoch  
Shiz committed
95