-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
103 lines (82 loc) · 2.87 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
.POSIX:
# Override the prefix by running e.g. `make PREFIX=.`
PREFIX = /usr/local
# All assignments (`?=`, `+=`, or `=`) can be overridden
# by calling make as e.g. `make VAR=custom_value`
# ?= allows overriding via environment variables
# += adds to an environment variable if one exists
CC ?= gcc
CFLAGS += -O3 -g -Wall -std=c99
LDFLAGS +=
BUILD_DIR = build
TARGET = ./$(BUILD_DIR)/hextoggle
# Version can be overridden
ifdef VERSION
CFLAGS += -DHEXTOGGLE_VERSION="$(VERSION)"
endif
# REPRODUCIBLE BUILDS
# set timestamps to 0 (used by libtool and ld64 on macOS)
export ZERO_AR_DATE=1
# remove compilation dir from binary
is_fdebug_compilation_dir_supported=$(shell \
touch foo.c && \
$(CC) -fdebug-compilation-dir . -c foo.c -o foo.o >/dev/null 2>/dev/null && \
echo 'yes'; \
rm -f foo.c foo.o)
$(info is_fdebug_compilation_dir_supported=$(is_fdebug_compilation_dir_supported))
ifeq ($(is_fdebug_compilation_dir_supported), yes)
CFLAGS += -fdebug-compilation-dir .
endif
# remove build dir from binary (see `man ld` on macOS)
is_oso_prefix_supported=$(shell \
echo "int main() { return 0; }" >foo.c && \
$(CC) -Wl,-oso_prefix,$(realpath .) foo.c -o foo >/dev/null 2>/dev/null && \
echo 'yes'; \
rm -f foo.c foo)
$(info is_oso_prefix_supported=$(is_oso_prefix_supported))
ifeq ($(is_oso_prefix_supported), yes)
LDFLAGS += -Wl,-oso_prefix,$(realpath $(BUILD_DIR))
endif
DIFF_TOOL = diffoscope --exclude-directory-metadata yes
HEADERS = $(wildcard src/*.h)
SOURCES = $(wildcard src/*.c)
OBJECTS = $(patsubst src/%.c, $(BUILD_DIR)/%.o, $(SOURCES))
.PHONY: default build all clean install uninstall test benchmark
.PRECIOUS: $(TARGET) $(OBJECTS)
build: $(TARGET)
default: build
all: build
$(BUILD_DIR)/%.o: src/%.c $(HEADERS) Makefile
mkdir -p $(BUILD_DIR)
$(CC) -c $< -o $@ $(CPPFLAGS) $(CFLAGS)
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -Wall -o $@ $(LOADLIBES) $(LDLIBS)
# the - means that we are ignoring the return code of this command
clean:
-rm -rf $(BUILD_DIR)
install: build
mkdir -p $(PREFIX)/bin
cp -f $(TARGET) $(PREFIX)/bin
chmod 755 $(PREFIX)/bin/hextoggle
uninstall:
-rm -f $(PREFIX)/bin/hextoggle
test: build
echo test >$(BUILD_DIR)/input.txt
$(TARGET) $(BUILD_DIR)/input.txt $(BUILD_DIR)/hex.txt
$(TARGET) $(BUILD_DIR)/hex.txt $(BUILD_DIR)/output.txt
diff -q $(BUILD_DIR)/input.txt $(BUILD_DIR)/output.txt
rm $(BUILD_DIR)/input.txt $(BUILD_DIR)/output.txt \
$(BUILD_DIR)/hex.txt
benchmark: build
dd if=/dev/random of="$(BUILD_DIR)/bin.txt" bs=1048576 count=64
time $(TARGET) "$(BUILD_DIR)/bin.txt" "$(BUILD_DIR)/hex.txt"
time $(TARGET) "$(BUILD_DIR)/hex.txt" "$(BUILD_DIR)/bin.txt"
rm "$(BUILD_DIR)/bin.txt" "$(BUILD_DIR)/hex.txt"
reproduce:
$(MAKE) BUILD_DIR=$(BUILD_DIR)/a
sleep 2
$(MAKE) BUILD_DIR=$(BUILD_DIR)/b
$(DIFF_TOOL) $(BUILD_DIR)/a $(BUILD_DIR)/b
ci: build reproduce
shasum -a 256 $(TARGET) \
$(BUILD_DIR)/a/hextoggle $(BUILD_DIR)/a/hextoggle