.ONESHELL:
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

VERSION_CODE := 20
VERSION_NUMBER := 0.0.20-wip
VERSION_NAME := One word all lowercase four words all uppercase.

SQLITE_URL := https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip
LIBUV_URL := https://dist.libuv.org/dist/v1.48.0/libuv-v1.48.0.tar.gz

PROJECT = tildefriends
BUILD_DIR ?= out
UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)

ANDROID_SDK ?= ~/Android/Sdk

HAVE_WIN := 0

ifeq ($(UNAME_S),Darwin)
BUILD_TYPES := macosdebug macosrelease iosdebug iosrelease iossimdebug iossimrelease
else ifeq ($(UNAME_S),Linux)
BUILD_TYPES := debug release
HAVE_ANDROID = $(if $(shell which $(ANDROID_SDK)/platform-tools/adb),1,0)
HAVE_LINUX_IOS = $(if $(shell which deps/ios_toolchain/target/bin deps/ios_toolchain/target/bin/arm-apple-darwin11-clang),1,0)
HAVE_WIN = $(if $(shell which x86_64-w64-mingw32-gcc-win32),1,0)
else ifeq ($(UNAME_S),Haiku)
BUILD_TYPES := debug release
CFLAGS += -Dstatic_assert=_Static_assert
LDFLAGS += \
	-lbsd \
	-lnetwork
else ifeq ($(UNAME_S),OpenBSD)
BUILD_TYPES := debug release
CFLAGS += \
	-Wno-unknown-warning-option
LDFLAGS += \
	-lexecinfo \
	-lc++abi
HAVE_ANDROID := 0
HAVE_LINUX_IOS := 0
else
$(error Unexpected host platform $(UNAME_S).)
endif

CFLAGS += \
	-std=gnu11 \
	-Wall \
	-Wextra \
	-Wno-unused-parameter \
	-MMD \
	-MP \
	-ffunction-sections \
	-fdata-sections \
	-fno-exceptions \
	-g

ANDROID_MIN_SDK_VERSION := 24
ANDROID_TARGET_SDK_VERSION := 34
ANDROID_BUILD_TOOLS := $(ANDROID_SDK)/build-tools/34.0.0
ANDROID_PLATFORM := $(ANDROID_SDK)/platforms/android-$(ANDROID_TARGET_SDK_VERSION)
ANDROID_NDK ?= $(ANDROID_SDK)/ndk/26.2.11394342

ANDROID_ARMV7A_TARGETS := \
	out/androiddebug-armv7a/tildefriends \
	out/androidrelease-armv7a/tildefriends
ANDROID_ARM64_TARGETS := \
	out/androiddebug/tildefriends \
	out/androidrelease/tildefriends
ANDROID_X86_TARGETS := \
	out/androiddebug-x86/tildefriends \
	out/androidrelease-x86/tildefriends
ANDROID_X86_64_TARGETS := \
	out/androiddebug-x86_64/tildefriends \
	out/androidrelease-x86_64/tildefriends
ANDROID_TARGETS := \
	$(ANDROID_X86_TARGETS) \
	$(ANDROID_X86_64_TARGETS) \
	$(ANDROID_ARMV7A_TARGETS) \
	$(ANDROID_ARM64_TARGETS)
ifeq ($(HAVE_ANDROID),1)
BUILD_TYPES += \
	androiddebug \
	androidrelease \
	androiddebug-armv7a \
	androidrelease-armv7a \
	androiddebug-x86 \
	androidrelease-x86 \
	androiddebug-x86_64 \
	androidrelease-x86_64
all: out/TildeFriends-arm-debug.apk out/TildeFriends-arm-release.apk out/TildeFriends-x86-debug.apk out/TildeFriends-x86-release.apk
endif

WINDOWS_TARGETS := \
	out/windebug/tildefriends.exe \
	out/winrelease/tildefriends.exe
ifeq ($(HAVE_WIN),1)
BUILD_TYPES += windebug winrelease
endif

LINUX_TARGETS := \
	out/debug/tildefriends \
	out/release/tildefriends
MACOS_TARGETS := \
	out/macosdebug/tildefriends \
	out/macosrelease/tildefriends
IOS_TARGETS := \
	out/iosdebug/tildefriends \
	out/iosrelease/tildefriends
IOSSIM_TARGETS := \
	out/iossimdebug/tildefriends \
	out/iossimrelease/tildefriends
IOS_APPS = \
	out/tildefriends-iosdebug.app/tildefriends \
	out/tildefriends-iosrelease.app/tildefriends
ifeq ($(HAVE_LINUX_IOS),1)
BUILD_TYPES += iosdebug iosrelease
all: $(IOS_APPS)
endif
ifeq ($(UNAME_S),Darwin)
all: $(IOS_APPS) \
	out/tildefriends-iossimdebug.app/tildefriends \
	out/tildefriends-iossimrelease.app/tildefriends
endif

DEBUG_TARGETS := \
	out/debug/tildefriends \
	out/windebug/tildefriends.exe \
	out/iosdebug/tildefriends \
	out/iossimdebug/tildefriends \
	out/macosdebug/tildefriends \
	out/androiddebug/tildefriends \
	out/androiddebug-armv7a/tildefriends \
	out/androiddebug-x86_64/tildefriends \
	out/androiddebug-x86/tildefriends
RELEASE_TARGETS := \
	out/release/tildefriends \
	out/winrelease/tildefriends.exe \
	out/iosrelease/tildefriends \
	out/iossimrelease/tildefriends \
	out/macosrelease/tildefriends \
	out/androidrelease/tildefriends \
	out/androidrelease-armv7a/tildefriends \
	out/androidrelease-x86_64/tildefriends \
	out/androidrelease-x86/tildefriends
ALL_TARGETS = $(DEBUG_TARGETS) $(RELEASE_TARGETS)
ANDROID_RELEASE_TARGETS := $(filter-out $(DEBUG_TARGETS),$(ANDROID_TARGETS))
NONANDROID_RELEASE_TARGETS := $(filter-out $(ANDROID_ARM64_TARGETS),$(RELEASE_TARGETS))
NONANDROID_TARGETS := $(filter-out $(ANDROID_TARGETS),$(ALL_TARGETS))
NONMACOS_TARGETS := $(filter-out $(MACOS_TARGETS) $(IOS_TARGETS) $(IOSSIM_TARGETS),$(ALL_TARGETS))

$(NONANDROID_TARGETS): CFLAGS += -fno-omit-frame-pointer
$(filter-out $(ANDROID_TARGETS) $(WINDOWS_TARGETS),$(ALL_TARGETS)): LDFLAGS += -rdynamic
$(ANDROID_TARGETS): CFLAGS += \
	--sysroot $(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
	-fPIC \
	-fdebug-compilation-dir . \
	-fomit-frame-pointer \
	-fno-asynchronous-unwind-tables \
	-funwind-tables
$(ANDROID_TARGETS): LDFLAGS += --sysroot $(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC
$(DEBUG_TARGETS): CFLAGS += -DDEBUG -Og
$(RELEASE_TARGETS): CFLAGS += -DNDEBUG
$(NONANDROID_RELEASE_TARGETS): CFLAGS += -O3
$(ANDROID_RELEASE_TARGETS): CFLAGS += -Oz
$(WINDOWS_TARGETS): CC = x86_64-w64-mingw32-gcc-win32
$(WINDOWS_TARGETS): AS = $(CC)
$(WINDOWS_TARGETS): CFLAGS += \
	-D_WIN32_WINNT=0x0A00 \
	-DWINVER=0x0A00 \
	-DNTDDI_VERSION=NTDDI_WIN10 \
	-Ideps/openssl/mingw64/usr/local/include
$(WINDOWS_TARGETS): LDFLAGS += \
	-static \
	-lm \
	-Ldeps/openssl/mingw64/usr/local/lib
ifeq ($(UNAME_S),Darwin)
$(MACOS_TARGETS): CC = xcrun clang
$(IOS_TARGETS): IOS_SYSROOT := $(shell xcrun --sdk iphoneos --show-sdk-path)
$(IOS_TARGETS): CC = xcrun --sdk iphoneos clang -isysroot $(IOS_SYSROOT) -arch arm64
$(IOSSIM_TARGETS): IOSSIM_SYSROOT := $(shell xcrun --sdk iphonesimulator --show-sdk-path)
$(IOSSIM_TARGETS): CC = xcrun --sdk iphonesimulator clang -isysroot $(IOSSIM_SYSROOT) -arch x86_64
else ifeq ($(UNAME_S),Linux)
$(IOS_TARGETS): IOS_SYSROOT := deps/iPhoneOS17.0.sdk
$(IOS_TARGETS): CC = PATH=$$PATH:deps/ios_toolchain/target/bin deps/ios_toolchain/target/bin/arm-apple-darwin11-clang
endif
$(ANDROID_X86_64_TARGETS): ANDROID_NDK_TARGET_TRIPLE := x86_64-linux-android
$(ANDROID_X86_TARGETS): ANDROID_NDK_TARGET_TRIPLE := i686-linux-android
$(ANDROID_ARMV7A_TARGETS): ANDROID_NDK_TARGET_TRIPLE := armv7a-linux-androideabi
$(ANDROID_ARM64_TARGETS): ANDROID_NDK_TARGET_TRIPLE := aarch64-linux-android
$(ANDROID_TARGETS): CC = $(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/bin/$(ANDROID_NDK_TARGET_TRIPLE)$(ANDROID_MIN_SDK_VERSION)-clang
$(ANDROID_TARGETS): AS = $(CC)
$(ANDROID_TARGETS): CFLAGS += \
	-target $(ANDROID_NDK_TARGET_TRIPLE)$(ANDROID_MIN_SDK_VERSION) \
	-Wno-unknown-warning-option
$(ANDROID_ARMV7A_TARGETS): CFLAGS += -Ideps/openssl/android/armeabi-v7a/usr/local/include
$(ANDROID_ARMV7A_TARGETS): LDFLAGS += -Ldeps/openssl/android/armeabi-v7a/usr/local/lib
$(ANDROID_ARM64_TARGETS): CFLAGS += -Ideps/openssl/android/arm64-v8a/usr/local/include
$(ANDROID_ARM64_TARGETS): LDFLAGS += -Ldeps/openssl/android/arm64-v8a/usr/local/lib
$(ANDROID_X86_TARGETS): CFLAGS += -Ideps/openssl/android/x86/usr/local/include
$(ANDROID_X86_TARGETS): CFLAGS += -Wno-atomic-alignment
$(ANDROID_X86_TARGETS): LDFLAGS += -Ldeps/openssl/android/x86/usr/local/lib
$(ANDROID_X86_64_TARGETS): CFLAGS += -Ideps/openssl/android/x86_64/usr/local/include
$(ANDROID_X86_64_TARGETS): LDFLAGS += -Ldeps/openssl/android/x86_64/usr/local/lib
$(NONMACOS_TARGETS): CFLAGS += -Wno-cast-function-type
$(NONMACOS_TARGETS): LDFLAGS += -Wl,--gc-sections
$(IOS_TARGETS): CFLAGS += -mios-version-min=9.0 -Ideps/openssl/ios/ios64-xcrun/usr/local/include
$(IOS_TARGETS): LDFLAGS += -Ldeps/openssl/ios/ios64-xcrun/usr/local/lib
$(IOSSIM_TARGETS): CFLAGS += -Ideps/openssl/ios/iossimulator-xcrun/usr/local/include
$(IOSSIM_TARGETS): LDFLAGS += -Ldeps/openssl/ios/iossimulator-xcrun/usr/local/lib

ifeq ($(UNAME_M),x86_64)
ifneq ($(UNAME_S),Haiku)
out/debug/tildefriends: CFLAGS += -fsanitize=address -fsanitize=undefined -fno-common
out/debug/tildefriends: LDFLAGS += -fsanitize=address -fsanitize=undefined
endif
endif

ifeq ($(UNAME_M),aarch64)
out/debug/tildefriends: CFLAGS += -fsanitize=address -fsanitize=undefined -fno-common
out/debug/tildefriends: LDFLAGS += -fsanitize=address -fsanitize=undefined
endif

get_objs = \
	$(foreach build_type,$(BUILD_TYPES),$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)))))) \
	$(foreach build_type,debug release,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_unix))))) \
	$(foreach build_type,windebug winrelease,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_win))))) \
	$(foreach build_type,androiddebug androidrelease androiddebug-x86 androidrelease-x86 androiddebug-x86_64 androidrelease-x86_64 androiddebug-armv7a androiddebug-armv7a,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_android))))) \
	$(foreach build_type,androiddebug androidrelease androiddebug-x86 androidrelease-x86 androiddebug-x86_64 androidrelease-x86_64 androiddebug-armv7a androidrelease-armv7a,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_unix))))) \
	$(foreach build_type,macosdebug macosrelease iosdebug iosrelease iossimdebug iossimrelease,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_macos))))) \
	$(foreach build_type,iosdebug iosrelease iossimdebug iossimrelease,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_ios))))) \
	$(foreach build_type,androiddebug-x86 androidrelease-x86,$(addprefix $(BUILD_DIR)/$(build_type)/,$(addsuffix .o,$(basename $(value $(1)_x86)))))

APP_SOURCES := $(wildcard src/*.c)
APP_SOURCES_ios := $(wildcard src/*.m)
APP_OBJS := $(call get_objs,APP_SOURCES)
$(APP_OBJS): CFLAGS += \
	-Ideps/base64c/include \
	-Ideps/crypt_blowfish \
	-Ideps/libbacktrace \
	-Ideps/libsodium \
	-Ideps/libsodium/src/libsodium/include \
	-Ideps/libuv/include \
	-Ideps/zlib \
	-Ideps/zlib/contrib/minizip \
	-Ideps/picohttpparser \
	-Ideps/quickjs \
	-Ideps/sqlite \
	-Ideps/valgrind \
	-Wdouble-promotion \
	-Werror
ifeq ($(UNAME_M),x86_64)
$(filter-out $(BUILD_DIR)/android% $(BUILD_DIR)/macos% $(BUILD_DIR)/ios%,$(APP_OBJS)): CFLAGS += \
	-fanalyzer
endif

BLOWFISH_SOURCES := \
	deps/crypt_blowfish/crypt_blowfish.c \
	deps/crypt_blowfish/crypt_gensalt.c \
	deps/crypt_blowfish/wrapper.c
BLOWFISH_SOURCES_win := \
	deps/crypt_blowfish/x86.S
BLOWFISH_SOURCES_x86 := \
	deps/crypt_blowfish/x86.S
BLOWFISH_OBJS := $(call get_objs,BLOWFISH_SOURCES)

UV_SOURCES := \
	deps/libuv/src/fs-poll.c \
	deps/libuv/src/idna.c \
	deps/libuv/src/inet.c \
	deps/libuv/src/random.c \
	deps/libuv/src/strscpy.c \
	deps/libuv/src/strtok.c \
	deps/libuv/src/threadpool.c \
	deps/libuv/src/timer.c \
	deps/libuv/src/uv-common.c \
	deps/libuv/src/uv-data-getter-setters.c \
	deps/libuv/src/version.c
UV_SOURCES_unix := \
	deps/libuv/src/unix/async.c \
	deps/libuv/src/unix/core.c \
	deps/libuv/src/unix/dl.c \
	deps/libuv/src/unix/fs.c \
	deps/libuv/src/unix/getaddrinfo.c \
	deps/libuv/src/unix/getnameinfo.c \
	deps/libuv/src/unix/loop-watcher.c \
	deps/libuv/src/unix/loop.c \
	deps/libuv/src/unix/pipe.c \
	deps/libuv/src/unix/poll.c \
	deps/libuv/src/unix/process.c \
	deps/libuv/src/unix/random-devurandom.c \
	deps/libuv/src/unix/random-getrandom.c \
	deps/libuv/src/unix/signal.c \
	deps/libuv/src/unix/stream.c \
	deps/libuv/src/unix/tcp.c \
	deps/libuv/src/unix/thread.c \
	deps/libuv/src/unix/tty.c \
	deps/libuv/src/unix/udp.c
ifeq ($(UNAME_S),Linux)
	UV_SOURCES_unix += \
		deps/libuv/src/unix/linux.c \
		deps/libuv/src/unix/procfs-exepath.c \
		deps/libuv/src/unix/proctitle.c \
		deps/libuv/src/unix/random-sysctl-linux.c
else ifeq ($(UNAME_S),Haiku)
	UV_SOURCES_unix += \
		deps/libuv/src/unix/bsd-ifaddrs.c \
		deps/libuv/src/unix/haiku.c \
		deps/libuv/src/unix/no-fsevents.c \
		deps/libuv/src/unix/no-proctitle.c \
		deps/libuv/src/unix/posix-hrtime.c \
		deps/libuv/src/unix/posix-poll.c
else ifeq ($(UNAME_S),OpenBSD)
	UV_SOURCES_unix += \
		deps/libuv/src/unix/bsd-ifaddrs.c \
		deps/libuv/src/unix/kqueue.c \
		deps/libuv/src/unix/no-proctitle.c \
		deps/libuv/src/unix/openbsd.c \
		deps/libuv/src/unix/posix-hrtime.c \
		deps/libuv/src/unix/random-getentropy.c
endif
UV_SOURCES_android := \
	deps/libuv/src/unix/random-getentropy.c
UV_SOURCES_win := \
	deps/libuv/src/win/async.c \
	deps/libuv/src/win/core.c \
	deps/libuv/src/win/detect-wakeup.c \
	deps/libuv/src/win/dl.c \
	deps/libuv/src/win/error.c \
	deps/libuv/src/win/fs-event.c \
	deps/libuv/src/win/fs.c \
	deps/libuv/src/win/getaddrinfo.c \
	deps/libuv/src/win/getnameinfo.c \
	deps/libuv/src/win/handle.c \
	deps/libuv/src/win/loop-watcher.c \
	deps/libuv/src/win/pipe.c \
	deps/libuv/src/win/poll.c \
	deps/libuv/src/win/process-stdio.c \
	deps/libuv/src/win/process.c \
	deps/libuv/src/win/signal.c \
	deps/libuv/src/win/snprintf.c \
	deps/libuv/src/win/stream.c \
	deps/libuv/src/win/tcp.c \
	deps/libuv/src/win/thread.c \
	deps/libuv/src/win/tty.c \
	deps/libuv/src/win/udp.c \
	deps/libuv/src/win/util.c \
	deps/libuv/src/win/winapi.c \
	deps/libuv/src/win/winsock.c
UV_SOURCES_macos := \
	deps/libuv/src/unix/async.c \
	deps/libuv/src/unix/bsd-ifaddrs.c \
	deps/libuv/src/unix/core.c \
	deps/libuv/src/unix/darwin.c \
	deps/libuv/src/unix/darwin-proctitle.c \
	deps/libuv/src/unix/dl.c \
	deps/libuv/src/unix/fs.c \
	deps/libuv/src/unix/fsevents.c \
	deps/libuv/src/unix/getaddrinfo.c \
	deps/libuv/src/unix/getnameinfo.c \
	deps/libuv/src/unix/kqueue.c \
	deps/libuv/src/unix/loop-watcher.c \
	deps/libuv/src/unix/loop.c \
	deps/libuv/src/unix/pipe.c \
	deps/libuv/src/unix/poll.c \
	deps/libuv/src/unix/process.c \
	deps/libuv/src/unix/proctitle.c \
	deps/libuv/src/unix/random-devurandom.c \
	deps/libuv/src/unix/random-getentropy.c \
	deps/libuv/src/unix/signal.c \
	deps/libuv/src/unix/stream.c \
	deps/libuv/src/unix/tcp.c \
	deps/libuv/src/unix/thread.c \
	deps/libuv/src/unix/tty.c \
	deps/libuv/src/unix/udp.c
UV_OBJS := $(call get_objs,UV_SOURCES)
$(UV_OBJS): CFLAGS += \
	-Ideps/libuv/include \
	-Ideps/libuv/src \
	-Wno-dangling-pointer \
	-Wno-incompatible-pointer-types \
	-Wno-maybe-uninitialized \
	-Wno-sign-compare \
	-Wno-unused-but-set-parameter \
	-Wno-unused-but-set-variable \
	-Wno-unused-result \
	-Wno-unused-variable
ifeq ($(UNAME_S),Linux)
$(UV_OBJS): CFLAGS += \
	-D_GNU_SOURCE
else ifeq ($(UNAME_S),Haiku)
$(UV_OBJS): CFLAGS += \
	-D_BSD_SOURCE \
	-Wno-format-truncation
endif

SODIUM_SOURCES := \
	deps/libsodium/src/libsodium/crypto_aead/aegis128l/aead_aegis128l.c \
	deps/libsodium/src/libsodium/crypto_aead/aegis128l/aegis128l_soft.c \
	deps/libsodium/src/libsodium/crypto_aead/aegis256/aead_aegis256.c \
	deps/libsodium/src/libsodium/crypto_aead/aegis256/aegis256_soft.c \
	deps/libsodium/src/libsodium/crypto_auth/hmacsha512/auth_hmacsha512.c \
	deps/libsodium/src/libsodium/crypto_auth/hmacsha512256/auth_hmacsha512256.c \
	deps/libsodium/src/libsodium/crypto_box/crypto_box.c \
	deps/libsodium/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305.c \
	deps/libsodium/src/libsodium/crypto_core/ed25519/ref10/ed25519_ref10.c \
	deps/libsodium/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20_ref2.c \
	deps/libsodium/src/libsodium/crypto_core/salsa/ref/core_salsa_ref.c \
	deps/libsodium/src/libsodium/crypto_core/softaes/softaes.c \
	deps/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ref.c \
	deps/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c \
	deps/libsodium/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c \
	deps/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c \
	deps/libsodium/src/libsodium/crypto_hash/sha256/hash_sha256.c \
	deps/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c \
	deps/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna.c \
	deps/libsodium/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c \
	deps/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.c \
	deps/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ref.c \
	deps/libsodium/src/libsodium/crypto_pwhash/argon2/blake2b-long.c \
	deps/libsodium/src/libsodium/crypto_scalarmult/crypto_scalarmult.c \
	deps/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c \
	deps/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/curve25519_sandy2x.c \
	deps/libsodium/src/libsodium/crypto_scalarmult/curve25519/scalarmult_curve25519.c \
	deps/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c \
	deps/libsodium/src/libsodium/crypto_secretbox/xsalsa20poly1305/secretbox_xsalsa20poly1305.c \
	deps/libsodium/src/libsodium/crypto_sign/crypto_sign.c \
	deps/libsodium/src/libsodium/crypto_sign/ed25519/ref10/keypair.c \
	deps/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c \
	deps/libsodium/src/libsodium/crypto_sign/ed25519/ref10/sign.c \
	deps/libsodium/src/libsodium/crypto_sign/ed25519/sign_ed25519.c \
	deps/libsodium/src/libsodium/crypto_stream/chacha20/ref/chacha20_ref.c \
	deps/libsodium/src/libsodium/crypto_stream/chacha20/stream_chacha20.c \
	deps/libsodium/src/libsodium/crypto_stream/salsa20/ref/salsa20_ref.c \
	deps/libsodium/src/libsodium/crypto_stream/salsa20/stream_salsa20.c \
	deps/libsodium/src/libsodium/crypto_stream/xsalsa20/stream_xsalsa20.c \
	deps/libsodium/src/libsodium/crypto_verify/verify.c \
	deps/libsodium/src/libsodium/randombytes/randombytes.c \
	deps/libsodium/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c \
	deps/libsodium/src/libsodium/sodium/core.c \
	deps/libsodium/src/libsodium/sodium/codecs.c \
	deps/libsodium/src/libsodium/sodium/runtime.c \
	deps/libsodium/src/libsodium/sodium/utils.c \
	deps/libsodium/src/libsodium/sodium/version.c
SODIUM_OBJS := $(call get_objs,SODIUM_SOURCES)
$(SODIUM_OBJS): CFLAGS += \
	-DCONFIGURED=1 \
	-DMINIMAL=1 \
	-DHAVE_ALLOCA \
	-DHAVE_CPUID_V \
	-DHAVE_GCC_MEMORY_FENCES \
	-Wno-unused-function \
	-Wno-unused-variable \
	-Wno-type-limits \
	-Wno-unknown-pragmas \
	-Wno-attributes \
	-Ideps/libsodium/builds/msvc \
	-Ideps/libsodium/src/libsodium/include/sodium
ifneq ($(UNAME_S),OpenBSD)
$(filter-out $(BUILD_DIR)/win%,$(SODIUM_OBJS)): CFLAGS += \
	-DHAVE_ALLOCA_H
endif

SQLITE_SOURCES := deps/sqlite/sqlite3.c
SQLITE_OBJS := $(call get_objs,SQLITE_SOURCES)
$(SQLITE_OBJS): CFLAGS += \
	-DSQLITE_DBCONFIG_DEFAULT_DEFENSIVE \
	-DSQLITE_DEFAULT_MEMSTATUS=0 \
	-DSQLITE_DQS=0 \
	-DSQLITE_ENABLE_MEMSYS5 \
	-DSQLITE_ENABLE_FTS5 \
	-DSQLITE_ENABLE_JSON1 \
	-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
	-DSQLITE_MAX_ATTACHED=1 \
	-DSQLITE_MAX_COLUMN=100 \
	-DSQLITE_MAX_COMPOUND_SELECT=300 \
	-DSQLITE_MAX_EXPR_DEPTH=40 \
	-DSQLITE_MAX_FUNCTION_ARG=8 \
	-DSQLITE_MAX_LENGTH=5242880 \
	-DSQLITE_MAX_LIKE_PATTERN_LENGTH=50 \
	-DSQLITE_MAX_SQL_LENGTH=100000 \
	-DSQLITE_MAX_TRIGGER_DEPTH=10 \
	-DSQLITE_MAX_VARIABLE_NUMBER=100 \
	-DSQLITE_MAX_VDBE_OP=25000 \
	-DSQLITE_OMIT_DEPRECATED \
	-DSQLITE_OMIT_DESERIALIZE \
	-DSQLITE_OMIT_LOAD_EXTENSION \
	-DSQLITE_OMIT_TCL_VARIABLE \
	-DSQLITE_PRAGMA_DEFAULT_WAL_SYNCHRONOUS=1 \
	-DSQLITE_SECURE_DELETE \
	-DSQLITE_THREADSAFE=2 \
	-DSQLITE_UNTESTABLE \
	-DSQLITE_USE_ALLOCA \
	-DHAVE_ISNAN \
	-DHAVE_GETHOSTUUID=0 \
	-Wno-implicit-fallthrough \
	-Wno-unused-but-set-variable \
	-Wno-unused-function \
	-Wno-unused-variable

QUICKJS_SOURCES := \
	deps/quickjs/cutils.c \
	deps/quickjs/libbf.c \
	deps/quickjs/libregexp.c \
	deps/quickjs/libunicode.c \
	deps/quickjs/quickjs.c
QUICKJS_OBJS := $(call get_objs,QUICKJS_SOURCES)
$(QUICKJS_OBJS): CFLAGS += \
	-DCONFIG_VERSION=\"$(shell cat deps/quickjs/VERSION)\" \
	-DCONFIG_BIGNUM \
	-D_GNU_SOURCE \
	-Wno-enum-conversion \
	-Wno-implicit-const-int-float-conversion \
	-Wno-implicit-fallthrough \
	-Wno-sign-compare \
	-Wno-unused-but-set-variable \
	-Wno-unused-variable
$(NONANDROID_TARGETS): CFLAGS += -DDUMP_LEAKS

ifeq ($(UNAME_S),Haiku)
$(QUICKJS_OBJS): CFLAGS += "-Dmalloc_usable_size(x)=0"
else ifeq ($(UNAME_S),OpenBSD)
$(QUICKJS_OBJS): CFLAGS += "-Dmalloc_usable_size(x)=0"
endif

LIBBACKTRACE_SOURCES := \
	deps/libbacktrace/atomic.c \
	deps/libbacktrace/backtrace.c \
	deps/libbacktrace/dwarf.c \
	deps/libbacktrace/fileline.c \
	deps/libbacktrace/print.c \
	deps/libbacktrace/simple.c \
	deps/libbacktrace/sort.c \
	deps/libbacktrace/state.c
LIBBACKTRACE_SOURCES_unix := \
	deps/libbacktrace/elf.c \
	deps/libbacktrace/mmap.c \
	deps/libbacktrace/mmapio.c \
	deps/libbacktrace/posix.c
LIBBACKTRACE_SOURCES_win := \
	deps/libbacktrace/alloc.c \
	deps/libbacktrace/pecoff.c \
	deps/libbacktrace/posix.c \
	deps/libbacktrace/read.c
LIBBACKTRACE_SOURCES_macos := \
	deps/libbacktrace/dwarf.c \
	deps/libbacktrace/macho.c \
	deps/libbacktrace/mmap.c \
	deps/libbacktrace/mmapio.c \
	deps/libbacktrace/posix.c
LIBBACKTRACE_OBJS := $(call get_objs,LIBBACKTRACE_SOURCES)
$(LIBBACKTRACE_OBJS): CFLAGS += \
	-Ideps/libbacktrace_config \
	-Wno-unused-but-set-variable \
	-Wno-maybe-initialized \
	-Wno-unused-function \
	-DBACKTRACE_ELF_SIZE=64

PICOHTTPPARSER_SOURCES := \
	deps/picohttpparser/picohttpparser.c
PICOHTTPPARSER_OBJS := $(call get_objs,PICOHTTPPARSER_SOURCES)

MINIUNZIP_SOURCES := \
	deps/zlib/contrib/minizip/unzip.c \
	deps/zlib/contrib/minizip/ioapi.c \
	deps/zlib/adler32.c \
	deps/zlib/crc32.c \
	deps/zlib/inffast.c \
	deps/zlib/inflate.c \
	deps/zlib/inftrees.c \
	deps/zlib/zutil.c
MINIUNZIP_OBJS := $(call get_objs,MINIUNZIP_SOURCES)
$(MINIUNZIP_OBJS): CFLAGS += \
	-Ideps/zlib \
	-Wno-maybe-uninitialized

LDFLAGS += \
	-pthread \
	-lm
$(LINUX_TARGETS) $(MACOS_TARGETS) $(IOS_TARGETS) $(IOSSIM_TARGETS): LDFLAGS += \
	-lssl \
	-lcrypto
ifneq ($(UNAME_S),Haiku)
ifneq ($(UNAME_S),OpenBSD)
debug release $(MACOS_TARGETS) $(IOS_TARGETS) $(IOSSIM_TARGETS): LDFLAGS += \
	-ldl
endif
endif
$(WINDOWS_TARGETS): LDFLAGS += \
	-lssl \
	-lcrypto \
	-lcrypt32 \
	-ldbghelp \
	-liphlpapi \
	-lkernel32 \
	-lole32 \
	-luserenv \
	-luuid \
	-lws2_32 \
	-lwsock32
$(ANDROID_TARGETS): LDFLAGS += \
	-target $(ANDROID_NDK_TARGET_TRIPLE)$(ANDROID_MIN_SDK_VERSION) \
	-ldl \
	-llog \
	-lssl \
	-lcrypto
$(MACOS_TARGETS) $(IOS_TARGETS) $(IOSSIM_TARGETS): CFLAGS += \
	-Wno-unknown-warning-option
$(IOS_TARGETS) $(IOSSIM_TARGETS): LDFLAGS += \
	-framework Foundation \
	-framework CoreFoundation \
	-framework UIKit \
	-framework WebKit

unix: debug release
win: windebug winrelease
all: $(BUILD_TYPES) default.nix
.PHONY: all win unix

ALL_APP_OBJS := \
	$(APP_OBJS) \
	$(BLOWFISH_OBJS) \
	$(LIBBACKTRACE_OBJS) \
	$(MINIUNZIP_OBJS) \
	$(PICOHTTPPARSER_OBJS) \
	$(QUICKJS_OBJS) \
	$(SODIUM_OBJS) \
	$(SQLITE_OBJS) \
	$(UV_OBJS)

DEPS = $(ALL_APP_OBJS:.o=.d)
-include $(DEPS)

define build_rules
$(1): $(BUILD_DIR)/$(1)/$(PROJECT)$(if $(filter win%,$(1)),.exe)
.PHONY: $(1)

$(BUILD_DIR)/$(1)/$(PROJECT)$(if $(filter win%,$(1)),.exe): $(filter $(BUILD_DIR)/$(1)/%,$(ALL_APP_OBJS))
	@echo "[link] $$@"
	@$$(CC) -o $$@ $$^ $$(LDFLAGS)

$(BUILD_DIR)/$(1)/%.o: %.c
	@mkdir -p $$(dir $$@)
	@echo "[c] $$@"
	@$$(CC) $$(CFLAGS) -c $$< -o $$@

$(BUILD_DIR)/$(1)/%.o: %.m
	@mkdir -p $$(dir $$@)
	@echo "[m] $$@"
	@$$(CC) $$(CFLAGS) -c $$< -o $$@

$(BUILD_DIR)/$(1)/%.o: %.S
	@mkdir -p $$(dir $$@)
	@echo "[as] $$@"
	@$$(AS) -c $$< -o $$@
endef

$(foreach build_type,$(BUILD_TYPES),$(eval $(call build_rules,$(build_type))))

src/version.h : $(firstword $(MAKEFILE_LIST))
	@echo "[version] $@"
	@echo "#define VERSION_NUMBER \"$(VERSION_NUMBER)\"" > $@
	@echo "#define VERSION_NAME \"$(VERSION_NAME)\"" >> $@

src/android/AndroidManifest.xml : $(firstword $(MAKEFILE_LIST))
	@echo "[android_version] $@"
	@sed -i \
		-e 's/versionCode=".*"/versionCode="$(VERSION_CODE)"/' \
		-e 's/versionName=".*"/versionName="$(VERSION_NUMBER)"/' \
		-e 's/android:minSdkVersion="[[:digit:]]*"/android:minSdkVersion="$(ANDROID_MIN_SDK_VERSION)"/' \
		-e 's/android:targetSdkVersion="[[:digit:]]*"/android:targetSdkVersion="$(ANDROID_TARGET_SDK_VERSION)"/' \
		$@

default.nix : $(firstword $(MAKEFILE_LIST))
	@echo "[version] $@"
	@sed -i -e 's/version = ".*";/version = "$(VERSION_NUMBER)";/' $@

# Android support.
out/res/layout_activity_main.xml.flat: src/android/res/layout/activity_main.xml
	@mkdir -p $(dir $@)
	@echo "[aapt2] $@"
	@$(ANDROID_BUILD_TOOLS)/aapt2 compile -o out/res/ src/android/res/layout/activity_main.xml

out/res/drawable_icon.xml.flat: src/android/res/drawable/icon.xml
	@mkdir -p $(dir $@)
	@echo "[aapt2] $@"
	@$(ANDROID_BUILD_TOOLS)/aapt2 compile -o out/res/ src/android/res/drawable/icon.xml

out/apk/res.apk out/gen/com/unprompted/tildefriends/R.java: out/res/layout_activity_main.xml.flat out/res/drawable_icon.xml.flat src/android/AndroidManifest.xml
	@mkdir -p $(dir $@)
	@$(ANDROID_BUILD_TOOLS)/aapt2 link -I $(ANDROID_PLATFORM)/android.jar out/res/layout_activity_main.xml.flat out/res/drawable_icon.xml.flat --manifest src/android/AndroidManifest.xml -o out/apk/res.apk --java out/gen/

JAVA_FILES := out/gen/com/unprompted/tildefriends/R.java $(wildcard src/android/com/unprompted/tildefriends/*.java)
CLASS_FILES := $(foreach src,$(JAVA_FILES),out/classes/com/unprompted/tildefriends/$(notdir $(src:.java=.class)))

$(CLASS_FILES) &: $(JAVA_FILES)
	@echo "[javac] $(CLASS_FILES)"
	@javac --release 8 -encoding UTF-8 -Xlint:deprecation -XDuseUnsharedTable=true -classpath $(ANDROID_PLATFORM)/android.jar:$(ANDROID_BUILD_TOOLS)/core-lambda-stubs.jar -d out/classes $(JAVA_FILES)

out/apk/classes.dex: $(CLASS_FILES)
	@mkdir -p $(dir $@)
	@echo "[d8] $@"
	@$(ANDROID_BUILD_TOOLS)/d8 --$(BUILD_TYPE) --lib $(ANDROID_PLATFORM)/android.jar --output $(dir $@) out/classes/com/unprompted/tildefriends/*.class

PACKAGE_DIRS := \
	apps/ \
	core/ \
	deps/codemirror/ \
	deps/prettier/ \
	deps/lit/

RAW_FILES := $(filter-out apps/blog% apps/issues% apps/welcome% apps/journal% %.map, $(shell find $(PACKAGE_DIRS) -type f))

out/apk/TildeFriends-arm-debug.unsigned.apk: BUILD_TYPE := debug
out/apk/TildeFriends-arm-release.unsigned.apk: BUILD_TYPE := release
out/apk/TildeFriends-x86-debug.unsigned.apk: BUILD_TYPE := debug
out/apk/TildeFriends-x86-release.unsigned.apk: BUILD_TYPE := release

out/apk/TildeFriends-arm-debug.unsigned.apk: out/apk/classes.dex out/androiddebug/tildefriends out/androiddebug-armv7a/tildefriends $(RAW_FILES) out/apk/res.apk
out/apk/TildeFriends-arm-release.unsigned.apk: out/apk/classes.dex out/androidrelease/tildefriends out/androidrelease-armv7a/tildefriends $(RAW_FILES) out/apk/res.apk
out/apk/TildeFriends-x86-debug.unsigned.apk: out/apk/classes.dex out/androiddebug-x86_64/tildefriends out/androiddebug-x86/tildefriends $(RAW_FILES) out/apk/res.apk
out/apk/TildeFriends-x86-release.unsigned.apk: out/apk/classes.dex out/androidrelease-x86_64/tildefriends out/androidrelease-x86/tildefriends $(RAW_FILES) out/apk/res.apk

out/apk/TildeFriends-arm-%.unsigned.apk:
	@mkdir -p $(dir $@) out/apk-arm-$(BUILD_TYPE)/lib/arm64-v8a/ out/apk-arm-$(BUILD_TYPE)/lib/armeabi-v7a/
	@echo "[aapt] $@"
	@cp out/android$(BUILD_TYPE)/tildefriends out/apk-arm-$(BUILD_TYPE)/lib/arm64-v8a/tildefriends.so
	@cp out/android$(BUILD_TYPE)-armv7a/tildefriends out/apk-arm-$(BUILD_TYPE)/lib/armeabi-v7a/tildefriends.so
	@$(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip out/apk-arm-$(BUILD_TYPE)/lib/arm64-v8a/tildefriends.so
	@$(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip out/apk-arm-$(BUILD_TYPE)/lib/armeabi-v7a/tildefriends.so
	@cp out/apk/res.apk $@.zip
	@cp out/apk/classes.dex out/apk-arm-$(BUILD_TYPE)/
	@cd out/apk-arm-$(BUILD_TYPE) && zip -u ../../$@.zip -q -9 -r . && cd ../../
	@zip -u $@.zip -q -9 $(RAW_FILES)
	@$(ANDROID_BUILD_TOOLS)/zipalign -f 4 $@.zip $@

out/apk/TildeFriends-x86-%.unsigned.apk:
	@mkdir -p $(dir $@) out/apk-x86-$(BUILD_TYPE)/lib/x86_64/ out/apk-x86-$(BUILD_TYPE)/lib/x86/
	@echo "[aapt] $@"
	@cp out/android$(BUILD_TYPE)-x86_64/tildefriends out/apk-x86-$(BUILD_TYPE)/lib/x86_64/tildefriends.so
	@cp out/android$(BUILD_TYPE)-x86/tildefriends out/apk-x86-$(BUILD_TYPE)/lib/x86/tildefriends.so
	@$(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip out/apk-x86-$(BUILD_TYPE)/lib/x86_64/tildefriends.so
	@$(ANDROID_NDK)/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip out/apk-x86-$(BUILD_TYPE)/lib/x86/tildefriends.so
	@cp out/apk/res.apk $@.zip
	@cp out/apk/classes.dex out/apk-x86-$(BUILD_TYPE)/
	@cd out/apk-x86-$(BUILD_TYPE) && zip -u ../../$@.zip -q -9 -r . && cd ../../
	@zip -u $@.zip -q -9 $(RAW_FILES)
	@$(ANDROID_BUILD_TOOLS)/zipalign -f 4 $@.zip $@

out/%.apk: out/apk/%.unsigned.apk
	@echo "[apksigner] $(notdir $@)"
	@$(ANDROID_BUILD_TOOLS)/apksigner sign --ks .keys/android.jks --ks-key-alias androidKey --ks-pass pass:android --key-pass pass:android --min-sdk-version $(ANDROID_MIN_SDK_VERSION) --out $@ $<

out/%.zopfli.apk: out/%.apk
	@echo "[zopfli] $(notdir $@)"
	$(ANDROID_BUILD_TOOLS)/zipalign -f -z 4 $< $@.zopfli
	@$(ANDROID_BUILD_TOOLS)/apksigner sign --ks .keys/android.jks --ks-key-alias androidKey --ks-pass pass:android --key-pass pass:android --min-sdk-version $(ANDROID_MIN_SDK_VERSION) --out $@ $@.zopfli

release-apk: out/TildeFriends-arm-release.zopfli.apk out/TildeFriends-x86-release.zopfli.apk
.PHONY: release-apk

releaseapkgo: out/TildeFriends-arm-release.apk
	@adb install -r $<
	@adb shell am start com.unprompted.tildefriends/.TildeFriendsActivity
.PHONY: releaseapkgo

# iOS Support
out/%.app/Info.plist: src/ios/Info.plist
	@mkdir -p $(dir $@)
	@cp -v $< $@
out/%.app/tildefriends.png: src/ios/tildefriends.png
	@mkdir -p $(dir $@)
	@cp -v $< $@

out/data.zip: $(RAW_FILES)
	@zip -u $@ -q -9 $(RAW_FILES)

out/tildefriends-%.app/tildefriends: out/%/tildefriends out/tildefriends-%.app/Info.plist out/tildefriends-%.app/tildefriends.png out/data.zip
	@mkdir -p $(dir $@)
	@cp -v $< $@
ifeq ($(HAVE_LINUX_IOS),1)
	@zsign -q -k .keys/apple.p12 -f -m src/ios/embedded.mobileprovision $(realpath $(dir $@))
endif
.SECONDARY:
out/tildefriends-%.ipa: out/tildefriends-ios%.app/tildefriends
	@echo "[ipa] $@"
	@rm -rf $@.tmp $@
	@mkdir -p $@.tmp/Payload/tildefriends.app/
	@cp -R $(dir $<)/* $@.tmp/Payload/tildefriends.app/
	@cd $@.tmp/ && zip -u ../../$@ -q -9 -r ./
	@rm -rf $@.tmp/


out/%/tildefriends.standalone: out/%/tildefriends out/data.zip
	@echo "[standalone] $@"
	@cat $< out/data.zip > $@
	@chmod +x $@
out/%/tildefriends.standalone.exe: out/%/tildefriends.exe out/data.zip
	@echo "[standalone] $@"
	@cat $< out/data.zip > $@
	@chmod +x $@

iossimdebug-app: out/tildefriends-iossimdebug.app/tildefriends
iossimrelease-app: out/tildefriends-iossimrelease.app/tildefriends
iosdebug-app: out/tildefriends-iosdebug.app/tildefriends
iosrelease-app: out/tildefriends-iosrelease.app/tildefriends

iosdebug-ipa: out/tildefriends-debug.ipa
iosrelease-ipa: out/tildefriends-release.ipa
.PHONY: iossimdebug-app iossimrelease-app iosdebug-app iosrelease-app

ios%go: out/tildefriends-ios%.app/tildefriends
	ideviceinstaller -i $(realpath $(dir $<))

iossimdebuggo: out/tildefriends-iossimdebug.app/tildefriends
	xcrun simctl install booted out/tildefriends-iossimdebug.app/
	xcrun simctl launch booted com.unprompted.tildefriends
.PHONY: iossimdebuggo

apklog:
	@adb logcat *:S tildefriends
.PHONY: apklog

fetchdeps:
	@echo "[fetch] libuv"
	@test -f out/deps/libuv.tar.gz && test "$$(cat out/deps/libuv.txt 2>/dev/null)" = $(LIBUV_URL) || (mkdir -p out/deps/ && curl -q $(LIBUV_URL) -o out/deps/libuv.tar.gz)
	@test -d deps/libuv/ && test "$$(cat out/deps/libuv.txt 2>/dev/null)" = $(LIBUV_URL) || (rm -rf deps/libuv/ && mkdir -p deps/libuv/ && tar -C deps/libuv/ -m --strip=1 -xf out/deps/libuv.tar.gz)
	@echo -n $(LIBUV_URL) > out/deps/libuv.txt
	@echo "[fetch] sqlite"
	@test -f out/deps/sqlite.zip && test "$$(cat out/deps/sqlite.txt 2>/dev/null)" = $(SQLITE_URL) || (mkdir -p out/deps/ && curl -q $(SQLITE_URL) -o out/deps/sqlite.zip)
	@test -d deps/sqlite/ && test "$$(cat out/deps/sqlite.txt 2>/dev/null)" = $(SQLITE_URL) || (mkdir -p deps/sqlite/ && unzip -qDjo -d deps/sqlite/ out/deps/sqlite.zip)
	@echo -n $(SQLITE_URL) > out/deps/sqlite.txt
	@echo "[fetch] prettier"
	@test -f deps/prettier/standalone.mjs || curl -q --create-dirs -O --output-dir deps/prettier/ https://cdn.jsdelivr.net/npm/prettier@3.2.5/standalone.mjs
	@test -f deps/prettier/html.mjs || curl -q --create-dirs -O --output-dir deps/prettier/ https://cdn.jsdelivr.net/npm/prettier@3.2.5/plugins/html.mjs
	@test -f deps/prettier/babel.mjs || curl -q --create-dirs -O --output-dir deps/prettier/ https://cdn.jsdelivr.net/npm/prettier@3.2.5/plugins/babel.mjs
	@test -f deps/prettier/estree.mjs || curl -q --create-dirs -O --output-dir deps/prettier/ https://cdn.jsdelivr.net/npm/prettier@3.2.5/plugins/estree.mjs
.PHONY: fetchdeps

ANDROID_DEPS := deps/openssl/android/arm64-v8a/usr/local/lib/libssl.a
$(ANDROID_DEPS):
	+@tools/ssl-android
$(filter $(BUILD_DIR)/android%,$(APP_OBJS)): | $(ANDROID_DEPS)

ifeq ($(HAVE_WIN),1)
WINDOWS_DEPS := deps/openssl/mingw64/usr/local/lib/libssl.a
$(WINDOWS_DEPS):
	+@tools/ssl-mingw64
$(filter $(BUILD_DIR)/win%,$(APP_OBJS)): | $(WINDOWS_DEPS)
endif

ifeq ($(UNAME_S),Darwin)
IOS_DEPS := deps/openssl/ios/ios64-xcrun/usr/local/lib/libssl.a
$(IOS_DEPS):
	+@tools/ssl-ios
$(filter $(BUILD_DIR)/ios%,$(APP_OBJS)): | $(IOS_DEPS)
endif

clean:
	rm -rf $(BUILD_DIR)
.PHONY: clean

dist: release-apk iosrelease-ipa $(if $(HAVE_WIN), out/winrelease/tildefriends.standalone.exe) default.nix
	@echo [archive] dist/tildefriends-$(VERSION_NUMBER).tar.xz
	@rm -rf out/tildefriends-$(VERSION_NUMBER)
	@mkdir -p dist/ out/tildefriends-$(VERSION_NUMBER)
	@git ls-files --recurse-submodules | tar -c -T- | tar -x -C out/tildefriends-$(VERSION_NUMBER)
	@tar \
		--exclude=apps/welcome* \
		--exclude=deps/libbacktrace/Isaac.Newton-Opticks.txt \
		--exclude=deps/libsodium/builds/msvc/vs* \
		--exclude=deps/libsodium/builds/msvc/build \
		--exclude=deps/libsodium/builds/msvc/properties \
		--exclude=deps/libsodium/configure \
		--exclude=deps/libsodium/test \
		--exclude=deps/libuv/docs \
		--exclude=deps/libuv/test \
		--exclude=deps/openssl \
		--exclude=deps/speedscope/*.map \
		--exclude=deps/sqlite/shell.c \
		--exclude=deps/zlib/contrib/vstudio \
		--exclude=deps/zlib/doc \
		-caf dist/tildefriends-$(VERSION_NUMBER).tar.xz \
		-C out/ \
		tildefriends-$(VERSION_NUMBER)
	@echo "[cp] TildeFriends-x86-$(VERSION_NUMBER).apk"
	@cp out/TildeFriends-x86-release.zopfli.apk dist/TildeFriends-x86-$(VERSION_NUMBER).apk
	@echo "[cp] TildeFriends-arm-$(VERSION_NUMBER).apk"
	@cp out/TildeFriends-arm-release.zopfli.apk dist/TildeFriends-arm-$(VERSION_NUMBER).apk
	@echo "[cp] TildeFriends-$(VERSION_NUMBER).ipa"
	@cp out/tildefriends-release.ipa dist/TildeFriends-$(VERSION_NUMBER).ipa
	@test $(HAVE_WIN) && echo "[cp] tildefriends-$(VERSION_NUMBER).exe"
	@test $(HAVE_WIN) && cp out/winrelease/tildefriends.standalone.exe dist/tildefriends-$(VERSION_NUMBER).exe
.PHONY: dist

dist-test: dist
	@tar -xf tildefriends-$(VERSION_NUMBER).tar.xz
	@$(MAKE) -C tildefriends-$(VERSION_NUMBER)/ debug release
	@docker build tildefriends-$(VERSION_NUMBER)/
	@rm -rf tildefriends-$(VERSION_NUMBER)
.PHONY: dist-test

format:
	@clang-format -i $(wildcard src/*.c src/*.h src/*.m)
.PHONY: format

prettier:
	@npm run prettier
.PHONY: prettier

docs:
	@doxygen
.PHONY: docs