# HP LaserJet Pro P1108 plus - PCLmS CUPS filter
#
#   make           build the filter
#   make test      run the offline test suite (no printer needed)
#   make install   build, install the filter + PPD, create the queue (needs root)
#   make prebuilt  refresh the bundled universal macOS binary (maintainer only)
#   make clean     remove build products

CC      ?= cc
CFLAGS  ?= -O2 -Wall -Wextra
LIBS     = -lcupsimage -lcups

BIN      = rastertopclms
SRC      = rastertopclms.c

PREBUILT = prebuilt/rastertopclms-macos-universal

.PHONY: all test install prebuilt clean

all: $(BIN)

$(BIN): $(SRC)
	$(CC) $(CFLAGS) -o $@ $< $(LIBS)

test:
	@./tests/run-tests.sh

install:
	@[ "$$(id -u)" -eq 0 ] || { echo "run as root: sudo make install" >&2; exit 1; }
	@./install-driver.sh

# Shipped so a Mac with no Xcode Command Line Tools can install without
# compiling. Universal (arm64 + x86_64), targeting macOS 11 and later; the
# linker ad-hoc signs it, which arm64 requires in order to execute at all.
prebuilt:
	@[ "$$(uname -s)" = "Darwin" ] || { echo "run this on macOS" >&2; exit 1; }
	mkdir -p prebuilt
	$(CC) -arch arm64 -arch x86_64 -mmacosx-version-min=11.0 $(CFLAGS) \
	      -o $(PREBUILT) $(SRC) $(LIBS)
	@# The linker ad-hoc signs only the arm64 slice, because Apple Silicon
	@# refuses to execute unsigned code and Intel does not. Sign both, so the
	@# downloaded binary looks the same on either machine.
	codesign --force --sign - --identifier rastertopclms $(PREBUILT)
	codesign -v --arch arm64  $(PREBUILT)
	codesign -v --arch x86_64 $(PREBUILT)
	chmod 0755 $(PREBUILT)
	lipo -info $(PREBUILT)

clean:
	rm -f $(BIN)
