-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
195 lines (154 loc) · 5.68 KB
/
Makefile
File metadata and controls
195 lines (154 loc) · 5.68 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# ==============================
# Toolchains
# ==============================
CC = gcc
RUST_DIR = rust
# Cargo emits the staticlib under deps/ (not next to libbitgrain.so).
RUST_TARGET = $(RUST_DIR)/target/release/deps/libbitgrain.a
TARGET = bitgrain
# Base C flags
CFLAGS = -std=c11 -Wall -Wextra -Iincludes -Ic
# Release optimizations. Portable: make CFLAGS_NATIVE= RUSTFLAGS_NATIVE=
CFLAGS += -O3 -DNDEBUG
CFLAGS_NATIVE ?= -march=native
RUSTFLAGS_NATIVE ?= -C target-cpu=native
CFLAGS += $(CFLAGS_NATIVE)
# Aggressive math flags only for compute hot paths.
# Keep this scoped (not global) to avoid unintended behavior changes elsewhere.
HOT_MATH_CFLAGS = -ffast-math -fno-math-errno -fno-trapping-math
# WebP (standard; requires libwebp). pkg-config sets -I and -L when present.
WEBP_CFLAGS := $(shell pkg-config --cflags libwebp 2>/dev/null)
WEBP_LIBS := $(shell pkg-config --libs libwebp 2>/dev/null)
ifeq ($(WEBP_LIBS),)
WEBP_LIBS = -lwebp
endif
CFLAGS += -DBITGRAIN_USE_WEBP $(WEBP_CFLAGS)
# libpng for ICC profile support (optional)
PNG_CFLAGS := $(shell pkg-config --cflags libpng 2>/dev/null)
PNG_LIBS := $(shell pkg-config --libs libpng 2>/dev/null)
ifneq ($(PNG_LIBS),)
CFLAGS += -DBITGRAIN_USE_PNG_ICC $(PNG_CFLAGS)
LDFLAGS_EXTRA += $(PNG_LIBS)
endif
LDFLAGS_EXTRA ?=
# OS-specific libs
UNAME_S := $(shell uname -s 2>/dev/null || echo Unknown)
ifeq ($(UNAME_S),Linux)
LDFLAGS_EXTRA += -lpthread -ldl -lm $(WEBP_LIBS)
endif
ifeq ($(UNAME_S),Darwin)
LDFLAGS_EXTRA += -lpthread -ldl -lm $(WEBP_LIBS)
endif
ifeq ($(UNAME_S),Unknown)
LDFLAGS_EXTRA += -lpthread -ldl -lm $(WEBP_LIBS)
endif
LDFLAGS = $(LDFLAGS_EXTRA)
C_SRCS = \
c/dct.c \
c/quant.c \
c/bg_utils.c \
c/path_utils.c \
c/cli.c \
c/roundtrip_cli.c \
c/decode_cli.c \
c/encode_cli.c \
c/image_loader.c \
c/image_writer.c \
c/icc_io.c \
c/platform.c \
c/metrics.c \
c/webp_io.c \
main.c
C_OBJS = $(C_SRCS:.c=.o)
# ==============================
# Default target
# ==============================
all: build
build: bitgrain
# ==============================
# Full build
# ==============================
bitgrain: $(RUST_TARGET) $(C_OBJS)
$(CC) $(C_OBJS) $(RUST_TARGET) -o $(TARGET) $(LDFLAGS)
strip $(TARGET) 2>/dev/null || true
# ==============================
# Rust build
# ==============================
$(RUST_TARGET):
cd $(RUST_DIR) && CARGO_TARGET_DIR="$(abspath $(RUST_DIR)/target)" RUSTFLAGS="$(RUSTFLAGS_NATIVE)" cargo build --release
# Shared library for Python/Go bindings (Rust cdylib: libbitgrain.so or .dylib)
RUST_SO = $(RUST_DIR)/target/release/libbitgrain.so
ifeq ($(UNAME_S),Darwin)
RUST_SO = $(RUST_DIR)/target/release/libbitgrain.dylib
endif
lib-shared: $(RUST_TARGET)
@test -f $(RUST_SO) && echo "Shared lib: $(RUST_SO)" || true
# ==============================
# C build
# ==============================
c: $(C_OBJS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
c/dct.o: c/dct.c
$(CC) $(CFLAGS) $(HOT_MATH_CFLAGS) -c $< -o $@
c/quant.o: c/quant.c
$(CC) $(CFLAGS) $(HOT_MATH_CFLAGS) -c $< -o $@
main.o: main.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: all build c bench clean install rebuild lib-shared build-portable build-avx2 bench-avx2
# ==============================
# Bench (standalone profiler)
# ==============================
BENCH_TARGET = bitgrain-bench
BENCH_CFLAGS = -std=c11 -Wall -Wextra -Iincludes -Ic -Ibench -O3 -DNDEBUG $(CFLAGS_NATIVE)
BENCH_SRCS = bench/bench.c bench/main.c c/dct.c c/quant.c c/metrics.c
BENCH_OBJS = $(BENCH_SRCS:.c=.o)
bench: $(RUST_TARGET) $(BENCH_OBJS)
$(CC) $(BENCH_OBJS) $(RUST_TARGET) -o $(BENCH_TARGET) $(LDFLAGS)
@echo "Built: $(BENCH_TARGET)"
bench/bench.o: bench/bench.c bench/bench.h
$(CC) $(BENCH_CFLAGS) -c $< -o $@
bench/main.o: bench/main.c bench/bench.h
$(CC) $(BENCH_CFLAGS) -c $< -o $@
# ==============================
# Clean
# ==============================
clean:
rm -f $(C_OBJS) c/webp_io.o $(TARGET) $(BENCH_TARGET) $(BENCH_OBJS)
cd $(RUST_DIR) && CARGO_TARGET_DIR="$(abspath $(RUST_DIR)/target)" cargo clean
# ==============================
# Install (C library + CLI)
# ==============================
# Usage: make install [PREFIX=/usr/local]
PREFIX ?= /usr/local
install: bitgrain
install -d $(DESTDIR)$(PREFIX)/bin
install -m 755 bitgrain $(DESTDIR)$(PREFIX)/bin/
install -d $(DESTDIR)$(PREFIX)/include/bitgrain
install -m 644 includes/encoder.h $(DESTDIR)$(PREFIX)/include/bitgrain/
install -d $(DESTDIR)$(PREFIX)/lib
install -m 644 $(RUST_TARGET) $(DESTDIR)$(PREFIX)/lib/libbitgrain.a
install -d $(DESTDIR)$(PREFIX)/share/bash-completion/completions
install -m 644 completions/bitgrain.bash $(DESTDIR)$(PREFIX)/share/bash-completion/completions/bitgrain
install -d $(DESTDIR)$(PREFIX)/share/man/man1
install -m 644 man/bitgrain.1 $(DESTDIR)$(PREFIX)/share/man/man1/bitgrain.1
@if [ -f $(RUST_SO) ]; then install -m 755 $(RUST_SO) $(DESTDIR)$(PREFIX)/lib/; echo "Installed shared lib: $(PREFIX)/lib/"; fi
@echo "Installed: $(DESTDIR)$(PREFIX)/bin/bitgrain, include/bitgrain/encoder.h, lib/libbitgrain.a, completion and manpage"
# ==============================
# Rebuild
# ==============================
rebuild: clean build
# ==============================
# CPU-specific optimization presets (opt-in)
# ==============================
# Portable baseline (safe to distribute across CPUs):
# make build-portable
build-portable:
$(MAKE) CFLAGS_NATIVE= RUSTFLAGS_NATIVE= build
# x86_64 AVX2/FMA preset (host must support these ISA extensions):
# make build-avx2
# make bench-avx2
build-avx2:
$(MAKE) CFLAGS_NATIVE="-mavx2 -mfma -mbmi2 -mlzcnt" RUSTFLAGS_NATIVE="-C target-feature=+avx2,+fma,+bmi2,+lzcnt" build
bench-avx2:
$(MAKE) CFLAGS_NATIVE="-mavx2 -mfma -mbmi2 -mlzcnt" RUSTFLAGS_NATIVE="-C target-feature=+avx2,+fma,+bmi2,+lzcnt" bench