# ----------------------------------------------------------------------------
# libqlclglwidget — QOpenGLWidget helper library for LazOpenGLContextEx
#
# Build variants:
#   make / make linux    native Linux build            -> ../libqlclglwidget.so
#   make macos           native macOS build (on a Mac) -> ../libqlclglwidget.dylib
#   make windows         qlclglwidget.dll with a MinGW toolchain:
#                          * cross-build from Linux (mingw-w64), OR
#                          * natively on Windows with a Qt MinGW kit that has
#                            no pkg-config — override the variables:
#                            make windows MINGW_CXX=g++ \
#                              WIN_QT_INC=C:/Qt/6.x.x/mingw_64/include \
#                              WIN_QT_LIB=C:/Qt/6.x.x/mingw_64/lib
#   make windows-native  natively on Windows in an MSYS2 MinGW64 shell
#                        (uses pkg-config; auto-selected by plain `make` there)
#   build-msvc.bat       natively on Windows with MSVC (see the .bat file);
#                        required when the target Qt6/libQt6Pas is an MSVC
#                        build — MinGW and MSVC C++ ABIs are incompatible
#
#   make install         install the host-platform artifact into $(PREFIX)/lib
#                        (DESTDIR supported; run ldconfig yourself on Linux)
#   make install-app APPDIR=/path/to/app/dir
#                        copy every built artifact next to the app binary
#                        (the Pascal loader looks there first)
#   make clean
#
# Windows cross-build requirements (on the Linux build machine):
#   - mingw-w64 toolchain: x86_64-w64-mingw32-g++
#   - a *MinGW* build of Qt6 (headers + import libs), one of:
#       * Arch AUR: mingw-w64-qt6-base   (installs to /usr/x86_64-w64-mingw32)
#       * MXE (https://mxe.cc) with qt6  (set MINGW_PREFIX to the MXE usr dir)
#       * a Qt "MinGW" kit copied from a Windows Qt install
#         (set WIN_QT_INC / WIN_QT_LIB manually)
#
# Windows native (MSYS2 MinGW64 shell) requirements:
#   pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-qt6-base pkgconf make
#
# The MinGW-built DLL works only with MinGW Qt6 DLLs on the target machine,
# the MSVC-built DLL only with MSVC Qt6 DLLs (incompatible C++ ABIs).
#
# macOS notes: needs Xcode command line tools + Qt6 with pkg-config files,
#   e.g. Homebrew:  brew install qt pkgconf
#                   export PKG_CONFIG_PATH="$(brew --prefix qt)/libexec/lib/pkgconfig"
# ----------------------------------------------------------------------------

NAME    := qlclglwidget
SRC     := qlclglwidget.cpp
OUTDIR  := ..

PREFIX  ?= /usr/local
DESTDIR ?=

CXXFLAGS ?= -O2
CXXSTD   := -std=c++17

PKG_CONFIG ?= pkg-config
QT_PKGS    := Qt6Widgets Qt6OpenGLWidgets

# Windows cross toolchain / Qt location (override as needed)
MINGW_CXX    ?= x86_64-w64-mingw32-g++
MINGW_PREFIX ?= /usr/x86_64-w64-mingw32
WIN_QT_INC   ?= $(MINGW_PREFIX)/include/qt6
WIN_QT_LIB   ?= $(MINGW_PREFIX)/lib
WIN_QT_LIBS  ?= -lQt6Widgets -lQt6OpenGLWidgets -lQt6Gui -lQt6Core

LINUX_OUT := $(OUTDIR)/lib$(NAME).so
MACOS_OUT := $(OUTDIR)/lib$(NAME).dylib
WIN_OUT   := $(OUTDIR)/$(NAME).dll

UNAME := $(shell uname -s)
ifeq ($(UNAME),Darwin)
  HOST_OUT := $(MACOS_OUT)
  HOST_TARGET := macos
else ifneq (,$(findstring MINGW,$(UNAME))$(findstring MSYS,$(UNAME)))
  # MSYS2 MinGW64 shell on Windows
  HOST_OUT := $(WIN_OUT)
  HOST_TARGET := windows-native
else
  HOST_OUT := $(LINUX_OUT)
  HOST_TARGET := linux
endif

.PHONY: all linux macos windows windows-native install install-app clean

all: $(HOST_TARGET)

linux: $(LINUX_OUT)

$(LINUX_OUT): $(SRC)
	$(CXX) $(CXXFLAGS) $(CXXSTD) -shared -fPIC \
	  $(shell $(PKG_CONFIG) --cflags $(QT_PKGS)) \
	  -o $@ $(SRC) \
	  $(shell $(PKG_CONFIG) --libs $(QT_PKGS))

macos: $(MACOS_OUT)

$(MACOS_OUT): $(SRC)
	$(CXX) $(CXXFLAGS) $(CXXSTD) -dynamiclib -fPIC \
	  $(shell $(PKG_CONFIG) --cflags $(QT_PKGS)) \
	  -o $@ $(SRC) \
	  $(shell $(PKG_CONFIG) --libs $(QT_PKGS)) \
	  -Wl,-install_name,@rpath/lib$(NAME).dylib

# MinGW build: cross from Linux, or native on Windows with a Qt MinGW kit
# (no pkg-config needed — plain include/lib paths)
windows: $(WIN_OUT)

$(WIN_OUT): $(SRC)
	$(MINGW_CXX) $(CXXFLAGS) $(CXXSTD) -shared \
	  -I$(WIN_QT_INC) \
	  -I$(WIN_QT_INC)/QtCore \
	  -I$(WIN_QT_INC)/QtGui \
	  -I$(WIN_QT_INC)/QtWidgets \
	  -I$(WIN_QT_INC)/QtOpenGL \
	  -I$(WIN_QT_INC)/QtOpenGLWidgets \
	  -o $@ $(SRC) \
	  -L$(WIN_QT_LIB) $(WIN_QT_LIBS) \
	  -static-libgcc -static-libstdc++ \
	  -Wl,--out-implib,$(OUTDIR)/lib$(NAME).dll.a

# native Windows build in an MSYS2 MinGW64 shell (pkg-config available)
windows-native:
	$(CXX) $(CXXFLAGS) $(CXXSTD) -shared \
	  $(shell $(PKG_CONFIG) --cflags $(QT_PKGS)) \
	  -o $(WIN_OUT) $(SRC) \
	  $(shell $(PKG_CONFIG) --libs $(QT_PKGS)) \
	  -static-libgcc -static-libstdc++ \
	  -Wl,--out-implib,$(OUTDIR)/lib$(NAME).dll.a

install: $(HOST_OUT)
	install -d $(DESTDIR)$(PREFIX)/lib
	install -m755 $(HOST_OUT) $(DESTDIR)$(PREFIX)/lib/

install-app:
	@test -n "$(APPDIR)" || { echo "usage: make install-app APPDIR=/path/to/app/dir"; exit 1; }
	install -d $(APPDIR)
	@for f in $(LINUX_OUT) $(MACOS_OUT) $(WIN_OUT); do \
	  if [ -f $$f ]; then install -m755 $$f $(APPDIR)/ && echo "installed $$f -> $(APPDIR)/"; fi; \
	done

clean:
	rm -f $(LINUX_OUT) $(MACOS_OUT) $(WIN_OUT) \
	  $(OUTDIR)/lib$(NAME).dll.a $(OUTDIR)/$(NAME).lib $(OUTDIR)/$(NAME).exp \
	  $(NAME).obj
