Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
83 lines (68 loc) · 2.6 KB
/
Copy pathMakefile
File metadata and controls
83 lines (68 loc) · 2.6 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
# Competitive Programming Archive - build and run one solution.
#
# make run F=codeforces/1122/A compile, then feed A.in on stdin
# make build F=codeforces/1122/A compile only
# make run F=codeforces/1122/A SAN=1 same, with address/UB sanitizers
# make run F=codeforces/1122/A STD=c++17 build against another standard
# make clean remove the build/ tree
#
# F is a path relative to this file, with or without the .cpp suffix.
# When the sibling .in is missing the program reads your terminal instead.
# Matches the "GNU G++23" option on Codeforces. Override per-run: STD=c++17
STD ?= c++23
CXXFLAGS := -std=$(STD) -O2 -Wall -Wextra -Wshadow
# Each flag combination gets its own tree. Make compares timestamps, not flags,
# so a shared directory would hand back a binary built with the other settings.
ifeq ($(SAN),1)
CXXFLAGS += -g -fsanitize=address,undefined -fno-omit-frame-pointer
BUILDDIR := build/$(STD)-san
else
BUILDDIR := build/$(STD)
endif
# Codeforces builds with real GCC, and Apple clang has no <bits/stdc++.h>, so
# only Homebrew's versioned g++ counts. Override with `make GXX=/path/to/g++`.
GXX := $(shell for v in 20 19 18 17 16 15 14 13 12; do command -v g++-$$v && break; done)
.DEFAULT_GOAL := help
.PHONY: help clean build run require-gcc
help:
@echo 'usage:'
@echo ' make run F=codeforces/1122/A compile, then run against A.in'
@echo ' make build F=codeforces/1122/A compile only'
@echo ' make run F=codeforces/1122/A SAN=1 add address/UB sanitizers'
@echo ' make run F=codeforces/1122/A STD=c++17 pick another standard'
@echo ' make clean remove the build/ tree'
@echo ''
@echo "compiler: $(if $(GXX),$(GXX),none found - run 'brew install gcc')"
require-gcc:
@if [ -z "$(GXX)" ]; then \
echo 'error: no Homebrew GCC found (looked for g++-12 .. g++-20).' >&2; \
echo 'Codeforces builds with real GCC, and Apple clang has no <bits/stdc++.h>.' >&2; \
echo '' >&2; \
echo ' brew install gcc' >&2; \
echo '' >&2; \
exit 1; \
fi
ifeq ($(strip $(F)),)
build run:
@echo 'error: set F to the solution path, e.g.' >&2
@echo " make $@ F=codeforces/1122/A" >&2
@exit 1
else
STEM := $(patsubst %.cpp,%,$(F))
BIN := $(BUILDDIR)/$(STEM)
IN := $(STEM).in
build: $(BIN)
run: $(BIN)
@if [ -f '$(IN)' ]; then \
echo '--- $(BIN) < $(IN) ---'; \
'$(BIN)' < '$(IN)'; \
else \
echo '--- $(BIN) (no $(IN); type input, Ctrl-D to end) ---'; \
'$(BIN)'; \
fi
endif
$(BUILDDIR)/%: %.cpp | require-gcc
@mkdir -p $(dir $@)
$(GXX) $(CXXFLAGS) -o $@ $<
clean:
rm -rf build