# Linux: g++ with OpenMP. macOS (Apple silicon or Intel): clang++; OpenMP only if Homebrew's libomp is
# installed (brew install libomp), otherwise a single-threaded build. `make OMP=` forces no OpenMP.
# The macOS branch is untested (written on Linux); the same sources were built here with clang++
# without OpenMP and give identical output.
UNAME := $(shell uname -s)
ifeq ($(UNAME),Darwin)
  CXX := clang++
  NATIVE := -mcpu=native
  OMPDIR := $(shell brew --prefix libomp 2>/dev/null)
  ifneq ($(OMPDIR),)
    OMP ?= -Xpreprocessor -fopenmp -I$(OMPDIR)/include -L$(OMPDIR)/lib -lomp
  endif
else
  NATIVE := -march=native
  OMP ?= -fopenmp
endif
CXXFLAGS ?= -O3 $(NATIVE) -std=c++17 -w
all: fillsearch placesearch transsearch
fillsearch: src/fillsearch.cpp src/life.h
	$(CXX) $(CXXFLAGS) $< -o $@ $(OMP)
placesearch: src/placesearch.cpp src/life.h
	$(CXX) $(CXXFLAGS) $< -o $@ $(OMP)
transsearch: src/transsearch.cpp
	$(CXX) $(CXXFLAGS) $< -o $@ $(OMP)
# Regression tests (about 20 s). The oracle test needs node; it is skipped if node is missing.
test: all
	@if command -v node >/dev/null; then ./fillsearch examples/00_oracle_selftest.prob --dump 3000 -s 7 2>/dev/null | node tools/oracle_test.js ref/8than888searcher.html | head -1; else echo "node not found: oracle test skipped"; fi
	@./placesearch examples/01_knockout_p10.prob lib/stills.txt 2>/dev/null | cmp - examples/01_knockout_p10.expected.rle && echo "example 01 ok"
	@./fillsearch examples/02_fill_exhaustive_p10.prob -x 2>/dev/null | cmp - examples/02_fill_exhaustive_p10.expected.rle && echo "example 02 ok"
	@./placesearch examples/04_hassle_block_c2.prob lib/oscs.txt --sym c2 -M 16 --minperiod 5 --mindev 6 -k 0 --maxpop 100 2>/dev/null | cmp - examples/04_hassle_block_c2.expected.rle && echo "example 04 ok"
	@./placesearch examples/05_two_knockouts_p10.prob lib/stills_small.txt -n 2 -K 8 2>/dev/null | cmp - examples/05_two_knockouts_p10.expected.rle && echo "example 05 ok"
	@python3 tools/verify.py examples/04_hassle_block_c2.expected.rle --unique | grep -c "^[0-9]*: p14 " | sed 's/^/example 04 independent check: distinct p14 oscillators = /'
	@python3 tools/satprob.py selftest
	@python3 tools/sat_selfcheck.py examples/02_fill_exhaustive_p10.prob examples/02_fill_exhaustive_p10.expected.rle | tail -1
# Needs a SAT solver: make sat-test SOLVER=/path/to/cadical   (about 10 s; expects one solution, then UNSAT)
sat-test:
	@python3 tools/satprob.py solve examples/02_fill_exhaustive_p10.prob --solver $(SOLVER) -n 3 2>&1 | grep -E "solution\(s\)|satprob solution"
clean:
	rm -f fillsearch placesearch transsearch
