Makefile 2.15 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
10
# -mpreferred-stack-boundary=3 messes up the stack and kills SSE!
COPTFLAGS=-Os -fvisibility=hidden -fwhole-program \
Shiz's avatar
epoch  
Shiz committed
11
12
13
14
15
16
  -ffast-math -funsafe-math-optimizations -fno-stack-protector -fomit-frame-pointer \
  -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables
CXXOPTFLAGS=$(COPTFLAGS) \
  -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
17
CFLAGS=-Wall -Wextra -Wpedantic -std=gnu11 -nostartfiles -fno-PIC $(COPTFLAGS)
Shiz's avatar
epoch  
Shiz committed
18
CXXFLAGS=-Wall -Wextra -Wpedantic -std=c++11 $(CXXOPTFLAGS) -nostartfiles -fno-PIC
PoroCYon's avatar
PoroCYon committed
19

PoroCYon's avatar
PoroCYon committed
20
21
ASFLAGS=-I $(SRCDIR)/
ifeq ($(BITS),32)
PoroCYon's avatar
PoroCYon committed
22
LDFLAGS=-m elf_i386
PoroCYon's avatar
PoroCYon committed
23
24
25
26
27
ASFLAGS += -f elf32
else
LDFLAGS=-m elf_x86_64
ASFLAGS += -f elf64
endif
PoroCYon's avatar
PoroCYon committed
28
29
LDFLAGS_=$(LDFLAGS) -T $(LDDIR)/link.ld --oformat=binary

30
31
CFLAGS   += -m$(BITS) $(shell pkg-config --cflags sdl2)
CXXFLAGS += -m$(BITS) $(shell pkg-config --cflags sdl2)
PoroCYon's avatar
PoroCYon committed
32

33
LIBS=-lc
Shiz's avatar
epoch  
Shiz committed
34

35
ASFLAGS += -DUSE_INTERP -DALIGN_STACK
PoroCYon's avatar
PoroCYon committed
36
37
38

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

40
all: $(BINDIR)/hello-crt $(BINDIR)/sdl-crt
PoroCYon's avatar
PoroCYon committed
41

42
LIBS += $(shell pkg-config --libs sdl2) -lX11 #-lGL
Shiz's avatar
epoch  
Shiz committed
43
44

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

PoroCYon's avatar
PoroCYon committed
47
48
%/:
	@mkdir -vp "$@"
Shiz's avatar
epoch  
Shiz committed
49
50
51

.SECONDARY:

PoroCYon's avatar
PoroCYon committed
52
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)/
PoroCYon's avatar
PoroCYon committed
53
	$(CC) $(CFLAGS) -c "$<" -o "$@"
PoroCYon's avatar
PoroCYon committed
54
$(OBJDIR)/%.o: $(TESTDIR)/%.c $(OBJDIR)/
PoroCYon's avatar
PoroCYon committed
55
	$(CC) $(CFLAGS) -c "$<" -o "$@"
PoroCYon's avatar
PoroCYon committed
56
57
58
59

$(OBJDIR)/%.start.o: $(OBJDIR)/%.o $(OBJDIR)/crt1.o
	$(LD) $(LDFLAGS) -r -o "$@" $^

60
$(OBJDIR)/symbols.%.asm: $(OBJDIR)/%.o
PoroCYon's avatar
PoroCYon committed
61
	$(PYTHON3) ./smol.py $(LIBS) "$<" "$@"
Shiz's avatar
epoch  
Shiz committed
62

PoroCYon's avatar
PoroCYon committed
63
64
$(OBJDIR)/stub.%.o: $(OBJDIR)/symbols.%.asm $(SRCDIR)/header32.asm \
        $(SRCDIR)/loader32.asm
PoroCYon's avatar
PoroCYon committed
65
	$(NASM) $(ASFLAGS) $< -o $@
Shiz's avatar
epoch  
Shiz committed
66

67
68
69
70
$(OBJDIR)/stub.%.start.o: $(OBJDIR)/symbols.%.start.asm $(SRCDIR)/header32.asm \
        $(SRCDIR)/loader32.asm
	$(NASM) $(ASFLAGS) $< -o $@

71
72
$(BINDIR)/%: $(OBJDIR)/%.o $(OBJDIR)/stub.%.o $(BINDIR)/
	$(LD) -Map=$(BINDIR)/$*.map $(LDFLAGS_) $(OBJDIR)/$*.o $(OBJDIR)/stub.$*.o -o "$@"
Shiz's avatar
epoch  
Shiz committed
73

74
75
76
$(BINDIR)/%-crt: $(OBJDIR)/%.start.o $(OBJDIR)/stub.%.start.o $(BINDIR)/
	$(LD) -Map=$(BINDIR)/$*-crt.map $(LDFLAGS_) $(OBJDIR)/$*.start.o $(OBJDIR)/stub.$*.start.o -o "$@"

PoroCYon's avatar
PoroCYon committed
77
.PHONY: all clean
Shiz's avatar
epoch  
Shiz committed
78