| 1 |
#!/usr/bin/env make |
| 2 |
|
| 3 |
DATEANDTIME=$(shell date +%Y-%m-%dT%T%z) |
| 4 |
|
| 5 |
# External commands |
| 6 |
BLACK ?= black |
| 7 |
CTAGS ?= ctags |
| 8 |
FIND ?= find |
| 9 |
PYTHON ?= python3 |
| 10 |
PYTEST ?= $(PYTHON) -m pytest |
| 11 |
SPHINX ?= $(PYTHON) -m sphinx |
| 12 |
BENCHMARK ?= $(PYTEST) |
| 13 |
RM_R ?= rm -fr |
| 14 |
TOX ?= tox |
| 15 |
# Detect macOS to customize how we query the cpu count. |
| 16 |
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo unknown') |
| 17 |
ifeq ($(uname_S),Darwin) |
| 18 |
NPROC ?= sysctl -n hw.activecpu |
| 19 |
else |
| 20 |
NPROC ?= nproc |
| 21 |
endif |
| 22 |
|
| 23 |
# Options |
| 24 |
flags ?= |
| 25 |
|
| 26 |
# Capture extra arguments for the benchmark target |
| 27 |
EXTRA_BENCH_ARGS ?= |
| 28 |
|
| 29 |
# Default job count -- this is used if "-j" is not present in MAKEFLAGS. |
| 30 |
nproc := $(shell sh -c '$(NPROC) 2>/dev/null || echo 4') |
| 31 |
|
| 32 |
# Extract the "-j#" flags in $(MAKEFLAGS) so that we can forward the value to |
| 33 |
# other commands. This can be empty. |
| 34 |
JOB_FLAGS := $(shell echo -- $(MAKEFLAGS) | grep -o -e '-j[0-9]\+' | head -n 1) |
| 35 |
# Extract just the number from "-j#". |
| 36 |
JOB_COUNT := $(shell printf '%s' "$(JOB_FLAGS)" | sed -e 's/-j//') |
| 37 |
# We have "-jX" from MAKEFLAGS but tox wants "--parallel X" |
| 38 |
DASH_J := $(shell echo -- $(JOB_FLAGS) -j$(nproc) | grep -o -e '-j[0-9]\+' | head -n 1) |
| 39 |
NUM_JOBS := $(shell printf %s "$(DASH_J)" | sed -e 's/-j//') |
| 40 |
|
| 41 |
TESTCMD ?= $(PYTEST) |
| 42 |
BENCHMARKCMD ?= $(BENCHMARK) |
| 43 |
TOXCMD ?= $(TOX) run-parallel --parallel-live |
| 44 |
ifdef V |
| 45 |
TESTCMD += --verbose |
| 46 |
TOXCMD += -v |
| 47 |
BENCHMARKCMD += --benchmark-verbose |
| 48 |
endif |
| 49 |
|
| 50 |
# Data |
| 51 |
ARTIFACTS := build |
| 52 |
ARTIFACTS += dist |
| 53 |
ARTIFACTS += __pycache__ |
| 54 |
ARTIFACTS += tags |
| 55 |
ARTIFACTS += *.egg-info |
| 56 |
ARTIFACTS += fuzz_*.pkg.spec # Files created by OSS-Fuzz when running locally |
| 57 |
|
| 58 |
PYTHON_DIRS := tests |
| 59 |
PYTHON_DIRS += jsonpickle |
| 60 |
PYTHON_DIRS += fuzzing |
| 61 |
|
| 62 |
# The default target of this makefile is.... |
| 63 |
all:: help |
| 64 |
|
| 65 |
help:: |
| 66 |
@echo "---- Makefile Targets ----" |
| 67 |
@echo "make help - print this message" |
| 68 |
@echo "make test - run unit tests" |
| 69 |
@echo "make tox - run unit tests using tox" |
| 70 |
@echo "make clean - remove cruft" |
| 71 |
@echo "make benchmark - run pytest benchmarking" |
| 72 |
@echo "make doc - generate documentation using sphinx" |
| 73 |
.PHONY: help |
| 74 |
|
| 75 |
test:: |
| 76 |
$(TESTCMD) jsonpickle tests $(flags) |
| 77 |
.PHONY: test |
| 78 |
|
| 79 |
tox:: |
| 80 |
$(TOXCMD) $(flags) |
| 81 |
.PHONY: tox |
| 82 |
|
| 83 |
benchmark:: |
| 84 |
@if [ "$(uname_S)" = "Linux" ]; then \ |
| 85 |
echo "Operating System detected: Linux"; \ |
| 86 |
\ |
| 87 |
if [ -f /etc/os-release ]; then \ |
| 88 |
. /etc/os-release; \ |
| 89 |
if [ "$$ID" = "ubuntu" ]; then \ |
| 90 |
echo "Ubuntu detected."; \ |
| 91 |
\ |
| 92 |
if ! command -v taskset >/dev/null 2>&1; then \ |
| 93 |
echo "'taskset' not found. We need this to reduce noise. Installing util-linux..."; \ |
| 94 |
\ |
| 95 |
sudo apt-get update && \ |
| 96 |
\ |
| 97 |
sudo apt-get install -y util-linux && \ |
| 98 |
\ |
| 99 |
if ! command -v taskset >/dev/null 2>&1; then \ |
| 100 |
echo "Failed to install util-linux. Please install it manually."; \ |
| 101 |
exit 1; \ |
| 102 |
else \ |
| 103 |
echo "Successfully installed util-linux."; \ |
| 104 |
fi; \ |
| 105 |
else \ |
| 106 |
echo "'taskset' is already installed."; \ |
| 107 |
fi; \ |
| 108 |
else \ |
| 109 |
echo "Non-Ubuntu Linux distribution detected. Skipping util-linux installation."; \ |
| 110 |
fi; \ |
| 111 |
else \ |
| 112 |
echo "Cannot detect OS. Skipping util-linux installation."; \ |
| 113 |
fi; \ |
| 114 |
\ |
| 115 |
NUM_CORES=`$(NPROC)`; \ |
| 116 |
\ |
| 117 |
# use #cores / 2 - 1 because cores 0, 1, and last get most noise \ |
| 118 |
# and we divide by 2 to get physical cores for systems with SMT (most x86) \ |
| 119 |
# so this works well for 4+ core machines \ |
| 120 |
if [ $$NUM_CORES -gt 16 ]; then \ |
| 121 |
TARGET_CORE=7; \ |
| 122 |
else \ |
| 123 |
TARGET_CORE=$$((NUM_CORES / 2 - 1)); \ |
| 124 |
fi; \ |
| 125 |
\ |
| 126 |
echo "Running '$(BENCHMARKCMD)' on core $$TARGET_CORE of $$NUM_CORES cores."; \ |
| 127 |
\ |
| 128 |
# execute the benchmark command with core affinity set to the target core \ |
| 129 |
# disable gc for reproducibility, warmup on for the experimental python 3.13 JIT \ |
| 130 |
taskset -c $$TARGET_CORE $(BENCHMARKCMD) --benchmark-disable-gc --benchmark-warmup=on --benchmark-warmup-iterations=1 --benchmark-min-rounds=10000 --benchmark-histogram=./images/benchmark-$(DATEANDTIME) $(EXTRA_BENCH_ARGS) ./jsonpickle_benchmarks.py; \ |
| 131 |
else \ |
| 132 |
$(BENCHMARKCMD) --benchmark-disable-gc --benchmark-warmup=on --benchmark-warmup-iterations=1 --benchmark-min-rounds=10000 --benchmark-histogram=./images/benchmark-$(DATEANDTIME) $(EXTRA_BENCH_ARGS) ./jsonpickle_benchmarks.py; \ |
| 133 |
echo "The 'benchmark' target has much less noise on Linux, try running it on there!"; \ |
| 134 |
fi |
| 135 |
.PHONY: benchmark |
| 136 |
|
| 137 |
doc:: |
| 138 |
$(SPHINX) docs build/html |
| 139 |
|
| 140 |
tags:: |
| 141 |
$(FIND) $(PYTHON_DIRS) -name '*.py' -print0 | xargs -0 $(CTAGS) -f tags |
| 142 |
|
| 143 |
clean:: |
| 144 |
$(FIND) $(PYTHON_DIRS) -name '*.py[cod]' -print0 | xargs -0 rm -f |
| 145 |
$(FIND) $(PYTHON_DIRS) -name '__pycache__' -print0 | xargs -0 rm -fr |
| 146 |
$(RM_R) $(ARTIFACTS) |
| 147 |
.PHONY: clean |
| 148 |
|
| 149 |
format:: |
| 150 |
$(BLACK) --skip-string-normalization --target-version py310 $(PYTHON_DIRS) |
| 151 |
.PHONY: format |