# requires nall/Makefile

# imports:
# $(qt.libraries) -- list of Qt components to link against

# exports the following symbols:
# $(moc)        -- meta-object compiler
# $(rcc)        -- resource compiler
# $(qt.flags)   -- includes for compiling
# $(qt.options) -- libraries for linking

ifeq ($(qt.version),)
  qt.version := 5
endif

ifeq ($(moc),)
  ifeq ($(qt.path),)
    moc := moc
  else
    moc := $(qt.path)/bin/moc
  endif
endif

ifeq ($(rcc),)
  ifeq ($(qt.path),)
    rcc := rcc
  else
    rcc := $(qt.path)/bin/rcc
  endif
endif

ifeq ($(platform),windows)
  ifeq ($(qt.path),)
    # find Qt install directory from PATH environment variable
    qt.path := $(foreach path,$(subst ;, ,$(PATH)),$(if $(wildcard $(path)/$(moc).exe),$(path)))
    qt.path := $(strip $(qt.path))
    qt.path := $(subst \,/,$(qt.path))
    qt.path := $(patsubst %/bin,%,$(qt.path))
  endif
  ifeq ($(qt.path),)
    # use local copy of Qt, stored in the same directory as nall and ruby
    qt.path := ../qt/qtbase
    ifeq ($(moc),moc)
      moc := ../qt/qtbase/bin/moc
    endif
    ifeq ($(rcc),rcc)
      rcc := ../qt/qtbase/bin/rcc
    endif
  endif

  qt.flags := -I$(qt.path)/include
  qt.flags += $(foreach lib,$(qt.libraries),-I$(qt.path)/include/$(lib))

  qt.options := -L$(qt.path)/lib
  qt.options += -L$(qt.path)/plugins/imageformats

  qt.options += $(subst -lQt,-lQt5,$(foreach lib,$(qt.libraries),-l$(lib)))
  qt.options += -lmingw32 -lqtmain -lcomdlg32 -loleaut32 -limm32 -lwinmm
  qt.options += -lwinspool -lmsimg32 -lole32 -ladvapi32 -lws2_32 -luuid -lgdi32
else ifeq ($(platform),macos)
  ifeq ($(qt.path),)
    qt.flags := $(foreach lib,$(qt.libraries),-I/Library/Frameworks/$(lib).framework/Versions/$(qt.version)/Headers)
    qt.options := -L/Library/Frameworks
  else
    qt.flags := -F$(qt.path)/lib
    qt.flags += $(foreach lib,$(qt.libraries),-I$(qt.path)/lib/$(lib).framework/Versions/$(qt.version)/Headers)
    qt.options += -F$(qt.path)/lib -Wl,-rpath,$(qt.path)/lib
  endif
  qt.flags += $(foreach lib,$(qt.libraries),-I/usr/local/lib/$(lib).framework/Versions/$(qt.version)/Headers)
  qt.options += $(foreach lib,$(qt.libraries),-framework $(lib))
  qt.options += -framework Carbon
  qt.options += -framework CoreFoundation
  qt.options += -framework IOKit
  qt.options += -framework Cocoa
  qt.options += -framework OpenGL
  qt.options += -framework AppKit
  qt.options += -framework ApplicationServices
else
  qt.libraries := $(subst Qt,Qt5,$(qt.libraries))
  qt.flags := $(shell pkg-config --cflags $(qt.libraries))
  qt.options := $(shell pkg-config --libs $(qt.libraries))

  ifneq ($(filter reduce_relocations, $(shell pkg-config --variable qt_config $(qt.libraries))),)
    qt.flags += -fPIC
    qt.options += -fPIC
  endif
endif
