Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
137 lines (114 loc) · 5.3 KB
/
Copy pathMakefile
File metadata and controls
137 lines (114 loc) · 5.3 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
# VirtualAP engine - build system
# Copyright (C) 2026 ravindu644 <droidcasts@protonmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Builds the static musl `virtualap` binary from src/ the way Droidspaces does:
# `make aarch64 armhf` with the musl-cross gcc in ~/toolchains/<triplet>-cross
# (or MUSL_CROSS), straight into backend/<arch>/ next to the binaries that
# scripts/build-static.sh builds. It links nothing but libc, so unlike hostapd
# it needs no container.
BINARY_NAME = virtualap
SRC_DIR = src
OUT_DIR = output
# VAP_VERSION in virtualap.h is the one place the version lives; `make bump` rewrites it
VERSION := $(shell grep 'define VAP_VERSION' $(SRC_DIR)/include/virtualap.h | cut -d'"' -f2)
$(if $(VERSION),,$(error VAP_VERSION not found in $(SRC_DIR)/include/virtualap.h))
V ?= 0
ifeq ($(V),1)
Q =
msg_cc =
msg_ld =
else
Q = @
msg_cc = @printf " CC %s\n" $<
msg_ld = @printf " LD %s\n" $@
endif
# Every .c under src/ is part of the binary, so a new module needs no edit here.
SRCS := $(shell find $(SRC_DIR) -name '*.c' | sort)
# gcc only: -Wlogical-op and the -Wduplicated-* family are unknown to clang.
CFLAGS = -Wall -Wextra -Wpedantic -O2 -std=gnu11 -D_GNU_SOURCE -I$(SRC_DIR)/include -fno-pie
CFLAGS += -Wformat=2 -Wformat-overflow=2 -Wformat-truncation=2
CFLAGS += -Wnull-dereference -Wcast-qual -Wlogical-op -Wshadow -Wdouble-promotion -Wundef
CFLAGS += -Wduplicated-cond -Wduplicated-branches -Wimplicit-fallthrough=3
CFLAGS += -fstack-protector-strong
CFLAGS += -MMD -MP
# Only CI turns warnings into errors: its toolchain is pinned, a local
# musl-cross gcc of another version warns differently.
ifeq ($(WERROR),1)
CFLAGS += -Werror
endif
# -no-pie: a static-PIE still wants /lib/ld-musl-*.so.1 at runtime, which
# Android does not have; -fno-pie above keeps the objects to match.
LDFLAGS = -static -no-pie
ifeq ($(NO_STRIP),)
LDFLAGS += -s
endif
ARCH := $(shell $(CC) -dumpmachine 2>/dev/null | cut -d'-' -f1 | sed 's/^arm.*/armhf/')
# armeabi-v7a's floor. The musl-cross gcc defaults to armv5te, which runs on
# every phone but gives up Thumb-2 and the armv7 atomics.
CFLAGS_armhf = -march=armv7-a -mfpu=vfpv3-d16
CFLAGS += $(CFLAGS_$(ARCH))
OBJ_DIR = $(OUT_DIR)/.obj/$(ARCH)
OBJS = $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
BINARY = $(OUT_DIR)/$(BINARY_NAME)-$(ARCH)
find-cc = $(shell \
if [ -n "$(MUSL_CROSS)" ] && [ -f "$(MUSL_CROSS)/$(1)-gcc" ]; then \
echo "$(MUSL_CROSS)/$(1)-gcc"; \
elif [ -f "$(HOME)/toolchains/$(1)-cross/bin/$(1)-gcc" ]; then \
echo "$(HOME)/toolchains/$(1)-cross/bin/$(1)-gcc"; \
elif command -v $(1)-gcc >/dev/null 2>&1; then \
echo "$(1)-gcc"; \
fi)
triplet_aarch64 = aarch64-linux-musl
triplet_armhf = arm-linux-musleabihf
.DEFAULT_GOAL := help
.PHONY: help aarch64 armhf check bump version format clean
help:
@echo "VirtualAP v$(VERSION) engine"
@echo " make aarch64 - cross build into backend/aarch64/$(BINARY_NAME)"
@echo " make armhf - cross build into backend/armhf/$(BINARY_NAME)"
@echo " make check - build and run tests/check.c with the host gcc"
@echo " make bump NEW=x.y.z - set the version in src/include/virtualap.h"
@echo " make format - clang-format src/ and tests/"
@echo " make clean - remove output/"
@echo "Options: V=1, WERROR=1, NO_STRIP=1, MUSL_CROSS=<dir with <triplet>-gcc>"
# The flags come from the Makefile, so a flag edit rebuilds; -MMD covers the headers.
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c Makefile
$(msg_cc)
$(Q)mkdir -p $(dir $@)
$(Q)$(CC) $(CFLAGS) -c $< -o $@
$(BINARY): $(OBJS)
$(msg_ld)
$(Q)$(CC) $(OBJS) -o $@ $(LDFLAGS)
@echo "[+] Built: $@"
# The sub-make re-derives ARCH, OBJ_DIR and BINARY from the cross compiler.
aarch64 armhf:
@CROSS_CC="$(call find-cc,$(triplet_$@))"; \
[ -n "$$CROSS_CC" ] || { echo "Error: $(triplet_$@)-gcc not found, set MUSL_CROSS"; exit 1; }; \
$(MAKE) --no-print-directory $(OUT_DIR)/$(BINARY_NAME)-$@ CC=$$CROSS_CC && \
mkdir -p backend/$@ && cp -f $(OUT_DIR)/$(BINARY_NAME)-$@ backend/$@/$(BINARY_NAME)
# Host-side asserts over the pure logic: everything but main.c, dynamically
# linked with whatever gcc the host has, never shipped.
# ponytail: runs on the 64-bit host only, so a bug that needs 32-bit longs
# slips past it; run it under qemu-arm if one ever does.
# Headers are listed too: there is no .d file for this one-shot link.
CHECK_BIN = $(abspath $(OUT_DIR))/check
$(CHECK_BIN): $(wildcard tests/*.[ch]) $(filter-out $(SRC_DIR)/main.c,$(SRCS)) $(shell find $(SRC_DIR) -name '*.h') Makefile
@mkdir -p $(dir $@)
$(Q)gcc -Wall -Wextra -O1 -g -std=gnu11 -D_GNU_SOURCE -I$(SRC_DIR)/include -Itests $(filter %.c,$^) -o $@
check: $(CHECK_BIN)
$(Q)$(CHECK_BIN)
# CI asks here, so the header has one parser.
version:
@echo "$(VERSION)"
bump:
@echo "$(NEW)" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$$' || { echo "Usage: make bump NEW=x.y.z"; exit 1; }
@sed -i 's/define VAP_VERSION[[:space:]]*".*"/define VAP_VERSION "$(NEW)"/' $(SRC_DIR)/include/virtualap.h
@grep -q 'define VAP_VERSION "$(NEW)"' $(SRC_DIR)/include/virtualap.h || { echo "Error: VAP_VERSION not rewritten"; exit 1; }
@echo "[+] Version $(VERSION) -> $(NEW). Commit it as 'VirtualAP: bump v$(NEW)', then tag v$(NEW)."
format:
@find $(SRC_DIR) tests -name '*.[ch]' | xargs clang-format -i
clean:
@rm -rf $(OUT_DIR)
# Last, so a dependency file can never become the default goal.
-include $(OBJS:.o=.d)