-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
105 lines (85 loc) · 3.66 KB
/
Copy pathMakefile
File metadata and controls
105 lines (85 loc) · 3.66 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
# libphptok — standalone PHP tokenizer built from php-src's own lexer.
#
# Pipeline (all generated artifacts land in build/, nothing generated is
# committed):
# vendor/<tag>/*.l,*.y --re2c--> build/zend_language_scanner.c
# --strip--> build/scanner_noline.c (lex_scan + helpers only)
# --gen--> build/phptok_tokens.h (token enum + names)
# src/phptok.c + src/stubs/* --cc--> build/libphptok.a
# src/tool/phptok-dump.c --cc--> build/phptok-dump
#
# Common targets:
# make build the static lib + the phptok-dump CLI
# make test differential test: phptok-dump vs. PhpToken oracle over tests/corpus
# make extract re-vendor grammar for $(PHP_TAG) from php-src (network)
# make clean
PHP_TAG ?= php-8.5.7
VENDOR := vendor/$(PHP_TAG)
BUILD := build
UNAME_S := $(shell uname -s)
CC ?= cc
OPT ?= -O2
WARN ?= -Wall
CSTD ?= -std=c11
CFLAGS ?= $(OPT) $(WARN) $(CSTD)
LDFLAGS ?=
ifeq ($(UNAME_S),Darwin)
LDFLAGS += -Wl,-dead_strip
else
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections
endif
RE2C ?= re2c
PYTHON ?= python3
# Include search path for compiling the scanner + stubs:
# src/stubs/zend fake Zend headers the vendored scanner #includes
# src/stubs the phptok_zend_stubs.h runtime replacements
# build generated defs + token table
# include the public phptok.h
STUB_INC := -Isrc/stubs/zend -Isrc/stubs -I$(BUILD) -Iinclude
.PHONY: all lib tool test extract generate clean
all: lib tool
# --- 1. re2c: vendored grammar -> scanner C + state-enum defs ---
$(BUILD)/zend_language_scanner.c $(BUILD)/zend_language_scanner_defs.h: \
$(VENDOR)/zend_language_scanner.l | $(BUILD)
$(RE2C) --case-inverted -cbdFt $(BUILD)/zend_language_scanner_defs.h \
-o $(BUILD)/zend_language_scanner.c $<
# --- 2. strip to lex_scan() + the ~20 helpers it reaches ---
$(BUILD)/scanner_stripped.c: $(BUILD)/zend_language_scanner.c bin/strip-scanner.py
$(PYTHON) bin/strip-scanner.py $< $@
# drop re2c's #line directives so compiler diagnostics point at real lines
$(BUILD)/scanner_noline.c: $(BUILD)/scanner_stripped.c
grep -v '^#line' $< > $@
# --- 3. token enum + name table from the parser grammar ---
$(BUILD)/phptok_tokens.h: $(VENDOR)/zend_language_parser.y bin/gen-tokens.py | $(BUILD)
$(PYTHON) bin/gen-tokens.py $< $@
generate: $(BUILD)/scanner_noline.c $(BUILD)/phptok_tokens.h
# --- 4. objects ---
$(BUILD)/scanner.o: $(BUILD)/scanner_noline.c $(BUILD)/phptok_tokens.h
$(CC) $(CFLAGS) $(STUB_INC) -c $< -o $@
$(BUILD)/globals.o: src/stubs/globals.c $(BUILD)/phptok_tokens.h
$(CC) $(CFLAGS) $(STUB_INC) -c $< -o $@
$(BUILD)/phptok.o: src/phptok.c $(BUILD)/phptok_tokens.h include/phptok.h
$(CC) $(CFLAGS) $(STUB_INC) -c $< -o $@
# --- 5. static library ---
lib: $(BUILD)/libphptok.a
$(BUILD)/libphptok.a: $(BUILD)/scanner.o $(BUILD)/globals.o $(BUILD)/phptok.o
ar rcs $@ $^
# --- 6. reference CLIs (public-API-only) ---
tool: $(BUILD)/phptok-dump $(BUILD)/phptok-ts-dump
$(BUILD)/phptok-dump: src/tool/phptok-dump.c $(BUILD)/libphptok.a include/phptok.h
$(CC) $(CFLAGS) -Iinclude -o $@ $< $(BUILD)/libphptok.a $(LDFLAGS)
# tree-sitter compatibility shim demo + differential target
$(BUILD)/phptok-ts-dump: src/tool/phptok-ts-dump.c $(BUILD)/libphptok.a \
include/phptok.h include/phptok_ts.h
$(CC) $(CFLAGS) -Iinclude -o $@ $< $(BUILD)/libphptok.a $(LDFLAGS)
$(BUILD):
mkdir -p $(BUILD)
# --- re-vendor grammar for a different php-src tag: `make extract PHP_TAG=php-8.6.0` ---
extract:
bin/extract-scanner.sh $(PHP_TAG)
test: tool
bin/run-differential.sh
bin/run-differential.sh --bridge
clean:
rm -rf $(BUILD)