-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
113 lines (92 loc) · 3.8 KB
/
Copy pathMakefile
File metadata and controls
113 lines (92 loc) · 3.8 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
.PHONY: all build demo lib package clean php-runtime c-static-lib go-demo
# OS detection and sensible defaults for compilers (best-effort cross-platform)
UNAME_S := $(shell uname -s 2>/dev/null)
ifeq ($(UNAME_S),Darwin)
DEFAULT_CC := clang
DEFAULT_MUSL_CC := clang
else ifeq ($(OS),Windows_NT)
DEFAULT_CC := gcc
DEFAULT_MUSL_CC := gcc
EXE := .exe
else
DEFAULT_CC := cc
# Linux default musl toolchain (adjust for your arch if needed)
DEFAULT_MUSL_CC := /usr/local/musl/bin/x86_64-linux-musl-gcc
endif
PHP_VERSION := 8.4
SPC_PHP := $(shell pwd)/static-php-cli/bin/php
SPC_BIN := $(shell pwd)/static-php-cli/bin/spc
SPC := $(SPC_PHP) $(SPC_BIN)
EXTS := ast,tokenizer
PATH := $(shell pwd)/static-php-cli/bin:$(PATH)
spc: export PATH := $(shell pwd)/static-php-cli/bin:$(PATH)
spc:
mkdir -p log build buildroot downloads source
rm -rf static-php-cli || true
git clone https://github.com/crazywhalecc/static-php-cli.git
cd static-php-cli && chmod +x bin/setup-runtime
cd static-php-cli && bin/setup-runtime
cd static-php-cli && bin/composer install --no-dev
static-php-cli/bin/composer install --no-dev
cd static-php-cli && chmod +x bin/spc
ln -sf static-php-cli/bin/spc spc
$(SPC) --version
$(SPC) doctor || sudo $(SPC) doctor
$(SPC) download --with-php=$(PHP_VERSION) 8.4 --for-extensions $(EXTS)
# Build the embedded PHP runtime (libs under build/lib)
php-runtime: build-lib
build-lib: spc
$(SPC) build --build-embed "ast"
mkdir -p build
#rm -rf build/lib/embed-test
#mv source/embed-test build/lib
# rpath for non-Windows; empty on Windows
ifeq ($(OS),Windows_NT)
RPATH_FLAG :=
else
RPATH_FLAG := -Wl,-rpath,'$$ORIGIN'
endif
CC ?= $(DEFAULT_CC)
CFLAGS ?= -O3 -fPIC
MUSL_CC ?= $(DEFAULT_MUSL_CC)
# Build the C static library for the bridge
c-static-lib: lib
lib: build-lib v1/ast_bridge.c v1/ast_bridge.h
$(MUSL_CC) $(CFLAGS) $(shell $(SPC) spc-config $(EXTS) --includes | tr '\n' ' ') -c v1/ast_bridge.c -o v1/ast_bridge.o
ar rcs v1/libastbridge.a v1/ast_bridge.o
# Build the Go demo (CGO links against the built libs)
go-demo: demo
demo: lib
@mkdir -p bin
GOFLAGS=-mod=mod CC=$(MUSL_CC) CGO_ENABLED=1 \
CGO_CFLAGS="$(shell $(SPC) spc-config $(EXTS) --includes | tr '\n' ' ')" \
CGO_LDFLAGS="$(shell $(SPC) spc-config $(EXTS) --libs | tr '\n' ' ') $(RPATH_FLAG)" \
go build -v -o bin/demo$(EXE) ./demo
# Easy distribution. Should replace with a fetch go script, and a release publish step.
TARGET ?= linux-amd64
package: lib
@echo "Packaging native artifacts to v1/prebuilt/$(TARGET)"
@rm -rf v1/prebuilt/$(TARGET)
@mkdir -p v1/prebuilt/$(TARGET)/include v1/prebuilt/$(TARGET)/lib
# Prepare includes
@if [ -f v1/libastbridge.a ]; then cp -f v1/libastbridge.a v1/prebuilt/$(TARGET)/lib/; else echo "libastbridge.a introuvable (run 'make lib')"; exit 1; fi
@if [ -f v1/ast_bridge.h ]; then cp -f v1/ast_bridge.h v1/prebuilt/$(TARGET)/include/; else echo "ast_bridge.h introuvable"; exit 1; fi
# PHP embed (spc)
@if [ -d buildroot/include/php ]; then cp -a buildroot/include/php v1/prebuilt/$(TARGET)/include/; else echo "headers PHP not found (run 'make build-lib')"; exit 1; fi
@if [ -f buildroot/lib/libphp.a ]; then cp -f buildroot/lib/libphp.a v1/prebuilt/$(TARGET)/lib/; else echo "libphp.a not found (run 'make build-lib')"; exit 1; fi
# Cleanup
@rm -rf v1/prebuilt/$(TARGET)/embed v1/prebuilt/$(TARGET)/embed-test v1/prebuilt/$(TARGET)/embed.c v1/prebuilt/$(TARGET)/embed.php
@echo "OK:"
@du -hs v1/prebuilt/$(TARGET)
@find v1/prebuilt/$(TARGET) -maxdepth 2 -type f -print
# Build everything in one shot (runtime, C lib, demo, and package artifacts)
all: spc php-runtime c-static-lib go-demo package
clean:
rm -f v1/*.o v1/libastbridge.a
rm -f *.o *.a
rm -rf bin
rm -f spc
rm -rf static-php-cli
rm -Rf build
rm -rf v1/prebuilt
rm -rf pkgroot source downloads buildroot static-php-cli vendor log