-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
executable file
·121 lines (104 loc) · 3.5 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
# Command ard arguments
RM ?= rm -f
GREP ?= grep
LDCONF ?= /sbin/ldconfig
FILE ?= file
# Support MacOS and Linux
MACHINE := $(shell $(CC) -dumpmachine 2>/dev/null)
IS_DARWIN := $(shell echo "$(MACHINE)" | $(GREP) -i -c "Darwin")
IS_LINUX := $(shell echo "$(MACHINE)" | $(GREP) -i -c "Linux")
# General variables
#OPT ?= -O3
OPT ?= -DDEBUG -g -O0
WARNINGS = #-Wall -Wextra
LDLIB_PEEKABOO ?= -lpeekaboo
LDLIBS := $(LDLIB_PEEKABOO)
ifneq ($(IS_DARWIN), 1)
LDLIBS += -lm # MacOS doesn't need link math library
endif
CFLAGS ?= $(OPT) $(WARNINGS)
# Path
PROJ_HOME := .
DIR_PEEKABOO := $(addprefix $(PROJ_HOME)/,libpeekaboo)
# Check if have capstone
ifneq (, $(shell which dpkg))
HAS_CAPSTONE_DEV := $(shell dpkg -s libcapstone-dev | $(GREP) -c -E "Version: ([3-9])")
else
HAS_CAPSTONE_DEV := 0
endif
ifneq (0, $(HAS_CAPSTONE_DEV))
# Disasm with Capstone
CFLAGS += -DASM_CAPSTONE
LDLIBS += -lcapstone
else # HAS_CAPSTONE_DEV
# Disasm with libopcodes
# We only support binutils-dev 2.29 or later for disasm with libopcodes.
# KH: Note that MacOS doesn't use binutils.
HAVE_GAS := $(shell $(CC) -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | $(GREP) -c "GNU assembler")
ifneq ($(HAVE_GAS),0)
GAS229_OR_LATER := $(shell $(CC) -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | $(GREP) -c -E "GNU assembler version (2\.29|2\.[3-9]|[3-9])")
ifneq ($(GAS229_OR_LATER),0)
ifneq (, $(shell which dpkg))
Binutils_dev_229_OR_LATER := $(shell dpkg -s binutils-dev | $(GREP) -c -E "Version: (2\.29|2\.[3-9]|[3-9])")
endif
ifneq (, $(Binutils_dev_229_OR_LATER))
ifeq ($(Binutils_dev_229_OR_LATER), 1)
# We are sure it has right version of binutils-dev
CFLAGS += -DASM
LDLIBS += -lopcodes
else
HAVE_GAS_BUT_NO_DEV := 1
endif
else
# Fixup: It has right version of binutils, but not sure if it has binutils-dev correctly.
CFLAGS += -DASM
LDLIBS += -lopcodes
endif
endif # -DASM
endif # HAVE_GAS
endif # HAS_CAPSTONE_DEV
# Search for libpeekaboo dynamic library on system
ifeq ($(IS_LINUX),1)
# Use ldconf to search for peekaboo in dynamic lib caceh
HAVE_LIBPEEKABOO_SO := $(shell $(LDCONF) -p | $(GREP) -c "peekaboo")
endif
ifeq ($(IS_DARWIN), 1)
# MacOS doesn't have ldconf
HAVE_LIBPEEKABOO_SO ?= $(shell $(FILE) /usr/local/lib/libpeekaboo.dylib | $(GREP) -c -i "dynamically linked shared library")
endif
# Targets and Recipes
PROG := read_trace
all: $(PROG) | binutils_warning
debug: CFLAGS += -DDEBUG -g
debug: all
read_trace: read_trace.o $(LDLIB_PEEKABOO)
ifeq ($(HAVE_LIBPEEKABOO_SO), 0)
@# Cannot find peekaboo installed. Static link!
$(CC) -o $@ $(strip $(CFLAGS) $< $(patsubst -lpeekaboo,$(DIR_PEEKABOO)/libpeekaboo.a,$(LDLIBS)))
else
@# Dynamic link if libpeekaboo has been installed
$(CC) -o $@ $(strip $(CFLAGS) $< $(LDLIBS))
endif
%.o: %.c
$(CC) $(strip $(CFLAGS) -c) $<
$(LDLIB_PEEKABOO):
(cd $(DIR_PEEKABOO) && $(strip $(MAKE) $(patsubst DEBUG,debug,$(findstring DEBUG,$(CFLAGS)))))
.PHONY: clean
clean:
$(RM) *.o *.a *.gch $(PROG)
(cd $(DIR_PEEKABOO) && $(MAKE) clean)
.PHONY: binutils_warning
binutils_warning:
ifeq ($(HAVE_GAS),0)
$(info WARNING: Binutils not found. Disassembling in the trace reader is disabled.)
$(info )
endif
ifeq ($(HAVE_GAS)$(GAS229_OR_LATER),10)
$(info WARNING: Binutils-dev>=2.29 required. Disassembling in the trace reader is disabled.)
$(info )
endif
ifeq ($(HAVE_GAS_BUT_NO_DEV),1)
$(info WARNING: Binutils-dev>=2.29 required. Disassembling in the trace reader is disabled.)
$(info Upgrade your binutils-dev to enable disassembling.)
$(info)
endif