#
# Copyright (c) 2006 Martin Decky
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# - The name of the author may not be used to endorse or promote products
#   derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

## Configuration
#

ROOT_PATH = ..

VERSION_DEF = $(ROOT_PATH)/version

COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
COMMON_HEADER = $(ROOT_PATH)/common.h
COMMON_HEADER_ARCH = arch/$(BARCH)/include/common.h

CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
CONFIG_HEADER = $(ROOT_PATH)/config.h

-include $(VERSION_DEF)
-include $(COMMON_MAKEFILE)
-include $(CONFIG_MAKEFILE)

KERNEL_PATH = $(ROOT_PATH)/kernel
USPACE_PATH = $(ROOT_PATH)/uspace
DIST_PATH = $(USPACE_PATH)/dist
TOOLS_PATH = $(ROOT_PATH)/tools
DRVS_PATH = drv

SANDBOX = pack
PACK = $(TOOLS_PATH)/pack.py
MKHORD = $(TOOLS_PATH)/mkhord.py
MKTMPFS = $(TOOLS_PATH)/mktmpfs.py
MKFAT = $(TOOLS_PATH)/mkfat.py
MKEXT2 = $(TOOLS_PATH)/mkext2.py
MKUIMAGE = $(TOOLS_PATH)/mkuimage.py

JOBFILE = $(ROOT_PATH)/tools/jobfile.py

ARCH_INCLUDE = generic/include/arch
GENARCH_INCLUDE = generic/include/genarch

DEPEND = Makefile.depend
DEPEND_PREV = $(DEPEND).prev
DISTROOT = distroot
INITRD = initrd

INIT_TASKS = \
	$(USPACE_PATH)/srv/ns/ns \
	$(USPACE_PATH)/srv/loader/loader \
	$(USPACE_PATH)/app/init/init \
	$(USPACE_PATH)/srv/loc/loc \
	$(USPACE_PATH)/srv/bd/rd/rd \
	$(USPACE_PATH)/srv/vfs/vfs

ifeq ($(RDFMT),tmpfs)
	INIT_TASKS += $(USPACE_PATH)/srv/fs/tmpfs/tmpfs
endif

ifeq ($(RDFMT),fat)
	INIT_TASKS += $(USPACE_PATH)/srv/fs/fat/fat
endif

ifeq ($(RDFMT),ext2fs)
	INIT_TASKS += $(USPACE_PATH)/srv/fs/ext2fs/ext2fs
endif

RD_SRVS_ESSENTIAL = \
	$(USPACE_PATH)/srv/hid/fb/fb \
	$(USPACE_PATH)/srv/hid/input/input \
	$(USPACE_PATH)/srv/hid/console/console \
	$(USPACE_PATH)/srv/fs/locfs/locfs

RD_SRVS_NON_ESSENTIAL = \
	$(USPACE_PATH)/srv/bd/file_bd/file_bd \
	$(USPACE_PATH)/srv/bd/part/guid_part/g_part \
	$(USPACE_PATH)/srv/bd/part/mbr_part/mbr_part \
	$(USPACE_PATH)/srv/clip/clip \
	$(USPACE_PATH)/srv/fs/tmpfs/tmpfs \
	$(USPACE_PATH)/srv/fs/fat/fat \
	$(USPACE_PATH)/srv/fs/mfs/mfs \
	$(USPACE_PATH)/srv/fs/cdfs/cdfs \
	$(USPACE_PATH)/srv/fs/exfat/exfat \
	$(USPACE_PATH)/srv/fs/ext2fs/ext2fs \
	$(USPACE_PATH)/srv/taskmon/taskmon \
	$(USPACE_PATH)/srv/hw/netif/ne2000/ne2000 \
	$(USPACE_PATH)/srv/net/netif/lo/lo \
	$(USPACE_PATH)/srv/net/nil/eth/eth \
	$(USPACE_PATH)/srv/net/nil/nildummy/nildummy \
	$(USPACE_PATH)/srv/net/il/arp/arp \
	$(USPACE_PATH)/srv/net/il/ip/ip \
	$(USPACE_PATH)/srv/net/tl/icmp/icmp \
	$(USPACE_PATH)/srv/net/tl/udp/udp \
	$(USPACE_PATH)/srv/net/tl/tcp/tcp \
	$(USPACE_PATH)/srv/net/net/net \
	$(USPACE_PATH)/srv/devman/devman

RD_DRVS = \
	infrastructure/root \
	infrastructure/rootvirt \
	test/test1 \
	test/test2 \
	test/test3

RD_DRV_CFG =

RD_LIBS =

ifeq ($(CONFIG_DEVEL_FILES), y)
	RD_LIBS += \
		$(USPACE_PATH)/lib/c/libc.a \
		$(USPACE_PATH)/lib/softint/libsoftint.a \
		$(USPACE_PATH)/lib/softfloat/libsoftfloat.a
endif

ifeq ($(CONFIG_BUILD_SHARED_LIBS), y)
	RD_LIBS += \
		$(USPACE_PATH)/lib/c/libc.so0 \
		$(USPACE_PATH)/lib/softint/libsofti.so0
	RD_APPS += \
		$(USPACE_PATH)/app/dltest/dltest
endif

RD_APPS_ESSENTIAL = \
	$(USPACE_PATH)/app/bdsh/bdsh \
	$(USPACE_PATH)/app/getterm/getterm \
	$(USPACE_PATH)/app/klog/klog

RD_APPS_NON_ESSENTIAL = \
	$(USPACE_PATH)/app/blkdump/blkdump \
	$(USPACE_PATH)/app/bnchmark/bnchmark \
	$(USPACE_PATH)/app/devctl/devctl \
	$(USPACE_PATH)/app/dltest/dltest \
	$(USPACE_PATH)/app/dltest2/dltest2 \
	$(USPACE_PATH)/app/dload/dload \
	$(USPACE_PATH)/app/edit/edit \
	$(USPACE_PATH)/app/ext2info/ext2info \
	$(USPACE_PATH)/app/kill/kill \
	$(USPACE_PATH)/app/killall/killall \
	$(USPACE_PATH)/app/locinfo/locinfo \
	$(USPACE_PATH)/app/mkfat/mkfat \
	$(USPACE_PATH)/app/mkmfs/mkmfs \
	$(USPACE_PATH)/app/lsusb/lsusb \
	$(USPACE_PATH)/app/sbi/sbi \
	$(USPACE_PATH)/app/redir/redir \
	$(USPACE_PATH)/app/taskdump/taskdump \
	$(USPACE_PATH)/app/tester/tester \
	$(USPACE_PATH)/app/testread/testread \
	$(USPACE_PATH)/app/tetris/tetris \
	$(USPACE_PATH)/app/trace/trace \
	$(USPACE_PATH)/app/nettest1/nettest1 \
	$(USPACE_PATH)/app/nettest2/nettest2 \
	$(USPACE_PATH)/app/netecho/netecho \
	$(USPACE_PATH)/app/ping/ping \
	$(USPACE_PATH)/app/stats/stats \
	$(USPACE_PATH)/app/sysinfo/sysinfo \
	$(USPACE_PATH)/app/top/top \
	$(USPACE_PATH)/app/usbinfo/usbinfo \
	$(USPACE_PATH)/app/vuhid/vuh \
	$(USPACE_PATH)/app/mkbd/mkbd \
	$(USPACE_PATH)/app/websrv/websrv

ifeq ($(CONFIG_PCC),y)
RD_APPS_NON_ESSENTIAL += \
	$(USPACE_PATH)/app/cc/cc \
	$(USPACE_PATH)/app/ccom/ccom \
	$(USPACE_PATH)/app/ccom/mkext/cc_mkext \
	$(USPACE_PATH)/app/cpp/cpp
endif

ifeq ($(CONFIG_BINUTILS),y)
RD_APPS_NON_ESSENTIAL += \
	$(USPACE_PATH)/app/binutils/bin/as \
	$(USPACE_PATH)/app/binutils/bin/ld
endif

ifneq ($(CONFIG_BAREBONE),y)
NET_CFG = \
	$(USPACE_PATH)/srv/net/cfg/general \
	$(USPACE_PATH)/srv/net/cfg/lo \
	$(USPACE_PATH)/srv/net/cfg/ne2k
endif

COMPONENTS = \
	$(KERNEL_PATH)/kernel.bin \
	$(INIT_TASKS) \
	$(INITRD).img

LINK = arch/$(BARCH)/_link.ld
COMPS_H = arch/$(BARCH)/include/_components.h
COMPS_C = arch/$(BARCH)/src/_components.c

-include arch/$(BARCH)/Makefile.inc

ifeq ($(CONFIG_BAREBONE),y)
RD_SRVS = $(RD_SRVS_ESSENTIAL)
RD_APPS = $(RD_APPS_ESSENTIAL)
else
RD_SRVS = $(RD_SRVS_ESSENTIAL) $(RD_SRVS_NON_ESSENTIAL)
RD_APPS = $(RD_APPS_ESSENTIAL) $(RD_APPS_NON_ESSENTIAL)
endif

MODULES := $(notdir $(COMPONENTS))

BOOT_OUTPUT ?= $(ROOT_PATH)/image.boot
RAW ?= image.raw
JOB ?= image.job
MAP ?= image.map
PREBUILD ?= build_comps
BUILD ?= Makefile.build
