-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
121 lines (95 loc) · 4.38 KB
/
Copy pathMakefile
File metadata and controls
121 lines (95 loc) · 4.38 KB
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
# phpcpd-native — native C clone detector, consuming libphptok.
#
# Build (see README "Building"):
# make optimized, self-contained binary for the host (macOS/Linux)
# make static fully-static binary (Linux; use a musl toolchain — see below)
# make test run every differential suite against real phpcpd
# make clean remove all generated output (recurses into libphptok)
#
# The detector uses ONLY libphptok's public API (phptok.h) + its generated
# token-id constants; libphptok is built first and linked in statically (.a).
UNAME_S := $(shell uname -s)
CC ?= cc
OPT ?= -O2
WARN ?= -Wall
CSTD ?= -std=c11
CFLAGS ?= $(OPT) $(WARN) $(CSTD)
LDFLAGS ?=
# Per-platform dead-code stripping -> smallest self-contained binary.
ifeq ($(UNAME_S),Darwin)
LDFLAGS += -Wl,-dead_strip
else
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections
endif
LIBPHPTOK := libphptok
LIBPHPTOK_A := $(LIBPHPTOK)/build/libphptok.a
BUILD := build
# public API + generated token-id constants (real PHP values)
INC := -Isrc -I$(LIBPHPTOK)/include -I$(LIBPHPTOK)/build
# Forward the toolchain to the libphptok sub-make so static/musl builds are
# consistent across both.
SUBMAKE = $(MAKE) -C $(LIBPHPTOK) CC='$(CC)' CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)'
.PHONY: all static test test-sig test-clone test-text test-suffixtree clean libphptok
all: $(BUILD)/filetokens-dump $(BUILD)/phpcpd-detect
# --- Git submodule automation ------------------------------------------------
# libphptok lives in a Git submodule. If its tree isn't checked out yet
# (fresh `git clone` without --recursive), its Makefile is missing; fetch it.
# Presence of the sub-Makefile is our proxy for "submodule is populated".
$(LIBPHPTOK)/Makefile:
@echo "Initializing Git submodule: $(LIBPHPTOK)..."
git submodule update --init --recursive
# Build the tokenizer subproject (also generates phptok_tokens.h we include).
# Depends on the sub-Makefile so a fresh checkout is auto-initialized first.
# .PHONY: always forward to the sub-make and let it decide what's stale.
libphptok: $(LIBPHPTOK)/Makefile
$(SUBMAKE)
$(LIBPHPTOK_A): libphptok
$(BUILD):
mkdir -p $(BUILD)
# --- object files ---
$(BUILD)/%.o: src/%.c $(LIBPHPTOK_A) | $(BUILD)
$(CC) $(CFLAGS) $(INC) -c $< -o $@
DETECT_OBJS := $(BUILD)/signature.o $(BUILD)/md5.o $(BUILD)/clone.o $(BUILD)/detector.o $(BUILD)/report.o $(BUILD)/suffixtree.o $(BUILD)/tokenbag.o $(BUILD)/finder.o
# --- tools ---
$(BUILD)/filetokens-dump: src/tool/filetokens-dump.c $(BUILD)/signature.o $(LIBPHPTOK_A) | $(BUILD)
$(CC) $(CFLAGS) $(INC) -o $@ $< $(BUILD)/signature.o $(LIBPHPTOK_A) $(LDFLAGS)
$(BUILD)/phpcpd-detect: src/tool/phpcpd-detect.c $(DETECT_OBJS) $(LIBPHPTOK_A) | $(BUILD)
$(CC) $(CFLAGS) $(INC) -o $@ $< $(DETECT_OBJS) $(LIBPHPTOK_A) $(LDFLAGS)
# --- fully-static binary -----------------------------------------------------
# macOS cannot produce fully-static executables (no static libSystem); the
# default `make` there is already self-contained (only libSystem is dynamic).
# On Linux, link statically — a musl toolchain gives the cleanest result:
# Debian/Ubuntu: sudo apt install musl-tools && make static CC=musl-gcc
# Alpine: apk add build-base musl-dev re2c python3 && make static
static:
ifeq ($(UNAME_S),Darwin)
@echo "error: fully-static binaries are unsupported on macOS (no static libSystem)."
@echo " 'make' already yields a self-contained binary (only libSystem is dynamic)."
@echo " For a fully-static binary build on Linux: make static CC=musl-gcc"
@exit 1
else
$(MAKE) clean
$(MAKE) all CC='$(CC)' \
CFLAGS='$(OPT) $(WARN) $(CSTD) -ffunction-sections -fdata-sections' \
LDFLAGS='-static -Wl,--gc-sections'
@echo "static build: $(BUILD)/phpcpd-detect"
endif
test: test-sig test-clone test-text test-suffixtree test-tokenbag test-finder test-report
test-sig: $(BUILD)/filetokens-dump
bin/run-sig-differential.sh
test-clone: $(BUILD)/phpcpd-detect
bin/run-clone-differential.sh
test-text: $(BUILD)/phpcpd-detect
bin/run-text-differential.sh
test-suffixtree: $(BUILD)/phpcpd-detect
bin/run-suffixtree-differential.sh
test-tokenbag: $(BUILD)/phpcpd-detect
bin/run-tokenbag-differential.sh
test-finder: $(BUILD)/phpcpd-detect
bin/run-finder-differential.sh
test-report: $(BUILD)/phpcpd-detect
bin/run-report-differential.sh
clean:
rm -rf $(BUILD)
@if [ -f $(LIBPHPTOK)/Makefile ]; then $(SUBMAKE) clean; fi