# Cross-platform Makefile for Ryubing/Ryujinx.
# Requires GNU Make and .NET SDK 10+.

SHELL := /bin/sh

DOTNET ?= dotnet
CONFIG ?= Release
PROJECT ?= src/Ryujinx
SOLUTION ?= Ryujinx.sln

UNAME_S := $(shell uname -s 2>/dev/null || echo Unknown)
UNAME_M := $(shell uname -m 2>/dev/null || echo unknown)

# Runtime identifier can always be overridden: make publish RID=linux-arm64
ifeq ($(OS),Windows_NT)
RID ?= win-x64
else ifeq ($(UNAME_S),Darwin)
ifneq (,$(filter $(UNAME_M),arm64 aarch64))
RID ?= osx-arm64
else
RID ?= osx-x64
endif
else ifeq ($(UNAME_S),Linux)
ifneq (,$(filter $(UNAME_M),aarch64 arm64))
RID ?= linux-arm64
else
RID ?= linux-x64
endif
else ifneq (,$(findstring MINGW,$(UNAME_S)))
RID ?= win-x64
else ifneq (,$(findstring MSYS,$(UNAME_S)))
RID ?= win-x64
else ifneq (,$(findstring CYGWIN,$(UNAME_S)))
RID ?= win-x64
else
RID ?= linux-x64
endif

BUILD_DIR ?= build
PUBLISH_DIR ?= publish/$(RID)
SELF_CONTAINED ?= false

VERSION ?= 1.1.0-local
SOURCE_REVISION_ID ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
CANARY ?= 0

MAC_TEMP_DIR ?= artifacts/macos/tmp
MAC_OUTPUT_DIR ?= artifacts/macos/out
ENTITLEMENTS ?= distribution/macos/entitlements.xml

APPIMAGE_BUILD_DIR ?= $(PUBLISH_DIR)
APPIMAGE_OUT_DIR ?= artifacts/linux/appimage

.PHONY: help info restore build rebuild clean test publish publish-self-contained run macos-bundle linux-appimage

help:
	@echo "Targets:"
	@echo "  make info                  Show resolved build variables"
	@echo "  make restore               Restore NuGet packages"
	@echo "  make build                 Build solution"
	@echo "  make rebuild               Clean + build"
	@echo "  make test                  Run tests"
	@echo "  make publish               Publish app for RID=$(RID)"
	@echo "  make publish-self-contained Publish self-contained app for RID=$(RID)"
	@echo "  make run                   Run Ryujinx project"
	@echo "  make macos-bundle          Build macOS universal .app (Darwin only)"
	@echo "  make linux-appimage        Build Linux AppImage (Linux only)"
	@echo ""
	@echo "Common overrides:"
	@echo "  CONFIG=Debug|Release"
	@echo "  RID=osx-arm64|osx-x64|linux-x64|linux-arm64|win-x64"
	@echo "  PUBLISH_DIR=publish/custom"
	@echo "  VERSION=1.1.0 SOURCE_REVISION_ID=$$(git rev-parse --short HEAD)"

info:
	@echo "OS=$(UNAME_S) ARCH=$(UNAME_M)"
	@echo "CONFIG=$(CONFIG) RID=$(RID)"
	@echo "PROJECT=$(PROJECT) SOLUTION=$(SOLUTION)"
	@echo "BUILD_DIR=$(BUILD_DIR)"
	@echo "PUBLISH_DIR=$(PUBLISH_DIR)"

restore:
	$(DOTNET) restore $(SOLUTION)

build:
	$(DOTNET) build $(SOLUTION) -c $(CONFIG) -o $(BUILD_DIR)

rebuild: clean build

clean:
	$(DOTNET) clean $(SOLUTION) -c $(CONFIG)

test:
	$(DOTNET) test $(SOLUTION) -c $(CONFIG) --no-build

publish:
	$(DOTNET) publish $(PROJECT) -c $(CONFIG) -r $(RID) -o $(PUBLISH_DIR) --self-contained $(SELF_CONTAINED)

publish-self-contained:
	$(DOTNET) publish $(PROJECT) -c $(CONFIG) -r $(RID) -o $(PUBLISH_DIR) --self-contained true

run:
	$(DOTNET) run --project $(PROJECT) -c $(CONFIG)

macos-bundle:
	@if [ "$(UNAME_S)" != "Darwin" ]; then \
		echo "macos-bundle target is intended for macOS (Darwin)."; \
		exit 1; \
	fi
	bash distribution/macos/create_macos_build_ava.sh \
		. \
		$(MAC_TEMP_DIR) \
		$(MAC_OUTPUT_DIR) \
		$(ENTITLEMENTS) \
		$(VERSION) \
		$(SOURCE_REVISION_ID) \
		$(CONFIG) \
		$(CANARY)

linux-appimage:
	@if [ "$(UNAME_S)" != "Linux" ]; then \
		echo "linux-appimage target is intended for Linux."; \
		exit 1; \
	fi
	BUILDDIR=$(APPIMAGE_BUILD_DIR) OUTDIR=$(APPIMAGE_OUT_DIR) sh distribution/linux/appimage/build-appimage.sh
