#!/usr/bin/make -f
#
# \brief  Tool for preparing the Qt5 tool-chain for the Genode OS Framework
# \author Christian Prochaska
# \date   2018-01-03
#

SHELL   = bash
ECHO    = @echo -e
VERBOSE = @

help:
	$(ECHO)
	$(ECHO) "Build Qt5 tools for the Genode OS Framework tool chain"
	$(ECHO)
	$(ECHO) "--- available commands ---"
	$(ECHO) "build         - build Qt5 tools"
	$(ECHO) "install       - install Qt5 tools to '$(INSTALL_LOCATION)'"
	$(ECHO) "clean         - clean everything except contrib sources"
	$(ECHO)
	$(ECHO) "--- available command line options ---"
	$(ECHO) "MAKE_JOBS=4 - number of parallel make jobs (default: 4)"
	$(ECHO)

.PHONY: build help install

#
# Enable parallel build for 2nd-level $(MAKE) by default
#

MAKE_JOBS ?= 4

#
# Source, build and install location
#

GENODE_DIR       ?= $(realpath $(dir $(firstword $(MAKEFILE_LIST)))/..)
CONTRIB_DIR       = $(shell $(GENODE_DIR)/tool/ports/current qt5-host)/src/lib/qt5-host
BUILD_DIR         = $(GENODE_DIR)/build/tool/qt5-5.8.0
INSTALL_LOCATION  = /usr/local/genode-qt5

$(CONTRIB_DIR)/configure:
	$(VERBOSE)$(GENODE_DIR)/tool/ports/prepare_port qt5-host

QMAKE = $(BUILD_DIR)/bin/qmake

$(QMAKE): $(CONTRIB_DIR)/configure
	$(VERBOSE)mkdir -p $(BUILD_DIR)
	$(VERBOSE)cd $(BUILD_DIR) && $(CONTRIB_DIR)/configure -opensource -confirm-license -no-iconv -no-opengl -prefix $(INSTALL_LOCATION)

$(BUILD_DIR)/src/Makefile: $(QMAKE)
	$(VERBOSE)cd $(BUILD_DIR)/src && \
	          $(QMAKE) -o Makefile \
	          $(CONTRIB_DIR)/src/src.pro -qtconf $(BUILD_DIR)/bin/qt.conf -- -opensource

$(BUILD_DIR)/bin/moc: $(BUILD_DIR)/src/Makefile
	$(VERBOSE)cd $(BUILD_DIR)/src && \
	          make -j$(MAKE_JOBS) sub-moc

$(BUILD_DIR)/bin/rcc: $(BUILD_DIR)/src/Makefile
	$(VERBOSE)cd $(BUILD_DIR)/src && \
	          make -j$(MAKE_JOBS) sub-rcc

$(BUILD_DIR)/bin/uic: $(BUILD_DIR)/src/Makefile
	$(VERBOSE)cd $(BUILD_DIR)/src && \
	          make -j$(MAKE_JOBS) sub-uic

build: $(BUILD_DIR)/bin/moc \
       $(BUILD_DIR)/bin/rcc \
       $(BUILD_DIR)/bin/uic

$(INSTALL_LOCATION)/bin/moc: $(BUILD_DIR)/bin/moc
	$(VERBOSE)cd $(BUILD_DIR)/src && \
              sudo make sub-moc-install_subtargets

$(INSTALL_LOCATION)/bin/rcc: $(BUILD_DIR)/bin/rcc
	$(VERBOSE)cd $(BUILD_DIR)/src && \
              sudo make sub-rcc-install_subtargets

$(INSTALL_LOCATION)/bin/uic: $(BUILD_DIR)/bin/uic
	$(VERBOSE)cd $(BUILD_DIR)/src && \
              sudo make sub-uic-install_subtargets

install: $(INSTALL_LOCATION)/bin/moc \
         $(INSTALL_LOCATION)/bin/rcc \
         $(INSTALL_LOCATION)/bin/uic

clean:
	rm -rf $(BUILD_DIR)