CLANG ?= clang
CFLAGS ?=
OS ?=
REMOTE ?= origin

CFLAGS += -Os -g3 -Wall -Wextra -Wno-unused-parameter
ifneq ($(OS),Windows_NT) 
	# NOTE: clang on windows does not support fPIC
	CFLAGS += -fPIC
endif

INCLUDES += -Ibuild/

# On x86-64 (non-Windows), also build a second instantiation of the generated
# parser with -msse4.2 so llhttp_execute() can select the SSE4.2 span scanners
# at runtime (see src/native/api.c). The default build passes no -march, so the
# SSE4.2 code generated under `#ifdef __SSE4_2__` is otherwise never compiled
# in. LLHTTP_SSE42_DISPATCH enables the dispatch in api.c and is defined only
# here, where the second object is also built. Detect the *target* from the
# compiler, not the host (uname -m), so a cross-compile (e.g. CLANG with
# --target=aarch64-...) does not add an x86-only object. Windows/MSVC is
# skipped (no __builtin_cpu_supports), matching the CMake and gyp builds.
TARGET_MACHINE ?= $(shell $(CLANG) $(CFLAGS) -dumpmachine 2>/dev/null)
TARGET_CPU := $(firstword $(subst -, ,$(TARGET_MACHINE)))
IS_WINDOWS := $(or $(findstring windows,$(TARGET_MACHINE)),$(findstring mingw,$(TARGET_MACHINE)))
ifeq (,$(IS_WINDOWS))
ifneq (,$(filter $(TARGET_CPU),x86_64 amd64))
	SSE42_OBJ = build/c/llhttp_sse42.o
	CFLAGS += -DLLHTTP_SSE42_DISPATCH
endif
endif

INSTALL ?= install
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
LIBDIR = $(PREFIX)/lib
INCLUDEDIR = $(PREFIX)/include

all: build/libllhttp.a build/libllhttp.so

clean:
	rm -rf release/
	rm -rf build/
	rm -rf test/tmp

build/libllhttp.so: build/c/llhttp.o $(SSE42_OBJ) build/native/api.o \
		build/native/http.o
	$(CLANG) -shared $^ -o $@

build/libllhttp.a: build/c/llhttp.o $(SSE42_OBJ) build/native/api.o \
		build/native/http.o
	$(AR) rcs $@ build/c/llhttp.o $(SSE42_OBJ) build/native/api.o build/native/http.o

build/libllhttp.pc: libllhttp.pc.in package.json
	mkdir -p build
	sed -e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|g' \
		-e 's|@CMAKE_INSTALL_FULL_BINDIR@|$(BINDIR)|g' \
		-e 's|@CMAKE_INSTALL_FULL_LIBDIR@|$(LIBDIR)|g' \
		-e 's|@CMAKE_INSTALL_FULL_INCLUDEDIR@|$(INCLUDEDIR)|g' \
		-e "s|@PROJECT_VERSION@|$$(node -p "require('./package.json').version")|g" \
		$< > $@

build/c/llhttp.o: build/c/llhttp.c
	$(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@

build/c/llhttp_sse42.o: build/c/llhttp_sse42.c build/c/llhttp.c
	$(CLANG) $(CFLAGS) -msse4.2 $(INCLUDES) -c $< -o $@

build/native/%.o: src/native/%.c build/llhttp.h src/native/api.h \
		build/native
	$(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@

build/llhttp.h: generate
build/c/llhttp.c: generate

build/native:
	mkdir -p build/native

release: clean all generate
	@echo "${RELEASE}" | grep -q -E ".+" || { echo "Please make sure the RELEASE argument is set."; exit 1; }
	rm -rf release
	mkdir -p release/cmake
	mkdir -p release/src
	mkdir -p release/include
	cp -rf build/llhttp.h release/include/
	cp -rf build/c/llhttp.c release/src/
	cp -rf build/c/llhttp_sse42.c release/src/
	cp -rf build/c/llhttp_sse42_rename.h release/src/
	cp -rf src/native/*.c release/src/
	cp -rf src/llhttp.gyp release/
	cp -rf src/common.gypi release/
	cp -rf cmake/llhttpConfig.cmake.in release/cmake
	sed s/_RELEASE_/$(RELEASE)/ CMakeLists.txt > release/CMakeLists.txt
	cp -rf libllhttp.pc.in release/
	cp -rf README.md release/
	cp -rf LICENSE release/

github-release:
	@echo "${RELEASE_V}" | grep -q -E "^v" || { echo "Please make sure version starts with \"v\"."; exit 1; }
	gh release create -d --generate-notes ${RELEASE_V}
	@sleep 5
	gh release view ${RELEASE_V} -t "{{.body}}" --json body > RELEASE_NOTES
	gh release delete ${RELEASE_V} -y
	gh release create -F RELEASE_NOTES -d --title ${RELEASE_V} --target release release/${RELEASE_V}
	@sleep 5
	rm -rf RELEASE_NOTES
	open $$(gh release view release/${RELEASE_V} --json url -t "{{.url}}")

postversion: release	
	git fetch $(REMOTE)
	@git show-ref --verify --quiet refs/heads/release || git branch --track release $(REMOTE)/release
	git push $(REMOTE)
	git checkout release --
	cp -rf release/* ./
	rm -rf build release
	git add include src *.gyp *.gypi CMakeLists.txt README.md LICENSE libllhttp.pc.in cmake/llhttpConfig.cmake.in
	git commit -a -n -m "release: $(RELEASE)"
	git tag "release/v$(RELEASE)"
	git push $(REMOTE) && git push $(REMOTE) --tags
	git checkout main

generate:
	node --import tsx bin/generate.ts

# Links a small C program against the built library (which contains api.c and,
# on x86-64, both parser instantiations) and drives the runtime dispatch added
# in src/native/api.c. The markdown test suite never compiles api.c.
test-c: build/libllhttp.a
	$(CLANG) $(CFLAGS) $(INCLUDES) test/dispatch/dispatch_test.c \
		build/libllhttp.a -o build/dispatch_test
	./build/dispatch_test

install: build/libllhttp.a build/libllhttp.so build/libllhttp.pc
	$(INSTALL) -d $(DESTDIR)$(INCLUDEDIR)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)/pkgconfig
	$(INSTALL) -C build/llhttp.h $(DESTDIR)$(INCLUDEDIR)/llhttp.h
	$(INSTALL) -C build/libllhttp.a $(DESTDIR)$(LIBDIR)/libllhttp.a
	$(INSTALL) build/libllhttp.so $(DESTDIR)$(LIBDIR)/libllhttp.so
	$(INSTALL) -m 644 build/libllhttp.pc $(DESTDIR)$(LIBDIR)/pkgconfig/libllhttp.pc

# Regenerate pkg-config metadata when installation paths change.
.PHONY: all generate clean release postversion github-release test-c install build/libllhttp.pc
