# Makefile for building C example library

CC = gcc
CFLAGS = -Wall -fPIC -shared
TARGET = libexample
SRC = example.c

# Determine OS and set appropriate library extension
UNAME := $(shell uname)
ifeq ($(UNAME), Linux)
    EXT = so
endif
ifeq ($(UNAME), Darwin)
    EXT = dylib
endif
ifeq ($(OS), Windows_NT)
    EXT = dll
endif

all: $(TARGET).$(EXT)

$(TARGET).$(EXT): $(SRC)
	$(CC) $(CFLAGS) -o $@ $<

# Build C# library (requires .NET 8.0 SDK)
csharp:
	@echo "Building C# library..."
	dotnet publish CSharpExample.csproj -c Release
	@echo "C# library built successfully!"

# Build C# library with NativeAOT (creates true native library)
csharp-aot:
	@echo "Building C# library with NativeAOT..."
	@if [ "$$(uname)" = "Darwin" ]; then \
		if [ "$$(uname -m)" = "arm64" ]; then \
			echo "Detected macOS ARM64"; \
			dotnet publish CSharpExample.csproj -c Release -r osx-arm64; \
		else \
			echo "Detected macOS x64"; \
			dotnet publish CSharpExample.csproj -c Release -r osx-x64; \
		fi \
	elif [ "$$(uname)" = "Linux" ]; then \
		echo "Detected Linux"; \
		dotnet publish CSharpExample.csproj -c Release -r linux-x64; \
	else \
		echo "Detected Windows"; \
		dotnet publish CSharpExample.csproj -c Release -r win-x64; \
	fi
	@echo "C# NativeAOT library built successfully!"

clean:
	rm -f $(TARGET).so $(TARGET).dylib $(TARGET).dll
	rm -rf bin/ obj/

.PHONY: all clean csharp csharp-aot
