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

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

PoroCYon's avatar
PoroCYon committed
9
# -mpreferred-stack-boundary=3 messes up the stack and kills SSE!
PoroCYon's avatar
PoroCYon committed
10
11
12
13
#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
14
COPTFLAGS=-Os -fno-plt -fno-stack-protector -fno-stack-check -fno-unwind-tables \
PoroCYon's avatar
PoroCYon committed
15
  -fno-asynchronous-unwind-tables -fomit-frame-pointer -ffast-math -no-pie \
16
  -fno-pic -fno-PIE -m64 -march=core2 -ffunction-sections -fdata-sections -fno-plt
PoroCYon's avatar
PoroCYon committed
17
CXXOPTFLAGS=$(COPTFLAGS) -fno-exceptions \
Shiz's avatar
epoch  
Shiz committed
18
19
20
  -fno-rtti -fno-enforce-eh-specs -fnothrow-opt -fno-use-cxa-get-exception-ptr \
  -fno-implicit-templates -fno-threadsafe-statics -fno-use-cxa-atexit

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

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

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

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

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

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

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

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

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

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

.SECONDARY:

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

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

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

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

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

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

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

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