forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
155 lines (122 loc) · 3.63 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
LIBDIR ?= $(PREFIX)/lib
MANDIR ?= $(PREFIX)/share/man
D = $(DESTDIR)
ifeq ($(origin CC), default)
CC = clang
endif
ifeq ($(origin CXX), default)
CXX = clang++
endif
STRIP ?= strip
OS ?= $(shell uname -s)
# Used for both C and C++
COMMON_FLAGS = -pthread -fPIE -fno-unwind-tables -fno-asynchronous-unwind-tables
CFLAGS ?= -O2
CFLAGS += $(COMMON_FLAGS)
CXXFLAGS ?= -O2
CXXFLAGS += $(COMMON_FLAGS) -std=c++20 -fno-exceptions
CPPFLAGS += -DMOLD_VERSION=\"1.0.0\" -DLIBDIR="\"$(LIBDIR)\""
LIBS = -pthread -lz -lxxhash -ldl -lm
SRCS=$(wildcard *.cc elf/*.cc macho/*.cc)
HEADERS=$(wildcard *.h elf/*.h macho/*.h)
OBJS=$(SRCS:%.cc=out/%.o)
DEBUG ?= 0
LTO ?= 0
ASAN ?= 0
TSAN ?= 0
GIT_HASH ?= $(shell [ -d .git ] && git rev-parse HEAD)
ifneq ($(GIT_HASH),)
CPPFLAGS += -DGIT_HASH=\"$(GIT_HASH)\"
endif
ifeq ($(DEBUG), 1)
CXXFLAGS += -O0 -g
endif
ifeq ($(LTO), 1)
CXXFLAGS += -flto -O3
LDFLAGS += -flto
endif
ifeq ($(ASAN), 1)
CXXFLAGS += -fsanitize=address
LDFLAGS += -fsanitize=address
else ifeq ($(TSAN), 1)
CXXFLAGS += -fsanitize=thread
LDFLAGS += -fsanitize=thread
else ifneq ($(OS), Darwin)
# By default, we want to use mimalloc as a memory allocator.
# Since replacing the standard malloc is not compatible with ASAN,
# we do that only when ASAN is not enabled.
ifdef SYSTEM_MIMALLOC
LIBS += -lmimalloc
else
MIMALLOC_LIB = out/mimalloc/libmimalloc.a
CPPFLAGS += -Ithird-party/mimalloc/include
LIBS += -Wl,-whole-archive $(MIMALLOC_LIB) -Wl,-no-whole-archive
endif
endif
# Homebrew on macOS/ARM installs packages under /opt/homebrew
# instead of /usr/local
ifneq ($(wildcard /opt/homebrew/.),)
CPPFLAGS += -I/opt/homebrew/include
LIBS += -L/opt/homebrew/lib
endif
ifdef SYSTEM_TBB
LIBS += -ltbb
else
TBB_LIB = out/tbb/libs/libtbb.a
LIBS += $(TBB_LIB)
CPPFLAGS += -Ithird-party/tbb/include
endif
ifneq ($(OS), Darwin)
LIBS += -lcrypto
endif
all: mold mold-wrapper.so
mold: $(OBJS) $(MIMALLOC_LIB) $(TBB_LIB)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(OBJS) -o $@ $(LIBS)
ln -sf mold ld
ln -sf mold ld64.mold
mold-wrapper.so: elf/mold-wrapper.c Makefile
$(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -shared -o $@ $(LDFLAGS) $< -ldl
out/%.o: %.cc $(HEADERS) Makefile out/elf/.keep out/macho/.keep
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
out/elf/.keep:
mkdir -p out/elf
touch $@
out/macho/.keep:
mkdir -p out/macho
touch $@
$(MIMALLOC_LIB):
mkdir -p out/mimalloc
(cd out/mimalloc; CFLAGS=-DMI_USE_ENVIRON=0 cmake -G'Unix Makefiles' ../../third-party/mimalloc)
$(MAKE) -C out/mimalloc mimalloc-static
$(TBB_LIB):
mkdir -p out/tbb
(cd out/tbb; cmake -G'Unix Makefiles' -DBUILD_SHARED_LIBS=OFF -DTBB_TEST=OFF -DCMAKE_CXX_FLAGS=-D__TBB_DYNAMIC_LOAD_ENABLED=0 -DTBB_STRICT=OFF ../../third-party/tbb)
$(MAKE) -C out/tbb tbb
(cd out/tbb; ln -sf *_relwithdebinfo libs)
ifeq ($(OS), Darwin)
test tests check: all
$(MAKE) -C test -f Makefile.darwin --no-print-directory
else
test tests check: all
$(MAKE) -C test -f Makefile.linux --no-print-directory --output-sync
endif
install: all
install -m 755 -d $D$(BINDIR)
install -m 755 mold $D$(BINDIR)
$(STRIP) $D$(BINDIR)/mold
install -m 755 -d $D$(LIBDIR)/mold
install -m 644 mold-wrapper.so $D$(LIBDIR)/mold
$(STRIP) $D$(LIBDIR)/mold/mold-wrapper.so
install -m 755 -d $D$(MANDIR)/man1
install -m 644 docs/mold.1 $D$(MANDIR)/man1
ln -sf mold $D$(BINDIR)/ld.mold
ln -sf mold $D$(BINDIR)/ld64.mold
uninstall:
rm -f $D$(BINDIR)/mold $D$(BINDIR)/ld.mold $D$(BINDIR)/ld64.mold
rm -f $D$(MANDIR)/man1/mold.1
rm -rf $D$(LIBDIR)/mold
clean:
rm -rf *~ mold mold-wrapper.so out ld ld64.mold
.PHONY: all test tests check clean