Makefile 2.78 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
14
15
16
17
#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

COPTFLAGS=-O2 -fno-plt -fno-stack-protector -fno-stack-check -fno-unwind-tables \
  -fno-asynchronous-unwind-tables -fomit-frame-pointer -ffast-math -no-pie \
  -fno-pic -fno-PIE -m64 -march=core2 -ffunction-sections -fdata-sections
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

PoroCYon's avatar
PoroCYon committed
21
CFLAGS=-Wall -Wextra -Wpedantic -std=gnu11 -nostartfiles -fno-PIC $(COPTFLAGS)
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

PoroCYon's avatar
PoroCYon committed
43
ASFLAGS += -DUSE_INTERP -DALIGN_STACK #-DUSE_DT_DEBUG #-DNO_START_ARG
PoroCYon's avatar
PoroCYon committed
44
45
46

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

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

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

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

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

.SECONDARY:

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

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

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

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

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

84
$(BINDIR)/%: $(OBJDIR)/%.o $(OBJDIR)/stub.%.o $(BINDIR)/
PoroCYon's avatar
PoroCYon committed
85
	$(CC) -Wl,-Map=$(BINDIR)/$*.map $(LDFLAGS_) $(OBJDIR)/$*.o $(OBJDIR)/stub.$*.o -o "$@"
Shiz's avatar
epoch  
Shiz committed
86

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

PoroCYon's avatar
PoroCYon committed
90
.PHONY: all clean
Shiz's avatar
epoch  
Shiz committed
91