-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMakefile
More file actions
216 lines (191 loc) · 7.63 KB
/
Makefile
File metadata and controls
216 lines (191 loc) · 7.63 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
SHELL := /bin/bash
.PHONY: all build build-target build-linux build-linux-amd64 build-linux-arm64 build-macos build-macos-amd64 build-macos-arm64 build-macos-universal build-all clean test test-coverage deps fmt lint smoke-macos smoke-macos-browser help
# Build variables
GO_MODULE = github.com/puzed/wrapguard
BINARY_NAME = wrapguard
VERSION ?= 1.0.0-dev
DIST_DIR ?= dist
TARGET_GOOS ?= $(shell go env GOOS)
TARGET_GOARCH ?= $(shell go env GOARCH)
TARGET_DIR ?= .
GO_BUILD_FLAGS = -ldflags="-s -w -X main.version=$(VERSION)"
LIBRARY_NAME = $(if $(filter darwin,$(TARGET_GOOS)),libwrapguard.dylib,libwrapguard.so)
BROWSER_APP ?=
BROWSER_ARGS_TEMPLATE ?= --no-remote -profile __PROFILE__
BROWSER_PROFILE_DIR ?=
SMOKE_URL ?= http://icanhazip.com
WG_CONFIG ?=
WG_LOG_FILE ?= /tmp/wrapguard-browser-smoke.log
ifeq ($(TARGET_GOOS),darwin)
ifeq ($(TARGET_GOARCH),amd64)
DARWIN_ARCH = x86_64
else ifeq ($(TARGET_GOARCH),arm64)
DARWIN_ARCH = arm64
else
DARWIN_ARCH = $(TARGET_GOARCH)
endif
C_COMPILER ?= clang
GO_CGO_ENV = CGO_ENABLED=1 CC="$(C_COMPILER)" CGO_CFLAGS="-arch $(DARWIN_ARCH)" CGO_LDFLAGS="-arch $(DARWIN_ARCH)"
C_ARCH_FLAGS = -arch $(DARWIN_ARCH)
C_SHARED_FLAGS = -dynamiclib
C_WARNING_FLAGS = -Wall -Wextra -Wpedantic -O2
C_LINK_FLAGS = -Wl,-undefined,dynamic_lookup
else
C_COMPILER ?= gcc
GO_CGO_ENV = CGO_ENABLED=1 CC="$(C_COMPILER)"
C_ARCH_FLAGS =
C_SHARED_FLAGS = -shared -fPIC
C_WARNING_FLAGS = -Wall -Wextra -Wpedantic -O2
C_LINK_FLAGS = -ldl
endif
# Default target
all: build
# Build the current host platform into the requested output directory
build: build-target
build-target:
@mkdir -p "$(TARGET_DIR)"
@echo "Building $(TARGET_GOOS)/$(TARGET_GOARCH) into $(TARGET_DIR)..."
@GOOS=$(TARGET_GOOS) GOARCH=$(TARGET_GOARCH) $(GO_CGO_ENV) go build $(GO_BUILD_FLAGS) -o "$(TARGET_DIR)/$(BINARY_NAME)" .
@$(C_COMPILER) $(C_ARCH_FLAGS) $(C_SHARED_FLAGS) $(C_WARNING_FLAGS) lib/intercept.c $(C_LINK_FLAGS) -o "$(TARGET_DIR)/$(LIBRARY_NAME)"
build-linux: TARGET_GOOS = linux
build-linux: TARGET_DIR = $(DIST_DIR)/linux-$(TARGET_GOARCH)
build-linux: build-target
build-linux-amd64:
@$(MAKE) build-linux TARGET_GOARCH=amd64
build-linux-arm64:
@$(MAKE) build-linux TARGET_GOARCH=arm64 C_COMPILER=aarch64-linux-gnu-gcc
build-macos: TARGET_GOOS = darwin
build-macos: TARGET_DIR = $(DIST_DIR)/darwin-$(TARGET_GOARCH)
build-macos: build-target
build-macos-amd64:
@$(MAKE) build-macos TARGET_GOARCH=amd64
build-macos-arm64:
@$(MAKE) build-macos TARGET_GOARCH=arm64
build-macos-universal: TARGET_GOOS = darwin
build-macos-universal:
@if [ "$$(uname -s)" != "Darwin" ]; then \
echo "build-macos-universal must be run on macOS"; \
exit 1; \
fi
@set -euo pipefail; \
stage_dir="$$(mktemp -d)"; \
final_dir="$(DIST_DIR)/darwin-universal"; \
trap 'rm -rf "$$stage_dir"' EXIT; \
$(MAKE) build-target TARGET_GOOS=darwin TARGET_GOARCH=amd64 TARGET_DIR="$$stage_dir/amd64" C_COMPILER=clang; \
$(MAKE) build-target TARGET_GOOS=darwin TARGET_GOARCH=arm64 TARGET_DIR="$$stage_dir/arm64" C_COMPILER=clang; \
mkdir -p "$$final_dir"; \
lipo -create "$$stage_dir/amd64/$(BINARY_NAME)" "$$stage_dir/arm64/$(BINARY_NAME)" -output "$$final_dir/$(BINARY_NAME)"; \
lipo -create "$$stage_dir/amd64/$(LIBRARY_NAME)" "$$stage_dir/arm64/$(LIBRARY_NAME)" -output "$$final_dir/$(LIBRARY_NAME)"; \
chmod +x "$$final_dir/$(BINARY_NAME)"; \
echo "Built universal macOS binaries in $$final_dir"
build-all: build-linux-amd64 build-linux-arm64 build-macos-amd64 build-macos-arm64
# Clean build artifacts
clean:
@echo "Cleaning build artifacts..."
rm -rf "$(DIST_DIR)" "$(BINARY_NAME)" "$(LIBRARY_NAME)"
go clean
# Run tests
test:
@echo "Running tests..."
go test -v ./...
# Run tests with coverage
test-coverage:
@echo "Running tests with coverage..."
go test -cover ./...
# Build debug version
debug: GO_BUILD_FLAGS = -ldflags="-X main.version=$(VERSION)-debug"
debug: C_WARNING_FLAGS += -g -O0
debug: build
# Install dependencies
deps:
@echo "Installing dependencies..."
go mod download
# Format code
fmt:
@echo "Formatting Go code..."
go fmt ./...
# Run linter
lint:
@echo "Running linter..."
go vet ./...
# Validate a local macOS package end to end
smoke-macos:
@if [ "$$(uname -s)" != "Darwin" ]; then \
echo "smoke-macos must be run on macOS"; \
exit 1; \
fi
@set -euo pipefail; \
$(MAKE) build-macos TARGET_GOARCH=$(TARGET_GOARCH); \
staging="$$(mktemp -d)"; \
package_dir="$$staging/package"; \
verify_dir="$$staging/verify"; \
mkdir -p "$$package_dir" "$$verify_dir"; \
cp "$(DIST_DIR)/darwin-$(TARGET_GOARCH)/$(BINARY_NAME)" "$$package_dir/"; \
cp "$(DIST_DIR)/darwin-$(TARGET_GOARCH)/$(LIBRARY_NAME)" "$$package_dir/"; \
cp README.md example-wg0.conf "$$package_dir/"; \
tar -C "$$package_dir" -czf "$$staging/$(BINARY_NAME)-macos-smoke.tar.gz" $(BINARY_NAME) $(LIBRARY_NAME) README.md example-wg0.conf; \
tar -xzf "$$staging/$(BINARY_NAME)-macos-smoke.tar.gz" -C "$$verify_dir"; \
test -x "$$verify_dir/$(BINARY_NAME)"; \
test -f "$$verify_dir/$(LIBRARY_NAME)"; \
chmod +x "$$verify_dir/$(BINARY_NAME)"; \
"$$verify_dir/$(BINARY_NAME)" --version; \
"$$verify_dir/$(BINARY_NAME)" --help; \
rm -rf "$$staging"
# Launch an experimental macOS browser target through WrapGuard with a fresh profile
smoke-macos-browser:
@if [ "$$(uname -s)" != "Darwin" ]; then \
echo "smoke-macos-browser must be run on macOS"; \
exit 1; \
fi
@if [ -z "$(WG_CONFIG)" ]; then \
echo "WG_CONFIG=/path/to/config.conf is required"; \
exit 1; \
fi
@if [ -z "$(BROWSER_APP)" ]; then \
echo "BROWSER_APP=/Applications/LibreWolf.app/Contents/MacOS/librewolf is required"; \
exit 1; \
fi
@set -euo pipefail; \
$(MAKE) build; \
profile_dir="$(BROWSER_PROFILE_DIR)"; \
if [ -z "$$profile_dir" ]; then \
profile_dir="$$(mktemp -d /tmp/wrapguard-browser-profile.XXXXXX)"; \
echo "Using temporary browser profile: $$profile_dir"; \
else \
mkdir -p "$$profile_dir"; \
echo "Using browser profile: $$profile_dir"; \
fi; \
args_template='$(BROWSER_ARGS_TEMPLATE)'; \
browser_args="$${args_template//__PROFILE__/$$profile_dir}"; \
echo "Logging to $(WG_LOG_FILE)"; \
echo "Suggested validation URL: $(SMOKE_URL)"; \
echo "Launching $(BROWSER_APP) $$browser_args"; \
eval "./$(BINARY_NAME) --config=\"$(WG_CONFIG)\" --log-level=debug --log-file=\"$(WG_LOG_FILE)\" -- \"$(BROWSER_APP)\" $$browser_args"
# Run demo
demo: build
@echo "Running demo..."
cd demo && ./setup.sh && docker-compose up
# Help
help:
@echo "Available targets:"
@echo " all - Build the current host platform (default)"
@echo " build - Build the current host platform"
@echo " build-linux - Build a Linux package into dist/"
@echo " build-linux-amd64 - Build a Linux amd64 package into dist/"
@echo " build-linux-arm64 - Build a Linux arm64 package into dist/"
@echo " build-macos - Build a macOS package into dist/"
@echo " build-macos-amd64 - Build a macOS amd64 package into dist/"
@echo " build-macos-arm64 - Build a macOS arm64 package into dist/"
@echo " build-macos-universal - Build a universal macOS package into dist/"
@echo " build-all - Build all packaged Linux and macOS variants"
@echo " clean - Clean build artifacts"
@echo " test - Run tests"
@echo " test-coverage - Run tests with coverage"
@echo " debug - Build debug version"
@echo " deps - Install dependencies"
@echo " fmt - Format Go code"
@echo " lint - Run go vet"
@echo " smoke-macos - Validate a local macOS package end to end"
@echo " smoke-macos-browser - Launch a macOS browser target via WrapGuard with a fresh profile"
@echo " demo - Run demo"
@echo " help - Show this help"