-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.mk
188 lines (165 loc) · 5.27 KB
/
env.mk
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Setup for egalito compilation environment
# Compute the root directory of the repo, containing env.mk.
EGALITO_ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
# Optional settings:
# USE_KEYSTONE=1
# USE_LOADER=1
# VERBOSE=1
# PROFILE=1
# STACK_PROTECTOR=1
# To cross-compile, set e.g. CROSS=aarch64-linux-gnu-
# for loader support, also set RTLD_TARGET to an appropriate simulator for
# running binaries (e.g. qemu-user-*).
# To cross-analyze, set CROSS as before and
# also CROSS_ANALYZE=1 (the loader is not supported in this config)
ifneq ($(CROSS),)
ifeq ($(CROSS_ANALYZE),1)
USE_LOADER=0
endif
endif
ifeq ($(USE_LOADER),)
USE_LOADER=1
endif
ifeq ($(USE_LOADER),1)
ifneq ($(CROSS),)
ifneq ($(RTLD_TARGET),)
RTLD_EXEC = $(RTLD_TARGET)
else
$(error Specify command to execute target rtld for current arch)
endif
endif
endif
ifneq ($(CROSS_ANALYZE),1)
CC = $(EGALITO_CCACHE) $(CROSS)gcc
CXX = $(EGALITO_CCACHE) $(CROSS)g++
else
CC = $(EGALITO_CCACHE) gcc
CXX = $(EGALITO_CCACHE) g++
endif
TARGET_CC = $(EGALITO_CCACHE) $(CROSS)gcc
TARGET_CXX = $(EGALITO_CCACHE) $(CROSS)g++
AS = $(CC)
LINK = $(CXX)
AR = ar
GENERIC_FLAGS = -Wall -Wextra -Wno-unused-parameter -I.
ifneq ($(CROSS),)
ifneq ($(CAPSTONE_INC),)
GENERIC_FLAGS += -isystem $(CAPSTONE_INC)
endif
ifneq ($(CAPSTONE_LIB),)
CROSSLD = -L $(CAPSTONE_LIB)
endif
endif
CAPSTONE_DIR = $(EGALITO_ROOT_DIR)/dep/capstone/install
GENERIC_FLAGS += -I $(CAPSTONE_DIR)/include
ifeq ($(USE_KEYSTONE),1)
KEYSTONE_DIR = $(EGALITO_ROOT_DIR)/dep/keystone
GENERIC_FLAGS += -I $(KEYSTONE_DIR)/include
endif
ifeq ($(USE_LOADER),1)
GENERIC_FLAGS += -DUSE_LOADER
endif
OPT_FLAGS = -g3 -Og
DEPFLAGS = -MT '$@ $(@:.o=.so) $(@:.o=.d)' -MMD -MF $(@:.o=.d) -MP
CFLAGS = -std=gnu99 $(GENERIC_FLAGS) $(OPT_FLAGS)
CXXFLAGS = -std=c++14 $(GENERIC_FLAGS) $(OPT_FLAGS)
CLDFLAGS = $(CROSSLD)
CLDFLAGS += -L $(CAPSTONE_DIR)/lib -lcapstone \
-Wl,-rpath,$(abspath $(CAPSTONE_DIR)/lib)
ifdef USE_KEYSTONE # set USE_KEYSTONE=1 to link with str->instr assembler
CLDFLAGS += -L $(KEYSTONE_DIR)/build/llvm/lib -lkeystone \
-Wl,-rpath,$(abspath $(KEYSTONE_DIR)/build/llvm/lib)
CFLAGS += -D USE_KEYSTONE
CXXFLAGS += -D USE_KEYSTONE
endif
ifdef PROFILE # set PROFILE=1 to enable gprof profiling
CFLAGS += -no-pie -pg
CXXFLAGS += -no-pie -pg
CLDFLAGS += -no-pie -pg
endif
ifdef STACK_PROTECTOR # set STACK_PROTECTOR=1 to enable -fstack-protector flag
CFLAGS += -fstack-protector-all -D EGALITO_STACK_PROTECTOR
CXXFLAGS += -fstack-protector-all -D EGALITO_STACK_PROTECTOR
else
CFLAGS += -fno-stack-protector
CXXFLAGS += -fno-stack-protector
endif
ifneq ($(CROSS_ANALYZE),)
P_ARCH := $(strip $(shell echo $(CROSS) | awk -F- '{print $$1}'))
else
P_ARCH := $(shell uname -m)
ifeq (armv7l,$(P_ARCH))
P_ARCH = arm
endif
DISTRO = $(word 3,$(shell lsb_release -i))
endif
export P_ARCH
$(if $(VERBOSE),$(info Building for $(P_ARCH)))
ifeq (aarch64,$(P_ARCH))
CFLAGS += -DARCH_AARCH64
CXXFLAGS += -DARCH_AARCH64
AFLAGS += -DARCH_AARCH64
BUILDDIR = build_aarch64/
else ifeq (x86_64,$(P_ARCH))
CFLAGS += -DARCH_X86_64
CXXFLAGS += -DARCH_X86_64
AFLAGS += -DARCH_X86_64
BUILDDIR = build_x86_64/
else ifeq (arm,$(P_ARCH))
CFLAGS += -DARCH_ARM
CXXFLAGS += -DARCH_ARM
AFLAGS += -DARCH_ARM
BUILDDIR = build_arm/
else ifeq (riscv64,$(P_ARCH))
CFLAGS += -DARCH_RISCV
CXXFLAGS += -DARCH_RISCV
AFLAGS += -DARCH_RISCV
BUILDDIR = build_riscv/
else
$(error "Unsupported platform $(P_ARCH), we only handle arm, aarch64, riscv, and x86_64")
endif
GLIBCDIR = $(dirname $(shell $(CC) --print-file-name=libc.so))
CRTI = $(shell $(CC) --print-file-name=crti.o)
CRTBEGIN = $(shell $(CC) --print-file-name=crtbegin.o)
CRTEND = $(shell $(CC) --print-file-name=crtend.o)
CRTN = $(shell $(CC) --print-file-name=crtn.o)
# We do not include crt1.o in STARTFILES because we specify our own _start.
STARTFILES = $(CRTI) $(CRTBEGIN)
ENDFILES = $(CRTEND) $(CRTN)
ifeq ($(VERBOSE),)
SHORT_AS = @echo "AS $<"; $(AS)
SHORT_CC = @echo "CC $<"; $(CC)
SHORT_CXX = @echo "CXX $<"; $(CXX)
SHORT_LINK = @echo "LINK $@"; $(LINK)
SHORT_AR = @echo "AR $@"; $(AR)
else
SHORT_AS = $(AS)
SHORT_CC = $(CC)
SHORT_CXX = $(CXX)
SHORT_LINK = $(LINK)
SHORT_AR = $(AR)
endif
ifneq ($(MAKEVERBOSE),)
MAKE += --no-print-directory
short-make = @+echo '>>>' MAKE -C ${1} ${2} ;\
$(MAKE) -C ${1} ${2} && echo '<<<' MAKE -C ${1} ${2}
else
short-make = +$(MAKE) -C ${1} ${2}
endif
# Rules
$(BUILDDIR)%.o: %.s
$(SHORT_AS) -fPIC -c $< -o $@
$(BUILDDIR)%.o: %.S
$(SHORT_AS) -fPIC $(AFLAGS) $(DEPFLAGS) -c $< -o $@
$(BUILDDIR)%.o: %.c
$(SHORT_CC) $(CFLAGS) $(DEPFLAGS) -DDEBUG_GROUP=$(shell echo $< | perl -ne 'm|^(\w+)/|g;print lc($$1)') -c -o $@ $<
$(BUILDDIR)%.o: %.cpp
$(SHORT_CXX) $(CXXFLAGS) $(DEPFLAGS) -DDEBUG_GROUP=$(shell echo $< | perl -ne 'm|^(\w+)/|g;print lc($$1)') -c -o $@ $<
$(BUILDDIR)%.so: %.s
$(SHORT_AS) -fPIC -c $< -o $@
$(BUILDDIR)%.so: %.S
$(SHORT_AS) -fPIC $(AFLAGS) $(DEPFLAGS) -c -o $@ $<
$(BUILDDIR)%.so: %.c
$(SHORT_CC) -fPIC $(CFLAGS) -DDEBUG_GROUP=$(shell echo $< | perl -ne 'm|^(\w+)/|g;print lc($$1)') -c -o $@ $<
$(BUILDDIR)%.so: %.cpp
$(SHORT_CXX) -fPIC $(CXXFLAGS) -DDEBUG_GROUP=$(shell echo $< | perl -ne 'm|^(\w+)/|g;print lc($$1)') -c -o $@ $<