#!/usr/bin/make -f

#
# \brief  Prepare Genode build directory
# \author Christian Helmuth, Norman Feske
# \date   2008-08-14
#

MAKEOVERRIDES =

PLATFORM = $(MAKECMDGOALS)

usage:
	@echo
	@echo "Tool for preparing Genode build directories"
	@echo
	@echo "usage:"
	@echo
	@echo "  create_builddir <platform> [BUILD_DIR=<build-dir>]"
	@echo
	@echo "  <platform>   can be:"
	@echo "                  'linux_x86'"
	@echo "                  'linux_arm'"
	@echo "                  'fiasco_x86'"
	@echo "                  'pistachio_x86'"
	@echo "                  'okl4_x86'"
	@echo "                  'nova_x86_32'"
	@echo "                  'nova_x86_64'"
	@echo "                  'hw_panda'"
	@echo "                  'hw_pbxa9'"
	@echo "                  'hw_imx53_qsb'"
	@echo "                  'hw_imx53_qsb_tz'"
	@echo "                  'hw_usb_armory'"
	@echo "                  'hw_wand_quad'"
	@echo "                  'hw_arndale'"
	@echo "                  'hw_odroid_xu'"
	@echo "                  'hw_rpi'"
	@echo "                  'hw_zynq'"
	@echo "                  'hw_x86_64'"
	@echo "                  'hw_x86_64_muen'"
	@echo "                  'hw_riscv'"
	@echo "                  'foc_x86_32'"
	@echo "                  'foc_x86_64'"
	@echo "                  'foc_pbxa9'"
	@echo "                  'foc_imx53'"
	@echo "                  'foc_panda'"
	@echo "                  'foc_arndale'"
	@echo "                  'foc_rpi'"
	@echo "                  'foc_odroid_x2'"
	@echo "                  'sel4_x86_32'"
	@echo
	@echo "  The definition of BUILD_DIR is optional. If specified,"
	@echo "  <build-dir> is the location of the build directory to create."
	@echo "  If not specified, the build directory will be created at"
	@echo "  <genode-dir>/build/<platform>."
	@echo

#
# Determine Genode base directory based on the known location of the
# 'create_builddir' tool within the Genode source tree
#
GENODE_DIR ?= $(realpath $(dir $(MAKEFILE_LIST))/..)

#
# Define default location of the build directory if not explicitly specified
#
BUILD_DIR ?= $(GENODE_DIR)/build/$(PLATFORM)

SHELL := bash

ifneq ($(wildcard $(BUILD_DIR)),)
$(PLATFORM):: build_dir_already_exists
build_dir_already_exists:
	@echo "Directory $(BUILD_DIR) already exists."
	@false
endif

#
# Convert GENODE_DIR to an absolute directory because the user
# may have specified a '~'-relative location or a pwd-relative
# location.
#
GENODE_ABS_DIR := $(realpath $(shell echo $(GENODE_DIR)))

#
# Define absolute path to the contrib directory as written to the
# 'etc/build.conf' file. We use 'abs_path' instead of 'realpath' because the
# contrib directory may not exist when the build directory is created. In this
# case, 'realpath' would return an empty string.
#
ifeq ($(CONTRIB_DIR),)
CONTRIB_ABS_DIR := $$(GENODE_DIR)/contrib
else
CONTRIB_ABS_DIR := $(abspath $(shell echo $(CONTRIB_DIR)))
endif

#
# Generic build-directory preparation rules
#
BUILD_CONF          = $(GENODE_ABS_DIR)/tool/builddir/etc/build.conf
BUILD_CONF_PLATFORM = $(BUILD_CONF).$(PLATFORM)

$(BUILD_DIR)/etc:
	@mkdir -p $@

$(BUILD_DIR)/etc/build.conf:: $(BUILD_CONF_PLATFORM)
	@echo "GENODE_DIR    = $(GENODE_ABS_DIR)" > $@
	@echo 'BASE_DIR      = $$(GENODE_DIR)/repos/base' >> $@
	@echo 'CONTRIB_DIR   = $(CONTRIB_ABS_DIR)' >> $@

#
# Supply -no-kvm argument to Qemu for kernels that are incompatible with KVM
#
ifeq ($(filter-out foc_x86_32 foc_x86_64 okl4_x86 nova_x86_32 nova_x86_64 pistachio_x86,$(PLATFORM)),)
$(BUILD_DIR)/etc/build.conf::
	@cat $(BUILD_CONF).qemu_no_kvm >> $@
endif

#
# Add 'base-<platform>' and generic repositories
#
$(BUILD_DIR)/etc/build.conf:: $(BUILD_CONF_PLATFORM)
	@echo >> $@
	@echo "##" >> $@
	@echo "## Kernel-specific base repository" >> $@
	@echo "##" >> $@
	@echo >> $@
	@cat $< >> $@
	@cat $(BUILD_CONF).generic >> $@

#
# Add optional repositories for all platforms
#
# Those repositories rely on Genode's libc or contain device drivers.
# Both prerequisites are not available for hybrid Linux/Genode programs.
#
$(BUILD_DIR)/etc/build.conf::
	@cat $(BUILD_CONF).optional >> $@

$(BUILD_DIR)/Makefile:
	@ln -sf $(GENODE_ABS_DIR)/tool/builddir/build.mk $@

#
# Add 'ports-foc' repository to Fiasco.OC build directory
#
ifeq ($(filter-out foc_x86_32 foc_imx53 foc_pbxa9 foc_panda foc_arndale foc_rpi foc_odroid_x2,$(PLATFORM)),)
$(BUILD_DIR)/etc/build.conf::
	@cat $(BUILD_CONF).ports-foc >> $@
endif

#
# Add x86 drivers repositories to x86 build directories
#
ifeq ($(filter-out foc_x86_32 foc_x86_64 okl4_x86 nova_x86_32 nova_x86_64 pistachio_x86 fiasco_x86 hw_x86_64 hw_x86_64_muen sel4_x86_32,$(PLATFORM)),)
$(BUILD_DIR)/etc/build.conf::
	@cat $(BUILD_CONF).drivers_x86 >> $@
endif

#
# Add ARM drivers repositories to ARM build directories
#
ifeq ($(filter-out foc_panda foc_arndale hw_panda hw_arndale hw_odroid_xu foc_imx53 foc_rpi foc_odroid_x2,$(PLATFORM)),)
$(BUILD_DIR)/etc/build.conf::
	@cat $(BUILD_CONF).drivers_arm >> $@
endif

#
# Generic platform dependencies
#
$(PLATFORM):: $(BUILD_DIR)/etc
$(PLATFORM):: $(BUILD_DIR)/etc/build.conf
$(PLATFORM):: $(BUILD_DIR)/Makefile

#
# Platform-specific dependencies
#
foc_x86_32::
	@echo "SPECS  = genode foc_x86_32" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += acpi"             >> $(BUILD_DIR)/etc/specs.conf

foc_x86_64::
	@echo "SPECS = genode foc_x86_64" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += acpi"             >> $(BUILD_DIR)/etc/specs.conf

nova_x86_32::
	@echo "SPECS  = genode nova_x86_32" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += acpi"              >> $(BUILD_DIR)/etc/specs.conf

nova_x86_64::
	@echo "SPECS = genode nova_x86_64" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += acpi"             >> $(BUILD_DIR)/etc/specs.conf

sel4_x86_32::
	@echo "SPECS  = genode sel4_x86_32" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += acpi"              >> $(BUILD_DIR)/etc/specs.conf

foc_pbxa9::
	@echo "SPECS = genode foc_pbxa9" > $(BUILD_DIR)/etc/specs.conf

foc_panda::
	@echo "SPECS = genode foc_panda" > $(BUILD_DIR)/etc/specs.conf

foc_imx53::
	@echo "SPECS = genode foc_imx53" > $(BUILD_DIR)/etc/specs.conf

foc_arndale::
	@echo "SPECS = genode foc_arndale" > $(BUILD_DIR)/etc/specs.conf

foc_rpi::
	@echo "SPECS = genode foc_rpi" > $(BUILD_DIR)/etc/specs.conf

foc_odroid_x2::
	@echo "SPECS = genode foc_odroid_x2" > $(BUILD_DIR)/etc/specs.conf

#
# On all other platforms, the performance counter is assumed to be active by
# default. On HW, its activation is done by an optional core lib. To be
# conform to the other platforms, opt it in by default and, e.g. for security
# reasons, allow users to opt it out.
#
hw_pbxa9::
	@echo "SPECS = genode hw_pbxa9" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_panda::
	@echo "SPECS = genode hw_panda" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_imx53_qsb::
	@echo "SPECS = genode hw_imx53_qsb" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_imx53_qsb_tz::
	@echo "SPECS = genode hw_imx53_qsb_tz" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_usb_armory::
	@echo "SPECS = genode hw_usb_armory" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_wand_quad::
	@echo "SPECS = genode hw_wand_quad" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_arndale::
	@echo "SPECS = genode hw_arndale" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_rpi::
	@echo "SPECS = genode hw_rpi" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_zynq::
	@echo "SPECS = genode hw_zynq" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf
	@echo "## By default, hw_zynq is executed on Qemu" >> $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += zynq_qemu" >> $(BUILD_DIR)/etc/specs.conf

hw_odroid_xu::
	@echo "SPECS = genode hw_odroid_xu" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += perf_counter" >> $(BUILD_DIR)/etc/specs.conf

hw_x86_64::
	@echo "SPECS = genode hw_x86_64" > $(BUILD_DIR)/etc/specs.conf
	@echo "SPECS += acpi" >> $(BUILD_DIR)/etc/specs.conf

hw_x86_64_muen::
	@echo "SPECS = genode hw_x86_64_muen" > $(BUILD_DIR)/etc/specs.conf

hw_riscv::
	@echo "SPECS = genode hw_riscv" > $(BUILD_DIR)/etc/specs.conf

$(PLATFORM)::
	@echo "Successfully created build directory at $(BUILD_DIR)."
	@echo "Please adjust $(BUILD_DIR)/etc/build.conf according to your needs."

.PHONY: $(PLATFORM)