#!/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)
QTBASE_DIR                = $(CONTRIB_DIR)/src/lib/qtbase
QTTOOLS_DIR               = $(CONTRIB_DIR)/src/lib/qttools
TOOL_VERSION              = 20.08
BUILD_DIR                 = $(GENODE_DIR)/build/tool/qt5/$(TOOL_VERSION)
DEFAULT_INSTALL_LOCATION  = /usr/local/genode/qt5/$(TOOL_VERSION)
INSTALL_LOCATION         ?= $(DEFAULT_INSTALL_LOCATION)
SUDO                     ?= sudo

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

QMAKE = $(BUILD_DIR)/qtbase/bin/qmake

# the '-no-strip' option is needed for installing syncqt.pl

$(QMAKE): $(QTBASE_DIR)/configure
	$(VERBOSE)mkdir -p $(BUILD_DIR)/qtbase
	$(VERBOSE)cd $(BUILD_DIR)/qtbase && $(QTBASE_DIR)/configure \
	                                    -opensource \
	                                    -confirm-license \
	                                    -no-iconv \
	                                    -no-zstd \
	                                    -no-opengl \
	                                    -no-strip \
	                                    -prefix $(INSTALL_LOCATION)

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

$(BUILD_DIR)/qtbase/bin/moc: $(BUILD_DIR)/qtbase/src/Makefile
	$(VERBOSE)make -C $(BUILD_DIR)/qtbase/src -j$(MAKE_JOBS) sub-moc

$(BUILD_DIR)/qtbase/bin/rcc: $(BUILD_DIR)/qtbase/src/Makefile
	$(VERBOSE)make -C $(BUILD_DIR)/qtbase/src -j$(MAKE_JOBS) sub-rcc

$(BUILD_DIR)/qtbase/bin/uic: $(BUILD_DIR)/qtbase/src/Makefile
	$(VERBOSE)make -C $(BUILD_DIR)/qtbase/src -j$(MAKE_JOBS) sub-uic

$(BUILD_DIR)/qtbase/bin/qlalr: $(BUILD_DIR)/qtbase/src/Makefile
	$(VERBOSE)make -C $(BUILD_DIR)/qtbase/src -j$(MAKE_JOBS) sub-qlalr

$(BUILD_DIR)/qtbase/bin/qfloat16-tables: $(BUILD_DIR)/qtbase/src/Makefile
	$(VERBOSE)make -C $(BUILD_DIR)/qtbase/src -j$(MAKE_JOBS) sub-qfloat16-tables

$(BUILD_DIR)/qtbase/lib/libQt5Xml.so.5: $(BUILD_DIR)/qtbase/src/Makefile
	$(VERBOSE)make -C $(BUILD_DIR)/qtbase/src -j$(MAKE_JOBS) sub-xml

$(BUILD_DIR)/qttools/Makefile: $(QTTOOLS_DIR)/qttools.pro $(BUILD_DIR)/qtbase/lib/libQt5Xml.so.5
	$(VERBOSE)mkdir -p $(dir $@)
	$(VERBOSE)cd $(dir $@) && $(BUILD_DIR)/qtbase/bin/qmake $<

$(BUILD_DIR)/qttools/src/linguist/lupdate/Makefile: $(QTTOOLS_DIR)/src/linguist/lupdate/lupdate.pro \
                                                    $(BUILD_DIR)/qttools/Makefile
	$(VERBOSE)mkdir -p $(dir $@)
	$(VERBOSE)cd $(dir $@) && $(BUILD_DIR)/qtbase/bin/qmake $<

$(BUILD_DIR)/qttools/bin/lupdate: $(BUILD_DIR)/qttools/src/linguist/lupdate/Makefile
	$(VERBOSE)make -C $(dir $<) -j$(MAKE_JOBS)

$(BUILD_DIR)/qttools/src/linguist/lrelease/Makefile: $(QTTOOLS_DIR)/src/linguist/lrelease/lrelease.pro \
                                                     $(BUILD_DIR)/qttools/Makefile
	$(VERBOSE)mkdir -p $(dir $@)
	$(VERBOSE)cd $(dir $@) && $(BUILD_DIR)/qtbase/bin/qmake $<

$(BUILD_DIR)/qttools/bin/lrelease: $(BUILD_DIR)/qttools/src/linguist/lrelease/Makefile
	$(VERBOSE)make -C $(dir $<) -j$(MAKE_JOBS)

build: $(BUILD_DIR)/qtbase/bin/moc \
       $(BUILD_DIR)/qtbase/bin/rcc \
       $(BUILD_DIR)/qtbase/bin/uic \
       $(BUILD_DIR)/qtbase/bin/qlalr \
       $(BUILD_DIR)/qtbase/bin/qfloat16-tables \
       $(BUILD_DIR)/qttools/bin/lupdate \
       $(BUILD_DIR)/qttools/bin/lrelease

$(INSTALL_LOCATION)/bin:
	$(VERBOSE)$(SUDO) mkdir -p $@

$(INSTALL_LOCATION)/lib:
	$(VERBOSE)$(SUDO) mkdir -p $@

$(INSTALL_LOCATION)/lib/libQt5Core.so.5: $(BUILD_DIR)/qtbase/lib/libQt5Core.so.5 $(INSTALL_LOCATION)/lib
	$(VERBOSE)$(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/lib/libQt5Xml.so.5: $(BUILD_DIR)/qtbase/lib/libQt5Xml.so.5
	$(VERBOSE)$(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/moc: $(BUILD_DIR)/qtbase/bin/moc $(INSTALL_LOCATION)/bin
	$(VERBOSE)cd $(BUILD_DIR)/qtbase/src && \
              $(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/rcc: $(BUILD_DIR)/qtbase/bin/rcc $(INSTALL_LOCATION)/bin
	$(VERBOSE)cd $(BUILD_DIR)/qtbase/src && \
              $(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/uic: $(BUILD_DIR)/qtbase/bin/uic $(INSTALL_LOCATION)/bin $(INSTALL_LOCATION)/lib/libQt5Core.so.5
	$(VERBOSE)cd $(BUILD_DIR)/qtbase/src && \
              $(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/qlalr: $(BUILD_DIR)/qtbase/bin/qlalr $(INSTALL_LOCATION)/bin $(INSTALL_LOCATION)/lib/libQt5Core.so.5
	$(VERBOSE)cd $(BUILD_DIR)/qtbase/src && \
              $(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/qfloat16-tables: $(BUILD_DIR)/qtbase/bin/qfloat16-tables $(INSTALL_LOCATION)/bin
	$(VERBOSE)$(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/qmake: $(BUILD_DIR)/qtbase/bin/qmake $(INSTALL_LOCATION)/bin
	$(VERBOSE)$(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/syncqt.pl: $(INSTALL_LOCATION)/bin
	$(VERBOSE)$(SUDO) make -C $(BUILD_DIR)/qtbase install_syncqt

$(INSTALL_LOCATION)/bin/lupdate: $(BUILD_DIR)/qttools/bin/lupdate $(INSTALL_LOCATION)/bin $(INSTALL_LOCATION)/lib/libQt5Core.so.5 $(INSTALL_LOCATION)/lib/libQt5Xml.so.5
	$(VERBOSE)$(SUDO) strip $< -o $@

$(INSTALL_LOCATION)/bin/lrelease: $(BUILD_DIR)/qttools/bin/lrelease $(INSTALL_LOCATION)/bin $(INSTALL_LOCATION)/lib/libQt5Core.so.5 $(INSTALL_LOCATION)/lib/libQt5Xml.so.5
	$(VERBOSE)$(SUDO) strip $< -o $@

install: $(INSTALL_LOCATION)/bin/moc \
         $(INSTALL_LOCATION)/bin/rcc \
         $(INSTALL_LOCATION)/bin/uic \
         $(INSTALL_LOCATION)/bin/qlalr \
         $(INSTALL_LOCATION)/bin/qfloat16-tables \
         $(INSTALL_LOCATION)/bin/qmake \
         $(INSTALL_LOCATION)/bin/syncqt.pl \
         $(INSTALL_LOCATION)/bin/lupdate \
         $(INSTALL_LOCATION)/bin/lrelease
ifeq ($(INSTALL_LOCATION),$(DEFAULT_INSTALL_LOCATION))
	$(VERBOSE)$(SUDO) ln -snf $(TOOL_VERSION) $(dir $(INSTALL_LOCATION))/current
endif

clean:
	rm -rf $(BUILD_DIR)
